*** 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 methdod (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-suffix "SCORE"
200   "*Suffix of the score files.")
201
202 (defvar gnus-score-interactive-default-score 1000
203   "*Scoring commands will raise/lower the score with this number as the default.")
204
205 (defvar gnus-large-newsgroup 200
206   "*The number of articles which indicates a large newsgroup.
207 If the number of articles in a newsgroup is greater than this value,
208 confirmation is required for selecting the newsgroup.")
209
210 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
211 (defvar gnus-no-groups-message "No news is horrible news"
212   "*Message displayed by Gnus when no groups are available.")
213
214 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
215   "*Non-nil means that the default name of a file to save articles in is the group name.
216 If it's nil, the directory form of the group name is used instead.
217
218 If this variable is a list, and the list contains the element
219 `not-score', long file names will not be used for score files; if it
220 contains the element `not-save', long file names will not be used for
221 saving; and if it contains the element `not-kill', long file names
222 will not be used for kill files.")
223
224 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
225   "*Name of the directory articles will be saved in (default \"~/News\").
226 Initialized from the SAVEDIR environment variable.")
227
228 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
229   "*Name of the directory where kill files will be stored (default \"~/News\").
230 Initialized from the SAVEDIR environment variable.")
231
232 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
233   "*A function to save articles in your favorite format.
234 The function must be interactively callable (in other words, it must
235 be an Emacs command).
236
237 Gnus provides the following functions:
238
239 * gnus-summary-save-in-rmail (Rmail format)
240 * gnus-summary-save-in-mail (Unix mail format)
241 * gnus-summary-save-in-folder (MH folder)
242 * gnus-summary-save-in-file (article format).
243 * gnus-summary-save-in-vm (use VM's folder format).")
244
245 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
246   "*A function generating a file name to save articles in Rmail format.
247 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
248
249 (defvar gnus-mail-save-name (function gnus-plain-save-name)
250   "*A function generating a file name to save articles in Unix mail format.
251 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
252
253 (defvar gnus-folder-save-name (function gnus-folder-save-name)
254   "*A function generating a file name to save articles in MH folder.
255 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
256
257 (defvar gnus-file-save-name (function gnus-numeric-save-name)
258   "*A function generating a file name to save articles in article format.
259 The function is called with NEWSGROUP, HEADERS, and optional
260 LAST-FILE.")
261
262 (defvar gnus-split-methods nil
263   "*Variable used to suggest where articles are to be saved.
264 The syntax of this variable is the same as `nnmail-split-methods'.  
265
266 For instance, if you would like to save articles related to Gnus in
267 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
268 you could set this variable to something like:
269
270  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
271    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
272
273 (defvar gnus-save-score nil
274   "*If non-nil, save group scoring info.")
275
276 (defvar gnus-use-adaptive-scoring nil
277   "*If non-nil, use some adaptive scoring scheme.")
278
279 (defvar gnus-use-cache nil
280   "*If non-nil, Gnus will cache (some) articles locally.")
281
282 (defvar gnus-use-scoring t
283   "*If non-nil, enable scoring.")
284
285 (defvar gnus-fetch-old-headers nil
286   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
287 If an unread article in the group refers to an older, already read (or
288 just marked as read) article, the old article will not normally be
289 displayed in the Summary buffer.  If this variable is non-nil, Gnus
290 will attempt to grab the headers to the old articles, and thereby
291 build complete threads.  If it has the value `some', only enough
292 headers to connect otherwise loose threads will be displayed.
293
294 The server has to support XOVER for any of this to work.")
295
296 (defvar gnus-visual t
297   "*If non-nil, will do various highlighting.
298 If nil, no mouse highlights (or any other highlights) will be
299 performed.  This might speed up Gnus some when generating large group
300 and summary buffers.")
301
302 (defvar gnus-novice-user t
303   "*Non-nil means that you are a usenet novice.
304 If non-nil, verbose messages may be displayed and confirmations may be
305 required.")
306
307 (defvar gnus-expert-user nil
308   "*Non-nil means that you will never be asked for confirmation about anything.
309 And that means *anything*.")
310
311 (defvar gnus-verbose 7
312   "*Integer that says how verbose Gnus should be.
313 The higher the number, the more messages Gnus will flash to say what
314 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
315 display most important messages; and at ten, Gnus will keep on
316 jabbering all the time.")
317
318 (defvar gnus-keep-same-level nil
319   "*Non-nil means that the next newsgroup after the current will be on the same level.
320 When you type, for instance, `n' after reading the last article in the
321 current newsgroup, you will go to the next newsgroup. If this variable
322 is nil, the next newsgroup will be the next from the group
323 buffer. 
324 If this variable is non-nil, Gnus will either put you in the
325 next newsgroup with the same level, or, if no such newsgroup is
326 available, the next newsgroup with the lowest possible level higher
327 than the current level.
328 If this variable is `best', Gnus will make the next newsgroup the one
329 with the best level.")
330
331 (defvar gnus-summary-make-false-root 'adopt
332   "*nil means that Gnus won't gather loose threads.
333 If the root of a thread has expired or been read in a previous
334 session, the information necessary to build a complete thread has been
335 lost. Instead of having many small sub-threads from this original thread
336 scattered all over the summary buffer, Gnus can gather them. 
337
338 If non-nil, Gnus will try to gather all loose sub-threads from an
339 original thread into one large thread.
340
341 If this variable is non-nil, it should be one of `none', `adopt',
342 `dummy' or `empty'.
343
344 If this variable is `none', Gnus will not make a false root, but just
345 present the sub-threads after another.
346 If this variable is `dummy', Gnus will create a dummy root that will
347 have all the sub-threads as children.
348 If this variable is `adopt', Gnus will make one of the \"children\"
349 the parent and mark all the step-children as such.
350 If this variable is `empty', the \"children\" are printed with empty
351 subject fields.  (Or rather, they will be printed with a string
352 given by the `gnus-summary-same-subject' variable.)")
353
354 (defvar gnus-summary-gather-subject-limit nil
355   "*Maximum length of subject comparisons when gathering loose threads.
356 Use nil to compare full subjects.  Setting this variable to a low
357 number will help gather threads that have been corrupted by
358 newsreaders chopping off subject lines, but it might also mean that
359 unrelated articles that have subject that happen to begin with the
360 same few characters will be incorrectly gathered.
361
362 If this variable is `fuzzy', Gnus will use a fuzzy algortihm when
363 comparing subjects.")
364
365 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
366 (defvar gnus-summary-same-subject ""
367   "*String indicating that the current article has the same subject as the previous.
368 This variable will only be used if the value of
369 `gnus-summary-make-false-root' is `empty'.")
370
371 (defvar gnus-summary-goto-unread nil
372   "*If non-nil, marking commands will go to the next unread article.")
373
374 (defvar gnus-group-goto-unread t
375   "*If non-nil, movement commands will go to the next unread and subscribed group.")
376
377 (defvar gnus-check-new-newsgroups t
378   "*Non-nil means that Gnus will add new newsgroups at startup.
379 If this variable is `ask-server', Gnus will ask the server for new
380 groups since the last time it checked. This means that the killed list
381 is no longer necessary, so you could set `gnus-save-killed-list' to
382 nil. 
383
384 A variant is to have this variable be a list of select methods. Gnus
385 will then use the `ask-server' method on all these select methods to
386 query for new groups from all those servers.
387
388 Eg.
389   (setq gnus-check-new-newsgroups 
390         '((nntp \"some.server\") (nntp \"other.server\")))
391
392 If this variable is nil, then you have to tell Gnus explicitly to
393 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
394
395 (defvar gnus-check-bogus-newsgroups nil
396   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
397 If this variable is nil, then you have to tell Gnus explicitly to
398 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
399
400 (defvar gnus-read-active-file t
401   "*Non-nil means that Gnus will read the entire active file at startup.
402 If this variable is nil, Gnus will only know about the groups in your
403 `.newsrc' file.
404
405 If this variable is `some', Gnus will try to only read the relevant
406 parts of the active file from the server.  Not all servers support
407 this, and it might be quite slow with other servers, but this should
408 generally be faster than both the t and nil value.
409
410 If you set this variable to nil or `some', you probably still want to
411 be told about new newsgroups that arrive.  To do that, set
412 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
413 properly with all servers.")
414
415 (defvar gnus-level-subscribed 5
416   "*Groups with levels less than or equal to this variable are subscribed.")
417
418 (defvar gnus-level-unsubscribed 7
419   "*Groups with levels less than or equal to this variable are unsubscribed.
420 Groups with levels less than `gnus-level-subscribed', which should be
421 less than this variable, are subscribed.")
422
423 (defvar gnus-level-zombie 8
424   "*Groups with this level are zombie groups.")
425
426 (defvar gnus-level-killed 9
427   "*Groups with this level are killed.")
428
429 (defvar gnus-level-default-subscribed 3
430   "*New subscribed groups will be subscribed at this level.")
431
432 (defvar gnus-level-default-unsubscribed 6
433   "*New unsubscribed groups will be unsubscribed at this level.")
434
435 (defvar gnus-activate-foreign-newsgroups nil
436   "*If nil, Gnus will not check foreign newsgroups at startup.
437 If it is non-nil, it should be a number between one and nine. Foreign
438 newsgroups that have a level lower or equal to this number will be
439 activated on startup. For instance, if you want to active all
440 subscribed newsgroups, but not the rest, you'd set this variable to 
441 `gnus-level-subscribed'.
442
443 If you subscribe to lots of newsgroups from different servers, startup
444 might take a while. By setting this variable to nil, you'll save time,
445 but you won't be told how many unread articles there are in the
446 groups.")
447
448 (defvar gnus-save-newsrc-file t
449   "*Non-nil means that Gnus will save the `.newsrc' file.
450 Gnus always saves its own startup file, which is called
451 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
452 be readily understood by other newsreaders.  If you don't plan on
453 using other newsreaders, set this variable to nil to save some time on
454 exit.")
455
456 (defvar gnus-save-killed-list t
457   "*If non-nil, save the list of killed groups to the startup file.
458 This will save both time (when starting and quitting) and space (both
459 memory and disk), but it will also mean that Gnus has no record of
460 which groups are new and which are old, so the automatic new
461 newsgroups subscription methods become meaningless. You should always
462 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
463 variable to nil.")
464
465 (defvar gnus-interactive-catchup t
466   "*If non-nil, require your confirmation when catching up a group.")
467
468 (defvar gnus-interactive-post t
469   "*If non-nil, group name will be asked for when posting.")
470
471 (defvar gnus-interactive-exit t
472   "*If non-nil, require your confirmation when exiting Gnus.")
473
474 (defvar gnus-kill-killed nil
475   "*If non-nil, Gnus will apply kill files to already killed articles.
476 If it is nil, Gnus will never apply kill files to articles that have
477 already been through the scoring process, which might very well save lots
478 of time.")
479
480 (defvar gnus-extract-address-components 'gnus-extract-address-components
481   "*Function for extracting address components from a From header.
482 Two pre-defined function exist: `gnus-extract-address-components',
483 which is the default, quite fast, and too simplistic solution, and
484 `mail-extract-address-components', which works much better, but is
485 slower.")
486
487 (defvar gnus-summary-default-score 0
488   "*Default article score level.
489 If this variable is nil, scoring will be disabled.")
490
491 (defvar gnus-group-default-list-level gnus-level-subscribed
492   "*Default listing level. 
493 Ignored if `gnus-group-use-permanent-levels' is nil.")
494
495 (defvar gnus-group-use-permanent-levels nil
496   "*If non-nil, once you set a level, Gnus will use this level.")
497
498 (defvar gnus-show-mime nil
499   "*If non-ni, do mime processing of articles.
500 The articles will simply be fed to the function given by
501 `gnus-show-mime-method'.")
502
503 (defvar gnus-show-mime-method (function metamail-buffer)
504   "*Function to process a MIME message.
505 The function is called from the article buffer.")
506
507 (defvar gnus-show-threads t
508   "*If non-nil, display threads in summary mode.")
509
510 (defvar gnus-thread-hide-subtree nil
511   "*If non-nil, hide all threads initially.
512 If threads are hidden, you have to run the command
513 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
514 to expose hidden threads.")
515
516 (defvar gnus-thread-hide-killed t
517   "*If non-nil, hide killed threads automatically.")
518
519 (defvar gnus-thread-ignore-subject nil
520   "*If non-nil, ignore subjects and do all threading based on the Reference header.
521 If nil, which is the default, articles that have different subjects
522 from their parents will start separate threads.")
523
524 (defvar gnus-thread-indent-level 4
525   "*Number that says how much each sub-thread should be indented.")
526
527 (defvar gnus-ignored-newsgroups ""
528   "*A regexp to match uninteresting newsgroups in the active file.
529 Any lines in the active file matching this regular expression are
530 removed from the newsgroup list before anything else is done to it,
531 thus making them effectively non-existant.")
532
533 (defvar gnus-ignored-headers
534   "^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:"
535   "*All headers that match this regexp will be hidden.
536 Also see `gnus-visible-headers'.")
537
538 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
539   "*All headers that do not match this regexp will be hidden.
540 Also see `gnus-ignored-headers'.")
541
542 (defvar gnus-sorted-header-list
543   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
544     "^Cc:" "^Date:" "^Organization:")
545   "*This variable is a list of regular expressions.
546 If it is non-nil, headers that match the regular expressions will
547 be placed first in the article buffer in the sequence specified by
548 this list.")
549
550 (defvar gnus-show-all-headers nil
551   "*If non-nil, don't hide any headers.")
552
553 (defvar gnus-save-all-headers t
554   "*If non-nil, don't remove any headers before saving.")
555
556 (defvar gnus-inhibit-startup-message nil
557   "*If non-nil, the startup message will not be displayed.")
558
559 (defvar gnus-auto-extend-newsgroup t
560   "*If non-nil, extend newsgroup forward and backward when requested.")
561
562 (defvar gnus-auto-select-first t
563   "*If non-nil, select the first unread article when entering a group.
564 If you want to prevent automatic selection of the first unread article
565 in some newsgroups, set the variable to nil in
566 `gnus-select-group-hook'.") 
567
568 (defvar gnus-auto-select-next t
569   "*If non-nil, offer to go to the next group from the end of the previous.
570 If the value is t and the next newsgroup is empty, Gnus will exit
571 summary mode and go back to group mode.  If the value is neither nil
572 nor t, Gnus will select the following unread newsgroup.  In
573 particular, if the value is the symbol `quietly', the next unread
574 newsgroup will be selected without any confirmations.")
575
576 (defvar gnus-auto-select-same nil
577   "*If non-nil, select the next article with the same subject.")
578
579 (defvar gnus-summary-check-current nil
580   "*If non-nil, consider the current article when moving.
581 The \"unread\" movement commands will stay on the same line if the
582 current article is unread.")
583
584 (defvar gnus-auto-center-summary t
585   "*If non-nil, always center the current summary buffer.")
586
587 (defvar gnus-break-pages t
588   "*If non-nil, do page breaking on articles.
589 The page delimiter is specified by the `gnus-page-delimiter'
590 variable.")
591
592 (defvar gnus-page-delimiter "^\^L"
593   "*Regexp describing what to use as article page delimiters.
594 The default value is \"^\^L\", which is a form linefeed at the
595 beginning of a line.")
596
597 (defvar gnus-use-full-window t
598   "*If non-nil, use the entire Emacs screen.")
599
600 (defvar gnus-window-configuration nil
601   "Obsolete variable.  See `gnus-buffer-configuration'.")
602
603 (defvar gnus-buffer-configuration
604   '((group ([group 1.0 point] 
605             (if gnus-carpal [group-carpal 4])))
606     (summary ([summary 1.0 point]
607               (if gnus-carpal [summary-carpal 4])))
608     (article ([summary 0.25 point] 
609               (if gnus-carpal [summary-carpal 4]) 
610               [article 1.0]))
611     (server ([server 1.0 point]
612              (if gnus-carpal [server-carpal 2])))
613     (browse ([browse 1.0 point]
614              (if gnus-carpal [browse-carpal 2])))
615     (group-mail ([mail 1.0 point]))
616     (summary-mail ([mail 1.0 point]))
617     (summary-reply ([article 0.5]
618                     [mail 1.0 point]))
619     (info ([nil 1.0 point]))
620     (summary-faq ([summary 0.25]
621                   [article 1.0 point]))
622     (edit-group ([group 0.5]
623                  [edit-group 1.0 point]))
624     (edit-server ([server 0.5]
625                   [edit-server 1.0 point]))
626     (post ([post 1.0 point]))
627     (reply ([article 0.5]
628             [mail 1.0 point]))
629     (mail-forward ([mail 1.0 point]))
630     (post-forward ([post 1.0 point]))
631     (reply-yank ([mail 1.0 point]))
632     (followup ([article 0.5]
633                [post 1.0 point]))
634     (followup-yank ([post 1.0 point])))
635   "Window configuration for all possible Gnus buffers.
636 This variable is a list of lists.  Each of these lists has a NAME and
637 a RULE.  The NAMEs are commonsense names like `group', which names a
638 rule used when displaying the group buffer; `summary', which names a
639 rule for what happens when you enter a group and do not display an
640 article buffer; and so on.  See the value of this variable for a
641 complete list of NAMEs.
642
643 Each RULE is a list of vectors.  The first element in this vector is
644 the name of the buffer to be displayed; the second element is the
645 percentage of the screen this buffer is to occupy (a number in the
646 0.0-0.99 range); the optional third element is `point', which should
647 be present to denote which buffer point is to go to after making this
648 buffer configuration.")
649
650 (defvar gnus-window-to-buffer
651   '((group . gnus-group-buffer)
652     (summary . gnus-summary-buffer)
653     (article . gnus-article-buffer)
654     (server . gnus-server-buffer)
655     (browse . "*Gnus Browse Server*")
656     (edit-group . gnus-group-edit-buffer)
657     (edit-server . gnus-server-edit-buffer)
658     (group-carpal . gnus-carpal-group-buffer)
659     (summary-carpal . gnus-carpal-summary-buffer)
660     (server-carpal . gnus-carpal-server-buffer)
661     (browse-carpal . gnus-carpal-browse-buffer)
662     (mail . "*mail*")
663     (post . gnus-post-news-buffer))
664   "Mapping from short symbols to buffer names or buffer variables.")
665
666 (defvar gnus-carpal nil
667   "*If non-nil, display clickable icons.")
668
669 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
670   "*Function called with a group name when new group is detected.
671 A few pre-made functions are supplied: `gnus-subscribe-randomly'
672 inserts new groups at the beginning of the list of groups;
673 `gnus-subscribe-alphabetically' inserts new groups in strict
674 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
675 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
676 for your decision.")
677
678 ;; Suggested by a bug report by Hallvard B Furuseth.
679 ;; <h.b.furuseth@usit.uio.no>. 
680 (defvar gnus-subscribe-options-newsgroup-method
681   (function gnus-subscribe-alphabetically)
682   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
683 If, for instance, you want to subscribe to all newsgroups in the
684 \"no\" and \"alt\" hierarchies, you'd put the following in your
685 .newsrc file:
686
687 options -n no.all alt.all
688
689 Gnus will the subscribe all new newsgroups in these hierarchies with
690 the subscription method in this variable.")
691
692 (defvar gnus-subscribe-hierarchical-interactive nil
693   "*If non-nil, Gnus will offer to subscribe hierarchically.
694 When a new hierarchy appears, Gnus will ask the user:
695
696 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
697
698 If the user pressed `d', Gnus will descend the hierarchy, `y' will
699 subscribe to all newsgroups in the hierarchy and `s' will skip this
700 hierarchy in its entirety.")
701
702 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
703   "*Function used for sorting the group buffer.
704 This function will be called with group info entries as the arguments
705 for the groups to be sorted.  Pre-made functions include
706 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
707 `gnus-sort-by-level'")
708
709 ;; Mark variables suggested by Thomas Michanek
710 ;; <Thomas.Michanek@telelogic.se>. 
711 (defvar gnus-unread-mark ? 
712   "*Mark used for unread articles.")
713 (defvar gnus-ticked-mark ?!
714   "*Mark used for ticked articles.")
715 (defvar gnus-dormant-mark ??
716   "*Mark used for dormant articles.")
717 (defvar gnus-del-mark ?D
718   "*Mark used for del'd articles.")
719 (defvar gnus-read-mark ?d
720   "*Mark used for read articles.")
721 (defvar gnus-expirable-mark ?E
722   "*Mark used for expirable articles.")
723 (defvar gnus-killed-mark ?K
724   "*Mark used for killed articles.")
725 (defvar gnus-kill-file-mark ?X
726   "*Mark used for articles killed by kill files.")
727 (defvar gnus-low-score-mark ?Y
728   "*Mark used for articles with a low score.")
729 (defvar gnus-catchup-mark ?C
730   "*Mark used for articles that are caught up.")
731 (defvar gnus-replied-mark ?R
732   "*Mark used for articles that have been replied to.")
733 (defvar gnus-process-mark ?# 
734   "*Process mark.")
735 (defvar gnus-ancient-mark ?A
736   "*Mark used for ancient articles.")
737 (defvar gnus-canceled-mark ?G
738   "*Mark used for cancelled articles.")
739 (defvar gnus-score-over-mark ?+
740   "*Score mark used for articles with high scores.")
741 (defvar gnus-score-below-mark ?-
742   "*Score mark used for articles with low scores.")
743 (defvar gnus-empty-thread-mark ? 
744   "*There is no thread under the article.")
745 (defvar gnus-not-empty-thread-mark ?=
746   "*There is a thread under the article.")
747
748 (defvar gnus-view-pseudo-asynchronously nil
749   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
750
751 (defvar gnus-view-pseudos nil
752   "*If `automatic', pseudo-articles will be viewed automatically.
753 If `not-confirm', pseudos will be viewed automatically, and the user
754 will not be asked to confirm the command.")
755
756 (defvar gnus-view-pseudos-separately t
757   "*If non-nil, one pseudo-article will be created for each file to be viewed.
758 If nil, all files that use the same viewing command will be given as a
759 list of parameters to that command.")
760
761 (defvar gnus-group-line-format "%M%S%5y: %(%g%)\n"
762   "*Format of group lines.
763 It works along the same lines as a normal formatting string,
764 with some simple extensions.
765
766 %M    Only marked articles (character, \"*\" or \" \")
767 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
768 %L    Level of subscribedness (integer)
769 %N    Number of unread articles (integer)
770 %I    Number of dormant articles (integer)
771 %i    Number of ticked and dormant (integer)
772 %T    Number of ticked articles (integer)
773 %R    Number of read articles (integer)
774 %t    Total number of articles (integer)
775 %y    Number of unread, unticked articles (integer)
776 %G    Group name (string)
777 %g    Qualified group name (string)
778 %D    Group description (string)
779 %s    Select method (string)
780 %o    Moderated group (char, \"m\")
781 %O    Moderated group (string, \"(m)\" or \"\")
782 %n    Select from where (string)
783 %z    A string that look like `<%s:%n>' if a foreign select method is used
784 %u    User defined specifier. The next character in the format string should
785       be a letter.  Gnus will call the function gnus-user-format-function-X,
786       where X is the letter following %u. The function will be passed the
787       current header as argument. The function should return a string, which
788       will be inserted into the buffer just like information from any other
789       group specifier.
790
791 Text between %( and %) will be highlighted with `gnus-mouse-face' when
792 the mouse point move inside the area.  There can only be one such area.
793
794 Note that this format specification is not always respected. For
795 reasons of efficiency, when listing killed groups, this specification
796 is ignored altogether. If the spec is changed considerably, your
797 output may end up looking strange when listing both alive and killed
798 groups.
799
800 If you use %o or %O, reading the active file will be slower and quite
801 a bit of extra memory will be used. %D will also worsen performance.
802 Also note that if you change the format specification to include any
803 of these specs, you must probably re-start Gnus to see them go into
804 effect.") 
805
806 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
807   "*The format specification of the lines in the summary buffer.
808
809 It works along the same lines as a normal formatting string,
810 with some simple extensions.
811
812 %N   Article number, left padded with spaces (string)
813 %S   Subject (string)
814 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
815 %n   Name of the poster (string)
816 %A   Address of the poster (string)
817 %F   Contents of the From: header (string)
818 %x   Contents of the Xref: header (string)
819 %D   Date of the article (string)
820 %d   Date of the article (string) in DD-MMM format
821 %M   Message-id of the article (string)
822 %r   References of the article (string)
823 %c   Number of characters in the article (integer)
824 %L   Number of lines in the article (integer)
825 %I   Indentation based on thread level (a string of spaces)
826 %T   A string with two possible values: 80 spaces if the article
827      is on thread level two or larger and 0 spaces on level one
828 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
829 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
830 %[   Opening bracket (character, \"[\" or \"<\")
831 %]   Closing bracket (character, \"]\" or \">\")
832 %>   Spaces of length thread-level (string)
833 %<   Spaces of length (- 20 thread-level) (string)
834 %i   Article score (number)
835 %z   Article zcore (character)
836 %t   Number of articles under the current thread (number).
837 %e   Whether the thread is empty or not (character).
838 %u   User defined specifier. The next character in the format string should
839      be a letter.  Gnus will call the function gnus-user-format-function-X,
840      where X is the letter following %u. The function will be passed the
841      current header as argument. The function should return a string, which
842      will be inserted into the summary just like information from any other
843      summary specifier.
844
845 Text between %( and %) will be highlighted with `gnus-mouse-face'
846 when the mouse point is placed inside the area.  There can only be one
847 such area.
848
849 The %U (status), %R (replied) and %z (zcore) specs have to be handled
850 with care. For reasons of efficiency, Gnus will compute what column
851 these characters will end up in, and \"hard-code\" that. This means that
852 it is illegal to have these specs after a variable-length spec. Well,
853 you might not be arrested, but your summary buffer will look strange,
854 which is bad enough.
855
856 The smart choice is to have these specs as for to the left as
857 possible. 
858
859 This restriction may disappear in later versions of Gnus.")
860
861 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
862   "*The format specification for the dummy roots in the summary buffer.
863 It works along the same lines as a normal formatting string,
864 with some simple extensions.
865
866 %S  The subject")
867
868 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
869   "*The format specification for the summary mode line.")
870
871 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
872   "*The format specification for the article mode line.")
873
874 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
875   "*The format specification for the group mode line.")
876
877 (defvar gnus-valid-select-methods
878   '(("nntp" post address prompt-address)
879     ("nnspool" post)
880     ("nnvirtual" none virtual prompt-address) 
881     ("nnmbox" mail respool) 
882     ("nnml" mail respool)
883     ("nnmh" mail respool) 
884     ("nndir" none prompt-address address)
885     ("nndigest" none) 
886     ("nndoc" none prompt-address) 
887     ("nnbabyl" mail respool) 
888     ("nnkiboze" none virtual) 
889     ("nnfolder" mail respool))
890   "An alist of valid select methods.
891 The first element of each list lists should be a string with the name
892 of the select method. The other elements may be be the category of
893 this method (ie. `post', `mail', `none' or whatever) or other
894 properties that this method has (like being respoolable).
895 If you implement a new select method, all you should have to change is
896 this variable. I think.")
897
898 (defvar gnus-updated-mode-lines '(group article summary)
899   "*List of buffers that should update their mode lines.
900 The list may contain the symbols `group', `article' and `summary'. If
901 the corresponding symbol is present, Gnus will keep that mode line
902 updated with information that may be pertinent. 
903 If this variable is nil, screen refresh may be quicker.")
904
905 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
906 (defvar gnus-mode-non-string-length 21
907   "*Max length of mode-line non-string contents.
908 If this is nil, Gnus will take space as is needed, leaving the rest
909 of the modeline intact.")
910
911 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
912 (defvar gnus-mouse-face 'highlight
913   "*Face used for mouse highlighting in Gnus.
914 No mouse highlights will be done if `gnus-visual' is nil.")
915
916 (defvar gnus-summary-mark-below nil
917   "*Mark all articles with a score below this variable as read.
918 This variable is local to each summary buffer and usually set by the
919 score file.")  
920
921 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
922   "*List of functions used for sorting threads in the summary buffer.
923 By default, threads are sorted by article number.
924
925 Each function takes two threads and return non-nil if the first thread
926 should be sorted before the other.  If you use more than one function,
927 the primary sort function should be the last.
928
929 Ready-mady functions include `gnus-thread-sort-by-number',
930 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
931 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
932 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
933
934 (defvar gnus-thread-score-function '+
935   "*Function used for calculating the total score of a thread.
936
937 The function is called with the scores of the article and each
938 subthread and should then return the score of the thread.
939
940 Some functions you can use are `+', `max', or `min'.")
941
942 (defvar gnus-options-subscribe nil
943   "*All new groups matching this regexp will be subscribed unconditionally.
944 Note that this variable deals only with new newsgroups.  This variable
945 does not affect old newsgroups.")
946
947 (defvar gnus-options-not-subscribe nil
948   "*All new groups matching this regexp will be ignored.
949 Note that this variable deals only with new newsgroups.  This variable
950 does not affect old (already subscribed) newsgroups.")
951
952 (defvar gnus-auto-expirable-newsgroups nil
953   "*Groups in which to automatically mark read articles as expirable.
954 If non-nil, this should be a regexp that should match all groups in
955 which to perform auto-expiry.  This only makes sense for mail groups.")
956
957 (defvar gnus-hidden-properties '(invisible t intangible t)
958   "Property list to use for hiding text.")
959
960 ;; Hooks.
961
962 (defvar gnus-group-mode-hook nil
963   "*A hook for Gnus group mode.")
964
965 (defvar gnus-summary-mode-hook nil
966   "*A hook for Gnus summary mode.")
967
968 (defvar gnus-article-mode-hook nil
969   "*A hook for Gnus article mode.")
970
971 (defvar gnus-open-server-hook nil
972   "*A hook called just before opening connection to the news server.")
973
974 (defvar gnus-startup-hook nil
975   "*A hook called at startup.
976 This hook is called after Gnus is connected to the NNTP server.")
977
978 (defvar gnus-get-new-news-hook nil
979   "*A hook run just before Gnus checks for new news.")
980
981 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
982   "*A function that is called to generate the group buffer.
983 The function is called with three arguments: The first is a number;
984 all group with a level less or equal to that number should be listed,
985 if the second is non-nil, empty groups should also be displayed. If
986 the third is non-nil, it is a number. No groups with a level lower
987 than this number should be displayed.
988
989 The only current function implemented is `gnus-group-prepare-flat'.")
990
991 (defvar gnus-group-prepare-hook nil
992   "*A hook called after the group buffer has been generated.
993 If you want to modify the group buffer, you can use this hook.")
994
995 (defvar gnus-summary-prepare-hook nil
996   "*A hook called after the summary buffer has been generated.
997 If you want to modify the summary buffer, you can use this hook.")
998
999 (defvar gnus-article-prepare-hook nil
1000   "*A hook called after an article has been prepared in the article buffer.
1001 If you want to run a special decoding program like nkf, use this hook.")
1002
1003 (defvar gnus-article-display-hook nil
1004   "*A hook called after the article is displayed in the article buffer.
1005 The hook is designed to change the contents of the article
1006 buffer. Typical functions that this hook may contain are
1007 `gnus-article-hide-headers' (hide selected headers),
1008 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1009 `gnus-article-hide-signature' (hide signature) and
1010 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1011 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1012 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1013
1014 (defvar gnus-article-x-face-command
1015   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1016   "String or function to be executed to display an X-Face header.
1017 If it is a string, the command will be executed in a sub-shell
1018 asynchronously. The compressed face will be piped to this command.") 
1019
1020 (defvar gnus-article-x-face-too-ugly nil
1021   "Regexp matching posters whose face shouldn't be shown automatically.")
1022
1023 (defvar gnus-select-group-hook nil
1024   "*A hook called when a newsgroup is selected.
1025
1026 If you'd like to simplify subjects like the
1027 `gnus-summary-next-same-subject' command does, you can use the
1028 following hook:
1029
1030  (setq gnus-select-group-hook
1031       (list
1032         (lambda ()
1033           (mapcar (lambda (header)
1034                      (header-set-subject
1035                       header
1036                       (gnus-simplify-subject
1037                        (header-subject header) 're-only)))
1038                   gnus-newsgroup-headers))))")
1039
1040 (defvar gnus-select-article-hook
1041   '(gnus-summary-show-thread)
1042   "*A hook called when an article is selected.
1043 The default hook shows conversation thread subtrees of the selected
1044 article automatically using `gnus-summary-show-thread'.")
1045
1046 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1047   "*A hook called to apply kill files to a group.
1048 This hook is intended to apply a kill file to the selected newsgroup.
1049 The function `gnus-apply-kill-file' is called by default.
1050
1051 Since a general kill file is too heavy to use only for a few
1052 newsgroups, I recommend you to use a lighter hook function. For
1053 example, if you'd like to apply a kill file to articles which contains
1054 a string `rmgroup' in subject in newsgroup `control', you can use the
1055 following hook:
1056
1057 \(setq gnus-apply-kill-hook
1058       (list
1059         (lambda ()
1060           (cond ((string-match \"control\" gnus-newsgroup-name)
1061                  (gnus-kill \"Subject\" \"rmgroup\")
1062                  (gnus-expunge \"X\"))))))")
1063
1064 (defvar gnus-visual-mark-article-hook 
1065   (list 'gnus-highlight-selected-summary)
1066   "*Hook run after selecting an article in the summary buffer.
1067 It is meant to be used for highlighting the article in some way.  It
1068 is not run if `gnus-visual' is nil.")
1069
1070 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1071   "*A hook called after preparing body, but before preparing header headers.
1072 The default hook (`gnus-inews-insert-signature') inserts a signature
1073 file specified by the variable `gnus-signature-file'.")
1074
1075 (defvar gnus-exit-group-hook nil
1076   "*A hook called when exiting (not quitting) summary mode.")
1077
1078 (defvar gnus-suspend-gnus-hook nil
1079   "*A hook called when suspending (not exiting) Gnus.")
1080
1081 (defvar gnus-exit-gnus-hook nil
1082   "*A hook called when exiting Gnus.")
1083
1084 (defvar gnus-save-newsrc-hook nil
1085   "*A hook called when saving the newsrc file.")
1086
1087 (defvar gnus-summary-update-hook 
1088   (list 'gnus-summary-highlight-line)
1089   "*A hook called when a summary line is changed.
1090 The hook will not be called if `gnus-visual' is nil.
1091
1092 The default function `gnus-summary-highlight-line' will
1093 highlight the line according to the `gnus-summary-highlight'
1094 variable.")
1095
1096 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1097   "*A hook called when an article is selected for the first time.
1098 The hook is intended to mark an article as read (or unread)
1099 automatically when it is selected.")
1100
1101 \f
1102 ;; Internal variables
1103
1104 ;; Avoid highlighting in kill files.
1105 (defvar gnus-summary-inhibit-highlight nil)
1106 (defvar gnus-newsgroup-selected-overlay nil)
1107
1108 (defvar gnus-article-mode-map nil)
1109 (defvar caesar-translate-table nil)
1110 (defvar gnus-dribble-buffer nil)
1111 (defvar gnus-headers-retrieved-by nil)
1112 (defvar gnus-article-reply nil)
1113 (defvar gnus-override-method nil)
1114 (defvar gnus-article-check-size nil)
1115
1116 (defvar gnus-current-score-file nil)
1117 (defvar gnus-current-move-group nil)
1118
1119 (defvar gnus-newsgroup-dependencies nil)
1120 (defvar gnus-newsgroup-threads nil)
1121 (defvar gnus-newsgroup-async nil)
1122 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1123
1124 (defvar gnus-newsgroup-adaptive nil)
1125
1126 (defvar gnus-summary-display-table nil)
1127
1128 (defconst gnus-group-line-format-alist
1129   (list (list ?M 'marked ?c)
1130         (list ?S 'subscribed ?c)
1131         (list ?L 'level ?d)
1132         (list ?N 'number ?s)
1133         (list ?I 'number-of-dormant ?d)
1134         (list ?T 'number-of-ticked ?d)
1135         (list ?R 'number-of-read ?s)
1136         (list ?t 'number-total ?d)
1137         (list ?y 'number-of-unread-unticked ?s)
1138         (list ?i 'number-of-ticked-and-dormant ?d)
1139         (list ?g 'group ?s)
1140         (list ?G 'qualified-group ?s)
1141         (list ?D 'newsgroup-description ?s)
1142         (list ?o 'moderated ?c)
1143         (list ?O 'moderated-string ?s)
1144         (list ?s 'news-server ?s)
1145         (list ?n 'news-method ?s)
1146         (list ?z 'news-method-string ?s)
1147         (list ?u 'user-defined ?s)))
1148
1149 (defconst gnus-summary-line-format-alist 
1150   (list (list ?N 'number ?d)
1151         (list ?S 'subject ?s)
1152         (list ?s 'subject-or-nil ?s)
1153         (list ?n 'name ?s)
1154         (list ?A 'address ?s)
1155         (list ?F 'from ?s)
1156         (list ?x (macroexpand '(header-xref header)) ?s)
1157         (list ?D (macroexpand '(header-date header)) ?s)
1158         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1159         (list ?M (macroexpand '(header-id header)) ?s)
1160         (list ?r (macroexpand '(header-references header)) ?s)
1161         (list ?c '(or (header-chars header) 0) ?d)
1162         (list ?L 'lines ?d)
1163         (list ?I 'indentation ?s)
1164         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1165         (list ?R 'replied ?c)
1166         (list ?\[ 'opening-bracket ?c)
1167         (list ?\] 'closing-bracket ?c)
1168         (list ?\> '(make-string level ? ) ?s)
1169         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1170         (list ?i 'score ?d)
1171         (list ?z 'score-char ?c)
1172         (list ?U 'unread ?c)
1173         (list ?t '(gnus-summary-number-of-articles-in-thread 
1174                    (or (prog1 gnus-tmp-adopt-thread 
1175                          (setq gnus-tmp-adopt-thread nil))
1176                        (if (boundp 'thread) (symbol-value 'thread)
1177                          thread nil)))
1178                    ?d)
1179         (list ?e '(gnus-summary-number-of-articles-in-thread 
1180                    (or gnus-tmp-adopt-thread 
1181                        (if (boundp 'thread) (symbol-value 'thread)
1182                          thread nil)) t)
1183                    ?d)
1184         (list ?u 'user-defined ?s))
1185   "An alist of format specifications that can appear in summary lines,
1186 and what variables they correspond with, along with the type of the
1187 variable (string, integer, character, etc).")
1188
1189 (defconst gnus-summary-dummy-line-format-alist
1190   (list (list ?S 'subject ?s)
1191         (list ?N 'number ?d)
1192         (list ?u 'user-defined ?s)))
1193
1194 (defconst gnus-summary-mode-line-format-alist 
1195   (list (list ?G 'group-name ?s)
1196         (list ?g '(gnus-short-group-name group-name) ?s)
1197         (list ?A 'article-number ?d)
1198         (list ?Z 'unread-and-unselected ?s)
1199         (list ?V 'gnus-version ?s)
1200         (list ?U 'unread ?d)
1201         (list ?S 'subject ?s)
1202         (list ?e 'unselected ?d)
1203         (list ?u 'user-defined ?s)
1204         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1205
1206 (defconst gnus-group-mode-line-format-alist 
1207   (list (list ?S 'news-server ?s)
1208         (list ?M 'news-method ?s)
1209         (list ?u 'user-defined ?s)))
1210
1211 (defvar gnus-have-read-active-file nil)
1212
1213 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls & Boys)"
1214   "The mail address of the Gnus maintainer.")
1215
1216 (defconst gnus-version "(ding) Gnus v0.75"
1217   "Version number for this version of Gnus.")
1218
1219 (defvar gnus-info-nodes
1220   '((gnus-group-mode            "(gnus)The Group Buffer")
1221     (gnus-summary-mode          "(gnus)The Summary Buffer")
1222     (gnus-article-mode          "(gnus)The Article Buffer"))
1223   "Assoc list of major modes and related Info nodes.")
1224
1225 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1226   "The location of the (ding) Gnus documentation group.")
1227
1228 (defvar gnus-group-buffer "*Group*")
1229 (defvar gnus-summary-buffer "*Summary*")
1230 (defvar gnus-article-buffer "*Article*")
1231 (defvar gnus-server-buffer "*Server*")
1232
1233 (defvar gnus-work-buffer " *gnus work*")
1234
1235 (defvar gnus-buffer-list nil
1236   "Gnus buffers that should be killed on exit.")
1237
1238 (defvar gnus-server-alist nil
1239   "List of available servers.")
1240
1241 (defvar gnus-variable-list
1242   '(gnus-newsrc-options gnus-newsrc-options-n
1243     gnus-newsrc-last-checked-date 
1244     gnus-newsrc-alist gnus-server-alist
1245     gnus-killed-list gnus-zombie-list)
1246   "Gnus variables saved in the quick startup file.")
1247
1248 (defvar gnus-overload-functions
1249   '((news-inews gnus-inews-news "rnewspost")
1250     (caesar-region gnus-caesar-region "rnews"))
1251   "Functions overloaded by gnus.
1252 It is a list of `(original overload &optional file)'.")
1253
1254 (defvar gnus-newsrc-options nil
1255   "Options line in the .newsrc file.")
1256
1257 (defvar gnus-newsrc-options-n nil
1258   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1259
1260 (defvar gnus-newsrc-last-checked-date nil
1261   "Date Gnus last asked server for new newsgroups.")
1262
1263 (defvar gnus-newsrc-alist nil
1264   "Assoc list of read articles.
1265 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1266
1267 (defvar gnus-newsrc-hashtb nil
1268   "Hashtable of gnus-newsrc-alist.")
1269
1270 (defvar gnus-killed-list nil
1271   "List of killed newsgroups.")
1272
1273 (defvar gnus-killed-hashtb nil
1274   "Hash table equivalent of gnus-killed-list.")
1275
1276 (defvar gnus-zombie-list nil
1277   "List of almost dead newsgroups.")
1278
1279 (defvar gnus-description-hashtb nil
1280   "Descriptions of newsgroups.")
1281
1282 (defvar gnus-list-of-killed-groups nil
1283   "List of newsgroups that have recently been killed by the user.")
1284
1285 (defvar gnus-active-hashtb nil
1286   "Hashtable of active articles.")
1287
1288 (defvar gnus-moderated-list nil
1289   "List of moderated newsgroups.")
1290
1291 (defvar gnus-group-marked nil)
1292
1293 (defvar gnus-current-startup-file nil
1294   "Startup file for the current host.")
1295
1296 (defvar gnus-last-search-regexp nil
1297   "Default regexp for article search command.")
1298
1299 (defvar gnus-last-shell-command nil
1300   "Default shell command on article.")
1301
1302 (defvar gnus-current-select-method nil
1303   "The current method for selecting a newsgroup.")
1304
1305 (defvar gnus-have-all-newsgroups nil)
1306
1307 (defvar gnus-article-internal-prepare-hook nil)
1308
1309 (defvar gnus-newsgroup-name nil)
1310 (defvar gnus-newsgroup-begin nil)
1311 (defvar gnus-newsgroup-end nil)
1312 (defvar gnus-newsgroup-last-rmail nil)
1313 (defvar gnus-newsgroup-last-mail nil)
1314 (defvar gnus-newsgroup-last-folder nil)
1315 (defvar gnus-newsgroup-last-file nil)
1316 (defvar gnus-newsgroup-auto-expire nil)
1317 (defvar gnus-newsgroup-active nil)
1318
1319 (defvar gnus-newsgroup-unreads nil
1320   "List of unread articles in the current newsgroup.")
1321
1322 (defvar gnus-newsgroup-unselected nil
1323   "List of unselected unread articles in the current newsgroup.")
1324
1325 (defvar gnus-newsgroup-marked nil
1326   "List of ticked articles in the current newsgroup (a subset of unread art).")
1327
1328 (defvar gnus-newsgroup-killed nil
1329   "List of ranges of articles that have been through the scoring process.")
1330
1331 (defvar gnus-newsgroup-kill-headers nil)
1332
1333 (defvar gnus-newsgroup-replied nil
1334   "List of articles that have been replied to in the current newsgroup.")
1335
1336 (defvar gnus-newsgroup-expirable nil
1337   "List of articles in the current newsgroup that can be expired.")
1338
1339 (defvar gnus-newsgroup-processable nil
1340   "List of articles in the current newsgroup that can be processed.")
1341
1342 (defvar gnus-newsgroup-bookmarks nil
1343   "List of articles in the current newsgroup that have bookmarks.")
1344
1345 (defvar gnus-newsgroup-dormant nil
1346   "List of dormant articles in the current newsgroup.")
1347
1348 (defvar gnus-newsgroup-scored nil
1349   "List of scored articles in the current newsgroup.")
1350
1351 (defvar gnus-newsgroup-headers nil
1352   "List of article headers in the current newsgroup.")
1353 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1354
1355 (defvar gnus-newsgroup-ancient nil
1356   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1357
1358 (defvar gnus-current-article nil)
1359 (defvar gnus-article-current nil)
1360 (defvar gnus-current-headers nil)
1361 (defvar gnus-have-all-headers nil)
1362 (defvar gnus-last-article nil)
1363 (defvar gnus-newsgroup-history nil)
1364 (defvar gnus-current-kill-article nil)
1365
1366 ;; Save window configuration.
1367 (defvar gnus-prev-winconf nil)
1368
1369 ;; Format specs
1370 (defvar gnus-summary-line-format-spec nil)
1371 (defvar gnus-summary-dummy-line-format-spec nil)
1372 (defvar gnus-group-line-format-spec nil)
1373 (defvar gnus-summary-mode-line-format-spec nil)
1374 (defvar gnus-article-mode-line-format-spec nil)
1375 (defvar gnus-group-mode-line-format-spec nil)
1376 (defvar gnus-summary-mark-positions nil)
1377
1378 (defvar gnus-summary-expunge-below nil)
1379 (defvar gnus-reffed-article-number nil)
1380
1381 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1382 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1383
1384 (defconst gnus-summary-local-variables 
1385   '(gnus-newsgroup-name 
1386     gnus-newsgroup-begin gnus-newsgroup-end 
1387     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1388     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1389     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1390     gnus-newsgroup-unselected gnus-newsgroup-marked
1391     gnus-newsgroup-replied gnus-newsgroup-expirable
1392     gnus-newsgroup-processable gnus-newsgroup-killed
1393     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1394     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1395     gnus-current-article gnus-current-headers gnus-have-all-headers
1396     gnus-last-article gnus-article-internal-prepare-hook
1397     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1398     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1399     gnus-newsgroup-threads gnus-newsgroup-async
1400     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1401     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1402     gnus-newsgroup-history gnus-newsgroup-ancient
1403     gnus-newsgroup-adaptive)
1404   "Variables that are buffer-local to the summary buffers.")
1405
1406 (defconst gnus-bug-message
1407   "Sending a bug report to the Gnus Towers.
1408 ========================================
1409
1410 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1411 be sent to the Gnus Bug Exterminators. 
1412
1413 At the bottom of the buffer you'll see lots of variable settings.
1414 Please do not delete those.  They will tell the Bug People what your
1415 environment is, so that it will be easier to locate the bugs.
1416
1417 If you have found a bug that makes Emacs go \"beep\", set
1418 debug-on-error to t (`M-ESC (setq debug-on-error t)') and include the
1419 backtrace in your bug report.
1420
1421 Please describe the bug in annoying, painstaking detail.
1422
1423 Thank you for your help in stamping out bugs.
1424 ")
1425
1426 ;;; End of variables.
1427
1428 ;; Define some autoload functions Gnus might use.
1429 (eval-and-compile
1430
1431   ;; Various 
1432   (autoload 'metamail-buffer "metamail")
1433   (autoload 'Info-goto-node "info")
1434   (autoload 'hexl-hex-string-to-integer "hexl")
1435   (autoload 'pp "pp")
1436   (autoload 'pp-to-string "pp")
1437   (autoload 'pp-eval-expression "pp")
1438   (autoload 'mail-extract-address-components "mail-extr")
1439
1440   (autoload 'nnmail-split-fancy "nnmail")
1441
1442   ;; timezone
1443   (autoload 'timezone-make-date-arpa-standard "timezone")
1444   (autoload 'timezone-fix-time "timezone")
1445   (autoload 'timezone-make-sortable-date "timezone")
1446   (autoload 'timezone-make-time-string "timezone")
1447
1448   ;; rmail & friends
1449   (autoload 'mail-position-on-field "sendmail")
1450   (autoload 'mail-setup "sendmail")
1451   (autoload 'rmail-output "rmailout")
1452   (autoload 'news-mail-other-window "rnewspost")
1453   (autoload 'news-reply-yank-original "rnewspost")
1454   (autoload 'news-caesar-buffer-body "rnewspost")
1455   (autoload 'rmail-insert-rmail-file-header "rmail")
1456   (autoload 'rmail-count-new-messages "rmail")
1457   (autoload 'rmail-show-message "rmail")
1458
1459   ;; gnus-mh
1460   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1461   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1462   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1463   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1464   (autoload 'gnus-Folder-save-name "gnus-mh")
1465   (autoload 'gnus-folder-save-name "gnus-mh")
1466
1467   ;; gnus-vis misc
1468   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1469   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1470   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1471   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1472   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1473   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1474   (autoload 'gnus-summary-highlight-line "gnus-vis")
1475   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1476
1477   ;; gnus-vis article
1478   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1479   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1480   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1481   (autoload 'gnus-article-hide "gnus-vis" nil t)
1482   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1483   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1484   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1485   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1486   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1487   (autoload 'gnus-article-add-button "gnus-vis")
1488
1489   ;; gnus-cite
1490   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1491   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1492   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1493
1494   ;; gnus-kill
1495   (autoload 'gnus-kill "gnus-kill")
1496   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1497   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1498   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1499   (autoload 'gnus-execute "gnus-kill")
1500   (autoload 'gnus-expunge "gnus-kill")
1501
1502   ;; gnus-cache
1503   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1504   (autoload 'gnus-cache-save-buffers "gnus-cache")
1505   (autoload 'gnus-cache-possibly-remove-article "gnus-cache")
1506   (autoload 'gnus-cache-request-article "gnus-cache")
1507   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1508   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1509   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1510
1511   ;; gnus-score
1512   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1513   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1514   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1515   (autoload 'gnus-score-save "gnus-score")
1516   (autoload 'gnus-score-headers "gnus-score")
1517   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1518   (autoload 'gnus-score-adaptive "gnus-score")
1519   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1520   (autoload 'gnus-possibly-score-headers "gnus-score")
1521   (autoload 'gnus-score-find-trace "gnus-score")
1522
1523   ;; gnus-uu
1524   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1525   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1526   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1527   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1528   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1529   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1530   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1531   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1532   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1533   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1534   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1535   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1536   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1537   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1538   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1539   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1540
1541   ;; gnus-msg
1542   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1543   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1544   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1545   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1546   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1547   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1548   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1549   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1550   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1551   (autoload 'gnus-post-news "gnus-msg" nil t)
1552   (autoload 'gnus-inews-news "gnus-msg" nil t)
1553   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1554   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1555   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1556   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1557   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1558   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1559   (autoload 'gnus-mail-yank-original "gnus-msg")
1560   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1561   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1562   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1563   (autoload 'gnus-article-mail-with-original "gnus-msg")
1564   (autoload 'gnus-article-mail "gnus-msg")
1565
1566   ;; gnus-vm
1567   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1568   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1569   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1570   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1571   (autoload 'gnus-yank-article "gnus-vm" nil t)
1572
1573   )
1574
1575 \f
1576
1577 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1578 ;; If you want the cursor to go somewhere else, set these two
1579 ;; functions in some startup hook to whatever you want.
1580 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1581 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1582
1583 ;;; Various macros and substs.
1584
1585 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1586   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1587   (` (let ((GnusStartBufferWindow (selected-window)))
1588        (unwind-protect
1589            (progn
1590              (pop-to-buffer (, buffer))
1591              (,@ forms))
1592          (select-window GnusStartBufferWindow)))))
1593
1594 (defmacro gnus-gethash (string hashtable)
1595   "Get hash value of STRING in HASHTABLE."
1596   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1597   ;;(` (abbrev-expansion (, string) (, hashtable)))
1598   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1599
1600 (defmacro gnus-sethash (string value hashtable)
1601   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1602   ;; We cannot use define-abbrev since it only accepts string as value.
1603   ;; (set (intern string hashtable) value))
1604   (` (set (intern (, string) (, hashtable)) (, value))))
1605
1606 (defsubst gnus-buffer-substring (beg end)
1607   (buffer-substring (match-beginning beg) (match-end end)))
1608
1609 (defsubst gnus-simplify-subject-re (subject)
1610   "Remove \"Re:\" from subject lines."
1611   (let ((case-fold-search t))
1612     (if (string-match "^re: *" subject)
1613         (substring subject (match-end 0))
1614       subject)))
1615
1616 (defsubst gnus-goto-char (point)
1617   (and point (goto-char point)))
1618
1619 (defmacro gnus-buffer-exists-p (buffer)
1620   (` (and (, buffer)
1621           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1622                    (, buffer)))))
1623
1624 (defmacro gnus-kill-buffer (buffer)
1625   (` (if (gnus-buffer-exists-p (, buffer))
1626          (kill-buffer (, buffer)))))
1627
1628 (defsubst gnus-point-at-bol ()
1629   "Return point at the beginning of line."
1630   (let ((p (point)))
1631     (beginning-of-line)
1632     (prog1
1633         (point)
1634       (goto-char p))))
1635
1636 (defsubst gnus-point-at-eol ()
1637   "Return point at the beginning of line."
1638   (let ((p (point)))
1639     (end-of-line)
1640     (prog1
1641         (point)
1642       (goto-char p))))
1643
1644 ;;; Load the compatability functions. 
1645
1646 (require 'gnus-ems)
1647
1648 \f
1649 ;;;
1650 ;;; Gnus Utility Functions
1651 ;;;
1652
1653 (defun gnus-extract-address-components (from)
1654   (let (name address)
1655     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1656         (setq address (substring from (match-beginning 0) (match-end 0))))
1657     (and address
1658          (string-match (concat "<" (regexp-quote address) ">") from)
1659          (setq name (substring from 0 (1- (match-beginning 0)))))
1660     (or name
1661         (and (string-match "(.+)" from)
1662              (setq name (substring from (1+ (match-beginning 0)) 
1663                                    (1- (match-end 0))))))
1664     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1665     (list (or name from) (or address from))))
1666
1667 (defun gnus-fetch-field (field)
1668   "Return the value of the header FIELD of current article."
1669   (save-excursion
1670     (save-restriction
1671       (let ((case-fold-search t))
1672         (gnus-narrow-to-headers)
1673         (mail-fetch-field field)))))
1674
1675 (defun gnus-goto-colon ()
1676   (beginning-of-line)
1677   (search-forward ":" (gnus-point-at-eol) t))
1678
1679 (defun gnus-narrow-to-headers ()
1680   (widen)
1681   (save-excursion
1682     (narrow-to-region
1683      (goto-char (point-min))
1684      (if (search-forward "\n\n" nil t)
1685          (1- (point))
1686        (point-max)))))
1687
1688 (defun gnus-update-format-specifications ()
1689   (gnus-make-thread-indent-array)
1690   (setq gnus-summary-line-format-spec 
1691         (gnus-parse-format
1692          gnus-summary-line-format gnus-summary-line-format-alist))
1693   (gnus-update-summary-mark-positions)
1694   (setq gnus-summary-dummy-line-format-spec 
1695         (gnus-parse-format gnus-summary-dummy-line-format 
1696                            gnus-summary-dummy-line-format-alist))
1697   (setq gnus-group-line-format-spec
1698         (gnus-parse-format 
1699          gnus-group-line-format 
1700          gnus-group-line-format-alist))
1701   (if (and (string-match "%D" gnus-group-line-format)
1702            (not gnus-description-hashtb)
1703            gnus-read-active-file)
1704       (gnus-read-descriptions-file))
1705   (setq gnus-summary-mode-line-format-spec 
1706         (gnus-parse-format gnus-summary-mode-line-format 
1707                            gnus-summary-mode-line-format-alist))
1708   (setq gnus-article-mode-line-format-spec 
1709         (gnus-parse-format gnus-article-mode-line-format 
1710                            gnus-summary-mode-line-format-alist))
1711   (setq gnus-group-mode-line-format-spec 
1712         (gnus-parse-format gnus-group-mode-line-format 
1713                            gnus-group-mode-line-format-alist)))
1714
1715 (defun gnus-update-summary-mark-positions ()
1716   (save-excursion
1717     (let ((gnus-replied-mark 129)
1718           (gnus-score-below-mark 130)
1719           (gnus-score-over-mark 130)
1720           (thread nil)
1721           pos)
1722       (gnus-set-work-buffer)
1723       (gnus-summary-insert-line 
1724        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1725       (goto-char (point-min))
1726       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1727                                          (- (point) 2)))))
1728       (goto-char (point-min))
1729       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1730                                           (- (point) 2))) pos))
1731       (goto-char (point-min))
1732       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1733                                         (- (point) 2))) pos))
1734       (setq gnus-summary-mark-positions pos))))
1735
1736 (defun gnus-format-max-width (form length)
1737   (let* ((val (eval form))
1738          (valstr (if (numberp val) (int-to-string val) val)))
1739     (if (> (length valstr) length)
1740         (substring valstr 0 length)
1741       valstr)))
1742
1743 (defun gnus-set-mouse-face (string)
1744   ;; Set mouse face property on STRING.
1745   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1746   string)
1747
1748 (defun gnus-parse-format (format spec-alist)
1749   ;; This function parses the FORMAT string with the help of the
1750   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1751   ;; string.  If the FORMAT string contains the specifiers %( and %)
1752   ;; the text between them will have the mouse-face text property.
1753   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1754       (if (and gnus-visual gnus-mouse-face)
1755           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1756                 (button (substring format (match-beginning 2) (match-end 2)))
1757                 (post (substring format (match-beginning 3) (match-end 3))))
1758             (list 'concat
1759                   (gnus-parse-simple-format pre spec-alist)
1760                   (list 'gnus-set-mouse-face
1761                         (gnus-parse-simple-format button spec-alist))
1762                   (gnus-parse-simple-format post spec-alist)))
1763         (gnus-parse-simple-format
1764          (concat (substring format (match-beginning 1) (match-end 1))
1765                  (substring format (match-beginning 2) (match-end 2))
1766                  (substring format (match-beginning 3) (match-end 3)))
1767          spec-alist))
1768     (gnus-parse-simple-format format spec-alist)))
1769
1770 (defun gnus-parse-simple-format (format spec-alist)
1771   ;; This function parses the FORMAT string with the help of the
1772   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1773   ;; string. The list will consist of the symbol `format', a format
1774   ;; specification string, and a list of forms depending on the
1775   ;; SPEC-ALIST.
1776   (let ((max-width 0)
1777         spec flist fstring b newspec max-width elem beg)
1778     (save-excursion
1779       (gnus-set-work-buffer)
1780       (insert format)
1781       (goto-char (point-min))
1782       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1783         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1784                                                      (match-end 2))))
1785         ;; First check if there are any specs that look anything like
1786         ;; "%12,12A", ie. with a "max width specification". These have
1787         ;; to be treated specially.
1788         (if (setq beg (match-beginning 1))
1789             (setq max-width 
1790                   (string-to-int 
1791                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1792           (setq max-width 0)
1793           (setq beg (match-beginning 2)))
1794         ;; Find the specification from `spec-alist'.
1795         (if (not (setq elem (cdr (assq spec spec-alist))))
1796             (setq elem '("*" ?s)))
1797         ;; Treat user defined format specifiers specially
1798         (and (eq (car elem) 'user-defined)
1799              (setq elem
1800                    (list 
1801                     (list (intern (concat "gnus-user-format-function-"
1802                                           (buffer-substring
1803                                            (match-beginning 3)
1804                                            (match-end 3))))
1805                           'header)
1806                     ?s))
1807              (delete-region (match-beginning 3) (match-end 3)))
1808         (if (not (zerop max-width))
1809             (let ((el (car elem)))
1810               (cond ((= (car (cdr elem)) ?c) 
1811                      (setq el (list 'char-to-string el)))
1812                     ((= (car (cdr elem)) ?d)
1813                      (numberp el) (setq el (list 'int-to-string el))))
1814               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1815                                 flist))
1816               (setq newspec ?s))
1817           (setq flist (cons (car elem) flist))
1818           (setq newspec (car (cdr elem))))
1819         ;; Remove the old specification (and possibly a ",12" string).
1820         (delete-region beg (match-end 2))
1821         ;; Insert the new specification.
1822         (goto-char beg)
1823         (insert newspec))
1824       (setq fstring (buffer-substring 1 (point-max))))
1825     (cons 'format (cons fstring (nreverse flist)))))
1826
1827 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1828 (defun gnus-read-init-file ()
1829   (and gnus-init-file
1830        (or (and (file-exists-p gnus-init-file) 
1831                 ;; Don't try to load a directory.
1832                 (not (file-directory-p gnus-init-file)))
1833            (file-exists-p (concat gnus-init-file ".el"))
1834            (file-exists-p (concat gnus-init-file ".elc")))
1835        (load gnus-init-file nil t)))
1836
1837 (defun gnus-set-work-buffer ()
1838   (if (get-buffer gnus-work-buffer)
1839       (progn
1840         (set-buffer gnus-work-buffer)
1841         (erase-buffer))
1842     (set-buffer (get-buffer-create gnus-work-buffer))
1843     (buffer-disable-undo (current-buffer))
1844     (gnus-add-current-to-buffer-list)))
1845
1846 ;; Article file names when saving.
1847
1848 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1849   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1850 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1851 Otherwise, it is like ~/News/news/group/num."
1852   (let ((default
1853           (expand-file-name
1854            (concat (if (gnus-use-long-file-name 'not-save)
1855                        (gnus-capitalize-newsgroup newsgroup)
1856                      (gnus-newsgroup-directory-form newsgroup))
1857                    "/" (int-to-string (header-number headers)))
1858            (or gnus-article-save-directory "~/News"))))
1859     (if (and last-file
1860              (string-equal (file-name-directory default)
1861                            (file-name-directory last-file))
1862              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1863         default
1864       (or last-file default))))
1865
1866 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1867   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1868 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1869 Otherwise, it is like ~/News/news/group/num."
1870   (let ((default
1871           (expand-file-name
1872            (concat (if (gnus-use-long-file-name 'not-save)
1873                        newsgroup
1874                      (gnus-newsgroup-directory-form newsgroup))
1875                    "/" (int-to-string (header-number headers)))
1876            (or gnus-article-save-directory "~/News"))))
1877     (if (and last-file
1878              (string-equal (file-name-directory default)
1879                            (file-name-directory last-file))
1880              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1881         default
1882       (or last-file default))))
1883
1884 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1885   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1886 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1887 Otherwise, it is like ~/News/news/group/news."
1888   (or last-file
1889       (expand-file-name
1890        (if (gnus-use-long-file-name 'not-save)
1891            (gnus-capitalize-newsgroup newsgroup)
1892          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1893        (or gnus-article-save-directory "~/News"))))
1894
1895 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1896   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1897 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1898 Otherwise, it is like ~/News/news/group/news."
1899   (or last-file
1900       (expand-file-name
1901        (if (gnus-use-long-file-name 'not-save)
1902            newsgroup
1903          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1904        (or gnus-article-save-directory "~/News"))))
1905
1906 ;; For subscribing new newsgroup
1907
1908 (defun gnus-subscribe-hierarchical-interactive (groups)
1909   (let ((groups (sort groups 'string<))
1910         prefixes prefix start rest ans group starts)
1911     (while groups
1912       (setq prefixes (list "^"))
1913       (while (and groups prefixes)
1914         (while (not (string-match (car prefixes) (car groups)))
1915           (setq prefixes (cdr prefixes)))
1916         (setq prefix (car prefixes))
1917         (setq start (1- (length prefix)))
1918         (if (and (string-match "[^\\.]\\." (car groups) start)
1919                  (cdr groups)
1920                  (setq prefix 
1921                        (concat "^" (substring (car groups) 0 (match-end 0))))
1922                  (string-match prefix (car (cdr groups))))
1923             (progn
1924               (setq prefixes (cons prefix prefixes))
1925               (message "Descend hierarchy %s? ([y]nsq): " 
1926                        (substring prefix 1 (1- (length prefix))))
1927               (setq ans (read-char))
1928               (cond ((= ans ?n)
1929                      (while (and groups 
1930                                  (string-match prefix 
1931                                                (setq group (car groups))))
1932                        (setq gnus-killed-list 
1933                              (cons group gnus-killed-list))
1934                        (gnus-sethash group group gnus-killed-hashtb)
1935                        (setq groups (cdr groups)))
1936                      (setq starts (cdr starts)))
1937                     ((= ans ?s)
1938                      (while (and groups 
1939                                  (string-match prefix 
1940                                                (setq group (car groups))))
1941                        (gnus-sethash group group gnus-killed-hashtb)
1942                        (gnus-subscribe-alphabetically (car groups))
1943                        (setq groups (cdr groups)))
1944                      (setq starts (cdr starts)))
1945                     ((= ans ?q)
1946                      (while groups
1947                        (setq group (car groups))
1948                        (setq gnus-killed-list (cons group gnus-killed-list))
1949                        (gnus-sethash group group gnus-killed-hashtb)
1950                        (setq groups (cdr groups))))
1951                     (t nil)))
1952           (message "Subscribe %s? ([n]yq)" (car groups))
1953           (setq ans (read-char))
1954           (setq group (car groups))
1955           (cond ((= ans ?y)
1956                  (gnus-subscribe-alphabetically (car groups))
1957                  (gnus-sethash group group gnus-killed-hashtb))
1958                 ((= ans ?q)
1959                  (while groups
1960                    (setq group (car groups))
1961                    (setq gnus-killed-list (cons group gnus-killed-list))
1962                    (gnus-sethash group group gnus-killed-hashtb)
1963                    (setq groups (cdr groups))))
1964                 (t 
1965                  (setq gnus-killed-list (cons group gnus-killed-list))
1966                  (gnus-sethash group group gnus-killed-hashtb)))
1967           (setq groups (cdr groups)))))))
1968
1969 (defun gnus-subscribe-randomly (newsgroup)
1970   "Subscribe new NEWSGROUP by making it the first newsgroup."
1971   (gnus-subscribe-newsgroup newsgroup))
1972
1973 (defun gnus-subscribe-alphabetically (newgroup)
1974   "Subscribe new NEWSGROUP and insert it in alphabetical order."
1975   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1976   (let ((groups (cdr gnus-newsrc-alist))
1977         before)
1978     (while (and (not before) groups)
1979       (if (string< newgroup (car (car groups)))
1980           (setq before (car (car groups)))
1981         (setq groups (cdr groups))))
1982     (gnus-subscribe-newsgroup newgroup before)))
1983
1984 (defun gnus-subscribe-hierarchically (newgroup)
1985   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
1986   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1987   (save-excursion
1988     (set-buffer (find-file-noselect gnus-current-startup-file))
1989     (let ((groupkey newgroup)
1990           before)
1991       (while (and (not before) groupkey)
1992         (goto-char (point-min))
1993         (let ((groupkey-re
1994                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
1995           (while (and (re-search-forward groupkey-re nil t)
1996                       (progn
1997                         (setq before (buffer-substring
1998                                       (match-beginning 1) (match-end 1)))
1999                         (string< before newgroup)))))
2000         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2001         (setq groupkey
2002               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2003                   (substring groupkey (match-beginning 1) (match-end 1)))))
2004       (gnus-subscribe-newsgroup newgroup before))))
2005
2006 (defun gnus-subscribe-interactively (newsgroup)
2007   "Subscribe new NEWSGROUP interactively.
2008 It is inserted in hierarchical newsgroup order if subscribed. If not,
2009 it is killed."
2010   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2011       (gnus-subscribe-hierarchically newsgroup)
2012     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2013
2014 (defun gnus-subscribe-zombies (newsgroup)
2015   "Make new NEWSGROUP a zombie group."
2016   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2017
2018 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2019   "Subscribe new NEWSGROUP.
2020 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2021 the first newsgroup."
2022   ;; We subscribe the group by changing its level to `subscribed'.
2023   (gnus-group-change-level 
2024    newsgroup gnus-level-default-subscribed
2025    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2026   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2027
2028 ;; For directories
2029
2030 (defun gnus-newsgroup-directory-form (newsgroup)
2031   "Make hierarchical directory name from NEWSGROUP name."
2032   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2033         (len (length newsgroup))
2034         idx)
2035     ;; If this is a foreign group, we don't want to translate the
2036     ;; entire name.  
2037     (if (setq idx (string-match ":" newsgroup))
2038         (aset newsgroup idx ?/)
2039       (setq idx 0))
2040     ;; Replace all occurrences of `.' with `/'.
2041     (while (< idx len)
2042       (if (= (aref newsgroup idx) ?.)
2043           (aset newsgroup idx ?/))
2044       (setq idx (1+ idx)))
2045     newsgroup))
2046
2047 (defun gnus-make-directory (dir)
2048   "Make DIRECTORY recursively."
2049   (let* ((dir (expand-file-name dir default-directory))
2050          dirs)
2051     (if (string-match "/$" dir)
2052         (setq dir (substring dir 0 (match-beginning 0))))
2053     (while (not (file-exists-p dir))
2054       (setq dirs (cons dir dirs))
2055       (string-match "/[^/]+$" dir)
2056       (setq dir (substring dir 0 (match-beginning 0))))
2057     (while dirs
2058       (make-directory (car dirs))
2059       (setq dirs (cdr dirs)))))
2060
2061 (defun gnus-capitalize-newsgroup (newsgroup)
2062   "Capitalize NEWSGROUP name."
2063   (and (not (zerop (length newsgroup)))
2064        (concat (char-to-string (upcase (aref newsgroup 0)))
2065                (substring newsgroup 1))))
2066
2067 ;; Var
2068
2069 (defun gnus-simplify-subject (subject &optional re-only)
2070   "Remove `Re:' and words in parentheses.
2071 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2072   (let ((case-fold-search t))           ;Ignore case.
2073     ;; Remove `Re:' and `Re^N:'.
2074     (if (string-match "^re:[ \t]*" subject)
2075         (setq subject (substring subject (match-end 0))))
2076     ;; Remove words in parentheses from end.
2077     (or re-only
2078         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2079           (setq subject (substring subject 0 (match-beginning 0)))))
2080     ;; Return subject string.
2081     subject))
2082
2083 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2084 ;; all whitespace.
2085 (defun gnus-simplify-subject-fuzzy (subject)
2086   (let ((case-fold-search t))
2087     (save-excursion
2088       (gnus-set-work-buffer)
2089       (insert subject)
2090       (gnus-simplify-buffer-fuzzy)
2091       (buffer-string))))
2092
2093 (defun gnus-simplify-buffer-fuzzy ()
2094   (goto-char (point-min))
2095   (while (re-search-forward "^[ \t]*re:[ \t]*" nil t)
2096     (replace-match "" t t))
2097   (goto-char (point-min))
2098   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2099     (replace-match "" t t))
2100   (goto-char (point-min))
2101   (while (re-search-forward "[ \t]+" nil t)
2102     (replace-match " " t t))
2103   (goto-char (point-min))
2104   (while (re-search-forward "[ \t]+$" nil t)
2105     (replace-match "" t t))
2106   (goto-char (point-min))
2107   (while (re-search-forward "^[ \t]+" nil t)
2108     (replace-match "" t t)))
2109
2110 ;; Add the current buffer to the list of buffers to be killed on exit. 
2111 (defun gnus-add-current-to-buffer-list ()
2112   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
2113
2114 (defun gnus-string> (s1 s2)
2115   (not (or (string< s1 s2)
2116            (string= s1 s2))))
2117
2118 ;; Functions accessing headers.
2119 ;; Functions are more convenient than macros in some cases.
2120
2121 (defun gnus-header-number (header)
2122   (header-number header))
2123
2124 (defun gnus-header-subject (header)
2125   (header-subject header))
2126
2127 (defun gnus-header-from (header)
2128   (header-from header))
2129
2130 (defun gnus-header-xref (header)
2131   (header-xref header))
2132
2133 (defun gnus-header-lines (header)
2134   (header-lines header))
2135
2136 (defun gnus-header-date (header)
2137   (header-date header))
2138
2139 (defun gnus-header-id (header)
2140   (header-id header))
2141
2142 (defun gnus-header-references (header)
2143   (header-references header))
2144
2145 ;;; General various misc type functions.
2146
2147 (defun gnus-clear-system ()
2148   "Clear all variables and buffers."
2149   ;; Clear Gnus variables.
2150   (let ((variables gnus-variable-list))
2151     (while variables
2152       (set (car variables) nil)
2153       (setq variables (cdr variables))))
2154   ;; Clear other internal variables.
2155   (setq gnus-list-of-killed-groups nil
2156         gnus-have-read-active-file nil
2157         gnus-newsrc-alist nil
2158         gnus-newsrc-hashtb nil
2159         gnus-killed-list nil
2160         gnus-zombie-list nil
2161         gnus-killed-hashtb nil
2162         gnus-active-hashtb nil
2163         gnus-moderated-list nil
2164         gnus-description-hashtb nil
2165         gnus-newsgroup-headers nil
2166         gnus-newsgroup-headers-hashtb-by-number nil
2167         gnus-newsgroup-name nil
2168         gnus-server-alist nil
2169         gnus-current-select-method nil)
2170   ;; Reset any score variables.
2171   (and (boundp 'gnus-score-cache)
2172        (set 'gnus-score-cache nil))
2173   (and (boundp 'gnus-internal-global-score-files)
2174        (set 'gnus-internal-global-score-files nil))
2175   ;; Kill the startup file.
2176   (and gnus-current-startup-file
2177        (get-file-buffer gnus-current-startup-file)
2178        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2179   ;; Save any cache buffers.
2180   (and gnus-use-cache (gnus-cache-save-buffers))
2181   ;; Clear the dribble buffer.
2182   (gnus-dribble-clear)
2183   ;; Kill global KILL file buffer.
2184   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2185       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2186   (gnus-kill-buffer nntp-server-buffer)
2187   ;; Kill Gnus buffers.
2188   (while gnus-buffer-list
2189     (gnus-kill-buffer (car gnus-buffer-list))
2190     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2191
2192 (defun gnus-windows-old-to-new (setting)
2193   (if (symbolp setting)
2194       (setq setting 
2195             (cond ((eq setting 'SelectArticle)
2196                    'article)
2197                   ((eq setting 'SelectSubject)
2198                    'summary)
2199                   ((eq setting 'SelectNewsgroup)
2200                    'group)
2201                   (t setting))))
2202   (if (or (listp setting)
2203           (not (and gnus-window-configuration
2204                     (memq setting '(group summary article)))))
2205       setting
2206     (let* ((setting (if (eq setting 'group) 
2207                         (if (assq 'newsgroup gnus-window-configuration)
2208                             'newsgroup
2209                           'newsgroups) setting))
2210            (elem (car (cdr (assq setting gnus-window-configuration))))
2211            (total (apply '+ elem))
2212            (types '(group summary article))
2213            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2214            (i 0)
2215            perc
2216            out)
2217       (while (< i 3)
2218         (or (zerop (nth i elem))
2219             (progn
2220               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2221               (setq out (cons (if (eq pbuf (nth i types))
2222                                   (vector (nth i types) perc 'point)
2223                                 (vector (nth i types) perc))
2224                               out))))
2225         (setq i (1+ i)))
2226       (list (nreverse out)))))
2227            
2228 (defun gnus-configure-windows (setting)
2229   (setq setting (gnus-windows-old-to-new setting))
2230   (let ((r (if (symbolp setting)
2231                   (cdr (assq setting gnus-buffer-configuration))
2232                 setting))
2233         (in-buf (current-buffer))
2234         rule val window w height hor ohor heights sub jump-buffer
2235         rel total to-buf)
2236     (or r (error "No such setting: %s" setting))
2237
2238     ;; Either remove all windows or just remove all Gnus windows.
2239     (if gnus-use-full-window
2240         (delete-other-windows)
2241       (gnus-remove-some-windows)
2242       (switch-to-buffer nntp-server-buffer))
2243
2244     (while r
2245       (setq hor (car r)
2246             ohor nil)
2247
2248       ;; We have to do the (possible) horizontal splitting before the
2249       ;; vertical. 
2250       (if (and (listp (car hor)) 
2251                (eq (car (car hor)) 'horizontal))
2252           (progn
2253             (split-window nil (- (frame-width) 
2254                                  (floor (* (frame-width) (nth 1 (car hor)))))
2255                           t)
2256             (setq hor (cdr hor))))
2257
2258       ;; Go through the rules and eval the elements that are to be
2259       ;; evaled.  
2260       (while hor
2261         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2262             (progn
2263               ;; Expand short buffer name.
2264               (setq w (aref val 0))
2265               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2266                    (progn
2267                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2268                      (aset val 0 w)))
2269               (setq ohor (cons val ohor))))
2270         (setq hor (cdr hor)))
2271       (setq rule (cons (nreverse ohor) rule))
2272       (setq r (cdr r)))
2273     (setq rule (nreverse rule))
2274
2275     ;; We tally the window sizes.
2276     (setq total (window-height))
2277     (while rule
2278       (setq hor (car rule))
2279       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2280           (setq hor (cdr hor)))
2281       (setq sub 0)
2282       (while hor
2283         (setq rel (aref (car hor) 1)
2284               heights (cons
2285                        (cond ((and (floatp rel) (= 1.0 rel))
2286                               'x)
2287                              ((integerp rel)
2288                               rel)
2289                              (t
2290                               (max (floor (* total rel)) 4)))
2291                        heights)
2292               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2293               hor (cdr hor)))
2294       (setq heights (nreverse heights)
2295             hor (car rule))
2296       
2297       ;; We then go through these heighs and create windows for them.
2298       (while heights
2299         (setq height (car heights)
2300               heights (cdr heights))
2301         (and (eq height 'x)
2302              (setq height (- total sub)))
2303         (and heights
2304              (split-window nil height))
2305         (setq to-buf (aref (car hor) 0))
2306         (switch-to-buffer 
2307          (cond ((not to-buf)
2308                 in-buf)
2309                ((symbolp to-buf)
2310                 (symbol-value (aref (car hor) 0)))
2311                (t
2312                 (aref (car hor) 0))))
2313         (and (> (length (car hor)) 2)
2314              (eq (aref (car hor) 2) 'point)
2315              (setq jump-buffer (current-buffer)))
2316         (other-window 1)
2317         (setq hor (cdr hor)))
2318       
2319       (setq rule (cdr rule)))
2320
2321     ;; Finally, we pop to the buffer that's supposed to have point. 
2322     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2323
2324     (pop-to-buffer jump-buffer)
2325     jump-buffer))
2326       
2327 (defun gnus-remove-some-windows ()
2328   (let ((buffers gnus-window-to-buffer)
2329         (first t)
2330         buf)
2331     (while buffers
2332       (setq buf (cdr (car buffers)))
2333       (if (symbolp buf)
2334           (setq buf (and (boundp buf) (symbol-value buf))))
2335       (and buf 
2336            (get-buffer-window buf)
2337            (progn
2338              (set-buffer buf)
2339              (if first
2340                  (progn
2341                    (switch-to-buffer nntp-server-buffer)
2342                    (setq first nil))
2343                (delete-window (get-buffer-window buf)))))
2344       (setq buffers (cdr buffers)))
2345     (set-buffer nntp-server-buffer)))
2346
2347 (defun gnus-version ()
2348   "Version numbers of this version of Gnus."
2349   (interactive)
2350   (let ((methods gnus-valid-select-methods)
2351         (mess gnus-version)
2352         meth)
2353     ;; Go through all the legal select methods and add their version
2354     ;; numbers to the total version string. Only the backends that are
2355     ;; currently in use will have their message numbers taken into
2356     ;; consideration. 
2357     (while methods
2358       (setq meth (intern (concat (car (car methods)) "-version")))
2359       (and (boundp meth)
2360            (stringp (symbol-value meth))
2361            (setq mess (concat mess "; " (symbol-value meth))))
2362       (setq methods (cdr methods)))
2363     (gnus-message 2 mess)))
2364
2365 (defun gnus-info-find-node ()
2366   "Find Info documentation of Gnus."
2367   (interactive)
2368   ;; Enlarge info window if needed.
2369   (let ((mode major-mode))
2370     (gnus-configure-windows 'info)
2371     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2372
2373 (defun gnus-bug ()
2374   "Send a bug report to the Gnus maintainers."
2375   (interactive)
2376   (let ((winconf (current-window-configuration)))
2377     (delete-other-windows)
2378     (switch-to-buffer "*Gnus Bug Help*")
2379     (erase-buffer)
2380     (insert gnus-bug-message)
2381     (goto-char (point-min))
2382     (pop-to-buffer "*Gnus Bug*")
2383     (erase-buffer)
2384     (mail-mode)
2385     (mail-setup gnus-maintainer nil nil nil nil nil)
2386     (make-local-variable 'gnus-prev-winconf)
2387     (setq gnus-prev-winconf winconf)
2388     (goto-char (point-min))
2389     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2390     (forward-line 1)
2391     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
2392     (let ((b (point)))
2393       (gnus-debug)
2394       (goto-char (- b 3)))
2395     (message "")))
2396
2397 (defun gnus-debug ()
2398   "Attemps to go through the Gnus source file and report what variables have been changed.
2399 The source file has to be in the Emacs load path."
2400   (interactive)
2401   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el"))
2402         file dirs expr olist)
2403     (save-excursion
2404       (set-buffer (get-buffer-create " *gnus bug info*"))
2405       (buffer-disable-undo (current-buffer))
2406       (message "Please wait while we snoop your variables...")
2407       (sit-for 0)
2408       (while files
2409         (erase-buffer)
2410         (setq dirs load-path)
2411         (while dirs
2412           (if (not (file-exists-p 
2413                     (setq file (concat (file-name-as-directory 
2414                                         (car dirs)) (car files)))))
2415               (setq dirs (cdr dirs))
2416             (setq dirs nil)
2417             (insert-file-contents file)
2418             (goto-char (point-min))
2419             (or (re-search-forward "^;;* Internal variables" nil t)
2420                 (error "Malformed sources in file %s" file))
2421             (narrow-to-region (point-min) (point))
2422             (goto-char (point-min))
2423             (while (setq expr (condition-case () 
2424                                   (read (current-buffer)) (error nil)))
2425               (and (eq (car expr) 'defvar)
2426                    (stringp (nth 3 expr))
2427                    (or (not (boundp (nth 1 expr)))
2428                        (not (equal (eval (nth 2 expr))
2429                                    (symbol-value (nth 1 expr)))))
2430                    (setq olist (cons (nth 1 expr) olist))))))
2431         (setq files (cdr files)))
2432       (kill-buffer (current-buffer)))
2433     (insert "------------------- Environment follows -------------------\n\n")
2434     (while olist
2435       (if (boundp (car olist))
2436           (insert "(setq " (symbol-name (car olist)) " '" 
2437                   (prin1-to-string (symbol-value (car olist))) ")\n")
2438         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2439       (setq olist (cdr olist)))
2440     (insert "\n\n")))
2441
2442 (defun gnus-overload-functions (&optional overloads)
2443   "Overload functions specified by optional argument OVERLOADS.
2444 If nothing is specified, use the variable gnus-overload-functions."
2445   (let ((defs nil)
2446         (overloads (or overloads gnus-overload-functions)))
2447     (while overloads
2448       (setq defs (car overloads))
2449       (setq overloads (cdr overloads))
2450       ;; Load file before overloading function if necessary.  Make
2451       ;; sure we cannot use `require' always.
2452       (and (not (fboundp (car defs)))
2453            (car (cdr (cdr defs)))
2454            (load (car (cdr (cdr defs))) nil 'nomessage))
2455       (fset (car defs) (car (cdr defs))))))
2456
2457 (defun gnus-replace-chars-in-string (string from to)
2458   "Replace characters in STRING from FROM to TO."
2459   (let ((string (substring string 0))   ;Copy string.
2460         (len (length string))
2461         (idx 0))
2462     ;; Replace all occurrences of FROM with TO.
2463     (while (< idx len)
2464       (if (= (aref string idx) from)
2465           (aset string idx to))
2466       (setq idx (1+ idx)))
2467     string))
2468
2469 (defun gnus-days-between (date1 date2)
2470   ;; Return the number of days between date1 and date2.
2471   (- (gnus-day-number date1) (gnus-day-number date2)))
2472
2473 (defun gnus-day-number (date)
2474   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2475                      (timezone-parse-date date))))
2476     (timezone-absolute-from-gregorian 
2477      (nth 1 dat) (nth 2 dat) (car dat))))
2478
2479 ;; Returns a floating point number that says how many seconds have
2480 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2481 (defun gnus-seconds-since-epoch (date)
2482   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2483                         (timezone-parse-date date)))
2484          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2485                         (timezone-parse-time
2486                          (aref (timezone-parse-date date) 3))))
2487          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2488                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2489          (tday (- (timezone-absolute-from-gregorian 
2490                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2491                   (timezone-absolute-from-gregorian 
2492                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2493     (+ (nth 2 ttime)
2494        (* (nth 1 ttime) 60)
2495        (* 1.0 (nth 0 ttime) 60 60)
2496        (* 1.0 tday 60 60 24))))
2497
2498 (defun gnus-file-newer-than (file date)
2499   (let ((fdate (nth 5 (file-attributes file))))
2500     (or (> (car fdate) (car date))
2501         (and (= (car fdate) (car date))
2502              (> (nth 1 fdate) (nth 1 date))))))
2503
2504 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2505 ;; the echo area.
2506 (defun gnus-y-or-n-p (prompt)
2507   (prog1
2508       (y-or-n-p prompt)
2509     (message "")))
2510
2511 (defun gnus-yes-or-no-p (prompt)
2512   (prog1
2513       (yes-or-no-p prompt)
2514     (message "")))
2515
2516 ;; Check whether to use long file names.
2517 (defun gnus-use-long-file-name (symbol)
2518   ;; The variable has to be set...
2519   (and gnus-use-long-file-name
2520        ;; If it isn't a list, then we return t.
2521        (or (not (listp gnus-use-long-file-name))
2522            ;; If it is a list, and the list contains `symbol', we
2523            ;; return nil.  
2524            (not (memq symbol gnus-use-long-file-name)))))
2525
2526 ;; I suspect there's a better way, but I haven't taken the time to do
2527 ;; it yet. -erik selberg@cs.washington.edu
2528 (defun gnus-dd-mmm (messy-date)
2529   "Return a string like DD-MMM from a big messy string"
2530   (let ((datevec (timezone-parse-date messy-date)))
2531     (format "%2s-%s"
2532             (or (aref datevec 2) "??")
2533             (capitalize
2534              (or (car 
2535                   (nth (1- (string-to-number (aref datevec 1)))
2536                        timezone-months-assoc))
2537                  "???")))))
2538
2539 ;; Make a hash table (default and minimum size is 255).
2540 ;; Optional argument HASHSIZE specifies the table size.
2541 (defun gnus-make-hashtable (&optional hashsize)
2542   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2543
2544 ;; Make a number that is suitable for hashing; bigger than MIN and one
2545 ;; less than 2^x.
2546 (defun gnus-create-hash-size (min)
2547   (let ((i 1))
2548     (while (< i min)
2549       (setq i (* 2 i)))
2550     (1- i)))
2551
2552 ;; Delete the current line (and the next N lines.);
2553 (defun gnus-delete-line (&optional n)
2554   (let ((n (or n 1)))
2555     (delete-region (progn (beginning-of-line) (point))
2556                    (progn (forward-line n) (point)))))
2557
2558 ;; Show message if message has a lower level than `gnus-verbose'. 
2559 ;; Guide-line for numbers:
2560 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2561 ;; for things that take a long time, 7 - not very important messages
2562 ;; on stuff, 9 - messages inside loops.
2563 (defun gnus-message (level &rest args)
2564   (if (<= level gnus-verbose)
2565       (apply 'message args)
2566     ;; We have to do this format thingie here even if the result isn't
2567     ;; shown - the return value has to be the same as the return value
2568     ;; from `message'.
2569     (apply 'format args)))
2570
2571 ;;; List and range functions
2572
2573 (defun gnus-last-element (list)
2574   "Return last element of LIST."
2575   (while (cdr list)
2576     (setq list (cdr list)))
2577   (car list))
2578
2579 (defun gnus-copy-sequence (list)
2580   "Do a complete, total copy of a list."
2581   (if (and (consp list) (not (consp (cdr list))))
2582       (cons (car list) (cdr list))
2583     (mapcar (lambda (elem) (if (consp elem) 
2584                                (if (consp (cdr elem))
2585                                    (gnus-copy-sequence elem)
2586                                  (cons (car elem) (cdr elem)))
2587                              elem))
2588             list)))
2589
2590 (defun gnus-set-difference (list1 list2)
2591   "Return a list of elements of LIST1 that do not appear in LIST2."
2592   (let ((list1 (copy-sequence list1)))
2593     (while list2
2594       (setq list1 (delq (car list2) list1))
2595       (setq list2 (cdr list2)))
2596     list1))
2597
2598 (defun gnus-sorted-complement (list1 list2)
2599   "Return a list of elements of LIST1 that do not appear in LIST2.
2600 Both lists have to be sorted over <."
2601   (let (out)
2602     (if (or (null list1) (null list2))
2603         (or list1 list2)
2604       (while (and list1 list2)
2605         (cond ((= (car list1) (car list2))
2606                (setq list1 (cdr list1)
2607                      list2 (cdr list2)))
2608               ((< (car list1) (car list2))
2609                (setq out (cons (car list1) out))
2610                (setq list1 (cdr list1)))
2611               (t
2612                (setq out (cons (car list2) out))
2613                (setq list2 (cdr list2)))))
2614       (nconc (nreverse out) (or list1 list2)))))
2615
2616 (defun gnus-intersection (list1 list2)      
2617   (let ((result nil))
2618     (while list2
2619       (if (memq (car list2) list1)
2620           (setq result (cons (car list2) result)))
2621       (setq list2 (cdr list2)))
2622     result))
2623
2624 (defun gnus-sorted-intersection (list1 list2)
2625   ;; LIST1 and LIST2 have to be sorted over <.
2626   (let (out)
2627     (while (and list1 list2)
2628       (cond ((= (car list1) (car list2))
2629              (setq out (cons (car list1) out)
2630                    list1 (cdr list1)
2631                    list2 (cdr list2)))
2632             ((< (car list1) (car list2))
2633              (setq list1 (cdr list1)))
2634             (t
2635              (setq list2 (cdr list2)))))
2636     (nreverse out)))
2637
2638 (defun gnus-set-sorted-intersection (list1 list2)
2639   ;; LIST1 and LIST2 have to be sorted over <.
2640   ;; This function modifies LIST1.
2641   (let* ((top (cons nil list1))
2642          (prev top))
2643   (while (and list1 list2)
2644     (cond ((= (car list1) (car list2))
2645            (setq prev list1
2646                  list1 (cdr list1)
2647                  list2 (cdr list2)))
2648           ((< (car list1) (car list2))
2649            (setcdr prev (cdr list1))
2650            (setq list1 (cdr list1)))
2651           (t
2652            (setq list2 (cdr list2)))))
2653   (setcdr prev nil)
2654   (cdr top)))
2655
2656 (defun gnus-compress-sequence (numbers &optional always-list)
2657   "Convert list of numbers to a list of ranges or a single range.
2658 If ALWAYS-LIST is non-nil, this function will always release a list of
2659 ranges."
2660   (let* ((first (car numbers))
2661          (last (car numbers))
2662          result)
2663     (if (null numbers)
2664         nil
2665       (if (not (listp (cdr numbers)))
2666           numbers
2667         (while numbers
2668           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2669                 ((= (1+ last) (car numbers)) ;Still in sequence
2670                  (setq last (car numbers)))
2671                 (t                      ;End of one sequence
2672                  (setq result 
2673                        (cons (if (= first last) first
2674                                (cons first last)) result))
2675                  (setq first (car numbers))
2676                  (setq last  (car numbers))))
2677           (setq numbers (cdr numbers)))
2678         (if (and (not always-list) (null result))
2679             (if (= first last) (list first) (cons first last))
2680           (nreverse (cons (if (= first last) first (cons first last))
2681                           result)))))))
2682
2683 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2684 (defun gnus-uncompress-range (ranges)
2685   "Expand a list of ranges into a list of numbers.
2686 RANGES is either a single range on the form `(num . num)' or a list of
2687 these ranges."
2688   (let (first last result)
2689     (cond 
2690      ((null ranges)
2691       nil)
2692      ((not (listp (cdr ranges)))
2693       (setq first (car ranges))
2694       (setq last (cdr ranges))
2695       (while (<= first last)
2696         (setq result (cons first result))
2697         (setq first (1+ first)))
2698       (nreverse result))
2699      (t
2700       (while ranges
2701         (if (atom (car ranges))
2702             (if (numberp (car ranges))
2703                 (setq result (cons (car ranges) result)))
2704           (setq first (car (car ranges)))
2705           (setq last  (cdr (car ranges)))
2706           (while (<= first last)
2707             (setq result (cons first result))
2708             (setq first (1+ first))))
2709         (setq ranges (cdr ranges)))
2710       (nreverse result)))))
2711
2712 (defun gnus-add-to-range (ranges list)
2713   "Return a list of ranges that has all articles from both RANGES and LIST.
2714 Note: LIST has to be sorted over `<'."
2715   (if (not ranges)
2716       (gnus-compress-sequence list t)
2717     (setq list (copy-sequence list))
2718     (or (listp (cdr ranges))
2719         (setq ranges (list ranges)))
2720     (let ((out ranges)
2721           ilist lowest highest temp)
2722       (while (and ranges list)
2723         (setq ilist list)
2724         (setq lowest (or (and (atom (car ranges)) (car ranges))
2725                          (car (car ranges))))
2726         (while (and list (cdr list) (< (car (cdr list)) lowest))
2727           (setq list (cdr list)))
2728         (if (< (car ilist) lowest)
2729             (progn
2730               (setq temp list)
2731               (setq list (cdr list))
2732               (setcdr temp nil)
2733               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2734         (setq highest (or (and (atom (car ranges)) (car ranges))
2735                           (cdr (car ranges))))
2736         (while (and list (<= (car list) highest))
2737           (setq list (cdr list)))
2738         (setq ranges (cdr ranges)))
2739       (if list
2740           (setq out (nconc (gnus-compress-sequence list t) out)))
2741       (setq out (sort out (lambda (r1 r2) 
2742                             (< (or (and (atom r1) r1) (car r1))
2743                                (or (and (atom r2) r2) (car r2))))))
2744       (setq ranges out)
2745       (while ranges
2746         (if (atom (car ranges))
2747             (if (cdr ranges)
2748                 (if (atom (car (cdr ranges)))
2749                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2750                         (progn
2751                           (setcar ranges (cons (car ranges) 
2752                                                (car (cdr ranges))))
2753                           (setcdr ranges (cdr (cdr ranges)))))
2754                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2755                       (progn
2756                         (setcar (car (cdr ranges)) (car ranges))
2757                         (setcar ranges (car (cdr ranges)))
2758                         (setcdr ranges (cdr (cdr ranges)))))))
2759           (if (cdr ranges)
2760               (if (atom (car (cdr ranges)))
2761                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2762                       (progn
2763                         (setcdr (car ranges) (car (cdr ranges)))
2764                         (setcdr ranges (cdr (cdr ranges)))))
2765                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2766                     (progn
2767                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2768                       (setcdr ranges (cdr (cdr ranges))))))))
2769         (setq ranges (cdr ranges)))
2770       out)))
2771
2772 (defun gnus-remove-from-range (ranges list)
2773   "Return a list of ranges that has all articles from LIST removed from RANGES.
2774 Note: LIST has to be sorted over `<'."
2775   ;; !!! This function shouldn't look like this, but I've got a headache.
2776   (gnus-compress-sequence 
2777    (gnus-sorted-complement
2778     (gnus-uncompress-range ranges) list)))
2779
2780 (defun gnus-member-of-range (number ranges)
2781   (if (not (listp (cdr ranges)))
2782       (and (>= number (car ranges)) 
2783            (<= number (cdr ranges)))
2784     (let ((not-stop t))
2785       (while (and ranges 
2786                   (if (numberp (car ranges))
2787                       (>= number (car ranges))
2788                     (>= number (car (car ranges))))
2789                   not-stop)
2790         (if (if (numberp (car ranges))
2791                 (= number (car ranges))
2792               (and (>= number (car (car ranges)))
2793                    (<= number (cdr (car ranges)))))
2794             (setq not-stop nil))
2795         (setq ranges (cdr ranges)))
2796       (not not-stop))))
2797
2798 \f
2799 ;;;
2800 ;;; Gnus group mode
2801 ;;;
2802
2803 (defvar gnus-group-mode-map nil)
2804 (defvar gnus-group-group-map nil)
2805 (defvar gnus-group-mark-map nil)
2806 (defvar gnus-group-list-map nil)
2807 (defvar gnus-group-sub-map nil)
2808 (put 'gnus-group-mode 'mode-class 'special)
2809
2810 (if gnus-group-mode-map
2811     nil
2812   (setq gnus-group-mode-map (make-keymap))
2813   (suppress-keymap gnus-group-mode-map)
2814   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2815   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2816   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2817   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2818   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2819   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2820   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2821   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2822   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2823   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2824   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2825   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2826   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2827   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2828   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2829   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2830   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2831   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2832   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2833   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2834   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2835   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2836   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2837   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2838   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2839   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2840   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2841   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2842   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2843   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2844   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2845   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2846   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2847   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2848   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2849   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2850   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2851   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2852   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2853   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2854   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2855   (define-key gnus-group-mode-map "V" 'gnus-version)
2856   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2857   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2858   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2859   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2860   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2861   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2862   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2863   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2864   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2865   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2866   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
2867   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2868   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2869   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
2870
2871   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
2872   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
2873   (define-prefix-command 'gnus-group-mark-map)
2874   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
2875   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
2876   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
2877   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
2878
2879   (define-prefix-command 'gnus-group-group-map)
2880   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
2881   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
2882   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
2883   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
2884   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
2885   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
2886   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
2887   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
2888   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
2889   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
2890
2891   (define-prefix-command 'gnus-group-list-map)
2892   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
2893   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
2894   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
2895   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
2896   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
2897   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
2898   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
2899   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
2900   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
2901
2902   (define-prefix-command 'gnus-group-sub-map)
2903   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
2904   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
2905   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
2906   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
2907   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
2908   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
2909   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
2910   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
2911
2912 (defun gnus-group-mode ()
2913   "Major mode for reading news.
2914
2915 All normal editing commands are switched off.
2916 \\<gnus-group-mode-map>
2917 The group buffer lists (some of) the groups available.  For instance,
2918 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
2919 lists all zombie groups. 
2920
2921 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
2922 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
2923
2924 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
2925
2926 The following commands are available:
2927
2928 \\{gnus-group-mode-map}"
2929   (interactive)
2930   (if gnus-visual (gnus-group-make-menu-bar))
2931   (kill-all-local-variables)
2932   (setq mode-line-modified "-- ")
2933   (make-local-variable 'mode-line-format)
2934   (setq mode-line-format (copy-sequence mode-line-format))
2935   (and (equal (nth 3 mode-line-format) "   ")
2936        (setcar (nthcdr 3 mode-line-format) ""))
2937   (setq major-mode 'gnus-group-mode)
2938   (setq mode-name "Group")
2939   (gnus-group-set-mode-line)
2940   (setq mode-line-process nil)
2941   (use-local-map gnus-group-mode-map)
2942   (buffer-disable-undo (current-buffer))
2943   (setq truncate-lines t)
2944   (setq buffer-read-only t)
2945   (run-hooks 'gnus-group-mode-hook))
2946
2947 (defun gnus-mouse-pick-group (e)
2948   (interactive "e")
2949   (mouse-set-point e)
2950   (gnus-group-read-group nil))
2951
2952 ;;;###autoload
2953 (defun gnus-no-server (&optional arg)
2954   "Read network news.
2955 If ARG is a positive number, Gnus will use that as the
2956 startup level. If ARG is nil, Gnus will be started at level 2. 
2957 If ARG is non-nil and not a positive number, Gnus will
2958 prompt the user for the name of an NNTP server to use.
2959 As opposed to `gnus', this command will not connect to the local server."
2960   (interactive "P")
2961   (gnus (or arg (1- gnus-level-default-subscribed)) t))
2962
2963 (defalias '\(ding\) 'gnus)
2964
2965 ;;;###autoload
2966 (defun gnus (&optional arg dont-connect)
2967   "Read network news.
2968 If ARG is non-nil and a positive number, Gnus will use that as the
2969 startup level. If ARG is non-nil and not a positive number, Gnus will
2970 prompt the user for the name of an NNTP server to use."
2971   (interactive "P")
2972   (if (get-buffer gnus-group-buffer)
2973       (progn
2974         (switch-to-buffer gnus-group-buffer)
2975         (gnus-group-get-new-news))
2976     (gnus-clear-system)
2977     (nnheader-init-server-buffer)
2978     (gnus-read-init-file)
2979     (let ((level (and arg (numberp arg) (> arg 0) arg))
2980           did-connect)
2981       (unwind-protect
2982           (progn
2983             (gnus-group-setup-buffer)
2984             (or dont-connect 
2985                 (setq did-connect
2986                       (gnus-start-news-server (and arg (not level))))))
2987         (if (and (not dont-connect) 
2988                  (not did-connect))
2989             (gnus-group-quit)
2990           (run-hooks 'gnus-startup-hook)
2991           ;; NNTP server is successfully open. 
2992           (gnus-update-format-specifications)
2993           (gnus-summary-make-display-table)
2994           (let ((buffer-read-only nil))
2995             (erase-buffer)
2996             (if (not gnus-inhibit-startup-message)
2997                 (progn
2998                   (gnus-group-startup-message)
2999                   (sit-for 0))))
3000           (gnus-setup-news nil level)
3001           (and gnus-use-dribble-file (gnus-dribble-open))
3002           (gnus-group-list-groups level)
3003           (gnus-configure-windows 'group))))))
3004
3005 (defun gnus-group-startup-message (&optional x y)
3006   "Insert startup message in current buffer."
3007   ;; Insert the message.
3008   (erase-buffer)
3009   (insert
3010    (format "
3011     %s
3012            A newsreader 
3013       for GNU Emacs
3014
3015         Based on GNUS 
3016              written by 
3017      Masanobu UMEDA
3018
3019     Lars Magne 
3020          Ingebrigtsen 
3021       larsi@ifi.uio.no
3022
3023            gnus-version))
3024   ;; And then hack it.
3025   ;; 18 is the longest line.
3026   (indent-rigidly (point-min) (point-max) 
3027                   (/ (max (- (window-width) (or x 28)) 0) 2))
3028   (goto-char (point-min))
3029   ;; +4 is fuzzy factor.
3030   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3031
3032 (defun gnus-group-setup-buffer ()
3033   (or (get-buffer gnus-group-buffer)
3034       (progn
3035         (switch-to-buffer gnus-group-buffer)
3036         (gnus-add-current-to-buffer-list)
3037         (gnus-group-mode)
3038         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3039
3040 (defun gnus-group-list-groups (level &optional unread)
3041   "List newsgroups with level LEVEL or lower that have unread alticles.
3042 Default is all subscribed groups.
3043 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3044   (interactive (list (and current-prefix-arg
3045                           (prefix-numeric-value current-prefix-arg))))
3046   (if gnus-group-use-permanent-levels
3047       (progn
3048         (setq gnus-group-default-list-level 
3049               (or level gnus-group-default-list-level))
3050         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3051     (setq level (or level gnus-group-default-list-level 
3052                     gnus-level-subscribed)))
3053   (gnus-group-setup-buffer)     ;May call from out of group buffer
3054   (let ((case-fold-search nil)
3055         (group (gnus-group-group-name)))
3056     (funcall gnus-group-prepare-function level unread nil)
3057     (if (zerop (buffer-size))
3058         (gnus-message 5 gnus-no-groups-message)
3059       (goto-char (point-min))
3060       (if (not group)
3061           ;; Go to the first group with unread articles.
3062           (gnus-group-search-forward nil nil nil t)
3063         ;; Find the right group to put point on. If the current group
3064         ;; has disapeared in the new listing, try to find the next
3065         ;; one. If no next one can be found, just leave point at the
3066         ;; first newsgroup in the buffer.
3067         (if (not (gnus-goto-char
3068                   (text-property-any (point-min) (point-max) 
3069                                      'gnus-group (intern group))))
3070             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3071               (while (and newsrc
3072                           (not (gnus-goto-char 
3073                                 (text-property-any 
3074                                  (point-min) (point-max) 'gnus-group 
3075                                  (intern (car (car newsrc)))))))
3076                 (setq newsrc (cdr newsrc)))
3077               (or newsrc (progn (goto-char (point-max))
3078                                 (forward-line -1))))))
3079       ;; Adjust cursor point.
3080       (gnus-group-position-cursor))))
3081
3082 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3083   "List all newsgroups with unread articles of level LEVEL or lower.
3084 If ALL is non-nil, list groups that have no unread articles.
3085 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3086 If REGEXP, only list groups matching REGEXP."
3087   (set-buffer gnus-group-buffer)
3088   (let ((buffer-read-only nil)
3089         (newsrc (cdr gnus-newsrc-alist))
3090         (lowest (or lowest 1))
3091         info clevel unread group)
3092     (erase-buffer)
3093     (if (< lowest gnus-level-zombie)
3094         ;; List living groups.
3095         (while newsrc
3096           (setq info (car newsrc)
3097                 group (car info)
3098                 newsrc (cdr newsrc)
3099                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3100           (and unread ; This group might be bogus
3101                (or (not regexp)
3102                    (string-match regexp group))
3103                (<= (setq clevel (car (cdr info))) level) 
3104                (>= clevel lowest)
3105                (or all            ; We list all groups?
3106                    (eq unread t)  ; We list unactivated groups
3107                    (> unread 0)   ; We list groups with unread articles
3108                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3109                (gnus-group-insert-group-line 
3110                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3111
3112     ;; List dead groups.
3113     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3114          (gnus-group-prepare-flat-list-dead 
3115           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3116           gnus-level-zombie ?Z
3117           regexp))
3118     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3119          (gnus-group-prepare-flat-list-dead 
3120           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3121           gnus-level-killed ?K regexp))
3122
3123     (gnus-group-set-mode-line)
3124     (setq gnus-have-all-newsgroups all)
3125     (run-hooks 'gnus-group-prepare-hook)))
3126
3127 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3128   ;; List zombies and killed lists somehwat faster, which was
3129   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3130   ;; this by ignoring the group format specification altogether.
3131   (let (group beg)
3132     (while groups
3133       (setq group (car groups)
3134             groups (cdr groups))
3135       (if (or (not regexp)
3136               (string-match regexp group))
3137           (progn
3138             (setq beg (point))
3139             (insert (format " %c    *: %s\n" mark group))
3140             (add-text-properties 
3141              beg (1+ beg) 
3142              (list 'gnus-group (intern group)
3143                    'gnus-unread t
3144                    'gnus-level level)))))))
3145
3146 (defun gnus-group-real-name (group)
3147   "Find the real name of a foreign newsgroup."
3148   (if (string-match "^[^:]+:" group)
3149       (substring group (match-end 0))
3150     group))
3151
3152 (defun gnus-group-prefixed-name (group method)
3153   "Return the whole name from GROUP and METHOD."
3154   (and (stringp method) (setq method (gnus-server-to-method method)))
3155   (concat (format "%s" (car method))
3156           (if (and 
3157                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3158                (not (string= (nth 1 method) "")))
3159               (concat "+" (nth 1 method)))
3160           ":" group))
3161
3162 (defun gnus-group-real-prefix (group)
3163   "Return the prefix of the current group name."
3164   (if (string-match "^[^:]+:" group)
3165       (substring group 0 (match-end 0))
3166     ""))
3167
3168 (defun gnus-group-method-name (group)
3169   "Return the method used for selecting GROUP."
3170   (let ((prefix (gnus-group-real-prefix group)))
3171     (if (equal prefix "")
3172         gnus-select-method
3173       (if (string-match "^[^\\+]+\\+" prefix)
3174           (list (intern (substring prefix 0 (1- (match-end 0))))
3175                 (substring prefix (match-end 0) (1- (length prefix))))
3176         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3177
3178 (defun gnus-group-foreign-p (group)
3179   "Return nil if GROUP is native, non-nil if it is foreign."
3180   (string-match ":" group))
3181
3182 (defun gnus-group-set-info (info &optional method-only-group part)
3183   (let* ((entry (gnus-gethash
3184                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3185          (part-info info)
3186          (info (if method-only-group (nth 2 entry) info)))
3187     (if (not method-only-group)
3188         ()
3189       (or entry
3190           (error "Trying to change non-existant group %s" method-only-group))
3191       ;; We have recevied parts of the actual group info - either the
3192       ;; select method or the group parameters.  We first check
3193       ;; whether we have to extend the info, and if so, do that.
3194       (let ((len (length info))
3195             (total (if (eq part 'method) 5 6)))
3196         (and (< len total)
3197              (setcdr (nthcdr (1- len) info)
3198                      (make-list (- total len) nil)))
3199         ;; Then we enter the new info.
3200         (setcar (nthcdr (1- total) info) part-info)))
3201     ;; We uncompress some lists of marked articles.
3202     (let (marked)
3203       (if (not (setq marked (nth 3 info)))
3204           ()
3205         (while marked
3206           (or (eq 'score (car (car marked)))
3207               (eq 'bookmark (car (car marked)))
3208               (eq 'killed (car (car marked)))
3209               (setcdr (car marked) 
3210                       (gnus-uncompress-range (cdr (car marked)))))
3211           (setq marked (cdr marked)))))
3212     (if entry
3213         ()
3214       ;; This is a new group, so we just create it.
3215       (save-excursion
3216         (set-buffer gnus-group-buffer)
3217         (if (nth 4 info)
3218             ;; It's a foreign group...
3219             (gnus-group-make-group 
3220              (gnus-group-real-name (car info))
3221              (prin1-to-string (car (nth 4 info)))
3222              (nth 1 (nth 4 info)))
3223           ;; It's a native group.
3224           (gnus-group-make-group
3225            (car info)
3226            (prin1-to-string (car gnus-select-method))
3227            (nth 1 gnus-select-method)))
3228         (gnus-message 6 "Note: New group created")
3229         (setq entry 
3230               (gnus-gethash (gnus-group-prefixed-name 
3231                              (gnus-group-real-name (car info))
3232                              (or (nth 4 info) gnus-select-method))
3233                             gnus-newsrc-hashtb))))
3234     ;; Whether it was a new group or not, we now have the entry, so we
3235     ;; can do the update.
3236     (if entry
3237         (progn
3238           (setcar (nthcdr 2 entry) info)
3239           (if (and (not (eq (car entry) t)) 
3240                    (gnus-gethash (car info) gnus-active-hashtb))
3241               (let ((marked (nth 3 info)))
3242                 (setcar entry 
3243                         (max 0 (- (length (gnus-list-of-unread-articles 
3244                                            (car info)))
3245                                   (length (cdr (assq 'tick marked)))
3246                                   (length (cdr (assq 'dormant marked)))))))))
3247       (error "No such group: %s" (car info)))))
3248
3249 (defun gnus-group-set-method-info (group select-method)
3250   (gnus-group-set-info select-method group 'method))
3251
3252 (defun gnus-group-set-params-info (group params)
3253   (gnus-group-set-info params group 'params))
3254
3255 (defun gnus-group-update-group-line ()
3256   "This function updates the current line in the newsgroup buffer and
3257 moves the point to the colon."
3258   (let* ((buffer-read-only nil)
3259          (group (gnus-group-group-name))
3260          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3261     (if entry
3262         (gnus-dribble-enter 
3263          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3264                  ")")))
3265     (beginning-of-line)
3266     (delete-region (point) (progn (forward-line 1) (point)))
3267     (gnus-group-insert-group-line-info group)
3268     (forward-line -1)
3269     (gnus-group-position-cursor)))
3270
3271 (defun gnus-group-insert-group-line-info (group)
3272   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3273         active info)
3274     (if entry
3275         (progn
3276           (setq info (nth 2 entry))
3277           (gnus-group-insert-group-line 
3278            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3279       (setq active (gnus-gethash group gnus-active-hashtb))
3280       (gnus-group-insert-group-line 
3281        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3282                    gnus-level-killed)
3283        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3284
3285 (defun gnus-group-insert-group-line (gformat group level marked number method)
3286   (let* ((gformat (or gformat gnus-group-line-format-spec))
3287          (active (gnus-gethash group gnus-active-hashtb))
3288          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3289          (number-of-dormant (length (cdr (assq 'dormant marked))))
3290          (number-of-ticked (length (cdr (assq 'tick marked))))
3291          (number-of-ticked-and-dormant
3292           (+ number-of-ticked number-of-dormant))
3293          (number-of-unread-unticked 
3294           (if (numberp number) (int-to-string (max 0 number))
3295             "*"))
3296          (number-of-read
3297           (if (numberp number)
3298               (max 0 (- number-total number))
3299             "*"))
3300          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3301                            ((<= level gnus-level-unsubscribed) ?U)
3302                            ((= level gnus-level-zombie) ?Z)
3303                            (t ?K)))
3304          (qualified-group (gnus-group-real-name group))
3305          (newsgroup-description 
3306           (if gnus-description-hashtb
3307               (or (gnus-gethash group gnus-description-hashtb) "")
3308             ""))
3309          (moderated (if (member group gnus-moderated-list) ?m ? ))
3310          (moderated-string (if (eq moderated ?m) "(m)" ""))
3311          (method (gnus-server-get-method group method))
3312          (news-server (or (car (cdr method)) ""))
3313          (news-method (or (car method) ""))
3314          (news-method-string 
3315           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3316          (marked (if (and 
3317                       (numberp number) 
3318                       (zerop number)
3319                       (> number-of-ticked 0))
3320                      ?* ? ))
3321          (number (if (eq number t) "*" (+ number number-of-dormant 
3322                                           number-of-ticked)))
3323          (buffer-read-only nil)
3324          b)
3325     (beginning-of-line)
3326     (setq b (point))
3327     ;; Insert the text.
3328     (insert (eval gformat))
3329
3330     (add-text-properties 
3331      b (1+ b) (list 'gnus-group (intern group)
3332                     'gnus-unread (if (numberp number)
3333                                      (string-to-int number-of-unread-unticked)
3334                                    t)
3335                     'gnus-marked marked
3336                     'gnus-level level))))
3337
3338 (defun gnus-group-update-group (group &optional visible-only)
3339   "Update newsgroup info of GROUP.
3340 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3341   (save-excursion
3342     (set-buffer gnus-group-buffer)
3343     (let ((buffer-read-only nil)
3344           visible)
3345       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3346         (if entry
3347             (gnus-dribble-enter 
3348              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3349                      ")"))))
3350       ;; Buffer may be narrowed.
3351       (save-restriction
3352         (widen)
3353         ;; Search a line to modify.  If the buffer is large, the search
3354         ;; takes long time.  In most cases, current point is on the line
3355         ;; we are looking for.  So, first of all, check current line. 
3356         (if (or (progn
3357                   (beginning-of-line)
3358                   (eq (get-text-property (point) 'gnus-group)
3359                       (intern group)))
3360                 (progn
3361                   (gnus-goto-char 
3362                    (text-property-any 
3363                     (point-min) (point-max) 'gnus-group (intern group)))))
3364             ;; GROUP is listed in current buffer. So, delete old line.
3365             (progn
3366               (setq visible t)
3367               (beginning-of-line)
3368               (delete-region (point) (progn (forward-line 1) (point))))
3369           ;; No such line in the buffer, find out where it's supposed to
3370           ;; go, and insert it there (or at the end of the buffer).
3371           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3372           (or visible-only
3373               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
3374                 (while (and entry
3375                             (car entry)
3376                             (not
3377                              (gnus-goto-char
3378                               (text-property-any
3379                                (point-min) (point-max) 
3380                                'gnus-group (intern (car (car entry)))))))
3381                   (setq entry (cdr entry)))
3382                 (or entry (goto-char (point-max)))))))
3383       (if (or visible (not visible-only))
3384           (gnus-group-insert-group-line-info group))
3385       (gnus-group-set-mode-line))))
3386
3387 (defun gnus-group-set-mode-line ()
3388   (if (memq 'group gnus-updated-mode-lines)
3389       (let* ((gformat (or gnus-group-mode-line-format-spec
3390                           (setq gnus-group-mode-line-format-spec
3391                                 (gnus-parse-format 
3392                                  gnus-group-mode-line-format 
3393                                  gnus-group-mode-line-format-alist))))
3394              (news-server (car (cdr gnus-select-method)))
3395              (news-method (car gnus-select-method))
3396              (max-len 60)
3397              (mode-string (eval gformat)))
3398         (setq mode-string (eval gformat))
3399         (if (> (length mode-string) max-len) 
3400             (setq mode-string (substring mode-string 0 (- max-len 4))))
3401         (setq mode-line-buffer-identification mode-string)
3402         (set-buffer-modified-p t))))
3403
3404 (defun gnus-group-group-name ()
3405   "Get the name of the newsgroup on the current line."
3406   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3407     (and group (symbol-name group))))
3408
3409 (defun gnus-group-group-level ()
3410   "Get the level of the newsgroup on the current line."
3411   (get-text-property (gnus-point-at-bol) 'gnus-level))
3412
3413 (defun gnus-group-search-forward (&optional backward all level first-too)
3414   "Find the next newsgroup with unread articles.
3415 If BACKWARD is non-nil, find the previous newsgroup instead.
3416 If ALL is non-nil, just find any newsgroup.
3417 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3418 group exists.
3419 If FIRST-TOO, the current line is also eligeble as a target."
3420   (let ((way (if backward -1 1))
3421         (low 10)
3422         (beg (point))
3423         pos found)
3424     (if (and backward (progn (beginning-of-line)) (bobp))
3425         nil
3426       (or first-too (forward-line way))
3427       (while (and 
3428               (not (eobp))
3429               (not (setq 
3430                     found 
3431                     (and (or all
3432                              (and
3433                               (let ((unread 
3434                                      (get-text-property (point) 'gnus-unread)))
3435                                 (or (eq unread t) (and unread (> unread 0))))
3436                               (let ((lev (get-text-property
3437                                           (point) 'gnus-level)))
3438                                 (and lev (<= (get-text-property 
3439                                               (point) 'gnus-level)
3440                                              gnus-level-subscribed)))))
3441                          (or (not level)
3442                              (let ((lev (get-text-property (point) 'gnus-level)))
3443                                (if (and lev (<= lev level))
3444                                    t
3445                                  (if (< lev low)
3446                                      (progn
3447                                        (setq low lev)
3448                                        (setq pos (point))))
3449                                  nil))))))
3450               (zerop (forward-line way)))))
3451     (if found 
3452         (progn (gnus-group-position-cursor) t)
3453       (if pos (goto-char pos) (goto-char beg))
3454       nil)))
3455
3456 ;;; Gnus group mode commands
3457
3458 ;; Group marking.
3459
3460 (defun gnus-group-mark-group (n &optional unmark)
3461   "Mark the current group."
3462   (interactive "p")
3463   (let ((buffer-read-only nil)
3464         group)
3465     (while 
3466         (and (> n 0) 
3467              (setq group (gnus-group-group-name))
3468              (progn
3469                (beginning-of-line)
3470                (forward-char 1)
3471                (delete-char 1)
3472                (if unmark
3473                    (progn
3474                      (insert " ")
3475                      (setq gnus-group-marked (delete group gnus-group-marked)))
3476                  (insert "#")
3477                  (setq gnus-group-marked (cons group gnus-group-marked)))
3478                t)
3479              (zerop (gnus-group-next-group 1)))
3480       (setq n (1- n)))
3481     (gnus-summary-position-cursor)
3482     n))
3483
3484 (defun gnus-group-unmark-group (n)
3485   "Remove the mark from the current group."
3486   (interactive "p")
3487   (gnus-group-mark-group n 'unmark))
3488
3489 (defun gnus-group-mark-region (unmark beg end)
3490   "Mark all groups between point and mark.
3491 If UNMARK, remove the mark instead."
3492   (interactive "P\nr")
3493   (let ((num (count-lines beg end)))
3494     (save-excursion
3495       (goto-char beg)
3496       (- num (gnus-group-mark-group num unmark)))))
3497
3498 (defun gnus-group-remove-mark (group)
3499   (and (gnus-group-goto-group group)
3500        (save-excursion
3501          (gnus-group-mark-group 1 'unmark))))
3502
3503 ;; Return a list of groups to work on.  Take into consideration N (the
3504 ;; prefix) and the list of marked groups.
3505 (defun gnus-group-process-prefix (n)
3506   (cond (n
3507          (setq n (prefix-numeric-value n))
3508          ;; There is a prefix, so we return a list of the N next
3509          ;; groups. 
3510          (let ((way (if (< n 0) -1 1))
3511                (n (abs n))
3512                group groups)
3513            (save-excursion
3514              (while (and (> n 0)
3515                          (setq group (gnus-group-group-name)))
3516                (setq groups (cons group groups))
3517                (setq n (1- n))
3518                (forward-line way)))
3519            (nreverse groups)))
3520         (gnus-group-marked
3521          ;; No prefix, but a list of marked articles.
3522          (reverse gnus-group-marked))
3523         (t
3524          ;; Neither marked articles or a prefix, so we return the
3525          ;; current group.
3526          (let ((group (gnus-group-group-name)))
3527            (and group (list group))))))
3528
3529
3530
3531 ;; Selecting groups.
3532
3533 (defun gnus-group-read-group (all &optional no-article group)
3534   "Read news in this newsgroup.
3535 If argument ALL is non-nil, already read articles become readable.
3536 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3537   (interactive "P")
3538   (let ((group (or group (gnus-group-group-name)))
3539         number active marked entry)
3540     (or group (error "No group on current line"))
3541     (setq marked 
3542           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3543     ;; This group might be a dead group. In that case we have to get
3544     ;; the number of unread articles from `gnus-active-hashtb'.
3545     (if entry
3546         (setq number (car entry))
3547       (if (setq active (gnus-gethash group gnus-active-hashtb))
3548           (setq number (- (1+ (cdr active)) (car active)))))
3549     (gnus-summary-read-group 
3550      group (or all (and (numberp number) 
3551                         (zerop (+ number (length (cdr (assq 'tick marked)))
3552                                   (length (cdr (assq 'dormant marked)))))))
3553      no-article)))
3554
3555 (defun gnus-group-select-group (all)
3556   "Select this newsgroup.
3557 No article is selected automatically.
3558 If argument ALL is non-nil, already read articles become readable."
3559   (interactive "P")
3560   (gnus-group-read-group all t))
3561
3562 (defun gnus-group-jump-to-group (group)
3563   "Jump to newsgroup GROUP."
3564   (interactive (list (completing-read "Group: " gnus-active-hashtb nil
3565                                       (not (not gnus-read-active-file)))))
3566
3567   (if (equal group "")
3568       (error "empty group name"))
3569
3570   (let ((b (text-property-any (point-min) (point-max) 
3571                               'gnus-group (intern group))))
3572     (if b
3573         ;; Either go to the line in the group buffer...
3574         (goto-char b)
3575       ;; ... or insert the line.
3576       (or
3577        (gnus-gethash group gnus-active-hashtb)
3578        (gnus-activate-newsgroup group)
3579        (error "%s error: %s" group (gnus-status-message group)))
3580
3581       (gnus-group-update-group group)
3582       (goto-char (text-property-any (point-min) (point-max) 
3583                                     'gnus-group (intern group)))))
3584   ;; Adjust cursor point.
3585   (gnus-group-position-cursor))
3586
3587 (defun gnus-group-goto-group (group)
3588   "Goto to newsgroup GROUP."
3589   (let ((b (text-property-any (point-min) (point-max) 
3590                               'gnus-group (intern group))))
3591     (and b (goto-char b))))
3592
3593 (defun gnus-group-next-group (n)
3594   "Go to next N'th newsgroup.
3595 If N is negative, search backward instead.
3596 Returns the difference between N and the number of skips actually
3597 done."
3598   (interactive "p")
3599   (gnus-group-next-unread-group n t))
3600
3601 (defun gnus-group-next-unread-group (n &optional all level)
3602   "Go to next N'th unread newsgroup.
3603 If N is negative, search backward instead.
3604 If ALL is non-nil, choose any newsgroup, unread or not.
3605 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3606 such group can be found, the next group with a level higher than
3607 LEVEL.
3608 Returns the difference between N and the number of skips actually
3609 made."
3610   (interactive "p")
3611   (let ((backward (< n 0))
3612         (n (abs n)))
3613     (while (and (> n 0)
3614                 (gnus-group-search-forward 
3615                  backward (or (not gnus-group-goto-unread) all) level))
3616       (setq n (1- n)))
3617     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3618                                (if level " on this level or higher" "")))
3619     n))
3620
3621 (defun gnus-group-prev-group (n)
3622   "Go to previous N'th newsgroup.
3623 Returns the difference between N and the number of skips actually
3624 done."
3625   (interactive "p")
3626   (gnus-group-next-unread-group (- n) t))
3627
3628 (defun gnus-group-prev-unread-group (n)
3629   "Go to previous N'th unread newsgroup.
3630 Returns the difference between N and the number of skips actually
3631 done."  
3632   (interactive "p")
3633   (gnus-group-next-unread-group (- n)))
3634
3635 (defun gnus-group-next-unread-group-same-level (n)
3636   "Go to next N'th unread newsgroup on the same level.
3637 If N is negative, search backward instead.
3638 Returns the difference between N and the number of skips actually
3639 done."
3640   (interactive "p")
3641   (gnus-group-next-unread-group n t (gnus-group-group-level))
3642   (gnus-group-position-cursor))
3643
3644 (defun gnus-group-prev-unread-group-same-level (n)
3645   "Go to next N'th unread newsgroup on the same level.
3646 Returns the difference between N and the number of skips actually
3647 done."
3648   (interactive "p")
3649   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3650   (gnus-group-position-cursor))
3651
3652 (defun gnus-group-best-unread-group (&optional exclude-group)
3653   "Go to the group with the highest level.
3654 If EXCLUDE-GROUP, do not go to that group."
3655   (interactive)
3656   (goto-char (point-min))
3657   (let ((best 100000)
3658         unread best-point)
3659     (while (setq unread (get-text-property (point) 'gnus-unread))
3660       (if (and (numberp unread) (> unread 0))
3661           (progn
3662             (or best-point (setq best-point (point)))
3663             (if (and (< (get-text-property (point) 'gnus-level) best)
3664                      (or (not exclude-group)
3665                          (not (equal exclude-group (gnus-group-group-name)))))
3666                 (progn 
3667                   (setq best (get-text-property (point) 'gnus-level))
3668                   (setq best-point (point))))))
3669       (forward-line 1))
3670     (if best-point (goto-char best-point))
3671     (gnus-summary-position-cursor)
3672     (and best-point (gnus-group-group-name))))
3673
3674 (defun gnus-group-first-unread-group ()
3675   "Go to the first group with unread articles."
3676   (interactive)
3677   (goto-char (point-min))
3678   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3679       (gnus-group-next-unread-group 1))
3680   (gnus-group-position-cursor))
3681
3682 (defun gnus-group-enter-server-mode ()
3683   "Jump to the server buffer."
3684   (interactive)
3685   (gnus-server-setup-buffer)
3686   (gnus-configure-windows 'server)
3687   (gnus-server-prepare))
3688
3689 (defun gnus-group-make-group (name method address)
3690   "Add a new newsgroup.
3691 The user will be prompted for a NAME, for a select METHOD, and an
3692 ADDRESS."
3693   (interactive
3694    (cons 
3695     (read-string "Group name: ")
3696     (let ((method
3697            (completing-read 
3698             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3699             nil t)))
3700       (if (assoc method gnus-valid-select-methods)
3701           (list method
3702                 (if (memq 'prompt-address
3703                           (assoc method gnus-valid-select-methods))
3704                     (read-string "Address: ")
3705                   ""))
3706         (list method nil)))))
3707   
3708   (let* ((meth (if address (list (intern method) address) method))
3709          (nname (gnus-group-prefixed-name name meth))
3710          info)
3711     (and (gnus-gethash nname gnus-newsrc-hashtb)
3712          (error "Group %s already exists" nname))
3713     (gnus-group-change-level 
3714      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3715      gnus-level-default-subscribed gnus-level-killed 
3716      (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3717                        gnus-newsrc-hashtb) t)
3718     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3719     (gnus-dribble-enter 
3720      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3721     (gnus-group-insert-group-line-info nname)
3722
3723     (and (gnus-check-backend-function 'request-create-group nname)
3724          (gnus-request-create-group nname))))
3725
3726 (defun gnus-group-edit-group (group &optional part)
3727   "Edit the group on the current line."
3728   (interactive (list (gnus-group-group-name)))
3729   (let ((done-func '(lambda () 
3730                       "Exit editing mode and update the information."
3731                       (interactive)
3732                       (gnus-group-edit-group-done 'part 'group)))
3733         (part (or part 'info))
3734         (winconf (current-window-configuration))
3735         info)
3736     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3737       (error "No group on current line"))
3738     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3739     (gnus-configure-windows 'edit-group)
3740     (gnus-add-current-to-buffer-list)
3741     (emacs-lisp-mode)
3742     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3743     (use-local-map (copy-keymap emacs-lisp-mode-map))
3744     (local-set-key "\C-c\C-c" done-func)
3745     (make-local-variable 'gnus-prev-winconf)
3746     (setq gnus-prev-winconf winconf)
3747     ;; We modify the func to let it know what part it is editing.
3748     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3749     (setcar (cdr (cdr (nth 4 done-func))) group)
3750     (erase-buffer)
3751     (insert
3752      (cond 
3753       ((eq part 'method)
3754        ";; Type `C-c C-c' after editing the select method.\n\n")
3755       ((eq part 'params)
3756        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3757       ((eq part 'info)
3758        ";; Type `C-c C-c' after editing the group info.\n\n")))
3759     (let ((cinfo (gnus-copy-sequence info))
3760           marked)
3761       (if (not (setq marked (nth 3 cinfo)))
3762           ()
3763         (while marked
3764           (or (eq 'score (car (car marked)))
3765               (eq 'bookmark (car (car marked)))
3766               (eq 'killed (car (car marked)))
3767               (setcdr (car marked) 
3768                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3769           (setq marked (cdr marked))))
3770       (insert 
3771        (pp-to-string
3772         (cond ((eq part 'method)
3773                (or (nth 4 info) "native"))
3774               ((eq part 'params)
3775                (nth 5 info))
3776               (t
3777                cinfo)))
3778        "\n"))))
3779
3780 (defun gnus-group-edit-group-method (group)
3781   "Edit the select method of GROUP."
3782   (interactive (list (gnus-group-group-name)))
3783   (gnus-group-edit-group group 'method))
3784
3785 (defun gnus-group-edit-group-parameters (group)
3786   "Edit the group parameters of GROUP."
3787   (interactive (list (gnus-group-group-name)))
3788   (gnus-group-edit-group group 'params))
3789
3790 (defun gnus-group-edit-group-done (part group)
3791   "Get info from buffer, update variables and jump to the group buffer."
3792   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3793   (goto-char (point-min))
3794   (let ((form (read (current-buffer)))
3795         (winconf gnus-prev-winconf))
3796     (if (eq part 'info) 
3797         (gnus-group-set-info form)
3798       (gnus-group-set-info form group part))
3799     (kill-buffer (current-buffer))
3800     (and winconf (set-window-configuration winconf))
3801     (set-buffer gnus-group-buffer)
3802     (gnus-group-update-group (gnus-group-group-name))
3803     (gnus-group-position-cursor)))
3804
3805 (defun gnus-group-make-help-group ()
3806   "Create the (ding) Gnus documentation group."
3807   (interactive)
3808   (and (gnus-gethash (gnus-group-prefixed-name "gnus-help" '(nndoc ""))
3809                      gnus-newsrc-hashtb)
3810        (error "Documentation group already exists"))
3811   (let ((path load-path))
3812     (while (and path
3813                 (not (file-exists-p (concat (file-name-as-directory (car path))
3814                                             "doc.txt"))))
3815       (setq path (cdr path)))
3816     (or path (error "Couldn't find doc group"))
3817     (gnus-group-make-group 
3818      "gnus-help" "nndoc" 
3819      (concat (file-name-as-directory (car path)) "doc.txt"))
3820     (gnus-group-position-cursor)))
3821
3822 (defun gnus-group-make-archive-group ()
3823   "Create the (ding) Gnus archive group."
3824   (interactive)
3825   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3826                      gnus-newsrc-hashtb)
3827        (error "Archive group already exists"))
3828   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
3829   (gnus-group-position-cursor))
3830
3831 (defun gnus-group-make-directory-group (dir)
3832   "Create an nndir group.
3833 The user will be prompted for a directory. The contents of this
3834 directory will be used as a newsgroup. The directory should contain
3835 mail messages or news articles in files that have numeric names."
3836   (interactive
3837    (list (read-file-name "Create group from directory: ")))
3838   (or (file-exists-p dir) (error "No such directory"))
3839   (or (file-directory-p dir) (error "Not a directory"))
3840   (gnus-group-make-group dir "nndir" dir)
3841   (gnus-group-position-cursor))
3842
3843 (defun gnus-group-make-kiboze-group (group address scores)
3844   "Create an nnkiboze group.
3845 The user will be prompted for a name, a regexp to match groups, and
3846 score file entries for articles to include in the group."
3847   (interactive
3848    (list
3849     (read-string "nnkiboze group name: ")
3850     (read-string "Source groups (regexp): ")
3851     (let ((headers (mapcar (lambda (group) (list group))
3852                            '("subject" "from" "number" "date" "message-id"
3853                              "references" "chars" "lines" "xref")))
3854           scores header regexp regexps)
3855       (while (not (equal "" (setq header (completing-read 
3856                                           "Match on header: " headers nil t))))
3857         (setq regexps nil)
3858         (while (not (equal "" (setq regexp (read-string 
3859                                             (format "Match on %s (string): "
3860                                                     header)))))
3861           (setq regexps (cons (list regexp nil nil 'r) regexps)))
3862         (setq scores (cons (cons header regexps) scores)))
3863       scores)))
3864   (gnus-group-make-group group "nnkiboze" address)
3865   (save-excursion
3866     (gnus-set-work-buffer)
3867     (let (emacs-lisp-mode-hook)
3868       (pp scores (current-buffer)))
3869     (write-region (point-min) (point-max) 
3870                   (concat (or gnus-kill-files-directory "~/News")
3871                           "nnkiboze:" group "." gnus-score-file-suffix)))
3872   (gnus-group-position-cursor))
3873
3874 (defun gnus-group-add-to-virtual (n vgroup)
3875   "Add the current group to a virtual group."
3876   (interactive
3877    (list current-prefix-arg
3878          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t)))
3879   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
3880       (error "%s is not an nnvirtual group" vgroup))
3881   (let* ((groups (gnus-group-process-prefix n))
3882          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
3883     (setcar (cdr method)
3884             (concat 
3885              (nth 1 method) "\\|"
3886              (mapconcat 
3887               (lambda (s) 
3888                 (gnus-group-remove-mark s)
3889                 (concat "\\(^" (regexp-quote s) "$\\)"))
3890               groups "\\|")))))
3891
3892 ;; Group sorting commands
3893 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
3894
3895 (defun gnus-group-sort-groups ()
3896   "Sort the group buffer using `gnus-group-sort-function'."
3897   (interactive)
3898   (setq gnus-newsrc-alist 
3899         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
3900   (gnus-make-hashtable-from-newsrc-alist)
3901   (gnus-get-unread-articles (1+ gnus-level-subscribed))
3902   (gnus-group-list-groups nil))
3903
3904 (defun gnus-group-sort-by-alphabet (info1 info2)
3905   (string< (car info1) (car info2)))
3906
3907 (defun gnus-group-sort-by-unread (info1 info2)
3908   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
3909         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
3910     (< (or (and (numberp n1) n1) 0)
3911        (or (and (numberp n2) n2) 0))))
3912
3913 (defun gnus-group-sort-by-level (info1 info2)
3914   (< (nth 1 info1) (nth 1 info2)))
3915
3916 ;; Group catching up.
3917
3918 (defun gnus-group-catchup-current (n &optional all)
3919   "Mark all articles not marked as unread in current newsgroup as read.
3920 If prefix argument N is numeric, the ARG next newsgroups will be
3921 caught up. If ALL is non-nil, marked articles will also be marked as
3922 read. Cross references (Xref: header) of articles are ignored.
3923 The difference between N and actual number of newsgroups that were
3924 caught up is returned."
3925   (interactive "P")
3926   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
3927                gnus-expert-user
3928                (gnus-y-or-n-p
3929                 (if all
3930                     "Do you really want to mark all articles as read? "
3931                   "Mark all unread articles as read? "))))
3932       n
3933     (let ((groups (gnus-group-process-prefix n))
3934           (ret 0))
3935       (while groups
3936         (gnus-group-remove-mark (car groups))
3937         (if (not (gnus-group-goto-group (car groups)))
3938             (setq ret (1+ ret))
3939           (gnus-group-catchup (car groups) all)
3940           (gnus-group-update-group-line))
3941         (setq groups (cdr groups)))
3942       (gnus-group-next-unread-group 1)
3943       ret)))
3944
3945 (defun gnus-group-catchup-current-all (n)
3946   "Mark all articles in current newsgroup as read.
3947 Cross references (Xref: header) of articles are ignored."
3948   (interactive "P")
3949   (gnus-group-catchup-current n 'all))
3950
3951 (defun gnus-group-catchup (group &optional all)
3952   "Mark all articles in GROUP as read.
3953 If ALL is non-nil, all articles are marked as read.
3954 The return value is the number of articles that were marked as read,
3955 or nil if no action could be taken."
3956   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3957          (num (car entry))
3958          (marked (nth 3 (nth 2 entry)))
3959          ticked)
3960     (if (not (numberp (car entry)))
3961         (gnus-message 1 "Can't catch up; non-active group")
3962       ;; Do the updating only if the newsgroup isn't killed.
3963       (if entry
3964           (progn
3965             (setq ticked (if all nil (cdr (assq 'tick marked))))
3966             (gnus-update-read-articles group ticked nil ticked)
3967             (if (and all marked)
3968                 (setcar (nthcdr 3 (nth 2 entry)) 
3969                         (delq (assq 'dormant marked) marked))))))
3970     num))
3971
3972 (defun gnus-group-expire-articles (n)
3973   "Expire all expirable articles in the current newsgroup."
3974   (interactive "P")
3975   (let ((groups (gnus-group-process-prefix n))
3976         group)
3977     (or groups (error "No groups to expire"))
3978     (while groups
3979       (setq group (car groups)
3980             groups (cdr groups))
3981       (gnus-group-remove-mark group)
3982       (if (not (gnus-check-backend-function 'request-expire-articles group))
3983           ()
3984         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3985                (expirable (if (memq 'total-expire (nth 5 info))
3986                               (cons nil (gnus-list-of-read-articles group))
3987                             (assq 'expire (nth 3 info)))))
3988           (and expirable 
3989                (setcdr expirable
3990                        (gnus-request-expire-articles 
3991                         (cdr expirable) group))))))))
3992
3993 (defun gnus-group-expire-all-groups ()
3994   "Expire all expirable articles in all newsgroups."
3995   (interactive)
3996   (gnus-message 5 "Expiring...")
3997   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
3998                                    (cdr gnus-newsrc-alist))))
3999     (gnus-group-expire-articles nil))
4000   (gnus-message 5 "Expiring...done"))
4001
4002 (defun gnus-group-set-current-level (n level)
4003   "Set the level of the next N groups to LEVEL."
4004   (interactive "P\nnLevel: ")
4005   (or (and (>= level 1) (<= level gnus-level-killed))
4006       (error "Illegal level: %d" level))
4007   (let ((groups (gnus-group-process-prefix n))
4008         group)
4009     (while groups
4010       (setq group (car groups)
4011             groups (cdr groups))
4012       (gnus-group-remove-mark group)
4013       (gnus-message 6 "Changed level of %s from %d to %d" 
4014                     group (gnus-group-group-level) level)
4015       (gnus-group-change-level group level
4016                                (gnus-group-group-level))
4017       (gnus-group-update-group-line)))
4018   (gnus-group-position-cursor))
4019
4020 (defun gnus-group-unsubscribe-current-group (arg)
4021   "Toggle subscribe from/to unsubscribe current group."
4022   (interactive "P")
4023   (let ((group (gnus-group-group-name)))
4024     (or group (error "No newsgroup on current line"))
4025     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
4026                           gnus-level-default-unsubscribed
4027                         gnus-level-default-subscribed)))
4028     (gnus-group-unsubscribe-group group arg)
4029     (gnus-group-next-group 1)))
4030
4031 (defun gnus-group-unsubscribe-group (group &optional level)
4032   "Toggle subscribe from/to unsubscribe GROUP.
4033 New newsgroup is added to .newsrc automatically."
4034   (interactive
4035    (list (completing-read "Group: " gnus-active-hashtb nil 
4036                           gnus-have-read-active-file)))
4037   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4038     (cond (newsrc
4039            ;; Toggle subscription flag.
4040            (gnus-group-change-level 
4041             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4042                                            gnus-level-subscribed) 
4043                                        (1+ gnus-level-subscribed)
4044                                      gnus-level-default-subscribed)))
4045            (gnus-group-update-group group))
4046           ((and (stringp group)
4047                 (or (not gnus-have-read-active-file)
4048                     (gnus-gethash group gnus-active-hashtb)))
4049            ;; Add new newsgroup.
4050            (gnus-group-change-level 
4051             group 
4052             (if level level gnus-level-default-subscribed) 
4053             (or (and (member group gnus-zombie-list) 
4054                      gnus-level-zombie) 
4055                 gnus-level-killed)
4056             (and (gnus-group-group-name)
4057                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4058            (gnus-group-update-group group))
4059           (t (error "No such newsgroup: %s" group)))
4060     (gnus-group-position-cursor)))
4061
4062 (defun gnus-group-transpose-groups (n)
4063   "Move the current newsgroup up N places.
4064 If given a negative prefix, move down instead. The difference between
4065 N and the number of steps taken is retured." 
4066   (interactive "p")
4067   (or (gnus-group-group-name)
4068       (error "No group on current line"))
4069   (gnus-group-kill-group 1)
4070   (prog1
4071       (forward-line (- n))
4072     (gnus-group-yank-group)
4073     (gnus-group-position-cursor)))
4074
4075 (defun gnus-group-kill-all-zombies ()
4076   "Kill all zombie newsgroups."
4077   (interactive)
4078   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4079   (setq gnus-zombie-list nil)
4080   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4081   (goto-char (point-min))
4082   (gnus-group-position-cursor))
4083
4084 (defun gnus-group-kill-region (begin end)
4085   "Kill newsgroups in current region (excluding current point).
4086 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4087   (interactive "r")
4088   (let ((lines
4089          ;; Exclude a line where current point is on.
4090          (1-
4091           ;; Count lines.
4092           (save-excursion
4093             (count-lines
4094              (progn
4095                (goto-char begin)
4096                (beginning-of-line)
4097                (point))
4098              (progn
4099                (goto-char end)
4100                (end-of-line)
4101                (point)))))))
4102     (goto-char begin)
4103     (beginning-of-line)                 ;Important when LINES < 1
4104     (gnus-group-kill-group lines)))
4105
4106 (defun gnus-group-kill-group (n)
4107   "The the next N groups.
4108 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4109 However, only groups that were alive can be yanked; already killed 
4110 groups or zombie groups can't be yanked.
4111 The return value is the name of the (last) group that was killed."
4112   (interactive "P")
4113   (let ((buffer-read-only nil)
4114         (groups (gnus-group-process-prefix n))
4115         group entry level)
4116     (while groups
4117       (setq group (car groups)
4118             groups (cdr groups))
4119       (gnus-group-remove-mark group)
4120       (setq level (gnus-group-group-level))
4121       (gnus-delete-line)
4122       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4123           (setq gnus-list-of-killed-groups 
4124                 (cons (cons (car entry) (nth 2 entry)) 
4125                       gnus-list-of-killed-groups)))
4126       (gnus-group-change-level 
4127        (if entry entry group) gnus-level-killed (if entry nil level)))
4128     (gnus-group-position-cursor)
4129     group))
4130
4131 (defun gnus-group-yank-group (&optional arg)
4132   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4133 inserting it before the current newsgroup.  The numeric ARG specifies
4134 how many newsgroups are to be yanked.  The name of the (last)
4135 newsgroup yanked is returned."
4136   (interactive "p")
4137   (if (not arg) (setq arg 1))
4138   (let (info group prev)
4139     (while (>= (setq arg (1- arg)) 0)
4140       (if (not (setq info (car gnus-list-of-killed-groups)))
4141           (error "No more newsgroups to yank"))
4142       (setq group (nth 2 info))
4143       ;; Find which newsgroup to insert this one before - search
4144       ;; backward until something suitable is found. If there are no
4145       ;; other newsgroups in this buffer, just make this newsgroup the
4146       ;; first newsgroup.
4147       (setq prev (gnus-group-group-name))
4148       (gnus-group-change-level 
4149        info (nth 2 info) gnus-level-killed 
4150        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4151        t)
4152       (gnus-group-insert-group-line-info (nth 1 info))
4153       (setq gnus-list-of-killed-groups 
4154             (cdr gnus-list-of-killed-groups)))
4155     (forward-line -1)
4156     (gnus-group-position-cursor)
4157     group))
4158       
4159 (defun gnus-group-list-all-groups (arg)
4160   "List all newsgroups with level ARG or lower.
4161 Default is gnus-level-unsubscribed, which lists all subscribed and most
4162 unsubscribed groups."
4163   (interactive "P")
4164   (setq arg (or arg gnus-level-unsubscribed))
4165   (gnus-group-list-groups arg t))
4166
4167 (defun gnus-group-list-killed ()
4168   "List all killed newsgroups in the group buffer."
4169   (interactive)
4170   (if (not gnus-killed-list)
4171       (gnus-message 6 "No killed groups")
4172     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4173     (goto-char (point-min)))
4174   (gnus-group-position-cursor))
4175
4176 (defun gnus-group-list-zombies ()
4177   "List all zombie newsgroups in the group buffer."
4178   (interactive)
4179   (if (not gnus-zombie-list)
4180       (gnus-message 6 "No zombie groups")
4181     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4182     (goto-char (point-min)))
4183   (gnus-group-position-cursor))
4184
4185 (defun gnus-group-get-new-news (&optional arg)
4186   "Get newly arrived articles.
4187 If ARG is non-nil, it should be a number between one and nine to
4188 specify which levels you are interested in re-scanning."
4189   (interactive "P")
4190   (run-hooks 'gnus-get-new-news-hook)
4191   (let ((level arg))
4192     (if gnus-group-use-permanent-levels
4193         (progn
4194           (if level
4195               (setq gnus-group-default-list-level level)
4196             (setq level (or gnus-group-default-list-level 
4197                             gnus-level-subscribed)))))
4198     (if (and gnus-read-active-file (not level))
4199         (progn
4200           (gnus-read-active-file)
4201           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4202       (let ((gnus-read-active-file nil))
4203         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4204     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4205                                 gnus-level-subscribed)
4206                             gnus-have-all-newsgroups)))
4207
4208 (defun gnus-group-get-new-news-this-group (n)
4209   "Check for newly arrived news in the current group (and the N-1 next groups).
4210 The difference between N and the number of newsgroup checked is returned.
4211 If N is negative, this group and the N-1 previous groups will be checked."
4212   (interactive "P")
4213   (let* ((groups (gnus-group-process-prefix n))
4214          (ret (if (numberp n) (- n (length groups)) 0))
4215          (w-p (window-start))
4216          group)
4217     (while groups
4218       (setq group (car groups)
4219             groups (cdr groups))
4220       (gnus-group-remove-mark group)
4221       (or (gnus-get-new-news-in-group group)
4222           (progn 
4223             (ding) 
4224             (message "%s error: %s" group (gnus-status-message group))
4225             (sit-for 2))))
4226     ;; !!! I don't know why the buffer scrolls forward when updating
4227     ;; the first line in the group buffer, but it does. So we set the
4228     ;; window start forcibly.
4229 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4230     (forward-line 1)
4231     (gnus-summary-position-cursor)
4232     ret))
4233
4234 (defun gnus-get-new-news-in-group (group)
4235   (and group 
4236        (gnus-activate-newsgroup group)
4237        (progn
4238          (gnus-get-unread-articles-in-group 
4239           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4240           (gnus-gethash group gnus-active-hashtb))
4241          (gnus-group-update-group-line)
4242          t)))
4243
4244 (defun gnus-group-fetch-faq (group)
4245   "Fetch the FAQ for the current group."
4246   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4247   (or group (error "No group name given"))
4248   (let ((file (concat gnus-group-faq-directory group))) 
4249     (if (not (file-exists-p file))
4250         (error "No such file: %s" file)
4251       (find-file file))))
4252   
4253 (defun gnus-group-describe-group (force &optional group)
4254   "Display a description of the current newsgroup."
4255   (interactive "P")
4256   (and force (setq gnus-description-hashtb nil))
4257   (let ((group (or group (gnus-group-group-name)))
4258         desc)
4259     (or group (error "No group name given"))
4260     (and (or gnus-description-hashtb
4261              (setq desc (gnus-group-get-description group))
4262              (gnus-read-descriptions-file))
4263          (message
4264           (or desc (gnus-gethash group gnus-description-hashtb)
4265               "No description available")))))
4266
4267 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4268 (defun gnus-group-describe-all-groups (force)
4269   "Pop up a buffer with descriptons of all newsgroups."
4270   (interactive "P")
4271   (and force (setq gnus-description-hashtb nil))
4272   (if (not (or gnus-description-hashtb
4273                (gnus-read-descriptions-file)))
4274       (error "Couldn't request descriptions file"))
4275   (let ((buffer-read-only nil)
4276         b)
4277     (erase-buffer)
4278     (mapatoms
4279      (lambda (group)
4280        (setq b (point))
4281        (insert (format "      *: %-20s %s\n" (symbol-name group)
4282                        (symbol-value group)))
4283        (add-text-properties 
4284         b (1+ b) (list 'gnus-group group
4285                        'gnus-unread t 'gnus-marked nil
4286                        'gnus-level (1+ gnus-level-subscribed))))
4287      gnus-description-hashtb)
4288     (goto-char (point-min))
4289     (gnus-group-position-cursor)))
4290
4291 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4292 (defun gnus-group-apropos (regexp &optional search-description)
4293   "List all newsgroups that have names that match a regexp."
4294   (interactive "sGnus apropos (regexp): ")
4295   (let ((prev "")
4296         (obuf (current-buffer))
4297         groups des prev)
4298     ;; Go through all newsgroups that are known to Gnus.
4299     (mapatoms 
4300      (lambda (group)
4301        (and (string-match regexp (symbol-name group))
4302             (setq groups (cons (symbol-name group) groups))))
4303      gnus-active-hashtb)
4304     ;; Go through all descriptions that are known to Gnus. 
4305     (if search-description
4306         (mapatoms 
4307          (lambda (group)
4308            (and (string-match regexp (symbol-value group))
4309                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4310                 (setq groups (cons (symbol-name group) groups))))
4311          gnus-description-hashtb))
4312     (if (not groups)
4313         (gnus-message 3 "No groups matched \"%s\"." regexp)
4314       ;; Print out all the groups.
4315       (save-excursion
4316         (pop-to-buffer "*Gnus Help*")
4317         (buffer-disable-undo (current-buffer))
4318         (erase-buffer)
4319         (setq groups (sort groups 'string<))
4320         (while groups
4321           ;; Groups may be entered twice into the list of groups.
4322           (if (not (string= (car groups) prev))
4323               (progn
4324                 (insert (setq prev (car groups)) "\n")
4325                 (if (and gnus-description-hashtb
4326                          (setq des (gnus-gethash (car groups) 
4327                                                  gnus-description-hashtb)))
4328                     (insert "  " des "\n"))))
4329           (setq groups (cdr groups)))
4330         (goto-char (point-min))))
4331     (pop-to-buffer obuf)))
4332
4333 (defun gnus-group-description-apropos (regexp)
4334   "List all newsgroups that have names or desccriptions that match a regexp."
4335   (interactive "sGnus description apropos (regexp): ")
4336   (if (not (or gnus-description-hashtb
4337                (gnus-read-descriptions-file)))
4338       (error "Couldn't request descriptions file"))
4339   (gnus-group-apropos regexp t))
4340
4341 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4342 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4343   "List all groups with unread articles that match REGEXP.
4344 If the prefix LEVEL is non-nil, it should be a number that says which
4345 level to cut off listing groups. 
4346 If ALL, also list groups with no unread articles.
4347 If LOWEST, don't list groups with level lower than LOWEST."
4348   (interactive "P\nsList newsgroups matching: ")
4349   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4350                            all (or lowest 1) regexp)
4351   (goto-char (point-min))
4352   (gnus-group-position-cursor))
4353
4354 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4355   "List all groups that match REGEXP.
4356 If the prefix LEVEL is non-nil, it should be a number that says which
4357 level to cut off listing groups. 
4358 If LOWEST, don't list groups with level lower than LOWEST."
4359   (interactive "P\nsList newsgroups matching: ")
4360   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4361
4362 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4363 (defun gnus-group-save-newsrc ()
4364   "Save the Gnus startup files."
4365   (interactive)
4366   (gnus-save-newsrc-file))
4367
4368 (defun gnus-group-restart (&optional arg)
4369   "Force Gnus to read the .newsrc file."
4370   (interactive "P")
4371   (gnus-save-newsrc-file)
4372   (gnus-setup-news 'force)
4373   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4374
4375 (defun gnus-group-read-init-file ()
4376   "Read the Gnus elisp init file."
4377   (interactive)
4378   (gnus-read-init-file))
4379
4380 (defun gnus-group-check-bogus-groups ()
4381   "Check bogus newsgroups."
4382   (interactive)
4383   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
4384   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4385
4386 (defun gnus-group-mail ()
4387   "Start composing a mail."
4388   (interactive)
4389   (mail))
4390
4391 (defun gnus-group-edit-global-kill (article &optional group)
4392   "Edit the global kill file.
4393 If GROUP, edit that local kill file instead."
4394   (interactive "P")
4395   (setq gnus-current-kill-article article)
4396   (gnus-kill-file-edit-file group)
4397   (gnus-message 6
4398    (substitute-command-keys
4399     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4400
4401 (defun gnus-group-edit-local-kill (article group)
4402   "Edit a local kill file."
4403   (interactive (list nil (gnus-group-group-name)))
4404   (gnus-group-edit-global-kill article group))
4405
4406 (defun gnus-group-force-update ()
4407   "Update `.newsrc' file."
4408   (interactive)
4409   (gnus-save-newsrc-file))
4410
4411 (defun gnus-group-suspend ()
4412   "Suspend the current Gnus session.
4413 In fact, cleanup buffers except for group mode buffer.
4414 The hook gnus-suspend-gnus-hook is called before actually suspending."
4415   (interactive)
4416   (run-hooks 'gnus-suspend-gnus-hook)
4417   ;; Kill Gnus buffers except for group mode buffer.
4418   (let ((group-buf (get-buffer gnus-group-buffer)))
4419     ;; Do this on a separate list in case the user does a ^G before we finish
4420     (let ((gnus-buffer-list
4421            (delq group-buf (delq gnus-dribble-buffer
4422                                  (append gnus-buffer-list nil)))))
4423       (while gnus-buffer-list
4424         (gnus-kill-buffer (car gnus-buffer-list))
4425         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4426     (if group-buf
4427         (progn
4428           (setq gnus-buffer-list (list group-buf))
4429           (bury-buffer group-buf)
4430           (delete-windows-on group-buf t)))))
4431
4432 (defun gnus-group-clear-dribble ()
4433   "Clear all information from the dribble buffer."
4434   (interactive)
4435   (gnus-dribble-clear))
4436
4437 (defun gnus-group-exit ()
4438   "Quit reading news after updating .newsrc.eld and .newsrc.
4439 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4440   (interactive)
4441   (if (or noninteractive                ;For gnus-batch-kill
4442           (zerop (buffer-size))         ;No news is good news.
4443           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4444           (not gnus-interactive-exit)   ;Without confirmation
4445           gnus-expert-user
4446           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4447       (progn
4448         (run-hooks 'gnus-exit-gnus-hook)
4449         (gnus-save-newsrc-file)
4450         (gnus-close-backends)
4451         (gnus-clear-system))))
4452
4453 (defun gnus-close-backends ()
4454   ;; Send a close request to all backends that support such a request. 
4455   (let ((methods gnus-valid-select-methods)
4456         func)
4457     (while methods
4458       (if (fboundp (setq func (intern (concat (car (car methods))
4459                                               "-request-close"))))
4460           (funcall func))
4461       (setq methods (cdr methods)))))
4462
4463 (defun gnus-group-quit ()
4464   "Quit reading news without updating .newsrc.eld or .newsrc.
4465 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4466   (interactive)
4467   (if (or noninteractive                ;For gnus-batch-kill
4468           (zerop (buffer-size))
4469           (not (gnus-server-opened gnus-select-method))
4470           gnus-expert-user
4471           (not gnus-current-startup-file)
4472           (gnus-yes-or-no-p
4473            (format "Quit reading news without saving %s? "
4474                    (file-name-nondirectory gnus-current-startup-file))))
4475       (progn
4476         (run-hooks 'gnus-exit-gnus-hook)
4477         (gnus-dribble-save)
4478         (gnus-close-backends)
4479         (gnus-clear-system))))
4480
4481 (defun gnus-group-describe-briefly ()
4482   "Give a one line description of the group mode commands."
4483   (interactive)
4484   (gnus-message 6
4485    (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")))
4486
4487 (defun gnus-group-browse-foreign-server (method)
4488   "Browse a foreign news server.
4489 If called interactively, this function will ask for a select method
4490  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4491 If not, METHOD should be a list where the first element is the method
4492 and the second element is the address."
4493   (interactive
4494    (list (let ((how (completing-read 
4495                      "Which backend: "
4496                      (append gnus-valid-select-methods gnus-server-alist)
4497                      nil t "nntp")))
4498            ;; We either got a backend name or a virtual server name.
4499            ;; If the first, we also need an address.
4500            (if (assoc how gnus-valid-select-methods)
4501                (list (intern how)
4502                      ;; Suggested by mapjph@bath.ac.uk.
4503                      (completing-read 
4504                       "Address: " 
4505                       (mapcar (lambda (server) (list server))
4506                               gnus-secondary-servers)))
4507              ;; We got a server name, so we find the method.
4508              (gnus-server-to-method how)))))
4509   (gnus-browse-foreign-server method))
4510
4511 \f
4512 ;;;
4513 ;;; Browse Server Mode
4514 ;;;
4515
4516 (defvar gnus-browse-mode-hook nil)
4517 (defvar gnus-browse-mode-map nil)
4518 (put 'gnus-browse-mode 'mode-class 'special)
4519
4520 (if gnus-browse-mode-map
4521     nil
4522   (setq gnus-browse-mode-map (make-keymap))
4523   (suppress-keymap gnus-browse-mode-map)
4524   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4525   (define-key gnus-browse-mode-map "=" 'gnus-browse-read-group)
4526   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4527   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4528   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4529   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4530   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4531   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4532   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4533   (define-key gnus-browse-mode-map "\r" 'gnus-browse-read-group)
4534   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4535   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4536   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4537   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4538   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4539   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4540   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4541   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4542   )
4543
4544 (defvar gnus-browse-current-method nil)
4545 (defvar gnus-browse-return-buffer nil)
4546
4547 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4548
4549 (defun gnus-browse-foreign-server (method &optional return-buffer)
4550   (setq gnus-browse-current-method method)
4551   (setq gnus-browse-return-buffer return-buffer)
4552   (let ((gnus-select-method method)
4553         groups group)
4554     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4555     (or (gnus-server-opened method)
4556         (gnus-open-server method)
4557         (error "Unable to contact server: %s" (gnus-status-message method)))
4558     (or (gnus-request-list method)
4559         (error "Couldn't request list: %s" (gnus-status-message method)))
4560     (get-buffer-create gnus-browse-buffer)
4561     (gnus-add-current-to-buffer-list)
4562     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4563     (gnus-configure-windows 'browse)
4564     (buffer-disable-undo (current-buffer))
4565     (let ((buffer-read-only nil))
4566       (erase-buffer))
4567     (gnus-browse-mode)
4568     (setq mode-line-buffer-identification
4569           (format
4570            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4571     (save-excursion
4572       (set-buffer nntp-server-buffer)
4573       (let ((cur (current-buffer)))
4574         (goto-char (point-min))
4575         (or (string= gnus-ignored-newsgroups "")
4576             (delete-matching-lines gnus-ignored-newsgroups))
4577         (while (re-search-forward 
4578                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4579           (goto-char (match-end 1))
4580           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4581                                                      (match-end 1))
4582                                    (max 0 (- (1+ (read cur)) (read cur))))
4583                              groups)))))
4584     (setq groups (sort groups 
4585                        (lambda (l1 l2)
4586                          (string< (car l1) (car l2)))))
4587     (let ((buffer-read-only nil))
4588       (while groups
4589         (setq group (car groups))
4590         (insert 
4591          (format "K%7d: %s\n" (cdr group) (car group)))
4592         (setq groups (cdr groups))))
4593     (switch-to-buffer (current-buffer))
4594     (goto-char (point-min))
4595     (gnus-group-position-cursor)))
4596
4597 (defun gnus-browse-mode ()
4598   "Major mode for browsing a foreign server."
4599   (interactive)
4600   (kill-all-local-variables)
4601   (if gnus-visual (gnus-browse-make-menu-bar))
4602   (setq mode-line-modified "-- ")
4603   (make-local-variable 'mode-line-format)
4604   (setq mode-line-format (copy-sequence mode-line-format))
4605   (and (equal (nth 3 mode-line-format) "   ")
4606        (setcar (nthcdr 3 mode-line-format) ""))
4607   (setq major-mode 'gnus-browse-mode)
4608   (setq mode-name "Browse Server")
4609   (setq mode-line-process nil)
4610   (use-local-map gnus-browse-mode-map)
4611   (buffer-disable-undo (current-buffer))
4612   (setq truncate-lines t)
4613   (setq buffer-read-only t)
4614   (run-hooks 'gnus-browse-mode-hook))
4615
4616 (defun gnus-browse-read-group ()
4617   "Not implemented, and will probably never be."
4618   (interactive)
4619   (error "You can't read while browsing"))
4620
4621 (defun gnus-browse-next-group (n)
4622   "Go to the next group."
4623   (interactive "p")
4624   (prog1
4625       (forward-line n)
4626     (gnus-group-position-cursor)))
4627
4628 (defun gnus-browse-prev-group (n)
4629   "Go to the next group."
4630   (interactive "p")
4631   (gnus-browse-next-group (- n)))
4632
4633 (defun gnus-browse-unsubscribe-current-group (arg)
4634   "(Un)subscribe to the next ARG groups."
4635   (interactive "p")
4636   (and (eobp)
4637        (error "No group at current line."))
4638   (let ((ward (if (< arg 0) -1 1))
4639         (arg (abs arg)))
4640     (while (and (> arg 0)
4641                 (not (eobp))
4642                 (gnus-browse-unsubscribe-group)
4643                 (zerop (gnus-browse-next-group ward)))
4644       (setq arg (1- arg)))
4645     (gnus-group-position-cursor)
4646     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4647     arg))
4648   
4649 (defun gnus-browse-unsubscribe-group ()
4650   (let ((sub nil)
4651         (buffer-read-only nil)
4652         group)
4653     (save-excursion
4654       (beginning-of-line)
4655       (if (= (following-char) ?K) (setq sub t))
4656       (re-search-forward ": \\(.*\\)$" nil t)
4657       (setq group (gnus-group-prefixed-name 
4658                    (buffer-substring (match-beginning 1) (match-end 1))
4659                    gnus-browse-current-method))
4660       (beginning-of-line)
4661       (delete-char 1)
4662       (if sub
4663           (progn
4664             (gnus-group-change-level 
4665              (list t group gnus-level-default-subscribed
4666                    nil nil gnus-browse-current-method) 
4667              gnus-level-default-subscribed gnus-level-killed
4668              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4669              t)
4670             (insert ? ))
4671         (gnus-group-change-level 
4672          group gnus-level-killed gnus-level-default-subscribed)
4673         (insert ?K)))
4674     t))
4675
4676 (defun gnus-browse-exit ()
4677   "Quit browsing and return to the group buffer."
4678   (interactive)
4679   (if (eq major-mode 'gnus-browse-mode)
4680       (kill-buffer (current-buffer)))
4681   (if gnus-browse-return-buffer
4682       (gnus-configure-windows 'server)
4683     (gnus-configure-windows 'group)))
4684
4685 (defun gnus-browse-describe-briefly ()
4686   "Give a one line description of the group mode commands."
4687   (interactive)
4688   (gnus-message 6
4689    (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")))
4690       
4691 \f
4692 ;;;
4693 ;;; Gnus summary mode
4694 ;;;
4695
4696 (defvar gnus-summary-mode-map nil)
4697 (defvar gnus-summary-mark-map nil)
4698 (defvar gnus-summary-mscore-map nil)
4699 (defvar gnus-summary-article-map nil)
4700 (defvar gnus-summary-thread-map nil)
4701 (defvar gnus-summary-goto-map nil)
4702 (defvar gnus-summary-exit-map nil)
4703 (defvar gnus-summary-various-map nil)
4704 (defvar gnus-summary-interest-map nil)
4705 (defvar gnus-summary-sort-map nil)
4706 (defvar gnus-summary-backend-map nil)
4707 (defvar gnus-summary-save-map nil)
4708 (defvar gnus-summary-wash-map nil)
4709 (defvar gnus-summary-help-map nil)
4710
4711 (put 'gnus-summary-mode 'mode-class 'special)
4712
4713 (if gnus-summary-mode-map
4714     nil
4715   (setq gnus-summary-mode-map (make-keymap))
4716   (suppress-keymap gnus-summary-mode-map)
4717
4718   ;; Non-orthogonal keys
4719
4720   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4721   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4722   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4723   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4724   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4725   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4726   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4727   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4728   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4729   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
4730   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
4731   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
4732   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
4733   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
4734   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
4735   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
4736   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
4737   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
4738   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
4739   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
4740   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
4741   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
4742   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
4743   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
4744   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
4745   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
4746   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
4747   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
4748   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
4749   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
4750   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
4751   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
4752   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
4753   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
4754   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
4755   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
4756   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
4757   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
4758   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
4759   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
4760   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
4761   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
4762   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
4763   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
4764   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
4765   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
4766   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
4767   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
4768   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
4769   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
4770   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
4771   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
4772   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
4773   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
4774   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
4775   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
4776   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
4777   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
4778   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
4779   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
4780   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
4781   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
4782   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
4783   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
4784   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
4785   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
4786   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
4787   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
4788   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
4789   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
4790   (define-key gnus-summary-mode-map "V" 'gnus-version)
4791   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
4792   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
4793   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
4794   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
4795   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
4796   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
4797   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
4798   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
4799 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
4800   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
4801   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
4802   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
4803 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
4804   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
4805   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
4806   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
4807
4808
4809   ;; Sort of orthogonal keymap
4810   (define-prefix-command 'gnus-summary-mark-map)
4811   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
4812   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
4813   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
4814   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
4815   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
4816   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
4817   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
4818   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
4819   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
4820   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
4821   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
4822   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
4823   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
4824   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
4825   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
4826   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
4827   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
4828   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
4829   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
4830   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
4831   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
4832   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
4833   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
4834   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
4835
4836   (define-prefix-command 'gnus-summary-mscore-map)
4837   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
4838   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
4839   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
4840   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
4841   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
4842
4843   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
4844   
4845   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
4846   
4847   (define-prefix-command 'gnus-summary-goto-map)
4848   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
4849   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
4850   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
4851   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
4852   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
4853   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
4854   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
4855   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
4856   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
4857   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
4858   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
4859   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
4860   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
4861   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
4862
4863
4864   (define-prefix-command 'gnus-summary-thread-map)
4865   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
4866   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
4867   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
4868   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
4869   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
4870   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
4871   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
4872   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
4873   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
4874   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
4875   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
4876   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
4877   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
4878   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
4879
4880   
4881   (define-prefix-command 'gnus-summary-exit-map)
4882   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
4883   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
4884   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
4885   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
4886   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
4887   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
4888   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
4889   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
4890   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
4891   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
4892   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
4893
4894
4895   (define-prefix-command 'gnus-summary-article-map)
4896   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
4897   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
4898   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
4899   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
4900   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
4901   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
4902   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
4903   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
4904   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
4905   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
4906   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
4907   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
4908   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
4909   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
4910   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
4911   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
4912   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
4913   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
4914
4915
4916   (define-prefix-command 'gnus-summary-wash-map)
4917   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
4918   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
4919   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
4920   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
4921   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
4922   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
4923   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
4924   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
4925   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
4926   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
4927   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
4928   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
4929
4930   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
4931   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
4932   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
4933   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
4934   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
4935   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
4936
4937
4938   (define-prefix-command 'gnus-summary-help-map)
4939   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
4940   (define-key gnus-summary-help-map "v" 'gnus-version)
4941   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
4942   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
4943   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
4944   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
4945
4946
4947   (define-prefix-command 'gnus-summary-backend-map)
4948   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
4949   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
4950   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
4951   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
4952   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
4953   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
4954   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
4955   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
4956
4957
4958   (define-prefix-command 'gnus-summary-save-map)
4959   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
4960   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
4961   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
4962   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
4963   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
4964   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
4965   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
4966   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
4967
4968   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
4969   
4970   (define-prefix-command 'gnus-summary-various-map)
4971   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
4972   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
4973   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
4974   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
4975   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
4976   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
4977   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
4978   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
4979   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
4980   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
4981   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
4982
4983   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
4984
4985   (define-prefix-command 'gnus-summary-sort-map)
4986   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
4987   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
4988   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
4989   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
4990   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
4991   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
4992
4993   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
4994   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
4995   )
4996
4997
4998 \f
4999
5000 (defun gnus-summary-mode ()
5001   "Major mode for reading articles.
5002
5003 All normal editing commands are switched off.
5004 \\<gnus-summary-mode-map>
5005 Each line in this buffer represents one article.  To read an
5006 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5007 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5008 respectively.
5009
5010 You can also post articles and send mail from this buffer.  To 
5011 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5012 of an article, type `\\[gnus-summary-reply]'.
5013
5014 There are approx. one gazillion commands you can execute in this 
5015 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5016
5017 The following commands are available:
5018
5019 \\{gnus-summary-mode-map}"
5020   (interactive)
5021   (if gnus-visual (gnus-summary-make-menu-bar))
5022   (kill-all-local-variables)
5023   (let ((locals gnus-summary-local-variables))
5024     (while locals
5025       (if (consp (car locals))
5026           (progn
5027             (make-local-variable (car (car locals)))
5028             (set (car (car locals)) (eval (cdr (car locals)))))
5029         (make-local-variable (car locals))
5030         (set (car locals) nil))
5031       (setq locals (cdr locals))))
5032   (gnus-make-thread-indent-array)
5033   (gnus-update-format-specifications)
5034   (setq mode-line-modified "-- ")
5035   (make-local-variable 'mode-line-format)
5036   (setq mode-line-format (copy-sequence mode-line-format))
5037   (and (equal (nth 3 mode-line-format) "   ")
5038        (setcar (nthcdr 3 mode-line-format) ""))
5039   (setq major-mode 'gnus-summary-mode)
5040   (setq mode-name "Summary")
5041   (make-local-variable 'minor-mode-alist)
5042   (use-local-map gnus-summary-mode-map)
5043   (buffer-disable-undo (current-buffer))
5044   (setq buffer-read-only t)             ;Disable modification
5045   (setq truncate-lines t)
5046   (setq selective-display t)
5047   (setq selective-display-ellipses t)   ;Display `...'
5048   (setq buffer-display-table gnus-summary-display-table)
5049   (run-hooks 'gnus-summary-mode-hook))
5050
5051 (defun gnus-summary-make-display-table ()
5052   ;; Change the display table.  Odd characters have a tendency to mess
5053   ;; up nicely formatted displays - we make all possible glyphs
5054   ;; display only a single character.
5055
5056   ;; We start from the standard display table, if any.
5057   (setq gnus-summary-display-table 
5058         (or (copy-sequence standard-display-table)
5059             (make-display-table)))
5060   ;; Nix out all the control chars...
5061   (let ((i 32))
5062     (while (>= (setq i (1- i)) 0)
5063       (aset gnus-summary-display-table i [??])))
5064   ;; ... but not newline, of course.
5065   (aset gnus-summary-display-table ?\n nil)
5066   ;; We nix out any glyphs over 126 that are not set already.  
5067   (let ((i 256))
5068     (while (>= (setq i (1- i)) 127)
5069       ;; Only modify if the entry is nil.
5070       (or (aref gnus-summary-display-table i) 
5071           (aset gnus-summary-display-table i [??])))))
5072
5073 (defun gnus-summary-clear-local-variables ()
5074   (let ((locals gnus-summary-local-variables))
5075     (while locals
5076       (if (consp (car locals))
5077           (and (vectorp (car (car locals)))
5078                (set (car (car locals)) nil))
5079         (and (vectorp (car locals))
5080              (set (car locals) nil)))
5081       (setq locals (cdr locals)))))
5082
5083 (defun gnus-mouse-pick-article (e)
5084   (interactive "e")
5085   (mouse-set-point e)
5086   (gnus-summary-next-page nil t))
5087
5088 (defun gnus-summary-setup-buffer (group)
5089   "Initialize summary buffer."
5090   (let ((buffer (concat "*Summary " group "*")))
5091     (if (get-buffer buffer)
5092         (progn
5093           (set-buffer buffer)
5094           (not gnus-newsgroup-threads))
5095       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5096       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5097       (gnus-add-current-to-buffer-list)
5098       (gnus-summary-mode)
5099       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5100       (setq gnus-newsgroup-name group)
5101       t)))
5102
5103 (defun gnus-set-global-variables ()
5104   ;; Set the global equivalents of the summary buffer-local variables
5105   ;; to the latest values they had. These reflect the summary buffer
5106   ;; that was in action when the last article was fetched.
5107   (if (eq major-mode 'gnus-summary-mode) 
5108       (progn
5109         (setq gnus-summary-buffer (current-buffer))
5110         (let ((name gnus-newsgroup-name)
5111               (marked gnus-newsgroup-marked)
5112               (unread gnus-newsgroup-unreads)
5113               (headers gnus-current-headers)
5114               (score-file gnus-current-score-file))
5115           (save-excursion
5116             (set-buffer gnus-group-buffer)
5117             (setq gnus-newsgroup-name name)
5118             (setq gnus-newsgroup-marked marked)
5119             (setq gnus-newsgroup-unreads unread)
5120             (setq gnus-current-headers headers)
5121             (setq gnus-current-score-file score-file))))))
5122
5123 (defun gnus-summary-insert-dummy-line (sformat subject number)
5124   (if (not sformat) 
5125       (setq sformat gnus-summary-dummy-line-format-spec))
5126   (let (b)
5127     (beginning-of-line)
5128     (setq b (point))
5129     (insert (eval sformat))
5130     (add-text-properties
5131      b (1+ b)
5132      (list 'gnus (list number ?Z 0)))))
5133
5134 (defvar gnus-thread-indent-array nil)
5135 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5136 (defun gnus-make-thread-indent-array ()
5137   (let ((n 200))
5138     (if (and gnus-thread-indent-array
5139              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5140         nil
5141       (setq gnus-thread-indent-array (make-vector 201 "")
5142             gnus-thread-indent-array-level gnus-thread-indent-level)
5143       (while (>= n 0)
5144         (aset gnus-thread-indent-array n
5145               (make-string (* n gnus-thread-indent-level) ? ))
5146         (setq n (1- n))))))
5147
5148 (defun gnus-summary-insert-line 
5149   (sformat header level current unread replied expirable subject-or-nil
5150            &optional dummy score)
5151   (or sformat (setq sformat gnus-summary-line-format-spec))
5152   (let* ((indentation (aref gnus-thread-indent-array level))
5153          (lines (header-lines header))
5154          (score (or score gnus-summary-default-score 0))
5155          (score-char (if (or (null gnus-summary-default-score)
5156                              (= score gnus-summary-default-score)) ? 
5157                        (if (< score gnus-summary-default-score) 
5158                            gnus-score-below-mark gnus-score-over-mark)))
5159          (replied (if replied gnus-replied-mark ? ))
5160          (from (header-from header))
5161          (name-address (funcall gnus-extract-address-components from))
5162          (address (car (cdr name-address)))
5163          (name (or (car name-address) (car (cdr name-address))))
5164          (subject (header-subject header))
5165          (number (header-number header))
5166          (opening-bracket (if dummy ?\< ?\[))
5167          (closing-bracket (if dummy ?\> ?\]))
5168          (buffer-read-only nil)
5169          (b (progn (beginning-of-line) (point))))
5170     (or (numberp lines) (setq lines 0))
5171     (insert (eval sformat))
5172     (add-text-properties
5173      b (1+ b) (list 'gnus (list number (or unread gnus-unread-mark) level)))))
5174
5175 (defun gnus-summary-update-line (&optional dont-update)
5176   ;; Update summary line after change.
5177   (or (not gnus-summary-default-score)
5178       gnus-summary-inhibit-highlight
5179       (let ((gnus-summary-inhibit-highlight t)
5180             (article (gnus-summary-article-number)))
5181         (progn
5182           (or dont-update
5183               (if (and gnus-summary-mark-below
5184                        (< (gnus-summary-article-score)
5185                           gnus-summary-mark-below))
5186                   (and (not (memq article gnus-newsgroup-marked))
5187                        (not (memq article gnus-newsgroup-dormant))
5188                        (memq article gnus-newsgroup-unreads)
5189                        (gnus-summary-mark-article nil gnus-low-score-mark))
5190                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5191                      (gnus-summary-mark-article nil gnus-unread-mark))))
5192           (and gnus-visual
5193                (run-hooks 'gnus-summary-update-hook))))))
5194
5195 (defun gnus-summary-update-lines (&optional beg end)
5196   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5197   (let ((beg (or beg (point-min)))
5198         (end (or end (point-max))))
5199     (save-excursion
5200       (set-buffer gnus-summary-buffer)
5201       (goto-char beg)
5202       (while (and (not (eobp)) (< (point) end))
5203         (gnus-summary-update-line)
5204         (forward-line 1)))))
5205
5206 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5207   ;; Sum up all elements (and sub-elements) in a list.
5208   (let ((number 
5209          (if (listp thread) 
5210              (apply 
5211               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5212            1)))
5213     (if char 
5214         (if (> number 1) gnus-not-empty-thread-mark
5215           gnus-empty-thread-mark)
5216       number)))
5217
5218 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5219   "Start reading news in newsgroup GROUP.
5220 If SHOW-ALL is non-nil, already read articles are also listed.
5221 If NO-ARTICLE is non-nil, no article is selected initially."
5222   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5223   (let* ((new-group (gnus-summary-setup-buffer group))
5224          (did-select (and new-group (gnus-select-newsgroup group show-all)))
5225          (method (car (gnus-find-method-for-group group))))
5226     (cond 
5227      ((not new-group)
5228       (gnus-set-global-variables)
5229       (gnus-kill-buffer kill-buffer)
5230       (gnus-configure-windows 'summary)
5231       (gnus-set-mode-line 'summary)
5232       (gnus-summary-position-cursor)
5233       (message "")
5234       t)
5235      ((null did-select) 
5236       (and (eq major-mode 'gnus-summary-mode)
5237            (not (equal (current-buffer) kill-buffer))
5238            (progn
5239              (kill-buffer (current-buffer))
5240              (set-buffer gnus-group-buffer)
5241              (gnus-group-next-unread-group 1)))
5242       (message "Can't select group")
5243       nil)
5244      ((eq did-select 'quit)
5245       (and (eq major-mode 'gnus-summary-mode)
5246            (not (equal (current-buffer) kill-buffer))
5247            (kill-buffer (current-buffer)))
5248       (gnus-kill-buffer kill-buffer)
5249       (gnus-configure-windows 'group)
5250       (gnus-group-next-unread-group 1)
5251       (signal 'quit nil))
5252      (t
5253       (gnus-set-global-variables)
5254       ;; Save the active value in effect when the group was entered.
5255       (setq gnus-newsgroup-active 
5256             (gnus-copy-sequence
5257              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5258       ;; You can change the subjects in this hook.
5259       (run-hooks 'gnus-select-group-hook)
5260       ;; Do score processing.
5261       (and gnus-use-scoring (gnus-possibly-score-headers))
5262       ;; Update the format specifiers.
5263       (gnus-update-format-specifications)
5264       ;; Generate the summary buffer.
5265       (gnus-summary-prepare)
5266       (if (zerop (buffer-size))
5267           (cond (gnus-newsgroup-dormant
5268                  (gnus-summary-show-all-dormant))
5269                 ((and gnus-newsgroup-scored show-all)
5270                  (gnus-summary-show-all-expunged))))
5271       ;; Function `gnus-apply-kill-file' must be called in this hook.
5272       (run-hooks 'gnus-apply-kill-hook)
5273       (if (zerop (buffer-size))
5274           (progn
5275             ;; This newsgroup is empty.
5276             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5277             (gnus-message 6 "No unread news")
5278             (gnus-kill-buffer kill-buffer)
5279             nil)
5280         (save-excursion
5281           (if kill-buffer
5282               (let ((gnus-summary-buffer kill-buffer))
5283                 (gnus-configure-windows 'group))))
5284         ;; Hide conversation thread subtrees.  We cannot do this in
5285         ;; gnus-summary-prepare-hook since kill processing may not
5286         ;; work with hidden articles.
5287         (and gnus-show-threads
5288              gnus-thread-hide-subtree
5289              (gnus-summary-hide-all-threads))
5290         ;; Show first unread article if requested.
5291         (goto-char (point-min))
5292         (if (and (not no-article)
5293                  gnus-auto-select-first
5294                  (gnus-summary-first-unread-article))
5295             (gnus-configure-windows 'article)
5296           (gnus-configure-windows 'summary))
5297         (gnus-set-mode-line 'summary)
5298         (gnus-summary-position-cursor)
5299         ;; If in async mode, we send some info to the backend.
5300         (and gnus-newsgroup-async
5301              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5302              (gnus-request-asynchronous 
5303               gnus-newsgroup-name
5304               (if (and gnus-asynchronous-article-function
5305                        (fboundp gnus-asynchronous-article-function))
5306                   (funcall gnus-asynchronous-article-function
5307                            gnus-newsgroup-threads)
5308                 gnus-newsgroup-threads)))
5309         (gnus-kill-buffer kill-buffer))
5310       t))))
5311
5312 (defun gnus-summary-prepare ()
5313   ;; Generate the summary buffer.
5314   (let ((buffer-read-only nil))
5315     (erase-buffer)
5316     (gnus-summary-prepare-threads 
5317      (if gnus-show-threads
5318          (gnus-gather-threads 
5319           (gnus-sort-threads 
5320            (if (and gnus-summary-expunge-below
5321                     (not gnus-fetch-old-headers))
5322                (gnus-make-threads-and-expunge)
5323              (gnus-make-threads))))
5324        gnus-newsgroup-headers)
5325      0 nil nil t)
5326     ;; Erase header retrieval message.
5327     (gnus-summary-update-lines)
5328     (message "")
5329     ;; Remove the final newline.
5330     ;;(goto-char (point-max))
5331     ;;(delete-char -1)
5332     ;; Call hooks for modifying summary buffer.
5333     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5334     (goto-char (point-min))
5335     (run-hooks 'gnus-summary-prepare-hook)))
5336
5337 (defun gnus-gather-threads (threads)
5338   "Gather threads that have lost their roots."
5339   (if (not gnus-summary-make-false-root)
5340       threads 
5341     (let ((hashtb (gnus-make-hashtable 1023))
5342           (prev threads)
5343           (result threads)
5344           thread subject hthread whole-subject)
5345       (while threads
5346         (setq whole-subject 
5347               (setq subject (header-subject (car (car threads)))))
5348         (if gnus-summary-gather-subject-limit
5349             (or (and (numberp gnus-summary-gather-subject-limit)
5350                      (> (length subject) gnus-summary-gather-subject-limit)
5351                      (setq subject
5352                            (substring subject 0 
5353                                       gnus-summary-gather-subject-limit)))
5354                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5355                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5356           (setq subject (gnus-simplify-subject-re subject)))
5357         (if (setq hthread 
5358                   (gnus-gethash subject hashtb))
5359             (progn
5360               (or (stringp (car (car hthread)))
5361                   (setcar hthread (list whole-subject (car hthread))))
5362               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5363                                            (list (car threads))))
5364               (setcdr prev (cdr threads))
5365               (setq threads prev))
5366           (gnus-sethash subject threads hashtb))
5367         (setq prev threads)
5368         (setq threads (cdr threads)))
5369       result)))
5370
5371 (defun gnus-make-threads ()
5372   ;; This function takes the dependencies already made by 
5373   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5374   ;; through the dependecies in the hash table and finds all the
5375   ;; roots. Roots do not refer back to any valid articles.
5376   (let (roots)
5377     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5378          (gnus-build-old-threads))
5379     (mapatoms
5380      (lambda (refs)
5381        (if (not (car (symbol-value refs)))
5382            (setq roots (append (cdr (symbol-value refs)) roots))
5383          ;; Ok, these refer back to valid articles, but if
5384          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5385          ;; the root has the same subject as its children. The children
5386          ;; that do not are made into roots and removed from the list
5387          ;; of children. 
5388          (or gnus-thread-ignore-subject
5389              (let* ((prev (symbol-value refs))
5390                     (subject (gnus-simplify-subject-re 
5391                               (header-subject (car prev))))
5392                     (headers (cdr prev)))
5393                (while headers
5394                  (if (not (string= subject
5395                                    (gnus-simplify-subject-re 
5396                                     (header-subject (car headers)))))
5397                      (progn
5398                        (setq roots (cons (car headers) roots))
5399                        (setcdr prev (cdr headers)))
5400                    (setq prev headers))
5401                  (setq headers (cdr headers)))))))
5402      gnus-newsgroup-dependencies)
5403     
5404     (mapcar 'gnus-trim-thread
5405             (apply 'append
5406                    (mapcar 'gnus-cut-thread
5407                            (mapcar 'gnus-make-sub-thread roots))))))
5408   
5409 (defun gnus-make-threads-and-expunge ()
5410   ;; This function takes the dependencies already made by 
5411   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5412   ;; through the dependecies in the hash table and finds all the
5413   ;; roots. Roots do not refer back to any valid articles.
5414   (let ((default (or gnus-summary-default-score 0))
5415         (below gnus-summary-expunge-below)
5416         roots article)
5417     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5418          (gnus-build-old-threads))
5419     (mapatoms
5420      (lambda (refs)
5421        (if (not (car (symbol-value refs)))
5422            ;; These articles do not refer back to any other articles -
5423            ;; they are roots.
5424            (let ((headers (cdr (symbol-value refs))))
5425              ;; We weed out the low-scored articles.
5426              (while headers
5427                (if (not (< (or (cdr (assq (header-number (car headers))
5428                                           gnus-newsgroup-scored)) default)
5429                            below))
5430                    ;; It is over.
5431                    (setq roots (cons (car headers) roots))
5432                  ;; It is below, so we mark it as read.
5433                  (setq gnus-newsgroup-unreads
5434                        (delq (header-number (car headers))
5435                              gnus-newsgroup-unreads)))
5436                (setq headers (cdr headers))))
5437          ;; Ok, these refer back to valid articles, but if
5438          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5439          ;; the root has the same subject as its children. The children
5440          ;; that do not are made into roots and removed from the list
5441          ;; of children. 
5442          (or gnus-thread-ignore-subject
5443              (let* ((prev (symbol-value refs))
5444                     (subject (gnus-simplify-subject-re 
5445                               (header-subject (car prev))))
5446                     (headers (cdr prev)))
5447                (while headers
5448                  (if (not (string= subject
5449                                    (gnus-simplify-subject-re 
5450                                     (header-subject (car headers)))))
5451                      (progn
5452                        (if (not (< (or (cdr (assq (header-number (car headers))
5453                                                   gnus-newsgroup-scored))
5454                                        default) below))
5455                            (setq roots (cons (car headers) roots))
5456                          (setq gnus-newsgroup-unreads
5457                                (delq (header-number (car headers))
5458                                      gnus-newsgroup-unreads)))
5459                        (setcdr prev (cdr headers)))
5460                    (setq prev headers))
5461                  (setq headers (cdr headers)))))
5462          ;; If this article is expunged, some of the children might be
5463          ;; roots.  
5464          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5465                                gnus-newsgroup-scored)) default)
5466                 below)
5467              (let* ((prev (symbol-value refs))
5468                     (headers (cdr prev)))
5469                (while headers
5470                  (setq article (header-number (car headers)))
5471                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5472                                  default) below))
5473                      (progn (setq roots (cons (car headers) roots))
5474                             (setq prev headers))
5475                    (setq gnus-newsgroup-unreads 
5476                          (delq article gnus-newsgroup-unreads))
5477                    (setcdr prev (cdr headers)))
5478                  (setq headers (cdr headers))))
5479            ;; It was not expunged, but we look at expunged children.
5480            (let* ((prev (symbol-value refs))
5481                   (headers (cdr prev))
5482                   article id)
5483              (while headers
5484                (setq article (header-number (car headers)))
5485                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5486                                default) below))
5487                    (setq prev headers)
5488                  (setq gnus-newsgroup-unreads 
5489                        (delq article gnus-newsgroup-unreads))
5490                  (setcdr prev (cdr headers)))
5491                (setq headers (cdr headers)))))))
5492      gnus-newsgroup-dependencies)
5493
5494     (mapcar 'gnus-trim-thread
5495             (apply 'append
5496                    (mapcar 'gnus-cut-thread
5497                            (mapcar 'gnus-make-sub-thread roots))))))
5498   
5499 (defun gnus-cut-thread (thread)
5500   ;; Remove leaf dormant or ancient articles from THREAD.
5501   (let ((head (car thread))
5502         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5503     (if (and (null tail)
5504              (let ((number (header-number head)))
5505                (or (memq number gnus-newsgroup-ancient)
5506                    (memq number gnus-newsgroup-dormant)
5507                    (and gnus-summary-expunge-below
5508                         (eq gnus-fetch-old-headers 'some)
5509                         (< (or (cdr (assq number gnus-newsgroup-scored))
5510                                gnus-summary-default-score 0)
5511                            gnus-summary-expunge-below)
5512                         (progn
5513                           (setq gnus-newsgroup-unreads
5514                                 (delq number gnus-newsgroup-unreads))
5515                           t)))))
5516         nil
5517       (list (cons head tail)))))
5518
5519 (defun gnus-trim-thread (thread)
5520   ;; Remove root ancient articles with only one child from THREAD.
5521   (if (and (eq gnus-fetch-old-headers 'some)
5522            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5523            (= (length thread) 2))
5524       (gnus-trim-thread (nth 1 thread))
5525     thread))
5526
5527 (defun gnus-make-sub-thread (root)
5528   ;; This function makes a sub-tree for a node in the tree.
5529   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5530                                               gnus-newsgroup-dependencies)))))
5531     (cons root (mapcar 'gnus-make-sub-thread children))))
5532
5533 (defun gnus-build-old-threads ()
5534   ;; Look at all the articles that refer back to old articles, and
5535   ;; fetch the headers for the articles that aren't there. This will
5536   ;; build complete threads - if the roots haven't been expired by the
5537   ;; server, that is.
5538   (let (id heads)
5539     (mapatoms
5540      (lambda (refs)
5541        (if (not (car (symbol-value refs)))
5542            (progn
5543              (setq heads (cdr (symbol-value refs)))
5544              (while heads
5545                (if (not (memq (header-number (car heads))
5546                               gnus-newsgroup-dormant))
5547                    (progn
5548                      (setq id (symbol-name refs))
5549                      (while (and (setq id (gnus-build-get-header id))
5550                                  (not (car (gnus-gethash 
5551                                             id gnus-newsgroup-dependencies)))))
5552                      (setq heads nil))
5553                  (setq heads (cdr heads)))))))
5554      gnus-newsgroup-dependencies)))
5555
5556 (defun gnus-build-get-header (id)
5557   ;; Look through the buffer of NOV lines and find the header to
5558   ;; ID. Enter this line into the dependencies hash table, and return
5559   ;; the id of the parent article (if any).
5560   (let ((deps gnus-newsgroup-dependencies)
5561         found header)
5562     (prog1
5563         (save-excursion
5564           (set-buffer nntp-server-buffer)
5565           (goto-char (point-min))
5566           (while (and (not found) (search-forward id nil t))
5567             (beginning-of-line)
5568             (setq found (looking-at 
5569                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5570                                  (regexp-quote id))))
5571             (or found (beginning-of-line 2)))
5572           (if found
5573               (let (ref)
5574                 (beginning-of-line)
5575                 (and
5576                  (setq header (gnus-nov-parse-line 
5577                                (read (current-buffer)) deps))
5578                  (setq ref (header-references header))
5579                  (string-match "\\(<[^>]+>\\) *$" ref)
5580                  (substring ref (match-beginning 1) (match-end 1))))))
5581       (and header
5582            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5583                  gnus-newsgroup-ancient (cons (header-number header)
5584                                               gnus-newsgroup-ancient))))))
5585
5586 ;; Re-build the thread containing ID.
5587 (defun gnus-rebuild-thread (id)
5588   (let ((dep gnus-newsgroup-dependencies)
5589         (buffer-read-only nil)
5590         parent headers refs thread art)
5591     (while (and id (setq headers
5592                          (car (setq art (gnus-gethash (downcase id) dep)))))
5593       (setq parent art)
5594       (setq id (and (setq refs (header-references headers))
5595                     (string-match "\\(<[^>]+>\\) *$" refs)
5596                     (substring refs (match-beginning 1) (match-end 1)))))
5597     (setq thread (gnus-make-sub-thread (car parent)))
5598     (gnus-rebuild-remove-articles thread)
5599     (let ((beg (point)))
5600       (gnus-summary-prepare-threads (list thread) 0)
5601       (gnus-summary-update-lines beg (point)))))
5602
5603 ;; Delete all lines in the summary buffer that correspond to articles
5604 ;; in this thread.
5605 (defun gnus-rebuild-remove-articles (thread)
5606   (and (gnus-summary-goto-subject (header-number (car thread)))
5607        (gnus-delete-line))
5608   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5609
5610 (defun gnus-sort-threads (threads)
5611   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5612   (let ((fun gnus-thread-sort-functions))
5613     (while fun
5614       (setq threads (sort threads (car fun))
5615             fun (cdr fun))))
5616   threads)
5617
5618 (defun gnus-thread-header (thread)
5619   ;; Return header of first article in THREAD.
5620   (if (consp thread)
5621       (if (stringp (car thread))
5622           (car (car (cdr thread)))
5623         (car thread))
5624     thread))
5625
5626 (defun gnus-thread-sort-by-number (h1 h2)
5627   "Sort threads by root article number."
5628   (let ((h1 (gnus-thread-header h1))
5629         (h2 (gnus-thread-header h2)))
5630     (< (header-number h1) (header-number h2))))
5631
5632 (defun gnus-thread-sort-by-author (h1 h2)
5633   "Sort threads by root author."
5634   (let ((h1 (gnus-thread-header h1))
5635         (h2 (gnus-thread-header h2)))
5636     (string-lessp
5637      (let ((extract (funcall 
5638                      gnus-extract-address-components (header-from h1))))
5639        (or (car extract) (cdr extract)))
5640      (let ((extract (funcall
5641                      gnus-extract-address-components (header-from h2))))
5642        (or (car extract) (cdr extract))))))
5643
5644 (defun gnus-thread-sort-by-subject (h1 h2)
5645   "Sort threads by root subject."
5646   (let ((h1 (gnus-thread-header h1))
5647         (h2 (gnus-thread-header h2)))
5648     (string-lessp
5649      (downcase (gnus-simplify-subject (header-subject h1)))
5650      (downcase (gnus-simplify-subject (header-subject h2))))))
5651
5652 (defun gnus-thread-sort-by-date (h1 h2)
5653   "Sort threads by root article date."
5654   (let ((h1 (gnus-thread-header h1))
5655         (h2 (gnus-thread-header h2)))
5656     (string-lessp
5657      (gnus-sortable-date (header-date h1))
5658      (gnus-sortable-date (header-date h2)))))
5659
5660 (defun gnus-thread-sort-by-score (h1 h2)
5661   "Sort threads by root article score.
5662 Unscored articles will be counted as havin a score of zero."
5663   (let ((h1 (gnus-thread-header h1))
5664         (h2 (gnus-thread-header h2)))
5665     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5666           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5667       (> (or (cdr s1) gnus-summary-default-score 0)
5668          (or (cdr s2) gnus-summary-default-score 0)))))
5669
5670 (defun gnus-thread-sort-by-total-score (h1 h2)
5671   "Sort threads by the sum of all scores in the thread.
5672 Unscored articles will be counted as havin a score of zero."
5673   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5674
5675 (defun gnus-thread-total-score (thread)
5676   ;;  This function find the total score of  THREAD.
5677   (if (consp thread)
5678       (if (stringp (car thread))
5679           (apply gnus-thread-score-function 0
5680                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5681         (gnus-thread-total-score-1 thread))
5682     (gnus-thread-total-score-1 (list thread))))
5683
5684 (defun gnus-thread-total-score-1 (root)
5685   ;; This function find the total score of the thread below ROOT.
5686   (setq root (car root))
5687   (apply gnus-thread-score-function
5688          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5689              gnus-summary-default-score 0)
5690          (mapcar 'gnus-thread-total-score
5691                  (cdr (gnus-gethash (downcase (header-id root))
5692                                     gnus-newsgroup-dependencies)))))
5693
5694 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5695 (defvar gnus-tmp-prev-subject "")
5696 (defvar gnus-tmp-adopt-thread nil)
5697
5698 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
5699 (defun gnus-summary-prepare-threads 
5700   (threads level &optional not-child no-subject cull)
5701   "Prepare summary buffer from THREADS and indentation LEVEL.  
5702 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
5703 or a straight list of headers."
5704   (let (thread header number subject clevel)
5705     (while threads
5706       (setq thread (car threads)
5707             threads (cdr threads))
5708       ;; If `thread' is a cons, hierarchical threads are used.  If not,
5709       ;; `thread' is the header.
5710       (if (consp thread)
5711           (setq header (car thread))
5712         (setq header thread)
5713         (and cull
5714              (or (memq (setq number (header-number header))
5715                        gnus-newsgroup-dormant)
5716                  (and gnus-summary-expunge-below
5717                       (< (or (cdr (assq number gnus-newsgroup-scored))
5718                              gnus-summary-default-score 0)
5719                          gnus-summary-expunge-below)))
5720              (progn
5721                (setq header nil)
5722                (setq gnus-newsgroup-unreads 
5723                      (delq number gnus-newsgroup-unreads)))))
5724       (cond 
5725        ((stringp header)
5726         ;; The header is a dummy root.
5727         (cond ((eq gnus-summary-make-false-root 'adopt)
5728                ;; We let the first article adopt the rest.
5729                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
5730                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
5731                (setq thread (cdr (cdr thread)))
5732                (while thread
5733                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
5734                  (setq thread (cdr thread))))
5735               ((eq gnus-summary-make-false-root 'dummy)
5736                ;; We output a dummy root.
5737                (gnus-summary-insert-dummy-line 
5738                 nil header (header-number (car (car (cdr thread)))))
5739                (setq clevel 1))
5740               ((eq gnus-summary-make-false-root 'empty)
5741                ;; We print the articles with empty subject fields. 
5742                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
5743                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
5744                (setq thread (cdr (cdr thread)))
5745                (while thread
5746                  (gnus-summary-prepare-threads 
5747                   (list (car thread)) 0 nil
5748                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
5749                             (not (string=  
5750                                   (gnus-simplify-subject-re 
5751                                    (header-subject (car (car thread))))
5752                                   (gnus-simplify-subject-re header))))))
5753                  (setq thread (cdr thread))))
5754               (t
5755                ;; We do not make a root for the gathered
5756                ;; sub-threads at all.  
5757                (setq clevel 0)))
5758         ;; Print the sub-threads.
5759         (and (consp thread) (cdr thread)
5760              (gnus-summary-prepare-threads (cdr thread) clevel)))
5761        ;; The header is a real article.
5762        (header
5763         (setq number (header-number header)
5764               subject (header-subject header))
5765         (and gnus-newsgroup-async
5766              (setq gnus-newsgroup-threads
5767                    (cons (cons (header-number header)
5768                                (header-lines header)) gnus-newsgroup-threads)))
5769         (gnus-summary-insert-line
5770          nil header level nil 
5771          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
5772                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
5773                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
5774                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
5775                (t gnus-ancient-mark))
5776          (memq number gnus-newsgroup-replied)
5777          (memq number gnus-newsgroup-expirable)
5778          (if no-subject 
5779              gnus-summary-same-subject
5780            (if (or (zerop level)
5781                    (and gnus-thread-ignore-subject
5782                         (not (string= 
5783                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
5784                               (gnus-simplify-subject-re subject)))))
5785                subject
5786              gnus-summary-same-subject))
5787          not-child
5788          (cdr (assq number gnus-newsgroup-scored)))
5789         (setq gnus-tmp-prev-subject subject)
5790         ;; Recursively print subthreads.
5791         (and (consp thread) (cdr thread)
5792              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
5793
5794 (defun gnus-select-newsgroup (group &optional read-all)
5795   "Select newsgroup GROUP.
5796 If READ-ALL is non-nil, all articles in the group are selected."
5797   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5798          (info (nth 2 entry))
5799          articles header-marks)
5800     (gnus-check-news-server
5801      (setq gnus-current-select-method (gnus-find-method-for-group group)))
5802
5803     (or (gnus-server-opened gnus-current-select-method)
5804         (gnus-open-server gnus-current-select-method)
5805         (error "Couldn't open server"))
5806     
5807     (or (and (eq (car entry) t)
5808              (gnus-activate-newsgroup (car info)))
5809         (gnus-request-group group t)
5810         (progn
5811           (kill-buffer (current-buffer))
5812           (error "Couldn't request group %s: %s" 
5813                  group (gnus-status-message group))))
5814
5815     (setq gnus-newsgroup-name group)
5816     (setq gnus-newsgroup-unselected nil)
5817     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5818
5819     (and gnus-asynchronous
5820          (gnus-check-backend-function 
5821           'request-asynchronous gnus-newsgroup-name)
5822          (setq gnus-newsgroup-async
5823                (gnus-request-asynchronous gnus-newsgroup-name)))
5824
5825     (setq articles (gnus-articles-to-read group read-all))
5826
5827     (cond 
5828      ((null articles) 
5829       (gnus-message 3 "Couldn't select newsgroup")
5830       'quit)
5831      ((eq articles 0) nil)
5832      (t
5833       ;; Init the dependencies hash table.
5834       (setq gnus-newsgroup-dependencies 
5835             (gnus-make-hashtable (length articles)))
5836       ;; Retrieve the headers and read them in.
5837       (setq gnus-newsgroup-headers 
5838             (if (eq 'nov (setq gnus-headers-retrieved-by
5839                                (gnus-retrieve-headers 
5840                                 (if gnus-fetch-old-headers 
5841                                     (cons 1 articles) articles) 
5842                                 gnus-newsgroup-name)))
5843                 (progn
5844                   (gnus-get-newsgroup-headers-xover articles))
5845               (gnus-get-newsgroup-headers)))
5846       ;; If we were to fetch old headers, but the backend didn't
5847       ;; support XOVER, then it is possible we fetched one article
5848       ;; that we shouldn't have. If that's the case, we pop it off the
5849       ;; list of headers.
5850       (and (not (eq gnus-headers-retrieved-by 'nov))
5851            gnus-fetch-old-headers
5852            gnus-newsgroup-headers
5853            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
5854            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
5855       ;; Remove cancelled articles from the list of unread articles.
5856       (setq gnus-newsgroup-unreads
5857             (gnus-set-sorted-intersection 
5858              gnus-newsgroup-unreads
5859              (mapcar (lambda (headers) (header-number headers))
5860                      gnus-newsgroup-headers)))
5861       ;; Adjust and set lists of article marks.
5862       (and info
5863            (let (marked)
5864              (gnus-adjust-marked-articles info)
5865              (setq gnus-newsgroup-marked 
5866                    (cdr (assq 'tick (setq marked (nth 3 info)))))
5867              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
5868              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
5869              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
5870              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
5871              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
5872              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
5873              (setq gnus-newsgroup-processable nil)))
5874       ;; Check whether auto-expire is to be done in this group.
5875       (setq gnus-newsgroup-auto-expire
5876             (or (and (stringp gnus-auto-expirable-newsgroups)
5877                      (string-match gnus-auto-expirable-newsgroups group))
5878                 (memq 'auto-expire (nth 5 info))))
5879       ;; First and last article in this newsgroup.
5880       (and gnus-newsgroup-headers
5881            (setq gnus-newsgroup-begin 
5882                  (header-number (car gnus-newsgroup-headers)))
5883            (setq gnus-newsgroup-end
5884                  (header-number (gnus-last-element gnus-newsgroup-headers))))
5885       (setq gnus-reffed-article-number -1)
5886       ;; GROUP is successfully selected.
5887       (or gnus-newsgroup-headers t)))))
5888
5889 (defun gnus-articles-to-read (group read-all)
5890   ;; Find out what articles the user wants to read.
5891   (let* ((articles
5892           ;; Select all articles if `read-all' is non-nil, or if all the
5893           ;; unread articles are dormant articles.
5894           (if (or read-all
5895                   (= (length gnus-newsgroup-unreads) 
5896                      (length gnus-newsgroup-dormant)))
5897               (gnus-uncompress-range 
5898                (gnus-gethash group gnus-active-hashtb))
5899             gnus-newsgroup-unreads))
5900          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5901          (scored (length scored-list))
5902          (number (length articles))
5903          (marked (+ (length gnus-newsgroup-marked)
5904                     (length gnus-newsgroup-dormant)))
5905          (select
5906           (condition-case ()
5907               (cond ((and (or (<= scored marked)
5908                               (= scored number))
5909                           (numberp gnus-large-newsgroup)
5910                           (> number gnus-large-newsgroup))
5911                      (let ((input
5912                             (read-string
5913                              (format
5914                               "How many articles from %s (default %d): "
5915                               gnus-newsgroup-name number))))
5916                        (if (string-equal input "")
5917                            number input)))
5918                     ((and (> scored marked) (< scored number))
5919                      (let ((input
5920                             (read-string
5921                              (format 
5922                               "%s %s (%d scored, %d total): "
5923                               "How many articles from"
5924                               group scored number))))
5925                        (if (string-equal input "")
5926                            number input)))
5927                     (t number))
5928             (quit nil)))
5929          total-articles)
5930     (setq select (if (stringp select) (string-to-number select) select))
5931     (if (or (null select) (zerop select))
5932         select
5933       (if (and (not (zerop scored)) (<= (abs select) scored))
5934           (progn
5935             (setq articles (sort scored-list '<))
5936             (setq number (length articles)))
5937         (setq articles (copy-sequence articles)))
5938
5939       (setq total-articles articles)
5940       
5941       (if (< (abs select) number)
5942           (if (< select 0) 
5943               ;; Select the N oldest articles.
5944               (setcdr (nthcdr (1- (abs select)) articles) nil)
5945             ;; Select the N most recent articles.
5946             (setq articles (nthcdr (- number select) articles))))
5947       (setq gnus-newsgroup-unselected
5948             (gnus-sorted-intersection
5949              gnus-newsgroup-unreads
5950              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
5951       articles)))
5952
5953 (defun gnus-killed-articles (killed articles)
5954   (let (out)
5955     (while articles
5956       (if (inline (gnus-member-of-range (car articles) killed))
5957           (setq out (cons (car articles) out)))
5958       (setq articles (cdr articles)))
5959     out))
5960
5961 (defun gnus-adjust-marked-articles (info &optional active)
5962   "Remove all marked articles that are no longer legal."
5963   (let ((marked-lists (nth 3 info))
5964         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
5965         marked m prev)
5966     ;; There are many types of marked articles.
5967     (while marked-lists
5968       (setq m (cdr (setq prev (car marked-lists))))
5969       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
5970              ;; Make sure that all ticked articles are a subset of the
5971              ;; unread/unselected articles.
5972              (while m
5973                (if (or (memq (car m) gnus-newsgroup-unreads)
5974                        (memq (car m) gnus-newsgroup-unselected))
5975                    (setq prev m)
5976                  (setcdr prev (cdr m)))
5977                (setq m (cdr m))))
5978             ((eq 'score (car prev))
5979              ;; Scored articles should be a subset of
5980              ;; unread/unselected articles. 
5981              (while m
5982                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
5983                        (memq (car (car m)) gnus-newsgroup-unreads))
5984                    (setq prev m)
5985                  (setcdr prev (cdr m)))
5986                (setq m (cdr m))))
5987             ((eq 'bookmark (car prev))
5988              ;; Bookmarks should be a subset of active articles.
5989              (while m
5990                (if (< (car (car m)) (car active))
5991                    (setcdr prev (cdr m))
5992                  (setq prev m))
5993                (setq m (cdr m))))
5994             ((eq 'killed (car prev))
5995              ;; Articles that have been through the kill process are
5996              ;; to be a subset of active articles.
5997              (while (and m (< (or (and (numberp (car m)) (car m))
5998                                   (cdr (car m)))
5999                               (car active)))
6000                (setcdr prev (cdr m))
6001                (setq m (cdr m)))
6002              (if (and m (< (or (and (numberp (car m)) (car m))
6003                                (car (car m)))
6004                            (car active))) 
6005                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6006             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6007              ;; The replied and expirable articles have to be articles
6008              ;; that are active. 
6009              (while m
6010                (if (< (car m) (car active))
6011                    (setcdr prev (cdr m))
6012                  (setq prev m))
6013                (setq m (cdr m)))))
6014       (setq marked-lists (cdr marked-lists)))
6015     ;; Remove all lists that are empty.
6016     (setq marked-lists (nth 3 info))
6017     (if marked-lists
6018         (progn
6019           (while (= 1 (length (car marked-lists)))
6020             (setq marked-lists (cdr marked-lists)))
6021           (setq m (cdr (setq prev marked-lists)))
6022           (while m
6023             (if (= 1 (length (car m)))
6024                 (setcdr prev (cdr m))
6025               (setq prev m))
6026             (setq m (cdr m)))
6027           (setcar (nthcdr 3 info) marked-lists)))
6028     ;; Finally, if there are no marked lists at all left, and if there
6029     ;; are no elements after the lists in the info list, we just chop
6030     ;; the info list off before the marked lists.
6031     (and (null marked-lists) 
6032          (not (nthcdr 4 info))
6033          (setcdr (nthcdr 2 info) nil)))
6034   info)
6035
6036 (defun gnus-set-marked-articles 
6037   (info ticked replied expirable killed dormant bookmark score) 
6038   "Enter the various lists of marked articles into the newsgroup info list."
6039   (let (newmarked)
6040     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6041     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6042     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6043                                          newmarked)))
6044     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6045     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6046     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6047                                         newmarked)))
6048     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6049     (if (nthcdr 3 info)
6050         (if newmarked
6051             (setcar (nthcdr 3 info) newmarked)
6052           (if (not (nthcdr 4 info))
6053               (setcdr (nthcdr 2 info) nil)
6054             (setcar (nthcdr 3 info) nil)))
6055       (if newmarked
6056           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
6057
6058 (defun gnus-add-marked-articles (group type articles &optional info force)
6059   ;; Add ARTICLES of TYPE to the info of GROUP.
6060   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6061   ;; add, but replace marked articles of TYPE with ARTICLES.
6062   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6063         marked m)
6064     (or (not info)
6065         (and (not (setq marked (nthcdr 3 info)))
6066              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6067         (and (not (setq m (assq type (car marked))))
6068              (setcar marked (cons (cons type articles) (car marked))))
6069         (if force
6070             (setcdr m articles)
6071           (nconc m articles)))))
6072          
6073 (defun gnus-set-mode-line (where)
6074   "This function sets the mode line of the article or summary buffers.
6075 If WHERE is `summary', the summary mode line format will be used."
6076   (if (memq where gnus-updated-mode-lines)
6077       (let (mode-string)
6078         (save-excursion
6079           (set-buffer gnus-summary-buffer)
6080           (let* ((mformat (if (eq where 'article) 
6081                               gnus-article-mode-line-format-spec
6082                             gnus-summary-mode-line-format-spec))
6083                  (group-name gnus-newsgroup-name)
6084                  (article-number (or gnus-current-article 0))
6085                  (unread (- (length gnus-newsgroup-unreads)
6086                             (length gnus-newsgroup-dormant)))
6087                  (unread-and-unticked 
6088                   (- unread (length gnus-newsgroup-marked)))
6089                  (unselected (length gnus-newsgroup-unselected))
6090                  (unread-and-unselected
6091                   (cond ((and (zerop unread-and-unticked)
6092                               (zerop unselected)) "")
6093                         ((zerop unselected) 
6094                          (format "{%d more}" unread-and-unticked))
6095                         (t (format "{%d(+%d) more}"
6096                                    unread-and-unticked unselected))))
6097                  (subject
6098                   (if gnus-current-headers
6099                       (header-subject gnus-current-headers) ""))
6100                  (max-len (and gnus-mode-non-string-length
6101                                (- (frame-width) gnus-mode-non-string-length)))
6102                  header) ;; passed as argument to any user-format-funcs
6103             (setq mode-string (eval mformat))
6104             (or (numberp max-len)
6105                 (setq max-len (length mode-string)))
6106             (if (< max-len 4) (setq max-len 4))
6107             (if (> (length mode-string) max-len)
6108                 (setq mode-string 
6109                       (concat (substring mode-string 0 (- max-len 3))
6110                               "...")))
6111             (setq mode-string (format (format "%%-%ds" max-len)
6112                                       mode-string))))
6113         (setq mode-line-buffer-identification mode-string)
6114         (set-buffer-modified-p t))))
6115
6116 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6117   "Go through the HEADERS list and add all Xrefs to a hash table.
6118 The resulting hash table is returned, or nil if no Xrefs were found."
6119   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6120          (prefix (if (and 
6121                       (gnus-group-foreign-p from-newsgroup)
6122                       (not (memq 'virtual 
6123                                  (assoc (symbol-name (car from-method))
6124                                         gnus-valid-select-methods))))
6125                      (gnus-group-real-prefix from-newsgroup)))
6126          (xref-hashtb (make-vector 63 0))
6127          start group entry number xrefs header)
6128     (while headers
6129       (setq header (car headers))
6130       (if (and (setq xrefs (header-xref header))
6131                (not (memq (header-number header) unreads)))
6132           (progn
6133             (setq start 0)
6134             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6135               (setq start (match-end 0))
6136               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6137                                                     (match-end 1))))
6138               (setq number 
6139                     (string-to-int (substring xrefs (match-beginning 2) 
6140                                               (match-end 2))))
6141               (if (setq entry (gnus-gethash group xref-hashtb))
6142                   (setcdr entry (cons number (cdr entry)))
6143                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6144       (setq headers (cdr headers)))
6145     (if start xref-hashtb nil)))
6146
6147 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6148   "Look through all the headers and mark the Xrefs as read."
6149   (let ((virtual (memq 'virtual 
6150                        (assoc (symbol-name (car (gnus-find-method-for-group 
6151                                                  from-newsgroup)))
6152                               gnus-valid-select-methods)))
6153         name entry read info xref-hashtb idlist active num range exps method
6154         nth4)
6155     (save-excursion
6156       (set-buffer gnus-group-buffer)
6157       (if (setq xref-hashtb 
6158                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6159           (mapatoms 
6160            (lambda (group)
6161              (if (string= from-newsgroup (setq name (symbol-name group)))
6162                  ()
6163                (setq idlist (symbol-value group))
6164                ;; Dead groups are not updated.
6165                (if (and (prog1 
6166                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6167                                   info (nth 2 entry))
6168                           (if (stringp (setq nth4 (nth 4 info)))
6169                               (setq nth4 (gnus-server-to-method nth4))))
6170                         ;; Only do the xrefs if the group has the same
6171                         ;; select method as the group we have just read.
6172                         (or (gnus-methods-equal-p 
6173                              nth4 (gnus-find-method-for-group from-newsgroup))
6174                             virtual
6175                             (equal nth4 
6176                                    (setq method (gnus-find-method-for-group 
6177                                                  from-newsgroup)))
6178                             (and (equal (car nth4) (car method))
6179                                  (equal (nth 1 nth4) (nth 1 method))))
6180                         gnus-use-cross-reference
6181                         (or (not (eq gnus-use-cross-reference t))
6182                             virtual
6183                             ;; Only do cross-references on subscribed
6184                             ;; groups, if that is what is wanted.  
6185                             (<= (nth 1 info) gnus-level-subscribed)))
6186                    (progn
6187                      (setq num 0)
6188                      ;; Set the new list of read articles in this group.
6189                      (setq active (gnus-gethash name gnus-active-hashtb))
6190                      ;; First peel off all illegal article numbers.
6191                      (if active
6192                          (let ((ids idlist)
6193                                (ticked (cdr (assq 'tick (nth 3 info))))
6194                                (dormant (cdr (assq 'dormant (nth 3 info))))
6195                                id)
6196                            (setq exps nil)
6197                            (while ids
6198                              (setq id (car ids))
6199                              (if (or (> id (cdr active))
6200                                      (< id (car active))
6201                                      (memq id ticked)
6202                                      (memq id dormant))
6203                                  (setq idlist (delq id idlist)))
6204                              (and (memq id expirable)
6205                                   (setq exps (cons id exps)))
6206                              (setq ids (cdr ids)))))
6207                      ;; Update expirable articles.
6208                      (gnus-add-marked-articles nil 'expirable exps info)
6209                      (and active
6210                           (null (nth 2 info))
6211                           (> (car active) 1)
6212                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6213                      (setcar (nthcdr 2 info)
6214                              (setq range
6215                                    (gnus-add-to-range 
6216                                     (nth 2 info) 
6217                                     (setq idlist (sort idlist '<)))))
6218                      ;; Then we have to re-compute how many unread
6219                      ;; articles there are in this group.
6220                      (if active
6221                          (progn
6222                            (cond 
6223                             ((not range)
6224                              (setq num (- (1+ (cdr active)) (car active))))
6225                             ((not (listp (cdr range)))
6226                              (setq num (- (cdr active) (- (1+ (cdr range)) 
6227                                                           (car range)))))
6228                             (t
6229                              (while range
6230                                (if (numberp (car range))
6231                                    (setq num (1+ num))
6232                                  (setq num (+ num (- (1+ (cdr (car range)))
6233                                                      (car (car range))))))
6234                                (setq range (cdr range)))
6235                              (setq num (- (cdr active) num))))
6236                            ;; Update the number of unread articles.
6237                            (setcar 
6238                             entry 
6239                             (max 0 (- num 
6240                                       (length (cdr (assq 'tick (nth 3 info))))
6241                                       (length 
6242                                        (cdr (assq 'dormant (nth 3 info)))))))
6243                            ;; Update the group buffer.
6244                            (gnus-group-update-group name t)))))))
6245            xref-hashtb)))))
6246
6247 (defun gnus-methods-equal-p (m1 m2)
6248   (let ((m1 (or m1 gnus-select-method))
6249         (m2 (or m2 gnus-select-method)))
6250     (or (equal m1 m2)
6251         (and (eq (car m1) (car m2))
6252              (or (not (memq 'address (assoc (symbol-name (car m1))
6253                                             gnus-valid-select-methods)))
6254                  (equal (nth 1 m1) (nth 1 m2)))))))
6255
6256 (defsubst gnus-header-value ()
6257   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6258
6259 (defvar gnus-newsgroup-none-id 0)
6260
6261 (defun gnus-get-newsgroup-headers ()
6262   (setq gnus-article-internal-prepare-hook nil)
6263   (let ((cur nntp-server-buffer)
6264         (dependencies gnus-newsgroup-dependencies)
6265         headers char article id dep end)
6266     (save-excursion
6267       (set-buffer nntp-server-buffer)
6268       (goto-char (point-min))
6269       ;; Search to the beginning of the next header. Error messages
6270       ;; do not begin with 2 or 3.
6271       (while (re-search-forward "^[23][0-9]+ " nil t)
6272         (let ((header (make-vector 9 nil))
6273               (c (following-char))
6274               (case-fold-search t)
6275               (p (point))
6276               from subject in-reply-to references ref)
6277           (setq id nil
6278                 ref nil
6279                 references nil
6280                 subject nil
6281                 from nil)
6282           (header-set-number header (setq article (read cur)))
6283           ;; This implementation of this function, with nine
6284           ;; search-forwards instead of the one re-search-forward and
6285           ;; a case (which basically was the old function) is actually
6286           ;; about twice as fast, even though it looks messier. You
6287           ;; can't have everything, I guess. Speed and elegance
6288           ;; doesn't always come hand in hand.
6289           (save-restriction
6290             (narrow-to-region (point) (save-excursion 
6291                                         (search-forward "\n.\n" nil t)))
6292             (if (search-forward "\nfrom: " nil t)
6293                 (header-set-from header (gnus-header-value))
6294               (header-set-from header "(nobody)"))
6295             (goto-char p)
6296             (if (search-forward "\nsubject: " nil t)
6297                 (header-set-subject header (gnus-header-value))
6298               (header-set-subject header "(none)"))
6299             (goto-char p)
6300             (and (search-forward "\nxref: " nil t)
6301                  (header-set-xref header (gnus-header-value)))
6302             (goto-char p)
6303             (or (numberp (and (search-forward "\nlines: " nil t)
6304                               (header-set-lines header (read cur))))
6305                 (header-set-lines header 0))
6306             (goto-char p)
6307             (and (search-forward "\ndate: " nil t)
6308                  (header-set-date header (gnus-header-value)))
6309             (goto-char p)
6310             (if (search-forward "\nmessage-id: " nil t)
6311                 (header-set-id header (setq id (gnus-header-value)))
6312               ;; If there was no message-id, we just fake one to make
6313               ;; subsequent routines simpler.
6314               (header-set-id 
6315                header 
6316                (setq id (concat "none+" 
6317                                 (int-to-string 
6318                                  (setq gnus-newsgroup-none-id 
6319                                        (1+ gnus-newsgroup-none-id)))))))
6320             (goto-char p)
6321             (if (search-forward "\nreferences: " nil t)
6322                 (progn
6323                   (header-set-references header (gnus-header-value))
6324                   (setq end (match-end 0))
6325                   (save-excursion
6326                     (setq ref 
6327                           (downcase
6328                            (buffer-substring
6329                             (progn 
6330                               (end-of-line)
6331                               (search-backward ">" end t)
6332                               (1+ (point)))
6333                             (progn
6334                               (search-backward "<" end t)
6335                               (point)))))))
6336               ;; Get the references from the in-reply-to header if there
6337               ;; ware no references and the in-reply-to header looks
6338               ;; promising. 
6339               (if (and (search-forward "\nin-reply-to: " nil t)
6340                        (setq in-reply-to (gnus-header-value))
6341                        (string-match "<[^>]+>" in-reply-to))
6342                   (progn
6343                     (header-set-references 
6344                      header 
6345                      (setq ref (substring in-reply-to (match-beginning 0)
6346                                           (match-end 0))))
6347                     (setq ref (downcase ref)))
6348                 (setq ref "none")))
6349             ;; We do some threading while we read the headers. The
6350             ;; message-id and the last reference are both entered into
6351             ;; the same hash table. Some tippy-toeing around has to be
6352             ;; done in case an article has arrived before the article
6353             ;; which it refers to.
6354             (if (boundp (setq dep (intern (downcase id) dependencies)))
6355                 (if (car (symbol-value dep))
6356                     ;; An article with this Message-ID has already
6357                     ;; been seen, so we ignore this one, except we add
6358                     ;; any additional Xrefs (in case the two articles
6359                     ;; came from different servers.
6360                     (progn
6361                       (header-set-xref 
6362                        (car (symbol-value dep))
6363                        (concat (or (header-xref (car (symbol-value dep))) "")
6364                                (or (header-xref header) "")))
6365                       (setq header nil))
6366                   (setcar (symbol-value dep) header))
6367               (set dep (list header)))
6368             (if header
6369                 (progn
6370                   (if (boundp (setq dep (intern ref dependencies)))
6371                       (setcdr (symbol-value dep) 
6372                               (cons header (cdr (symbol-value dep))))
6373                     (set dep (list nil header)))
6374                   (setq headers (cons header headers))))
6375             (goto-char (point-max))))))
6376     (nreverse headers)))
6377
6378 ;; The following macros and functions were written by Felix Lee
6379 ;; <flee@cse.psu.edu>. 
6380
6381 (defmacro gnus-nov-read-integer ()
6382   '(prog1
6383        (if (= (following-char) ?\t)
6384            0
6385          (let ((num (condition-case nil (read buffer) (error nil))))
6386            (if (numberp num) num 0)))
6387      (or (eobp) (forward-char 1))))
6388
6389 (defmacro gnus-nov-skip-field ()
6390   '(search-forward "\t" eol 'move))
6391
6392 (defmacro gnus-nov-field ()
6393   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6394
6395 ;; Goes through the xover lines and returns a list of vectors
6396 (defun gnus-get-newsgroup-headers-xover (sequence)
6397   "Parse the news overview data in the server buffer, and return a
6398 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6399   ;; Get the Xref when the users reads the articles since most/some
6400   ;; NNTP servers do not include Xrefs when using XOVER.
6401   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6402   (let ((cur nntp-server-buffer)
6403         (dependencies gnus-newsgroup-dependencies)
6404         (none 0)
6405         number headers header)
6406     (save-excursion
6407       (set-buffer nntp-server-buffer)
6408       (goto-char (point-min))
6409       (while (and sequence (not (eobp)))
6410         (setq number (read cur))
6411         (while (and sequence (< (car sequence) number))
6412           (setq sequence (cdr sequence)))
6413         (and sequence 
6414              (eq number (car sequence))
6415              (progn
6416                (setq sequence (cdr sequence))
6417                (if (setq header 
6418                          (inline (gnus-nov-parse-line number dependencies)))
6419                    (setq headers (cons header headers)))))
6420         (forward-line 1))
6421       (setq headers (nreverse headers)))
6422     headers))
6423
6424 ;; This function has to be called with point after the article number
6425 ;; on the beginning of the line.
6426 (defun gnus-nov-parse-line (number dependencies)
6427   (let ((none 0)
6428         (eol (gnus-point-at-eol)) 
6429         (buffer (current-buffer))
6430         header ref id dep)
6431
6432     ;; overview: [num subject from date id refs chars lines misc]
6433     (narrow-to-region (point) eol)
6434     (forward-char)
6435
6436     (condition-case nil
6437         (setq header
6438               (vector 
6439                number                   ; number
6440                (gnus-nov-field)         ; subject
6441                (gnus-nov-field)         ; from
6442                (gnus-nov-field)         ; date
6443                (setq id (or (gnus-nov-field)
6444                             (concat "none+"
6445                                     (int-to-string 
6446                                      (setq none (1+ none)))))) ; id
6447                (progn
6448                  (save-excursion
6449                    (let ((beg (point)))
6450                      (search-forward "\t" eol)
6451                      (if (search-backward ">" beg t)
6452                          (setq ref 
6453                                (downcase 
6454                                 (buffer-substring 
6455                                  (1+ (point))
6456                                  (progn
6457                                    (search-backward "<" beg t)
6458                                    (point)))))
6459                        (setq ref nil))))
6460                  (gnus-nov-field))      ; refs
6461                (gnus-nov-read-integer)  ; chars
6462                (gnus-nov-read-integer)  ; lines
6463                (if (= (following-char) ?\n)
6464                    nil
6465                  (gnus-nov-field))      ; misc
6466                ))
6467       (quit (progn 
6468                (setq header nil)
6469                (goto-char eol))))
6470
6471     (widen)
6472
6473     ;; We build the thread tree.
6474     (and header
6475          (if (boundp (setq dep (intern (downcase id) dependencies)))
6476              (if (car (symbol-value dep))
6477                  ;; An article with this Message-ID has already been seen,
6478                  ;; so we ignore this one, except we add any additional
6479                  ;; Xrefs (in case the two articles came from different
6480                  ;; servers.
6481                  (progn
6482                    (header-set-xref 
6483                     (car (symbol-value dep))
6484                     (concat (or (header-xref (car (symbol-value dep))) "")
6485                             (or (header-xref header) "")))
6486                    (setq header nil))
6487                (setcar (symbol-value dep) header))
6488            (set dep (list header))))
6489     (if header
6490         (progn
6491           (if (boundp (setq dep (intern (or ref "none") 
6492                                         dependencies)))
6493               (setcdr (symbol-value dep) 
6494                       (cons header (cdr (symbol-value dep))))
6495             (set dep (list nil header)))))
6496     header))
6497
6498 (defun gnus-article-get-xrefs ()
6499   "Fill in the Xref value in `gnus-current-headers', if necessary.
6500 This is meant to be called in `gnus-article-internal-prepare-hook'."
6501   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6502                                  gnus-current-headers)))
6503     (or (not gnus-use-cross-reference)
6504         (and (header-xref headers)
6505              (not (string= (header-xref headers) "")))
6506         (let ((case-fold-search t)
6507               xref)
6508           (save-restriction
6509             (gnus-narrow-to-headers)
6510             (goto-char (point-min))
6511             (if (or (and (eq (downcase (following-char)) ?x)
6512                          (looking-at "Xref:"))
6513                     (search-forward "\nXref:" nil t))
6514                 (progn
6515                   (goto-char (1+ (match-end 0)))
6516                   (setq xref (buffer-substring (point) 
6517                                                (progn (end-of-line) (point))))
6518                   (header-set-xref headers xref))))))))
6519
6520 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6521 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6522
6523 ;; Return a header specified by a NUMBER.
6524 (defun gnus-get-header-by-number (number)
6525   (save-excursion
6526     (set-buffer gnus-summary-buffer)
6527     (or gnus-newsgroup-headers-hashtb-by-number
6528         (gnus-make-headers-hashtable-by-number))
6529     (gnus-gethash (int-to-string number)
6530                   gnus-newsgroup-headers-hashtb-by-number)))
6531
6532 (defun gnus-make-headers-hashtable-by-number ()
6533   "Make hashtable for the variable gnus-newsgroup-headers by number."
6534   (save-excursion
6535     (set-buffer gnus-summary-buffer)
6536     (let ((headers gnus-newsgroup-headers)
6537           header)
6538       (setq gnus-newsgroup-headers-hashtb-by-number
6539             (gnus-make-hashtable (length headers)))
6540       (while headers
6541         (setq header (car headers))
6542         (gnus-sethash (int-to-string (header-number header))
6543                       header gnus-newsgroup-headers-hashtb-by-number)
6544         (setq headers (cdr headers))))))
6545
6546 (defun gnus-more-header-backward ()
6547   "Find new header backward."
6548   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6549         (artnum gnus-newsgroup-begin)
6550         (header nil))
6551     (while (and (not header)
6552                 (> artnum first))
6553       (setq artnum (1- artnum))
6554       (setq header (gnus-read-header artnum)))
6555     header))
6556
6557 (defun gnus-more-header-forward (&optional backward)
6558   "Find new header forward.
6559 If BACKWARD, find new header backward instead."
6560   (if backward
6561       (gnus-more-header-backward)
6562     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6563           (artnum gnus-newsgroup-end)
6564           (header nil))
6565       (while (and (not header)
6566                   (< artnum last))
6567         (setq artnum (1+ artnum))
6568         (setq header (gnus-read-header artnum)))
6569       header)))
6570
6571 (defun gnus-extend-newsgroup (header &optional backward)
6572   "Extend newsgroup selection with HEADER.
6573 Optional argument BACKWARD means extend toward backward."
6574   (if header
6575       (let ((artnum (header-number header)))
6576         (setq gnus-newsgroup-headers
6577               (if backward
6578                   (cons header gnus-newsgroup-headers)
6579                 (nconc gnus-newsgroup-headers (list header))))
6580         (setq gnus-newsgroup-unselected
6581               (delq artnum gnus-newsgroup-unselected))
6582         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6583         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6584
6585 (defun gnus-summary-work-articles (n)
6586   "Return a list of articles to be worked upon. The prefix argument,
6587 the list of process marked articles, and the current article will be
6588 taken into consideration."
6589   (let (articles)
6590     (if (and n (numberp n))
6591         (let ((backward (< n 0))
6592               (n (abs n)))
6593           (save-excursion
6594             (while (and (> n 0)
6595                         (setq articles (cons (gnus-summary-article-number) 
6596                                              articles))
6597                         (gnus-summary-search-forward nil nil backward))
6598               (setq n (1- n))))
6599           (sort articles (function <)))
6600       (or (reverse gnus-newsgroup-processable)
6601           (list (gnus-summary-article-number))))))
6602
6603 (defun gnus-summary-search-group (&optional backward use-level)
6604   "Search for next unread newsgroup.
6605 If optional argument BACKWARD is non-nil, search backward instead."
6606   (save-excursion
6607     (set-buffer gnus-group-buffer)
6608     (save-excursion
6609       ;; We don't want to alter current point of group mode buffer.
6610       (if (gnus-group-search-forward 
6611            backward nil
6612            (if use-level (gnus-group-group-level) nil))
6613           (gnus-group-group-name)))))
6614
6615 (defun gnus-summary-best-group (&optional exclude-group)
6616   "Find the name of the best unread group.
6617 If EXCLUDE-GROUP, do not go to this group."
6618   (save-excursion
6619     (set-buffer gnus-group-buffer)
6620     (save-excursion
6621       (gnus-group-best-unread-group exclude-group))))
6622
6623 (defun gnus-summary-search-subject (&optional backward unread subject)
6624   "Search for article forward.
6625 If BACKWARD is non-nil, search backward.
6626 If UNREAD is non-nil, only unread articles are selected.
6627 If SUBJECT is non-nil, the article which has the same subject will be
6628 searched for." 
6629   (let ((func (if backward 'previous-single-property-change
6630                 'next-single-property-change))
6631         (beg (point))
6632         (did t)
6633         pos)
6634     (beginning-of-line)
6635     (and gnus-summary-check-current unread
6636          (eq (nth 1 (get-text-property (point) 'gnus)) gnus-unread-mark)
6637          (setq did nil))
6638     (if (not did)
6639         ()
6640       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6641       (while
6642           (and 
6643            (setq pos (funcall func (point) 'gnus))
6644            (goto-char (if backward (1- pos) pos))
6645            (setq did
6646                  (not (and
6647                        (or (not unread)
6648                            (eq (nth 1 (get-text-property (point) 'gnus))
6649                                gnus-unread-mark))
6650                        (or (not subject)
6651                            (equal (gnus-simplify-subject-re subject)
6652                                   (gnus-simplify-subject-re
6653                                    (gnus-summary-subject-string)))))))
6654            (if backward (if (bobp) nil (forward-char -1) t)
6655              (if (eobp) nil (forward-char 1) t)))))
6656     (if did
6657         (progn (goto-char beg) nil)
6658       (prog1
6659           (car (get-text-property (point) 'gnus))
6660         (gnus-summary-position-cursor)))))
6661
6662 (defun gnus-summary-search-forward (&optional unread subject backward)
6663   "Search for article forward.
6664 If UNREAD is non-nil, only unread articles are selected.
6665 If SUBJECT is non-nil, the article which has the same subject will be
6666 searched for. 
6667 If BACKWARD is non-nil, the search will be performed backwards instead."
6668   (gnus-summary-search-subject backward unread subject))
6669
6670 (defun gnus-summary-search-backward (&optional unread subject)
6671   "Search for article backward.
6672 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6673 If 2nd optional argument SUBJECT is non-nil, the article which has
6674 the same subject will be searched for."
6675   (gnus-summary-search-forward unread subject t))
6676
6677 (defun gnus-summary-article-number (&optional number-or-nil)
6678   "The article number of the article on the current line.
6679 If there isn's an article number here, then we return the current
6680 article number."
6681   (let* ((p (point))
6682          (number (car (get-text-property 
6683                        (progn (beginning-of-line) 
6684                               (prog1 (point) (goto-char p)))
6685                        'gnus))))
6686     (if number-or-nil number (or number gnus-current-article))))
6687
6688 (defun gnus-summary-thread-level ()
6689   "The thread level of the article on the current line."
6690   (or (nth 2 (get-text-property (gnus-point-at-bol) 'gnus))
6691       0))
6692
6693 (defun gnus-summary-pseudo-article ()
6694   "The thread level of the article on the current line."
6695   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
6696
6697 (defun gnus-summary-article-mark ()
6698   "The mark on the current line."
6699   (nth 1 (get-text-property (gnus-point-at-bol) 'gnus)))
6700
6701 (defun gnus-summary-subject-string ()
6702   "Return current subject string or nil if nothing."
6703   (let ((article (gnus-summary-article-number))
6704         header)
6705     (and article 
6706          (setq header (gnus-get-header-by-number article))
6707          (vectorp header)
6708          (header-subject header))))
6709
6710 (defalias 'gnus-summary-score 'gnus-summary-article-score)
6711 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
6712 (defun gnus-summary-article-score ()
6713   "Return current article score."
6714   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
6715       gnus-summary-default-score 0))
6716
6717 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
6718
6719 (defun gnus-summary-recenter ()
6720   "Center point in the summary window.
6721 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6722 displayed, no centering will be performed." 
6723   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6724   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6725   (let* ((top (cond ((< (window-height) 4) 0)
6726                     ((< (window-height) 6) 1)
6727                     (t 2)))
6728          (height (1- (window-height)))
6729          (bottom (save-excursion (goto-char (point-max))
6730                                  (forward-line (- height))
6731                                  (point)))
6732          (window (get-buffer-window (current-buffer))))
6733     (and 
6734      ;; The user has to want it,
6735      gnus-auto-center-summary 
6736      ;; the article buffer must be displayed,
6737      (get-buffer-window gnus-article-buffer)
6738      ;; Set the window start to either `bottom', which is the biggest
6739      ;; possible valid number, or the second line from the top,
6740      ;; whichever is the least.
6741      (set-window-start
6742       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
6743
6744 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
6745 (defun gnus-short-group-name (group &optional levels)
6746   "Collapse GROUP name LEVELS."
6747   (let ((name "") (foreign "")
6748         (levels (or levels 2)))
6749     (if (string-match ":" group)
6750         (setq foreign (substring group 0 (match-end 0))
6751               group (substring group (match-end 0))))
6752     (while group
6753       (if (and (string-match "\\." group) (> levels 0))
6754           (setq name (concat name (substring group 0 1))
6755                 group (substring group (match-end 0))
6756                 levels (- levels 1)
6757                 name (concat name "."))
6758         (setq name (concat foreign name group)
6759               group nil)))
6760     name))
6761
6762 (defun gnus-summary-jump-to-group (newsgroup)
6763   "Move point to NEWSGROUP in group mode buffer."
6764   ;; Keep update point of group mode buffer if visible.
6765   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6766       (save-window-excursion
6767         ;; Take care of tree window mode.
6768         (if (get-buffer-window gnus-group-buffer)
6769             (pop-to-buffer gnus-group-buffer))
6770         (gnus-group-jump-to-group newsgroup))
6771     (save-excursion
6772       ;; Take care of tree window mode.
6773       (if (get-buffer-window gnus-group-buffer)
6774           (pop-to-buffer gnus-group-buffer)
6775         (set-buffer gnus-group-buffer))
6776       (gnus-group-jump-to-group newsgroup))))
6777
6778 ;; This function returns a list of article numbers based on the
6779 ;; difference between the ranges of read articles in this group and
6780 ;; the range of active articles.
6781 (defun gnus-list-of-unread-articles (group)
6782   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6783          (active (gnus-gethash group gnus-active-hashtb))
6784          (last (cdr active))
6785          unread first nlast unread)
6786     ;; If none are read, then all are unread. 
6787     (if (not read)
6788         (setq first (car active))
6789       ;; If the range of read articles is a single range, then the
6790       ;; first unread article is the article after the last read
6791       ;; article. Sounds logical, doesn't it?
6792       (if (not (listp (cdr read)))
6793           (setq first (1+ (cdr read)))
6794         ;; `read' is a list of ranges.
6795         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
6796                                 (car (car read)))) 1)
6797             (setq first 1))
6798         (while read
6799           (if first 
6800               (while (< first nlast)
6801                 (setq unread (cons first unread))
6802                 (setq first (1+ first))))
6803           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
6804           (setq nlast (if (atom (car (cdr read))) 
6805                           (car (cdr read))
6806                         (car (car (cdr read)))))
6807           (setq read (cdr read)))))
6808     ;; And add the last unread articles.
6809     (while (<= first last)
6810       (setq unread (cons first unread))
6811       (setq first (1+ first)))
6812     ;; Return the list of unread articles.
6813     (nreverse unread)))
6814
6815 (defun gnus-list-of-read-articles (group)
6816   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
6817         (active (gnus-gethash group gnus-active-hashtb)))
6818     (and info active
6819          (gnus-sorted-complement 
6820           (gnus-uncompress-range active) 
6821           (gnus-list-of-unread-articles group)))))
6822
6823 ;; Various summary commands
6824
6825 (defun gnus-summary-universal-argument ()
6826   "Perform any operation on all articles marked with the process mark."
6827   (interactive)
6828   (gnus-set-global-variables)
6829   (let ((articles (reverse gnus-newsgroup-processable))
6830         key func)
6831     (or articles (error "No articles marked"))
6832     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
6833         (error "Undefined key"))
6834     (while articles
6835       (gnus-summary-goto-subject (car articles))
6836       (command-execute func)
6837       (gnus-summary-remove-process-mark (car articles))
6838       (setq articles (cdr articles)))))
6839
6840 (defun gnus-summary-toggle-truncation (arg)
6841   "Toggle truncation of summary lines.
6842 With arg, turn line truncation on iff arg is positive."
6843   (interactive "P")
6844   (setq truncate-lines
6845         (if (null arg) (not truncate-lines)
6846           (> (prefix-numeric-value arg) 0)))
6847   (redraw-display))
6848
6849 (defun gnus-summary-reselect-current-group (all)
6850   "Once exit and then reselect the current newsgroup.
6851 The prefix argument ALL means to select all articles."
6852   (interactive "P")
6853   (gnus-set-global-variables)
6854   (let ((current-subject (gnus-summary-article-number))
6855         (group gnus-newsgroup-name))
6856     (setq gnus-newsgroup-threads nil)
6857     (gnus-summary-exit t)
6858     ;; We have to adjust the point of group mode buffer because the
6859     ;; current point was moved to the next unread newsgroup by
6860     ;; exiting.
6861     (gnus-summary-jump-to-group group)
6862     (gnus-group-read-group all t)
6863     (gnus-summary-goto-subject current-subject)))
6864
6865 (defun gnus-summary-rescan-group (all)
6866   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6867   (interactive "P")
6868   (gnus-set-global-variables)
6869   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
6870   (let ((group gnus-newsgroup-name))
6871     (gnus-summary-exit)
6872     (gnus-summary-jump-to-group group)
6873     (save-excursion
6874       (set-buffer gnus-group-buffer)
6875       (gnus-group-get-new-news-this-group 1))
6876     (gnus-summary-jump-to-group group)
6877     (gnus-group-read-group all)))
6878
6879 (defun gnus-summary-update-info ()
6880   (let* ((group gnus-newsgroup-name)
6881          (method (car (gnus-find-method-for-group group))))
6882     (if gnus-newsgroup-kill-headers
6883         (setq gnus-newsgroup-killed
6884               (gnus-compress-sequence
6885                (nconc
6886                 (gnus-set-sorted-intersection
6887                  (gnus-uncompress-range gnus-newsgroup-killed)
6888                  (setq gnus-newsgroup-unselected
6889                        (sort gnus-newsgroup-unselected '<)))
6890                 (setq gnus-newsgroup-unreads
6891                       (sort gnus-newsgroup-unreads '<))) t)))
6892     (or (listp (cdr gnus-newsgroup-killed))
6893         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6894     (let ((updated nil)
6895           (headers gnus-newsgroup-headers))
6896       (gnus-close-group group)
6897       (run-hooks 'gnus-exit-group-hook)
6898       (gnus-update-read-articles 
6899        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
6900        gnus-newsgroup-marked
6901        t gnus-newsgroup-replied gnus-newsgroup-expirable
6902        gnus-newsgroup-killed gnus-newsgroup-dormant
6903        gnus-newsgroup-bookmarks 
6904        (and gnus-save-score gnus-newsgroup-scored))
6905       (and gnus-use-cross-reference
6906            (gnus-mark-xrefs-as-read 
6907             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
6908       ;; Do adaptive scoring, and possibly save score files.
6909       (and gnus-newsgroup-adaptive
6910            (gnus-score-adaptive))
6911       (and (fboundp 'gnus-score-save)
6912            (funcall 'gnus-score-save))
6913       ;; Do not switch windows but change the buffer to work.
6914       (set-buffer gnus-group-buffer)
6915       (or (eq 'nndigest method)
6916           (gnus-group-update-group group)))))
6917   
6918 (defun gnus-summary-exit (&optional temporary)
6919   "Exit reading current newsgroup, and then return to group selection mode.
6920 gnus-exit-group-hook is called with no arguments if that value is non-nil."
6921   (interactive)
6922   (gnus-set-global-variables)
6923   (gnus-kill-save-kill-buffer)
6924   (let* ((group gnus-newsgroup-name)
6925          (quit-buffer (cdr (assoc 'quit-buffer (gnus-find-method-for-group
6926                                                 gnus-newsgroup-name))))
6927          (mode major-mode)
6928          (method (car (gnus-find-method-for-group group)))
6929          (buf (current-buffer)))
6930     (gnus-summary-update-info) ; Make all changes in this group permanent.
6931     ;; Make sure where I was, and go to next newsgroup.
6932     (if (eq method 'nndigest)
6933         ()
6934       (gnus-group-jump-to-group group)
6935       (gnus-group-next-unread-group 1))
6936     (if temporary
6937         nil                             ;Nothing to do.
6938       ;; We set all buffer-local variables to nil. It is unclear why
6939       ;; this is needed, but if we don't, buffer-local variables are
6940       ;; not garbage-collected, it seems. This would the lead to en
6941       ;; ever-growing Emacs.
6942       (set-buffer buf)
6943       (gnus-summary-clear-local-variables)
6944       ;; We clear the global counterparts of the buffer-local
6945       ;; variables as well, just to be on the safe side.
6946       (gnus-configure-windows 'group)
6947       (gnus-summary-clear-local-variables)
6948       ;; Return to group mode buffer. 
6949       (if (eq mode 'gnus-summary-mode)
6950           (gnus-kill-buffer buf))
6951       (if (get-buffer gnus-article-buffer)
6952           (bury-buffer gnus-article-buffer))
6953       (setq gnus-current-select-method gnus-select-method)
6954       (pop-to-buffer gnus-group-buffer)
6955       (if (eq method 'nndigest)
6956           ()
6957         (gnus-group-jump-to-group group)
6958         (gnus-group-next-unread-group 1))
6959       (if (gnus-buffer-exists-p quit-buffer)
6960           (progn
6961             (switch-to-buffer quit-buffer)
6962             (gnus-set-global-variables)
6963             (gnus-configure-windows 'summary))))))
6964
6965 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6966 (defun gnus-summary-exit-no-update (&optional no-questions)
6967   "Quit reading current newsgroup without updating read article info."
6968   (interactive)
6969   (let* ((group gnus-newsgroup-name)
6970          (quit-buffer (cdr (assoc 'quit-buffer 
6971                                   (gnus-find-method-for-group group)))))
6972     (if (or no-questions
6973             gnus-expert-user
6974             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
6975         (progn
6976           (gnus-close-group group)
6977           (gnus-summary-clear-local-variables)
6978           (set-buffer gnus-group-buffer)
6979           (gnus-summary-clear-local-variables)
6980           ;; Return to group selection mode.
6981           (gnus-configure-windows 'group)
6982           (if (get-buffer gnus-summary-buffer)
6983               (kill-buffer gnus-summary-buffer))
6984           (if (get-buffer gnus-article-buffer)
6985               (bury-buffer gnus-article-buffer))
6986           (if (equal (gnus-group-group-name) group)
6987               (gnus-group-next-unread-group 1))
6988           (if (gnus-buffer-exists-p quit-buffer)
6989               (progn
6990                 (switch-to-buffer quit-buffer)
6991                 (gnus-configure-windows 'summary)))))))
6992
6993 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6994 (defun gnus-summary-fetch-faq (group)
6995   "Fetch the FAQ for the current group."
6996   (interactive (list gnus-newsgroup-name))
6997   (gnus-configure-windows 'summary-faq)
6998   (find-file (concat gnus-group-faq-directory group)))
6999
7000 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7001 (defun gnus-summary-describe-group (force)
7002   "Describe the current newsgroup."
7003   (interactive "P")
7004   (gnus-group-describe-group force gnus-newsgroup-name))
7005
7006 (defun gnus-summary-describe-briefly ()
7007   "Describe summary mode commands briefly."
7008   (interactive)
7009   (gnus-message 6
7010     (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")))
7011
7012 ;; Walking around group mode buffer from summary mode.
7013
7014 (defun gnus-summary-next-group (&optional no-article group backward)
7015   "Exit current newsgroup and then select next unread newsgroup.
7016 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7017 If BACKWARD, go to previous group instead."
7018   (interactive "P")
7019   (gnus-set-global-variables)
7020   (let ((ingroup gnus-newsgroup-name)
7021         (sumbuf (current-buffer))
7022         num)
7023     (gnus-summary-exit t)               ;Update all information.
7024     (if (and group
7025              (or (and (numberp (setq num (car (gnus-gethash
7026                                                group gnus-newsrc-hashtb))))
7027                       (< num 1))
7028                  (null num)))
7029         (progn
7030           (gnus-group-jump-to-group group)
7031           (setq group nil))
7032       (gnus-group-jump-to-group ingroup))
7033     (gnus-summary-search-group backward)
7034     (let ((group (or group (gnus-summary-search-group backward)))
7035           (buf gnus-summary-buffer))
7036       (if (null group)
7037           (gnus-summary-exit-no-update t)
7038         (gnus-message 5 "Selecting %s..." group)
7039         ;; We are now in group mode buffer.
7040         ;; Make sure group mode buffer point is on GROUP.
7041         (gnus-group-jump-to-group group)
7042         (if (not (eq gnus-auto-select-next 'quietly))
7043             (progn
7044               (gnus-summary-read-group group nil no-article buf)
7045               (and (string= gnus-newsgroup-name ingroup)
7046                    (bufferp sumbuf) (buffer-name sumbuf)
7047                    (progn
7048                      (set-buffer (setq gnus-summary-buffer sumbuf))
7049                      (gnus-summary-exit-no-update t))))
7050           (let ((prevgroup group))
7051             (gnus-summary-read-group group nil no-article buf)
7052             (while (and (string= gnus-newsgroup-name ingroup)
7053                         (bufferp sumbuf) 
7054                         (buffer-name sumbuf)
7055                         (not (string= prevgroup (gnus-group-group-name))))
7056               (set-buffer gnus-group-buffer)
7057               (gnus-summary-read-group 
7058                (setq prevgroup (gnus-group-group-name)) 
7059                nil no-article buf))
7060             (and (string= prevgroup (gnus-group-group-name))
7061                  ;; We have reached the final group in the group
7062                  ;; buffer.
7063                  (progn
7064                    (set-buffer sumbuf)
7065                    (gnus-summary-exit)))))))))
7066
7067 (defun gnus-summary-prev-group (no-article)
7068   "Exit current newsgroup and then select previous unread newsgroup.
7069 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7070   (interactive "P")
7071   (gnus-summary-next-group no-article nil t))
7072
7073 ;; Walking around summary lines.
7074
7075 (defun gnus-summary-first-subject (unread)
7076   "Go to the first unread subject.
7077 If UNREAD is non-nil, go to the first unread article.
7078 Returns nil if there are no unread articles."
7079   (interactive "P")
7080   (let ((begin (point)))
7081     (goto-char (point-min))
7082     (if (not unread)
7083         t
7084       (while (and (not (eq (nth 1 (get-text-property (point) 'gnus))
7085                            gnus-unread-mark))
7086                   (zerop (forward-line 1))))
7087       (prog1
7088           (if (not (eobp))
7089               t
7090             ;; If there is no unread articles, stay where you are.
7091             (goto-char begin)
7092             (gnus-message 3 "No more unread articles")
7093             nil)
7094         (gnus-summary-position-cursor)))))
7095
7096 (defun gnus-summary-next-subject (n &optional unread dont-display)
7097   "Go to next N'th summary line.
7098 If N is negative, go to the previous N'th subject line.
7099 If UNREAD is non-nil, only unread articles are selected.
7100 The difference between N and the actual number of steps taken is
7101 returned."
7102   (interactive "p")
7103   (let ((backward (< n 0))
7104         (n (abs n)))
7105     (while (and (> n 0)
7106                 (gnus-summary-search-forward unread nil backward))
7107       (setq n (1- n)))
7108     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7109                                (if unread " unread" "")))
7110     (or dont-display
7111         (progn
7112           (gnus-summary-recenter)
7113           (gnus-summary-position-cursor)))
7114   n))
7115
7116 (defun gnus-summary-next-unread-subject (n)
7117   "Go to next N'th unread summary line."
7118   (interactive "p")
7119   (gnus-summary-next-subject n t))
7120
7121 (defun gnus-summary-prev-subject (n &optional unread)
7122   "Go to previous N'th summary line.
7123 If optional argument UNREAD is non-nil, only unread article is selected."
7124   (interactive "p")
7125   (gnus-summary-next-subject (- n) unread))
7126
7127 (defun gnus-summary-prev-unread-subject (n)
7128   "Go to previous N'th unread summary line."
7129   (interactive "p")
7130   (gnus-summary-next-subject (- n) t))
7131
7132 (defun gnus-summary-goto-subject (article)
7133   "Go the subject line of ARTICLE."
7134   (interactive
7135    (list
7136     (string-to-int
7137      (completing-read "Article number: "
7138                       (mapcar
7139                        (lambda (headers)
7140                          (list
7141                           (int-to-string (header-number headers))))
7142                        gnus-newsgroup-headers)
7143                       nil 'require-match))))
7144   (or article (error "No article number"))
7145   (let ((b (point)))
7146     (goto-char (point-min))
7147     (while (and (not (eq (car (get-text-property (point) 'gnus)) article))
7148                 (zerop (forward-line 1))))
7149     (gnus-summary-show-thread)
7150     ;; Skip dummy articles. 
7151     (if (eq (gnus-summary-article-mark) ?Z)
7152         (forward-line 1))
7153     (prog1
7154         (if (not (eobp))
7155             article
7156           (goto-char b)
7157           nil)
7158       (gnus-summary-position-cursor))))
7159
7160 ;; Walking around summary lines with displaying articles.
7161
7162 (defun gnus-summary-expand-window ()
7163   "Expand summary window to show headers full window."
7164   (interactive)
7165   (gnus-set-global-variables)
7166   (gnus-configure-windows 'summary))
7167
7168 (defun gnus-summary-display-article (article &optional all-header)
7169   "Display ARTICLE in article buffer."
7170   (gnus-set-global-variables)
7171   (if (null article)
7172       nil
7173     (gnus-article-prepare article all-header)
7174     (gnus-summary-show-thread)
7175     (if (eq (gnus-summary-article-mark) ?Z)
7176         (progn
7177           (forward-line 1)
7178           (gnus-summary-position-cursor)))
7179     (run-hooks 'gnus-select-article-hook)
7180     (gnus-summary-recenter)
7181 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
7182 ;    (sit-for 0)
7183     (gnus-summary-goto-subject article)
7184     ;; Successfully display article.
7185     (gnus-summary-update-line)
7186     (gnus-article-set-window-start 
7187      (cdr (assq article gnus-newsgroup-bookmarks)))
7188     t))
7189
7190 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7191   "Select the current article.
7192 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7193 non-nil, the article will be re-fetched even if it already present in
7194 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7195 be displayed."
7196   (and (not pseudo) (gnus-summary-pseudo-article)
7197        (error "This is a pseudo-article."))
7198   (let ((article (or article (gnus-summary-article-number)))
7199         (all-headers (not (not all-headers))) ;Must be T or NIL.
7200         did) 
7201     (prog1
7202         (save-excursion
7203           (set-buffer gnus-summary-buffer)
7204           (if (or (null gnus-current-article)
7205                   (null gnus-article-current)
7206                   (not (eq article (cdr gnus-article-current)))
7207                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7208                   force)
7209               ;; The requested article is different from the current article.
7210               (progn
7211                 (gnus-summary-display-article article all-headers)
7212                 (gnus-configure-windows 'article)
7213                 (setq did article))
7214             (if all-headers (gnus-article-show-all-headers))
7215             (gnus-configure-windows 'article)
7216             nil))
7217       (if did (gnus-article-set-window-start 
7218                (cdr (assq article gnus-newsgroup-bookmarks)))))))
7219
7220 (defun gnus-summary-set-current-mark (&optional current-mark)
7221   "Obsolete function."
7222   nil)
7223
7224 (defun gnus-summary-next-article (unread &optional subject backward)
7225   "Select the next article.
7226 If UNREAD, only unread articles are selected.
7227 If SUBJECT, only articles with SUBJECT are selected.
7228 If BACKWARD, the previous article is selected instead of the next."
7229   (interactive "P")
7230   (let ((opoint (point))
7231         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
7232         header)
7233     (cond
7234      ;; Is there such an article?
7235      ((gnus-summary-display-article 
7236        (gnus-summary-search-forward unread subject backward))
7237       (gnus-summary-position-cursor))
7238      ;; If not, we try the first unread, if that is wanted.
7239      ((and subject
7240            gnus-auto-select-same
7241            (gnus-summary-first-unread-article))
7242       (gnus-message 6 "Wrapped"))
7243      ;; Try to get next/previous article not displayed in this group.
7244      ((and gnus-auto-extend-newsgroup
7245            (not unread) (not subject)
7246            (setq header (gnus-more-header-forward backward)))
7247       (gnus-extend-newsgroup header backward)
7248       (let ((buffer-read-only nil))
7249         (goto-char (if backward (point-min) (point-max)))
7250         (gnus-summary-prepare-threads (list header) 0))
7251       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7252                                    gnus-newsgroup-end)))
7253      ;; Go to next/previous group.
7254      (t
7255       (or (eq method 'nndigest)
7256           (gnus-summary-jump-to-group gnus-newsgroup-name))
7257       (let ((cmd (aref (this-command-keys) 0))
7258             (group 
7259              (if (eq gnus-keep-same-level 'best) 
7260                  (gnus-summary-best-group gnus-newsgroup-name)
7261                (gnus-summary-search-group backward gnus-keep-same-level))))
7262         ;; Keep just the event type of CMD.
7263         (and (listp cmd) (setq cmd (car cmd)))
7264         ;; Select next unread newsgroup automagically.
7265         (cond 
7266          ((not gnus-auto-select-next)
7267           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7268          ((eq gnus-auto-select-next 'quietly)
7269           ;; Select quietly.
7270           (if (eq method 'nndigest)
7271               (gnus-summary-exit)
7272             (gnus-message 7 "No more%s articles (%s)..."
7273                           (if unread " unread" "") 
7274                           (if group (concat "selecting " group)
7275                             "exiting"))
7276             (gnus-summary-next-group nil group backward)))
7277          (t
7278           (let ((keystrokes '(?\C-n ?\C-p))
7279                 key)
7280             (while (or (null key) (memq key keystrokes))
7281               (gnus-message 
7282                7 "No more%s articles%s" (if unread " unread" "")
7283                (if (and group (not (eq method 'nndigest)))
7284                    (format " (Type %s for %s [%s])"
7285                            (single-key-description cmd) group
7286                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7287                  (format " (Type %s to exit %s)"
7288                          (single-key-description cmd)
7289                          gnus-newsgroup-name)))
7290               ;; Confirm auto selection.
7291               (let* ((event (read-event)))
7292                 (setq key (if (listp event) (car event) event))
7293                 (if (member key keystrokes)
7294                     (let ((obuf (current-buffer)))
7295                       (switch-to-buffer gnus-group-buffer)
7296                       (gnus-group-jump-to-group group)
7297                       (execute-kbd-macro (char-to-string key))
7298                       (setq group (gnus-group-group-name))
7299                       (switch-to-buffer obuf)))))
7300             (if (eq key cmd)
7301                 (if (or (not group) (eq method 'nndigest))
7302                     (gnus-summary-exit)
7303                   (gnus-summary-next-group nil group backward))
7304               (setq unread-command-events (list key)))))))))))
7305
7306 (defun gnus-summary-next-unread-article ()
7307   "Select unread article after current one."
7308   (interactive)
7309   (gnus-summary-next-article t (and gnus-auto-select-same
7310                                     (gnus-summary-subject-string))))
7311
7312 (defun gnus-summary-prev-article (unread &optional subject)
7313   "Select the article after the current one.
7314 If UNREAD is non-nil, only unread articles are selected."
7315   (interactive "P")
7316   (gnus-summary-next-article unread subject t))
7317
7318 (defun gnus-summary-prev-unread-article ()
7319   "Select unred article before current one."
7320   (interactive)
7321   (gnus-summary-prev-article t (and gnus-auto-select-same
7322                                     (gnus-summary-subject-string))))
7323
7324 (defun gnus-summary-next-page (lines &optional circular)
7325   "Show next page of selected article.
7326 If end of article, select next article.
7327 Argument LINES specifies lines to be scrolled up.
7328 If CIRCULAR is non-nil, go to the start of the article instead of 
7329 instead of selecting the next article when reaching the end of the
7330 current article." 
7331   (interactive "P")
7332   (setq gnus-summary-buffer (current-buffer))
7333   (gnus-set-global-variables)
7334   (let ((article (gnus-summary-article-number))
7335         (endp nil))
7336     (if (or (null gnus-current-article)
7337             (null gnus-article-current)
7338             (/= article (cdr gnus-article-current))
7339             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7340         ;; Selected subject is different from current article's.
7341         (gnus-summary-display-article article)
7342       (gnus-configure-windows 'article)
7343       (gnus-eval-in-buffer-window
7344        gnus-article-buffer
7345        (setq endp (gnus-article-next-page lines)))
7346       (gnus-summary-recenter)
7347       (if endp
7348           (cond (circular
7349                  (gnus-summary-beginning-of-article))
7350                 (lines
7351                  (gnus-message 3 "End of message"))
7352                 ((null lines)
7353                  (gnus-summary-next-unread-article)))))))
7354
7355 (defun gnus-summary-prev-page (lines)
7356   "Show previous page of selected article.
7357 Argument LINES specifies lines to be scrolled down."
7358   (interactive "P")
7359   (gnus-set-global-variables)
7360   (let ((article (gnus-summary-article-number)))
7361     (if (or (null gnus-current-article)
7362             (null gnus-article-current)
7363             (/= article (cdr gnus-article-current))
7364             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7365         ;; Selected subject is different from current article's.
7366         (gnus-summary-display-article article)
7367       (gnus-configure-windows 'article)
7368       (gnus-summary-recenter)
7369       (gnus-eval-in-buffer-window gnus-article-buffer
7370         (gnus-article-prev-page lines))))
7371   (gnus-summary-position-cursor))
7372
7373 (defun gnus-summary-scroll-up (lines)
7374   "Scroll up (or down) one line current article.
7375 Argument LINES specifies lines to be scrolled up (or down if negative)."
7376   (interactive "p")
7377   (gnus-set-global-variables)
7378   (or (gnus-summary-select-article nil nil 'pseudo)
7379       (gnus-eval-in-buffer-window 
7380        gnus-article-buffer
7381        (cond ((> lines 0)
7382               (if (gnus-article-next-page lines)
7383                   (gnus-message 3 "End of message")))
7384              ((< lines 0)
7385               (gnus-article-prev-page (- lines))))))
7386   (gnus-summary-recenter)
7387   (gnus-summary-position-cursor))
7388
7389 (defun gnus-summary-next-same-subject ()
7390   "Select next article which has the same subject as current one."
7391   (interactive)
7392   (gnus-set-global-variables)
7393   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7394
7395 (defun gnus-summary-prev-same-subject ()
7396   "Select previous article which has the same subject as current one."
7397   (interactive)
7398   (gnus-set-global-variables)
7399   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7400
7401 (defun gnus-summary-next-unread-same-subject ()
7402   "Select next unread article which has the same subject as current one."
7403   (interactive)
7404   (gnus-set-global-variables)
7405   (gnus-summary-next-article t (gnus-summary-subject-string)))
7406
7407 (defun gnus-summary-prev-unread-same-subject ()
7408   "Select previous unread article which has the same subject as current one."
7409   (interactive)
7410   (gnus-set-global-variables)
7411   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7412
7413 (defun gnus-summary-first-unread-article ()
7414   "Select the first unread article. 
7415 Return nil if there are no unread articles."
7416   (interactive)
7417   (gnus-set-global-variables)
7418   (prog1
7419       (if (gnus-summary-first-subject t)
7420           (gnus-summary-display-article (gnus-summary-article-number)))
7421     (gnus-summary-position-cursor)))
7422
7423 (defun gnus-summary-best-unread-article ()
7424   "Select the unread article with the highest score."
7425   (interactive)
7426   (gnus-set-global-variables)
7427   (let ((scored gnus-newsgroup-scored)
7428         (best -1000000)
7429         article art)
7430     (while scored
7431       (or (> best (cdr (car scored)))
7432           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7433                (not (memq art gnus-newsgroup-marked))
7434                (not (memq art gnus-newsgroup-dormant))
7435                (if (= best (cdr (car scored)))
7436                    (setq article (min art article))
7437                  (setq article art)
7438                  (setq best (cdr (car scored))))))
7439       (setq scored (cdr scored)))
7440     (if article 
7441         (gnus-summary-goto-article article)
7442       (gnus-summary-first-unread-article))
7443     (gnus-summary-position-cursor)))
7444
7445 (defun gnus-summary-goto-article (article &optional all-headers)
7446   "Fetch ARTICLE and display it if it exists.
7447 If ALL-HEADERS is non-nil, no header lines are hidden."
7448   (interactive
7449    (list
7450     (string-to-int
7451      (completing-read 
7452       "Article number: "
7453       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7454               gnus-newsgroup-headers) 
7455       nil 'require-match))))
7456   (prog1
7457       (and (gnus-summary-goto-subject article)
7458            (gnus-summary-display-article article all-headers))
7459     (gnus-summary-position-cursor)))
7460
7461 (defun gnus-summary-goto-last-article ()
7462   "Go to the last article."
7463   (interactive)
7464   (prog1
7465       (and gnus-last-article
7466            (gnus-summary-goto-article gnus-last-article))
7467     (gnus-summary-position-cursor)))
7468
7469 (defun gnus-summary-pop-article (number)
7470   "Pop one article off the history and go to the previous.
7471 NUMBER articles will be popped off."
7472   (interactive "p")
7473   (let (to)
7474     (setq gnus-newsgroup-history
7475           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7476     (if to
7477         (gnus-summary-goto-article (car to))
7478       (error "Article history empty")))
7479   (gnus-summary-position-cursor))
7480
7481 ;; Summary article oriented commands
7482
7483 (defun gnus-summary-refer-parent-article (n)
7484   "Refer parent article N times.
7485 The difference between N and the number of articles fetched is returned."
7486   (interactive "p")
7487   (gnus-set-global-variables)
7488   (while 
7489       (and 
7490        (> n 0)
7491        (let ((ref (header-references (gnus-get-header-by-number
7492                                       (gnus-summary-article-number)))))
7493          (if (and ref (not (equal ref ""))
7494                   (string-match "<[^<>]*>[ \t]*$" ref))
7495              (gnus-summary-refer-article 
7496               (substring ref (match-beginning 0) (match-end 0))))))
7497     (setq n (1- n)))
7498   (or (zerop n) 
7499       (gnus-message 1 "No references in article or expired article."))
7500   (gnus-summary-position-cursor)
7501   n)
7502     
7503 (defun gnus-summary-refer-article (message-id)
7504   "Refer article specified by MESSAGE-ID.
7505 NOTE: This command only works with newsgroup that use NNTP."
7506   (interactive "sMessage-ID: ")
7507   (if (or (not (stringp message-id))
7508           (zerop (length message-id)))
7509       ()
7510     ;; Construct the correct Message-ID if necessary.
7511     ;; Suggested by tale@pawl.rpi.edu.
7512     (or (string-match "^<" message-id)
7513         (setq message-id (concat "<" message-id)))
7514     (or (string-match ">$" message-id)
7515         (setq message-id (concat message-id ">")))
7516     (let ((header (car (gnus-gethash (downcase message-id)
7517                                      gnus-newsgroup-dependencies))))
7518       (if header
7519           (or (gnus-summary-goto-article (header-number header))
7520               ;; The header has been read, but the article had been
7521               ;; expunged, so we insert it again.
7522               (progn
7523                 (gnus-summary-insert-line
7524                  nil header 0 nil gnus-read-mark nil nil
7525                  (header-subject header))
7526                 (forward-line -1)
7527                 (header-number header)))
7528         (let ((gnus-override-method gnus-refer-article-method)
7529               (gnus-ancient-mark gnus-read-mark)
7530               number)
7531           (and gnus-refer-article-method
7532                (or (gnus-server-opened gnus-refer-article-method)
7533                    (gnus-open-server gnus-refer-article-method)))
7534           (if (gnus-article-prepare 
7535                message-id nil (gnus-read-header message-id))
7536               (progn
7537                 (setq number (header-number gnus-current-headers))
7538                 (gnus-rebuild-thread message-id)
7539                 (gnus-summary-goto-subject number)
7540                 (gnus-article-set-window-start 
7541                  (cdr (assq number gnus-newsgroup-bookmarks)))
7542                 message-id)
7543             (gnus-message 1 "No such references")
7544             nil))))))
7545
7546 (defun gnus-summary-enter-digest-group ()
7547   "Enter a digest group based on the current article."
7548   (interactive)
7549   (gnus-set-global-variables)
7550   (gnus-summary-select-article)
7551   ;; We do not want a narrowed article.
7552   (gnus-summary-stop-page-breaking)
7553   (let ((name (format "%s-%d" 
7554                       (gnus-group-prefixed-name 
7555                        gnus-newsgroup-name (list 'nndigest "")) 
7556                       gnus-current-article))
7557         (buf (current-buffer)))
7558     (set-buffer gnus-group-buffer)
7559     (gnus-sethash 
7560      name 
7561      (list t nil (list name gnus-level-default-subscribed nil nil 
7562                        (list 'nndigest gnus-article-buffer
7563                              (cons 'quit-buffer buf))))
7564      gnus-newsrc-hashtb)
7565     (condition-case ()
7566         (gnus-group-read-group t nil name)
7567       (error nil)
7568       (quit nil))
7569     (if (not (equal major-mode 'gnus-group-mode))
7570         ()
7571       (switch-to-buffer buf)
7572       (gnus-set-global-variables)
7573       (gnus-configure-windows 'summary)
7574       (gnus-message 3 "Article not a digest?"))))
7575   
7576 (defun gnus-summary-isearch-article ()
7577   "Do incremental search forward on current article."
7578   (interactive)
7579   (gnus-set-global-variables)
7580   (gnus-summary-select-article)
7581   (gnus-eval-in-buffer-window gnus-article-buffer
7582                               (isearch-forward)))
7583
7584 (defun gnus-summary-search-article-forward (regexp)
7585   "Search for an article containing REGEXP forward.
7586 gnus-select-article-hook is not called during the search."
7587   (interactive
7588    (list (read-string
7589           (concat "Search forward (regexp): "
7590                   (if gnus-last-search-regexp
7591                       (concat "(default " gnus-last-search-regexp ") "))))))
7592   (gnus-set-global-variables)
7593   (if (string-equal regexp "")
7594       (setq regexp (or gnus-last-search-regexp ""))
7595     (setq gnus-last-search-regexp regexp))
7596   (if (gnus-summary-search-article regexp nil)
7597       (gnus-eval-in-buffer-window 
7598        gnus-article-buffer
7599        (recenter 0))
7600     (error "Search failed: \"%s\"" regexp)))
7601
7602 (defun gnus-summary-search-article-backward (regexp)
7603   "Search for an article containing REGEXP backward.
7604 gnus-select-article-hook is not called during the search."
7605   (interactive
7606    (list (read-string
7607           (concat "Search backward (regexp): "
7608                   (if gnus-last-search-regexp
7609                       (concat "(default " gnus-last-search-regexp ") "))))))
7610   (gnus-set-global-variables)
7611   (if (string-equal regexp "")
7612       (setq regexp (or gnus-last-search-regexp ""))
7613     (setq gnus-last-search-regexp regexp))
7614   (if (gnus-summary-search-article regexp t)
7615       (gnus-eval-in-buffer-window
7616        gnus-article-buffer
7617        (recenter 0))
7618     (error "Search failed: \"%s\"" regexp)))
7619
7620 (defun gnus-summary-search-article (regexp &optional backward)
7621   "Search for an article containing REGEXP.
7622 Optional argument BACKWARD means do search for backward.
7623 gnus-select-article-hook is not called during the search."
7624   (let ((gnus-select-article-hook nil)  ;Disable hook.
7625         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7626         (re-search
7627          (if backward
7628              (function re-search-backward) (function re-search-forward)))
7629         (found nil)
7630         (last nil))
7631     ;; Hidden thread subtrees must be searched for ,too.
7632     (gnus-summary-show-all-threads)
7633     (if (eobp) (forward-line -1))
7634     ;; First of all, search current article.
7635     ;; We don't want to read article again from NNTP server nor reset
7636     ;; current point.
7637     (gnus-summary-select-article)
7638     (gnus-message 9 "Searching article: %d..." gnus-current-article)
7639     (setq last gnus-current-article)
7640     (gnus-eval-in-buffer-window gnus-article-buffer
7641       (save-restriction
7642         (widen)
7643         ;; Begin search from current point.
7644         (setq found (funcall re-search regexp nil t))))
7645     ;; Then search next articles.
7646     (while (and (not found)
7647                 (gnus-summary-display-article 
7648                  (gnus-summary-search-subject backward nil nil)))
7649       (gnus-message 9 "Searching article: %d..." gnus-current-article)
7650       (gnus-eval-in-buffer-window gnus-article-buffer
7651         (save-restriction
7652           (widen)
7653           (goto-char (if backward (point-max) (point-min)))
7654           (setq found (funcall re-search regexp nil t)))))
7655     (message "")
7656     ;; Adjust article pointer.
7657     (or (eq last gnus-current-article)
7658         (setq gnus-last-article last))
7659     ;; Return T if found such article.
7660     found))
7661
7662 (defun gnus-summary-execute-command (header regexp command &optional backward)
7663   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7664 If HEADER is an empty string (or nil), the match is done on the entire
7665 article. If BACKWARD (the prefix) is non-nil, search backward instead."
7666   (interactive
7667    (list (let ((completion-ignore-case t))
7668            (completing-read 
7669             "Header name: "
7670             (mapcar (lambda (string) (list string))
7671                     '("Number" "Subject" "From" "Lines" "Date"
7672                       "Message-ID" "Xref" "References"))
7673             nil 'require-match))
7674          (read-string "Regexp: ")
7675          (read-key-sequence "Command: ")
7676          current-prefix-arg))
7677   (gnus-set-global-variables)
7678   ;; Hidden thread subtrees must be searched as well.
7679   (gnus-summary-show-all-threads)
7680   ;; We don't want to change current point nor window configuration.
7681   (save-excursion
7682     (save-window-excursion
7683       (gnus-message 6 "Executing %s..." (key-description command))
7684       ;; We'd like to execute COMMAND interactively so as to give arguments.
7685       (gnus-execute header regexp
7686                     (` (lambda ()
7687                          (call-interactively '(, (key-binding command)))))
7688                     backward)
7689       (gnus-message 6 "Executing %s... done" (key-description command)))))
7690
7691 (defun gnus-summary-beginning-of-article ()
7692   "Scroll the article back to the beginning."
7693   (interactive)
7694   (gnus-set-global-variables)
7695   (gnus-summary-select-article)
7696   (gnus-eval-in-buffer-window
7697    gnus-article-buffer
7698    (widen)
7699    (goto-char (point-min))
7700    (and gnus-break-pages (gnus-narrow-to-page))))
7701
7702 (defun gnus-summary-end-of-article ()
7703   "Scroll to the end of the article."
7704   (interactive)
7705   (gnus-set-global-variables)
7706   (gnus-summary-select-article)
7707   (gnus-eval-in-buffer-window 
7708    gnus-article-buffer
7709    (widen)
7710    (goto-char (point-max))
7711    (and gnus-break-pages (gnus-narrow-to-page))))
7712
7713 (defun gnus-summary-show-article ()
7714   "Force re-fetching of the current article."
7715   (interactive)
7716   (gnus-set-global-variables)
7717   (gnus-summary-select-article gnus-have-all-headers t))
7718
7719 (defun gnus-summary-toggle-header (arg)
7720   "Show the headers if they are hidden, or hide them if they are shown.
7721 If ARG is a positive number, show the entire header.
7722 If ARG is a negative number, hide the unwanted header lines."
7723   (interactive "P")
7724   (gnus-set-global-variables)
7725   (save-excursion
7726     (set-buffer gnus-article-buffer)
7727     (let ((buffer-read-only nil))
7728       (if (numberp arg) 
7729           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
7730                                                 gnus-hidden-properties)
7731             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
7732         (if (text-property-any (point-min) (point-max) 'invisible t)
7733             (remove-text-properties (point-min) (point-max)
7734                                     gnus-hidden-properties)
7735           (let ((gnus-have-all-headers nil))
7736             (run-hooks 'gnus-article-display-hook))))
7737       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
7738
7739 (defun gnus-summary-show-all-headers ()
7740   "Make all header lines visible."
7741   (interactive)
7742   (gnus-set-global-variables)
7743   (gnus-article-show-all-headers))
7744
7745 (defun gnus-summary-toggle-mime (arg)
7746   "Toggle MIME processing.
7747 If ARG is a positive number, turn MIME processing on."
7748   (interactive "P")
7749   (gnus-set-global-variables)
7750   (setq gnus-show-mime
7751         (if (null arg) (not gnus-show-mime)
7752           (> (prefix-numeric-value arg) 0)))
7753   (gnus-summary-select-article t 'force))
7754
7755 (defun gnus-summary-caesar-message (rotnum)
7756   "Caesar rotates all letters of current message by 13/47 places.
7757 With prefix arg, specifies the number of places to rotate each letter forward.
7758 Caesar rotates Japanese letters by 47 places in any case."
7759   (interactive "P")
7760   (gnus-set-global-variables)
7761   (gnus-summary-select-article)
7762   (let ((mail-header-separator "")) ; !!! Is this necessary?
7763     (gnus-overload-functions)
7764     (gnus-eval-in-buffer-window 
7765      gnus-article-buffer
7766      (save-restriction
7767        (widen)
7768        ;; We don't want to jump to the beginning of the message.
7769        ;; `save-excursion' does not do its job.
7770        (move-to-window-line 0)
7771        (let ((last (point)))
7772          (news-caesar-buffer-body rotnum)
7773          (goto-char last)
7774          (recenter 0))))))
7775
7776 (defun gnus-summary-stop-page-breaking ()
7777   "Stop page breaking in the current article."
7778   (interactive)
7779   (gnus-set-global-variables)
7780   (gnus-summary-select-article)
7781   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
7782
7783 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
7784
7785 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
7786   "Move the current article to a different newsgroup.
7787 If N is a positive number, move the N next articles.
7788 If N is a negative number, move the N previous articles.
7789 If N is nil and any articles have been marked with the process mark,
7790 move those articles instead.
7791 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7792 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7793 re-spool using this method.
7794 For this function to work, both the current newsgroup and the
7795 newsgroup that you want to move to have to support the `request-move'
7796 and `request-accept' functions. (Ie. mail newsgroups at present.)"
7797   (interactive "P")
7798   (gnus-set-global-variables)
7799   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
7800       (error "The current newsgroup does not support article moving"))
7801   (let ((articles (gnus-summary-work-articles n))
7802         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7803         art-group)
7804     (if (and (not to-newsgroup) (not select-method))
7805         (setq to-newsgroup
7806               (completing-read 
7807                (format "Where do you want to move %s? %s"
7808                        (if (> (length articles) 1)
7809                            (format "these %d articles" (length articles))
7810                          "this article")
7811                        (if gnus-current-move-group
7812                            (format "(%s default) " gnus-current-move-group)
7813                          ""))
7814                gnus-active-hashtb nil nil prefix)))
7815     (if to-newsgroup
7816         (progn
7817           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7818               (setq to-newsgroup (or gnus-current-move-group "")))
7819           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7820               (gnus-activate-newsgroup to-newsgroup)
7821               (error "No such group: %s" to-newsgroup))
7822           (setq gnus-current-move-group to-newsgroup)))
7823     (or (gnus-check-backend-function 'request-accept-article 
7824                                      (or select-method to-newsgroup))
7825         (error "%s does not support article moving" to-newsgroup))
7826     (gnus-message 6 "Moving to %s: %s..." 
7827                   (or select-method to-newsgroup) articles)
7828     (while articles
7829       (if (setq art-group
7830                 (gnus-request-move-article 
7831                  (car articles)                   ; Article to move
7832                  gnus-newsgroup-name              ; From newsgrouo
7833                  (nth 1 (gnus-find-method-for-group 
7834                          gnus-newsgroup-name))    ; Server
7835                  (list 'gnus-request-accept-article 
7836                        (if select-method
7837                            (list 'quote select-method)
7838                          to-newsgroup)
7839                        (not (cdr articles)))     ; Accept form
7840                  (not (cdr articles))))          ; Only save nov last time
7841           (let* ((buffer-read-only nil)
7842                  (entry 
7843                   (or
7844                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7845                    (gnus-gethash 
7846                     (gnus-group-prefixed-name 
7847                      (car art-group) 
7848                      (if select-method (list select-method "")
7849                        (gnus-find-method-for-group to-newsgroup)))
7850                     gnus-newsrc-hashtb)))
7851                  (info (nth 2 entry))
7852                  (article (car articles))
7853                  (marked (nth 3 info)))
7854             (gnus-summary-goto-subject article)
7855             (beginning-of-line)
7856             (delete-region (point)
7857                            (progn (forward-line 1) (point)))
7858             (if (not (memq article gnus-newsgroup-unreads))
7859                 (setcar (cdr (cdr info))
7860                         (gnus-add-to-range (nth 2 info) 
7861                                            (list (cdr art-group)))))
7862             ;; Copy any marks over to the new group.
7863             (let ((marks '((tick . gnus-newsgroup-marked)
7864                            (dormant . gnus-newsgroup-dormant)
7865                            (expire . gnus-newsgroup-expirable)
7866                            (bookmark . gnus-newsgroup-bookmarks)
7867                         ;   (score . gnus-newsgroup-scored)
7868                            (reply . gnus-newsgroup-replied)))
7869                   (to-article (cdr art-group)))
7870               (while marks
7871                 (if (memq article (symbol-value (cdr (car marks))))
7872                     (gnus-add-marked-articles 
7873                      (car info) (car (car marks)) (list to-article) info))
7874                 (setq marks (cdr marks))))
7875             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7876             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7877             (setq gnus-newsgroup-dormant
7878                   (delq article gnus-newsgroup-dormant)))
7879         (gnus-message 1 "Couldn't move article %s" (car articles)))
7880       (gnus-summary-remove-process-mark (car articles))
7881       (setq articles (cdr articles)))))
7882
7883 (defun gnus-summary-respool-article (n &optional respool-method)
7884   "Respool the current article.
7885 The article will be squeezed through the mail spooling process again,
7886 which means that it will be put in some mail newsgroup or other
7887 depending on `nnmail-split-methods'.
7888 If N is a positive number, respool the N next articles.
7889 If N is a negative number, respool the N previous articles.
7890 If N is nil and any articles have been marked with the process mark,
7891 respool those articles instead.
7892
7893 Respooling can be done both from mail groups and \"real\" newsgroups.
7894 In the former case, the articles in question will be moved from the
7895 current group into whatever groups they are destined to.  In the
7896 latter case, they will be copied into the relevant groups."
7897   (interactive "P")
7898   (gnus-set-global-variables)
7899   (let ((respool-methods (gnus-methods-using 'respool))
7900         (methname 
7901          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
7902     (or respool-method
7903         (setq respool-method
7904               (completing-read
7905                "What method do you want to use when respooling? "
7906                respool-methods nil t methname)))
7907     (or (string= respool-method "")
7908         (if (assoc (symbol-name
7909                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
7910                    respool-methods)
7911             (gnus-summary-move-article n nil (intern respool-method))
7912           (gnus-summary-copy-article n nil (intern respool-method))))))
7913
7914 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
7915 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
7916   "Move the current article to a different newsgroup.
7917 If N is a positive number, move the N next articles.
7918 If N is a negative number, move the N previous articles.
7919 If N is nil and any articles have been marked with the process mark,
7920 move those articles instead.
7921 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7922 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7923 re-spool using this method.
7924 For this function to work, the newsgroup that you want to move to have
7925 to support the `request-move' and `request-accept'
7926 functions. (Ie. mail newsgroups at present.)"
7927   (interactive "P")
7928   (gnus-set-global-variables)
7929   (let ((articles (gnus-summary-work-articles n))
7930         (copy-buf (get-buffer-create "*copy work*"))
7931         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7932         art-group)
7933     (buffer-disable-undo copy-buf)
7934     (if (and (not to-newsgroup) (not select-method))
7935         (setq to-newsgroup
7936               (completing-read 
7937                (format "Where do you want to copy %s? %s"
7938                        (if (> (length articles) 1)
7939                            (format "these %d articles" (length articles))
7940                          "this article")
7941                        (if gnus-current-move-group
7942                            (format "(%s default) " gnus-current-move-group)
7943                          ""))
7944                gnus-active-hashtb nil nil prefix)))
7945     (if to-newsgroup
7946         (progn
7947           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7948               (setq to-newsgroup (or gnus-current-move-group "")))
7949           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7950               (gnus-activate-newsgroup to-newsgroup)
7951               (error "No such group: %s" to-newsgroup))
7952           (setq gnus-current-move-group to-newsgroup)))
7953     (or (gnus-check-backend-function 'request-accept-article 
7954                                      (or select-method to-newsgroup))
7955         (error "%s does not support article copying" to-newsgroup))
7956     (gnus-message 6 "Copying to %s: %s..." 
7957                   (or select-method to-newsgroup) articles)
7958     (while articles
7959       (if (setq art-group
7960                 (save-excursion
7961                   (set-buffer copy-buf)
7962                   (gnus-request-article-this-buffer
7963                    (car articles) gnus-newsgroup-name)
7964                   (gnus-request-accept-article
7965                    (if select-method (quote select-method) to-newsgroup)
7966                    (not (cdr articles)))))
7967           (let* ((entry 
7968                   (or
7969                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7970                    (gnus-gethash 
7971                     (gnus-group-prefixed-name 
7972                      (car art-group) 
7973                      (if select-method (list select-method "")
7974                        (gnus-find-method-for-group to-newsgroup)))
7975                     gnus-newsrc-hashtb)))
7976                  (info (nth 2 entry))
7977                  (article (car articles))
7978                  (marked (nth 3 info)))
7979             (if (not (memq article gnus-newsgroup-unreads))
7980                 (setcar (cdr (cdr info))
7981                         (gnus-add-to-range (nth 2 info) 
7982                                            (list (cdr art-group)))))
7983             ;; Copy any marks over to the new group.
7984             (let ((marks '((tick . gnus-newsgroup-marked)
7985                            (dormant . gnus-newsgroup-dormant)
7986                            (expire . gnus-newsgroup-expirable)
7987                            (bookmark . gnus-newsgroup-bookmarks)
7988                         ;   (score . gnus-newsgroup-scored)
7989                            (reply . gnus-newsgroup-replied)))
7990                   (to-article (cdr art-group)))
7991               (while marks
7992                 (if (memq article (symbol-value (cdr (car marks))))
7993                     (gnus-add-marked-articles 
7994                      (car info) (car (car marks)) (list to-article) info))
7995                 (setq marks (cdr marks)))))
7996         (gnus-message 1 "Couldn't copy article %s" (car articles)))
7997       (gnus-summary-remove-process-mark (car articles))
7998       (setq articles (cdr articles)))
7999     (kill-buffer copy-buf)))
8000
8001 (defun gnus-summary-expire-articles ()
8002   "Expire all articles that are marked as expirable in the current group."
8003   (interactive)
8004   (if (and gnus-newsgroup-expirable
8005            (gnus-check-backend-function 
8006             'request-expire-articles gnus-newsgroup-name))
8007       (let ((expirable gnus-newsgroup-expirable))
8008         ;; The list of articles that weren't expired is returned.
8009         (setq gnus-newsgroup-expirable 
8010               (gnus-request-expire-articles gnus-newsgroup-expirable
8011                                             gnus-newsgroup-name))
8012         ;; We go through the old list of expirable, and mark all
8013         ;; really expired articles as non-existant.
8014         (while expirable
8015           (or (memq (car expirable) gnus-newsgroup-expirable)
8016               (gnus-summary-mark-as-read (car expirable) "%"))
8017           (setq expirable (cdr expirable))))))
8018
8019 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8020 (defun gnus-summary-delete-article (n)
8021   "Delete the N next (mail) articles.
8022 This command actually deletes articles. This is not a marking
8023 command. The article will disappear forever from you life, never to
8024 return. 
8025 If N is negative, delete backwards.
8026 If N is nil and articles have been marked with the process mark,
8027 delete these instead."
8028   (interactive "P")
8029   (or (gnus-check-backend-function 'request-expire-articles 
8030                                    gnus-newsgroup-name)
8031       (error "The current newsgroup does not support article deletion."))
8032   ;; Compute the list of articles to delete.
8033   (let ((articles (gnus-summary-work-articles n)))
8034     (if (and gnus-novice-user
8035              (not (gnus-y-or-n-p 
8036                    (format "Do you really want to delete %s forever? "
8037                            (if (> (length articles) 1) "these articles"
8038                              "this article")))))
8039         ()
8040       ;; Delete the articles.
8041       (setq gnus-newsgroup-expirable 
8042             (gnus-request-expire-articles 
8043              articles gnus-newsgroup-name 'force))
8044       (while articles
8045         (gnus-summary-remove-process-mark (car articles))       
8046         ;; The backend might not have been able to delete the article
8047         ;; after all.  
8048         (or (memq (car articles) gnus-newsgroup-expirable)
8049             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8050         (setq articles (cdr articles)))))
8051   (gnus-summary-position-cursor))
8052
8053 (defun gnus-summary-edit-article ()
8054   "Enter into a buffer and edit the current article.
8055 This will have permanent effect only in mail groups."
8056   (interactive)
8057   (or (gnus-check-backend-function 
8058        'request-replace-article gnus-newsgroup-name)
8059       (error "The current newsgroup does not support article editing."))
8060   (gnus-summary-select-article t)
8061   (other-window 1)
8062   (gnus-message 6 "C-c C-c to end edits")
8063   (setq buffer-read-only nil)
8064   (text-mode)
8065   (use-local-map (copy-keymap (current-local-map)))
8066   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8067   (goto-char (point-min))
8068   (search-forward "\n\n" nil t))
8069
8070 (defun gnus-summary-edit-article-done ()
8071   "Make edits to the current article permanent."
8072   (interactive)
8073   (if (not (gnus-request-replace-article 
8074             (cdr gnus-article-current) (car gnus-article-current) 
8075             (current-buffer)))
8076       (error "Couldn't replace article.")
8077     (gnus-article-mode)
8078     (use-local-map gnus-article-mode-map)
8079     (setq buffer-read-only t)
8080     (pop-to-buffer gnus-summary-buffer)))      
8081
8082 (defun gnus-summary-fancy-query ()
8083   "Query where the fancy respool algorithm would put this article."
8084   (interactive)
8085   (gnus-summary-select-article)
8086   (save-excursion
8087     (set-buffer gnus-article-buffer)
8088     (save-restriction
8089       (goto-char (point-min))
8090       (search-forward "\n\n")
8091       (narrow-to-region (point-min) (point))
8092       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8093
8094 ;; Summary score commands.
8095
8096 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8097
8098 (defun gnus-summary-raise-score (n)
8099   "Raise the score of the current article by N."
8100   (interactive "p")
8101   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8102
8103 (defun gnus-summary-lower-score (n)
8104   "Lower the score of the current article by N."
8105   (interactive "p")
8106   (gnus-summary-raise-score (- n)))
8107
8108 (defun gnus-summary-set-score (n)
8109   "Set the score of the current article to N."
8110   (interactive "p")
8111   ;; Skip dummy header line.
8112   (save-excursion
8113     (gnus-summary-show-thread)
8114     (if (eq (gnus-summary-article-mark) ?Z)
8115         (forward-line 1))
8116     (let ((buffer-read-only nil))
8117       ;; Set score.
8118       (gnus-summary-update-mark
8119        (if (= n (or gnus-summary-default-score 0)) ? 
8120          (if (< n (or gnus-summary-default-score 0)) 
8121              gnus-score-below-mark gnus-score-over-mark)) 'score))
8122     (let* ((article (gnus-summary-article-number))
8123            (score (assq article gnus-newsgroup-scored)))
8124       (if score (setcdr score n)
8125         (setq gnus-newsgroup-scored 
8126               (cons (cons article n) gnus-newsgroup-scored))))
8127     (gnus-summary-update-line)))
8128
8129 (defun gnus-summary-current-score ()
8130   "Return the score of the current article."
8131   (interactive)
8132   (message "%s" (gnus-summary-article-score)))
8133
8134 ;; Summary marking commands.
8135
8136 (defun gnus-summary-raise-same-subject-and-select (score)
8137   "Raise articles which has the same subject with SCORE and select the next."
8138   (interactive "p")
8139   (let ((subject (gnus-summary-subject-string)))
8140     (gnus-summary-raise-score score)
8141     (while (gnus-summary-search-subject nil nil subject)
8142       (gnus-summary-raise-score score))
8143     (gnus-summary-next-article t)))
8144
8145 (defun gnus-summary-raise-same-subject (score)
8146   "Raise articles which has the same subject with SCORE."
8147   (interactive "p")
8148   (let ((subject (gnus-summary-subject-string)))
8149     (gnus-summary-raise-score score)
8150     (while (gnus-summary-search-subject nil nil subject)
8151       (gnus-summary-raise-score score))
8152     (gnus-summary-next-subject 1 t)))
8153
8154 (defun gnus-score-default (level)
8155   (if level (prefix-numeric-value level) 
8156     gnus-score-interactive-default-score))
8157
8158 (defun gnus-summary-raise-thread (score)
8159   "Raise articles under current thread with SCORE."
8160   (interactive "P")
8161   (setq score (1- (gnus-score-default score)))
8162   (let (e)
8163     (save-excursion
8164       (let ((level (gnus-summary-thread-level)))
8165         (gnus-summary-raise-score score)
8166         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8167                     (> (gnus-summary-thread-level) level))
8168           (gnus-summary-raise-score score))
8169         (setq e (point))))
8170     (let ((gnus-summary-check-current t))
8171       (or (zerop (gnus-summary-next-subject 1 t))
8172           (goto-char e))))
8173   (gnus-summary-recenter)
8174   (gnus-summary-position-cursor)
8175   (gnus-set-mode-line 'summary))
8176
8177 (defun gnus-summary-lower-same-subject-and-select (score)
8178   "Raise articles which has the same subject with SCORE and select the next."
8179   (interactive "p")
8180   (gnus-summary-raise-same-subject-and-select (- score)))
8181
8182 (defun gnus-summary-lower-same-subject (score)
8183   "Raise articles which has the same subject with SCORE."
8184   (interactive "p")
8185   (gnus-summary-raise-same-subject (- score)))
8186
8187 (defun gnus-summary-lower-thread (score)
8188   "Raise articles under current thread with SCORE."
8189   (interactive "P")
8190   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8191
8192 (defun gnus-summary-kill-same-subject-and-select (unmark)
8193   "Mark articles which has the same subject as read, and then select the next.
8194 If UNMARK is positive, remove any kind of mark.
8195 If UNMARK is negative, tick articles."
8196   (interactive "P")
8197   (if unmark
8198       (setq unmark (prefix-numeric-value unmark)))
8199   (let ((count
8200          (gnus-summary-mark-same-subject
8201           (gnus-summary-subject-string) unmark)))
8202     ;; Select next unread article. If auto-select-same mode, should
8203     ;; select the first unread article.
8204     (gnus-summary-next-article t (and gnus-auto-select-same
8205                                       (gnus-summary-subject-string)))
8206     (gnus-message 7 "%d articles are marked as %s"
8207                   count (if unmark "unread" "read"))))
8208
8209 (defun gnus-summary-kill-same-subject (unmark)
8210   "Mark articles which has the same subject as read. 
8211 If UNMARK is positive, remove any kind of mark.
8212 If UNMARK is negative, tick articles."
8213   (interactive "P")
8214   (if unmark
8215       (setq unmark (prefix-numeric-value unmark)))
8216   (let ((count
8217          (gnus-summary-mark-same-subject
8218           (gnus-summary-subject-string) unmark)))
8219     ;; If marked as read, go to next unread subject.
8220     (if (null unmark)
8221         ;; Go to next unread subject.
8222         (gnus-summary-next-subject 1 t))
8223     (gnus-message 7 "%d articles are marked as %s"
8224                   count (if unmark "unread" "read"))))
8225
8226 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8227   "Mark articles with same SUBJECT as read, and return marked number.
8228 If optional argument UNMARK is positive, remove any kinds of marks.
8229 If optional argument UNMARK is negative, mark articles as unread instead."
8230   (let ((count 1))
8231     (save-excursion
8232       (cond ((null unmark)
8233              (gnus-summary-mark-as-read nil gnus-killed-mark))
8234             ((> unmark 0)
8235              (gnus-summary-tick-article nil t))
8236             (t
8237              (gnus-summary-tick-article)))
8238       (while (and subject
8239                   (gnus-summary-search-forward nil subject))
8240         (cond ((null unmark)
8241                (gnus-summary-mark-as-read nil gnus-killed-mark))
8242               ((> unmark 0)
8243                (gnus-summary-tick-article nil t))
8244               (t
8245                (gnus-summary-tick-article)))
8246         (setq count (1+ count))))
8247     ;; Hide killed thread subtrees.  Does not work properly always.
8248     ;;(and (null unmark)
8249     ;;     gnus-thread-hide-killed
8250     ;;     (gnus-summary-hide-thread))
8251     ;; Return number of articles marked as read.
8252     count))
8253
8254 (defun gnus-summary-mark-as-processable (n &optional unmark)
8255   "Set the process mark on the next N articles.
8256 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8257 the process mark instead.  The difference between N and the actual
8258 number of articles marked is returned."
8259   (interactive "p")
8260   (let ((backward (< n 0))
8261         (n (abs n)))
8262   (while (and 
8263           (> n 0)
8264           (if unmark
8265               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8266             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8267           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8268     (setq n (1- n)))
8269   (if (/= 0 n) (gnus-message 7 "No more articles"))
8270   (gnus-summary-recenter)
8271   (gnus-summary-position-cursor)
8272   n))
8273
8274 (defun gnus-summary-unmark-as-processable (n)
8275   "Remove the process mark from the next N articles.
8276 If N is negative, mark backward instead.  The difference between N and
8277 the actual number of articles marked is returned."
8278   (interactive "p")
8279   (gnus-summary-mark-as-processable n t))
8280
8281 (defun gnus-summary-unmark-all-processable ()
8282   "Remove the process mark from all articles."
8283   (interactive)
8284   (save-excursion
8285     (while gnus-newsgroup-processable
8286       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8287   (gnus-summary-position-cursor))
8288
8289 (defun gnus-summary-mark-as-expirable (n)
8290   "Mark N articles forward as expirable.
8291 If N is negative, mark backward instead. The difference between N and
8292 the actual number of articles marked is returned."
8293   (interactive "p")
8294   (gnus-summary-mark-forward n gnus-expirable-mark))
8295
8296 (defun gnus-summary-mark-article-as-replied (article)
8297   "Mark ARTICLE replied and update the summary line."
8298   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8299   (let ((buffer-read-only nil))
8300     (if (gnus-summary-goto-subject article)
8301         (progn
8302           (gnus-summary-update-mark gnus-replied-mark 'replied)
8303           t))))
8304
8305 (defun gnus-summary-set-bookmark (article)
8306   "Set a bookmark in current article."
8307   (interactive (list (gnus-summary-article-number)))
8308   (if (or (not (get-buffer gnus-article-buffer))
8309           (not gnus-current-article)
8310           (not gnus-article-current)
8311           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8312       (error "No current article selected"))
8313   ;; Remove old bookmark, if one exists.
8314   (let ((old (assq article gnus-newsgroup-bookmarks)))
8315     (if old (setq gnus-newsgroup-bookmarks 
8316                   (delq old gnus-newsgroup-bookmarks))))
8317   ;; Set the new bookmark, which is on the form 
8318   ;; (article-number . line-number-in-body).
8319   (setq gnus-newsgroup-bookmarks 
8320         (cons 
8321          (cons article 
8322                (save-excursion
8323                  (set-buffer gnus-article-buffer)
8324                  (count-lines
8325                   (min (point)
8326                        (save-excursion
8327                          (goto-char (point-min))
8328                          (search-forward "\n\n" nil t)
8329                          (point)))
8330                   (point))))
8331          gnus-newsgroup-bookmarks))
8332   (gnus-message 6 "A bookmark has been added to the current article."))
8333
8334 (defun gnus-summary-remove-bookmark (article)
8335   "Remove the bookmark from the current article."
8336   (interactive (list (gnus-summary-article-number)))
8337   ;; Remove old bookmark, if one exists.
8338   (let ((old (assq article gnus-newsgroup-bookmarks)))
8339     (if old 
8340         (progn
8341           (setq gnus-newsgroup-bookmarks 
8342                 (delq old gnus-newsgroup-bookmarks))
8343           (gnus-message 6 "Removed bookmark."))
8344       (gnus-message 6 "No bookmark in current article."))))
8345
8346 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8347 (defun gnus-summary-mark-as-dormant (n)
8348   "Mark N articles forward as dormant.
8349 If N is negative, mark backward instead.  The difference between N and
8350 the actual number of articles marked is returned."
8351   (interactive "p")
8352   (gnus-summary-mark-forward n gnus-dormant-mark))
8353
8354 (defun gnus-summary-set-process-mark (article)
8355   "Set the process mark on ARTICLE and update the summary line."
8356   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8357   (let ((buffer-read-only nil))
8358     (if (gnus-summary-goto-subject article)
8359         (progn
8360           (gnus-summary-show-thread)
8361           (and (eq (gnus-summary-article-mark) ?Z)
8362                (forward-line 1))
8363           (gnus-summary-update-mark gnus-process-mark 'replied)
8364           t))))
8365
8366 (defun gnus-summary-remove-process-mark (article)
8367   "Remove the process mark from ARTICLE and update the summary line."
8368   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8369   (let ((buffer-read-only nil))
8370     (if (gnus-summary-goto-subject article)
8371         (progn
8372           (gnus-summary-show-thread)
8373           (and (eq (gnus-summary-article-mark) ?Z)
8374                (forward-line 1))
8375           (gnus-summary-update-mark ?  'replied)
8376           (if (memq article gnus-newsgroup-replied) 
8377               (gnus-summary-update-mark gnus-replied-mark 'replied))
8378           t))))
8379
8380 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8381   "Mark N articles as read forwards.
8382 If N is negative, mark backwards instead.
8383 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8384 marked as unread. 
8385 The difference between N and the actual number of articles marked is
8386 returned."
8387   (interactive "p")
8388   (gnus-set-global-variables)
8389   (let ((backward (< n 0))
8390         (n (abs n))
8391         (mark (or mark gnus-del-mark)))
8392   (while (and (> n 0)
8393               (gnus-summary-mark-article nil mark no-expire)
8394               (zerop (gnus-summary-next-subject 
8395                       (if backward -1 1) gnus-summary-goto-unread t)))
8396     (setq n (1- n)))
8397   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8398   (gnus-summary-recenter)
8399   (gnus-summary-position-cursor)
8400   (gnus-set-mode-line 'summary)
8401   n))
8402
8403 (defun gnus-summary-mark-article (&optional article mark no-expire)
8404   "Mark ARTICLE with MARK.
8405 MARK can be any character.
8406 Five MARK strings are reserved: ?  (unread), 
8407 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8408 If MARK is nil, then the default character ?D is used.
8409 If ARTICLE is nil, then the article on the current line will be
8410 marked." 
8411   ;; If no mark is given, then we check auto-expiring.
8412   (and (not no-expire)
8413        gnus-newsgroup-auto-expire 
8414        (or (not mark)
8415            (and (numberp mark) (or (= mark gnus-killed-mark)
8416                                    (= mark gnus-del-mark)
8417                                    (= mark gnus-catchup-mark)
8418                                    (= mark gnus-low-score-mark)
8419                                    (= mark gnus-read-mark))))
8420        (setq mark gnus-expirable-mark))
8421   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8422          (article (or article (gnus-summary-article-number))))
8423     (if (or (= mark gnus-unread-mark) 
8424             (= mark gnus-ticked-mark) 
8425             (= mark gnus-dormant-mark))
8426         (gnus-mark-article-as-unread article mark)
8427       (gnus-mark-article-as-read article mark))
8428
8429     ;; See whether the article is to be put in the cache.
8430     (and gnus-use-cache
8431          (save-excursion
8432            (gnus-cache-possibly-enter-article 
8433             gnus-newsgroup-name article 
8434             (gnus-get-header-by-number article)
8435             (= mark gnus-ticked-mark)
8436             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8437
8438     (if (gnus-summary-goto-subject article)
8439         (let ((buffer-read-only nil))
8440           (gnus-summary-show-thread)
8441           (and (eq (gnus-summary-article-mark) ?Z)
8442                (forward-line 1))
8443           ;; Fix the mark.
8444           (gnus-summary-update-mark mark 'unread)
8445           t))))
8446
8447 (defun gnus-summary-update-mark (mark type)
8448   (beginning-of-line)
8449   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8450         plist)
8451     (if (not forward)
8452         ()
8453       (forward-char forward)
8454       (setq plist (text-properties-at (point)))
8455       (delete-char 1)
8456       (and (memq 'gnus plist) 
8457            (setcar (cdr (car (cdr (memq 'gnus plist)))) mark))
8458       (insert mark)
8459       (and plist (add-text-properties (1- (point)) (point) plist))
8460       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8461   
8462 (defun gnus-mark-article-as-read (article &optional mark)
8463   "Enter ARTICLE in the pertinent lists and remove it from others."
8464   ;; Make the article expirable.
8465   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8466     (if (= mark gnus-expirable-mark)
8467         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8468       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8469     ;; Remove from unread and marked lists.
8470     (setq gnus-newsgroup-unreads
8471           (delq article gnus-newsgroup-unreads))
8472     (setq gnus-newsgroup-marked
8473           (delq article gnus-newsgroup-marked))
8474     (setq gnus-newsgroup-dormant
8475           (delq article gnus-newsgroup-dormant))))
8476
8477 (defun gnus-mark-article-as-unread (article &optional mark)
8478   "Enter ARTICLE in the pertinent lists and remove it from others."
8479   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8480     ;; Add to unread list.
8481     (or (memq article gnus-newsgroup-unreads)
8482         (setq gnus-newsgroup-unreads
8483               (cons article gnus-newsgroup-unreads)))
8484     ;; If CLEAR-MARK is non-nil, the article must be removed from marked
8485     ;; list.  Otherwise, it must be added to the list.
8486     (setq gnus-newsgroup-marked
8487           (delq article gnus-newsgroup-marked))
8488     (setq gnus-newsgroup-dormant
8489           (delq article gnus-newsgroup-dormant))
8490     (setq gnus-newsgroup-expirable 
8491           (delq article gnus-newsgroup-expirable))
8492     (if (= mark gnus-ticked-mark)
8493         (setq gnus-newsgroup-marked 
8494               (cons article gnus-newsgroup-marked)))
8495     (if (= mark gnus-dormant-mark)
8496         (setq gnus-newsgroup-dormant 
8497               (cons article gnus-newsgroup-dormant)))))
8498
8499 (defalias 'gnus-summary-mark-as-unread-forward 
8500   'gnus-summary-tick-article-forward)
8501 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8502                'gnus-summary-tick-article-forward)
8503 (defun gnus-summary-tick-article-forward (n)
8504   "Tick N articles forwards.
8505 If N is negative, tick backwards instead.
8506 The difference between N and the number of articles ticked is returned."
8507   (interactive "p")
8508   (gnus-summary-mark-forward n gnus-ticked-mark))
8509
8510 (defalias 'gnus-summary-mark-as-unread-backward 
8511   'gnus-summary-tick-article-backward)
8512 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8513                'gnus-summary-tick-article-backward)
8514 (defun gnus-summary-tick-article-backward (n)
8515   "Tick N articles backwards.
8516 The difference between N and the number of articles ticked is returned."
8517   (interactive "p")
8518   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8519
8520 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8521 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8522 (defun gnus-summary-tick-article (&optional article clear-mark)
8523   "Mark current article as unread.
8524 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8525 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8526   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8527                                        gnus-ticked-mark)))
8528
8529 (defun gnus-summary-mark-as-read-forward (n)
8530   "Mark N articles as read forwards.
8531 If N is negative, mark backwards instead.
8532 The difference between N and the actual number of articles marked is
8533 returned."
8534   (interactive "p")
8535   (gnus-summary-mark-forward n gnus-del-mark t))
8536
8537 (defun gnus-summary-mark-as-read-backward (n)
8538   "Mark the N articles as read backwards.
8539 The difference between N and the actual number of articles marked is
8540 returned."
8541   (interactive "p")
8542   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8543
8544 (defun gnus-summary-mark-as-read (&optional article mark)
8545   "Mark current article as read.
8546 ARTICLE specifies the article to be marked as read.
8547 MARK specifies a string to be inserted at the beginning of the line."
8548   (gnus-summary-mark-article article mark))
8549
8550 (defun gnus-summary-clear-mark-forward (n)
8551   "Clear marks from N articles forward.
8552 If N is negative, clear backward instead.
8553 The difference between N and the number of marks cleared is returned."
8554   (interactive "p")
8555   (gnus-summary-mark-forward n gnus-unread-mark))
8556
8557 (defun gnus-summary-clear-mark-backward (n)
8558   "Clear marks from N articles backward.
8559 The difference between N and the number of marks cleared is returned."
8560   (interactive "p")
8561   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8562
8563 (defun gnus-summary-mark-unread-as-read ()
8564   "Intended to be used by `gnus-summary-mark-article-hook'."
8565   (or (memq gnus-current-article gnus-newsgroup-marked)
8566       (memq gnus-current-article gnus-newsgroup-dormant)
8567       (memq gnus-current-article gnus-newsgroup-expirable)
8568       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8569
8570 (defun gnus-summary-mark-region-as-read (point mark all)
8571   "Mark all unread articles between point and mark as read.
8572 If given a prefix, mark all articles between point and mark as read,
8573 even ticked and dormant ones."
8574   (interactive "r\nP")
8575   (save-excursion
8576     (goto-char point)
8577     (beginning-of-line)
8578     (while (and 
8579             (< (point) mark)
8580             (progn
8581               (and
8582                (or all
8583                    (and
8584                     (not (memq (gnus-summary-article-number)
8585                                gnus-newsgroup-marked))
8586                     (not (memq (gnus-summary-article-number)
8587                                gnus-newsgroup-dormant))))
8588                (gnus-summary-mark-article
8589                 (gnus-summary-article-number) gnus-del-mark))
8590               t)
8591             (zerop (forward-line 1))))))
8592
8593 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8594 (defalias 'gnus-summary-delete-marked-as-read 
8595   'gnus-summary-remove-lines-marked-as-read)
8596 (make-obsolete 'gnus-summary-delete-marked-as-read 
8597                'gnus-summary-remove-lines-marked-as-read)
8598 (defun gnus-summary-remove-lines-marked-as-read ()
8599   "Remove lines that are marked as read."
8600   (interactive)
8601   (gnus-summary-remove-lines-marked-with 
8602    (concat (mapconcat
8603             (lambda (char) (char-to-string (symbol-value char)))
8604             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
8605               gnus-killed-mark gnus-kill-file-mark
8606               gnus-low-score-mark gnus-expirable-mark)
8607             ""))))
8608
8609 (defalias 'gnus-summary-delete-marked-with 
8610   'gnus-summary-remove-lines-marked-with)
8611 (make-obsolete 'gnus-summary-delete-marked-with 
8612                'gnus-summary-remove-lines-marked-with)
8613 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8614 (defun gnus-summary-remove-lines-marked-with (marks)
8615   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8616   (interactive "sMarks: ")
8617   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8618   (save-excursion
8619     (set-buffer gnus-summary-buffer)
8620     (let ((buffer-read-only nil)
8621           (marks (concat "^[" marks "]")))
8622       (goto-char (point-min))
8623       (if gnus-newsgroup-adaptive
8624           (gnus-score-remove-lines-adaptive marks)
8625         (while (re-search-forward marks (point-max) t)
8626           (beginning-of-line)
8627           (delete-region (point)
8628                          (progn (forward-line 1) (point))))))
8629     (or (zerop (buffer-size))
8630         (if (eobp)
8631             (gnus-summary-prev-subject 1)
8632           (gnus-summary-position-cursor)))))
8633
8634 (defun gnus-summary-expunge-below (score)
8635   "Remove articles with score less than SCORE."
8636   (interactive "P")
8637   (setq score (if score
8638                   (prefix-numeric-value score)
8639                 (or gnus-summary-default-score 0)))
8640   (save-excursion
8641     (set-buffer gnus-summary-buffer)
8642     (goto-char (point-min))
8643     (let ((buffer-read-only nil)
8644           beg)
8645       (while (not (eobp))
8646         (if (< (gnus-summary-article-score) score)
8647             (progn
8648               (setq beg (point))
8649               (forward-line 1)
8650               (delete-region beg (point)))
8651           (forward-line 1)))
8652       ;; Adjust point.
8653       (or (zerop (buffer-size))
8654           (if (eobp)
8655               (gnus-summary-prev-subject 1)
8656             (gnus-summary-position-cursor))))))
8657
8658 (defun gnus-summary-mark-below (score mark)
8659   "Mark articles with score less than SCORE with MARK."
8660   (interactive "P\ncMark: ")
8661   (setq score (if score
8662                   (prefix-numeric-value score)
8663                 (or gnus-summary-default-score 0)))
8664   (save-excursion
8665     (set-buffer gnus-summary-buffer)
8666     (goto-char (point-min))
8667     (while (not (eobp))
8668       (and (< (gnus-summary-article-score) score)
8669            (gnus-summary-mark-article nil mark))
8670       (forward-line 1))))
8671
8672 (defun gnus-summary-kill-below (score)
8673   "Mark articles with score below SCORE as read."
8674   (interactive "P")
8675   (gnus-summary-mark-below score gnus-killed-mark))
8676
8677 (defun gnus-summary-clear-above (score)
8678   "Clear all marks from articles with score above SCORE."
8679   (interactive "P")
8680   (gnus-summary-mark-above score gnus-unread-mark))
8681
8682 (defun gnus-summary-tick-above (score)
8683   "Tick all articles with score above SCORE."
8684   (interactive "P")
8685   (gnus-summary-mark-above score gnus-ticked-mark))
8686
8687 (defun gnus-summary-mark-above (score mark)
8688   "Mark articles with score over SCORE with MARK."
8689   (interactive "P\ncMark: ")
8690   (setq score (if score
8691                   (prefix-numeric-value score)
8692                 (or gnus-summary-default-score 0)))
8693   (save-excursion
8694     (set-buffer gnus-summary-buffer)
8695     (goto-char (point-min))
8696     (while (not (eobp))
8697       (if (> (gnus-summary-article-score) score)
8698           (progn
8699             (gnus-summary-mark-article nil mark)
8700             (forward-line 1))
8701         (forward-line 1)))))
8702
8703 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
8704 (defun gnus-summary-show-all-expunged ()
8705   "Display all the hidden articles that were expunged for low scores."
8706   (interactive)
8707   (let ((buffer-read-only nil))
8708     (let ((scored gnus-newsgroup-scored)
8709           headers h)
8710       (while scored
8711         (or (gnus-summary-goto-subject (car (car scored)))
8712             (and (setq h (gnus-get-header-by-number (car (car scored))))
8713                  (< (cdr (car scored)) gnus-summary-expunge-below)
8714                  (setq headers (cons h headers))))
8715         (setq scored (cdr scored)))
8716       (or headers (error "No expunged articles hidden."))
8717       (goto-char (point-min))
8718       (save-excursion 
8719         (gnus-summary-update-lines 
8720          (point)
8721          (progn
8722            (gnus-summary-prepare-threads (nreverse headers) 0)
8723            (point)))))
8724     (goto-char (point-min))
8725     (gnus-summary-position-cursor)))
8726
8727 (defun gnus-summary-show-all-dormant ()
8728   "Display all the hidden articles that are marked as dormant."
8729   (interactive)
8730   (let ((buffer-read-only nil))
8731     (goto-char (point-min))
8732     (let ((dormant gnus-newsgroup-dormant)
8733           headers h)
8734       (while dormant
8735         (or (gnus-summary-goto-subject (car dormant))
8736             (and (setq h (gnus-get-header-by-number (car dormant)))
8737                  (setq headers (cons h headers))))
8738         (setq dormant (cdr dormant)))
8739       (or headers (error "No dormant articles hidden."))
8740       (save-excursion 
8741         (gnus-summary-update-lines 
8742          (point)
8743          (progn
8744            (gnus-summary-prepare-threads (nreverse headers) 0)
8745            (point)))))
8746     (goto-char (point-min))
8747     (gnus-summary-position-cursor)))
8748
8749 (defun gnus-summary-hide-all-dormant ()
8750   "Hide all dormant articles."
8751   (interactive)
8752   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
8753   (gnus-summary-position-cursor))
8754
8755 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
8756   "Mark all articles not marked as unread in this newsgroup as read.
8757 If prefix argument ALL is non-nil, all articles are marked as read.
8758 If QUIETLY is non-nil, no questions will be asked.
8759 If TO-HERE is non-nil, it should be a point in the buffer. All
8760 articles before this point will be marked as read.
8761 The number of articles marked as read is returned."
8762   (interactive "P")
8763   (prog1
8764       (if (or quietly
8765               (not gnus-interactive-catchup) ;Without confirmation?
8766               gnus-expert-user
8767               (gnus-y-or-n-p
8768                (if all
8769                    "Mark absolutely all articles as read? "
8770                  "Mark all unread articles as read? ")))
8771           (if (and not-mark 
8772                    (not gnus-newsgroup-adaptive)
8773                    (not gnus-newsgroup-auto-expire))
8774               (progn
8775                 (and all (setq gnus-newsgroup-marked nil
8776                                gnus-newsgroup-dormant nil))
8777                 (setq gnus-newsgroup-unreads gnus-newsgroup-marked))
8778             (let ((unreads (length gnus-newsgroup-unreads)))
8779               (if (gnus-summary-first-subject (not all))
8780                   (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
8781                               (if to-here (< (point) to-here) t)
8782                               (gnus-summary-search-subject nil (not all)))))
8783               (- unreads (length gnus-newsgroup-unreads))
8784               (or to-here
8785                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
8786     (gnus-summary-position-cursor)))
8787
8788 (defun gnus-summary-catchup-to-here (&optional all)
8789   "Mark all unticked articles before the current one as read.
8790 If ALL is non-nil, also mark ticked and dormant articles as read."
8791   (interactive)
8792   (beginning-of-line)
8793   (gnus-summary-catchup all t (point))
8794   (gnus-summary-position-cursor))
8795
8796 (defun gnus-summary-catchup-all (&optional quietly)
8797   "Mark all articles in this newsgroup as read."
8798   (interactive)
8799   (gnus-summary-catchup t quietly))
8800
8801 (defun gnus-summary-catchup-and-exit (all &optional quietly)
8802   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8803 If prefix argument ALL is non-nil, all articles are marked as read."
8804   (interactive "P")
8805   (gnus-summary-catchup all quietly nil 'fast)
8806   ;; Select next newsgroup or exit.
8807   (if (eq gnus-auto-select-next 'quietly)
8808       (gnus-summary-next-group nil)
8809     (gnus-summary-exit)))
8810
8811 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8812   "Mark all articles in this newsgroup as read, and then exit."
8813   (interactive)
8814   (gnus-summary-catchup-and-exit t quietly))
8815
8816 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8817 (defun gnus-summary-catchup-and-goto-next-group (all)
8818   "Mark all articles in this group as read and select the next group.
8819 If given a prefix, mark all articles, unread as well as ticked, as
8820 read." 
8821   (interactive "P")
8822   (gnus-summary-catchup all)
8823   (gnus-summary-next-group))
8824
8825 ;; Thread-based commands.
8826
8827 (defun gnus-summary-toggle-threads (arg)
8828   "Toggle showing conversation threads.
8829 If ARG is positive number, turn showing conversation threads on."
8830   (interactive "P")
8831   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8832     (setq gnus-show-threads
8833           (if (null arg) (not gnus-show-threads)
8834             (> (prefix-numeric-value arg) 0)))
8835     (gnus-summary-prepare)
8836     (gnus-summary-goto-subject current)
8837     (gnus-summary-position-cursor)))
8838
8839 (defun gnus-summary-show-all-threads ()
8840   "Show all threads."
8841   (interactive)
8842   (save-excursion
8843     (let ((buffer-read-only nil))
8844       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8845   (gnus-summary-position-cursor))
8846
8847 (defun gnus-summary-show-thread ()
8848   "Show thread subtrees.
8849 Returns nil if no thread was there to be shown."
8850   (interactive)
8851   (prog1
8852       (save-excursion
8853         (let ((buffer-read-only nil)
8854               ;; first goto end then to beg, to have point at beg after let
8855               (end (progn (end-of-line) (point)))
8856               (beg (progn (beginning-of-line) (point))))
8857           (prog1
8858               ;; Any hidden lines here?
8859               (search-forward "\r" end t)
8860             (subst-char-in-region beg end ?\^M ?\n t))))
8861     (gnus-summary-position-cursor)))
8862
8863 (defun gnus-summary-hide-all-threads ()
8864   "Hide all thread subtrees."
8865   (interactive)
8866   (save-excursion
8867     (goto-char (point-min))
8868     (gnus-summary-hide-thread)
8869     (while (and (not (eobp)) (zerop (forward-line 1)))
8870       (gnus-summary-hide-thread)))
8871   (gnus-summary-position-cursor))
8872
8873 (defun gnus-summary-hide-thread ()
8874   "Hide thread subtrees.
8875 Returns nil if no threads were there to be hidden."
8876   (interactive)
8877   (let ((buffer-read-only nil)
8878         (start (point))
8879         (level (gnus-summary-thread-level))
8880         (end (point)))
8881     ;; Go forward until either the buffer ends or the subthread
8882     ;; ends. 
8883     (if (eobp)
8884         ()
8885       (while (and (zerop (forward-line 1))
8886                   (> (gnus-summary-thread-level) level))
8887         (setq end (point)))
8888       (prog1
8889           (save-excursion
8890             (goto-char end)
8891             (search-backward "\n" start t))
8892         (subst-char-in-region start end ?\n ?\^M t)
8893         (forward-line -1)))))
8894
8895 (defun gnus-summary-go-to-next-thread (&optional previous)
8896   "Go to the same level (or less) next thread.
8897 If PREVIOUS is non-nil, go to previous thread instead.
8898 Return the article number moved to, or nil if moving was impossible."
8899   (let ((level (gnus-summary-thread-level))
8900         (article (gnus-summary-article-number)))
8901     (if previous 
8902         (while (and (zerop (gnus-summary-prev-subject 1))
8903                     (> (gnus-summary-thread-level) level)))
8904       (while (and (zerop (gnus-summary-next-subject 1))
8905                   (> (gnus-summary-thread-level) level))))
8906     (gnus-summary-recenter)
8907     (gnus-summary-position-cursor)
8908     (let ((oart (gnus-summary-article-number)))
8909       (and (/= oart article) oart))))
8910
8911 (defun gnus-summary-next-thread (n)
8912   "Go to the same level next N'th thread.
8913 If N is negative, search backward instead.
8914 Returns the difference between N and the number of skips actually
8915 done."
8916   (interactive "p")
8917   (let ((backward (< n 0))
8918         (n (abs n)))
8919   (while (and (> n 0)
8920               (gnus-summary-go-to-next-thread backward))
8921     (setq n (1- n)))
8922   (gnus-summary-position-cursor)
8923   (if (/= 0 n) (gnus-message 7 "No more threads"))
8924   n))
8925
8926 (defun gnus-summary-prev-thread (n)
8927   "Go to the same level previous N'th thread.
8928 Returns the difference between N and the number of skips actually
8929 done."
8930   (interactive "p")
8931   (gnus-summary-next-thread (- n)))
8932
8933 (defun gnus-summary-go-down-thread (&optional same)
8934   "Go down one level in the current thread.
8935 If SAME is non-nil, also move to articles of the same level."
8936   (let ((level (gnus-summary-thread-level))
8937         (start (point)))
8938     (if (and (zerop (forward-line 1))
8939              (> (gnus-summary-thread-level) level))
8940         t
8941       (goto-char start)
8942       nil)))
8943
8944 (defun gnus-summary-go-up-thread ()
8945   "Go up one level in the current thread."
8946   (let ((level (gnus-summary-thread-level))
8947         (start (point)))
8948     (while (and (zerop (forward-line -1))
8949                 (>= (gnus-summary-thread-level) level)))
8950     (if (>= (gnus-summary-thread-level) level)
8951         (progn
8952           (goto-char start)
8953           nil)
8954       t)))
8955
8956 (defun gnus-summary-down-thread (n)
8957   "Go down thread N steps.
8958 If N is negative, go up instead.
8959 Returns the difference between N and how many steps down that were
8960 taken."
8961   (interactive "p")
8962   (let ((up (< n 0))
8963         (n (abs n)))
8964   (while (and (> n 0)
8965               (if up (gnus-summary-go-up-thread)
8966                 (gnus-summary-go-down-thread)))
8967     (setq n (1- n)))
8968   (gnus-summary-position-cursor)
8969   (if (/= 0 n) (gnus-message 7 "Can't go further"))
8970   n))
8971
8972 (defun gnus-summary-up-thread (n)
8973   "Go up thread N steps.
8974 If N is negative, go up instead.
8975 Returns the difference between N and how many steps down that were
8976 taken."
8977   (interactive "p")
8978   (gnus-summary-down-thread (- n)))
8979
8980 (defun gnus-summary-kill-thread (unmark)
8981   "Mark articles under current thread as read.
8982 If the prefix argument is positive, remove any kinds of marks.
8983 If the prefix argument is negative, tick articles instead."
8984   (interactive "P")
8985   (if unmark
8986       (setq unmark (prefix-numeric-value unmark)))
8987   (let ((killing t)
8988         (level (gnus-summary-thread-level)))
8989     (save-excursion
8990       (while killing
8991         ;; Mark the article...
8992         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
8993               ((> unmark 0) (gnus-summary-tick-article nil t))
8994               (t (gnus-summary-tick-article)))
8995         ;; ...and go forward until either the buffer ends or the subtree
8996         ;; ends. 
8997         (if (not (and (zerop (forward-line 1))
8998                       (> (gnus-summary-thread-level) level)))
8999             (setq killing nil))))
9000     ;; Hide killed subtrees.
9001     (and (null unmark)
9002          gnus-thread-hide-killed
9003          (gnus-summary-hide-thread))
9004     ;; If marked as read, go to next unread subject.
9005     (if (null unmark)
9006         ;; Go to next unread subject.
9007         (gnus-summary-next-subject 1 t)))
9008   (gnus-set-mode-line 'summary))
9009
9010 ;; Summary sorting commands
9011
9012 (defun gnus-summary-sort-by-number (&optional reverse)
9013   "Sort summary buffer by article number.
9014 Argument REVERSE means reverse order."
9015   (interactive "P")
9016   (gnus-summary-sort 
9017    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9018
9019 (defun gnus-summary-sort-by-author (&optional reverse)
9020   "Sort summary buffer by author name alphabetically.
9021 If case-fold-search is non-nil, case of letters is ignored.
9022 Argument REVERSE means reverse order."
9023   (interactive "P")
9024   (gnus-summary-sort
9025    (cons
9026     (lambda ()
9027       (let ((extract (funcall
9028                       gnus-extract-address-components
9029                       (header-from (gnus-get-header-by-number
9030                                     (gnus-summary-article-number))))))
9031         (or (car extract) (cdr extract))))
9032     'gnus-thread-sort-by-author)
9033    reverse))
9034
9035 (defun gnus-summary-sort-by-subject (&optional reverse)
9036   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9037 If case-fold-search is non-nil, case of letters is ignored.
9038 Argument REVERSE means reverse order."
9039   (interactive "P")
9040   (gnus-summary-sort
9041    (cons
9042     (lambda ()
9043       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9044     'gnus-thread-sort-by-subject)
9045    reverse))
9046
9047 (defun gnus-summary-sort-by-date (&optional reverse)
9048   "Sort summary buffer by date.
9049 Argument REVERSE means reverse order."
9050   (interactive "P")
9051   (gnus-summary-sort
9052    (cons
9053     (lambda ()
9054       (gnus-sortable-date
9055        (header-date 
9056         (gnus-get-header-by-number (gnus-summary-article-number)))))
9057     'gnus-thread-sort-by-date)
9058    reverse))
9059
9060 (defun gnus-summary-sort-by-score (&optional reverse)
9061   "Sort summary buffer by score.
9062 Argument REVERSE means reverse order."
9063   (interactive "P")
9064   (gnus-summary-sort 
9065    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9066    (not reverse)))
9067
9068 (defvar gnus-summary-already-sorted nil)
9069 (defun gnus-summary-sort (predicate reverse)
9070   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9071   (if gnus-summary-already-sorted
9072       ()
9073     (let (buffer-read-only)
9074       (if (not gnus-show-threads)
9075           (progn
9076             (goto-char (point-min))
9077             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9078         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9079               (gnus-summary-prepare-hook nil)
9080               (gnus-summary-already-sorted nil))
9081           (gnus-summary-prepare)
9082           (and gnus-show-threads
9083                gnus-thread-hide-subtree
9084                (gnus-summary-hide-all-threads)))))))
9085   
9086 (defun gnus-sortable-date (date)
9087   "Make sortable string by string-lessp from DATE.
9088 Timezone package is used."
9089   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9090          (year   (aref date 0))
9091          (month  (aref date 1))
9092          (day    (aref date 2)))
9093     (timezone-make-sortable-date 
9094      year month day 
9095      (timezone-make-time-string
9096       (aref date 3) (aref date 4) (aref date 5)))))
9097
9098
9099 ;; Summary saving commands.
9100
9101 (defun gnus-summary-save-article (n)
9102   "Save the current article using the default saver function.
9103 If N is a positive number, save the N next articles.
9104 If N is a negative number, save the N previous articles.
9105 If N is nil and any articles have been marked with the process mark,
9106 save those articles instead.
9107 The variable `gnus-default-article-saver' specifies the saver function."
9108   (interactive "P")
9109   (let ((articles (gnus-summary-work-articles n)))
9110     (while articles
9111       (let ((header (gnus-get-header-by-number (car articles))))
9112         (if (vectorp header)
9113             (progn
9114               (gnus-summary-select-article t nil nil (car articles))
9115               (or gnus-save-all-headers
9116                   (gnus-article-hide-headers t))
9117               ;; Remove any X-Gnus lines.
9118               (save-excursion
9119                 (save-restriction
9120                   (set-buffer gnus-article-buffer)
9121                   (let ((buffer-read-only nil))
9122                     (goto-char (point-min))
9123                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9124                                                   (point-max)))
9125                     (while (re-search-forward "^X-Gnus" nil t)
9126                       (beginning-of-line)
9127                       (delete-region (point)
9128                                      (progn (forward-line 1) (point))))
9129                     (widen))))
9130               (save-excursion
9131                 (if gnus-default-article-saver
9132                     (funcall gnus-default-article-saver)
9133                   (error "No default saver is defined."))))
9134           (if (assq 'name header)
9135               (gnus-copy-file (cdr (assq 'name header)))
9136             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9137       (gnus-summary-remove-process-mark (car articles))
9138       (setq articles (cdr articles)))
9139     (gnus-summary-position-cursor)
9140     n))
9141
9142 (defun gnus-summary-pipe-output (arg)
9143   "Pipe the current article to a subprocess.
9144 If N is a positive number, pipe the N next articles.
9145 If N is a negative number, pipe the N previous articles.
9146 If N is nil and any articles have been marked with the process mark,
9147 pipe those articles instead."
9148   (interactive "P")
9149   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9150     (gnus-summary-save-article arg)))
9151
9152 (defun gnus-summary-save-article-mail (arg)
9153   "Append the current article to an mail file.
9154 If N is a positive number, save the N next articles.
9155 If N is a negative number, save the N previous articles.
9156 If N is nil and any articles have been marked with the process mark,
9157 save those articles instead."
9158   (interactive "P")
9159   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9160     (gnus-summary-save-article arg)))
9161
9162 (defun gnus-summary-save-article-rmail (arg)
9163   "Append the current article to an rmail file.
9164 If N is a positive number, save the N next articles.
9165 If N is a negative number, save the N previous articles.
9166 If N is nil and any articles have been marked with the process mark,
9167 save those articles instead."
9168   (interactive "P")
9169   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9170     (gnus-summary-save-article arg)))
9171
9172 (defun gnus-summary-save-article-file (arg)
9173   "Append the current article to a file.
9174 If N is a positive number, save the N next articles.
9175 If N is a negative number, save the N previous articles.
9176 If N is nil and any articles have been marked with the process mark,
9177 save those articles instead."
9178   (interactive "P")
9179   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9180     (gnus-summary-save-article arg)))
9181
9182 (defun gnus-summary-save-article-folder (arg)
9183   "Append the current article to an mh folder.
9184 If N is a positive number, save the N next articles.
9185 If N is a negative number, save the N previous articles.
9186 If N is nil and any articles have been marked with the process mark,
9187 save those articles instead."
9188   (interactive "P")
9189   (let ((gnus-default-article-saver 'gnus-summary-save-in-folder))
9190     (gnus-summary-save-article arg)))
9191
9192 (defun gnus-summary-save-article-vm (arg)
9193   "Append the current article to a vm folder.
9194 If N is a positive number, save the N next articles.
9195 If N is a negative number, save the N previous articles.
9196 If N is nil and any articles have been marked with the process mark,
9197 save those articles instead."
9198   (interactive "P")
9199   (let ((gnus-default-article-saver 'gnus-summary-save-in-vm))
9200     (gnus-summary-save-article arg)))
9201
9202 (defun gnus-read-save-file-name (prompt default-name)
9203   (let ((methods gnus-split-methods)
9204         split-name)
9205     (if (not gnus-split-methods)
9206         ()
9207       (save-excursion
9208         (set-buffer gnus-article-buffer)
9209         (gnus-narrow-to-headers)
9210         (while methods
9211           (goto-char (point-min))
9212           (and (condition-case () 
9213                    (re-search-forward (car (car methods)) nil t)
9214                  (error nil))
9215                (setq split-name (cons (nth 1 (car methods)) split-name)))
9216           (setq methods (cdr methods)))
9217         (widen)))
9218     (cond ((null split-name)
9219            (read-file-name
9220             (concat prompt " (default "
9221                     (file-name-nondirectory default-name) ") ")
9222             (file-name-directory default-name)
9223             default-name))
9224           ((= 1 (length split-name))
9225            (read-file-name
9226             (concat prompt " (default " (car split-name) ") ")
9227             gnus-article-save-directory
9228             (concat gnus-article-save-directory (car split-name))))
9229           (t
9230            (setq split-name (mapcar (lambda (el) (list el))
9231                                     (nreverse split-name)))
9232            (let ((result (completing-read 
9233                           (concat prompt " ")
9234                           split-name nil nil)))
9235              (concat gnus-article-save-directory
9236                      (if (string= result "")
9237                          (car (car split-name))
9238                        result)))))))
9239
9240 (defun gnus-summary-save-in-rmail (&optional filename)
9241   "Append this article to Rmail file.
9242 Optional argument FILENAME specifies file name.
9243 Directory to save to is default to `gnus-article-save-directory' which
9244 is initialized from the SAVEDIR environment variable."
9245   (interactive)
9246   (let ((default-name
9247           (funcall gnus-rmail-save-name gnus-newsgroup-name
9248                    gnus-current-headers gnus-newsgroup-last-rmail)))
9249     (or filename
9250         (setq filename (gnus-read-save-file-name 
9251                         "Save in rmail file:" default-name)))
9252     (gnus-make-directory (file-name-directory filename))
9253     (gnus-eval-in-buffer-window 
9254      gnus-article-buffer
9255      (save-excursion
9256        (save-restriction
9257          (widen)
9258          (gnus-output-to-rmail filename))))
9259     ;; Remember the directory name to save articles.
9260     (setq gnus-newsgroup-last-rmail filename)))
9261
9262 (defun gnus-summary-save-in-mail (&optional filename)
9263   "Append this article to Unix mail file.
9264 Optional argument FILENAME specifies file name.
9265 Directory to save to is default to `gnus-article-save-directory' which
9266 is initialized from the SAVEDIR environment variable."
9267   (interactive)
9268   (let ((default-name
9269           (funcall gnus-mail-save-name gnus-newsgroup-name
9270                    gnus-current-headers gnus-newsgroup-last-mail)))
9271     (or filename
9272         (setq filename (gnus-read-save-file-name 
9273                         "Save in Unix mail file:" default-name)))
9274     (setq filename
9275           (expand-file-name filename
9276                             (and default-name
9277                                  (file-name-directory default-name))))
9278     (gnus-make-directory (file-name-directory filename))
9279     (gnus-eval-in-buffer-window 
9280      gnus-article-buffer
9281      (save-excursion
9282        (save-restriction
9283          (widen)
9284          (if (and (file-readable-p filename) (rmail-file-p filename))
9285              (gnus-output-to-rmail filename)
9286            (rmail-output filename 1 t t)))))
9287     ;; Remember the directory name to save articles.
9288     (setq gnus-newsgroup-last-mail filename)))
9289
9290 (defun gnus-summary-save-in-file (&optional filename)
9291   "Append this article to file.
9292 Optional argument FILENAME specifies file name.
9293 Directory to save to is default to `gnus-article-save-directory' which
9294 is initialized from the SAVEDIR environment variable."
9295   (interactive)
9296   (let ((default-name
9297           (funcall gnus-file-save-name gnus-newsgroup-name
9298                    gnus-current-headers gnus-newsgroup-last-file)))
9299     (or filename
9300         (setq filename (gnus-read-save-file-name 
9301                         "Save in file:" default-name)))
9302     (gnus-make-directory (file-name-directory filename))
9303     (gnus-eval-in-buffer-window 
9304      gnus-article-buffer
9305      (save-excursion
9306        (save-restriction
9307          (widen)
9308          (gnus-output-to-file filename))))
9309     ;; Remember the directory name to save articles.
9310     (setq gnus-newsgroup-last-file filename)))
9311
9312 (defun gnus-summary-save-in-pipe (&optional command)
9313   "Pipe this article to subprocess."
9314   (interactive)
9315   (let ((command (read-string "Shell command on article: "
9316                               gnus-last-shell-command)))
9317     (if (string-equal command "")
9318         (setq command gnus-last-shell-command))
9319     (gnus-eval-in-buffer-window 
9320      gnus-article-buffer
9321      (save-restriction
9322        (widen)
9323        (shell-command-on-region (point-min) (point-max) command nil)))
9324     (setq gnus-last-shell-command command)))
9325
9326 ;; Summary extract commands
9327
9328 (defun gnus-summary-insert-pseudos (pslist)
9329   (let ((buffer-read-only nil)
9330         (article (gnus-summary-article-number))
9331         b)
9332     (or (gnus-summary-goto-subject article)
9333         (error (format "No such article: %d" article)))
9334     (or gnus-newsgroup-headers-hashtb-by-number
9335         (gnus-make-headers-hashtable-by-number))
9336     (gnus-summary-position-cursor)
9337     ;; If all commands are to be bunched up on one line, we collect
9338     ;; them here.  
9339     (if gnus-view-pseudos-separately
9340         ()
9341       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9342             files action)
9343         (while ps
9344           (setq action (cdr (assq 'action (car ps))))
9345           (setq files (list (cdr (assq 'name (car ps)))))
9346           (while (and ps (cdr ps)
9347                       (string= (or action "1")
9348                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9349             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9350             (setcdr ps (cdr (cdr ps))))
9351           (if (not files)
9352               ()
9353             (if (not (string-match "%s" action))
9354                 (setq files (cons " " files)))
9355             (setq files (cons " " files))
9356             (and (assq 'execute (car ps))
9357                  (setcdr (assq 'execute (car ps))
9358                          (funcall (if (string-match "%s" action)
9359                                       'format 'concat)
9360                                   action 
9361                                   (mapconcat (lambda (f) f) files " ")))))
9362           (setq ps (cdr ps)))))
9363     (if gnus-view-pseudos
9364         (while pslist
9365           (and (assq 'execute (car pslist))
9366                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9367                                      (eq gnus-view-pseudos 'not-confirm)))
9368           (setq pslist (cdr pslist)))
9369       (save-excursion
9370         (while pslist
9371           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9372                                          (gnus-summary-article-number)))
9373           (forward-line 1)
9374           (setq b (point))
9375           (insert "          " (file-name-nondirectory 
9376                                 (cdr (assq 'name (car pslist))))
9377                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9378           (add-text-properties 
9379            b (1+ b) (list 'gnus (list gnus-reffed-article-number
9380                                       gnus-unread-mark 0)
9381                           'gnus-pseudo (car pslist)))
9382           (forward-line -1)
9383           (gnus-sethash (int-to-string gnus-reffed-article-number)
9384                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9385           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9386           (setq pslist (cdr pslist)))))))
9387
9388 (defun gnus-pseudos< (p1 p2)
9389   (let ((c1 (cdr (assq 'action p1)))
9390         (c2 (cdr (assq 'action p2))))
9391     (and c1 c2 (string< c1 c2))))
9392
9393 (defun gnus-request-pseudo-article (props)
9394   (cond ((assq 'execute props)
9395          (gnus-execute-command (cdr (assq 'execute props)))))
9396   (let ((gnus-current-article (gnus-summary-article-number)))
9397     (run-hooks 'gnus-mark-article-hook)))
9398
9399 (defun gnus-execute-command (command &optional automatic)
9400   (save-excursion
9401     (gnus-article-setup-buffer)
9402     (set-buffer gnus-article-buffer)
9403     (let ((command (if automatic command (read-string "Command: " command)))
9404           (buffer-read-only nil))
9405       (erase-buffer)
9406       (insert "$ " command "\n\n")
9407       (if gnus-view-pseudo-asynchronously
9408           (start-process "gnus-execute" nil "sh" "-c" command)
9409         (call-process "sh" nil t nil "-c" command)))))
9410
9411 (defun gnus-copy-file (file &optional to)
9412   "Copy FILE to TO."
9413   (interactive
9414    (list (read-file-name "Copy file: " default-directory)
9415          (read-file-name "Copy file to: " default-directory)))
9416   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9417   (and (file-directory-p to) 
9418        (setq to (concat (file-name-as-directory to)
9419                         (file-name-nondirectory file))))
9420   (copy-file file to))
9421
9422 ;; Summary kill commands.
9423
9424 (defun gnus-summary-edit-global-kill (article)
9425   "Edit the \"global\" kill file."
9426   (interactive (list (gnus-summary-article-number)))
9427   (gnus-group-edit-global-kill article))
9428
9429 (defun gnus-summary-edit-local-kill ()
9430   "Edit a local kill file applied to the current newsgroup."
9431   (interactive)
9432   (setq gnus-current-headers 
9433         (gnus-gethash 
9434          (int-to-string (gnus-summary-article-number))
9435          gnus-newsgroup-headers-hashtb-by-number))
9436   (gnus-set-global-variables)
9437   (gnus-group-edit-local-kill 
9438    (gnus-summary-article-number) gnus-newsgroup-name))
9439
9440 \f
9441 ;;;
9442 ;;; Gnus article mode
9443 ;;;
9444
9445 (put 'gnus-article-mode 'mode-class 'special)
9446
9447 (if gnus-article-mode-map
9448     nil
9449   (setq gnus-article-mode-map (make-keymap))
9450   (suppress-keymap gnus-article-mode-map)
9451   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9452   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9453   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9454   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9455   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9456   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9457   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9458   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9459   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9460   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9461   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9462   
9463   ;; Duplicate almost all summary keystrokes in the article mode map.
9464   (let ((commands 
9465          (list "#" "\M-#" "\C-c\M-#" "n" "p"
9466                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9467                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9468                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k""x" "X" 
9469                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9470                "t" "\M-t" "C" "S"
9471                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9472                "V" "\C-c\C-d")))
9473     (while commands
9474       (define-key gnus-article-mode-map (car commands) 
9475         'gnus-article-summary-command)
9476       (setq commands (cdr commands))))
9477
9478   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9479     (while commands
9480       (define-key gnus-article-mode-map (car commands) 
9481         'gnus-article-summary-command-nosave)
9482       (setq commands (cdr commands)))))
9483
9484
9485 (defun gnus-article-mode ()
9486   "Major mode for displaying an article.
9487
9488 All normal editing commands are switched off.
9489
9490 The following commands are available:
9491
9492 \\<gnus-article-mode-map>
9493 \\[gnus-article-next-page]\t Scroll the article one page forwards
9494 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9495 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9496 \\[gnus-article-show-summary]\t Display the summary buffer
9497 \\[gnus-article-mail]\t Send a reply to the address near point
9498 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9499 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9500 \\[gnus-info-find-node]\t Go to the Gnus info node"
9501   (interactive)
9502   (if gnus-visual (gnus-article-make-menu-bar))
9503   (kill-all-local-variables)
9504   (setq mode-line-modified "-- ")
9505   (make-local-variable 'mode-line-format)
9506   (setq mode-line-format (copy-sequence mode-line-format))
9507   (and (equal (nth 3 mode-line-format) "   ")
9508        (setcar (nthcdr 3 mode-line-format) ""))
9509   (setq mode-name "Article")
9510   (setq major-mode 'gnus-article-mode)
9511   (make-local-variable 'minor-mode-alist)
9512   (or (assq 'gnus-show-mime minor-mode-alist)
9513       (setq minor-mode-alist
9514             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9515   (use-local-map gnus-article-mode-map)
9516   (make-local-variable 'page-delimiter)
9517   (setq page-delimiter gnus-page-delimiter)
9518   (buffer-disable-undo (current-buffer))
9519   (setq buffer-read-only t)             ;Disable modification
9520   (run-hooks 'gnus-article-mode-hook))
9521
9522 (defun gnus-article-setup-buffer ()
9523   "Initialize article mode buffer."
9524   (or (get-buffer gnus-article-buffer)
9525       (save-excursion
9526         (set-buffer (get-buffer-create gnus-article-buffer))
9527         (gnus-add-current-to-buffer-list)
9528         (gnus-article-mode))))
9529
9530 ;; Set article window start at LINE, where LINE is the number of lines
9531 ;; from the head of the article.
9532 (defun gnus-article-set-window-start (&optional line)
9533   (set-window-start 
9534    (get-buffer-window gnus-article-buffer)
9535    (save-excursion
9536      (set-buffer gnus-article-buffer)
9537      (goto-char (point-min))
9538      (if (not line)
9539          (point-min)
9540        (gnus-message 6 "Moved to bookmark")
9541        (search-forward "\n\n" nil t)
9542        (forward-line line)
9543        (point)))))
9544
9545 (defun gnus-request-article-this-buffer (article group)
9546   "Get an article and insert it into this buffer."
9547   (setq group (or group gnus-newsgroup-name))
9548
9549   ;; Open server if it has closed.
9550   (gnus-check-news-server (gnus-find-method-for-group group))
9551
9552   ;; Using `gnus-request-article' directly will insert the article into
9553   ;; `nntp-server-buffer' - so we'll save some time by not having to
9554   ;; copy it from the server buffer into the article buffer.
9555
9556   ;; We only request an article by message-id when we do not have the
9557   ;; headers for it, so we'll have to get those.
9558   (and (stringp article) 
9559        (let ((gnus-override-method gnus-refer-article-method))
9560          (gnus-read-header article)))
9561
9562   ;; If the article number is negative, that means that this article
9563   ;; doesn't belong in this newsgroup (possibly), so we find its
9564   ;; message-id and request it by id instead of number.
9565   (if (and (numberp article) (< article 0))
9566       (save-excursion
9567         (set-buffer gnus-summary-buffer)
9568         (let ((header (gnus-gethash (int-to-string article)
9569                                     gnus-newsgroup-headers-hashtb-by-number)))
9570           (if (vectorp header)
9571               ;; It's a real article.
9572               (setq article (header-id header))
9573             ;; It is an extracted pseudo-article.
9574             (setq article nil)
9575             (gnus-request-pseudo-article header)))))
9576
9577   ;; Check the cache.
9578   (if (and gnus-use-cache
9579            (numberp article)
9580            (gnus-cache-request-article article group))
9581       'article
9582     ;; Get the article and into the article buffer.
9583     (if article
9584         (progn
9585           (erase-buffer)
9586           (let ((gnus-override-method 
9587                  (and (stringp article) gnus-refer-article-method)))
9588             (and (gnus-request-article article group (current-buffer))
9589                  'article)))
9590       'pseudo)))
9591
9592 (defun gnus-read-header (id)
9593   "Read the headers of article ID and enter them into the Gnus system."
9594   (or gnus-newsgroup-headers-hashtb-by-number
9595       (gnus-make-headers-hashtable-by-number))
9596   (let (header)
9597     (if (not (setq header 
9598                    (car (if (let ((gnus-nov-is-evil t))
9599                               (gnus-retrieve-headers 
9600                                (list id) gnus-newsgroup-name))
9601                             (gnus-get-newsgroup-headers)))))
9602         nil
9603       (if (stringp id)
9604           (header-set-number header gnus-reffed-article-number))
9605       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
9606       (gnus-sethash (int-to-string (header-number header)) header
9607                     gnus-newsgroup-headers-hashtb-by-number)
9608       (if (stringp id)
9609           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
9610       (setq gnus-current-headers header)
9611       header)))
9612
9613 (defun gnus-article-prepare (article &optional all-headers header)
9614   "Prepare ARTICLE in article mode buffer.
9615 ARTICLE should either be an article number or a Message-ID.
9616 If ARTICLE is an id, HEADER should be the article headers.
9617 If ALL-HEADERS is non-nil, no headers are hidden."
9618   (save-excursion
9619     ;; Make sure we start in a summary buffer.
9620     (or (eq major-mode 'gnus-summary-mode)
9621         (set-buffer gnus-summary-buffer))
9622     (setq gnus-summary-buffer (current-buffer))
9623     ;; Make sure the connection to the server is alive.
9624     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
9625         (progn
9626           (gnus-check-news-server 
9627            (gnus-find-method-for-group gnus-newsgroup-name))
9628           (gnus-request-group gnus-newsgroup-name t)))
9629     (or gnus-newsgroup-headers-hashtb-by-number
9630         (gnus-make-headers-hashtable-by-number))
9631     (let* ((article (if header (header-number header) article))
9632            (summary-buffer (current-buffer))
9633            (internal-hook gnus-article-internal-prepare-hook)
9634            (group gnus-newsgroup-name)
9635            result)
9636       (save-excursion
9637         (gnus-article-setup-buffer)
9638         (set-buffer gnus-article-buffer)
9639         (if (not (setq result (let ((buffer-read-only nil))
9640                                 (gnus-request-article-this-buffer 
9641                                  article group))))
9642             ;; There is no such article.
9643             (progn
9644               (save-excursion
9645                 (set-buffer gnus-summary-buffer)
9646                 (setq gnus-current-article 0)
9647                 (and (numberp article) 
9648                      (gnus-summary-mark-as-read article gnus-canceled-mark))
9649                 (gnus-message 1 "No such article (may be canceled)")
9650                 (and (numberp article)
9651                      (setq gnus-current-article article))
9652                 (ding))
9653               (and (numberp article)
9654                    (setq gnus-article-current 
9655                          (cons gnus-newsgroup-name article)))
9656               nil)
9657           (if (not (eq result 'article))
9658               (progn
9659                 (save-excursion
9660                   (set-buffer summary-buffer)
9661                   (setq gnus-last-article gnus-current-article
9662                         gnus-newsgroup-history (cons gnus-current-article
9663                                                      gnus-newsgroup-history)
9664                         gnus-current-article 0
9665                         gnus-current-headers nil
9666                         gnus-article-current nil)
9667                   (gnus-configure-windows 'article)
9668                   (gnus-set-global-variables))
9669                 (gnus-set-mode-line 'article))
9670             ;; The result from the `request' was an actual article -
9671             ;; or at least some text that is now displayed in the
9672             ;; article buffer.
9673             (if (and (numberp article)
9674                      (not (eq article gnus-current-article)))
9675                 ;; Seems like a new article has been selected.
9676                 ;; `gnus-current-article' must be an article number.
9677                 (save-excursion
9678                   (set-buffer summary-buffer)
9679                   (setq gnus-last-article gnus-current-article
9680                         gnus-newsgroup-history (cons gnus-current-article
9681                                                      gnus-newsgroup-history)
9682                         gnus-current-article article
9683                         gnus-current-headers 
9684                         (gnus-get-header-by-number gnus-current-article)
9685                         gnus-article-current 
9686                         (cons gnus-newsgroup-name gnus-current-article))
9687                   (gnus-summary-show-thread)
9688                   (run-hooks 'gnus-mark-article-hook)
9689                   (gnus-set-mode-line 'summary)
9690                   (and gnus-visual 
9691                        (run-hooks 'gnus-visual-mark-article-hook))
9692                   ;; Set the global newsgroup variables here.
9693                   ;; Suggested by Jim Sisolak
9694                   ;; <sisolak@trans4.neep.wisc.edu>.
9695                   (gnus-set-global-variables)
9696                   (and gnus-use-cache 
9697                        (gnus-cache-possibly-enter-article
9698                         group article
9699                         (gnus-get-header-by-number article)
9700                         (memq article gnus-newsgroup-marked)
9701                         (memq article gnus-newsgroup-dormant)
9702                         (memq article gnus-newsgroup-unreads)))))
9703             ;; gnus-have-all-headers must be either T or NIL.
9704             (setq gnus-have-all-headers
9705                   (not (not (or all-headers gnus-show-all-headers))))
9706             ;; Hooks for getting information from the article.
9707             ;; This hook must be called before being narrowed.
9708             (let (buffer-read-only)
9709               (run-hooks 'internal-hook)
9710               (run-hooks 'gnus-article-prepare-hook))
9711             ;; Decode MIME message.
9712             (if (and gnus-show-mime
9713                      (gnus-fetch-field "Mime-Version"))
9714                 (funcall gnus-show-mime-method))
9715             ;; Perform the article display hooks.
9716             (let (buffer-read-only)
9717               (run-hooks 'gnus-article-display-hook))
9718             ;; Do page break.
9719             (goto-char (point-min))
9720             (and gnus-break-pages (gnus-narrow-to-page))
9721             (gnus-set-mode-line 'article)
9722             (gnus-configure-windows 'article)
9723             (goto-char (point-min))
9724             t))))))
9725
9726 (defun gnus-article-show-all-headers ()
9727   "Show all article headers in article mode buffer."
9728   (save-excursion 
9729     (gnus-article-setup-buffer)
9730     (set-buffer gnus-article-buffer)
9731     (let ((buffer-read-only nil))
9732       (remove-text-properties (point-min) (point-max) 
9733                               gnus-hidden-properties))))
9734
9735 (defun gnus-article-hide-headers-if-wanted ()
9736   "Hide unwanted headers if `gnus-have-all-headers' is nil.
9737 Provided for backwards compatability."
9738   (or gnus-have-all-headers
9739       (gnus-article-hide-headers)))
9740
9741 (defun gnus-article-hide-headers (&optional delete)
9742   "Hide unwanted headers and possibly sort them as well."
9743   (interactive "P")
9744   (save-excursion
9745     (set-buffer gnus-article-buffer)
9746     (save-restriction
9747       (let ((sorted gnus-sorted-header-list)
9748             (buffer-read-only nil)
9749             want want-list beg want-l)
9750         ;; First we narrow to just the headers.
9751         (widen)
9752         (goto-char (point-min))
9753         ;; Hide any "From " lines at the beginning of (mail) articles. 
9754         (while (looking-at "From ")
9755           (forward-line 1))
9756         (if (bobp) 
9757             (add-text-properties (point-min) (point) gnus-hidden-properties))
9758         ;; Then treat the rest of the header lines.
9759         (narrow-to-region 
9760          (point) 
9761          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
9762         ;; Then we use the two regular expressions
9763         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
9764         ;; select which header lines is to remain visible in the
9765         ;; article buffer.
9766         (goto-char (point-min))
9767         (while (re-search-forward "^[^ \t]*:" nil t)
9768           (beginning-of-line)
9769           ;; We add the headers we want to keep to a list and delete
9770           ;; them from the buffer.
9771           (if (or (and (stringp gnus-visible-headers)
9772                        (looking-at gnus-visible-headers))
9773                   (and (not (stringp gnus-visible-headers))
9774                        (stringp gnus-ignored-headers)
9775                        (not (looking-at gnus-ignored-headers))))
9776               (progn
9777                 (setq beg (point))
9778                 (forward-line 1)
9779                 ;; Be sure to get multi-line headers...
9780                 (re-search-forward "^[^ \t]*:" nil t)
9781                 (beginning-of-line)
9782                 (setq want-list 
9783                       (cons (buffer-substring beg (point)) want-list))
9784                 (delete-region beg (point))
9785                 (goto-char beg))
9786             (forward-line 1)))
9787         ;; Next we perform the sorting by looking at
9788         ;; `gnus-sorted-header-list'. 
9789         (goto-char (point-min))
9790         (while (and sorted want-list)
9791           (setq want-l want-list)
9792           (while (and want-l
9793                       (not (string-match (car sorted) (car want-l))))
9794             (setq want-l (cdr want-l)))
9795           (if want-l 
9796               (progn
9797                 (insert (car want-l))
9798                 (setq want-list (delq (car want-l) want-list))))
9799           (setq sorted (cdr sorted)))
9800         ;; Any headers that were not matched by the sorted list we
9801         ;; just tack on the end of the visible header list.
9802         (while want-list
9803           (insert (car want-list))
9804           (setq want-list (cdr want-list)))
9805         ;; And finally we make the unwanted headers invisible.
9806         (if delete
9807             (delete-region (point) (point-max))
9808           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
9809           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
9810
9811 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
9812 (defun gnus-article-treat-overstrike ()
9813   "Translate overstrikes into bold text."
9814   (interactive)
9815   (save-excursion
9816     (set-buffer gnus-article-buffer)
9817     (let ((buffer-read-only nil))
9818       (while (search-forward "\b" nil t)
9819         (let ((next (following-char))
9820               (previous (char-after (- (point) 2))))
9821           (cond ((eq next previous)
9822                  (delete-region (- (point) 2) (point))
9823                  (put-text-property (point) (1+ (point))
9824                                     'face 'bold))
9825                 ((eq next ?_)
9826                  (delete-region (1- (point)) (1+ (point)))
9827                  (put-text-property (1- (point)) (point)
9828                                     'face 'underline))
9829                 ((eq previous ?_)
9830                  (delete-region (- (point) 2) (point))
9831                  (put-text-property (point) (1+ (point))
9832                                     'face 'underline))))))))
9833
9834 (defun gnus-article-word-wrap ()
9835   "Format too long lines."
9836   (interactive)
9837   (save-excursion
9838     (set-buffer gnus-article-buffer)
9839     (let ((buffer-read-only nil))
9840       (goto-char (point-min))
9841       (search-forward "\n\n" nil t)
9842       (end-of-line 1)
9843       (let ((paragraph-start "^\\W"))
9844         (while (not (eobp))
9845           (and (>= (current-column) (window-width))
9846                (/= (preceding-char) ?:)
9847                (fill-paragraph nil))
9848           (end-of-line 2))))))
9849
9850 (defun gnus-article-remove-cr ()
9851   "Remove carriage returns from an article."
9852   (interactive)
9853   (save-excursion
9854     (set-buffer gnus-article-buffer)
9855     (let ((buffer-read-only nil))
9856       (goto-char (point-min))
9857       (while (search-forward "\r" nil t)
9858         (replace-match "" t t)))))
9859
9860 (defun gnus-article-display-x-face (&optional force)
9861   "Look for an X-Face header and display it if present."
9862   (interactive (list 'force))
9863   (save-excursion
9864     (set-buffer gnus-article-buffer)
9865     (let ((inhibit-point-motion-hooks t)
9866           (case-fold-search nil))
9867       (save-restriction
9868         (goto-char (point-min))
9869         (search-forward "\n\n")
9870         (narrow-to-region (point-min) (point))
9871         (goto-char (point-min))
9872         (if (or (not gnus-article-x-face-command)
9873                 (and (not force)
9874                      (or (not gnus-article-x-face-too-ugly)
9875                          (string-match gnus-article-x-face-too-ugly
9876                                        (mail-fetch-field "from"))))
9877                 (progn
9878                   (goto-char (point-min))
9879                   (not (re-search-forward "^X-Face: " nil t))))
9880             nil
9881           (let ((beg (point))
9882                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
9883             (if (symbolp gnus-article-x-face-command)
9884                 (and (or (fboundp gnus-article-x-face-command)
9885                          (error "%s is not a function"
9886                                 gnus-article-x-face-command))
9887                      (funcall gnus-article-x-face-command beg end))
9888               (call-process-region beg end "sh" nil 0 nil
9889                                    "-c" gnus-article-x-face-command))))))))
9890
9891 (defun gnus-article-de-quoted-unreadable (&optional force)
9892   "Do a naïve translation of a quoted-printable-encoded article.
9893 This is in no way, shape or form meant as a replacement for real MIME
9894 processing, but is simply a stop-gap measure until MIME support is
9895 written.
9896 If FORCE, decode the article whether it is marked as quoted-printable
9897 or not." 
9898   (interactive (list 'force))
9899   (save-excursion
9900     (set-buffer gnus-article-buffer)
9901     (let ((case-fold-search t)
9902           (buffer-read-only nil)
9903           (type (gnus-fetch-field "content-transfer-encoding")))
9904       (if (or force (and type (string-match "quoted-printable" type)))
9905           (progn
9906             (goto-char (point-min))
9907             (search-forward "\n\n" nil 'move)
9908             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
9909
9910 (defun gnus-mime-decode-quoted-printable (from to)
9911   ;; Decode quoted-printable from region between FROM and TO.
9912   (save-excursion
9913     (goto-char from)
9914     (while (search-forward "=" to t)
9915       (cond ((eq (following-char) ?\n)
9916              (delete-char -1)
9917              (delete-char 1))
9918             ((looking-at "[0-9A-F][0-9A-F]")
9919              (delete-char -1)
9920              (insert (hexl-hex-string-to-integer
9921                       (buffer-substring (point) (+ 2 (point)))))
9922              (delete-char 2))
9923             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
9924
9925 (defun gnus-article-date-ut (&optional type)
9926   "Convert DATE date to universal time in the current article.
9927 If TYPE is `local', convert to local time; if it is `lapsed', output
9928 how much time has lapsed since DATE."
9929   (interactive (list 'ut))
9930   (let ((date (header-date (or gnus-current-headers 
9931                                (gnus-get-header-by-number
9932                                 (gnus-summary-article-number)))))
9933         (date-regexp "^Date: \\|^X-Sent: "))
9934     (save-excursion
9935       (set-buffer gnus-article-buffer)
9936       (let ((buffer-read-only nil))
9937         (goto-char (point-min))
9938         (if (and (re-search-forward date-regexp nil t)
9939                  (progn 
9940                    (beginning-of-line)
9941                    (looking-at date-regexp)))
9942             (delete-region (gnus-point-at-bol)
9943                            (progn (end-of-line) (1+ (point))))
9944           (goto-char (point-min))
9945           (goto-char (- (search-forward "\n\n") 2)))
9946         (insert
9947          (cond 
9948           ((eq type 'local)
9949            (concat "Date: " (timezone-make-date-arpa-standard date) "\n"))
9950           ((eq type 'ut)
9951            (concat "Date: " (timezone-make-date-arpa-standard date nil "UT")
9952                    "\n"))
9953           ((eq type 'lapsed)
9954            (let* ((sec (- (gnus-seconds-since-epoch 
9955                            (timezone-make-date-arpa-standard
9956                             (current-time-string) (current-time-zone) "UT"))
9957                           (gnus-seconds-since-epoch 
9958                            (timezone-make-date-arpa-standard date nil "UT"))))
9959                   (units (list (cons 'year (* 365.25 24 60 60))
9960                                (cons 'week (* 7 24 60 60))
9961                                (cons 'day (* 24 60 60))
9962                                (cons 'hour (* 60 60))
9963                                (cons 'minute 60)
9964                                (cons 'second 1)))
9965                   num prev)
9966              (concat
9967               "X-Sent: "
9968               (mapconcat 
9969                (lambda (unit)
9970                  (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
9971                      ""
9972                    (setq sec (- sec (* num (cdr unit))))
9973                    (prog1
9974                        (concat (if prev ", " "") (int-to-string (floor num))
9975                                " " (symbol-name (car unit))
9976                                (if (> num 1) "s" ""))
9977                      (setq prev t))))
9978                units "")
9979               " ago\n")))
9980           (t
9981            (error "Unknown conversion type: %s" type))))))))
9982
9983 (defun gnus-article-date-local ()
9984   "Convert the current article date to the local timezone."
9985   (interactive)
9986   (gnus-article-date-ut 'local))
9987
9988 (defun gnus-article-date-lapsed ()
9989   "Convert the current article date to time lapsed since it was sent."
9990   (interactive)
9991   (gnus-article-date-ut 'lapsed))
9992
9993 (defun gnus-article-maybe-highlight ()
9994   (if gnus-visual (gnus-article-highlight)))
9995
9996 ;; Article savers.
9997
9998 (defun gnus-output-to-rmail (file-name)
9999   "Append the current article to an Rmail file named FILE-NAME."
10000   (require 'rmail)
10001   ;; Most of these codes are borrowed from rmailout.el.
10002   (setq file-name (expand-file-name file-name))
10003   (setq rmail-default-rmail-file file-name)
10004   (let ((artbuf (current-buffer))
10005         (tmpbuf (get-buffer-create " *Gnus-output*")))
10006     (save-excursion
10007       (or (get-file-buffer file-name)
10008           (file-exists-p file-name)
10009           (if (gnus-yes-or-no-p
10010                (concat "\"" file-name "\" does not exist, create it? "))
10011               (let ((file-buffer (create-file-buffer file-name)))
10012                 (save-excursion
10013                   (set-buffer file-buffer)
10014                   (rmail-insert-rmail-file-header)
10015                   (let ((require-final-newline nil))
10016                     (write-region (point-min) (point-max) file-name t 1)))
10017                 (kill-buffer file-buffer))
10018             (error "Output file does not exist")))
10019       (set-buffer tmpbuf)
10020       (buffer-disable-undo (current-buffer))
10021       (erase-buffer)
10022       (insert-buffer-substring artbuf)
10023       (gnus-convert-article-to-rmail)
10024       ;; Decide whether to append to a file or to an Emacs buffer.
10025       (let ((outbuf (get-file-buffer file-name)))
10026         (if (not outbuf)
10027             (append-to-file (point-min) (point-max) file-name)
10028           ;; File has been visited, in buffer OUTBUF.
10029           (set-buffer outbuf)
10030           (let ((buffer-read-only nil)
10031                 (msg (and (boundp 'rmail-current-message)
10032                           (symbol-value 'rmail-current-message))))
10033             ;; If MSG is non-nil, buffer is in RMAIL mode.
10034             (if msg
10035                 (progn (widen)
10036                        (narrow-to-region (point-max) (point-max))))
10037             (insert-buffer-substring tmpbuf)
10038             (if msg
10039                 (progn
10040                   (goto-char (point-min))
10041                   (widen)
10042                   (search-backward "\^_")
10043                   (narrow-to-region (point) (point-max))
10044                   (goto-char (1+ (point-min)))
10045                   (rmail-count-new-messages t)
10046                   (rmail-show-message msg)))))))
10047     (kill-buffer tmpbuf)))
10048
10049 (defun gnus-output-to-file (file-name)
10050   "Append the current article to a file named FILE-NAME."
10051   (setq file-name (expand-file-name file-name))
10052   (let ((artbuf (current-buffer))
10053         (tmpbuf (get-buffer-create " *Gnus-output*")))
10054     (save-excursion
10055       (set-buffer tmpbuf)
10056       (buffer-disable-undo (current-buffer))
10057       (erase-buffer)
10058       (insert-buffer-substring artbuf)
10059       ;; Append newline at end of the buffer as separator, and then
10060       ;; save it to file.
10061       (goto-char (point-max))
10062       (insert "\n")
10063       (append-to-file (point-min) (point-max) file-name))
10064     (kill-buffer tmpbuf)))
10065
10066 (defun gnus-convert-article-to-rmail ()
10067   "Convert article in current buffer to Rmail message format."
10068   (let ((buffer-read-only nil))
10069     ;; Convert article directly into Babyl format.
10070     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10071     (goto-char (point-min))
10072     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10073     (while (search-forward "\n\^_" nil t) ;single char
10074       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10075     (goto-char (point-max))
10076     (insert "\^_")))
10077
10078 (defun gnus-narrow-to-page (&optional arg)
10079   "Make text outside current page invisible except for page delimiter.
10080 A numeric arg specifies to move forward or backward by that many pages,
10081 thus showing a page other than the one point was originally in."
10082   (interactive "P")
10083   (setq arg (if arg (prefix-numeric-value arg) 0))
10084   (save-excursion
10085     (forward-page -1)                   ;Beginning of current page.
10086     (widen)
10087     (if (> arg 0)
10088         (forward-page arg)
10089       (if (< arg 0)
10090           (forward-page (1- arg))))
10091     ;; Find the end of the page.
10092     (forward-page)
10093     ;; If we stopped due to end of buffer, stay there.
10094     ;; If we stopped after a page delimiter, put end of restriction
10095     ;; at the beginning of that line.
10096     ;; These are commented out.
10097     ;;    (if (save-excursion (beginning-of-line)
10098     ;;                  (looking-at page-delimiter))
10099     ;;  (beginning-of-line))
10100     (narrow-to-region (point)
10101                       (progn
10102                         ;; Find the top of the page.
10103                         (forward-page -1)
10104                         ;; If we found beginning of buffer, stay there.
10105                         ;; If extra text follows page delimiter on same line,
10106                         ;; include it.
10107                         ;; Otherwise, show text starting with following line.
10108                         (if (and (eolp) (not (bobp)))
10109                             (forward-line 1))
10110                         (point)))))
10111
10112 (defun gnus-gmt-to-local ()
10113   "Rewrite Date header described in GMT to local in current buffer.
10114 Intended to be used with gnus-article-prepare-hook."
10115   (save-excursion
10116     (save-restriction
10117       (widen)
10118       (goto-char (point-min))
10119       (narrow-to-region (point-min)
10120                         (progn (search-forward "\n\n" nil 'move) (point)))
10121       (goto-char (point-min))
10122       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10123           (let ((buffer-read-only nil)
10124                 (date (buffer-substring (match-beginning 1) (match-end 1))))
10125             (delete-region (match-beginning 1) (match-end 1))
10126             (insert
10127              (timezone-make-date-arpa-standard 
10128               date nil (current-time-zone))))))))
10129
10130
10131 ;; Article mode commands
10132
10133 (defun gnus-article-next-page (lines)
10134   "Show next page of current article.
10135 If end of article, return non-nil. Otherwise return nil.
10136 Argument LINES specifies lines to be scrolled up."
10137   (interactive "P")
10138   (move-to-window-line -1)
10139   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10140   (if (save-excursion
10141         (end-of-line)
10142         (and (pos-visible-in-window-p)  ;Not continuation line.
10143              (eobp)))
10144       ;; Nothing in this page.
10145       (if (or (not gnus-break-pages)
10146               (save-excursion
10147                 (save-restriction
10148                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10149           t                             ;Nothing more.
10150         (gnus-narrow-to-page 1)         ;Go to next page.
10151         nil)
10152     ;; More in this page.
10153     (condition-case ()
10154         (scroll-up lines)
10155       (end-of-buffer
10156        ;; Long lines may cause an end-of-buffer error.
10157        (goto-char (point-max))))
10158     nil))
10159
10160 (defun gnus-article-prev-page (lines)
10161   "Show previous page of current article.
10162 Argument LINES specifies lines to be scrolled down."
10163   (interactive "P")
10164   (move-to-window-line 0)
10165   (if (and gnus-break-pages
10166            (bobp)
10167            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10168       (progn
10169         (gnus-narrow-to-page -1) ;Go to previous page.
10170         (goto-char (point-max))
10171         (recenter -1))
10172     (scroll-down lines)))
10173
10174 (defun gnus-article-refer-article ()
10175   "Read article specified by message-id around point."
10176   (interactive)
10177   (search-forward ">" nil t)    ;Move point to end of "<....>".
10178   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10179       (let ((message-id
10180              (buffer-substring (match-beginning 1) (match-end 1))))
10181         (set-buffer gnus-summary-buffer)
10182         (gnus-summary-refer-article message-id))
10183     (error "No references around point")))
10184
10185 (defun gnus-article-show-summary ()
10186   "Reconfigure windows to show summary buffer."
10187   (interactive)
10188   (gnus-configure-windows 'article)
10189   (gnus-summary-goto-subject gnus-current-article))
10190
10191 (defun gnus-article-describe-briefly ()
10192   "Describe article mode commands briefly."
10193   (interactive)
10194   (gnus-message 6
10195    (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")))
10196
10197 (defun gnus-article-summary-command ()
10198   "Execute the last keystroke in the summary buffer."
10199   (interactive)
10200   (let ((obuf (current-buffer))
10201         (owin (current-window-configuration))
10202         func)
10203     (switch-to-buffer gnus-summary-buffer 'norecord)
10204     (setq func (lookup-key (current-local-map) (this-command-keys)))
10205     (call-interactively func)
10206     (set-buffer obuf)
10207     (let ((npoint (point)))
10208       (set-window-configuration owin)
10209       (set-window-start (get-buffer-window (current-buffer)) (point)))))
10210
10211 (defun gnus-article-summary-command-nosave ()
10212   "Execute the last keystroke in the summary buffer."
10213   (interactive)
10214   (let ((obuf (current-buffer))
10215         (owin (current-window-configuration))
10216         func)
10217     (switch-to-buffer gnus-summary-buffer 'norecord)
10218     (setq func (lookup-key (current-local-map) (this-command-keys)))
10219     (call-interactively func)))
10220
10221 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10222 ;; Modified by tower@prep Nov 86
10223 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10224
10225 (defun gnus-caesar-region (&optional n)
10226   "Caesar rotation of region by N, default 13, for decrypting netnews.
10227 ROT47 will be performed for Japanese text in any case."
10228   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10229                    (list (prefix-numeric-value current-prefix-arg))
10230                  (list nil)))
10231   (cond ((not (numberp n)) (setq n 13))
10232         (t (setq n (mod n 26))))        ;canonicalize N
10233   (if (not (zerop n))           ; no action needed for a rot of 0
10234       (progn
10235         (if (or (not (boundp 'caesar-translate-table))
10236                 (not caesar-translate-table)
10237                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10238             (let ((i 0) 
10239                   (lower "abcdefghijklmnopqrstuvwxyz")
10240                   upper)
10241               (gnus-message 9 "Building caesar-translate-table...")
10242               (setq caesar-translate-table (make-vector 256 0))
10243               (while (< i 256)
10244                 (aset caesar-translate-table i i)
10245                 (setq i (1+ i)))
10246               (setq lower (concat lower lower)
10247                     upper (upcase lower)
10248                     i 0)
10249               (while (< i 26)
10250                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10251                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10252                 (setq i (1+ i)))
10253               ;; ROT47 for Japanese text.
10254               ;; Thanks to ichikawa@flab.fujitsu.junet.
10255               (setq i 161)
10256               (let ((t1 (logior ?O 128))
10257                     (t2 (logior ?! 128))
10258                     (t3 (logior ?~ 128)))
10259                 (while (< i 256)
10260                   (aset caesar-translate-table i
10261                         (let ((v (aref caesar-translate-table i)))
10262                           (if (<= v t1) (if (< v t2) v (+ v 47))
10263                             (if (<= v t3) (- v 47) v))))
10264                   (setq i (1+ i))))
10265               (gnus-message 9 "Building caesar-translate-table... done")))
10266         (let ((from (region-beginning))
10267               (to (region-end))
10268               (i 0) str len)
10269           (setq str (buffer-substring from to))
10270           (setq len (length str))
10271           (while (< i len)
10272             (aset str i (aref caesar-translate-table (aref str i)))
10273             (setq i (1+ i)))
10274           (goto-char from)
10275           (delete-region from to)
10276           (insert str)))))
10277
10278 \f
10279 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10280
10281 ;;;###autoload
10282 (defalias 'gnus-batch-kill 'gnus-batch-score)
10283 ;;;###autoload
10284 (defun gnus-batch-score ()
10285   "Run batched scoring.
10286 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10287 Newsgroups is a list of strings in Bnews format.  If you want to score
10288 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10289 score the alt hierarchy, you'd say \"!alt.all\"."
10290   (interactive)
10291   (let* ((yes-and-no
10292           (gnus-newsrc-parse-options
10293            (apply (function concat)
10294                   (mapcar (lambda (g) (concat g " "))
10295                           command-line-args-left))))
10296          (gnus-expert-user t)
10297          (nnmail-spool-file nil)
10298          (gnus-use-dribble-file nil)
10299          (yes (car yes-and-no))
10300          (no (cdr yes-and-no))
10301          group subscribed newsrc entry
10302          ;; Disable verbose message.
10303          gnus-novice-user gnus-large-newsgroup)
10304     ;; Eat all arguments.
10305     (setq command-line-args-left nil)
10306     ;; Start Gnus.
10307     (gnus)
10308     ;; Apply kills to specified newsgroups in command line arguments.
10309     (setq newsrc (cdr gnus-newsrc-alist))
10310     (while newsrc
10311       (setq group (car (car newsrc)))
10312       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10313       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10314                (and (car entry)
10315                     (or (eq (car entry) t)
10316                         (not (zerop (car entry)))))
10317                (if yes (string-match yes group) t)
10318                (or (null no) (not (string-match no group))))
10319           (progn
10320             (gnus-summary-read-group group nil t)
10321             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10322                  (gnus-summary-exit))))
10323       (setq newsrc (cdr newsrc)))
10324     ;; Exit Emacs.
10325     (switch-to-buffer gnus-group-buffer)
10326     (gnus-group-save-newsrc)))
10327
10328 (defun gnus-apply-kill-file ()
10329   "Apply a kill file to the current newsgroup.
10330 Returns the number of articles marked as read."
10331   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10332           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10333       (gnus-apply-kill-file-internal)
10334     0))
10335
10336 (defun gnus-kill-save-kill-buffer ()
10337   (save-excursion
10338     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10339       (if (get-file-buffer file)
10340           (progn
10341             (set-buffer (get-file-buffer file))
10342             (and (buffer-modified-p) (save-buffer))
10343             (kill-buffer (current-buffer)))))))
10344
10345 (defvar gnus-kill-file-name "KILL"
10346   "Suffix of the kill files.")
10347
10348 (defun gnus-newsgroup-kill-file (newsgroup)
10349   "Return the name of a kill file name for NEWSGROUP.
10350 If NEWSGROUP is nil, return the global kill file name instead."
10351   (cond ((or (null newsgroup)
10352              (string-equal newsgroup ""))
10353          ;; The global KILL file is placed at top of the directory.
10354          (expand-file-name gnus-kill-file-name
10355                            (or gnus-kill-files-directory "~/News")))
10356         ((gnus-use-long-file-name 'not-kill)
10357          ;; Append ".KILL" to newsgroup name.
10358          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10359                            (or gnus-kill-files-directory "~/News")))
10360         (t
10361          ;; Place "KILL" under the hierarchical directory.
10362          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10363                                    "/" gnus-kill-file-name)
10364                            (or gnus-kill-files-directory "~/News")))))
10365
10366 \f
10367 ;;;
10368 ;;; Dribble file
10369 ;;;
10370
10371 (defvar gnus-dribble-ignore nil)
10372
10373 (defun gnus-dribble-file-name ()
10374   (concat gnus-startup-file "-dribble"))
10375
10376 (defun gnus-dribble-open ()
10377   (save-excursion 
10378     (set-buffer 
10379      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10380     (buffer-disable-undo (current-buffer))
10381     (bury-buffer gnus-dribble-buffer)
10382     (auto-save-mode t)
10383     (goto-char (point-max))))
10384
10385 (defun gnus-dribble-enter (string)
10386   (if (and (not gnus-dribble-ignore)
10387            gnus-dribble-buffer
10388            (buffer-name gnus-dribble-buffer))
10389       (let ((obuf (current-buffer)))
10390         (set-buffer gnus-dribble-buffer)
10391         (insert string "\n")
10392         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10393         (set-buffer obuf))))
10394
10395 (defun gnus-dribble-read-file ()
10396   (let ((dribble-file (gnus-dribble-file-name)))
10397     (save-excursion 
10398       (set-buffer (setq gnus-dribble-buffer 
10399                         (get-buffer-create 
10400                          (file-name-nondirectory dribble-file))))
10401       (gnus-add-current-to-buffer-list)
10402       (erase-buffer)
10403       (set-visited-file-name dribble-file)
10404       (buffer-disable-undo (current-buffer))
10405       (bury-buffer (current-buffer))
10406       (set-buffer-modified-p nil)
10407       (let ((auto (make-auto-save-file-name))
10408             (gnus-dribble-ignore t))
10409         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10410             (progn
10411               (if (file-newer-than-file-p auto dribble-file)
10412                   (setq dribble-file auto))
10413               (insert-file-contents dribble-file)
10414               (if (not (zerop (buffer-size)))
10415                   (set-buffer-modified-p t))
10416               (if (gnus-y-or-n-p 
10417                    "Auto-save file exists. Do you want to read it? ")
10418                   (progn
10419                     (gnus-message 5 "Reading %s..." dribble-file) 
10420                     (eval-current-buffer)
10421                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10422
10423 (defun gnus-dribble-delete-file ()
10424   (if (file-exists-p (gnus-dribble-file-name))
10425       (delete-file (gnus-dribble-file-name)))
10426   (if gnus-dribble-buffer
10427       (save-excursion
10428         (set-buffer gnus-dribble-buffer)
10429         (let ((auto (make-auto-save-file-name)))
10430           (if (file-exists-p auto)
10431               (delete-file auto))
10432           (erase-buffer)
10433           (set-buffer-modified-p nil)))))
10434
10435 (defun gnus-dribble-save ()
10436   (if (and gnus-dribble-buffer
10437            (buffer-name gnus-dribble-buffer))
10438       (save-excursion
10439         (set-buffer gnus-dribble-buffer)
10440         (save-buffer))))
10441
10442 (defun gnus-dribble-clear ()
10443   (save-excursion
10444     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10445         (progn
10446           (set-buffer gnus-dribble-buffer)
10447           (erase-buffer)
10448           (set-buffer-modified-p nil)
10449           (setq buffer-saved-size (buffer-size))))))
10450
10451 ;;;
10452 ;;; Server Communication
10453 ;;;
10454
10455 ;; All the Gnus backends have the same interface, and should return
10456 ;; data in a similar format. Below is an overview of what functions
10457 ;; these packages must supply and what results they should return.
10458 ;;
10459 ;; Variables:
10460 ;;
10461 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10462 ;; buffer. 
10463 ;;
10464 ;; Functions for the imaginary backend `choke':
10465 ;;
10466 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10467 ;; Should return all headers for all ARTICLES, or return NOV lines for
10468 ;; the same.
10469 ;;
10470 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10471 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10472 ;; must be returned.
10473 ;;
10474 ;; `choke-close-group GROUP &optional SERVER'
10475 ;; Close group. Most backends won't have to do anything with this
10476 ;; call, but it is an opportunity to clean up, if that is needed. It
10477 ;; is called when Gnus exits a group.
10478 ;;
10479 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10480 ;; Return ARTICLE, which is either an article number or
10481 ;; message-id. Note that not all backends can return articles based on
10482 ;; message-id. 
10483 ;;
10484 ;; `choke-request-list SERVER'
10485 ;; Return a list of all newsgroups on SERVER.
10486 ;;
10487 ;; `choke-request-list-newsgroups SERVER'
10488 ;; Return a list of descriptions of all newsgroups on SERVER.
10489 ;;
10490 ;; `choke-request-newgroups DATE &optional SERVER'
10491 ;; Return a list of all groups that have arrived after DATE on
10492 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10493 ;; always check whether the groups are old or not. Backends that do
10494 ;; not store date information may just return the entire list of
10495 ;; groups, although this might not be a good idea in general.
10496 ;;
10497 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10498 ;; Should return a buffer that is suitable for "posting". nnspool and
10499 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10500 ;; buffer. This function should fill out the appropriate headers. 
10501 ;;
10502 ;; `choke-request-post &optional SERVER'
10503 ;; Function that will be called from a buffer to be posted. 
10504 ;;
10505 ;; `choke-open-server SERVER &optional ARGUMENT'
10506 ;; Open a connection to SERVER.
10507 ;;
10508 ;; `choke-close-server &optional SERVER'
10509 ;; Close the connection to SERVER.
10510 ;;
10511 ;; `choke-server-opened &optional SERVER'
10512 ;; Whether the conenction to SERVER is opened or not.
10513 ;;
10514 ;; `choke-server-status &optional SERVER'
10515 ;; Should return a status string (not in the nntp buffer, but as the
10516 ;; result of the function).
10517 ;;
10518 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10519 ;; Optional function for retrieving active file info on all groups in
10520 ;; GROUPS.  Two return formats are supported: The normal active file
10521 ;; format, and a list of GROUP lines.  This function should return (as
10522 ;; a function value) either `active' or `group', depending on what
10523 ;; format it returns.
10524 ;;
10525 ;; The following functions are optional and apply only to backends
10526 ;; that are able to control the contents of their groups totally
10527 ;; (ie. mail backends.)  Backends that aren't able to do that
10528 ;; shouldn't define these functions at all. Gnus will check for their
10529 ;; presence before attempting to call them.
10530 ;;
10531 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10532 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10533 ;; backends will not be able to expire articles. Should return a list
10534 ;; of all articles that were not expired.
10535 ;;
10536 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10537 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10538 ;; Removes any information it has added to the article (extra headers,
10539 ;; whatever - make it as clean as possible), and then passes the
10540 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
10541 ;; function described below. If the ACCEPT-FORM returns a non-nil
10542 ;; value, the article should then be deleted. If LAST is nil, that
10543 ;; means that there will be further calls to this function. This might
10544 ;; be taken as an advice not to save buffers/internal variables just
10545 ;; yet, but wait until the last call to speed things up.
10546 ;;
10547 ;; `choke-request-accept-article GROUP &optional LAST' 
10548 ;; The contents of the current buffer will be put into GROUP.  There
10549 ;; should, of course, be an article in the current buffer.  This
10550 ;; function is normally only called by the function described above,
10551 ;; and LAST works the same way as in that function.
10552 ;;
10553 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
10554 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
10555 ;; This provides an easy interface for allowing editing of
10556 ;; articles. Note that even headers may be edited, so the backend has
10557 ;; to update any tables (nov buffers, etc) that it maintains after
10558 ;; replacing the article.
10559 ;;
10560 ;; `choke-request-create-group GROUP &optional SERVER'
10561 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
10562 ;; might be a group that already exists, but hasn't been registered
10563 ;; yet. 
10564 ;;
10565 ;; All these functions must return nil if they couldn't service the
10566 ;; request. If the optional arguments are not supplied, some "current"
10567 ;; or "default" values should be used. In short, one should emulate an
10568 ;; NNTP server, in a way.
10569 ;;
10570 ;; If you want to write a new backend, you just have to supply the
10571 ;; functions listed above. In addition, you must enter the new backend
10572 ;; into the list of valid select methods:
10573 ;; (setq gnus-valid-select-methods 
10574 ;;       (cons '("choke" mail) gnus-valid-select-methods))
10575 ;; The first element in this list is the name of the backend. Other
10576 ;; elemnets may be `mail' (for mail groups),  `post' (for news
10577 ;; groups), `none' (neither), `respool' (for groups that can control
10578 ;; their contents). 
10579
10580 (defun gnus-start-news-server (&optional confirm)
10581   "Open a method for getting news.
10582 If CONFIRM is non-nil, the user will be asked for an NNTP server."
10583   (let (how where)
10584     (if gnus-current-select-method
10585         ;; Stream is already opened.
10586         nil
10587       ;; Open NNTP server.
10588       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
10589       (if confirm
10590           (progn
10591             ;; Read server name with completion.
10592             (setq gnus-nntp-server
10593                   (completing-read "NNTP server: "
10594                                    (mapcar (lambda (server) (list server))
10595                                            (cons (list gnus-nntp-server)
10596                                                  gnus-secondary-servers))
10597                                    nil nil gnus-nntp-server))))
10598
10599       (if (and gnus-nntp-server 
10600                (stringp gnus-nntp-server)
10601                (not (string= gnus-nntp-server "")))
10602           (setq gnus-select-method
10603                 (cond ((or (string= gnus-nntp-server "")
10604                            (string= gnus-nntp-server "::"))
10605                        (list 'nnspool (system-name)))
10606                       ((string-match "^:" gnus-nntp-server)
10607                        (list 'nnmh gnus-nntp-server 
10608                              (list 'nnmh-directory 
10609                                    (file-name-as-directory
10610                                     (expand-file-name
10611                                      (concat "~/" (substring
10612                                                    gnus-nntp-server 1)))))))
10613                       (t
10614                        (list 'nntp gnus-nntp-server)))))
10615
10616       (setq how (car gnus-select-method))
10617       (setq where (car (cdr gnus-select-method)))
10618       (cond ((eq how 'nnspool)
10619              (require 'nnspool)
10620              (gnus-message 5 "Looking up local news spool..."))
10621             ((eq how 'nnmh)
10622              (require 'nnmh)
10623              (gnus-message 5 "Looking up mh spool..."))
10624             (t
10625              (require 'nntp)))
10626       (setq gnus-current-select-method gnus-select-method)
10627       (run-hooks 'gnus-open-server-hook)
10628       (or 
10629        ;; gnus-open-server-hook might have opened it
10630        (gnus-server-opened gnus-select-method)  
10631        (gnus-open-server gnus-select-method)
10632        (gnus-y-or-n-p
10633         (format
10634          "%s server on %s can't be opened. Continue? "
10635          (car gnus-select-method) (nth 1 gnus-select-method)))
10636        (progn
10637          (gnus-message 1 "Couldn't open server on %s" 
10638                        (nth 1 gnus-select-method))
10639          (ding)
10640          nil)))))
10641
10642 (defun gnus-check-news-server (&optional method)
10643   "If the news server is down, start it up again."
10644   (let ((method (if method method gnus-select-method)))
10645     (and (stringp method)
10646          (setq method (gnus-server-to-method method)))
10647     (if (gnus-server-opened method)
10648         ;; Stream is already opened.
10649         t
10650       ;; Open server.
10651       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
10652       (run-hooks 'gnus-open-server-hook)
10653       (or (gnus-server-opened method)
10654           (gnus-open-server method))
10655       (message ""))))
10656
10657 (defun gnus-nntp-message (&optional message)
10658   "Check the status of the NNTP server.
10659 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
10660 is returned insted of the status string."
10661   (let ((status (gnus-status-message (gnus-find-method-for-group 
10662                                       gnus-newsgroup-name)))
10663         (message (or message "")))
10664     (if (and (stringp status) (> (length status) 0))
10665         status message)))
10666
10667 (defun gnus-get-function (method function)
10668   (and (stringp method)
10669        (setq method (gnus-server-to-method method)))
10670   (let ((func (intern (format "%s-%s" (car method) function))))
10671     (if (not (fboundp func)) 
10672         (progn
10673           (require (car method))
10674           (if (not (fboundp func)) 
10675               (error "No such function: %s" func))))
10676     func))
10677
10678 ;;; Interface functions to the backends.
10679
10680 (defun gnus-open-server (method)
10681   (funcall (gnus-get-function method 'open-server)
10682            (nth 1 method) (nthcdr 2 method)))
10683
10684 (defun gnus-close-server (method)
10685   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
10686
10687 (defun gnus-request-list (method)
10688   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
10689
10690 (defun gnus-request-list-newsgroups (method)
10691   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
10692
10693 (defun gnus-request-newgroups (date method)
10694   (funcall (gnus-get-function method 'request-newgroups) 
10695            date (nth 1 method)))
10696
10697 (defun gnus-server-opened (method)
10698   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
10699
10700 (defun gnus-status-message (method)
10701   (let ((method (if (stringp method) (gnus-find-method-for-group method)
10702                   method)))
10703     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
10704
10705 (defun gnus-request-group (group &optional dont-check)
10706   (let ((method (gnus-find-method-for-group group)))
10707     (funcall (gnus-get-function method 'request-group) 
10708              (gnus-group-real-name group) (nth 1 method) dont-check)))
10709
10710 (defun gnus-request-asynchronous (group &optional articles)
10711   (let ((method (gnus-find-method-for-group group)))
10712     (funcall (gnus-get-function method 'request-asynchronous) 
10713              (gnus-group-real-name group) (nth 1 method) articles)))
10714
10715 (defun gnus-list-active-group (group)
10716   (let ((method (gnus-find-method-for-group group))
10717         (func 'list-active-group))
10718     (and (gnus-check-backend-function func group)
10719          (funcall (gnus-get-function method func) 
10720                   (gnus-group-real-name group) (nth 1 method)))))
10721
10722 (defun gnus-request-group-description (group)
10723   (let ((method (gnus-find-method-for-group group))
10724         (func 'request-group-description))
10725     (and (gnus-check-backend-function func group)
10726          (funcall (gnus-get-function method func) 
10727                   (gnus-group-real-name group) (nth 1 method)))))
10728
10729 (defun gnus-close-group (group)
10730   (let ((method (gnus-find-method-for-group group)))
10731     (funcall (gnus-get-function method 'close-group) 
10732              (gnus-group-real-name group) (nth 1 method))))
10733
10734 (defun gnus-retrieve-headers (articles group)
10735   (let ((method (gnus-find-method-for-group group)))
10736     (if gnus-use-cache
10737         (gnus-cache-retrieve-headers articles group)
10738       (funcall (gnus-get-function method 'retrieve-headers) 
10739                articles (gnus-group-real-name group) (nth 1 method)))))
10740
10741 (defun gnus-retrieve-groups (groups method)
10742   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
10743
10744 (defun gnus-request-article (article group &optional buffer)
10745   (let ((method (gnus-find-method-for-group group)))
10746     (funcall (gnus-get-function method 'request-article) 
10747              article (gnus-group-real-name group) (nth 1 method) buffer)))
10748
10749 (defun gnus-request-head (article group)
10750   (let ((method (gnus-find-method-for-group group)))
10751     (funcall (gnus-get-function method 'request-head) 
10752              article (gnus-group-real-name group) (nth 1 method))))
10753
10754 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10755 (defun gnus-request-post-buffer (post group subject header artbuf
10756                                       info follow-to respect-poster)
10757    (let* ((info (or info (and group (nth 2 (gnus-gethash 
10758                                             group gnus-newsrc-hashtb)))))
10759           (method
10760            (if (and gnus-post-method
10761                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10762                     (memq 'post (assoc
10763                                  (format "%s" (car (gnus-find-method-for-group
10764                                                     gnus-newsgroup-name)))
10765                                         gnus-valid-select-methods)))
10766                gnus-post-method
10767              (gnus-find-method-for-group gnus-newsgroup-name))))
10768      (or (gnus-server-opened method)
10769          (gnus-open-server method)
10770          (error "Can't open server %s:%s" (car method) (nth 1 method)))
10771      (let ((mail-self-blind nil)
10772            (mail-archive-file-name nil))
10773        (funcall (gnus-get-function method 'request-post-buffer) 
10774                 post group subject header artbuf info follow-to
10775                 respect-poster))))
10776
10777 (defun gnus-request-post (method &optional force)
10778   (and (stringp method)
10779        (setq method (gnus-server-to-method method)))
10780   (and (not force) gnus-post-method
10781        (memq 'post (assoc (format "%s" (car method))
10782                           gnus-valid-select-methods))
10783        (setq method gnus-post-method))
10784   (funcall (gnus-get-function method 'request-post) 
10785            (nth 1 method)))
10786
10787 (defun gnus-request-expire-articles (articles group &optional force)
10788   (let ((method (gnus-find-method-for-group group)))
10789     (funcall (gnus-get-function method 'request-expire-articles) 
10790              articles (gnus-group-real-name group) (nth 1 method)
10791              force)))
10792
10793 (defun gnus-request-move-article 
10794   (article group server accept-function &optional last)
10795   (let ((method (gnus-find-method-for-group group)))
10796     (funcall (gnus-get-function method 'request-move-article) 
10797              article (gnus-group-real-name group) 
10798              (nth 1 method) accept-function last)))
10799
10800 (defun gnus-request-accept-article (group &optional last)
10801   (let ((func (if (symbolp group) group
10802                 (car (gnus-find-method-for-group group)))))
10803     (funcall (intern (format "%s-request-accept-article" func))
10804              (if (stringp group) (gnus-group-real-name group) group)
10805              last)))
10806
10807 (defun gnus-request-replace-article (article group buffer)
10808   (let ((func (car (gnus-find-method-for-group group))))
10809     (funcall (intern (format "%s-request-replace-article" func))
10810              article (gnus-group-real-name group) buffer)))
10811
10812 (defun gnus-request-create-group (group)
10813   (let ((method (gnus-find-method-for-group group)))
10814     (funcall (gnus-get-function method 'request-create-group) 
10815              (gnus-group-real-name group) (nth 1 method))))
10816
10817 (defun gnus-member-of-valid (symbol group)
10818   (memq symbol (assoc
10819                 (format "%s" (car (gnus-find-method-for-group group)))
10820                 gnus-valid-select-methods)))
10821
10822 (defun gnus-find-method-for-group (group &optional info)
10823   (or gnus-override-method
10824       (and (not group)
10825            gnus-select-method)
10826       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
10827             method)
10828         (if (or (not info)
10829                 (not (setq method (nth 4 info))))
10830             (setq method gnus-select-method)
10831           (setq method
10832                 (cond ((stringp method)
10833                        (gnus-server-to-method method))
10834                       ((stringp (car method))
10835                        (gnus-server-extend-method group method))
10836                       (t
10837                        method))))
10838         (gnus-server-add-address method))))
10839
10840 (defun gnus-check-backend-function (func group)
10841   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
10842                  group)))
10843     (fboundp (intern (format "%s-%s" method func)))))
10844
10845 (defun gnus-methods-using (method)
10846   (let ((valids gnus-valid-select-methods)
10847         outs)
10848     (while valids
10849       (if (memq method (car valids)) 
10850           (setq outs (cons (car valids) outs)))
10851       (setq valids (cdr valids)))
10852     outs))
10853
10854 ;;; 
10855 ;;; Active & Newsrc File Handling
10856 ;;;
10857
10858 ;; Newsrc related functions.
10859 ;; Gnus internal format of gnus-newsrc-alist:
10860 ;; (("alt.general" 3 (1 . 1))
10861 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
10862 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
10863 ;; The first item is the group name; the second is the subscription
10864 ;; level; the third is either a range of a list of ranges of read
10865 ;; articles, the optional fourth element is a list of marked articles,
10866 ;; the optional fifth element is the select method.
10867 ;;
10868 ;; Gnus internal format of gnus-newsrc-hashtb:
10869 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
10870 ;; This is the entry for "alt.misc". The first element is the number
10871 ;; of unread articles in "alt.misc". The cdr of this entry is the
10872 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
10873 ;; trivial to remove or add new elements into gnus-newsrc-alist
10874 ;; without scanning the entire list. So, to get the actual information
10875 ;; of "alt.misc", you'd say something like 
10876 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
10877 ;;
10878 ;; Gnus internal format of gnus-active-hashtb:
10879 ;; ((1 . 1))
10880 ;;  (5 . 10))
10881 ;;  (67 . 99)) ...)
10882 ;; The only element in each entry in this hash table is a range of
10883 ;; (possibly) available articles. (Articles in this range may have
10884 ;; been expired or cancelled.)
10885 ;;
10886 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
10887 ;; ("alt.misc" "alt.test" "alt.general" ...)
10888
10889 (defun gnus-setup-news (&optional rawfile level)
10890   "Setup news information.
10891 If RAWFILE is non-nil, the .newsrc file will also be read.
10892 If LEVEL is non-nil, the news will be set up at level LEVEL."
10893   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
10894     ;; Clear some variables to re-initialize news information.
10895     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
10896
10897     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
10898     (if init (gnus-read-newsrc-file rawfile))
10899
10900     ;; Read the active file and create `gnus-active-hashtb'.
10901     ;; If `gnus-read-active-file' is nil, then we just create an empty
10902     ;; hash table. The partial filling out of the hash table will be
10903     ;; done in `gnus-get-unread-articles'.
10904     (if (and gnus-read-active-file 
10905              (not level)
10906              (gnus-server-opened gnus-select-method))
10907         (gnus-read-active-file)
10908       (setq gnus-active-hashtb (make-vector 4095 0)))
10909
10910     (and init gnus-use-dribble-file (gnus-dribble-read-file))
10911
10912     ;; Find the number of unread articles in each non-dead group.
10913     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
10914     ;; Find new newsgroups and treat them.
10915     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
10916              (gnus-server-opened gnus-select-method))
10917         (gnus-find-new-newsgroups))
10918     (if (and init gnus-check-bogus-newsgroups 
10919              gnus-read-active-file (not level)
10920              (gnus-server-opened gnus-select-method))
10921         (gnus-check-bogus-newsgroups))))
10922
10923 (defun gnus-find-new-newsgroups ()
10924   "Search for new newsgroups and add them.
10925 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
10926 The `-n' option line from .newsrc is respected."
10927   (interactive)
10928   (or (gnus-check-first-time-used)
10929       (if (or (consp gnus-check-new-newsgroups)
10930               (eq gnus-check-new-newsgroups 'ask-server))
10931           (gnus-ask-server-for-new-groups)
10932         (let ((groups 0)
10933               group new-newsgroups)
10934           (or gnus-have-read-active-file (gnus-read-active-file))
10935           (setq gnus-newsrc-last-checked-date (current-time-string))
10936           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
10937           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
10938           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
10939           (mapatoms
10940            (lambda (sym)
10941              (setq group (symbol-name sym))
10942              (if (or (gnus-gethash group gnus-killed-hashtb)
10943                      (gnus-gethash group gnus-newsrc-hashtb))
10944                  ()
10945                (let ((do-sub (gnus-matches-options-n group)))
10946                  (cond ((eq do-sub 'subscribe)
10947                         (setq groups (1+ groups))
10948                         (gnus-sethash group group gnus-killed-hashtb)
10949                         (funcall 
10950                          gnus-subscribe-options-newsgroup-method group))
10951                        ((eq do-sub 'ignore)
10952                         nil)
10953                        (t
10954                         (setq groups (1+ groups))
10955                         (gnus-sethash group group gnus-killed-hashtb)
10956                         (if gnus-subscribe-hierarchical-interactive
10957                             (setq new-newsgroups (cons group new-newsgroups))
10958                           (funcall gnus-subscribe-newsgroup-method group)))))))
10959            gnus-active-hashtb)
10960           (if new-newsgroups 
10961               (gnus-subscribe-hierarchical-interactive new-newsgroups))
10962           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10963           (if (> groups 0)
10964               (gnus-message 6 "%d new newsgroup%s arrived." 
10965                             groups (if (> groups 1) "s have" " has")))))))
10966
10967 (defun gnus-matches-options-n (group)
10968   ;; Returns `subscribe' if the group is to be uncoditionally
10969   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
10970   ;; no match for the group.
10971
10972   ;; First we check the two user variables.
10973   (cond
10974    ((and gnus-options-subscribe
10975          (string-match gnus-options-not-subscribe group))
10976     'subscribe)
10977    ((and gnus-options-not-subscribe
10978          (string-match gnus-options-subscribe group))
10979     'ignore)
10980    ;; Then we go through the list that was retrieved from the .newsrc
10981    ;; file.  This list has elements on the form 
10982    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
10983    ;; is in the reverse order of the options line) is returned.
10984    (t
10985     (let ((regs gnus-newsrc-options-n))
10986       (while (and regs
10987                   (not (string-match (car (car regs)) group)))
10988         (setq regs (cdr regs)))
10989       (and regs (cdr (car regs)))))))
10990
10991 (defun gnus-ask-server-for-new-groups ()
10992   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
10993          (methods (cons gnus-select-method 
10994                         (append
10995                          (and (consp gnus-check-new-newsgroups)
10996                               gnus-check-new-newsgroups)
10997                          gnus-secondary-select-methods)))
10998          (groups 0)
10999          (new-date (current-time-string))
11000          hashtb group new-newsgroups got-new)
11001     ;; Go thorugh both primary and secondary select methods and
11002     ;; request new newsgroups.  
11003     (while methods
11004       (if (gnus-request-newgroups date (car methods))
11005           (save-excursion
11006             (setq got-new t)
11007             (or hashtb (setq hashtb (gnus-make-hashtable 
11008                                      (count-lines (point-min) (point-max)))))
11009             (set-buffer nntp-server-buffer)
11010             ;; Enter all the new groups in a hashtable.
11011             (gnus-active-to-gnus-format (car methods) hashtb)))
11012       (setq methods (cdr methods)))
11013     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11014     ;; Now all new groups from all select methods are in `hashtb'.
11015     (mapatoms
11016      (lambda (group-sym)
11017        (setq group (symbol-name group-sym))
11018        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11019                (member group gnus-zombie-list)
11020                (member group gnus-killed-list))
11021            ;; The group is already known.
11022            ()
11023          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
11024          (let ((do-sub (gnus-matches-options-n group)))
11025            (cond ((eq do-sub 'subscribe)
11026                   (setq groups (1+ groups))
11027                   (gnus-sethash group group gnus-killed-hashtb)
11028                   (funcall 
11029                    gnus-subscribe-options-newsgroup-method group))
11030                  ((eq do-sub 'ignore)
11031                   nil)
11032                  (t
11033                   (setq groups (1+ groups))
11034                   (gnus-sethash group group gnus-killed-hashtb)
11035                   (if gnus-subscribe-hierarchical-interactive
11036                       (setq new-newsgroups (cons group new-newsgroups))
11037                     (funcall gnus-subscribe-newsgroup-method group)))))))
11038      hashtb)
11039     (if new-newsgroups 
11040         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11041     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11042     (if (> groups 0)
11043         (gnus-message 6 "%d new newsgroup%s arrived." 
11044                       groups (if (> groups 1) "s have" " has")))
11045     got-new))
11046
11047 (defun gnus-check-first-time-used ()
11048   (if (or (> (length gnus-newsrc-alist) 1)
11049           (file-exists-p gnus-startup-file)
11050           (file-exists-p (concat gnus-startup-file ".el"))
11051           (file-exists-p (concat gnus-startup-file ".eld")))
11052       nil
11053     (gnus-message 6 "First time user; subscribing you to default groups")
11054     (or gnus-have-read-active-file (gnus-read-active-file))
11055     (setq gnus-newsrc-last-checked-date (current-time-string))
11056     (let ((groups gnus-default-subscribed-newsgroups)
11057           group)
11058       (if (eq groups t)
11059           nil
11060         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11061         (mapatoms
11062          (lambda (sym)
11063            (setq group (symbol-name sym))
11064            (let ((do-sub (gnus-matches-options-n group)))
11065              (cond ((eq do-sub 'subscribe)
11066                     (gnus-sethash group group gnus-killed-hashtb)
11067                     (funcall 
11068                      gnus-subscribe-options-newsgroup-method group))
11069                    ((eq do-sub 'ignore)
11070                     nil)
11071                    (t
11072                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11073          gnus-active-hashtb)
11074         (while groups
11075           (if (gnus-gethash (car groups) gnus-active-hashtb)
11076               (gnus-group-change-level 
11077                (car groups) gnus-level-default-subscribed gnus-level-killed))
11078           (setq groups (cdr groups)))
11079         (gnus-group-make-help-group)
11080         (and gnus-novice-user
11081              (gnus-message 7 "`A k' to list killed groups"))))))
11082
11083 ;; `gnus-group-change-level' is the fundamental function for changing
11084 ;; subscription levels of newsgroups. This might mean just changing
11085 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11086 ;; again, which subscribes/unsubscribes a group, which is equally
11087 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11088 ;; from 8-9 to 1-7 means that you remove the group from the list of
11089 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11090 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11091 ;; which is trivial.
11092 ;; ENTRY can either be a string (newsgroup name) or a list (if
11093 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11094 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11095 ;; entries. 
11096 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11097 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11098 ;; after. 
11099 (defun gnus-group-change-level (entry level &optional oldlevel
11100                                       previous fromkilled)
11101   (let ((pinfo entry)
11102         group info active num)
11103     ;; Glean what info we can from the arguments
11104     (if (consp entry)
11105         (if fromkilled (setq group (nth 1 entry))
11106           (setq group (car (nth 2 entry))))
11107       (setq group entry))
11108     (if (and (stringp entry)
11109              oldlevel 
11110              (< oldlevel gnus-level-zombie))
11111         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11112     (if (and (not oldlevel)
11113              (listp entry))
11114         (setq oldlevel (car (cdr (nth 2 entry)))))
11115     (if (stringp previous)
11116         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11117
11118     (gnus-dribble-enter
11119      (format "(gnus-group-change-level %S %S %S %S %S)" 
11120              group level oldlevel (car (nth 2 previous)) fromkilled))
11121     
11122     ;; Then we remove the newgroup from any old structures, if needed.
11123     ;; If the group was killed, we remove it from the killed or zombie
11124     ;; list. If not, and it is in fact going to be killed, we remove
11125     ;; it from the newsrc hash table and assoc.
11126     (cond ((>= oldlevel gnus-level-zombie)
11127            (if (= oldlevel gnus-level-zombie)
11128                (setq gnus-zombie-list (delete group gnus-zombie-list))
11129              (setq gnus-killed-list (delete group gnus-killed-list))))
11130           (t
11131            (if (>= level gnus-level-zombie)
11132                (progn
11133                  (gnus-sethash (car (nth 2 entry))
11134                                nil gnus-newsrc-hashtb)
11135                  (if (nth 3 entry)
11136                      (setcdr (gnus-gethash (car (nth 3 entry))
11137                                            gnus-newsrc-hashtb)
11138                              (cdr entry)))
11139                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11140
11141     ;; Finally we enter (if needed) the list where it is supposed to
11142     ;; go, and change the subscription level. If it is to be killed,
11143     ;; we enter it into the killed or zombie list.
11144     (cond ((>= level gnus-level-zombie)
11145            (and (string= group (gnus-group-real-name group))
11146                 (if (= level gnus-level-zombie)
11147                     (setq gnus-zombie-list (cons group gnus-zombie-list))
11148                   (setq gnus-killed-list (cons group gnus-killed-list)))))
11149           (t
11150            ;; If the list is to be entered into the newsrc assoc, and
11151            ;; it was killed, we have to create an entry in the newsrc
11152            ;; hashtb format and fix the pointers in the newsrc assoc.
11153            (if (>= oldlevel gnus-level-zombie)
11154                (progn
11155                  (if (listp entry)
11156                      (progn
11157                        (setq info (cdr entry))
11158                        (setq num (car entry)))
11159                    (setq active (gnus-gethash group gnus-active-hashtb))
11160                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11161                    ;; Check whether the group is foreign. If so, the
11162                    ;; foreign select method has to be entered into the
11163                    ;; info. 
11164                    (let ((method (gnus-group-method-name group)))
11165                      (if (eq method gnus-select-method)
11166                          (setq info (list group level nil))
11167                        (setq info (list group level nil nil method)))))
11168                  (or previous 
11169                      (setq previous 
11170                            (let ((p gnus-newsrc-alist))
11171                              (while (cdr (cdr p))
11172                                (setq p (cdr p)))
11173                              p)))
11174                  (setq entry (cons info (cdr (cdr previous))))
11175                  (if (cdr previous)
11176                      (progn
11177                        (setcdr (cdr previous) entry)
11178                        (gnus-sethash group (cons num (cdr previous)) 
11179                                      gnus-newsrc-hashtb))
11180                    (setcdr previous entry)
11181                    (gnus-sethash group (cons num previous)
11182                                  gnus-newsrc-hashtb))
11183                  (if (cdr entry)
11184                      (setcdr (gnus-gethash (car (car (cdr entry)))
11185                                            gnus-newsrc-hashtb)
11186                              entry)))
11187              ;; It was alive, and it is going to stay alive, so we
11188              ;; just change the level and don't change any pointers or
11189              ;; hash table entries.
11190              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11191
11192 (defun gnus-kill-newsgroup (newsgroup)
11193   "Obsolete function. Kills a newsgroup."
11194   (gnus-group-change-level
11195    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11196
11197 (defun gnus-check-bogus-newsgroups (&optional confirm)
11198   "Remove bogus newsgroups.
11199 If CONFIRM is non-nil, the user has to confirm the deletion of every
11200 newsgroup." 
11201   (let ((newsrc (cdr gnus-newsrc-alist))
11202         bogus group)
11203     (gnus-message 5 "Checking bogus newsgroups...")
11204     (or gnus-have-read-active-file (gnus-read-active-file))
11205     ;; Find all bogus newsgroup that are subscribed.
11206     (while newsrc
11207       (setq group (car (car newsrc)))
11208       (if (or (gnus-gethash group gnus-active-hashtb)
11209               (nth 4 (car newsrc))
11210               (and confirm
11211                    (not (gnus-y-or-n-p
11212                          (format "Remove bogus newsgroup: %s " group)))))
11213           ;; Active newsgroup.
11214           ()
11215         ;; Found a bogus newsgroup.
11216         (setq bogus (cons group bogus)))
11217       (setq newsrc (cdr newsrc)))
11218     ;; Remove all bogus subscribed groups by first killing them, and
11219     ;; then removing them from the list of killed groups.
11220     (while bogus
11221       (gnus-group-change-level 
11222        (gnus-gethash (car bogus) gnus-newsrc-hashtb) gnus-level-killed)
11223       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
11224       (setq bogus (cdr bogus)))
11225     ;; Then we remove all bogus groups from the list of killed and
11226     ;; zombie groups. They are are removed without confirmation.
11227     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11228           killed)
11229       (while dead-lists
11230         (setq killed (symbol-value (car dead-lists)))
11231         (while killed
11232           (setq group (car killed))
11233           (or (gnus-gethash group gnus-active-hashtb)
11234               ;; The group is bogus.
11235               (set (car dead-lists)
11236                    (delete group (symbol-value (car dead-lists)))))
11237           (setq killed (cdr killed)))
11238         (setq dead-lists (cdr dead-lists))))
11239     (gnus-message 5 "Checking bogus newsgroups... done")))
11240
11241 (defun gnus-check-duplicate-killed-groups ()
11242   "Remove duplicates from the list of killed groups."
11243   (interactive)
11244   (let ((killed gnus-killed-list))
11245     (while killed
11246       (gnus-message 9 "%d" (length killed))
11247       (setcdr killed (delete (car killed) (cdr killed)))
11248       (setq killed (cdr killed)))))
11249
11250 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11251 ;; and compute how many unread articles there are in each group.
11252 (defun gnus-get-unread-articles (&optional level)
11253   (let* ((newsrc (cdr gnus-newsrc-alist))
11254          (conditional level)
11255          (level (or level (1+ gnus-level-subscribed)))
11256          info group active virtuals method)
11257     (gnus-message 5 "Checking new news...")
11258     (while newsrc
11259       (setq info (car newsrc))
11260       (setq group (car info))
11261       (setq active (gnus-gethash group gnus-active-hashtb))
11262
11263       ;; Check newsgroups. If the user doesn't want to check them, or
11264       ;; they can't be checked (for instance, if the news server can't
11265       ;; be reached) we just set the number of unread articles in this
11266       ;; newsgroup to t. This means that Gnus thinks that there are
11267       ;; unread articles, but it has no idea how many.
11268       (if (and (setq method (nth 4 info))
11269                (not (gnus-server-equal gnus-select-method
11270                                        (gnus-server-get-method nil method))))
11271           (if (or (and gnus-activate-foreign-newsgroups 
11272                        (not (numberp gnus-activate-foreign-newsgroups)))
11273                   (and (numberp gnus-activate-foreign-newsgroups)
11274                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
11275                        (<= (nth 1 info) level)))
11276               (if (eq (car (if (stringp method) 
11277                                (gnus-server-to-method method)
11278                              (nth 4 info))) 'nnvirtual)
11279                   (setq virtuals (cons info virtuals))
11280                 (setq active (gnus-activate-newsgroup (car info)))))
11281         (if (and (not gnus-read-active-file)
11282                  (<= (nth 1 info) level))
11283             (progn
11284               (setq active (gnus-activate-newsgroup (car info))))))
11285       
11286       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
11287                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
11288       (and active 
11289            (gnus-get-unread-articles-in-group info active)
11290            ;; Close the groups as we look at them!
11291            (gnus-close-group group))
11292       (setq newsrc (cdr newsrc)))
11293
11294     ;; Activate the virtual groups. This has to be done after all the
11295     ;; other groups. 
11296     ;; !!! If one virtual group contains another virtual group, even
11297     ;; doing it this way might cause problems.
11298    (while virtuals
11299       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11300            (gnus-get-unread-articles-in-group (car virtuals) active))
11301       (setq virtuals (cdr virtuals)))
11302
11303     (gnus-message 5 "Checking new news... done")))
11304
11305 ;; Create a hash table out of the newsrc alist. The `car's of the
11306 ;; alist elements are used as keys.
11307 (defun gnus-make-hashtable-from-newsrc-alist ()
11308   (let ((alist gnus-newsrc-alist)
11309          prev)
11310     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11311     (setq alist 
11312           (setq prev (setq gnus-newsrc-alist 
11313                            (cons (list "dummy.group" 0 nil) alist))))
11314     (while alist
11315       (gnus-sethash (car (car alist)) (cons nil prev) gnus-newsrc-hashtb)
11316       (setq prev alist)
11317       (setq alist (cdr alist)))))
11318
11319 (defun gnus-make-hashtable-from-killed ()
11320   "Create a hash table from the killed and zombie lists."
11321   (let ((lists '(gnus-killed-list gnus-zombie-list))
11322         list)
11323     (setq gnus-killed-hashtb 
11324           (gnus-make-hashtable 
11325            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11326     (while lists
11327       (setq list (symbol-value (car lists)))
11328       (setq lists (cdr lists))
11329       (while list
11330         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11331         (setq list (cdr list))))))
11332
11333 (defun gnus-get-unread-articles-in-group (info active)
11334   (let* ((range (nth 2 info))
11335          (num 0)
11336          (marked (nth 3 info))
11337          srange lowest group highest)
11338     ;; If a cache is present, we may have to alter the active info.
11339     (and gnus-use-cache
11340          (gnus-cache-possibly-alter-active (car info) active))
11341     ;; Modify the list of read articles according to what articles 
11342     ;; are available; then tally the unread articles and add the
11343     ;; number to the group hash table entry.
11344     (cond ((zerop (cdr active))
11345            (setq num 0))
11346           ((not range)
11347            (setq num (- (1+ (cdr active)) (car active))))
11348           ((not (listp (cdr range)))
11349            ;; Fix a single (num . num) range according to the
11350            ;; active hash table.
11351            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11352            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11353            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11354            ;; Compute number of unread articles.
11355            (setq num (max 0 (- (cdr active) 
11356                                (- (1+ (cdr range)) (car range))))))
11357           (t
11358            ;; The read list is a list of ranges. Fix them according to
11359            ;; the active hash table.
11360            ;; First peel off any elements that are below the lower
11361            ;; active limit. 
11362            (while (and (cdr range) 
11363                        (>= (car active) 
11364                            (or (and (atom (car (cdr range))) (car (cdr range)))
11365                                (car (car (cdr range))))))
11366              (if (numberp (car range))
11367                  (setcar range 
11368                          (cons (car range) 
11369                                (or (and (numberp (car (cdr range)))
11370                                         (car (cdr range))) 
11371                                    (cdr (car (cdr range))))))
11372                (setcdr (car range) 
11373                        (or (and (numberp (nth 1 range)) (nth 1 range))
11374                            (cdr (car (cdr range))))))
11375              (setcdr range (cdr (cdr range))))
11376            ;; Adjust the first element to be the same as the lower limit. 
11377            (if (and (not (atom (car range))) 
11378                     (< (cdr (car range)) (car active)))
11379                (setcdr (car range) (1- (car active))))
11380            ;; Then we want to peel off any elements that are higher
11381            ;; than the upper active limit.  
11382            (let ((srange range))
11383              ;; Go past all legal elements.
11384              (while (and (cdr srange) 
11385                          (<= (or (and (atom (car (cdr srange)))
11386                                       (car (cdr srange)))
11387                                  (car (car (cdr srange)))) (cdr active)))
11388                (setq srange (cdr srange)))
11389              (if (cdr srange)
11390                  ;; Nuke all remaining illegal elements.
11391                  (setcdr srange nil))
11392
11393              ;; Adjust the final element.
11394              (if (and (not (atom (car srange)))
11395                       (> (cdr (car srange)) (cdr active)))
11396                  (setcdr (car srange) (cdr active))))
11397            ;; Compute the number of unread articles.
11398            (while range
11399              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11400                                          (cdr (car range))))
11401                                  (or (and (atom (car range)) (car range))
11402                                      (car (car range))))))
11403              (setq range (cdr range)))
11404            (setq num (max 0 (- (cdr active) num)))))
11405     (and info
11406          (progn
11407            (and (assq 'tick marked)
11408                 (inline (gnus-remove-illegal-marked-articles
11409                          (assq 'tick marked) (nth 2 info))))
11410            (and (assq 'dormant marked)
11411                 (inline (gnus-remove-illegal-marked-articles
11412                          (assq 'dormant marked) (nth 2 info))))
11413            (setcar
11414             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11415             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11416                                 (length (cdr (assq 'dormant marked)))))))))
11417     num))
11418
11419 (defun gnus-remove-illegal-marked-articles (marked ranges)
11420   (let ((m (cdr marked)))
11421     ;; Make sure that all ticked articles are a subset of the unread
11422     ;; articles. 
11423     (while m
11424       (if (gnus-member-of-range (car m) ranges)
11425           (setcdr marked (cdr m))
11426         (setq marked m))
11427       (setq m (cdr m)))))
11428
11429 (defun gnus-activate-newsgroup (group)
11430   (let ((method (gnus-find-method-for-group group))
11431         active)
11432     (and (or (gnus-server-opened method) (gnus-open-server method))
11433          (gnus-request-group group)
11434          (save-excursion
11435            (set-buffer nntp-server-buffer)
11436            (goto-char (point-min))
11437            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11438                 (progn
11439                   (goto-char (match-beginning 1))
11440                   (gnus-sethash 
11441                    group (setq active (cons (read (current-buffer))
11442                                             (read (current-buffer))))
11443                    gnus-active-hashtb))
11444                 active)))))
11445
11446 (defun gnus-update-read-articles 
11447   (group unread unselected ticked &optional domarks replied expirable killed
11448          dormant bookmark score)
11449   "Update the list of read and ticked articles in GROUP using the
11450 UNREAD and TICKED lists.
11451 Note: UNSELECTED has to be sorted over `<'.
11452 Returns whether the updating was successful."
11453   (let* ((active (or gnus-newsgroup-active 
11454                      (gnus-gethash group gnus-active-hashtb)))
11455          (entry (gnus-gethash group gnus-newsrc-hashtb))
11456          (number (car entry))
11457          (info (nth 2 entry))
11458          (marked (nth 3 info))
11459          (prev 1)
11460          (unread (sort (copy-sequence unread) (function <)))
11461          last read)
11462     (if (or (not info) (not active))
11463         ;; There is no info on this group if it was, in fact,
11464         ;; killed. Gnus stores no information on killed groups, so
11465         ;; there's nothing to be done. 
11466         ;; One could store the information somewhere temporarily,
11467         ;; perhaps... Hmmm... 
11468         ()
11469       ;; Remove any negative articles numbers.
11470       (while (and unread (< (car unread) 0))
11471         (setq unread (cdr unread)))
11472       ;; Remove any expired article numbers
11473       (while (and unread (< (car unread) (car active)))
11474         (setq unread (cdr unread)))
11475       (while (and ticked (< (car ticked) (car active)))
11476         (setq ticked (cdr ticked)))
11477       (while (and dormant (< (car dormant) (car active)))
11478         (setq dormant (cdr dormant)))
11479       (setq unread (sort (append unselected unread) '<))
11480       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11481       (setcar entry (max 0 (- (length unread) (length ticked) 
11482                               (length dormant))))
11483       ;; Compute the ranges of read articles by looking at the list of
11484       ;; unread articles.  
11485       (while unread
11486         (if (/= (car unread) prev)
11487             (setq read (cons (if (= prev (1- (car unread))) prev
11488                                (cons prev (1- (car unread)))) read)))
11489         (setq prev (1+ (car unread)))
11490         (setq unread (cdr unread)))
11491       (if (<= prev (cdr active))
11492           (setq read (cons (cons prev (cdr active)) read)))
11493       ;; Enter this list into the group info.
11494       (setcar (cdr (cdr info)) 
11495               (if (> (length read) 1) (nreverse read) read))
11496       ;; Enter the list of ticked articles.
11497       (gnus-set-marked-articles 
11498        info ticked
11499        (if domarks replied (cdr (assq 'reply marked)))
11500        (if domarks expirable (cdr (assq 'expire marked)))
11501        (if domarks killed (cdr (assq 'killed marked)))
11502        (if domarks dormant (cdr (assq 'dormant marked)))
11503        (if domarks bookmark (cdr (assq 'bookmark marked)))
11504        (if domarks score (cdr (assq 'score marked))))
11505       t)))
11506
11507 (defun gnus-make-articles-unread (group articles)
11508   "Mark ARTICLES in GROUP as unread."
11509   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
11510                           (gnus-gethash (gnus-group-real-name group)
11511                                         gnus-newsrc-hashtb))))
11512          (ranges (nth 2 info))
11513          news)
11514     (while articles
11515       (and (gnus-member-of-range (car articles) ranges)
11516            (setq news (cons (car articles) news)))
11517       (setq articles (cdr articles)))
11518     (if (not news)
11519         ()
11520       (setcar (nthcdr 2 info)
11521               (gnus-remove-from-range (nth 2 info) (nreverse news)))
11522       (gnus-group-update-group group t))))
11523
11524 (defun gnus-read-active-file ()
11525   "Get active file from NNTP server."
11526   (gnus-group-set-mode-line)
11527   (let ((methods (cons gnus-select-method gnus-secondary-select-methods))
11528         (not-first nil)
11529         list-type)
11530     (setq gnus-have-read-active-file nil)
11531     (save-excursion
11532       (set-buffer nntp-server-buffer)
11533       (while methods
11534         (let* ((where (nth 1 (car methods)))
11535                (mesg (format "Reading active file%s via %s..."
11536                              (if (and where (not (zerop (length where))))
11537                                  (concat " from " where) "")
11538                              (car (car methods)))))
11539           (gnus-message 5 mesg)
11540           (gnus-check-news-server (car methods))
11541           (cond 
11542            ((and (eq gnus-read-active-file 'some)
11543                  (gnus-check-backend-function
11544                   'retrieve-groups (car (car methods))))
11545             (let ((newsrc (cdr gnus-newsrc-alist))
11546                   groups)
11547               (while newsrc
11548                 (and (gnus-server-equal 
11549                       (gnus-find-method-for-group
11550                        (car (car newsrc)) (car newsrc))
11551                       (gnus-server-get-method nil (car methods)))
11552                      (setq groups (cons (car (car newsrc)) groups)))
11553                 (setq newsrc (cdr newsrc)))
11554               (setq list-type (gnus-retrieve-groups groups (car methods)))
11555               (cond ((not list-type)
11556                      (gnus-message 
11557                       1 "Cannot read partial active file from %s server." 
11558                       (car (car methods)))
11559                      (ding)
11560                      (sit-for 2))
11561                     ((eq list-type 'active)
11562                      (gnus-active-to-gnus-format (and not-first (car methods)))
11563                      (setq not-first t))
11564                     (t
11565                      (gnus-groups-to-gnus-format (and not-first (car methods)))
11566                      (setq not-first t)))))
11567            (t
11568             (if (not (gnus-request-list (car methods)))
11569                 (progn
11570                   (gnus-message 1 "Cannot read active file from %s server." 
11571                                 (car (car methods)))
11572                   (ding))
11573               (gnus-active-to-gnus-format 
11574                (and gnus-have-read-active-file (car methods)))
11575               (setq gnus-have-read-active-file t)
11576               (gnus-message 5 "%s...done" mesg)))))
11577         (setq methods (cdr methods))))))
11578
11579 ;; Read an active file and place the results in `gnus-active-hashtb'.
11580 (defun gnus-active-to-gnus-format (method &optional hashtb)
11581   (let ((cur (current-buffer))
11582         (hashtb (or hashtb 
11583                     (if method
11584                         gnus-active-hashtb
11585                       (setq gnus-active-hashtb
11586                             (gnus-make-hashtable 
11587                              (count-lines (point-min) (point-max))))))))
11588     ;; Delete unnecessary lines.
11589     (goto-char (point-min))
11590     (while (search-forward "\nto." nil t)
11591       (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
11592     (or (string= gnus-ignored-newsgroups "")
11593         (progn
11594           (goto-char (point-min))
11595           (delete-matching-lines gnus-ignored-newsgroups)))
11596     (and method (not (eq method gnus-select-method))
11597          (let ((prefix (gnus-group-prefixed-name "" method)))
11598            (goto-char (point-min))
11599            (while (and (not (eobp))
11600                        (null (insert prefix))
11601                        (zerop (forward-line 1))))))
11602     (goto-char (point-min))
11603     ;; Store active file in hashtable.
11604     (goto-char (point-min))
11605     (if (string-match "%[oO]" gnus-group-line-format)
11606         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
11607         ;; If we want information on moderated groups, we use this
11608         ;; loop...   
11609         (let* ((mod-hashtb (make-vector 7 0))
11610                (m (intern "m" mod-hashtb))
11611                group max mod)
11612           (while (not (eobp))
11613             (condition-case nil
11614                 (progn
11615                   (narrow-to-region (point) (gnus-point-at-eol))
11616                   (setq group (let ((obarray hashtb)) (read cur)))
11617                   (setq max (read cur))
11618                   (set group (cons (read cur) max))
11619                   ;; Enter moderated groups into a list.
11620                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
11621                       (setq gnus-moderated-list 
11622                             (cons (symbol-name group) gnus-moderated-list))))
11623               (error nil))
11624             (widen)
11625             (forward-line 1)))
11626       ;; And if we do not care about moderation, we use this loop,
11627       ;; which is faster.
11628       (let (group max)
11629         (while (not (eobp))
11630           (condition-case ()
11631               (progn
11632                 (narrow-to-region (point) (gnus-point-at-eol))
11633                 ;; group gets set to a symbol interned in the hash table
11634                 ;; (what a hack!!)
11635                 (setq group (let ((obarray hashtb)) (read cur)))
11636                 (setq max (read cur))
11637                 (set group (cons (read cur) max)))
11638             (error 
11639              (progn (ding) 
11640                     (gnus-message 3 "Illegal active: %s"
11641                                   (buffer-substring 
11642                                    (gnus-point-at-bol) (gnus-point-at-eol)))
11643                     nil)))
11644           (widen)
11645           (forward-line 1))))))
11646
11647 (defun gnus-groups-to-gnus-format (method &optional hashtb)
11648   ;; Parse a "groups" active file.
11649   (let ((cur (current-buffer))
11650         (hashtb (or hashtb 
11651                     (if method
11652                         gnus-active-hashtb
11653                       (setq gnus-active-hashtb
11654                             (gnus-make-hashtable 
11655                              (count-lines (point-min) (point-max)))))))
11656         (prefix (and method (not (eq method gnus-select-method))
11657                      (gnus-group-prefixed-name "" method))))
11658
11659     (goto-char (point-min))
11660     (condition-case ()
11661         ;; We split this into to separate loops, one with the prefix
11662         ;; and one without to speed the reading up somewhat.
11663         (if prefix
11664             (let (min max opoint)
11665               (while (not (eobp))
11666                 (read cur) (read cur)
11667                 (setq min (read cur)
11668                       max (read cur)
11669                       opoint (point))
11670                 (skip-chars-forward " \t")
11671                 (insert prefix)
11672                 (goto-char opoint)
11673                 (set (let ((obarray hashtb)) (read cur)) 
11674                      (cons min max))
11675                 (forward-line 1)))
11676           (let (min max opoint)
11677             (while (not (eobp))
11678               (if (= (following-char) ?2)
11679                   (progn
11680                     (read cur) (read cur)
11681                     (setq min (read cur)
11682                           max (read cur))
11683                     (set (let ((obarray hashtb)) (read cur)) 
11684                          (cons min max))))
11685               (forward-line 1))))
11686       (error 
11687        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
11688
11689 (defun gnus-read-newsrc-file (&optional force)
11690   "Read startup file.
11691 If FORCE is non-nil, the .newsrc file is read."
11692   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
11693   ;; Reset variables that might be defined in the .newsrc.eld file.
11694   (let ((variables gnus-variable-list))
11695     (while variables
11696       (set (car variables) nil)
11697       (setq variables (cdr variables))))
11698   (let* ((newsrc-file gnus-current-startup-file)
11699          (quick-file (concat newsrc-file ".el")))
11700     (save-excursion
11701       ;; We always load the .newsrc.eld file. If always contains
11702       ;; much information that can not be gotten from the .newsrc
11703       ;; file (ticked articles, killed groups, foreign methods, etc.)
11704       (gnus-read-newsrc-el-file quick-file)
11705  
11706       (if (or force
11707               (and (file-newer-than-file-p newsrc-file quick-file)
11708                    (file-newer-than-file-p newsrc-file 
11709                                            (concat quick-file "d")))
11710               (not gnus-newsrc-alist))
11711           ;; We read the .newsrc file. Note that if there if a
11712           ;; .newsrc.eld file exists, it has already been read, and
11713           ;; the `gnus-newsrc-hashtb' has been created. While reading
11714           ;; the .newsrc file, Gnus will only use the information it
11715           ;; can find there for changing the data already read -
11716           ;; ie. reading the .newsrc file will not trash the data
11717           ;; already read (except for read articles).
11718           (save-excursion
11719             (gnus-message 5 "Reading %s..." newsrc-file)
11720             (set-buffer (find-file-noselect newsrc-file))
11721             (buffer-disable-undo (current-buffer))
11722             (gnus-newsrc-to-gnus-format)
11723             (kill-buffer (current-buffer))
11724             (gnus-message 5 "Reading %s... done" newsrc-file))))))
11725
11726 (defun gnus-read-newsrc-el-file (file)
11727   (let ((ding-file (concat file "d")))
11728     ;; We always, always read the .eld file.
11729     (gnus-message 5 "Reading %s..." ding-file)
11730     (let (gnus-newsrc-assoc)
11731       (condition-case nil
11732           (load ding-file t t t)
11733         (error nil))
11734       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
11735     (let ((inhibit-quit t))
11736       (gnus-uncompress-newsrc-assoc))
11737     (gnus-make-hashtable-from-newsrc-alist)
11738     (if (not (file-newer-than-file-p file ding-file))
11739         ()
11740       ;; Old format quick file
11741       (gnus-message 5 "Reading %s..." file)
11742       ;; The .el file is newer than the .eld file, so we read that one
11743       ;; as well. 
11744       (gnus-read-old-newsrc-el-file file))))
11745
11746 ;; Parse the old-style quick startup file
11747 (defun gnus-read-old-newsrc-el-file (file)
11748   (let (newsrc killed marked group g m len info)
11749     (prog1
11750         (let ((gnus-killed-assoc nil)
11751               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
11752           (prog1
11753               (condition-case nil
11754                   (load file t t t)
11755                 (error nil))
11756             (setq newsrc gnus-newsrc-assoc
11757                   killed gnus-killed-assoc
11758                   marked gnus-marked-assoc)))
11759       (setq gnus-newsrc-alist nil)
11760       (while newsrc
11761         (setq group (car newsrc))
11762         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
11763           (if info
11764               (progn
11765                 (setcar (nthcdr 2 info) (cdr (cdr group)))
11766                 (setcar (cdr info)
11767                         (if (nth 1 group) gnus-level-default-subscribed 
11768                           gnus-level-default-unsubscribed))
11769                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
11770             (setq gnus-newsrc-alist
11771                   (cons 
11772                    (setq info
11773                          (list (car group)
11774                                (if (nth 1 group) gnus-level-default-subscribed
11775                                  gnus-level-default-unsubscribed) 
11776                                (cdr (cdr group))))
11777                    gnus-newsrc-alist)))
11778           (if (setq m (assoc (car group) marked))
11779             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
11780         (setq newsrc (cdr newsrc)))
11781       (setq newsrc killed)
11782       (while newsrc
11783         (setcar newsrc (car (car newsrc)))
11784         (setq newsrc (cdr newsrc)))
11785       (setq gnus-killed-list killed))
11786     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
11787     (gnus-make-hashtable-from-newsrc-alist)))
11788       
11789 (defun gnus-make-newsrc-file (file)
11790   "Make server dependent file name by catenating FILE and server host name."
11791   (let* ((file (expand-file-name file nil))
11792          (real-file (concat file "-" (nth 1 gnus-select-method))))
11793     (if (file-exists-p real-file)
11794         real-file file)))
11795
11796 (defun gnus-uncompress-newsrc-assoc ()
11797   ;; Uncompress all lists of marked articles in the newsrc assoc.
11798   (let ((newsrc gnus-newsrc-alist)
11799         marked)
11800     (while newsrc
11801       (if (not (setq marked (nth 3 (car newsrc))))
11802           ()
11803         (while marked
11804           (or (eq 'score (car (car marked)))
11805               (eq 'bookmark (car (car marked)))
11806               (eq 'killed (car (car marked)))
11807               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
11808           (setq marked (cdr marked))))
11809       (setq newsrc (cdr newsrc)))))
11810
11811 (defun gnus-compress-newsrc-assoc ()
11812   ;; Compress all lists of marked articles in the newsrc assoc.
11813   (let ((newsrc gnus-newsrc-alist)
11814         marked)
11815     (while newsrc
11816       (if (not (setq marked (nth 3 (car newsrc))))
11817           ()
11818         (while marked
11819           (or (eq 'score (car (car marked)))
11820               (eq 'bookmark (car (car marked)))
11821               (eq 'killed (car (car marked)))
11822               (setcdr (car marked) 
11823                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
11824           (setq marked (cdr marked))))
11825       (setq newsrc (cdr newsrc)))))
11826
11827 (defun gnus-newsrc-to-gnus-format ()
11828   (setq gnus-newsrc-options "")
11829   (or gnus-active-hashtb
11830       (setq gnus-active-hashtb (make-vector 4095 0)))
11831   (let ((buf (current-buffer))
11832         (already-read (> (length gnus-newsrc-alist) 1))
11833         group level subscribed info options-symbol newsrc
11834         symbol reads num1)
11835     (goto-char (point-min))
11836     ;; We intern the symbol `options' in the active hashtb so that we
11837     ;; can `eq' against it later.
11838     (setq options-symbol (intern "options" gnus-active-hashtb))
11839   
11840     (while (not (eobp))
11841       ;; We first read the first word on the line by narrowing and
11842       ;; then reading into `gnus-active-hashtb'.  Most groups will
11843       ;; already exist in that hashtb, so this will save some string
11844       ;; space.
11845       (narrow-to-region
11846        (point)
11847        (progn (skip-chars-forward "^ \t!:\n") (point)))
11848       (goto-char (point-min))
11849       (setq symbol 
11850             (and (/= (point-min) (point-max))
11851                  (let ((obarray gnus-active-hashtb)) (read buf))))
11852       (widen)
11853       ;; Now, the symbol we have read is either `options' or a group
11854       ;; name.  If it is an options line, we just add it to a string. 
11855       (cond 
11856        ((eq symbol options-symbol)
11857         (setq gnus-newsrc-options
11858               ;; This concatting is quite inefficient, but since our
11859               ;; thorough studies show that approx 99.37% of all
11860               ;; .newsrc files only contain a single options line, we
11861               ;; don't give a damn, frankly, my dear.
11862               (concat gnus-newsrc-options
11863                       (buffer-substring 
11864                        (gnus-point-at-bol)
11865                        ;; Options may continue on the next line.
11866                        (or (and (re-search-forward "^[^ \t]" nil 'move)
11867                                 (progn (beginning-of-line) (point)))
11868                            (point))))))
11869        (symbol
11870         ;; It was a group name.
11871         (setq subscribed (= (following-char) ?:)
11872               group (symbol-name symbol)
11873               reads nil)
11874         (if (eolp)
11875             ;; If the line ends here, this is clearly a buggy line, so
11876             ;; we put point a the beginning of line and let the cond
11877             ;; below do the error handling.
11878             (beginning-of-line)
11879           ;; We skip to the beginning of the ranges.
11880           (skip-chars-forward "!: \t"))
11881         ;; We are now at the beginning of the list of read articles.
11882         ;; We read them range by range.
11883         (while
11884             (cond 
11885              ((looking-at "[0-9]+")
11886               ;; We narrow and read a number instead of buffer-substring/
11887               ;; string-to-int because it's faster. narrow/widen is
11888               ;; faster than save-restriction/narrow, and save-restriction
11889               ;; produces a garbage object.
11890               (setq num1 (progn
11891                            (narrow-to-region (match-beginning 0) (match-end 0))
11892                            (read buf)))
11893               (widen)
11894               ;; If the next character is a dash, then this is a range.
11895               (if (= (following-char) ?-)
11896                   (progn
11897                     ;; We read the upper bound of the range.
11898                     (forward-char 1)
11899                     (if (not (looking-at "[0-9]+"))
11900                         ;; This is a buggy line, by we pretend that
11901                         ;; it's kinda OK. Perhaps the user should be
11902                         ;; dinged? 
11903                         (setq reads (cons num1 reads))
11904                       (setq reads 
11905                             (cons 
11906                              (cons num1 (progn
11907                                           (narrow-to-region (match-beginning 0) 
11908                                                             (match-end 0))
11909                                           (read buf)))
11910                              reads))
11911                       (widen)))
11912                 ;; It was just a simple number, so we add it to the
11913                 ;; list of ranges.
11914                 (setq reads (cons num1 reads)))
11915               ;; If the next char in ?\n, then we have reached the end
11916               ;; of the line and return nil.
11917               (/= (following-char) ?\n))
11918              ((= (following-char) ?\n)
11919               ;; End of line, so we end.
11920               nil)
11921              (t
11922               ;; Not numbers and not eol, so this might be a buggy
11923               ;; line... 
11924               (or (eobp) ; If it was eob instead of ?\n, we allow it.
11925                   (progn
11926                     ;; The line was buggy.
11927                     (setq group nil)
11928                     (gnus-message 3 "Mangled line: %s" 
11929                                   (buffer-substring (gnus-point-at-bol) 
11930                                                     (gnus-point-at-eol)))
11931                     (ding)
11932                     (sit-for 1)))
11933               nil))
11934           ;; Skip past ", ". Spaces are illegal in these ranges, but
11935           ;; we allow them, because it's a common mistake to put a
11936           ;; space after the comma.
11937           (skip-chars-forward ", "))
11938
11939         ;; We have already read .newsrc.eld, so we gently update the
11940         ;; data in the hash table with the information we have just
11941         ;; read. 
11942         (if (not group)
11943             ()
11944           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
11945                 level)
11946             (if info
11947                 ;; There is an entry for this file in the alist.
11948                 (progn
11949                   (setcar (nthcdr 2 info) (nreverse reads))
11950                   ;; We update the level very gently.  In fact, we
11951                   ;; only change it if there's been a status change
11952                   ;; from subscribed to unsubscribed, or vice versa.
11953                   (setq level (nth 1 info))
11954                   (cond ((and (<= level gnus-level-subscribed)
11955                               (not subscribed))
11956                          (setq level (if reads
11957                                          gnus-level-default-unsubscribed 
11958                                        (1+ gnus-level-default-unsubscribed))))
11959                         ((and (> level gnus-level-subscribed) subscribed)
11960                          (setq level gnus-level-default-subscribed)))
11961                   (setcar (cdr info) level))
11962               ;; This is a new group.
11963               (setq info (list group 
11964                                (if subscribed
11965                                    gnus-level-default-subscribed 
11966                                  (if reads
11967                                      (1+ gnus-level-subscribed)
11968                                    gnus-level-default-unsubscribed))
11969                                (nreverse reads))))
11970             (setq newsrc (cons info newsrc))))
11971         (forward-line 1))))
11972     
11973     (setq newsrc (nreverse newsrc))
11974
11975     (if (not already-read)
11976         ()
11977       ;; We now have two newsrc lists - `newsrc', which is what we
11978       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
11979       ;; what we've read from .newsrc.eld. We have to merge these
11980       ;; lists. We do this by "attaching" any (foreign) groups in the
11981       ;; gnus-newsrc-alist to the (native) group that precedes them. 
11982       (let ((rc (cdr gnus-newsrc-alist))
11983             (prev gnus-newsrc-alist)
11984             entry mentry)
11985         (while rc
11986           (or (assoc (car (car rc)) newsrc)
11987               (if (setq entry (assoc (car (car prev)) newsrc))
11988                   (setcdr (setq mentry (memq entry newsrc))
11989                           (cons (car rc) (cdr mentry)))
11990                 (setq newsrc (cons (car rc) newsrc))))
11991           (setq prev rc
11992                 rc (cdr rc)))))
11993
11994     (setq gnus-newsrc-alist newsrc)
11995     ;; We make the newsrc hashtb.
11996     (gnus-make-hashtable-from-newsrc-alist)
11997
11998     ;; Finally, if we read some options lines, we parse them.
11999     (or (string= gnus-newsrc-options "")
12000         (gnus-newsrc-parse-options gnus-newsrc-options))
12001     ))
12002
12003 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12004 ;; The return value will be a list on the form
12005 ;; ((regexp1 . ignore)
12006 ;;  (regexp2 . subscribe)...)
12007 ;; When handling new newsgroups, groups that match a `ignore' regexp
12008 ;; will be ignored, and groups that match a `subscribe' regexp will be
12009 ;; subscribed. A line like
12010 ;; options -n !all rec.all
12011 ;; will lead to a list that looks like
12012 ;; (("^rec\\..+" . subscribe) 
12013 ;;  ("^.+" . ignore))
12014 ;; So all "rec.*" groups will be subscribed, while all the other
12015 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12016 ;; different from "options -n rec.all !all". 
12017 (defun gnus-newsrc-parse-options (options)
12018   (let (out eol)
12019     (save-excursion
12020       (gnus-set-work-buffer)
12021       (insert (regexp-quote options))
12022       ;; First we treat all continuation lines.
12023       (goto-char (point-min))
12024       (while (re-search-forward "\n[ \t]+" nil t)
12025         (replace-match " " t t))
12026       ;; Then we transform all "all"s into ".+"s.
12027       (goto-char (point-min))
12028       (while (re-search-forward "\\ball\\b" nil t)
12029         (replace-match ".+" t t))
12030       (goto-char (point-min))
12031       ;; We remove all other options than the "-n" ones.
12032       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12033         (replace-match " ")
12034         (forward-char -1))
12035       (goto-char (point-min))
12036
12037       ;; We are only interested in "options -n" lines - we
12038       ;; ignore the other option lines.
12039       (while (re-search-forward "[ \t]-n" nil t)
12040         (setq eol 
12041               (or (save-excursion
12042                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12043                          (- (point) 2)))
12044                   (gnus-point-at-eol)))
12045         ;; Search for all "words"...
12046         (while (re-search-forward "[^ \t,\n-]+" eol t)
12047           (if (= (char-after (match-beginning 0)) ?!)
12048               ;; If the word begins with a bang (!), this is a "not"
12049               ;; spec. We put this spec (minus the bang) and the
12050               ;; symbol `ignore' into the list.
12051               (setq out (cons (cons (concat 
12052                                      "^" (buffer-substring 
12053                                           (1+ (match-beginning 0))
12054                                           (match-end 0)))
12055                                     'ignore) out))
12056             ;; There was no bang, so this is a "yes" spec.
12057             (setq out (cons (cons (concat 
12058                                    "^" (buffer-substring (match-beginning 0)
12059                                                          (match-end 0)))
12060                                   'subscribe) out)))))
12061     
12062       (setq gnus-newsrc-options-n out))))
12063                
12064
12065 (defun gnus-save-newsrc-file ()
12066   "Save .newsrc file."
12067   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12068   ;; from the variable gnus-newsrc-alist.
12069   (and (or gnus-newsrc-alist gnus-killed-list)
12070        gnus-current-startup-file
12071        (let ((make-backup-files t)
12072              (version-control nil)
12073              (require-final-newline t)) ;Don't ask even if requested.
12074          ;; You can stop or change version control of backup file.
12075          ;; Suggested by jason@violet.berkeley.edu.
12076          (run-hooks 'gnus-save-newsrc-hook)
12077          (save-excursion
12078            (if (or (not gnus-dribble-buffer)
12079                    (not (buffer-name gnus-dribble-buffer))
12080                    (zerop (save-excursion
12081                             (set-buffer gnus-dribble-buffer)
12082                             (buffer-size))))
12083                (gnus-message 4 "(No changes need to be saved)")
12084              (if gnus-save-newsrc-file
12085                  (progn
12086                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12087                    ;; Make backup file of master newsrc.
12088                    (gnus-gnus-to-newsrc-format)
12089                    (gnus-message 5 "Saving %s... done"
12090                                  gnus-current-startup-file)))
12091              ;; Quickly loadable .newsrc.
12092              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12093              (gnus-add-current-to-buffer-list)
12094              (buffer-disable-undo (current-buffer))
12095              (erase-buffer)
12096              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12097              (gnus-gnus-to-quick-newsrc-format)
12098              (write-region 1 (point-max) 
12099                            (concat gnus-current-startup-file ".eld") 
12100                            nil 'nomesg)
12101              (kill-buffer (current-buffer))
12102              (gnus-message 5 "Saving %s.eld... done" gnus-current-startup-file)
12103              (gnus-dribble-delete-file))))))
12104
12105 (defun gnus-gnus-to-quick-newsrc-format ()
12106   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12107   (insert ";; (ding) Gnus startup file.\n")
12108   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12109   (insert ";; to read .newsrc.\n")
12110   (let ((variables gnus-variable-list)
12111         (inhibit-quit t)
12112         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12113         variable)
12114     ;; insert lisp expressions.
12115     (gnus-compress-newsrc-assoc)
12116     (while variables
12117       (setq variable (car variables))
12118       (and (boundp variable)
12119            (symbol-value variable)
12120            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12121            (insert "(setq " (symbol-name variable) " '"
12122                    (prin1-to-string (symbol-value variable))
12123                    ")\n"))
12124       (setq variables (cdr variables)))
12125     (gnus-uncompress-newsrc-assoc)))
12126
12127
12128 (defun gnus-gnus-to-newsrc-format ()
12129   ;; Generate and save the .newsrc file.
12130   (let ((newsrc (cdr gnus-newsrc-alist))
12131         info ranges range)
12132     (save-excursion
12133       (set-buffer (create-file-buffer gnus-startup-file))
12134       (buffer-disable-undo (current-buffer))
12135       (erase-buffer)
12136       ;; Write options.
12137       (if gnus-newsrc-options (insert gnus-newsrc-options))
12138       ;; Write subscribed and unsubscribed.
12139       (while newsrc
12140         (setq info (car newsrc))
12141         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12142             (progn
12143               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12144                                      "!" ":"))
12145               (if (setq ranges (nth 2 info))
12146                   (progn
12147                     (insert " ")
12148                     (if (not (listp (cdr ranges)))
12149                         (if (= (car ranges) (cdr ranges))
12150                             (insert (int-to-string (car ranges)))
12151                           (insert (int-to-string (car ranges)) "-" 
12152                                   (int-to-string (cdr ranges))))
12153                       (while ranges
12154                         (setq range (car ranges)
12155                               ranges (cdr ranges))
12156                         (if (or (atom range) (= (car range) (cdr range)))
12157                             (insert (int-to-string 
12158                                      (or (and (atom range) range) 
12159                                          (car range))))
12160                           (insert (int-to-string (car range)) "-"
12161                                   (int-to-string (cdr range))))
12162                         (if ranges (insert ","))))))
12163               (insert "\n")))
12164         (setq newsrc (cdr newsrc)))
12165       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12166       (kill-buffer (current-buffer)))))
12167
12168 (defun gnus-read-descriptions-file ()
12169   (gnus-message 5 "Reading descriptions file...")
12170   (cond 
12171    ((not (or (gnus-server-opened gnus-select-method)
12172              (gnus-open-server gnus-select-method)))
12173     (gnus-message 1 "Couldn't open server")
12174     nil)
12175    ((not (gnus-request-list-newsgroups gnus-select-method))
12176     (gnus-message 1 "Couldn't read newsgroups descriptions")
12177     nil)
12178    (t
12179     (let (group)
12180       (setq gnus-description-hashtb 
12181             (gnus-make-hashtable (length gnus-active-hashtb)))
12182       (save-excursion
12183         (save-restriction
12184           (set-buffer nntp-server-buffer)
12185           (goto-char (point-min))
12186           (delete-non-matching-lines "^[-\\._+A-Za-z0-9]+[ \t]")
12187           (goto-char (point-min))
12188           (if (or (search-forward "\n.\n" nil t)
12189                   (goto-char (point-max)))
12190               (progn
12191                 (beginning-of-line)
12192                 (narrow-to-region (point-min) (point))))
12193           (goto-char (point-min))
12194           (while (not (eobp))
12195             (setq group (let ((obarray gnus-description-hashtb))
12196                           (read (current-buffer))))
12197             (skip-chars-forward " \t")
12198             (and (symbolp group)
12199                  (set group (buffer-substring (point) (gnus-point-at-eol))))
12200             (forward-line 1))))
12201       (gnus-message 5 "Reading descriptions file...done")
12202       t))))
12203
12204 (defun gnus-group-get-description (group)
12205   ;; Get the description of a group by sending XGTITLE to the server.
12206   (and (gnus-request-group-description group)
12207        (save-excursion
12208          (set-buffer nntp-server-buffer)
12209          (goto-char (point-min))
12210          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12211               (buffer-substring (match-beginning 1) (match-end 1))))))
12212
12213 ;;;
12214 ;;; Server
12215 ;;;
12216
12217 (defvar gnus-server-mode-hook nil
12218   "Hook run in `gnus-server-mode' buffers.")
12219
12220 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12221   "Format of server lines.
12222 It works along the same lines as a normal formatting string,
12223 with some simple extensions.")
12224
12225 (defvar gnus-server-mode-line-format "(ding) List of servers"
12226   "The format specification for the server mode line.")
12227
12228 (defconst gnus-server-line-format-alist
12229   (list (list ?h 'how ?s)
12230         (list ?n 'name ?s)
12231         (list ?w 'where ?s)
12232         ))
12233
12234 (defconst gnus-server-mode-line-format-alist 
12235   (list (list ?S 'news-server ?s)
12236         (list ?M 'news-method ?s)
12237         (list ?u 'user-defined ?s)))
12238
12239 (defvar gnus-server-line-format-spec nil)
12240 (defvar gnus-server-mode-line-format-spec nil)
12241 (defvar gnus-server-killed-servers nil)
12242
12243 (defvar gnus-server-mode-map nil)
12244 (put 'gnus-server-mode 'mode-class 'special)
12245
12246 (if gnus-server-mode-map
12247     nil
12248   (setq gnus-server-mode-map (make-sparse-keymap))
12249   (suppress-keymap gnus-server-mode-map)
12250   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12251   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12252   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12253   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12254   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12255   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12256   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12257   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12258   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12259   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12260
12261 (defun gnus-server-mode ()
12262   "Major mode for listing and editing servers.
12263
12264 All normal editing commands are switched off.
12265 \\<gnus-server-mode-map>
12266
12267 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12268
12269 The following commands are available:
12270
12271 \\{gnus-server-mode-map}"
12272   (interactive)
12273   (if gnus-visual (gnus-server-make-menu-bar))
12274   (kill-all-local-variables)
12275   (setq mode-line-modified "-- ")
12276   (make-local-variable 'mode-line-format)
12277   (setq mode-line-format (copy-sequence mode-line-format))
12278   (and (equal (nth 3 mode-line-format) "   ")
12279        (setcar (nthcdr 3 mode-line-format) ""))
12280   (setq major-mode 'gnus-server-mode)
12281   (setq mode-name "Server")
12282 ;  (gnus-group-set-mode-line)
12283   (setq mode-line-process nil)
12284   (use-local-map gnus-server-mode-map)
12285   (buffer-disable-undo (current-buffer))
12286   (setq truncate-lines t)
12287   (setq buffer-read-only t)
12288   (run-hooks 'gnus-server-mode-hook))
12289
12290 (defun gnus-server-insert-server-line (sformat name method)
12291   (let* ((sformat (or sformat gnus-server-line-format-spec))
12292          (how (car method))
12293          (where (nth 1 method))
12294          b)
12295     (beginning-of-line)
12296     (setq b (point))
12297     ;; Insert the text.
12298     (insert (eval sformat))
12299     (add-text-properties 
12300      b (1+ b) (list 'gnus-server (intern name)))))
12301
12302 (defun gnus-server-setup-buffer ()
12303   (if (get-buffer gnus-server-buffer)
12304       ()
12305     (save-excursion
12306       (set-buffer (get-buffer-create gnus-server-buffer))
12307       (gnus-server-mode)
12308       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12309
12310 (defun gnus-server-prepare ()
12311   (setq gnus-server-mode-line-format-spec 
12312         (gnus-parse-format gnus-server-mode-line-format 
12313                            gnus-server-mode-line-format-alist))
12314   (setq gnus-server-line-format-spec 
12315         (gnus-parse-format gnus-server-line-format 
12316                            gnus-server-line-format-alist))
12317   (let ((alist gnus-server-alist)
12318         (buffer-read-only nil))
12319     (erase-buffer)
12320     (while alist
12321       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12322       (setq alist (cdr alist))))
12323   (goto-char (point-min))
12324   (gnus-server-position-cursor))
12325
12326 (defun gnus-server-server-name ()
12327   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12328     (and server (symbol-name server))))
12329
12330 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12331
12332 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12333
12334 (defun gnus-server-update-server (server)
12335   (save-excursion
12336     (set-buffer gnus-server-buffer)
12337     (let ((buffer-read-only nil)
12338           (info (cdr (assoc server gnus-server-alist))))
12339       (gnus-dribble-enter 
12340        (concat "(gnus-server-set-info \"" server "\" '"
12341                (prin1-to-string info) ")"))
12342       ;; Buffer may be narrowed.
12343       (save-restriction
12344         (widen)
12345         (if (gnus-server-goto-server server)
12346             (delete-region (progn (beginning-of-line) (point))
12347                            (progn (forward-line 1) (point))))
12348         (let ((entry (assoc server gnus-server-alist)))
12349           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12350           (gnus-server-position-cursor))))))
12351
12352 (defun gnus-server-set-info (server info)
12353   ;; Enter a select method into the virtual server alist.
12354   (gnus-dribble-enter 
12355    (concat "(gnus-server-set-info \"" server "\" '"
12356            (prin1-to-string info) ")"))
12357   (let* ((server (nth 1 info))
12358          (entry (assoc server gnus-server-alist)))
12359     (if entry (setcdr entry info)
12360       (setq gnus-server-alist
12361             (nconc gnus-server-alist (list (cons server info)))))))
12362
12363 (defun gnus-server-to-method (server)
12364   ;; Map virtual server names to select methods.
12365   (or (and (equal server "native") gnus-select-method)
12366       (cdr (assoc server gnus-server-alist))))
12367
12368 (defun gnus-server-extend-method (group method)
12369   ;; This function "extends" a virtual server.  If the server is
12370   ;; "hello", and the select method is ("hello" (my-var "something")) 
12371   ;; in the group "alt.alt", this will result in a new virtual server
12372   ;; called "helly+alt.alt".
12373   (let ((entry
12374          (gnus-copy-sequence 
12375           (if (equal (car method) "native") gnus-select-method
12376               (cdr (assoc (car method) gnus-server-alist))))))
12377     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12378     (nconc entry (cdr method))))
12379
12380 (defun gnus-server-get-method (group method)
12381   ;; Input either a server name, and extended server name, or a
12382   ;; select method, and return a select method. 
12383   (cond ((stringp method)
12384          (gnus-server-to-method method))
12385         ((stringp (car method))
12386          (gnus-server-extend-method group method))
12387         (t
12388          (gnus-server-add-address method))))
12389
12390 (defun gnus-server-add-address (method)
12391   (let ((method-name (symbol-name (car method))))
12392     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12393              (not (assq (intern (concat method-name "-address")) method)))
12394         (append method (list (list (intern (concat method-name "-address"))
12395                                    (nth 1 method))))
12396       method)))
12397
12398 (defun gnus-server-equal (s1 s2)
12399   (or (equal s1 s2)
12400       (and (= (length s1) (length s2))
12401            (progn
12402              (while (and s1 (member (car s1) s2))
12403                (setq s1 (cdr s1)))
12404              (null s1)))))
12405
12406 ;;; Interactive server functions.
12407
12408 (defun gnus-server-kill-server (server)
12409   "Kill the server on the current line."
12410   (interactive (list (gnus-server-server-name)))
12411   (or (gnus-server-goto-server server)
12412       (if server (error "No such server: %s" server)
12413         (error "No server on the current line")))
12414   (let ((buffer-read-only nil))
12415     (delete-region (progn (beginning-of-line) (point))
12416                    (progn (forward-line 1) (point))))
12417   (setq gnus-server-killed-servers 
12418         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12419   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12420                                 gnus-server-alist))
12421   (gnus-server-position-cursor))
12422
12423 (defun gnus-server-yank-server ()
12424   "Yank the previously killed server."
12425   (interactive)
12426   (or gnus-server-killed-servers
12427       (error "No killed servers to be yanked"))
12428   (let ((alist gnus-server-alist)
12429         (server (gnus-server-server-name))
12430         (killed (car gnus-server-killed-servers)))
12431     (if (not server) 
12432         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12433       (if (string= server (car (car gnus-server-alist)))
12434           (setq gnus-server-alist (cons killed gnus-server-alist))
12435         (while (and (cdr alist)
12436                     (not (string= server (car (car (cdr alist))))))
12437           (setq alist (cdr alist)))
12438         (setcdr alist (cons killed (cdr alist)))))
12439     (gnus-server-update-server (car killed))
12440     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12441     (gnus-server-position-cursor)))
12442
12443 (defun gnus-server-exit ()
12444   "Return to the group buffer."
12445   (interactive)
12446   (kill-buffer (current-buffer))
12447   (switch-to-buffer gnus-group-buffer))
12448
12449 (defun gnus-server-list-servers ()
12450   "List all available servers."
12451   (interactive)
12452   (let ((cur (gnus-server-server-name)))
12453     (gnus-server-prepare)
12454     (if cur (gnus-server-goto-server cur)
12455       (goto-char (point-max))
12456       (forward-line -1))
12457     (gnus-server-position-cursor)))
12458
12459 (defun gnus-server-copy-server (from to)
12460   (interactive
12461    (list
12462     (or (gnus-server-server-name)
12463         (error "No server on the current line"))
12464     (read-string "Copy to: ")))
12465   (or from (error "No server on current line"))
12466   (or (and to (not (string= to ""))) (error "No name to copy to"))
12467   (and (assoc to gnus-server-alist) (error "%s already exists" to))
12468   (or (assoc from gnus-server-alist) 
12469       (error "%s: no such server" from))
12470   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
12471     (setcar to-entry to)
12472     (setcar (nthcdr 2 to-entry) to)
12473     (setq gnus-server-killed-servers 
12474           (cons to-entry gnus-server-killed-servers))
12475     (gnus-server-yank-server)))
12476
12477 (defun gnus-server-add-server (how where)
12478   (interactive 
12479    (list (intern (completing-read "Server method: "
12480                                   gnus-valid-select-methods nil t))
12481          (read-string "Server name: ")))
12482   (setq gnus-server-killed-servers 
12483         (cons (list where how where) gnus-server-killed-servers))
12484   (gnus-server-yank-server))
12485
12486 (defun gnus-server-goto-server (server)
12487   "Jump to a server line."
12488   (interactive
12489    (list (completing-read "Goto server: " gnus-server-alist nil t)))
12490   (let ((to (text-property-any (point-min) (point-max) 
12491                                'gnus-server (intern server))))
12492     (and to
12493          (progn
12494            (goto-char to) 
12495            (gnus-server-position-cursor)))))
12496
12497 (defun gnus-server-edit-server (server)
12498   "Edit the server on the current line."
12499   (interactive (list (gnus-server-server-name)))
12500   (or server
12501       (error "No server on current line"))
12502   (let ((winconf (current-window-configuration)))
12503     (get-buffer-create gnus-server-edit-buffer)
12504     (gnus-configure-windows 'edit-server)
12505     (gnus-add-current-to-buffer-list)
12506     (emacs-lisp-mode)
12507     (make-local-variable 'gnus-prev-winconf)
12508     (setq gnus-prev-winconf winconf)
12509     (use-local-map (copy-keymap (current-local-map)))
12510     (let ((done-func '(lambda () 
12511                         "Exit editing mode and update the information."
12512                         (interactive)
12513                         (gnus-server-edit-server-done 'group))))
12514       (setcar (cdr (nth 4 done-func)) server)
12515       (local-set-key "\C-c\C-c" done-func))
12516     (erase-buffer)
12517     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
12518     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
12519
12520 (defun gnus-server-edit-server-done (server)
12521   (interactive)
12522   (set-buffer (get-buffer-create gnus-server-edit-buffer))
12523   (goto-char (point-min))
12524   (let ((form (read (current-buffer)))
12525         (winconf gnus-prev-winconf))
12526     (gnus-server-set-info server form)
12527     (kill-buffer (current-buffer))
12528     (and winconf (set-window-configuration winconf))
12529     (set-buffer gnus-server-buffer)
12530     (gnus-server-update-server (gnus-server-server-name))
12531     (gnus-server-position-cursor)))
12532
12533 (defun gnus-server-read-server (server)
12534   "Browse a server."
12535   (interactive (list (gnus-server-server-name)))
12536   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
12537
12538 (defun gnus-mouse-pick-server (e)
12539   (interactive "e")
12540   (mouse-set-point e)
12541   (gnus-server-read-server (gnus-server-server-name)))
12542
12543 ;; Allow redefinition of Gnus functions.
12544
12545 (gnus-ems-redefine)
12546
12547 (provide 'gnus)
12548
12549 ;;; gnus.el ends here