*** 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 (require 'gnus-ems)
40
41 ;; Site dependent variables. These variables should be defined in
42 ;; paths.el.
43
44 (defvar gnus-default-nntp-server nil
45   "Specify a default NNTP server.
46 This variable should be defined in paths.el, and should never be set
47 by the user.
48 If you want to change servers, you should use `gnus-select-method'.
49 See the documentation to that variable.")
50
51 (defconst gnus-backup-default-subscribed-newsgroups 
52   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
53   "Default default new newsgroups the first time Gnus is run.
54 Should be set in paths.el, and shouldn't be touched by the user.")
55
56 (defvar gnus-local-domain nil
57   "Local domain name without a host name.
58 The DOMAINNAME environment variable is used instead if it is defined.
59 If the `system-name' function returns the full Internet name, there is
60 no need to set this variable.")
61
62 (defvar gnus-local-organization nil
63   "String with a description of what organization (if any) the user belongs to.
64 The ORGANIZATION environment variable is used instead if it is defined.
65 If this variable contains a function, this function will be called
66 with the current newsgroup name as the argument. The function should
67 return a string.
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
494 (defvar gnus-group-always-list-unread gnus-level-subscribed
495   "*Always list groups less than this variable with unread articles. 
496 If nil, use parameter to specify.")
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-move-group nil)
1117
1118 (defvar gnus-newsgroup-dependencies nil)
1119 (defvar gnus-newsgroup-threads nil)
1120 (defvar gnus-newsgroup-async nil)
1121 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1122
1123 (defvar gnus-newsgroup-adaptive nil)
1124
1125 (defconst gnus-group-line-format-alist
1126   (list (list ?M 'marked ?c)
1127         (list ?S 'subscribed ?c)
1128         (list ?L 'level ?d)
1129         (list ?N 'number ?s)
1130         (list ?I 'number-of-dormant ?d)
1131         (list ?T 'number-of-ticked ?d)
1132         (list ?R 'number-of-read ?s)
1133         (list ?t 'number-total ?d)
1134         (list ?y 'number-of-unread-unticked ?s)
1135         (list ?i 'number-of-ticked-and-dormant ?d)
1136         (list ?g 'group ?s)
1137         (list ?G 'qualified-group ?s)
1138         (list ?D 'newsgroup-description ?s)
1139         (list ?o 'moderated ?c)
1140         (list ?O 'moderated-string ?s)
1141         (list ?s 'news-server ?s)
1142         (list ?n 'news-method ?s)
1143         (list ?z 'news-method-string ?s)
1144         (list ?u 'user-defined ?s)))
1145
1146 (defconst gnus-summary-line-format-alist 
1147   (list (list ?N 'number ?d)
1148         (list ?S 'subject ?s)
1149         (list ?s 'subject-or-nil ?s)
1150         (list ?n 'name ?s)
1151         (list ?A 'address ?s)
1152         (list ?F 'from ?s)
1153         (list ?x (macroexpand '(header-xref header)) ?s)
1154         (list ?D (macroexpand '(header-date header)) ?s)
1155         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1156         (list ?M (macroexpand '(header-id header)) ?s)
1157         (list ?r (macroexpand '(header-references header)) ?s)
1158         (list ?c '(or (header-chars header) 0) ?d)
1159         (list ?L 'lines ?d)
1160         (list ?I 'indentation ?s)
1161         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1162         (list ?R 'replied ?c)
1163         (list ?\[ 'opening-bracket ?c)
1164         (list ?\] 'closing-bracket ?c)
1165         (list ?\> '(make-string level ? ) ?s)
1166         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1167         (list ?i 'score ?d)
1168         (list ?z 'score-char ?c)
1169         (list ?U 'unread ?c)
1170         (list ?t '(gnus-summary-number-of-articles-in-thread 
1171                    (or gnus-tmp-adopt-thread 
1172                        (if (boundp 'thread) (symbol-value 'thread)
1173                          thread nil)))
1174                    ?d)
1175         (list ?e '(gnus-summary-number-of-articles-in-thread 
1176                    (or gnus-tmp-adopt-thread 
1177                        (if (boundp 'thread) (symbol-value 'thread)
1178                          thread nil)) t)
1179                    ?d)
1180         (list ?u 'user-defined ?s))
1181   "An alist of format specifications that can appear in summary lines,
1182 and what variables they correspond with, along with the type of the
1183 variable (string, integer, character, etc).")
1184
1185 (defconst gnus-summary-dummy-line-format-alist
1186   (list (list ?S 'subject ?s)
1187         (list ?N 'number ?d)
1188         (list ?u 'user-defined ?s)))
1189
1190 (defconst gnus-summary-mode-line-format-alist 
1191   (list (list ?G 'group-name ?s)
1192         (list ?g '(gnus-short-group-name group-name) ?s)
1193         (list ?A 'article-number ?d)
1194         (list ?Z 'unread-and-unselected ?s)
1195         (list ?V 'gnus-version ?s)
1196         (list ?U 'unread ?d)
1197         (list ?S 'subject ?s)
1198         (list ?e 'unselected ?d)
1199         (list ?u 'user-defined ?s)
1200         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1201
1202 (defconst gnus-group-mode-line-format-alist 
1203   (list (list ?S 'news-server ?s)
1204         (list ?M 'news-method ?s)
1205         (list ?u 'user-defined ?s)))
1206
1207 (defvar gnus-have-read-active-file nil)
1208
1209 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls & Boys)"
1210   "The mail address of the Gnus maintainer.")
1211
1212 (defconst gnus-version "(ding) Gnus v0.71"
1213   "Version number for this version of Gnus.")
1214
1215 (defvar gnus-info-nodes
1216   '((gnus-group-mode            "(gnus)The Group Buffer")
1217     (gnus-summary-mode          "(gnus)The Summary Buffer")
1218     (gnus-article-mode          "(gnus)The Article Buffer"))
1219   "Assoc list of major modes and related Info nodes.")
1220
1221 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1222   "The location of the (ding) Gnus documentation group.")
1223
1224 (defvar gnus-group-buffer "*Group*")
1225 (defvar gnus-summary-buffer "*Summary*")
1226 (defvar gnus-article-buffer "*Article*")
1227 (defvar gnus-server-buffer "*Server*")
1228
1229 (defvar gnus-work-buffer " *gnus work*")
1230
1231 (defvar gnus-buffer-list nil
1232   "Gnus buffers that should be killed on exit.")
1233
1234 (defvar gnus-server-alist nil
1235   "List of available servers.")
1236
1237 (defvar gnus-variable-list
1238   '(gnus-newsrc-options gnus-newsrc-options-n
1239     gnus-newsrc-last-checked-date 
1240     gnus-newsrc-alist gnus-server-alist
1241     gnus-killed-list gnus-zombie-list)
1242   "Gnus variables saved in the quick startup file.")
1243
1244 (defvar gnus-overload-functions
1245   '((news-inews gnus-inews-news "rnewspost")
1246     (caesar-region gnus-caesar-region "rnews"))
1247   "Functions overloaded by gnus.
1248 It is a list of `(original overload &optional file)'.")
1249
1250 (defvar gnus-newsrc-options nil
1251   "Options line in the .newsrc file.")
1252
1253 (defvar gnus-newsrc-options-n nil
1254   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1255
1256 (defvar gnus-newsrc-last-checked-date nil
1257   "Date Gnus last asked server for new newsgroups.")
1258
1259 (defvar gnus-newsrc-alist nil
1260   "Assoc list of read articles.
1261 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1262
1263 (defvar gnus-newsrc-hashtb nil
1264   "Hashtable of gnus-newsrc-alist.")
1265
1266 (defvar gnus-killed-list nil
1267   "List of killed newsgroups.")
1268
1269 (defvar gnus-killed-hashtb nil
1270   "Hash table equivalent of gnus-killed-list.")
1271
1272 (defvar gnus-zombie-list nil
1273   "List of almost dead newsgroups.")
1274
1275 (defvar gnus-description-hashtb nil
1276   "Descriptions of newsgroups.")
1277
1278 (defvar gnus-list-of-killed-groups nil
1279   "List of newsgroups that have recently been killed by the user.")
1280
1281 (defvar gnus-active-hashtb nil
1282   "Hashtable of active articles.")
1283
1284 (defvar gnus-moderated-list nil
1285   "List of moderated newsgroups.")
1286
1287 (defvar gnus-group-marked nil)
1288
1289 (defvar gnus-current-startup-file nil
1290   "Startup file for the current host.")
1291
1292 (defvar gnus-last-search-regexp nil
1293   "Default regexp for article search command.")
1294
1295 (defvar gnus-last-shell-command nil
1296   "Default shell command on article.")
1297
1298 (defvar gnus-current-select-method nil
1299   "The current method for selecting a newsgroup.")
1300
1301 (defvar gnus-have-all-newsgroups nil)
1302
1303 (defvar gnus-article-internal-prepare-hook nil)
1304
1305 (defvar gnus-newsgroup-name nil)
1306 (defvar gnus-newsgroup-begin nil)
1307 (defvar gnus-newsgroup-end nil)
1308 (defvar gnus-newsgroup-last-rmail nil)
1309 (defvar gnus-newsgroup-last-mail nil)
1310 (defvar gnus-newsgroup-last-folder nil)
1311 (defvar gnus-newsgroup-last-file nil)
1312 (defvar gnus-newsgroup-auto-expire nil)
1313 (defvar gnus-newsgroup-active nil)
1314
1315 (defvar gnus-newsgroup-unreads nil
1316   "List of unread articles in the current newsgroup.")
1317
1318 (defvar gnus-newsgroup-unselected nil
1319   "List of unselected unread articles in the current newsgroup.")
1320
1321 (defvar gnus-newsgroup-marked nil
1322   "List of ticked articles in the current newsgroup (a subset of unread art).")
1323
1324 (defvar gnus-newsgroup-killed nil
1325   "List of ranges of articles that have been through the scoring process.")
1326
1327 (defvar gnus-newsgroup-kill-headers nil)
1328
1329 (defvar gnus-newsgroup-replied nil
1330   "List of articles that have been replied to in the current newsgroup.")
1331
1332 (defvar gnus-newsgroup-expirable nil
1333   "List of articles in the current newsgroup that can be expired.")
1334
1335 (defvar gnus-newsgroup-processable nil
1336   "List of articles in the current newsgroup that can be processed.")
1337
1338 (defvar gnus-newsgroup-bookmarks nil
1339   "List of articles in the current newsgroup that have bookmarks.")
1340
1341 (defvar gnus-newsgroup-dormant nil
1342   "List of dormant articles in the current newsgroup.")
1343
1344 (defvar gnus-newsgroup-scored nil
1345   "List of scored articles in the current newsgroup.")
1346
1347 (defvar gnus-newsgroup-headers nil
1348   "List of article headers in the current newsgroup.")
1349 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1350
1351 (defvar gnus-newsgroup-ancient nil
1352   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1353
1354 (defvar gnus-current-article nil)
1355 (defvar gnus-article-current nil)
1356 (defvar gnus-current-headers nil)
1357 (defvar gnus-have-all-headers nil)
1358 (defvar gnus-last-article nil)
1359 (defvar gnus-newsgroup-history nil)
1360 (defvar gnus-current-kill-article nil)
1361
1362 ;; Save window configuration.
1363 (defvar gnus-prev-winconf nil)
1364
1365 ;; Format specs
1366 (defvar gnus-summary-line-format-spec nil)
1367 (defvar gnus-summary-dummy-line-format-spec nil)
1368 (defvar gnus-group-line-format-spec nil)
1369 (defvar gnus-summary-mode-line-format-spec nil)
1370 (defvar gnus-article-mode-line-format-spec nil)
1371 (defvar gnus-group-mode-line-format-spec nil)
1372 (defvar gnus-summary-mark-positions nil)
1373
1374 (defvar gnus-summary-expunge-below nil)
1375 (defvar gnus-reffed-article-number nil)
1376
1377 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1378 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1379
1380 (defconst gnus-summary-local-variables 
1381   '(gnus-newsgroup-name 
1382     gnus-newsgroup-begin gnus-newsgroup-end 
1383     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1384     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1385     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1386     gnus-newsgroup-unselected gnus-newsgroup-marked
1387     gnus-newsgroup-replied gnus-newsgroup-expirable
1388     gnus-newsgroup-processable gnus-newsgroup-killed
1389     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1390     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1391     gnus-current-article gnus-current-headers gnus-have-all-headers
1392     gnus-last-article gnus-article-internal-prepare-hook
1393     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1394     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1395     gnus-newsgroup-threads gnus-newsgroup-async
1396     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1397     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1398     gnus-newsgroup-history gnus-newsgroup-ancient
1399     gnus-newsgroup-adaptive)
1400   "Variables that are buffer-local to the summary buffers.")
1401
1402 (defconst gnus-bug-message
1403   "Sending a bug report to the Gnus Towers.
1404 ========================================
1405
1406 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1407 be sent to the Gnus Bug Exterminators. 
1408
1409 At the bottom of the buffer you'll see lots of variable settings.
1410 Please do not delete those.  They will tell the Bug People what your
1411 environment is, so that it will be easier to locate the bugs.
1412
1413 If you have found a bug that makes Emacs go \"beep\", set
1414 debug-on-error to t (`M-ESC (setq debug-on-error t)') and include the
1415 backtrace in your bug report.
1416
1417 Please describe the bug in annoying, painstaking detail.
1418
1419 Thank you for your help in stamping out bugs.
1420 ")
1421
1422 ;;; End of variables.
1423
1424 ;; Define some autoload functions Gnus might use.
1425 (eval-and-compile
1426
1427   ;; Various 
1428   (autoload 'metamail-buffer "metamail")
1429   (autoload 'Info-goto-node "info")
1430   (autoload 'hexl-hex-string-to-integer "hexl")
1431   (autoload 'pp "pp")
1432   (autoload 'pp-to-string "pp")
1433   (autoload 'pp-eval-expression "pp")
1434   (autoload 'mail-extract-address-components "mail-extr")
1435
1436   (autoload 'nnmail-split-fancy "nnmail")
1437
1438   ;; timezone
1439   (autoload 'timezone-make-date-arpa-standard "timezone")
1440   (autoload 'timezone-fix-time "timezone")
1441   (autoload 'timezone-make-sortable-date "timezone")
1442   (autoload 'timezone-make-time-string "timezone")
1443
1444   ;; rmail & friends
1445   (autoload 'mail-position-on-field "sendmail")
1446   (autoload 'mail-setup "sendmail")
1447   (autoload 'rmail-output "rmailout")
1448   (autoload 'news-mail-other-window "rnewspost")
1449   (autoload 'news-reply-yank-original "rnewspost")
1450   (autoload 'news-caesar-buffer-body "rnewspost")
1451   (autoload 'rmail-insert-rmail-file-header "rmail")
1452   (autoload 'rmail-count-new-messages "rmail")
1453   (autoload 'rmail-show-message "rmail")
1454
1455   ;; gnus-mh
1456   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1457   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1458   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1459   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1460   (autoload 'gnus-Folder-save-name "gnus-mh")
1461   (autoload 'gnus-folder-save-name "gnus-mh")
1462
1463   ;; gnus-vis misc
1464   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1465   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1466   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1467   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1468   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1469   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1470   (autoload 'gnus-summary-highlight-line "gnus-vis")
1471   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1472
1473   ;; gnus-vis article
1474   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1475   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1476   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1477   (autoload 'gnus-article-hide "gnus-vis" nil t)
1478   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1479   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1480   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1481   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1482   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1483   (autoload 'gnus-article-add-button "gnus-vis")
1484
1485   ;; gnus-cite
1486   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1487   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1488   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1489
1490   ;; gnus-kill
1491   (autoload 'gnus-kill "gnus-kill")
1492   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1493   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1494   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1495   (autoload 'gnus-execute "gnus-kill")
1496   (autoload 'gnus-expunge "gnus-kill")
1497
1498   ;; gnus-cache
1499   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1500   (autoload 'gnus-cache-save-buffers "gnus-cache")
1501   (autoload 'gnus-cache-possibly-remove-article "gnus-cache")
1502   (autoload 'gnus-cache-request-article "gnus-cache")
1503   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1504   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1505   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1506
1507   ;; gnus-score
1508   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1509   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1510   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1511   (autoload 'gnus-score-save "gnus-score")
1512   (autoload 'gnus-score-headers "gnus-score")
1513   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1514   (autoload 'gnus-score-adaptive "gnus-score")
1515   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1516   (autoload 'gnus-possibly-score-headers "gnus-score")
1517
1518   ;; gnus-uu
1519   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1520   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1521   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1522   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1523   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1524   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1525   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1526   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1527   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1528   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1529   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1530   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1531   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1532   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1533   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1534   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1535
1536   ;; gnus-msg
1537   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1538   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1539   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1540   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1541   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1542   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1543   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1544   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1545   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1546   (autoload 'gnus-post-news "gnus-msg" nil t)
1547   (autoload 'gnus-inews-news "gnus-msg" nil t)
1548   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1549   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1550   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1551   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1552   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1553   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1554   (autoload 'gnus-mail-yank-original "gnus-msg")
1555   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1556   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1557   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1558   (autoload 'gnus-article-mail-with-original "gnus-msg")
1559   (autoload 'gnus-article-mail "gnus-msg")
1560
1561   ;; gnus-vm
1562   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1563   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1564   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1565   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1566   (autoload 'gnus-yank-article "gnus-vm" nil t)
1567
1568   )
1569
1570 \f
1571
1572 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1573 ;; If you want the cursor to go somewhere else, set these two
1574 ;; functions in some startup hook to whatever you want.
1575 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1576 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1577
1578 ;;; Various macros and substs.
1579
1580 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1581   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1582   (` (let ((GnusStartBufferWindow (selected-window)))
1583        (unwind-protect
1584            (progn
1585              (pop-to-buffer (, buffer))
1586              (,@ forms))
1587          (select-window GnusStartBufferWindow)))))
1588
1589 (defmacro gnus-gethash (string hashtable)
1590   "Get hash value of STRING in HASHTABLE."
1591   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1592   ;;(` (abbrev-expansion (, string) (, hashtable)))
1593   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1594
1595 (defmacro gnus-sethash (string value hashtable)
1596   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1597   ;; We cannot use define-abbrev since it only accepts string as value.
1598   ;; (set (intern string hashtable) value))
1599   (` (set (intern (, string) (, hashtable)) (, value))))
1600
1601 (defsubst gnus-buffer-substring (beg end)
1602   (buffer-substring (match-beginning beg) (match-end end)))
1603
1604 (defsubst gnus-simplify-subject-re (subject)
1605   "Remove \"Re:\" from subject lines."
1606   (let ((case-fold-search t))
1607     (if (string-match "^re: *" subject)
1608         (substring subject (match-end 0))
1609       subject)))
1610
1611 (defsubst gnus-goto-char (point)
1612   (and point (goto-char point)))
1613
1614 (defmacro gnus-buffer-exists-p (buffer)
1615   (` (and (, buffer)
1616           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1617                    (, buffer)))))
1618
1619 (defmacro gnus-kill-buffer (buffer)
1620   (` (if (gnus-buffer-exists-p (, buffer))
1621          (kill-buffer (, buffer)))))
1622
1623 (defsubst gnus-point-at-bol ()
1624   "Return point at the beginning of line."
1625   (let ((p (point)))
1626     (beginning-of-line)
1627     (prog1
1628         (point)
1629       (goto-char p))))
1630
1631 (defsubst gnus-point-at-eol ()
1632   "Return point at the beginning of line."
1633   (let ((p (point)))
1634     (end-of-line)
1635     (prog1
1636         (point)
1637       (goto-char p))))
1638
1639 \f
1640 ;;;
1641 ;;; Gnus Utility Functions
1642 ;;;
1643
1644 (defun gnus-extract-address-components (from)
1645   (let (name address)
1646     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1647         (setq address (substring from (match-beginning 0) (match-end 0))))
1648     (and address
1649          (string-match (concat "<" (regexp-quote address) ">") from)
1650          (setq name (substring from 0 (1- (match-beginning 0)))))
1651     (or name
1652         (and (string-match "(.+)" from)
1653              (setq name (substring from (1+ (match-beginning 0)) 
1654                                    (1- (match-end 0))))))
1655     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1656     (list (or name from) (or address from))))
1657
1658 (defun gnus-fetch-field (field)
1659   "Return the value of the header FIELD of current article."
1660   (save-excursion
1661     (save-restriction
1662       (let ((case-fold-search t))
1663         (gnus-narrow-to-headers)
1664         (mail-fetch-field field)))))
1665
1666 (defun gnus-goto-colon ()
1667   (beginning-of-line)
1668   (search-forward ":" (gnus-point-at-eol) t))
1669
1670 (defun gnus-narrow-to-headers ()
1671   (widen)
1672   (save-excursion
1673     (goto-char (point-min))
1674     (if (search-forward "\n\n")
1675         (narrow-to-region 1 (1- (point))))))
1676
1677 (defun gnus-update-format-specifications ()
1678   (gnus-make-thread-indent-array)
1679   (setq gnus-summary-line-format-spec 
1680         (gnus-parse-format
1681          gnus-summary-line-format gnus-summary-line-format-alist))
1682   (gnus-update-summary-mark-positions)
1683   (setq gnus-summary-dummy-line-format-spec 
1684         (gnus-parse-format gnus-summary-dummy-line-format 
1685                            gnus-summary-dummy-line-format-alist))
1686   (setq gnus-group-line-format-spec
1687         (gnus-parse-format 
1688          gnus-group-line-format 
1689          gnus-group-line-format-alist))
1690   (if (and (string-match "%D" gnus-group-line-format)
1691            (not gnus-description-hashtb)
1692            gnus-read-active-file)
1693       (gnus-read-descriptions-file))
1694   (setq gnus-summary-mode-line-format-spec 
1695         (gnus-parse-format gnus-summary-mode-line-format 
1696                            gnus-summary-mode-line-format-alist))
1697   (setq gnus-article-mode-line-format-spec 
1698         (gnus-parse-format gnus-article-mode-line-format 
1699                            gnus-summary-mode-line-format-alist))
1700   (setq gnus-group-mode-line-format-spec 
1701         (gnus-parse-format gnus-group-mode-line-format 
1702                            gnus-group-mode-line-format-alist)))
1703
1704 (defun gnus-update-summary-mark-positions ()
1705   (save-excursion
1706     (let ((gnus-replied-mark 129)
1707           (gnus-score-below-mark 130)
1708           (gnus-score-over-mark 130)
1709           (thread nil)
1710           pos)
1711       (gnus-set-work-buffer)
1712       (gnus-summary-insert-line 
1713        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1714       (goto-char (point-min))
1715       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1716                                          (- (point) 2)))))
1717       (goto-char (point-min))
1718       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1719                                           (- (point) 2))) pos))
1720       (goto-char (point-min))
1721       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1722                                         (- (point) 2))) pos))
1723       (setq gnus-summary-mark-positions pos))))
1724
1725 (defun gnus-format-max-width (form length)
1726   (let* ((val (eval form))
1727          (valstr (if (numberp val) (int-to-string val) val)))
1728     (if (> (length valstr) length)
1729         (substring valstr 0 length)
1730       valstr)))
1731
1732 (defun gnus-set-mouse-face (string)
1733   ;; Set mouse face property on STRING.
1734   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1735   string)
1736
1737 (defun gnus-parse-format (format spec-alist)
1738   ;; This function parses the FORMAT string with the help of the
1739   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1740   ;; string.  If the FORMAT string contains the specifiers %( and %)
1741   ;; the text between them will have the mouse-face text property.
1742   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1743       (if (and gnus-visual gnus-mouse-face)
1744           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1745                 (button (substring format (match-beginning 2) (match-end 2)))
1746                 (post (substring format (match-beginning 3) (match-end 3))))
1747             (list 'concat
1748                   (gnus-parse-simple-format pre spec-alist)
1749                   (list 'gnus-set-mouse-face
1750                         (gnus-parse-simple-format button spec-alist))
1751                   (gnus-parse-simple-format post spec-alist)))
1752         (gnus-parse-simple-format
1753          (concat (substring format (match-beginning 1) (match-end 1))
1754                  (substring format (match-beginning 2) (match-end 2))
1755                  (substring format (match-beginning 3) (match-end 3)))
1756          spec-alist))
1757     (gnus-parse-simple-format format spec-alist)))
1758
1759 (defun gnus-parse-simple-format (format spec-alist)
1760   ;; This function parses the FORMAT string with the help of the
1761   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1762   ;; string. The list will consist of the symbol `format', a format
1763   ;; specification string, and a list of forms depending on the
1764   ;; SPEC-ALIST.
1765   (let ((max-width 0)
1766         spec flist fstring b newspec max-width elem beg)
1767     (save-excursion
1768       (gnus-set-work-buffer)
1769       (insert format)
1770       (goto-char (point-min))
1771       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1772         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1773                                                      (match-end 2))))
1774         ;; First check if there are any specs that look anything like
1775         ;; "%12,12A", ie. with a "max width specification". These have
1776         ;; to be treated specially.
1777         (if (setq beg (match-beginning 1))
1778             (setq max-width 
1779                   (string-to-int 
1780                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1781           (setq max-width 0)
1782           (setq beg (match-beginning 2)))
1783         ;; Find the specification from `spec-alist'.
1784         (if (not (setq elem (cdr (assq spec spec-alist))))
1785             (setq elem '("*" ?s)))
1786         ;; Treat user defined format specifiers specially
1787         (and (eq (car elem) 'user-defined)
1788              (setq elem
1789                    (list 
1790                     (list (intern (concat "gnus-user-format-function-"
1791                                           (buffer-substring
1792                                            (match-beginning 3)
1793                                            (match-end 3))))
1794                           'header)
1795                     ?s))
1796              (delete-region (match-beginning 3) (match-end 3)))
1797         (if (not (zerop max-width))
1798             (let ((el (car elem)))
1799               (cond ((= (car (cdr elem)) ?c) 
1800                      (setq el (list 'char-to-string el)))
1801                     ((= (car (cdr elem)) ?d)
1802                      (numberp el) (setq el (list 'int-to-string el))))
1803               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1804                                 flist))
1805               (setq newspec ?s))
1806           (setq flist (cons (car elem) flist))
1807           (setq newspec (car (cdr elem))))
1808         ;; Remove the old specification (and possibly a ",12" string).
1809         (delete-region beg (match-end 2))
1810         ;; Insert the new specification.
1811         (goto-char beg)
1812         (insert newspec))
1813       (setq fstring (buffer-substring 1 (point-max))))
1814     (cons 'format (cons fstring (nreverse flist)))))
1815
1816 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1817 (defun gnus-read-init-file ()
1818   (and gnus-init-file
1819        (or (and (file-exists-p gnus-init-file) 
1820                 ;; Don't try to load a directory.
1821                 (not (file-directory-p gnus-init-file)))
1822            (file-exists-p (concat gnus-init-file ".el"))
1823            (file-exists-p (concat gnus-init-file ".elc")))
1824        (load gnus-init-file nil t)))
1825
1826 (defun gnus-set-work-buffer ()
1827   (if (get-buffer gnus-work-buffer)
1828       (progn
1829         (set-buffer gnus-work-buffer)
1830         (erase-buffer))
1831     (set-buffer (get-buffer-create gnus-work-buffer))
1832     (buffer-disable-undo (current-buffer))
1833     (gnus-add-current-to-buffer-list)))
1834
1835 ;; Article file names when saving.
1836
1837 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1838   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1839 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1840 Otherwise, it is like ~/News/news/group/num."
1841   (let ((default
1842           (expand-file-name
1843            (concat (if (gnus-use-long-file-name 'not-save)
1844                        (gnus-capitalize-newsgroup newsgroup)
1845                      (gnus-newsgroup-directory-form newsgroup))
1846                    "/" (int-to-string (header-number headers)))
1847            (or gnus-article-save-directory "~/News"))))
1848     (if (and last-file
1849              (string-equal (file-name-directory default)
1850                            (file-name-directory last-file))
1851              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1852         default
1853       (or last-file default))))
1854
1855 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1856   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1857 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1858 Otherwise, it is like ~/News/news/group/num."
1859   (let ((default
1860           (expand-file-name
1861            (concat (if (gnus-use-long-file-name 'not-save)
1862                        newsgroup
1863                      (gnus-newsgroup-directory-form newsgroup))
1864                    "/" (int-to-string (header-number headers)))
1865            (or gnus-article-save-directory "~/News"))))
1866     (if (and last-file
1867              (string-equal (file-name-directory default)
1868                            (file-name-directory last-file))
1869              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1870         default
1871       (or last-file default))))
1872
1873 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1874   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1875 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1876 Otherwise, it is like ~/News/news/group/news."
1877   (or last-file
1878       (expand-file-name
1879        (if (gnus-use-long-file-name 'not-save)
1880            (gnus-capitalize-newsgroup newsgroup)
1881          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1882        (or gnus-article-save-directory "~/News"))))
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            newsgroup
1892          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1893        (or gnus-article-save-directory "~/News"))))
1894
1895 ;; For subscribing new newsgroup
1896
1897 (defun gnus-subscribe-hierarchical-interactive (groups)
1898   (let ((groups (sort groups 'string<))
1899         prefixes prefix start rest ans group starts)
1900     (while groups
1901       (setq prefixes (list "^"))
1902       (while (and groups prefixes)
1903         (while (not (string-match (car prefixes) (car groups)))
1904           (setq prefixes (cdr prefixes)))
1905         (setq prefix (car prefixes))
1906         (setq start (1- (length prefix)))
1907         (if (and (string-match "[^\\.]\\." (car groups) start)
1908                  (cdr groups)
1909                  (setq prefix 
1910                        (concat "^" (substring (car groups) 0 (match-end 0))))
1911                  (string-match prefix (car (cdr groups))))
1912             (progn
1913               (setq prefixes (cons prefix prefixes))
1914               (message "Descend hierarchy %s? ([y]nsq): " 
1915                        (substring prefix 1 (1- (length prefix))))
1916               (setq ans (read-char))
1917               (cond ((= ans ?n)
1918                      (while (and groups 
1919                                  (string-match prefix 
1920                                                (setq group (car groups))))
1921                        (setq gnus-killed-list 
1922                              (cons group gnus-killed-list))
1923                        (gnus-sethash group group gnus-killed-hashtb)
1924                        (setq groups (cdr groups)))
1925                      (setq starts (cdr starts)))
1926                     ((= ans ?s)
1927                      (while (and groups 
1928                                  (string-match prefix 
1929                                                (setq group (car groups))))
1930                        (gnus-sethash group group gnus-killed-hashtb)
1931                        (gnus-subscribe-alphabetically (car groups))
1932                        (setq groups (cdr groups)))
1933                      (setq starts (cdr starts)))
1934                     ((= ans ?q)
1935                      (while groups
1936                        (setq group (car groups))
1937                        (setq gnus-killed-list (cons group gnus-killed-list))
1938                        (gnus-sethash group group gnus-killed-hashtb)
1939                        (setq groups (cdr groups))))
1940                     (t nil)))
1941           (message "Subscribe %s? ([n]yq)" (car groups))
1942           (setq ans (read-char))
1943           (setq group (car groups))
1944           (cond ((= ans ?y)
1945                  (gnus-subscribe-alphabetically (car groups))
1946                  (gnus-sethash group group gnus-killed-hashtb))
1947                 ((= ans ?q)
1948                  (while groups
1949                    (setq group (car groups))
1950                    (setq gnus-killed-list (cons group gnus-killed-list))
1951                    (gnus-sethash group group gnus-killed-hashtb)
1952                    (setq groups (cdr groups))))
1953                 (t 
1954                  (setq gnus-killed-list (cons group gnus-killed-list))
1955                  (gnus-sethash group group gnus-killed-hashtb)))
1956           (setq groups (cdr groups)))))))
1957
1958 (defun gnus-subscribe-randomly (newsgroup)
1959   "Subscribe new NEWSGROUP by making it the first newsgroup."
1960   (gnus-subscribe-newsgroup newsgroup))
1961
1962 (defun gnus-subscribe-alphabetically (newgroup)
1963   "Subscribe new NEWSGROUP and insert it in alphabetical order."
1964   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1965   (let ((groups (cdr gnus-newsrc-alist))
1966         before)
1967     (while (and (not before) groups)
1968       (if (string< newgroup (car (car groups)))
1969           (setq before (car (car groups)))
1970         (setq groups (cdr groups))))
1971     (gnus-subscribe-newsgroup newgroup before)))
1972
1973 (defun gnus-subscribe-hierarchically (newgroup)
1974   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
1975   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1976   (save-excursion
1977     (set-buffer (find-file-noselect gnus-current-startup-file))
1978     (let ((groupkey newgroup)
1979           before)
1980       (while (and (not before) groupkey)
1981         (goto-char (point-min))
1982         (let ((groupkey-re
1983                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
1984           (while (and (re-search-forward groupkey-re nil t)
1985                       (progn
1986                         (setq before (buffer-substring
1987                                       (match-beginning 1) (match-end 1)))
1988                         (string< before newgroup)))))
1989         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
1990         (setq groupkey
1991               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
1992                   (substring groupkey (match-beginning 1) (match-end 1)))))
1993       (gnus-subscribe-newsgroup newgroup before))))
1994
1995 (defun gnus-subscribe-interactively (newsgroup)
1996   "Subscribe new NEWSGROUP interactively.
1997 It is inserted in hierarchical newsgroup order if subscribed. If not,
1998 it is killed."
1999   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2000       (gnus-subscribe-hierarchically newsgroup)
2001     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2002
2003 (defun gnus-subscribe-zombies (newsgroup)
2004   "Make new NEWSGROUP a zombie group."
2005   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2006
2007 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2008   "Subscribe new NEWSGROUP.
2009 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2010 the first newsgroup."
2011   ;; We subscribe the group by changing its level to `subscribed'.
2012   (gnus-group-change-level 
2013    newsgroup gnus-level-default-subscribed
2014    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2015   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2016
2017 ;; For directories
2018
2019 (defun gnus-newsgroup-directory-form (newsgroup)
2020   "Make hierarchical directory name from NEWSGROUP name."
2021   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2022         (len (length newsgroup))
2023         idx)
2024     ;; If this is a foreign group, we don't want to translate the
2025     ;; entire name.  
2026     (if (setq idx (string-match ":" newsgroup))
2027         (aset newsgroup idx ?/)
2028       (setq idx 0))
2029     ;; Replace all occurrences of `.' with `/'.
2030     (while (< idx len)
2031       (if (= (aref newsgroup idx) ?.)
2032           (aset newsgroup idx ?/))
2033       (setq idx (1+ idx)))
2034     newsgroup))
2035
2036 (defun gnus-make-directory (dir)
2037   "Make DIRECTORY recursively."
2038   (let* ((dir (expand-file-name dir default-directory))
2039          dirs)
2040     (if (string-match "/$" dir)
2041         (setq dir (substring dir 0 (match-beginning 0))))
2042     (while (not (file-exists-p dir))
2043       (setq dirs (cons dir dirs))
2044       (string-match "/[^/]+$" dir)
2045       (setq dir (substring dir 0 (match-beginning 0))))
2046     (while dirs
2047       (make-directory (car dirs))
2048       (setq dirs (cdr dirs)))))
2049
2050 (defun gnus-capitalize-newsgroup (newsgroup)
2051   "Capitalize NEWSGROUP name."
2052   (and (not (zerop (length newsgroup)))
2053        (concat (char-to-string (upcase (aref newsgroup 0)))
2054                (substring newsgroup 1))))
2055
2056 ;; Var
2057
2058 (defun gnus-simplify-subject (subject &optional re-only)
2059   "Remove `Re:' and words in parentheses.
2060 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2061   (let ((case-fold-search t))           ;Ignore case.
2062     ;; Remove `Re:' and `Re^N:'.
2063     (if (string-match "^re:[ \t]*" subject)
2064         (setq subject (substring subject (match-end 0))))
2065     ;; Remove words in parentheses from end.
2066     (or re-only
2067         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2068           (setq subject (substring subject 0 (match-beginning 0)))))
2069     ;; Return subject string.
2070     subject))
2071
2072 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2073 ;; all whitespace.
2074 (defun gnus-simplify-subject-fuzzy (subject)
2075   (let ((case-fold-search t))
2076     (save-excursion
2077       (gnus-set-work-buffer)
2078       (insert subject)
2079       (gnus-simplify-buffer-fuzzy)
2080       (buffer-string))))
2081
2082 (defun gnus-simplify-buffer-fuzzy ()
2083   (goto-char (point-min))
2084   (while (re-search-forward "^[ \t]*re:[ \t]*" nil t)
2085     (replace-match "" t t))
2086   (goto-char (point-min))
2087   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2088     (replace-match "" t t))
2089   (goto-char (point-min))
2090   (while (re-search-forward "[ \t]+" nil t)
2091     (replace-match " " t t))
2092   (goto-char (point-min))
2093   (while (re-search-forward "[ \t]+$" nil t)
2094     (replace-match "" t t))
2095   (goto-char (point-min))
2096   (while (re-search-forward "^[ \t]+" nil t)
2097     (replace-match "" t t)))
2098
2099 ;; Add the current buffer to the list of buffers to be killed on exit. 
2100 (defun gnus-add-current-to-buffer-list ()
2101   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
2102
2103 (defun gnus-string> (s1 s2)
2104   (not (or (string< s1 s2)
2105            (string= s1 s2))))
2106
2107 ;; Functions accessing headers.
2108 ;; Functions are more convenient than macros in some cases.
2109
2110 (defun gnus-header-number (header)
2111   (header-number header))
2112
2113 (defun gnus-header-subject (header)
2114   (header-subject header))
2115
2116 (defun gnus-header-from (header)
2117   (header-from header))
2118
2119 (defun gnus-header-xref (header)
2120   (header-xref header))
2121
2122 (defun gnus-header-lines (header)
2123   (header-lines header))
2124
2125 (defun gnus-header-date (header)
2126   (header-date header))
2127
2128 (defun gnus-header-id (header)
2129   (header-id header))
2130
2131 (defun gnus-header-references (header)
2132   (header-references header))
2133
2134 ;;; General various misc type functions.
2135
2136 (defun gnus-clear-system ()
2137   "Clear all variables and buffers."
2138   ;; Clear Gnus variables.
2139   (let ((variables gnus-variable-list))
2140     (while variables
2141       (set (car variables) nil)
2142       (setq variables (cdr variables))))
2143   ;; Clear other internal variables.
2144   (setq gnus-list-of-killed-groups nil
2145         gnus-have-read-active-file nil
2146         gnus-newsrc-alist nil
2147         gnus-newsrc-hashtb nil
2148         gnus-killed-list nil
2149         gnus-zombie-list nil
2150         gnus-killed-hashtb nil
2151         gnus-active-hashtb nil
2152         gnus-moderated-list nil
2153         gnus-description-hashtb nil
2154         gnus-newsgroup-headers nil
2155         gnus-newsgroup-headers-hashtb-by-number nil
2156         gnus-newsgroup-name nil
2157         gnus-server-alist nil
2158         gnus-current-select-method nil)
2159   ;; Reset any score variables.
2160   (and (boundp 'gnus-score-cache)
2161        (set 'gnus-score-cache nil))
2162   (and (boundp 'gnus-internal-global-score-files)
2163        (set 'gnus-internal-global-score-files nil))
2164   ;; Kill the startup file.
2165   (and gnus-current-startup-file
2166        (get-file-buffer gnus-current-startup-file)
2167        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2168   ;; Save any cache buffers.
2169   (and gnus-use-cache (gnus-cache-save-buffers))
2170   ;; Clear the dribble buffer.
2171   (gnus-dribble-clear)
2172   ;; Kill global KILL file buffer.
2173   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2174       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2175   (gnus-kill-buffer nntp-server-buffer)
2176   ;; Kill Gnus buffers.
2177   (while gnus-buffer-list
2178     (gnus-kill-buffer (car gnus-buffer-list))
2179     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2180
2181 (defun gnus-windows-old-to-new (setting)
2182   (if (symbolp setting)
2183       (setq setting 
2184             (cond ((eq setting 'SelectArticle)
2185                    'article)
2186                   ((eq setting 'SelectSubject)
2187                    'summary)
2188                   ((eq setting 'SelectNewsgroup)
2189                    'group)
2190                   (t setting))))
2191   (if (or (listp setting)
2192           (not (and gnus-window-configuration
2193                     (memq setting '(group summary article)))))
2194       setting
2195     (let* ((setting (if (eq setting 'group) 
2196                         (if (assq 'newsgroup gnus-window-configuration)
2197                             'newsgroup
2198                           'newsgroups) setting))
2199            (elem (car (cdr (assq setting gnus-window-configuration))))
2200            (total (apply '+ elem))
2201            (types '(group summary article))
2202            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2203            (i 0)
2204            perc
2205            out)
2206       (while (< i 3)
2207         (or (zerop (nth i elem))
2208             (progn
2209               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2210               (setq out (cons (if (eq pbuf (nth i types))
2211                                   (vector (nth i types) perc 'point)
2212                                 (vector (nth i types) perc))
2213                               out))))
2214         (setq i (1+ i)))
2215       (list (nreverse out)))))
2216            
2217 (defun gnus-configure-windows (setting)
2218   (setq setting (gnus-windows-old-to-new setting))
2219   (let ((r (if (symbolp setting)
2220                   (cdr (assq setting gnus-buffer-configuration))
2221                 setting))
2222         (in-buf (current-buffer))
2223         rule val window w height hor ohor heights sub jump-buffer
2224         rel total to-buf)
2225     (or r (error "No such setting: %s" setting))
2226
2227     ;; Either remove all windows or just remove all Gnus windows.
2228     (if gnus-use-full-window
2229         (delete-other-windows)
2230       (gnus-remove-some-windows)
2231       (switch-to-buffer nntp-server-buffer))
2232
2233     (while r
2234       (setq hor (car r)
2235             ohor nil)
2236
2237       ;; We have to do the (possible) horizontal splitting before the
2238       ;; vertical. 
2239       (if (and (listp (car hor)) 
2240                (eq (car (car hor)) 'horizontal))
2241           (progn
2242             (split-window nil (floor (* (frame-width) (nth 1 (car hor)))) t)
2243             (setq hor (cdr hor))))
2244
2245       ;; Go through the rules and eval the elements that are to be
2246       ;; evaled.  
2247       (while hor
2248         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2249             (progn
2250               ;; Expand short buffer name.
2251               (setq w (aref val 0))
2252               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2253                    (progn
2254                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2255                      (aset val 0 w)))
2256               (setq ohor (cons val ohor))))
2257         (setq hor (cdr hor)))
2258       (setq rule (cons (nreverse ohor) rule))
2259       (setq r (cdr r)))
2260     (setq rule (nreverse rule))
2261
2262     ;; We tally the window sizes.
2263     (setq total (window-height))
2264     (while rule
2265       (setq hor (car rule))
2266       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2267           (setq hor (cdr hor)))
2268       (setq sub 0)
2269       (while hor
2270         (setq rel (aref (car hor) 1)
2271               heights (cons
2272                        (cond ((and (floatp rel) (= 1.0 rel))
2273                               'x)
2274                              ((integerp rel)
2275                               rel)
2276                              (t
2277                               (max (floor (* total rel)) 4)))
2278                        heights)
2279               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2280               hor (cdr hor)))
2281       (setq heights (nreverse heights)
2282             hor (car rule))
2283       
2284       ;; We then go through these heighs and create windows for them.
2285       (while heights
2286         (setq height (car heights)
2287               heights (cdr heights))
2288         (and (eq height 'x)
2289              (setq height (- total sub)))
2290         (and heights
2291              (split-window nil height))
2292         (setq to-buf (aref (car hor) 0))
2293         (switch-to-buffer 
2294          (cond ((not to-buf)
2295                 in-buf)
2296                ((symbolp to-buf)
2297                 (symbol-value (aref (car hor) 0)))
2298                (t
2299                 (aref (car hor) 0))))
2300         (and (> (length (car hor)) 2)
2301              (eq (aref (car hor) 2) 'point)
2302              (setq jump-buffer (current-buffer)))
2303         (other-window 1)
2304         (setq hor (cdr hor)))
2305       
2306       (setq rule (cdr rule)))
2307
2308     ;; Finally, we pop to the buffer that's supposed to have point. 
2309     (pop-to-buffer jump-buffer)
2310     jump-buffer))
2311       
2312 (defun gnus-remove-some-windows ()
2313   (let ((buffers gnus-window-to-buffer)
2314         (first t)
2315         buf)
2316     (while buffers
2317       (setq buf (cdr (car buffers)))
2318       (if (symbolp buf)
2319           (setq buf (and (boundp buf) (symbol-value buf))))
2320       (and buf 
2321            (get-buffer-window buf)
2322            (progn
2323              (set-buffer buf)
2324              (if first
2325                  (progn
2326                    (switch-to-buffer nntp-server-buffer)
2327                    (setq first nil))
2328                (delete-window (get-buffer-window buf)))))
2329       (setq buffers (cdr buffers)))
2330     (set-buffer nntp-server-buffer)))
2331
2332 (defun gnus-version ()
2333   "Version numbers of this version of Gnus."
2334   (interactive)
2335   (let ((methods gnus-valid-select-methods)
2336         (mess gnus-version)
2337         meth)
2338     ;; Go through all the legal select methods and add their version
2339     ;; numbers to the total version string. Only the backends that are
2340     ;; currently in use will have their message numbers taken into
2341     ;; consideration. 
2342     (while methods
2343       (setq meth (intern (concat (car (car methods)) "-version")))
2344       (and (boundp meth)
2345            (stringp (symbol-value meth))
2346            (setq mess (concat mess "; " (symbol-value meth))))
2347       (setq methods (cdr methods)))
2348     (gnus-message 2 mess)))
2349
2350 (defun gnus-info-find-node ()
2351   "Find Info documentation of Gnus."
2352   (interactive)
2353   ;; Enlarge info window if needed.
2354   (let ((mode major-mode))
2355     (gnus-configure-windows 'info)
2356     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2357
2358 (defun gnus-bug ()
2359   "Send a bug report to the Gnus maintainers."
2360   (interactive)
2361   (let ((winconf (current-window-configuration)))
2362     (delete-other-windows)
2363     (switch-to-buffer "*Gnus Bug Help*")
2364     (erase-buffer)
2365     (insert gnus-bug-message)
2366     (goto-char (point-min))
2367     (pop-to-buffer "*Gnus Bug*")
2368     (erase-buffer)
2369     (mail-mode)
2370     (mail-setup gnus-maintainer "[Gnus Bug Report] " nil nil nil nil)
2371     (make-local-variable 'gnus-prev-winconf)
2372     (setq gnus-prev-winconf winconf)
2373     (goto-char (point-min))
2374     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2375     (forward-line 1)
2376     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
2377     (let ((b (point)))
2378       (gnus-debug)
2379       (goto-char (- b 3)))
2380     (message "")))
2381
2382 (defun gnus-debug ()
2383   "Attemps to go through the Gnus source file and report what variables have been changed.
2384 The source file has to be in the Emacs load path."
2385   (interactive)
2386   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el"))
2387         file dirs expr olist)
2388     (save-excursion
2389       (set-buffer (get-buffer-create " *gnus bug info*"))
2390       (buffer-disable-undo (current-buffer))
2391       (message "Please wait while we snoop your variables...")
2392       (sit-for 0)
2393       (while files
2394         (erase-buffer)
2395         (setq dirs load-path)
2396         (while dirs
2397           (if (not (file-exists-p 
2398                     (setq file (concat (file-name-as-directory 
2399                                         (car dirs)) (car files)))))
2400               (setq dirs (cdr dirs))
2401             (setq dirs nil)
2402             (insert-file-contents file)
2403             (goto-char (point-min))
2404             (or (re-search-forward "^;;* Internal variables" nil t)
2405                 (error "Malformed sources in file %s" file))
2406             (narrow-to-region (point-min) (point))
2407             (goto-char (point-min))
2408             (while (setq expr (condition-case () 
2409                                   (read (current-buffer)) (error nil)))
2410               (and (eq (car expr) 'defvar)
2411                    (stringp (nth 3 expr))
2412                    (or (not (boundp (nth 1 expr)))
2413                        (not (equal (eval (nth 2 expr))
2414                                    (symbol-value (nth 1 expr)))))
2415                    (setq olist (cons (nth 1 expr) olist))))))
2416         (setq files (cdr files)))
2417       (kill-buffer (current-buffer)))
2418     (insert "------------------- Environment follows -------------------\n\n")
2419     (while olist
2420       (if (boundp (car olist))
2421           (insert "(setq " (symbol-name (car olist)) " '" 
2422                   (prin1-to-string (symbol-value (car olist))) ")\n")
2423         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2424       (setq olist (cdr olist)))
2425     (insert "\n\n")))
2426
2427 (defun gnus-overload-functions (&optional overloads)
2428   "Overload functions specified by optional argument OVERLOADS.
2429 If nothing is specified, use the variable gnus-overload-functions."
2430   (let ((defs nil)
2431         (overloads (or overloads gnus-overload-functions)))
2432     (while overloads
2433       (setq defs (car overloads))
2434       (setq overloads (cdr overloads))
2435       ;; Load file before overloading function if necessary.  Make
2436       ;; sure we cannot use `require' always.
2437       (and (not (fboundp (car defs)))
2438            (car (cdr (cdr defs)))
2439            (load (car (cdr (cdr defs))) nil 'nomessage))
2440       (fset (car defs) (car (cdr defs))))))
2441
2442 (defun gnus-replace-chars-in-string (string from to)
2443   "Replace characters in STRING from FROM to TO."
2444   (let ((string (substring string 0))   ;Copy string.
2445         (len (length string))
2446         (idx 0))
2447     ;; Replace all occurrences of FROM with TO.
2448     (while (< idx len)
2449       (if (= (aref string idx) from)
2450           (aset string idx to))
2451       (setq idx (1+ idx)))
2452     string))
2453
2454 (defun gnus-days-between (date1 date2)
2455   ;; Return the number of days between date1 and date2.
2456   (- (gnus-day-number date1) (gnus-day-number date2)))
2457
2458 (defun gnus-day-number (date)
2459   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2460                      (timezone-parse-date date))))
2461     (timezone-absolute-from-gregorian 
2462      (nth 1 dat) (nth 2 dat) (car dat))))
2463
2464 ;; Returns a floating point number that says how many seconds have
2465 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2466 (defun gnus-seconds-since-epoch (date)
2467   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2468                         (timezone-parse-date date)))
2469          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2470                         (timezone-parse-time
2471                          (aref (timezone-parse-date date) 3))))
2472          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2473                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2474          (tday (- (timezone-absolute-from-gregorian 
2475                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2476                   (timezone-absolute-from-gregorian 
2477                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2478     (+ (nth 2 ttime)
2479        (* (nth 1 ttime) 60)
2480        (* 1.0 (nth 0 ttime) 60 60)
2481        (* 1.0 tday 60 60 24))))
2482
2483 (defun gnus-file-newer-than (file date)
2484   (let ((fdate (nth 5 (file-attributes file))))
2485     (or (> (car fdate) (car date))
2486         (and (= (car fdate) (car date))
2487              (> (nth 1 fdate) (nth 1 date))))))
2488
2489 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2490 ;; the echo area.
2491 (defun gnus-y-or-n-p (prompt)
2492   (prog1
2493       (y-or-n-p prompt)
2494     (message "")))
2495
2496 (defun gnus-yes-or-no-p (prompt)
2497   (prog1
2498       (yes-or-no-p prompt)
2499     (message "")))
2500
2501 ;; Check whether to use long file names.
2502 (defun gnus-use-long-file-name (symbol)
2503   ;; The variable has to be set...
2504   (and gnus-use-long-file-name
2505        ;; If it isn't a list, then we return t.
2506        (or (not (listp gnus-use-long-file-name))
2507            ;; If it is a list, and the list contains `symbol', we
2508            ;; return nil.  
2509            (not (memq symbol gnus-use-long-file-name)))))
2510
2511 ;; I suspect there's a better way, but I haven't taken the time to do
2512 ;; it yet. -erik selberg@cs.washington.edu
2513 (defun gnus-dd-mmm (messy-date)
2514   "Return a string like DD-MMM from a big messy string"
2515   (let ((datevec (timezone-parse-date messy-date)))
2516     (format "%2s-%s"
2517             (or (aref datevec 2) "??")
2518             (capitalize
2519              (or (car 
2520                   (nth (1- (string-to-number (aref datevec 1)))
2521                        timezone-months-assoc))
2522                  "???")))))
2523
2524 ;; Make a hash table (default and minimum size is 255).
2525 ;; Optional argument HASHSIZE specifies the table size.
2526 (defun gnus-make-hashtable (&optional hashsize)
2527   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2528
2529 ;; Make a number that is suitable for hashing; bigger than MIN and one
2530 ;; less than 2^x.
2531 (defun gnus-create-hash-size (min)
2532   (let ((i 1))
2533     (while (< i min)
2534       (setq i (* 2 i)))
2535     (1- i)))
2536
2537 ;; Delete the current line (and the next N lines.);
2538 (defun gnus-delete-line (&optional n)
2539   (let ((n (or n 1)))
2540     (delete-region (progn (beginning-of-line) (point))
2541                    (progn (forward-line n) (point)))))
2542
2543 ;; Show message if message has a lower level than `gnus-verbose'. 
2544 ;; Guide-line for numbers:
2545 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2546 ;; for things that take a long time, 7 - not very important messages
2547 ;; on stuff, 9 - messages inside loops.
2548 (defun gnus-message (level &rest args)
2549   (if (<= level gnus-verbose)
2550       (apply 'message args)
2551     ;; We have to do this format thingie here even if the result isn't
2552     ;; shown - the return value has to be the same as the return value
2553     ;; from `message'.
2554     (apply 'format args)))
2555
2556 ;;; List and range functions
2557
2558 (defun gnus-last-element (list)
2559   "Return last element of LIST."
2560   (while (cdr list)
2561     (setq list (cdr list)))
2562   (car list))
2563
2564 (defun gnus-copy-sequence (list)
2565   "Do a complete, total copy of a list."
2566   (if (and (consp list) (not (consp (cdr list))))
2567       (cons (car list) (cdr list))
2568     (mapcar (lambda (elem) (if (consp elem) 
2569                                (if (consp (cdr elem))
2570                                    (gnus-copy-sequence elem)
2571                                  (cons (car elem) (cdr elem)))
2572                              elem))
2573             list)))
2574
2575 (defun gnus-set-difference (list1 list2)
2576   "Return a list of elements of LIST1 that do not appear in LIST2."
2577   (let ((list1 (copy-sequence list1)))
2578     (while list2
2579       (setq list1 (delq (car list2) list1))
2580       (setq list2 (cdr list2)))
2581     list1))
2582
2583 (defun gnus-sorted-complement (list1 list2)
2584   "Return a list of elements of LIST1 that do not appear in LIST2.
2585 Both lists have to be sorted over <."
2586   (let (out)
2587     (if (or (null list1) (null list2))
2588         (or list1 list2)
2589       (while (and list1 list2)
2590         (cond ((= (car list1) (car list2))
2591                (setq list1 (cdr list1)
2592                      list2 (cdr list2)))
2593               ((< (car list1) (car list2))
2594                (setq out (cons (car list1) out))
2595                (setq list1 (cdr list1)))
2596               (t
2597                (setq out (cons (car list2) out))
2598                (setq list2 (cdr list2)))))
2599       (nconc (nreverse out) (or list1 list2)))))
2600
2601 (defun gnus-intersection (list1 list2)      
2602   (let ((result nil))
2603     (while list2
2604       (if (memq (car list2) list1)
2605           (setq result (cons (car list2) result)))
2606       (setq list2 (cdr list2)))
2607     result))
2608
2609 (defun gnus-sorted-intersection (list1 list2)
2610   ;; LIST1 and LIST2 have to be sorted over <.
2611   (let (out)
2612     (while (and list1 list2)
2613       (cond ((= (car list1) (car list2))
2614              (setq out (cons (car list1) out)
2615                    list1 (cdr list1)
2616                    list2 (cdr list2)))
2617             ((< (car list1) (car list2))
2618              (setq list1 (cdr list1)))
2619             (t
2620              (setq list2 (cdr list2)))))
2621     (nreverse out)))
2622
2623 (defun gnus-set-sorted-intersection (list1 list2)
2624   ;; LIST1 and LIST2 have to be sorted over <.
2625   ;; This function modifies LIST1.
2626   (let* ((top (cons nil list1))
2627          (prev top))
2628   (while (and list1 list2)
2629     (cond ((= (car list1) (car list2))
2630            (setq prev list1
2631                  list1 (cdr list1)
2632                  list2 (cdr list2)))
2633           ((< (car list1) (car list2))
2634            (setcdr prev (cdr list1))
2635            (setq list1 (cdr list1)))
2636           (t
2637            (setq list2 (cdr list2)))))
2638   (setcdr prev nil)
2639   (cdr top)))
2640
2641 (defun gnus-compress-sequence (numbers &optional always-list)
2642   "Convert list of numbers to a list of ranges or a single range.
2643 If ALWAYS-LIST is non-nil, this function will always release a list of
2644 ranges."
2645   (let* ((first (car numbers))
2646          (last (car numbers))
2647          result)
2648     (if (null numbers)
2649         nil
2650       (if (not (listp (cdr numbers)))
2651           numbers
2652         (while numbers
2653           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2654                 ((= (1+ last) (car numbers)) ;Still in sequence
2655                  (setq last (car numbers)))
2656                 (t                      ;End of one sequence
2657                  (setq result 
2658                        (cons (if (= first last) first
2659                                (cons first last)) result))
2660                  (setq first (car numbers))
2661                  (setq last  (car numbers))))
2662           (setq numbers (cdr numbers)))
2663         (if (and (not always-list) (null result))
2664             (if (= first last) (list first) (cons first last))
2665           (nreverse (cons (if (= first last) first (cons first last))
2666                           result)))))))
2667
2668 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2669 (defun gnus-uncompress-range (ranges)
2670   "Expand a list of ranges into a list of numbers.
2671 RANGES is either a single range on the form `(num . num)' or a list of
2672 these ranges."
2673   (let (first last result)
2674     (cond 
2675      ((null ranges)
2676       nil)
2677      ((not (listp (cdr ranges)))
2678       (setq first (car ranges))
2679       (setq last (cdr ranges))
2680       (while (<= first last)
2681         (setq result (cons first result))
2682         (setq first (1+ first)))
2683       (nreverse result))
2684      (t
2685       (while ranges
2686         (if (atom (car ranges))
2687             (if (numberp (car ranges))
2688                 (setq result (cons (car ranges) result)))
2689           (setq first (car (car ranges)))
2690           (setq last  (cdr (car ranges)))
2691           (while (<= first last)
2692             (setq result (cons first result))
2693             (setq first (1+ first))))
2694         (setq ranges (cdr ranges)))
2695       (nreverse result)))))
2696
2697 (defun gnus-add-to-range (ranges list)
2698   "Return a list of ranges that has all articles from both RANGES and LIST.
2699 Note: LIST has to be sorted over `<'."
2700   (if (not ranges)
2701       (gnus-compress-sequence list t)
2702     (setq list (copy-sequence list))
2703     (or (listp (cdr ranges))
2704         (setq ranges (list ranges)))
2705     (let ((out ranges)
2706           ilist lowest highest temp)
2707       (while (and ranges list)
2708         (setq ilist list)
2709         (setq lowest (or (and (atom (car ranges)) (car ranges))
2710                          (car (car ranges))))
2711         (while (and list (cdr list) (< (car (cdr list)) lowest))
2712           (setq list (cdr list)))
2713         (if (< (car ilist) lowest)
2714             (progn
2715               (setq temp list)
2716               (setq list (cdr list))
2717               (setcdr temp nil)
2718               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2719         (setq highest (or (and (atom (car ranges)) (car ranges))
2720                           (cdr (car ranges))))
2721         (while (and list (<= (car list) highest))
2722           (setq list (cdr list)))
2723         (setq ranges (cdr ranges)))
2724       (if list
2725           (setq out (nconc (gnus-compress-sequence list t) out)))
2726       (setq out (sort out (lambda (r1 r2) 
2727                             (< (or (and (atom r1) r1) (car r1))
2728                                (or (and (atom r2) r2) (car r2))))))
2729       (setq ranges out)
2730       (while ranges
2731         (if (atom (car ranges))
2732             (if (cdr ranges)
2733                 (if (atom (car (cdr ranges)))
2734                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2735                         (progn
2736                           (setcar ranges (cons (car ranges) 
2737                                                (car (cdr ranges))))
2738                           (setcdr ranges (cdr (cdr ranges)))))
2739                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2740                       (progn
2741                         (setcar (car (cdr ranges)) (car ranges))
2742                         (setcar ranges (car (cdr ranges)))
2743                         (setcdr ranges (cdr (cdr ranges)))))))
2744           (if (cdr ranges)
2745               (if (atom (car (cdr ranges)))
2746                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2747                       (progn
2748                         (setcdr (car ranges) (car (cdr ranges)))
2749                         (setcdr ranges (cdr (cdr ranges)))))
2750                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2751                     (progn
2752                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2753                       (setcdr ranges (cdr (cdr ranges))))))))
2754         (setq ranges (cdr ranges)))
2755       out)))
2756
2757 (defun gnus-remove-from-range (ranges list)
2758   "Return a list of ranges that has all articles from LIST removed from RANGES.
2759 Note: LIST has to be sorted over `<'."
2760   ;; !!! This function shouldn't look like this, but I've got a headache.
2761   (gnus-compress-sequence 
2762    (gnus-sorted-complement
2763     (gnus-uncompress-range ranges) list)))
2764
2765 (defun gnus-member-of-range (number ranges)
2766   (if (not (listp (cdr ranges)))
2767       (and (>= number (car ranges)) 
2768            (<= number (cdr ranges)))
2769     (let ((not-stop t))
2770       (while (and ranges 
2771                   (if (numberp (car ranges))
2772                       (>= number (car ranges))
2773                     (>= number (car (car ranges))))
2774                   not-stop)
2775         (if (if (numberp (car ranges))
2776                 (= number (car ranges))
2777               (and (>= number (car (car ranges)))
2778                    (<= number (cdr (car ranges)))))
2779             (setq not-stop nil))
2780         (setq ranges (cdr ranges)))
2781       (not not-stop))))
2782
2783 \f
2784 ;;;
2785 ;;; Gnus group mode
2786 ;;;
2787
2788 (defvar gnus-group-mode-map nil)
2789 (defvar gnus-group-group-map nil)
2790 (defvar gnus-group-mark-map nil)
2791 (defvar gnus-group-list-map nil)
2792 (defvar gnus-group-sub-map nil)
2793 (put 'gnus-group-mode 'mode-class 'special)
2794
2795 (if gnus-group-mode-map
2796     nil
2797   (setq gnus-group-mode-map (make-keymap))
2798   (suppress-keymap gnus-group-mode-map)
2799   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2800   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2801   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2802   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2803   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2804   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2805   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2806   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2807   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2808   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2809   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2810   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2811   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2812   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2813   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2814   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2815   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2816   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2817   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2818   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2819   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2820   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2821   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2822   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2823   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2824   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2825   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2826   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2827   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2828   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2829   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2830   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2831   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2832   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2833   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2834   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2835   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2836   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2837   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2838   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2839   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2840   (define-key gnus-group-mode-map "V" 'gnus-version)
2841   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2842   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2843   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2844   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2845   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2846   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2847   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2848   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2849   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2850   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2851   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
2852   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2853   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2854   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
2855
2856   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
2857   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
2858   (define-prefix-command 'gnus-group-mark-map)
2859   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
2860   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
2861   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
2862   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
2863
2864   (define-prefix-command 'gnus-group-group-map)
2865   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
2866   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
2867   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
2868   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
2869   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
2870   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
2871   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
2872   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
2873   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
2874   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
2875
2876   (define-prefix-command 'gnus-group-list-map)
2877   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
2878   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
2879   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
2880   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
2881   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
2882   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
2883   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
2884   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
2885   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
2886
2887   (define-prefix-command 'gnus-group-sub-map)
2888   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
2889   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
2890   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
2891   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
2892   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
2893   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
2894   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
2895   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
2896
2897 (defun gnus-group-mode ()
2898   "Major mode for reading news.
2899
2900 All normal editing commands are switched off.
2901 \\<gnus-group-mode-map>
2902 The group buffer lists (some of) the groups available.  For instance,
2903 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
2904 lists all zombie groups. 
2905
2906 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
2907 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
2908
2909 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
2910
2911 The following commands are available:
2912
2913 \\{gnus-group-mode-map}"
2914   (interactive)
2915   (if gnus-visual (gnus-group-make-menu-bar))
2916   (kill-all-local-variables)
2917   (setq mode-line-modified "-- ")
2918   (make-local-variable 'mode-line-format)
2919   (setq mode-line-format (copy-sequence mode-line-format))
2920   (and (equal (nth 3 mode-line-format) "   ")
2921        (setcar (nthcdr 3 mode-line-format) ""))
2922   (setq major-mode 'gnus-group-mode)
2923   (setq mode-name "Group")
2924   (gnus-group-set-mode-line)
2925   (setq mode-line-process nil)
2926   (use-local-map gnus-group-mode-map)
2927   (buffer-disable-undo (current-buffer))
2928   (setq truncate-lines t)
2929   (setq buffer-read-only t)
2930   (run-hooks 'gnus-group-mode-hook))
2931
2932 (defun gnus-mouse-pick-group (e)
2933   (interactive "e")
2934   (mouse-set-point e)
2935   (gnus-group-read-group nil))
2936
2937 ;;;###autoload
2938 (defun gnus-no-server (&optional arg)
2939   "Read network news.
2940 If ARG is a positive number, Gnus will use that as the
2941 startup level. If ARG is nil, Gnus will be started at level 2. 
2942 If ARG is non-nil and not a positive number, Gnus will
2943 prompt the user for the name of an NNTP server to use.
2944 As opposed to `gnus', this command will not connect to the local server."
2945   (interactive "P")
2946   (gnus (or arg (1- gnus-level-default-subscribed)) t))
2947
2948 (defalias '\(ding\) 'gnus)
2949
2950 ;;;###autoload
2951 (defun gnus (&optional arg dont-connect)
2952   "Read network news.
2953 If ARG is non-nil and a positive number, Gnus will use that as the
2954 startup level. If ARG is non-nil and not a positive number, Gnus will
2955 prompt the user for the name of an NNTP server to use."
2956   (interactive "P")
2957   (if (get-buffer gnus-group-buffer)
2958       (progn
2959         (switch-to-buffer gnus-group-buffer)
2960         (gnus-group-get-new-news))
2961     (gnus-clear-system)
2962     (gnus-read-init-file)
2963     (let ((level (and arg (numberp arg) (> arg 0) arg))
2964           did-connect)
2965       (unwind-protect
2966           (progn
2967             (gnus-group-setup-buffer)
2968             (or dont-connect 
2969                 (setq did-connect
2970                       (gnus-start-news-server (and arg (not level))))))
2971         (if (and (not dont-connect) 
2972                  (not did-connect))
2973             (gnus-group-quit)
2974           (run-hooks 'gnus-startup-hook)
2975           ;; NNTP server is successfully open. 
2976           (gnus-update-format-specifications)
2977           (let ((buffer-read-only nil))
2978             (erase-buffer)
2979             (if (not gnus-inhibit-startup-message)
2980                 (progn
2981                   (gnus-group-startup-message)
2982                   (sit-for 0))))
2983           (gnus-setup-news nil level)
2984           (and gnus-use-dribble-file (gnus-dribble-open))
2985           (gnus-group-list-groups level)
2986           (gnus-configure-windows 'group))))))
2987
2988 (defun gnus-group-startup-message (&optional x y)
2989   "Insert startup message in current buffer."
2990   ;; Insert the message.
2991   (erase-buffer)
2992   (insert
2993    (format "
2994     %s
2995            A newsreader 
2996       for GNU Emacs
2997
2998         Based on GNUS 
2999              written by 
3000      Masanobu UMEDA
3001
3002     Lars Magne 
3003          Ingebrigtsen 
3004       larsi@ifi.uio.no
3005
3006            gnus-version))
3007   ;; And then hack it.
3008   ;; 18 is the longest line.
3009   (indent-rigidly (point-min) (point-max) 
3010                   (/ (max (- (window-width) (or x 28)) 0) 2))
3011   (goto-char (point-min))
3012   ;; +4 is fuzzy factor.
3013   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3014
3015 (defun gnus-group-setup-buffer ()
3016   (or (get-buffer gnus-group-buffer)
3017       (progn
3018         (switch-to-buffer gnus-group-buffer)
3019         (gnus-add-current-to-buffer-list)
3020         (gnus-group-mode)
3021         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3022
3023 (defun gnus-group-list-groups (level &optional unread)
3024   "List newsgroups with level LEVEL or lower that have unread alticles.
3025 Default is all subscribed groups.
3026 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3027   (interactive (list (and current-prefix-arg
3028                           (prefix-numeric-value current-prefix-arg))))
3029   (setq level (or level gnus-group-default-list-level gnus-level-subscribed))
3030   (gnus-group-setup-buffer)     ;May call from out of group buffer
3031   (let ((case-fold-search nil)
3032         (group (gnus-group-group-name)))
3033     (funcall gnus-group-prepare-function level unread nil)
3034     (if (zerop (buffer-size))
3035         (gnus-message 5 gnus-no-groups-message)
3036       (goto-char (point-min))
3037       (if (not group)
3038           ;; Go to the first group with unread articles.
3039           (gnus-group-search-forward nil nil nil t)
3040         ;; Find the right group to put point on. If the current group
3041         ;; has disapeared in the new listing, try to find the next
3042         ;; one. If no next one can be found, just leave point at the
3043         ;; first newsgroup in the buffer.
3044         (if (not (gnus-goto-char
3045                   (text-property-any (point-min) (point-max) 
3046                                      'gnus-group (intern group))))
3047             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3048               (while (and newsrc
3049                           (not (gnus-goto-char 
3050                                 (text-property-any 
3051                                  (point-min) (point-max) 'gnus-group 
3052                                  (intern (car (car newsrc)))))))
3053                 (setq newsrc (cdr newsrc)))
3054               (or newsrc (progn (goto-char (point-max))
3055                                 (forward-line -1))))))
3056       ;; Adjust cursor point.
3057       (gnus-group-position-cursor))))
3058
3059 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3060   "List all newsgroups with unread articles of level LEVEL or lower.
3061 If ALL is non-nil, list groups that have no unread articles.
3062 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3063 If REGEXP, only list groups matching REGEXP."
3064   (set-buffer gnus-group-buffer)
3065   (let ((buffer-read-only nil)
3066         (newsrc (cdr gnus-newsrc-alist))
3067         (lowest (or lowest 1))
3068         info clevel unread group)
3069     (erase-buffer)
3070     (if (< lowest gnus-level-zombie)
3071         ;; List living groups.
3072         (while newsrc
3073           (setq info (car newsrc)
3074                 group (car info)
3075                 newsrc (cdr newsrc)
3076                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3077           (and unread ; This group might be bogus
3078                (or (not regexp)
3079                    (string-match regexp group))
3080                (<= (setq clevel (car (cdr info))) level) 
3081                (>= clevel lowest)
3082                (or all            ; We list all groups?
3083                    (eq unread t)  ; We list unactivated groups
3084                    (> unread 0)   ; We list groups with unread articles
3085                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3086                (gnus-group-insert-group-line 
3087                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3088
3089     ;; List dead groups.
3090     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3091          (gnus-group-prepare-flat-list-dead 
3092           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3093           gnus-level-zombie ?Z
3094           regexp))
3095     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3096          (gnus-group-prepare-flat-list-dead 
3097           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3098           gnus-level-killed ?K regexp))
3099
3100     (gnus-group-set-mode-line)
3101     (setq gnus-have-all-newsgroups all)
3102     (run-hooks 'gnus-group-prepare-hook)))
3103
3104 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3105   ;; List zombies and killed lists somehwat faster, which was
3106   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3107   ;; this by ignoring the group format specification altogether.
3108   (let (group beg)
3109     (while groups
3110       (setq group (car groups)
3111             groups (cdr groups))
3112       (if (or (not regexp)
3113               (string-match regexp group))
3114           (progn
3115             (setq beg (point))
3116             (insert (format " %c    *: %s\n" mark group))
3117             (add-text-properties 
3118              beg (1+ beg) 
3119              (list 'gnus-group (intern group)
3120                    'gnus-unread t
3121                    'gnus-level level)))))))
3122
3123 (defun gnus-group-real-name (group)
3124   "Find the real name of a foreign newsgroup."
3125   (if (string-match "^[^:]+:" group)
3126       (substring group (match-end 0))
3127     group))
3128
3129 (defun gnus-group-prefixed-name (group method)
3130   "Return the whole name from GROUP and METHOD."
3131   (and (stringp method) (setq method (gnus-server-to-method method)))
3132   (concat (format "%s" (car method))
3133           (if (and 
3134                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3135                (not (string= (nth 1 method) "")))
3136               (concat "+" (nth 1 method)))
3137           ":" group))
3138
3139 (defun gnus-group-real-prefix (group)
3140   "Return the prefix of the current group name."
3141   (if (string-match "^[^:]+:" group)
3142       (substring group 0 (match-end 0))
3143     ""))
3144
3145 (defun gnus-group-method-name (group)
3146   "Return the method used for selecting GROUP."
3147   (let ((prefix (gnus-group-real-prefix group)))
3148     (if (equal prefix "")
3149         gnus-select-method
3150       (if (string-match "^[^\\+]+\\+" prefix)
3151           (list (intern (substring prefix 0 (1- (match-end 0))))
3152                 (substring prefix (match-end 0) (1- (length prefix))))
3153         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3154
3155 (defun gnus-group-foreign-p (group)
3156   "Return nil if GROUP is native, non-nil if it is foreign."
3157   (string-match ":" group))
3158
3159 (defun gnus-group-set-info (info &optional method-only-group part)
3160   (let* ((entry (gnus-gethash
3161                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3162          (part-info info)
3163          (info (if method-only-group (nth 2 entry) info)))
3164     (if (not method-only-group)
3165         ()
3166       (or entry
3167           (error "Trying to change non-existant group %s" method-only-group))
3168       ;; We have recevied parts of the actual group info - either the
3169       ;; select method or the group parameters.  We first check
3170       ;; whether we have to extend the info, and if so, do that.
3171       (let ((len (length info))
3172             (total (if (eq part 'method) 5 6)))
3173         (and (< len total)
3174              (setcdr (nthcdr (1- len) info)
3175                      (make-list (- total len) nil)))
3176         ;; Then we enter the new info.
3177         (setcar (nthcdr (1- total) info) part-info)))
3178     ;; We uncompress some lists of marked articles.
3179     (let (marked)
3180       (if (not (setq marked (nth 3 info)))
3181           ()
3182         (while marked
3183           (or (eq 'score (car (car marked)))
3184               (eq 'bookmark (car (car marked)))
3185               (eq 'killed (car (car marked)))
3186               (setcdr (car marked) 
3187                       (gnus-uncompress-range (cdr (car marked)))))
3188           (setq marked (cdr marked)))))
3189     (if entry
3190         ()
3191       ;; This is a new group, so we just create it.
3192       (save-excursion
3193         (set-buffer gnus-group-buffer)
3194         (if (nth 4 info)
3195             ;; It's a foreign group...
3196             (gnus-group-make-group 
3197              (gnus-group-real-name (car info))
3198              (prin1-to-string (car (nth 4 info)))
3199              (nth 1 (nth 4 info)))
3200           ;; It's a native group.
3201           (gnus-group-make-group
3202            (car info)
3203            (prin1-to-string (car gnus-select-method))
3204            (nth 1 gnus-select-method)))
3205         (gnus-message 6 "Note: New group created")
3206         (setq entry 
3207               (gnus-gethash (gnus-group-prefixed-name 
3208                              (gnus-group-real-name (car info))
3209                              (or (nth 4 info) gnus-select-method))
3210                             gnus-newsrc-hashtb))))
3211     ;; Whether it was a new group or not, we now have the entry, so we
3212     ;; can do the update.
3213     (if entry
3214         (progn
3215           (setcar (nthcdr 2 entry) info)
3216           (if (and (not (eq (car entry) t)) 
3217                    (gnus-gethash (car info) gnus-active-hashtb))
3218               (let ((marked (nth 3 info)))
3219                 (setcar entry 
3220                         (max 0 (- (length (gnus-list-of-unread-articles 
3221                                            (car info)))
3222                                   (length (cdr (assq 'tick marked)))
3223                                   (length (cdr (assq 'dormant marked)))))))))
3224       (error "No such group: %s" (car info)))))
3225
3226 (defun gnus-group-set-method-info (group select-method)
3227   (gnus-group-set-info select-method group 'method))
3228
3229 (defun gnus-group-set-params-info (group params)
3230   (gnus-group-set-info params group 'params))
3231
3232 (defun gnus-group-update-group-line ()
3233   "This function updates the current line in the newsgroup buffer and
3234 moves the point to the colon."
3235   (let* ((buffer-read-only nil)
3236          (group (gnus-group-group-name))
3237          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3238     (if entry
3239         (gnus-dribble-enter 
3240          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3241                  ")")))
3242     (beginning-of-line)
3243     (delete-region (point) (progn (forward-line 1) (point)))
3244     (gnus-group-insert-group-line-info group)
3245     (forward-line -1)
3246     (gnus-group-position-cursor)))
3247
3248 (defun gnus-group-insert-group-line-info (group)
3249   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3250         active info)
3251     (if entry
3252         (progn
3253           (setq info (nth 2 entry))
3254           (gnus-group-insert-group-line 
3255            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3256       (setq active (gnus-gethash group gnus-active-hashtb))
3257       (gnus-group-insert-group-line 
3258        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3259                    gnus-level-killed)
3260        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3261
3262 (defun gnus-group-insert-group-line (gformat group level marked number method)
3263   (let* ((gformat (or gformat gnus-group-line-format-spec))
3264          (active (gnus-gethash group gnus-active-hashtb))
3265          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3266          (number-of-dormant (length (cdr (assq 'dormant marked))))
3267          (number-of-ticked (length (cdr (assq 'tick marked))))
3268          (number-of-ticked-and-dormant
3269           (+ number-of-ticked number-of-dormant))
3270          (number-of-unread-unticked 
3271           (if (numberp number) (int-to-string (max 0 number))
3272             "*"))
3273          (number-of-read
3274           (if (numberp number)
3275               (max 0 (- number-total number))
3276             "*"))
3277          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3278                            ((<= level gnus-level-unsubscribed) ?U)
3279                            ((= level gnus-level-zombie) ?Z)
3280                            (t ?K)))
3281          (qualified-group (gnus-group-real-name group))
3282          (newsgroup-description 
3283           (if gnus-description-hashtb
3284               (or (gnus-gethash group gnus-description-hashtb) "")
3285             ""))
3286          (moderated (if (member group gnus-moderated-list) ?m ? ))
3287          (moderated-string (if (eq moderated ?m) "(m)" ""))
3288          (method (gnus-server-get-method group method))
3289          (news-server (or (car (cdr method)) ""))
3290          (news-method (or (car method) ""))
3291          (news-method-string 
3292           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3293          (marked (if (and 
3294                       (numberp number) 
3295                       (zerop number)
3296                       (> number-of-ticked 0))
3297                      ?* ? ))
3298          (number (if (eq number t) "*" (+ number number-of-dormant 
3299                                           number-of-ticked)))
3300          (buffer-read-only nil)
3301          b)
3302     (beginning-of-line)
3303     (setq b (point))
3304     ;; Insert the text.
3305     (insert (eval gformat))
3306
3307     (add-text-properties 
3308      b (1+ b) (list 'gnus-group (intern group)
3309                     'gnus-unread (if (numberp number)
3310                                      (string-to-int number-of-unread-unticked)
3311                                    t)
3312                     'gnus-marked marked
3313                     'gnus-level level))))
3314
3315 (defun gnus-group-update-group (group &optional visible-only)
3316   "Update newsgroup info of GROUP.
3317 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3318   (save-excursion
3319     (set-buffer gnus-group-buffer)
3320     (let ((buffer-read-only nil)
3321           visible)
3322       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3323         (if entry
3324             (gnus-dribble-enter 
3325              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3326                      ")"))))
3327       ;; Buffer may be narrowed.
3328       (save-restriction
3329         (widen)
3330         ;; Search a line to modify.  If the buffer is large, the search
3331         ;; takes long time.  In most cases, current point is on the line
3332         ;; we are looking for.  So, first of all, check current line. 
3333         (if (or (progn
3334                   (beginning-of-line)
3335                   (eq (get-text-property (point) 'gnus-group)
3336                       (intern group)))
3337                 (progn
3338                   (gnus-goto-char 
3339                    (text-property-any 
3340                     (point-min) (point-max) 'gnus-group (intern group)))))
3341             ;; GROUP is listed in current buffer. So, delete old line.
3342             (progn
3343               (setq visible t)
3344               (beginning-of-line)
3345               (delete-region (point) (progn (forward-line 1) (point))))
3346           ;; No such line in the buffer, find out where it's supposed to
3347           ;; go, and insert it there (or at the end of the buffer).
3348           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3349           (or visible-only
3350               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
3351                 (while (and entry
3352                             (car entry)
3353                             (not
3354                              (gnus-goto-char
3355                               (text-property-any
3356                                (point-min) (point-max) 
3357                                'gnus-group (intern (car (car entry)))))))
3358                   (setq entry (cdr entry)))
3359                 (or entry (goto-char (point-max)))))))
3360       (if (or visible (not visible-only))
3361           (gnus-group-insert-group-line-info group))
3362       (gnus-group-set-mode-line))))
3363
3364 (defun gnus-group-set-mode-line ()
3365   (if (memq 'group gnus-updated-mode-lines)
3366       (let* ((gformat (or gnus-group-mode-line-format-spec
3367                           (setq gnus-group-mode-line-format-spec
3368                                 (gnus-parse-format 
3369                                  gnus-group-mode-line-format 
3370                                  gnus-group-mode-line-format-alist))))
3371              (news-server (car (cdr gnus-select-method)))
3372              (news-method (car gnus-select-method))
3373              (max-len 60)
3374              (mode-string (eval gformat)))
3375         (setq mode-string (eval gformat))
3376         (if (> (length mode-string) max-len) 
3377             (setq mode-string (substring mode-string 0 (- max-len 4))))
3378         (setq mode-line-buffer-identification mode-string)
3379         (set-buffer-modified-p t))))
3380
3381 (defun gnus-group-group-name ()
3382   "Get the name of the newsgroup on the current line."
3383   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3384     (and group (symbol-name group))))
3385
3386 (defun gnus-group-group-level ()
3387   "Get the level of the newsgroup on the current line."
3388   (get-text-property (gnus-point-at-bol) 'gnus-level))
3389
3390 (defun gnus-group-search-forward (&optional backward all level first-too)
3391   "Find the next newsgroup with unread articles.
3392 If BACKWARD is non-nil, find the previous newsgroup instead.
3393 If ALL is non-nil, just find any newsgroup.
3394 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3395 group exists.
3396 If FIRST-TOO, the current line is also eligeble as a target."
3397   (let ((way (if backward -1 1))
3398         (low 10)
3399         (beg (point))
3400         pos found)
3401     (if (and backward (progn (beginning-of-line)) (bobp))
3402         nil
3403       (or first-too (forward-line way))
3404       (while (and 
3405               (not (eobp))
3406               (not (setq 
3407                     found 
3408                     (and (or all
3409                              (and
3410                               (let ((unread 
3411                                      (get-text-property (point) 'gnus-unread)))
3412                                 (or (eq unread t) (and unread (> unread 0))))
3413                               (let ((lev (get-text-property
3414                                           (point) 'gnus-level)))
3415                                 (and lev (<= (get-text-property 
3416                                               (point) 'gnus-level)
3417                                              gnus-level-subscribed)))))
3418                          (or (not level)
3419                              (let ((lev (get-text-property (point) 'gnus-level)))
3420                                (if (and lev (<= lev level))
3421                                    t
3422                                  (if (< lev low)
3423                                      (progn
3424                                        (setq low lev)
3425                                        (setq pos (point))))
3426                                  nil))))))
3427               (zerop (forward-line way)))))
3428     (if found 
3429         (progn (gnus-group-position-cursor) t)
3430       (if pos (goto-char pos) (goto-char beg))
3431       nil)))
3432
3433 ;;; Gnus group mode commands
3434
3435 ;; Group marking.
3436
3437 (defun gnus-group-mark-group (n &optional unmark)
3438   "Mark the current group."
3439   (interactive "p")
3440   (let ((buffer-read-only nil)
3441         group)
3442     (while 
3443         (and (> n 0) 
3444              (setq group (gnus-group-group-name))
3445              (progn
3446                (beginning-of-line)
3447                (forward-char 1)
3448                (delete-char 1)
3449                (if unmark
3450                    (progn
3451                      (insert " ")
3452                      (setq gnus-group-marked (delete group gnus-group-marked)))
3453                  (insert "#")
3454                  (setq gnus-group-marked (cons group gnus-group-marked)))
3455                t)
3456              (zerop (gnus-group-next-group 1)))
3457       (setq n (1- n)))
3458     (gnus-summary-position-cursor)
3459     n))
3460
3461 (defun gnus-group-unmark-group (n)
3462   "Remove the mark from the current group."
3463   (interactive "p")
3464   (gnus-group-mark-group n 'unmark))
3465
3466 (defun gnus-group-mark-region (unmark beg end)
3467   "Mark all groups between point and mark.
3468 If UNMARK, remove the mark instead."
3469   (interactive "P\nr")
3470   (let ((num (count-lines beg end)))
3471     (save-excursion
3472       (goto-char beg)
3473       (- num (gnus-group-mark-group num unmark)))))
3474
3475 (defun gnus-group-remove-mark (group)
3476   (and (gnus-group-goto-group group)
3477        (save-excursion
3478          (gnus-group-mark-group 1 'unmark))))
3479
3480 ;; Return a list of groups to work on.  Take into consideration N (the
3481 ;; prefix) and the list of marked groups.
3482 (defun gnus-group-process-prefix (n)
3483   (cond (n
3484          (setq n (prefix-numeric-value n))
3485          ;; There is a prefix, so we return a list of the N next
3486          ;; groups. 
3487          (let ((way (if (< n 0) -1 1))
3488                (n (abs n))
3489                group groups)
3490            (save-excursion
3491              (while (and (> n 0)
3492                          (setq group (gnus-group-group-name)))
3493                (setq groups (cons group groups))
3494                (setq n (1- n))
3495                (forward-line way)))
3496            (nreverse groups)))
3497         (gnus-group-marked
3498          ;; No prefix, but a list of marked articles.
3499          (reverse gnus-group-marked))
3500         (t
3501          ;; Neither marked articles or a prefix, so we return the
3502          ;; current group.
3503          (let ((group (gnus-group-group-name)))
3504            (and group (list group))))))
3505
3506
3507
3508 ;; Selecting groups.
3509
3510 (defun gnus-group-read-group (all &optional no-article group)
3511   "Read news in this newsgroup.
3512 If argument ALL is non-nil, already read articles become readable.
3513 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3514   (interactive "P")
3515   (let ((group (or group (gnus-group-group-name)))
3516         number active marked entry)
3517     (or group (error "No group on current line"))
3518     (setq marked 
3519           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3520     ;; This group might be a dead group. In that case we have to get
3521     ;; the number of unread articles from `gnus-active-hashtb'.
3522     (if entry
3523         (setq number (car entry))
3524       (if (setq active (gnus-gethash group gnus-active-hashtb))
3525           (setq number (- (1+ (cdr active)) (car active)))))
3526     (gnus-summary-read-group 
3527      group (or all (and (numberp number) 
3528                         (zerop (+ number (length (cdr (assq 'tick marked)))
3529                                   (length (cdr (assq 'dormant marked)))))))
3530      no-article)))
3531
3532 (defun gnus-group-select-group (all)
3533   "Select this newsgroup.
3534 No article is selected automatically.
3535 If argument ALL is non-nil, already read articles become readable."
3536   (interactive "P")
3537   (gnus-group-read-group all t))
3538
3539 (defun gnus-group-jump-to-group (group)
3540   "Jump to newsgroup GROUP."
3541   (interactive (list (completing-read "Group: " gnus-active-hashtb nil
3542                                       (not (not gnus-read-active-file)))))
3543
3544   (if (equal group "")
3545       (error "empty group name"))
3546
3547   (let ((b (text-property-any (point-min) (point-max) 
3548                               'gnus-group (intern group))))
3549     (if b
3550         ;; Either go to the line in the group buffer...
3551         (goto-char b)
3552       ;; ... or insert the line.
3553       (or
3554        (gnus-gethash group gnus-active-hashtb)
3555        (gnus-activate-newsgroup group)
3556        (error "%s error: %s" group (gnus-status-message group)))
3557
3558       (gnus-group-update-group group)
3559       (goto-char (text-property-any (point-min) (point-max) 
3560                                     'gnus-group (intern group)))))
3561   ;; Adjust cursor point.
3562   (gnus-group-position-cursor))
3563
3564 (defun gnus-group-goto-group (group)
3565   "Goto to newsgroup GROUP."
3566   (let ((b (text-property-any (point-min) (point-max) 
3567                               'gnus-group (intern group))))
3568     (and b (goto-char b))))
3569
3570 (defun gnus-group-next-group (n)
3571   "Go to next N'th newsgroup.
3572 If N is negative, search backward instead.
3573 Returns the difference between N and the number of skips actually
3574 done."
3575   (interactive "p")
3576   (gnus-group-next-unread-group n t))
3577
3578 (defun gnus-group-next-unread-group (n &optional all level)
3579   "Go to next N'th unread newsgroup.
3580 If N is negative, search backward instead.
3581 If ALL is non-nil, choose any newsgroup, unread or not.
3582 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3583 such group can be found, the next group with a level higher than
3584 LEVEL.
3585 Returns the difference between N and the number of skips actually
3586 made."
3587   (interactive "p")
3588   (let ((backward (< n 0))
3589         (n (abs n)))
3590     (while (and (> n 0)
3591                 (gnus-group-search-forward 
3592                  backward (or (not gnus-group-goto-unread) all) level))
3593       (setq n (1- n)))
3594     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3595                                (if level " on this level or higher" "")))
3596     n))
3597
3598 (defun gnus-group-prev-group (n)
3599   "Go to previous N'th newsgroup.
3600 Returns the difference between N and the number of skips actually
3601 done."
3602   (interactive "p")
3603   (gnus-group-next-unread-group (- n) t))
3604
3605 (defun gnus-group-prev-unread-group (n)
3606   "Go to previous N'th unread newsgroup.
3607 Returns the difference between N and the number of skips actually
3608 done."  
3609   (interactive "p")
3610   (gnus-group-next-unread-group (- n)))
3611
3612 (defun gnus-group-next-unread-group-same-level (n)
3613   "Go to next N'th unread newsgroup on the same level.
3614 If N is negative, search backward instead.
3615 Returns the difference between N and the number of skips actually
3616 done."
3617   (interactive "p")
3618   (gnus-group-next-unread-group n t (gnus-group-group-level))
3619   (gnus-group-position-cursor))
3620
3621 (defun gnus-group-prev-unread-group-same-level (n)
3622   "Go to next N'th unread newsgroup on the same level.
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 (gnus-group-group-level))
3627   (gnus-group-position-cursor))
3628
3629 (defun gnus-group-best-unread-group (&optional exclude-group)
3630   "Go to the group with the highest level.
3631 If EXCLUDE-GROUP, do not go to that group."
3632   (interactive)
3633   (goto-char (point-min))
3634   (let ((best 100000)
3635         unread best-point)
3636     (while (setq unread (get-text-property (point) 'gnus-unread))
3637       (if (and (numberp unread) (> unread 0))
3638           (progn
3639             (or best-point (setq best-point (point)))
3640             (if (and (< (get-text-property (point) 'gnus-level) best)
3641                      (or (not exclude-group)
3642                          (not (equal exclude-group (gnus-group-group-name)))))
3643                 (progn 
3644                   (setq best (get-text-property (point) 'gnus-level))
3645                   (setq best-point (point))))))
3646       (forward-line 1))
3647     (if best-point (goto-char best-point))
3648     (gnus-summary-position-cursor)
3649     (and best-point (gnus-group-group-name))))
3650
3651 (defun gnus-group-first-unread-group ()
3652   "Go to the first group with unread articles."
3653   (interactive)
3654   (goto-char (point-min))
3655   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3656       (gnus-group-next-unread-group 1))
3657   (gnus-group-position-cursor))
3658
3659 (defun gnus-group-enter-server-mode ()
3660   "Jump to the server buffer."
3661   (interactive)
3662   (gnus-server-setup-buffer)
3663   (gnus-configure-windows 'server)
3664   (gnus-server-prepare))
3665
3666 (defun gnus-group-make-group (name method address)
3667   "Add a new newsgroup.
3668 The user will be prompted for a NAME, for a select METHOD, and an
3669 ADDRESS."
3670   (interactive
3671    (cons 
3672     (read-string "Group name: ")
3673     (let ((method
3674            (completing-read 
3675             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3676             nil t)))
3677       (if (assoc method gnus-valid-select-methods)
3678           (list method
3679                 (if (memq 'prompt-address
3680                           (assoc method gnus-valid-select-methods))
3681                     (read-string "Address: ")
3682                   ""))
3683         (list method nil)))))
3684   
3685   (let* ((meth (if address (list (intern method) address) method))
3686          (nname (gnus-group-prefixed-name name meth))
3687          info)
3688     (and (gnus-gethash nname gnus-newsrc-hashtb)
3689          (error "Group %s already exists" nname))
3690     (gnus-group-change-level 
3691      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3692      gnus-level-default-subscribed gnus-level-killed 
3693      (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3694                        gnus-newsrc-hashtb) t)
3695     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3696     (gnus-dribble-enter 
3697      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3698     (gnus-group-insert-group-line-info nname)
3699
3700     (and (gnus-check-backend-function 'request-create-group nname)
3701          (gnus-request-create-group nname))))
3702
3703 (defun gnus-group-edit-group (group &optional part)
3704   "Edit the group on the current line."
3705   (interactive (list (gnus-group-group-name)))
3706   (let ((done-func '(lambda () 
3707                       "Exit editing mode and update the information."
3708                       (interactive)
3709                       (gnus-group-edit-group-done 'part 'group)))
3710         (part (or part 'info))
3711         (winconf (current-window-configuration))
3712         info)
3713     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3714       (error "No group on current line"))
3715     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3716     (gnus-configure-windows 'edit-group)
3717     (gnus-add-current-to-buffer-list)
3718     (emacs-lisp-mode)
3719     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3720     (use-local-map (copy-keymap emacs-lisp-mode-map))
3721     (local-set-key "\C-c\C-c" done-func)
3722     (make-local-variable 'gnus-prev-winconf)
3723     (setq gnus-prev-winconf winconf)
3724     ;; We modify the func to let it know what part it is editing.
3725     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3726     (setcar (cdr (cdr (nth 4 done-func))) group)
3727     (erase-buffer)
3728     (insert
3729      (cond 
3730       ((eq part 'method)
3731        ";; Type `C-c C-c' after editing the select method.\n\n")
3732       ((eq part 'params)
3733        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3734       ((eq part 'info)
3735        ";; Type `C-c C-c' after editing the group info.\n\n")))
3736     (let ((cinfo (gnus-copy-sequence info))
3737           marked)
3738       (if (not (setq marked (nth 3 cinfo)))
3739           ()
3740         (while marked
3741           (or (eq 'score (car (car marked)))
3742               (eq 'bookmark (car (car marked)))
3743               (eq 'killed (car (car marked)))
3744               (setcdr (car marked) 
3745                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3746           (setq marked (cdr marked))))
3747       (insert 
3748        (pp-to-string
3749         (cond ((eq part 'method)
3750                (or (nth 4 info) "native"))
3751               ((eq part 'params)
3752                (nth 5 info))
3753               (t
3754                cinfo)))
3755        "\n"))))
3756
3757 (defun gnus-group-edit-group-method (group)
3758   "Edit the select method of GROUP."
3759   (interactive (list (gnus-group-group-name)))
3760   (gnus-group-edit-group group 'method))
3761
3762 (defun gnus-group-edit-group-parameters (group)
3763   "Edit the group parameters of GROUP."
3764   (interactive (list (gnus-group-group-name)))
3765   (gnus-group-edit-group group 'params))
3766
3767 (defun gnus-group-edit-group-done (part group)
3768   "Get info from buffer, update variables and jump to the group buffer."
3769   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3770   (goto-char (point-min))
3771   (let ((form (read (current-buffer)))
3772         (winconf gnus-prev-winconf))
3773     (if (eq part 'info) 
3774         (gnus-group-set-info form)
3775       (gnus-group-set-info form group part))
3776     (kill-buffer (current-buffer))
3777     (and winconf (set-window-configuration winconf))
3778     (set-buffer gnus-group-buffer)
3779     (gnus-group-update-group (gnus-group-group-name))
3780     (gnus-group-position-cursor)))
3781
3782 (defun gnus-group-make-help-group ()
3783   "Create the (ding) Gnus documentation group."
3784   (interactive)
3785   (and (gnus-gethash (gnus-group-prefixed-name "gnus-help" '(nndoc ""))
3786                      gnus-newsrc-hashtb)
3787        (error "Documentation group already exists"))
3788   (let ((path load-path))
3789     (while (and path
3790                 (not (file-exists-p (concat (file-name-as-directory (car path))
3791                                             "doc.txt"))))
3792       (setq path (cdr path)))
3793     (or path (error "Couldn't find doc group"))
3794     (gnus-group-make-group 
3795      "gnus-help" "nndoc" 
3796      (concat (file-name-as-directory (car path)) "doc.txt"))
3797     (gnus-group-position-cursor)))
3798
3799 (defun gnus-group-make-archive-group ()
3800   "Create the (ding) Gnus archive group."
3801   (interactive)
3802   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3803                      gnus-newsrc-hashtb)
3804        (error "Archive group already exists"))
3805   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
3806   (gnus-group-position-cursor))
3807
3808 (defun gnus-group-make-directory-group (dir)
3809   "Create an nndir group.
3810 The user will be prompted for a directory. The contents of this
3811 directory will be used as a newsgroup. The directory should contain
3812 mail messages or news articles in files that have numeric names."
3813   (interactive
3814    (list (read-file-name "Create group from directory: ")))
3815   (or (file-exists-p dir) (error "No such directory"))
3816   (or (file-directory-p dir) (error "Not a directory"))
3817   (gnus-group-make-group dir "nndir" dir)
3818   (gnus-group-position-cursor))
3819
3820 (defun gnus-group-make-kiboze-group (group address scores)
3821   "Create an nnkiboze group.
3822 The user will be prompted for a name, a regexp to match groups, and
3823 score file entries for articles to include in the group."
3824   (interactive
3825    (list
3826     (read-string "nnkiboze group name: ")
3827     (read-string "Source groups (regexp): ")
3828     (let ((headers (mapcar (lambda (group) (list group))
3829                            '("subject" "from" "number" "date" "message-id"
3830                              "references" "chars" "lines" "xref")))
3831           scores header regexp regexps)
3832       (while (not (equal "" (setq header (completing-read 
3833                                           "Match on header: " headers nil t))))
3834         (setq regexps nil)
3835         (while (not (equal "" (setq regexp (read-string 
3836                                             (format "Match on %s (string): "
3837                                                     header)))))
3838           (setq regexps (cons (list regexp nil nil 'r) regexps)))
3839         (setq scores (cons (cons header regexps) scores)))
3840       scores)))
3841   (gnus-group-make-group group "nnkiboze" address)
3842   (save-excursion
3843     (gnus-set-work-buffer)
3844     (let (emacs-lisp-mode-hook)
3845       (pp scores (current-buffer)))
3846     (write-region (point-min) (point-max) 
3847                   (concat (or gnus-kill-files-directory "~/News")
3848                           "nnkiboze:" group "." gnus-score-file-suffix)))
3849   (gnus-group-position-cursor))
3850
3851 (defun gnus-group-add-to-virtual (n vgroup)
3852   "Add the current group to a virtual group."
3853   (interactive
3854    (list current-prefix-arg
3855          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t)))
3856   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
3857       (error "%s is not an nnvirtual group" vgroup))
3858   (let* ((groups (gnus-group-process-prefix n))
3859          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
3860     (setcar (cdr method)
3861             (concat 
3862              (nth 1 method) "\\|"
3863              (mapconcat 
3864               (lambda (s) 
3865                 (gnus-group-remove-mark s)
3866                 (concat "\\(^" (regexp-quote s) "$\\)"))
3867               groups "\\|")))))
3868
3869 ;; Group sorting commands
3870 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
3871
3872 (defun gnus-group-sort-groups ()
3873   "Sort the group buffer using `gnus-group-sort-function'."
3874   (interactive)
3875   (setq gnus-newsrc-alist 
3876         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
3877   (gnus-make-hashtable-from-newsrc-alist)
3878   (gnus-get-unread-articles (1+ gnus-level-subscribed))
3879   (gnus-group-list-groups nil))
3880
3881 (defun gnus-group-sort-by-alphabet (info1 info2)
3882   (string< (car info1) (car info2)))
3883
3884 (defun gnus-group-sort-by-unread (info1 info2)
3885   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
3886         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
3887     (< (or (and (numberp n1) n1) 0)
3888        (or (and (numberp n2) n2) 0))))
3889
3890 (defun gnus-group-sort-by-level (info1 info2)
3891   (< (nth 1 info1) (nth 1 info2)))
3892
3893 ;; Group catching up.
3894
3895 (defun gnus-group-catchup-current (n &optional all)
3896   "Mark all articles not marked as unread in current newsgroup as read.
3897 If prefix argument N is numeric, the ARG next newsgroups will be
3898 caught up. If ALL is non-nil, marked articles will also be marked as
3899 read. Cross references (Xref: header) of articles are ignored.
3900 The difference between N and actual number of newsgroups that were
3901 caught up is returned."
3902   (interactive "P")
3903   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
3904                gnus-expert-user
3905                (gnus-y-or-n-p
3906                 (if all
3907                     "Do you really want to mark all articles as read? "
3908                   "Mark all unread articles as read? "))))
3909       n
3910     (let ((groups (gnus-group-process-prefix n))
3911           (ret 0))
3912       (while groups
3913         (gnus-group-remove-mark (car groups))
3914         (if (not (gnus-group-goto-group (car groups)))
3915             (setq ret (1+ ret))
3916           (gnus-group-catchup (car groups) all)
3917           (gnus-group-update-group-line))
3918         (setq groups (cdr groups)))
3919       (gnus-group-next-unread-group 1)
3920       ret)))
3921
3922 (defun gnus-group-catchup-current-all (n)
3923   "Mark all articles in current newsgroup as read.
3924 Cross references (Xref: header) of articles are ignored."
3925   (interactive "P")
3926   (gnus-group-catchup-current n 'all))
3927
3928 (defun gnus-group-catchup (group &optional all)
3929   "Mark all articles in GROUP as read.
3930 If ALL is non-nil, all articles are marked as read.
3931 The return value is the number of articles that were marked as read,
3932 or nil if no action could be taken."
3933   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3934          (num (car entry))
3935          (marked (nth 3 (nth 2 entry)))
3936          ticked)
3937     (if (not (numberp (car entry)))
3938         (gnus-message 1 "Can't catch up; non-active group")
3939       ;; Do the updating only if the newsgroup isn't killed
3940       (if entry
3941           (progn
3942             (setq ticked (if all nil (cdr (assq 'tick marked))))
3943             (gnus-update-read-articles group ticked nil ticked)
3944             (if (and all marked)
3945                 (setcar (nthcdr 3 (nth 2 entry)) 
3946                         (delq (assq 'dormant marked) marked))))))
3947     num))
3948
3949 (defun gnus-group-expire-articles (n)
3950   "Expire all expirable articles in the current newsgroup."
3951   (interactive "P")
3952   (let ((groups (gnus-group-process-prefix n))
3953         group)
3954     (or groups (error "No groups to expire"))
3955     (while groups
3956       (setq group (car groups)
3957             groups (cdr groups))
3958       (gnus-group-remove-mark group)
3959       (if (not (gnus-check-backend-function 'request-expire-articles group))
3960           ()
3961         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3962                (expirable (if (memq 'total-expire (nth 5 info))
3963                               (cons nil (gnus-list-of-read-articles group))
3964                             (assq 'expire (nth 3 info)))))
3965           (and expirable 
3966                (setcdr expirable
3967                        (gnus-request-expire-articles 
3968                         (cdr expirable) group))))))))
3969
3970 (defun gnus-group-expire-all-groups ()
3971   "Expire all expirable articles in all newsgroups."
3972   (interactive)
3973   (gnus-message 5 "Expiring...")
3974   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
3975                                    (cdr gnus-newsrc-alist))))
3976     (gnus-group-expire-articles nil))
3977   (gnus-message 5 "Expiring...done"))
3978
3979 (defun gnus-group-set-current-level (n level)
3980   "Set the level of the next N groups to LEVEL."
3981   (interactive "P\nnLevel: ")
3982   (or (and (>= level 1) (<= level gnus-level-killed))
3983       (error "Illegal level: %d" level))
3984   (let ((groups (gnus-group-process-prefix n))
3985         group)
3986     (while groups
3987       (setq group (car groups)
3988             groups (cdr groups))
3989       (gnus-group-remove-mark group)
3990       (gnus-message 6 "Changed level of %s from %d to %d" 
3991                     group (gnus-group-group-level) level)
3992       (gnus-group-change-level group level
3993                                (gnus-group-group-level))
3994       (gnus-group-update-group-line)))
3995   (gnus-group-position-cursor))
3996
3997 (defun gnus-group-unsubscribe-current-group (arg)
3998   "Toggle subscribe from/to unsubscribe current group."
3999   (interactive "P")
4000   (let ((group (gnus-group-group-name)))
4001     (or group (error "No newsgroup on current line"))
4002     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
4003                           gnus-level-default-unsubscribed
4004                         gnus-level-default-subscribed)))
4005     (gnus-group-unsubscribe-group group arg)
4006     (gnus-group-next-group 1)))
4007
4008 (defun gnus-group-unsubscribe-group (group &optional level)
4009   "Toggle subscribe from/to unsubscribe GROUP.
4010 New newsgroup is added to .newsrc automatically."
4011   (interactive
4012    (list (completing-read "Group: " gnus-active-hashtb nil 
4013                           gnus-have-read-active-file)))
4014   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4015     (cond (newsrc
4016            ;; Toggle subscription flag.
4017            (gnus-group-change-level 
4018             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4019                                            gnus-level-subscribed) 
4020                                        (1+ gnus-level-subscribed)
4021                                      gnus-level-default-subscribed)))
4022            (gnus-group-update-group group))
4023           ((and (stringp group)
4024                 (or (not gnus-have-read-active-file)
4025                     (gnus-gethash group gnus-active-hashtb)))
4026            ;; Add new newsgroup.
4027            (gnus-group-change-level 
4028             group 
4029             (if level level gnus-level-default-subscribed) 
4030             (or (and (member group gnus-zombie-list) 
4031                      gnus-level-zombie) 
4032                 gnus-level-killed)
4033             (and (gnus-group-group-name)
4034                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4035            (gnus-group-update-group group))
4036           (t (error "No such newsgroup: %s" group)))
4037     (gnus-group-position-cursor)))
4038
4039 (defun gnus-group-transpose-groups (n)
4040   "Move the current newsgroup up N places.
4041 If given a negative prefix, move down instead. The difference between
4042 N and the number of steps taken is retured." 
4043   (interactive "p")
4044   (or (gnus-group-group-name)
4045       (error "No group on current line"))
4046   (gnus-group-kill-group 1)
4047   (prog1
4048       (forward-line (- n))
4049     (gnus-group-yank-group)
4050     (gnus-group-position-cursor)))
4051
4052 (defun gnus-group-kill-all-zombies ()
4053   "Kill all zombie newsgroups."
4054   (interactive)
4055   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4056   (setq gnus-zombie-list nil)
4057   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4058   (goto-char (point-min))
4059   (gnus-group-position-cursor))
4060
4061 (defun gnus-group-kill-region (begin end)
4062   "Kill newsgroups in current region (excluding current point).
4063 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4064   (interactive "r")
4065   (let ((lines
4066          ;; Exclude a line where current point is on.
4067          (1-
4068           ;; Count lines.
4069           (save-excursion
4070             (count-lines
4071              (progn
4072                (goto-char begin)
4073                (beginning-of-line)
4074                (point))
4075              (progn
4076                (goto-char end)
4077                (end-of-line)
4078                (point)))))))
4079     (goto-char begin)
4080     (beginning-of-line)                 ;Important when LINES < 1
4081     (gnus-group-kill-group lines)))
4082
4083 (defun gnus-group-kill-group (n)
4084   "The the next N groups.
4085 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4086 However, only groups that were alive can be yanked; already killed 
4087 groups or zombie groups can't be yanked.
4088 The return value is the name of the (last) group that was killed."
4089   (interactive "P")
4090   (let ((buffer-read-only nil)
4091         (groups (gnus-group-process-prefix n))
4092         group entry level)
4093     (while groups
4094       (setq group (car groups)
4095             groups (cdr groups))
4096       (gnus-group-remove-mark group)
4097       (setq level (gnus-group-group-level))
4098       (gnus-delete-line)
4099       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4100           (setq gnus-list-of-killed-groups 
4101                 (cons (cons (car entry) (nth 2 entry)) 
4102                       gnus-list-of-killed-groups)))
4103       (gnus-group-change-level 
4104        (if entry entry group) gnus-level-killed (if entry nil level)))
4105     (gnus-group-position-cursor)
4106     group))
4107
4108 (defun gnus-group-yank-group (&optional arg)
4109   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4110 inserting it before the current newsgroup.  The numeric ARG specifies
4111 how many newsgroups are to be yanked.  The name of the (last)
4112 newsgroup yanked is returned."
4113   (interactive "p")
4114   (if (not arg) (setq arg 1))
4115   (let (info group prev)
4116     (while (>= (setq arg (1- arg)) 0)
4117       (if (not (setq info (car gnus-list-of-killed-groups)))
4118           (error "No more newsgroups to yank"))
4119       (setq group (nth 2 info))
4120       ;; Find which newsgroup to insert this one before - search
4121       ;; backward until something suitable is found. If there are no
4122       ;; other newsgroups in this buffer, just make this newsgroup the
4123       ;; first newsgroup.
4124       (setq prev (gnus-group-group-name))
4125       (gnus-group-change-level 
4126        info (nth 2 info) gnus-level-killed 
4127        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4128        t)
4129       (gnus-group-insert-group-line-info (nth 1 info))
4130       (setq gnus-list-of-killed-groups 
4131             (cdr gnus-list-of-killed-groups)))
4132     (forward-line -1)
4133     (gnus-group-position-cursor)
4134     group))
4135       
4136 (defun gnus-group-list-all-groups (arg)
4137   "List all newsgroups with level ARG or lower.
4138 Default is gnus-level-unsubscribed, which lists all subscribed and most
4139 unsubscribed groups."
4140   (interactive "P")
4141   (setq arg (or arg gnus-level-unsubscribed))
4142   (gnus-group-list-groups arg t))
4143
4144 (defun gnus-group-list-killed ()
4145   "List all killed newsgroups in the group buffer."
4146   (interactive)
4147   (if (not gnus-killed-list)
4148       (gnus-message 6 "No killed groups")
4149     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4150     (goto-char (point-min)))
4151   (gnus-group-position-cursor))
4152
4153 (defun gnus-group-list-zombies ()
4154   "List all zombie newsgroups in the group buffer."
4155   (interactive)
4156   (if (not gnus-zombie-list)
4157       (gnus-message 6 "No zombie groups")
4158     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4159     (goto-char (point-min)))
4160   (gnus-group-position-cursor))
4161
4162 (defun gnus-group-get-new-news (&optional arg)
4163   "Get newly arrived articles.
4164 If ARG is non-nil, it should be a number between one and nine to
4165 specify which levels you are interested in re-scanning."
4166   (interactive "P")
4167   (run-hooks 'gnus-get-new-news-hook)
4168   (let ((level arg))
4169     (if (and gnus-read-active-file (not level))
4170         (progn
4171           (gnus-read-active-file)
4172           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4173       (let ((gnus-read-active-file nil))
4174         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4175     (gnus-group-list-groups 
4176      (or gnus-group-always-list-unread level gnus-level-subscribed)
4177      gnus-have-all-newsgroups)))
4178
4179 (defun gnus-group-get-new-news-this-group (n)
4180   "Check for newly arrived news in the current group (and the N-1 next groups).
4181 The difference between N and the number of newsgroup checked is returned.
4182 If N is negative, this group and the N-1 previous groups will be checked."
4183   (interactive "P")
4184   (let* ((groups (gnus-group-process-prefix n))
4185          (ret (if (numberp n) (- n (length groups)) 0))
4186          (w-p (window-start))
4187          group)
4188     (while groups
4189       (setq group (car groups)
4190             groups (cdr groups))
4191       (gnus-group-remove-mark group)
4192       (or (gnus-get-new-news-in-group group)
4193           (progn 
4194             (ding) 
4195             (message "%s error: %s" group (gnus-status-message group))
4196             (sit-for 2))))
4197     ;; !!! I don't know why the buffer scrolls forward when updating
4198     ;; the first line in the group buffer, but it does. So we set the
4199     ;; window start forcibly.
4200 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4201     (forward-line 1)
4202     (gnus-summary-position-cursor)
4203     ret))
4204
4205 (defun gnus-get-new-news-in-group (group)
4206   (and group 
4207        (gnus-activate-newsgroup group)
4208        (progn
4209          (gnus-get-unread-articles-in-group 
4210           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4211           (gnus-gethash group gnus-active-hashtb))
4212          (gnus-group-update-group-line)
4213          t)))
4214
4215 (defun gnus-group-fetch-faq (group)
4216   "Fetch the FAQ for the current group."
4217   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4218   (or group (error "No group name given"))
4219   (let ((file (concat gnus-group-faq-directory group))) 
4220     (if (not (file-exists-p file))
4221         (error "No such file: %s" file)
4222       (find-file file))))
4223   
4224 (defun gnus-group-describe-group (force &optional group)
4225   "Display a description of the current newsgroup."
4226   (interactive "P")
4227   (and force (setq gnus-description-hashtb nil))
4228   (let ((group (or group (gnus-group-group-name)))
4229         desc)
4230     (or group (error "No group name given"))
4231     (and (or gnus-description-hashtb
4232              (setq desc (gnus-group-get-description group))
4233              (gnus-read-descriptions-file))
4234          (message
4235           (or desc (gnus-gethash group gnus-description-hashtb)
4236               "No description available")))))
4237
4238 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4239 (defun gnus-group-describe-all-groups (force)
4240   "Pop up a buffer with descriptons of all newsgroups."
4241   (interactive "P")
4242   (and force (setq gnus-description-hashtb nil))
4243   (if (not (or gnus-description-hashtb
4244                (gnus-read-descriptions-file)))
4245       (error "Couldn't request descriptions file"))
4246   (let ((buffer-read-only nil)
4247         b)
4248     (erase-buffer)
4249     (mapatoms
4250      (lambda (group)
4251        (setq b (point))
4252        (insert (format "      *: %-20s %s\n" (symbol-name group)
4253                        (symbol-value group)))
4254        (add-text-properties 
4255         b (1+ b) (list 'gnus-group group
4256                        'gnus-unread t 'gnus-marked nil
4257                        'gnus-level (1+ gnus-level-subscribed))))
4258      gnus-description-hashtb)
4259     (goto-char (point-min))
4260     (gnus-group-position-cursor)))
4261
4262 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4263 (defun gnus-group-apropos (regexp &optional search-description)
4264   "List all newsgroups that have names that match a regexp."
4265   (interactive "sGnus apropos (regexp): ")
4266   (let ((prev "")
4267         (obuf (current-buffer))
4268         groups des prev)
4269     ;; Go through all newsgroups that are known to Gnus.
4270     (mapatoms 
4271      (lambda (group)
4272        (and (string-match regexp (symbol-name group))
4273             (setq groups (cons (symbol-name group) groups))))
4274      gnus-active-hashtb)
4275     ;; Go through all descriptions that are known to Gnus. 
4276     (if search-description
4277         (mapatoms 
4278          (lambda (group)
4279            (and (string-match regexp (symbol-value group))
4280                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4281                 (setq groups (cons (symbol-name group) groups))))
4282          gnus-description-hashtb))
4283     (if (not groups)
4284         (gnus-message 3 "No groups matched \"%s\"." regexp)
4285       ;; Print out all the groups.
4286       (save-excursion
4287         (pop-to-buffer "*Gnus Help*")
4288         (buffer-disable-undo (current-buffer))
4289         (erase-buffer)
4290         (setq groups (sort groups 'string<))
4291         (while groups
4292           ;; Groups may be entered twice into the list of groups.
4293           (if (not (string= (car groups) prev))
4294               (progn
4295                 (insert (setq prev (car groups)) "\n")
4296                 (if (and gnus-description-hashtb
4297                          (setq des (gnus-gethash (car groups) 
4298                                                  gnus-description-hashtb)))
4299                     (insert "  " des "\n"))))
4300           (setq groups (cdr groups)))
4301         (goto-char (point-min))))
4302     (pop-to-buffer obuf)))
4303
4304 (defun gnus-group-description-apropos (regexp)
4305   "List all newsgroups that have names or desccriptions that match a regexp."
4306   (interactive "sGnus description apropos (regexp): ")
4307   (if (not (or gnus-description-hashtb
4308                (gnus-read-descriptions-file)))
4309       (error "Couldn't request descriptions file"))
4310   (gnus-group-apropos regexp t))
4311
4312 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4313 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4314   "List all groups with unread articles that match REGEXP.
4315 If the prefix LEVEL is non-nil, it should be a number that says which
4316 level to cut off listing groups. 
4317 If ALL, also list groups with no unread articles.
4318 If LOWEST, don't list groups with level lower than LOWEST."
4319   (interactive "P\nsList newsgroups matching: ")
4320   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4321                            all (or lowest 1) regexp)
4322   (goto-char (point-min))
4323   (gnus-group-position-cursor))
4324
4325 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4326   "List all groups that match REGEXP.
4327 If the prefix LEVEL is non-nil, it should be a number that says which
4328 level to cut off listing groups. 
4329 If LOWEST, don't list groups with level lower than LOWEST."
4330   (interactive "P\nsList newsgroups matching: ")
4331   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4332
4333 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4334 (defun gnus-group-save-newsrc ()
4335   "Save the Gnus startup files."
4336   (interactive)
4337   (gnus-save-newsrc-file))
4338
4339 (defun gnus-group-restart (&optional arg)
4340   "Force Gnus to read the .newsrc file."
4341   (interactive "P")
4342   (gnus-save-newsrc-file)
4343   (gnus-setup-news 'force)
4344   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4345
4346 (defun gnus-group-read-init-file ()
4347   "Read the Gnus elisp init file."
4348   (interactive)
4349   (gnus-read-init-file))
4350
4351 (defun gnus-group-check-bogus-groups ()
4352   "Check bogus newsgroups."
4353   (interactive)
4354   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
4355   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4356
4357 (defun gnus-group-mail ()
4358   "Start composing a mail."
4359   (interactive)
4360   (mail))
4361
4362 (defun gnus-group-edit-global-kill (article &optional group)
4363   "Edit the global kill file.
4364 If GROUP, edit that local kill file instead."
4365   (interactive "P")
4366   (setq gnus-current-kill-article article)
4367   (gnus-kill-file-edit-file group)
4368   (gnus-message 6
4369    (substitute-command-keys
4370     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4371
4372 (defun gnus-group-edit-local-kill (article group)
4373   "Edit a local kill file."
4374   (interactive (list nil (gnus-group-group-name)))
4375   (gnus-group-edit-global-kill article group))
4376
4377 (defun gnus-group-force-update ()
4378   "Update `.newsrc' file."
4379   (interactive)
4380   (gnus-save-newsrc-file))
4381
4382 (defun gnus-group-suspend ()
4383   "Suspend the current Gnus session.
4384 In fact, cleanup buffers except for group mode buffer.
4385 The hook gnus-suspend-gnus-hook is called before actually suspending."
4386   (interactive)
4387   (run-hooks 'gnus-suspend-gnus-hook)
4388   ;; Kill Gnus buffers except for group mode buffer.
4389   (let ((group-buf (get-buffer gnus-group-buffer)))
4390     ;; Do this on a separate list in case the user does a ^G before we finish
4391     (let ((gnus-buffer-list
4392            (delq group-buf (delq gnus-dribble-buffer
4393                                  (append gnus-buffer-list nil)))))
4394       (while gnus-buffer-list
4395         (gnus-kill-buffer (car gnus-buffer-list))
4396         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4397     (if group-buf
4398         (progn
4399           (setq gnus-buffer-list (list group-buf))
4400           (bury-buffer group-buf)
4401           (delete-windows-on group-buf t)))))
4402
4403 (defun gnus-group-clear-dribble ()
4404   "Clear all information from the dribble buffer."
4405   (interactive)
4406   (gnus-dribble-clear))
4407
4408 (defun gnus-group-exit ()
4409   "Quit reading news after updating .newsrc.eld and .newsrc.
4410 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4411   (interactive)
4412   (if (or noninteractive                ;For gnus-batch-kill
4413           (zerop (buffer-size))         ;No news is good news.
4414           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4415           (not gnus-interactive-exit)   ;Without confirmation
4416           gnus-expert-user
4417           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4418       (progn
4419         (run-hooks 'gnus-exit-gnus-hook)
4420         (gnus-save-newsrc-file)
4421         (gnus-close-backends)
4422         (gnus-clear-system))))
4423
4424 (defun gnus-close-backends ()
4425   ;; Send a close request to all backends that support such a request. 
4426   (let ((methods gnus-valid-select-methods)
4427         func)
4428     (while methods
4429       (if (fboundp (setq func (intern (concat (car (car methods))
4430                                               "-request-close"))))
4431           (funcall func))
4432       (setq methods (cdr methods)))))
4433
4434 (defun gnus-group-quit ()
4435   "Quit reading news without updating .newsrc.eld or .newsrc.
4436 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4437   (interactive)
4438   (if (or noninteractive                ;For gnus-batch-kill
4439           (zerop (buffer-size))
4440           (not (gnus-server-opened gnus-select-method))
4441           gnus-expert-user
4442           (not gnus-current-startup-file)
4443           (gnus-yes-or-no-p
4444            (format "Quit reading news without saving %s? "
4445                    (file-name-nondirectory gnus-current-startup-file))))
4446       (progn
4447         (run-hooks 'gnus-exit-gnus-hook)
4448         (gnus-dribble-save)
4449         (gnus-close-backends)
4450         (gnus-clear-system))))
4451
4452 (defun gnus-group-describe-briefly ()
4453   "Give a one line description of the group mode commands."
4454   (interactive)
4455   (gnus-message 6
4456    (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")))
4457
4458 (defun gnus-group-browse-foreign-server (method)
4459   "Browse a foreign news server.
4460 If called interactively, this function will ask for a select method
4461  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4462 If not, METHOD should be a list where the first element is the method
4463 and the second element is the address."
4464   (interactive
4465    (list (let ((how (completing-read 
4466                      "Which backend: "
4467                      (append gnus-valid-select-methods gnus-server-alist)
4468                      nil t "nntp")))
4469            ;; We either got a backend name or a virtual server name.
4470            ;; If the first, we also need an address.
4471            (if (assoc how gnus-valid-select-methods)
4472                (list (intern how)
4473                      ;; Suggested by mapjph@bath.ac.uk.
4474                      (completing-read 
4475                       "Address: " 
4476                       (mapcar (lambda (server) (list server))
4477                               gnus-secondary-servers)))
4478              ;; We got a server name, so we find the method.
4479              (gnus-server-to-method how)))))
4480   (gnus-browse-foreign-server method))
4481
4482 \f
4483 ;;;
4484 ;;; Browse Server Mode
4485 ;;;
4486
4487 (defvar gnus-browse-mode-hook nil)
4488 (defvar gnus-browse-mode-map nil)
4489 (put 'gnus-browse-mode 'mode-class 'special)
4490
4491 (if gnus-browse-mode-map
4492     nil
4493   (setq gnus-browse-mode-map (make-keymap))
4494   (suppress-keymap gnus-browse-mode-map)
4495   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4496   (define-key gnus-browse-mode-map "=" 'gnus-browse-read-group)
4497   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4498   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4499   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4500   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4501   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4502   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4503   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4504   (define-key gnus-browse-mode-map "\r" 'gnus-browse-read-group)
4505   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4506   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4507   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4508   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4509   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4510   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4511   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4512   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4513   )
4514
4515 (defvar gnus-browse-current-method nil)
4516 (defvar gnus-browse-return-buffer nil)
4517
4518 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4519
4520 (defun gnus-browse-foreign-server (method &optional return-buffer)
4521   (setq gnus-browse-current-method method)
4522   (setq gnus-browse-return-buffer return-buffer)
4523   (let ((gnus-select-method method)
4524         groups group)
4525     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4526     (or (gnus-server-opened method)
4527         (gnus-open-server method)
4528         (error "Unable to contact server: %s" (gnus-status-message method)))
4529     (or (gnus-request-list method)
4530         (error "Couldn't request list: %s" (gnus-status-message method)))
4531     (get-buffer-create gnus-browse-buffer)
4532     (gnus-add-current-to-buffer-list)
4533     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4534     (gnus-configure-windows 'browse)
4535     (buffer-disable-undo (current-buffer))
4536     (let ((buffer-read-only nil))
4537       (erase-buffer))
4538     (gnus-browse-mode)
4539     (setq mode-line-buffer-identification
4540           (format
4541            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4542     (save-excursion
4543       (set-buffer nntp-server-buffer)
4544       (let ((cur (current-buffer)))
4545         (goto-char (point-min))
4546         (or (string= gnus-ignored-newsgroups "")
4547             (delete-matching-lines gnus-ignored-newsgroups))
4548         (while (re-search-forward 
4549                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4550           (goto-char (match-end 1))
4551           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4552                                                      (match-end 1))
4553                                    (max 0 (- (1+ (read cur)) (read cur))))
4554                              groups)))))
4555     (setq groups (sort groups 
4556                        (lambda (l1 l2)
4557                          (string< (car l1) (car l2)))))
4558     (let ((buffer-read-only nil))
4559       (while groups
4560         (setq group (car groups))
4561         (insert 
4562          (format "K%7d: %s\n" (cdr group) (car group)))
4563         (setq groups (cdr groups))))
4564     (switch-to-buffer (current-buffer))
4565     (goto-char (point-min))
4566     (gnus-group-position-cursor)))
4567
4568 (defun gnus-browse-mode ()
4569   "Major mode for browsing a foreign server."
4570   (interactive)
4571   (kill-all-local-variables)
4572   (if gnus-visual (gnus-browse-make-menu-bar))
4573   (setq mode-line-modified "-- ")
4574   (make-local-variable 'mode-line-format)
4575   (setq mode-line-format (copy-sequence mode-line-format))
4576   (and (equal (nth 3 mode-line-format) "   ")
4577        (setcar (nthcdr 3 mode-line-format) ""))
4578   (setq major-mode 'gnus-browse-mode)
4579   (setq mode-name "Browse Server")
4580   (setq mode-line-process nil)
4581   (use-local-map gnus-browse-mode-map)
4582   (buffer-disable-undo (current-buffer))
4583   (setq truncate-lines t)
4584   (setq buffer-read-only t)
4585   (run-hooks 'gnus-browse-mode-hook))
4586
4587 (defun gnus-browse-read-group ()
4588   "Not implemented, and will probably never be."
4589   (interactive)
4590   (error "You can't read while browsing"))
4591
4592 (defun gnus-browse-next-group (n)
4593   "Go to the next group."
4594   (interactive "p")
4595   (prog1
4596       (forward-line n)
4597     (gnus-group-position-cursor)))
4598
4599 (defun gnus-browse-prev-group (n)
4600   "Go to the next group."
4601   (interactive "p")
4602   (gnus-browse-next-group (- n)))
4603
4604 (defun gnus-browse-unsubscribe-current-group (arg)
4605   "(Un)subscribe to the next ARG groups."
4606   (interactive "p")
4607   (and (eobp)
4608        (error "No group at current line."))
4609   (let ((ward (if (< arg 0) -1 1))
4610         (arg (abs arg)))
4611     (while (and (> arg 0)
4612                 (not (eobp))
4613                 (gnus-browse-unsubscribe-group)
4614                 (zerop (gnus-browse-next-group ward)))
4615       (setq arg (1- arg)))
4616     (gnus-group-position-cursor)
4617     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4618     arg))
4619   
4620 (defun gnus-browse-unsubscribe-group ()
4621   (let ((sub nil)
4622         (buffer-read-only nil)
4623         group)
4624     (save-excursion
4625       (beginning-of-line)
4626       (if (= (following-char) ?K) (setq sub t))
4627       (re-search-forward ": \\(.*\\)$" nil t)
4628       (setq group (gnus-group-prefixed-name 
4629                    (buffer-substring (match-beginning 1) (match-end 1))
4630                    gnus-browse-current-method))
4631       (beginning-of-line)
4632       (delete-char 1)
4633       (if sub
4634           (progn
4635             (gnus-group-change-level 
4636              (list t group gnus-level-default-subscribed
4637                    nil nil gnus-browse-current-method) 
4638              gnus-level-default-subscribed gnus-level-killed
4639              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4640              t)
4641             (insert ? ))
4642         (gnus-group-change-level 
4643          group gnus-level-killed gnus-level-default-subscribed)
4644         (insert ?K)))
4645     t))
4646
4647 (defun gnus-browse-exit ()
4648   "Quit browsing and return to the group buffer."
4649   (interactive)
4650   (if (eq major-mode 'gnus-browse-mode)
4651       (kill-buffer (current-buffer)))
4652   (if gnus-browse-return-buffer
4653       (gnus-configure-windows 'server)
4654     (gnus-configure-windows 'group)))
4655
4656 (defun gnus-browse-describe-briefly ()
4657   "Give a one line description of the group mode commands."
4658   (interactive)
4659   (gnus-message 6
4660    (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")))
4661       
4662 \f
4663 ;;;
4664 ;;; Gnus summary mode
4665 ;;;
4666
4667 (defvar gnus-summary-mode-map nil)
4668 (defvar gnus-summary-mark-map nil)
4669 (defvar gnus-summary-mscore-map nil)
4670 (defvar gnus-summary-article-map nil)
4671 (defvar gnus-summary-thread-map nil)
4672 (defvar gnus-summary-goto-map nil)
4673 (defvar gnus-summary-exit-map nil)
4674 (defvar gnus-summary-various-map nil)
4675 (defvar gnus-summary-interest-map nil)
4676 (defvar gnus-summary-sort-map nil)
4677 (defvar gnus-summary-backend-map nil)
4678 (defvar gnus-summary-save-map nil)
4679 (defvar gnus-summary-wash-map nil)
4680 (defvar gnus-summary-help-map nil)
4681
4682 (put 'gnus-summary-mode 'mode-class 'special)
4683
4684 (if gnus-summary-mode-map
4685     nil
4686   (setq gnus-summary-mode-map (make-keymap))
4687   (suppress-keymap gnus-summary-mode-map)
4688
4689   ;; Non-orthogonal keys
4690
4691   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4692   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4693   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4694   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4695   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4696   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4697   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4698   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4699   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4700   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
4701   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
4702   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
4703   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
4704   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
4705   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
4706   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
4707   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
4708   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
4709   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
4710   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
4711   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
4712   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
4713   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
4714   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
4715   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
4716   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
4717   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
4718   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
4719   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
4720   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
4721   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
4722   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
4723   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
4724   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
4725   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
4726   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
4727   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
4728   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
4729   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
4730   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
4731   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
4732   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
4733   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
4734   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
4735   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
4736   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
4737   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
4738   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
4739   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
4740   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
4741   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
4742   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
4743   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
4744   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
4745   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
4746   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
4747   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
4748   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
4749   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
4750   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
4751   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
4752   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
4753   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
4754   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
4755   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
4756   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
4757   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
4758   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
4759   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
4760   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
4761   (define-key gnus-summary-mode-map "V" 'gnus-version)
4762   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
4763   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
4764   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
4765   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
4766   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
4767   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
4768   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
4769   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
4770 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
4771   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
4772   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
4773   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
4774 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
4775   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
4776   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
4777   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
4778
4779
4780   ;; Sort of orthogonal keymap
4781   (define-prefix-command 'gnus-summary-mark-map)
4782   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
4783   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
4784   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
4785   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
4786   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
4787   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
4788   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
4789   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
4790   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
4791   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
4792   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
4793   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
4794   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
4795   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
4796   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
4797   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
4798   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
4799   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
4800   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
4801   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
4802   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
4803   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
4804   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
4805   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
4806
4807   (define-prefix-command 'gnus-summary-mscore-map)
4808   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
4809   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
4810   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
4811   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
4812   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
4813
4814   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
4815   
4816   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
4817   
4818   (define-prefix-command 'gnus-summary-goto-map)
4819   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
4820   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
4821   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
4822   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
4823   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
4824   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
4825   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
4826   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
4827   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
4828   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
4829   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
4830   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
4831   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
4832   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
4833
4834
4835   (define-prefix-command 'gnus-summary-thread-map)
4836   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
4837   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
4838   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
4839   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
4840   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
4841   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
4842   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
4843   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
4844   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
4845   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
4846   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
4847   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
4848   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
4849   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
4850
4851   
4852   (define-prefix-command 'gnus-summary-exit-map)
4853   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
4854   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
4855   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
4856   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
4857   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
4858   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
4859   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
4860   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
4861   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
4862   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
4863   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
4864
4865
4866   (define-prefix-command 'gnus-summary-article-map)
4867   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
4868   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
4869   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
4870   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
4871   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
4872   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
4873   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
4874   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
4875   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
4876   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
4877   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
4878   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
4879   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
4880   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
4881   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
4882   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
4883   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
4884   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
4885
4886
4887   (define-prefix-command 'gnus-summary-wash-map)
4888   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
4889   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
4890   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
4891   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
4892   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
4893   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
4894   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
4895   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
4896   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
4897   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
4898   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
4899   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
4900
4901   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
4902   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
4903   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
4904   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
4905   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
4906   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
4907
4908
4909   (define-prefix-command 'gnus-summary-help-map)
4910   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
4911   (define-key gnus-summary-help-map "v" 'gnus-version)
4912   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
4913   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
4914   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
4915   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
4916
4917
4918   (define-prefix-command 'gnus-summary-backend-map)
4919   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
4920   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
4921   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
4922   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
4923   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
4924   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
4925   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
4926   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
4927
4928
4929   (define-prefix-command 'gnus-summary-save-map)
4930   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
4931   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
4932   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
4933   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
4934   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
4935   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
4936   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
4937   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
4938
4939   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
4940   
4941   (define-prefix-command 'gnus-summary-various-map)
4942   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
4943   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
4944   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
4945   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
4946   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
4947   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
4948   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
4949   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
4950   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
4951   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
4952   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
4953
4954   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
4955
4956   (define-prefix-command 'gnus-summary-sort-map)
4957   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
4958   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
4959   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
4960   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
4961   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
4962   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
4963
4964   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
4965   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
4966   )
4967
4968
4969 \f
4970
4971 (defun gnus-summary-mode ()
4972   "Major mode for reading articles.
4973
4974 All normal editing commands are switched off.
4975 \\<gnus-summary-mode-map>
4976 Each line in this buffer represents one article.  To read an
4977 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
4978 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
4979 respectively.
4980
4981 You can also post articles and send mail from this buffer.  To 
4982 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
4983 of an article, type `\\[gnus-summary-reply]'.
4984
4985 There are approx. one gazillion commands you can execute in this 
4986 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
4987
4988 The following commands are available:
4989
4990 \\{gnus-summary-mode-map}"
4991   (interactive)
4992   (if gnus-visual (gnus-summary-make-menu-bar))
4993   (kill-all-local-variables)
4994   (let ((locals gnus-summary-local-variables))
4995     (while locals
4996       (if (consp (car locals))
4997           (progn
4998             (make-local-variable (car (car locals)))
4999             (set (car (car locals)) (eval (cdr (car locals)))))
5000         (make-local-variable (car locals))
5001         (set (car locals) nil))
5002       (setq locals (cdr locals))))
5003   (gnus-make-thread-indent-array)
5004   (gnus-update-format-specifications)
5005   (setq mode-line-modified "-- ")
5006   (make-local-variable 'mode-line-format)
5007   (setq mode-line-format (copy-sequence mode-line-format))
5008   (and (equal (nth 3 mode-line-format) "   ")
5009        (setcar (nthcdr 3 mode-line-format) ""))
5010   (setq major-mode 'gnus-summary-mode)
5011   (setq mode-name "Summary")
5012   (make-local-variable 'minor-mode-alist)
5013   (use-local-map gnus-summary-mode-map)
5014   (buffer-disable-undo (current-buffer))
5015   (setq buffer-read-only t)             ;Disable modification
5016   (setq truncate-lines t)
5017   (setq selective-display t)
5018   (setq selective-display-ellipses t)   ;Display `...'
5019   ;; Change the display table.  Odd characters have a tendency to mess
5020   ;; up nicely formatted displays.
5021   (setq buffer-display-table (copy-sequence standard-display-table))
5022   (let ((i 32))
5023     (while (>= (setq i (1- i)) 0)
5024       (aset buffer-display-table i [??])))
5025   (aset buffer-display-table ?\n nil)
5026   (let ((i 160))
5027     (while (>= (setq i (1- i)) 127)
5028       (aset buffer-display-table i [??])))
5029   (run-hooks 'gnus-summary-mode-hook))
5030
5031 (defun gnus-summary-clear-local-variables ()
5032   (let ((locals gnus-summary-local-variables))
5033     (while locals
5034       (if (consp (car locals))
5035           (and (vectorp (car (car locals)))
5036                (set (car (car locals)) nil))
5037         (and (vectorp (car locals))
5038              (set (car locals) nil)))
5039       (setq locals (cdr locals)))))
5040
5041 (defun gnus-mouse-pick-article (e)
5042   (interactive "e")
5043   (mouse-set-point e)
5044   (gnus-summary-next-page nil t))
5045
5046 (defun gnus-summary-setup-buffer (group)
5047   "Initialize summary buffer."
5048   (let ((buffer (concat "*Summary " group "*")))
5049     (if (get-buffer buffer)
5050         (progn
5051           (set-buffer buffer)
5052           (not gnus-newsgroup-threads))
5053       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5054       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5055       (gnus-add-current-to-buffer-list)
5056       (gnus-summary-mode)
5057       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5058       (setq gnus-newsgroup-name group)
5059       t)))
5060
5061 (defun gnus-set-global-variables ()
5062   ;; Set the global equivalents of the summary buffer-local variables
5063   ;; to the latest values they had. These reflect the summary buffer
5064   ;; that was in action when the last article was fetched.
5065   (if (eq major-mode 'gnus-summary-mode) 
5066       (progn
5067         (setq gnus-summary-buffer (current-buffer))
5068         (let ((name gnus-newsgroup-name)
5069               (marked gnus-newsgroup-marked)
5070               (unread gnus-newsgroup-unreads)
5071               (headers gnus-current-headers))
5072           (save-excursion
5073             (set-buffer gnus-group-buffer)
5074             (setq gnus-newsgroup-name name)
5075             (setq gnus-newsgroup-marked marked)
5076             (setq gnus-newsgroup-unreads unread)
5077             (setq gnus-current-headers headers))))))
5078
5079 (defun gnus-summary-insert-dummy-line (sformat subject number)
5080   (if (not sformat) 
5081       (setq sformat gnus-summary-dummy-line-format-spec))
5082   (let (b)
5083     (beginning-of-line)
5084     (setq b (point))
5085     (insert (eval sformat))
5086     (add-text-properties
5087      b (1+ b)
5088      (list 'gnus (list number ?Z 0)))))
5089
5090 (defvar gnus-thread-indent-array nil)
5091 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5092 (defun gnus-make-thread-indent-array ()
5093   (let ((n 200))
5094     (if (and gnus-thread-indent-array
5095              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5096         nil
5097       (setq gnus-thread-indent-array (make-vector 201 "")
5098             gnus-thread-indent-array-level gnus-thread-indent-level)
5099       (while (>= n 0)
5100         (aset gnus-thread-indent-array n
5101               (make-string (* n gnus-thread-indent-level) ? ))
5102         (setq n (1- n))))))
5103
5104 (defun gnus-summary-insert-line 
5105   (sformat header level current unread replied expirable subject-or-nil
5106            &optional dummy score)
5107   (or sformat (setq sformat gnus-summary-line-format-spec))
5108   (let* ((indentation (aref gnus-thread-indent-array level))
5109          (lines (header-lines header))
5110          (score (or score gnus-summary-default-score 0))
5111          (score-char (if (or (null gnus-summary-default-score)
5112                              (= score gnus-summary-default-score)) ? 
5113                        (if (< score gnus-summary-default-score) 
5114                            gnus-score-below-mark gnus-score-over-mark)))
5115          (replied (if replied gnus-replied-mark ? ))
5116          (from (header-from header))
5117          (name-address (funcall gnus-extract-address-components from))
5118          (address (car (cdr name-address)))
5119          (name (or (car name-address) (car (cdr name-address))))
5120          (subject (header-subject header))
5121          (number (header-number header))
5122          (opening-bracket (if dummy ?\< ?\[))
5123          (closing-bracket (if dummy ?\> ?\]))
5124          (buffer-read-only nil)
5125          (b (progn (beginning-of-line) (point))))
5126     (or (numberp lines) (setq lines 0))
5127     (insert (eval sformat))
5128     (add-text-properties
5129      b (1+ b) (list 'gnus (list number (or unread gnus-unread-mark) level)))))
5130
5131 (defun gnus-summary-update-line (&optional dont-update)
5132   ;; Update summary line after change.
5133   (or (not gnus-summary-default-score)
5134       gnus-summary-inhibit-highlight
5135       (let ((gnus-summary-inhibit-highlight t)
5136             (article (gnus-summary-article-number)))
5137         (progn
5138           (or dont-update
5139               (if (and gnus-summary-mark-below
5140                        (< (gnus-summary-article-score)
5141                           gnus-summary-mark-below))
5142                   (and (not (memq article gnus-newsgroup-marked))
5143                        (not (memq article gnus-newsgroup-dormant))
5144                        (memq article gnus-newsgroup-unreads)
5145                        (gnus-summary-mark-article nil gnus-low-score-mark))
5146                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5147                      (gnus-summary-mark-article nil gnus-unread-mark))))
5148           (and gnus-visual
5149                (run-hooks 'gnus-summary-update-hook))))))
5150
5151 (defun gnus-summary-update-lines (&optional beg end)
5152   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5153   (let ((beg (or beg (point-min)))
5154         (end (or end (point-max))))
5155     (save-excursion
5156       (set-buffer gnus-summary-buffer)
5157       (goto-char beg)
5158       (while (and (not (eobp)) (< (point) end))
5159         (gnus-summary-update-line)
5160         (forward-line 1)))))
5161
5162 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5163   ;; Sum up all elements (and sub-elements) in a list.
5164   (let ((number 
5165          (if (listp thread) 
5166              (apply 
5167               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5168            1)))
5169     (if char 
5170         (if (> number 1) gnus-not-empty-thread-mark
5171           gnus-empty-thread-mark)
5172       number)))
5173
5174 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5175   "Start reading news in newsgroup GROUP.
5176 If SHOW-ALL is non-nil, already read articles are also listed.
5177 If NO-ARTICLE is non-nil, no article is selected initially."
5178   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5179   (let* ((new-group (gnus-summary-setup-buffer group))
5180          (did-select (and new-group (gnus-select-newsgroup group show-all)))
5181          (method (car (gnus-find-method-for-group group))))
5182     (cond 
5183      ((not new-group)
5184       (gnus-set-global-variables)
5185       (gnus-kill-buffer kill-buffer)
5186       (gnus-configure-windows 'summary)
5187       (gnus-set-mode-line 'summary)
5188       (gnus-summary-position-cursor)
5189       (message "")
5190       t)
5191      ((null did-select) 
5192       (and (eq major-mode 'gnus-summary-mode)
5193            (not (equal (current-buffer) kill-buffer))
5194            (progn
5195              (kill-buffer (current-buffer))
5196              (set-buffer gnus-group-buffer)
5197              (gnus-group-next-unread-group 1)))
5198       nil)
5199      ((eq did-select 'quit)
5200       (and (eq major-mode 'gnus-summary-mode)
5201            (not (equal (current-buffer) kill-buffer))
5202            (kill-buffer (current-buffer)))
5203       (gnus-kill-buffer kill-buffer)
5204       (gnus-configure-windows 'group)
5205       (gnus-group-next-unread-group 1)
5206       (signal 'quit nil))
5207      (t
5208       (gnus-set-global-variables)
5209       ;; Save the active value in effect when the group was entered.
5210       (setq gnus-newsgroup-active 
5211             (gnus-copy-sequence
5212              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5213       ;; You can change the subjects in this hook.
5214       (run-hooks 'gnus-select-group-hook)
5215       ;; Do score processing.
5216       (and gnus-use-scoring (gnus-possibly-score-headers))
5217       ;; Update the format specifiers.
5218       (gnus-update-format-specifications)
5219       ;; Generate the summary buffer.
5220       (gnus-summary-prepare)
5221       (if (zerop (buffer-size))
5222           (cond (gnus-newsgroup-dormant
5223                  (gnus-summary-show-all-dormant))
5224                 ((and gnus-newsgroup-scored show-all)
5225                  (gnus-summary-show-all-expunged))))
5226       ;; Function `gnus-apply-kill-file' must be called in this hook.
5227       (run-hooks 'gnus-apply-kill-hook)
5228       (if (zerop (buffer-size))
5229           (progn
5230             ;; This newsgroup is empty.
5231             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5232             (gnus-message 6 "No unread news")
5233             (gnus-kill-buffer kill-buffer))
5234         (save-excursion
5235           (if kill-buffer
5236               (let ((gnus-summary-buffer kill-buffer))
5237                 (gnus-configure-windows 'group))))
5238         ;; Hide conversation thread subtrees.  We cannot do this in
5239         ;; gnus-summary-prepare-hook since kill processing may not
5240         ;; work with hidden articles.
5241         (and gnus-show-threads
5242              gnus-thread-hide-subtree
5243              (gnus-summary-hide-all-threads))
5244         ;; Show first unread article if requested.
5245         (goto-char (point-min))
5246         (if (and (not no-article)
5247                  gnus-auto-select-first
5248                  (gnus-summary-first-unread-article))
5249             (gnus-configure-windows 'article)
5250           (gnus-configure-windows 'summary))
5251         (gnus-set-mode-line 'summary)
5252         (gnus-summary-position-cursor)
5253         ;; If in async mode, we send some info to the backend.
5254         (and gnus-newsgroup-async
5255              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5256              (gnus-request-asynchronous 
5257               gnus-newsgroup-name
5258               (if (and gnus-asynchronous-article-function
5259                        (fboundp gnus-asynchronous-article-function))
5260                   (funcall gnus-asynchronous-article-function
5261                            gnus-newsgroup-threads)
5262                 gnus-newsgroup-threads)))
5263         (gnus-kill-buffer kill-buffer))))))
5264
5265 (defun gnus-summary-prepare ()
5266   ;; Generate the summary buffer.
5267   (let ((buffer-read-only nil))
5268     (erase-buffer)
5269     (gnus-summary-prepare-threads 
5270      (if gnus-show-threads
5271          (gnus-gather-threads 
5272           (gnus-sort-threads 
5273            (if gnus-summary-expunge-below
5274                (gnus-make-threads-and-expunge)
5275              (gnus-make-threads))))
5276        gnus-newsgroup-headers)
5277      0 nil nil t)
5278     ;; Erase header retrieval message.
5279     (gnus-summary-update-lines)
5280     (message "")
5281     ;; Call hooks for modifying summary buffer.
5282     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5283     (goto-char (point-min))
5284     (run-hooks 'gnus-summary-prepare-hook)))
5285
5286 (defun gnus-gather-threads (threads)
5287   "Gather threads that have lost their roots."
5288   (if (not gnus-summary-make-false-root)
5289       threads 
5290     (let ((hashtb (gnus-make-hashtable 1023))
5291           (prev threads)
5292           (result threads)
5293           thread subject hthread unre-subject whole-subject)
5294       (while threads
5295         (setq subject (header-subject (car (car threads)))
5296               whole-subject subject)
5297         (and gnus-summary-gather-subject-limit
5298              (or (and (numberp gnus-summary-gather-subject-limit)
5299                       (> (length subject) gnus-summary-gather-subject-limit)
5300                       (setq subject
5301                             (substring subject 0 
5302                                        gnus-summary-gather-subject-limit)))
5303                  (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5304                       (setq subject (gnus-simplify-subject-fuzzy subject)))))
5305         (if (setq hthread 
5306                   (gnus-gethash 
5307                    (setq unre-subject (gnus-simplify-subject-re subject))
5308                    hashtb))
5309             (progn
5310               (or (stringp (car (car hthread)))
5311                   (setcar hthread (list whole-subject (car hthread))))
5312               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5313                                            (list (car threads))))
5314               (setcdr prev (cdr threads))
5315               (setq threads prev))
5316           (gnus-sethash unre-subject threads hashtb))
5317         (setq prev threads)
5318         (setq threads (cdr threads)))
5319       result)))
5320
5321 (defun gnus-make-threads ()
5322   ;; This function takes the dependencies already made by 
5323   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5324   ;; through the dependecies in the hash table and finds all the
5325   ;; roots. Roots do not refer back to any valid articles.
5326   (let (roots)
5327     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5328          (gnus-build-old-threads))
5329     (mapatoms
5330      (lambda (refs)
5331        (if (not (car (symbol-value refs)))
5332            (setq roots (append (cdr (symbol-value refs)) roots))
5333          ;; Ok, these refer back to valid articles, but if
5334          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5335          ;; the root has the same subject as its children. The children
5336          ;; that do not are made into roots and removed from the list
5337          ;; of children. 
5338          (or gnus-thread-ignore-subject
5339              (let* ((prev (symbol-value refs))
5340                     (subject (gnus-simplify-subject-re 
5341                               (header-subject (car prev))))
5342                     (headers (cdr prev)))
5343                (while headers
5344                  (if (not (string= subject
5345                                    (gnus-simplify-subject-re 
5346                                     (header-subject (car headers)))))
5347                      (progn
5348                        (setq roots (cons (car headers) roots))
5349                        (setcdr prev (cdr headers)))
5350                    (setq prev headers))
5351                  (setq headers (cdr headers)))))))
5352      gnus-newsgroup-dependencies)
5353     
5354     (mapcar 'gnus-trim-thread
5355             (apply 'append
5356                    (mapcar 'gnus-cut-thread
5357                            (mapcar 'gnus-make-sub-thread roots))))))
5358   
5359 (defun gnus-make-threads-and-expunge ()
5360   ;; This function takes the dependencies already made by 
5361   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5362   ;; through the dependecies in the hash table and finds all the
5363   ;; roots. Roots do not refer back to any valid articles.
5364   (let (roots)
5365     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5366          (gnus-build-old-threads))
5367     (mapatoms
5368      (lambda (refs)
5369        (if (not (car (symbol-value refs)))
5370            (if (and gnus-summary-expunge-below
5371                     (not gnus-fetch-old-headers))
5372                (let ((headers (cdr (symbol-value refs))))
5373                  (while headers
5374                   (if (not (< (or (cdr (assq (header-number (car headers))
5375                                              gnus-newsgroup-scored))
5376                                   gnus-summary-default-score 0)
5377                               gnus-summary-expunge-below))
5378                       (setq roots (cons (car headers) roots))
5379                     (setq gnus-newsgroup-unreads
5380                           (delq (header-number (car headers))
5381                                 gnus-newsgroup-unreads)))
5382                   (setq headers (cdr headers))))
5383              (setq roots (append (cdr (symbol-value refs)) roots)))
5384          ;; Ok, these refer back to valid articles, but if
5385          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5386          ;; the root has the same subject as its children. The children
5387          ;; that do not are made into roots and removed from the list
5388          ;; of children. 
5389          (or gnus-thread-ignore-subject
5390              (let* ((prev (symbol-value refs))
5391                     (subject (gnus-simplify-subject-re 
5392                               (header-subject (car prev))))
5393                     (headers (cdr prev)))
5394                (while headers
5395                  (if (not (string= subject
5396                                    (gnus-simplify-subject-re 
5397                                     (header-subject (car headers)))))
5398                      (progn
5399                        (if (not (< (or (cdr (assq (header-number (car headers))
5400                                                   gnus-newsgroup-scored))
5401                                        gnus-summary-default-score 0)
5402                                    gnus-summary-expunge-below))
5403                            (setq roots (cons (car headers) roots))
5404                          (setq gnus-newsgroup-unreads
5405                                (delq (header-number (car headers))
5406                                      gnus-newsgroup-unreads)))
5407                        (setcdr prev (cdr headers)))
5408                    (setq prev headers))
5409                  (setq headers (cdr headers)))))
5410          (and gnus-summary-expunge-below
5411               (not gnus-fetch-old-headers)
5412               (let* ((prev (symbol-value refs))
5413                      (headers (cdr prev))
5414                      id)
5415                 (while headers
5416                   (if (not (< (or (cdr (assq (header-number (car headers))
5417                                              gnus-newsgroup-scored))
5418                                   gnus-summary-default-score 0)
5419                               gnus-summary-expunge-below))
5420                       (setq prev (cdr prev))
5421                     (setq gnus-newsgroup-unreads 
5422                           (delq (header-number (car headers))
5423                                 gnus-newsgroup-unreads))
5424                     (setcdr prev (cdr headers))
5425                     (setq id (gnus-gethash (header-id (car headers))
5426                                            gnus-newsgroup-dependencies))
5427                     (let ((h (cdr id)))
5428                       (while h
5429                         (if (not (< (or (cdr (assq (header-number (car h))
5430                                                    gnus-newsgroup-scored))
5431                                         gnus-summary-default-score 0)
5432                                     gnus-summary-expunge-below))
5433                             (setq roots (cons (car h) roots)))
5434                         (setq h (cdr h)))))
5435                   (setq headers (cdr headers)))))))
5436      gnus-newsgroup-dependencies)
5437     
5438     (mapcar 'gnus-trim-thread
5439             (apply 'append
5440                    (mapcar 'gnus-cut-thread
5441                            (mapcar 'gnus-make-sub-thread roots))))))
5442   
5443 (defun gnus-cut-thread (thread)
5444   ;; Remove leaf dormant or ancient articles from THREAD.
5445   (let ((head (car thread))
5446         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5447     (if (and (null tail)
5448              (let ((number (header-number head)))
5449                (or (memq number gnus-newsgroup-ancient)
5450                    (memq number gnus-newsgroup-dormant)
5451                    (and gnus-summary-expunge-below
5452                         (eq gnus-fetch-old-headers 'some)
5453                         (< (or (cdr (assq number gnus-newsgroup-scored))
5454                                gnus-summary-default-score 0)
5455                            gnus-summary-expunge-below)
5456                         (progn
5457                           (setq gnus-newsgroup-unreads
5458                                 (delq number gnus-newsgroup-unreads))
5459                           t)))))
5460         nil
5461       (list (cons head tail)))))
5462
5463 (defun gnus-trim-thread (thread)
5464   ;; Remove root ancient articles with only one child from THREAD.
5465   (if (and (eq gnus-fetch-old-headers 'some)
5466            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5467            (= (length thread) 2))
5468       (gnus-trim-thread (nth 1 thread))
5469     thread))
5470
5471 (defun gnus-make-sub-thread (root)
5472   ;; This function makes a sub-tree for a node in the tree.
5473   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5474                                               gnus-newsgroup-dependencies)))))
5475     (cons root (mapcar 'gnus-make-sub-thread children))))
5476
5477 (defun gnus-build-old-threads ()
5478   ;; Look at all the articles that refer back to old articles, and
5479   ;; fetch the headers for the articles that aren't there. This will
5480   ;; build complete threads - if the roots haven't been expired by the
5481   ;; server, that is.
5482   (let (id heads)
5483     (mapatoms
5484      (lambda (refs)
5485        (if (not (car (symbol-value refs)))
5486            (progn
5487              (setq heads (cdr (symbol-value refs)))
5488              (while heads
5489                (if (not (memq (header-number (car heads))
5490                               gnus-newsgroup-dormant))
5491                    (progn
5492                      (setq id (symbol-name refs))
5493                      (while (and (setq id (gnus-build-get-header id))
5494                                  (not (car (gnus-gethash 
5495                                             id gnus-newsgroup-dependencies)))))
5496                      (setq heads nil))
5497                  (setq heads (cdr heads)))))))
5498      gnus-newsgroup-dependencies)))
5499
5500 (defun gnus-build-get-header (id)
5501   ;; Look through the buffer of NOV lines and find the header to
5502   ;; ID. Enter this line into the dependencies hash table, and return
5503   ;; the id of the parent article (if any).
5504   (let ((deps gnus-newsgroup-dependencies)
5505         found header)
5506     (prog1
5507         (save-excursion
5508           (set-buffer nntp-server-buffer)
5509           (goto-char (point-min))
5510           (while (and (not found) (search-forward id nil t))
5511             (beginning-of-line)
5512             (setq found (looking-at 
5513                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5514                                  (regexp-quote id))))
5515             (or found (beginning-of-line 2)))
5516           (if found
5517               (let (ref)
5518                 (beginning-of-line)
5519                 (and
5520                  (setq header (gnus-nov-parse-line 
5521                                (read (current-buffer)) deps))
5522                  (setq ref (header-references header))
5523                  (string-match "\\(<[^>]+>\\) *$" ref)
5524                  (substring ref (match-beginning 1) (match-end 1))))))
5525       (and header
5526            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5527                  gnus-newsgroup-ancient (cons (header-number header)
5528                                               gnus-newsgroup-ancient))))))
5529
5530 ;; Re-build the thread containing ID.
5531 (defun gnus-rebuild-thread (id)
5532   (let ((dep gnus-newsgroup-dependencies)
5533         (buffer-read-only nil)
5534         parent headers refs thread art)
5535     (while (and id (setq headers
5536                          (car (setq art (gnus-gethash (downcase id) dep)))))
5537       (setq parent art)
5538       (setq id (and (setq refs (header-references headers))
5539                     (string-match "\\(<[^>]+>\\) *$" refs)
5540                     (substring refs (match-beginning 1) (match-end 1)))))
5541     (setq thread (gnus-make-sub-thread (car parent)))
5542     (gnus-rebuild-remove-articles thread)
5543     (let ((beg (point)))
5544       (gnus-summary-prepare-threads (list thread) 0)
5545       (gnus-summary-update-lines beg (point)))))
5546
5547 ;; Delete all lines in the summary buffer that correspond to articles
5548 ;; in this thread.
5549 (defun gnus-rebuild-remove-articles (thread)
5550   (and (gnus-summary-goto-subject (header-number (car thread)))
5551        (gnus-delete-line))
5552   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5553
5554 (defun gnus-sort-threads (threads)
5555   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5556   (let ((fun gnus-thread-sort-functions))
5557     (while fun
5558       (setq threads (sort threads (car fun))
5559             fun (cdr fun))))
5560   threads)
5561
5562 (defun gnus-thread-header (thread)
5563   ;; Return header of first article in THREAD.
5564   (if (consp thread)
5565       (if (stringp (car thread))
5566           (car (car (cdr thread)))
5567         (car thread))
5568     thread))
5569
5570 (defun gnus-thread-sort-by-number (h1 h2)
5571   "Sort threads by root article number."
5572   (let ((h1 (gnus-thread-header h1))
5573         (h2 (gnus-thread-header h2)))
5574     (< (header-number h1) (header-number h2))))
5575
5576 (defun gnus-thread-sort-by-author (h1 h2)
5577   "Sort threads by root author."
5578   (let ((h1 (gnus-thread-header h1))
5579         (h2 (gnus-thread-header h2)))
5580     (string-lessp
5581      (let ((extract (funcall 
5582                      gnus-extract-address-components (header-from h1))))
5583        (or (car extract) (cdr extract)))
5584      (let ((extract (funcall
5585                      gnus-extract-address-components (header-from h2))))
5586        (or (car extract) (cdr extract))))))
5587
5588 (defun gnus-thread-sort-by-subject (h1 h2)
5589   "Sort threads by root subject."
5590   (let ((h1 (gnus-thread-header h1))
5591         (h2 (gnus-thread-header h2)))
5592     (string-lessp
5593      (downcase (gnus-simplify-subject (header-subject h1)))
5594      (downcase (gnus-simplify-subject (header-subject h2))))))
5595
5596 (defun gnus-thread-sort-by-date (h1 h2)
5597   "Sort threads by root article date."
5598   (let ((h1 (gnus-thread-header h1))
5599         (h2 (gnus-thread-header h2)))
5600     (string-lessp
5601      (gnus-sortable-date (header-date h1))
5602      (gnus-sortable-date (header-date h2)))))
5603
5604 (defun gnus-thread-sort-by-score (h1 h2)
5605   "Sort threads by root article score.
5606 Unscored articles will be counted as havin a score of zero."
5607   (let ((h1 (gnus-thread-header h1))
5608         (h2 (gnus-thread-header h2)))
5609     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5610           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5611       (> (or (cdr s1) gnus-summary-default-score 0)
5612          (or (cdr s2) gnus-summary-default-score 0)))))
5613
5614 (defun gnus-thread-sort-by-total-score (h1 h2)
5615   "Sort threads by the sum of all scores in the thread.
5616 Unscored articles will be counted as havin a score of zero."
5617   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5618
5619 (defun gnus-thread-total-score (thread)
5620   ;;  This function find the total score of  THREAD.
5621   (if (consp thread)
5622       (if (stringp (car thread))
5623           (apply gnus-thread-score-function 0
5624                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5625         (gnus-thread-total-score-1 thread))
5626     (gnus-thread-total-score-1 (list thread))))
5627
5628 (defun gnus-thread-total-score-1 (root)
5629   ;; This function find the total score of the thread below ROOT.
5630   (setq root (car root))
5631   (apply gnus-thread-score-function
5632          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5633              gnus-summary-default-score 0)
5634          (mapcar 'gnus-thread-total-score
5635                  (cdr (gnus-gethash (downcase (header-id root))
5636                                     gnus-newsgroup-dependencies)))))
5637
5638 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5639 (defvar gnus-tmp-prev-subject "")
5640 (defvar gnus-tmp-adopt-thread nil)
5641
5642 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
5643 (defun gnus-summary-prepare-threads 
5644   (threads level &optional not-child no-subject cull)
5645   "Prepare summary buffer from THREADS and indentation LEVEL.  
5646 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
5647 or a straight list of headers."
5648   (let (thread header number subject clevel)
5649     (while threads
5650       (setq thread (car threads)
5651             threads (cdr threads))
5652       ;; If `thread' is a cons, hierarchical threads are used.  If not,
5653       ;; `thread' is the header.
5654       (if (consp thread)
5655           (setq header (car thread))
5656         (setq header thread)
5657         (and cull
5658              (or (memq (setq number (header-number header))
5659                        gnus-newsgroup-dormant)
5660                  (and gnus-summary-expunge-below
5661                       (< (or (cdr (assq number gnus-newsgroup-scored))
5662                              gnus-summary-default-score 0)
5663                          gnus-summary-expunge-below)))
5664              (progn
5665                (setq header nil)
5666                (setq gnus-newsgroup-unreads 
5667                      (delq number gnus-newsgroup-unreads)))))
5668       (cond 
5669        ((stringp header)
5670         ;; The header is a dummy root.
5671         (cond ((eq gnus-summary-make-false-root 'adopt)
5672                ;; We let the first article adopt the rest.
5673                (let ((gnus-tmp-adopt-thread thread))
5674                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
5675                (setq thread (cdr (cdr thread)))
5676                (while thread
5677                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
5678                  (setq thread (cdr thread))))
5679               ((eq gnus-summary-make-false-root 'dummy)
5680                ;; We output a dummy root.
5681                (gnus-summary-insert-dummy-line 
5682                 nil header (header-number (car (car (cdr thread)))))
5683                (setq clevel 1))
5684               ((eq gnus-summary-make-false-root 'empty)
5685                ;; We print the articles with empty subject fields. 
5686                (let ((gnus-tmp-adopt-thread thread))
5687                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
5688                (setq thread (cdr (cdr thread)))
5689                (while thread
5690                  (gnus-summary-prepare-threads 
5691                   (list (car thread)) 0 nil
5692                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
5693                             (not (string=  
5694                                   (gnus-simplify-subject-re 
5695                                    (header-subject (car (car thread))))
5696                                   (gnus-simplify-subject-re header))))))
5697                  (setq thread (cdr thread))))
5698               (t
5699                ;; We do not make a root for the gathered
5700                ;; sub-threads at all.  
5701                (setq clevel 0)))
5702         ;; Print the sub-threads.
5703         (and (consp thread) (cdr thread)
5704              (gnus-summary-prepare-threads (cdr thread) clevel)))
5705        ;; The header is a real article.
5706        (header
5707         (setq number (header-number header)
5708               subject (header-subject header))
5709         (and gnus-newsgroup-async
5710              (setq gnus-newsgroup-threads
5711                    (cons (cons (header-number header)
5712                                (header-lines header)) gnus-newsgroup-threads)))
5713         (gnus-summary-insert-line
5714          nil header level nil 
5715          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
5716                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
5717                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
5718                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
5719                (t gnus-ancient-mark))
5720          (memq number gnus-newsgroup-replied)
5721          (memq number gnus-newsgroup-expirable)
5722          (if no-subject 
5723              gnus-summary-same-subject
5724            (if (or (zerop level)
5725                    (and gnus-thread-ignore-subject
5726                         (not (string= 
5727                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
5728                               (gnus-simplify-subject-re subject)))))
5729                subject
5730              gnus-summary-same-subject))
5731          not-child
5732          (cdr (assq number gnus-newsgroup-scored)))
5733         (setq gnus-tmp-prev-subject subject)
5734         ;; Recursively print subthreads.
5735         (and (consp thread) (cdr thread)
5736              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
5737
5738 (defun gnus-select-newsgroup (group &optional read-all)
5739   "Select newsgroup GROUP.
5740 If READ-ALL is non-nil, all articles in the group are selected."
5741   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5742          (info (nth 2 entry))
5743          articles header-marks)
5744     (gnus-check-news-server
5745      (setq gnus-current-select-method (gnus-find-method-for-group group)))
5746
5747     (or (gnus-server-opened gnus-current-select-method)
5748         (gnus-open-server gnus-current-select-method)
5749         (error "Couldn't open server"))
5750     
5751     (or (and (eq (car entry) t)
5752              (gnus-activate-newsgroup (car info)))
5753         (gnus-request-group group t)
5754         (progn
5755           (kill-buffer (current-buffer))
5756           (error "Couldn't request group %s: %s" 
5757                  group (gnus-status-message group))))
5758
5759     (setq gnus-newsgroup-name group)
5760     (setq gnus-newsgroup-unselected nil)
5761     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5762
5763     (and gnus-asynchronous
5764          (gnus-check-backend-function 
5765           'request-asynchronous gnus-newsgroup-name)
5766          (setq gnus-newsgroup-async
5767                (gnus-request-asynchronous gnus-newsgroup-name)))
5768
5769     (setq articles (gnus-articles-to-read group read-all))
5770
5771     (cond 
5772      ((null articles) 
5773       (gnus-message 3 "Couldn't select newsgroup")
5774       'quit)
5775      ((eq articles 0) nil)
5776      (t
5777       ;; Init the dependencies hash table.
5778       (setq gnus-newsgroup-dependencies 
5779             (gnus-make-hashtable (length articles)))
5780       ;; Retrieve the headers and read them in.
5781       (setq gnus-newsgroup-headers 
5782             (if (eq 'nov (setq gnus-headers-retrieved-by
5783                                (gnus-retrieve-headers 
5784                                 (if gnus-fetch-old-headers 
5785                                     (cons 1 articles) articles) 
5786                                 gnus-newsgroup-name)))
5787                 (progn
5788                   (gnus-get-newsgroup-headers-xover articles))
5789               (gnus-get-newsgroup-headers)))
5790       ;; If we were to fetch old headers, but the backend didn't
5791       ;; support XOVER, then it is possible we fetched one article
5792       ;; that we shouldn't have. If that's the case, we pop it off the
5793       ;; list of headers.
5794       (and (not (eq gnus-headers-retrieved-by 'nov))
5795            gnus-fetch-old-headers
5796            gnus-newsgroup-headers
5797            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
5798            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
5799       ;; Remove cancelled articles from the list of unread articles.
5800       (setq gnus-newsgroup-unreads
5801             (gnus-set-sorted-intersection 
5802              gnus-newsgroup-unreads
5803              (mapcar (lambda (headers) (header-number headers))
5804                      gnus-newsgroup-headers)))
5805       ;; Adjust and set lists of article marks.
5806       (and info
5807            (let (marked)
5808              (gnus-adjust-marked-articles info)
5809              (setq gnus-newsgroup-marked 
5810                    (cdr (assq 'tick (setq marked (nth 3 info)))))
5811              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
5812              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
5813              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
5814              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
5815              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
5816              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
5817              (setq gnus-newsgroup-processable nil)))
5818       ;; Check whether auto-expire is to be done in this group.
5819       (setq gnus-newsgroup-auto-expire
5820             (or (and (stringp gnus-auto-expirable-newsgroups)
5821                      (string-match gnus-auto-expirable-newsgroups group))
5822                 (memq 'auto-expire (nth 5 info))))
5823       ;; First and last article in this newsgroup.
5824       (and gnus-newsgroup-headers
5825            (setq gnus-newsgroup-begin 
5826                  (header-number (car gnus-newsgroup-headers)))
5827            (setq gnus-newsgroup-end
5828                  (header-number (gnus-last-element gnus-newsgroup-headers))))
5829       (setq gnus-reffed-article-number -1)
5830       ;; GROUP is successfully selected.
5831       (or gnus-newsgroup-headers t)))))
5832
5833 (defun gnus-articles-to-read (group read-all)
5834   ;; Find out what articles the user wants to read.
5835   (let* ((articles
5836           ;; Select all articles if `read-all' is non-nil, or if all the
5837           ;; unread articles are dormant articles.
5838           (if (or read-all
5839                   (= (length gnus-newsgroup-unreads) 
5840                      (length gnus-newsgroup-dormant)))
5841               (gnus-uncompress-range 
5842                (gnus-gethash group gnus-active-hashtb))
5843             gnus-newsgroup-unreads))
5844          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5845          (scored (length scored-list))
5846          (number (length articles))
5847          (marked (+ (length gnus-newsgroup-marked)
5848                     (length gnus-newsgroup-dormant)))
5849          (select
5850           (condition-case ()
5851               (cond ((and (or (<= scored marked)
5852                               (= scored number))
5853                           (numberp gnus-large-newsgroup)
5854                           (> number gnus-large-newsgroup))
5855                      (let ((input
5856                             (read-string
5857                              (format
5858                               "How many articles from %s (default %d): "
5859                               gnus-newsgroup-name number))))
5860                        (if (string-equal input "")
5861                            number input)))
5862                     ((and (> scored marked) (< scored number))
5863                      (let ((input
5864                             (read-string
5865                              (format 
5866                               "%s %s (%d scored, %d total): "
5867                               "How many articles from"
5868                               group scored number))))
5869                        (if (string-equal input "")
5870                            number input)))
5871                     (t number))
5872             (quit nil)))
5873          total-articles)
5874     (setq select (if (stringp select) (string-to-number select) select))
5875     (if (or (null select) (zerop select))
5876         select
5877       (if (and (not (zerop scored)) (<= (abs select) scored))
5878           (progn
5879             (setq articles (sort scored-list '<))
5880             (setq number (length articles)))
5881         (setq articles (copy-sequence articles)))
5882
5883       (setq total-articles articles)
5884       
5885       (if (< (abs select) number)
5886           (if (< select 0) 
5887               ;; Select the N oldest articles.
5888               (setcdr (nthcdr (1- (abs select)) articles) nil)
5889             ;; Select the N most recent articles.
5890             (setq articles (nthcdr (- number select) articles))))
5891       (setq gnus-newsgroup-unselected
5892             (gnus-sorted-intersection
5893              gnus-newsgroup-unreads
5894              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
5895       articles)))
5896
5897 (defun gnus-killed-articles (killed articles)
5898   (let (out)
5899     (while articles
5900       (if (inline (gnus-member-of-range (car articles) killed))
5901           (setq out (cons (car articles) out)))
5902       (setq articles (cdr articles)))
5903     out))
5904
5905 (defun gnus-adjust-marked-articles (info &optional active)
5906   "Remove all marked articles that are no longer legal."
5907   (let ((marked-lists (nth 3 info))
5908         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
5909         marked m prev)
5910     ;; There are many types of marked articles.
5911     (while marked-lists
5912       (setq m (cdr (setq prev (car marked-lists))))
5913       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
5914              ;; Make sure that all ticked articles are a subset of the
5915              ;; unread/unselected articles.
5916              (while m
5917                (if (or (memq (car m) gnus-newsgroup-unreads)
5918                        (memq (car m) gnus-newsgroup-unselected))
5919                    (setq prev m)
5920                  (setcdr prev (cdr m)))
5921                (setq m (cdr m))))
5922             ((eq 'score (car prev))
5923              ;; Scored articles should be a subset of
5924              ;; unread/unselected articles. 
5925              (while m
5926                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
5927                        (memq (car (car m)) gnus-newsgroup-unreads))
5928                    (setq prev m)
5929                  (setcdr prev (cdr m)))
5930                (setq m (cdr m))))
5931             ((eq 'bookmark (car prev))
5932              ;; Bookmarks should be a subset of active articles.
5933              (while m
5934                (if (< (car (car m)) (car active))
5935                    (setcdr prev (cdr m))
5936                  (setq prev m))
5937                (setq m (cdr m))))
5938             ((eq 'killed (car prev))
5939              ;; Articles that have been through the kill process are
5940              ;; to be a subset of active articles.
5941              (while (and m (< (or (and (numberp (car m)) (car m))
5942                                   (cdr (car m)))
5943                               (car active)))
5944                (setcdr prev (cdr m))
5945                (setq m (cdr m)))
5946              (if (and m (< (or (and (numberp (car m)) (car m))
5947                                (car (car m)))
5948                            (car active))) 
5949                  (setcar (if (numberp (car m)) m (car m)) (car active))))
5950             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
5951              ;; The replied and expirable articles have to be articles
5952              ;; that are active. 
5953              (while m
5954                (if (< (car m) (car active))
5955                    (setcdr prev (cdr m))
5956                  (setq prev m))
5957                (setq m (cdr m)))))
5958       (setq marked-lists (cdr marked-lists)))
5959     ;; Remove all lists that are empty.
5960     (setq marked-lists (nth 3 info))
5961     (if marked-lists
5962         (progn
5963           (while (= 1 (length (car marked-lists)))
5964             (setq marked-lists (cdr marked-lists)))
5965           (setq m (cdr (setq prev marked-lists)))
5966           (while m
5967             (if (= 1 (length (car m)))
5968                 (setcdr prev (cdr m))
5969               (setq prev m))
5970             (setq m (cdr m)))
5971           (setcar (nthcdr 3 info) marked-lists)))
5972     ;; Finally, if there are no marked lists at all left, and if there
5973     ;; are no elements after the lists in the info list, we just chop
5974     ;; the info list off before the marked lists.
5975     (and (null marked-lists) 
5976          (not (nthcdr 4 info))
5977          (setcdr (nthcdr 2 info) nil)))
5978   info)
5979
5980 (defun gnus-set-marked-articles 
5981   (info ticked replied expirable killed dormant bookmark score) 
5982   "Enter the various lists of marked articles into the newsgroup info list."
5983   (let (newmarked)
5984     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
5985     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
5986     (and expirable (setq newmarked (cons (cons 'expire expirable) 
5987                                          newmarked)))
5988     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
5989     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
5990     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
5991                                         newmarked)))
5992     (and score (setq newmarked (cons (cons 'score score) newmarked)))
5993     (if (nthcdr 3 info)
5994         (if newmarked
5995             (setcar (nthcdr 3 info) newmarked)
5996           (if (not (nthcdr 4 info))
5997               (setcdr (nthcdr 2 info) nil)
5998             (setcar (nthcdr 3 info) nil)))
5999       (if newmarked
6000           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
6001
6002 (defun gnus-add-marked-articles (group type articles &optional info force)
6003   ;; Add ARTICLES of TYPE to the info of GROUP.
6004   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6005   ;; add, but replace marked articles of TYPE with ARTICLES.
6006   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6007         marked m)
6008     (or (not info)
6009         (and (not (setq marked (nthcdr 3 info)))
6010              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6011         (and (not (setq m (assq type (car marked))))
6012              (setcar marked (cons (cons type articles) (car marked))))
6013         (if force
6014             (setcdr m articles)
6015           (nconc m articles)))))
6016          
6017 (defun gnus-set-mode-line (where)
6018   "This function sets the mode line of the article or summary buffers.
6019 If WHERE is `summary', the summary mode line format will be used."
6020   (if (memq where gnus-updated-mode-lines)
6021       (let (mode-string)
6022         (save-excursion
6023           (set-buffer gnus-summary-buffer)
6024           (let* ((mformat (if (eq where 'article) 
6025                               gnus-article-mode-line-format-spec
6026                             gnus-summary-mode-line-format-spec))
6027                  (group-name gnus-newsgroup-name)
6028                  (article-number (or gnus-current-article 0))
6029                  (unread (- (length gnus-newsgroup-unreads)
6030                             (length gnus-newsgroup-dormant)))
6031                  (unread-and-unticked 
6032                   (- unread (length gnus-newsgroup-marked)))
6033                  (unselected (length gnus-newsgroup-unselected))
6034                  (unread-and-unselected
6035                   (cond ((and (zerop unread-and-unticked)
6036                               (zerop unselected)) "")
6037                         ((zerop unselected) 
6038                          (format "{%d more}" unread-and-unticked))
6039                         (t (format "{%d(+%d) more}"
6040                                    unread-and-unticked unselected))))
6041                  (subject
6042                   (if gnus-current-headers
6043                       (header-subject gnus-current-headers) ""))
6044                  (max-len (and gnus-mode-non-string-length
6045                                (- (frame-width) gnus-mode-non-string-length)))
6046                  header) ;; passed as argument to any user-format-funcs
6047             (setq mode-string (eval mformat))
6048             (or (numberp max-len)
6049                 (setq max-len (length mode-string)))
6050             (if (< max-len 4) (setq max-len 4))
6051             (if (> (length mode-string) max-len)
6052                 (setq mode-string 
6053                       (concat (substring mode-string 0 (- max-len 3))
6054                               "...")))
6055             (setq mode-string (format (format "%%-%ds" max-len)
6056                                       mode-string))))
6057         (setq mode-line-buffer-identification mode-string)
6058         (set-buffer-modified-p t))))
6059
6060 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6061   "Go through the HEADERS list and add all Xrefs to a hash table.
6062 The resulting hash table is returned, or nil if no Xrefs were found."
6063   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6064          (prefix (if (and 
6065                       (gnus-group-foreign-p from-newsgroup)
6066                       (not (memq 'virtual 
6067                                  (assoc (symbol-name (car from-method))
6068                                         gnus-valid-select-methods))))
6069                      (gnus-group-real-prefix from-newsgroup)))
6070          (xref-hashtb (make-vector 63 0))
6071          start group entry number xrefs header)
6072     (while headers
6073       (setq header (car headers))
6074       (if (and (setq xrefs (header-xref header))
6075                (not (memq (header-number header) unreads)))
6076           (progn
6077             (setq start 0)
6078             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6079               (setq start (match-end 0))
6080               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6081                                                     (match-end 1))))
6082               (setq number 
6083                     (string-to-int (substring xrefs (match-beginning 2) 
6084                                               (match-end 2))))
6085               (if (setq entry (gnus-gethash group xref-hashtb))
6086                   (setcdr entry (cons number (cdr entry)))
6087                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6088       (setq headers (cdr headers)))
6089     (if start xref-hashtb nil)))
6090
6091 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6092   "Look through all the headers and mark the Xrefs as read."
6093   (let ((virtual (memq 'virtual 
6094                        (assoc (symbol-name (car (gnus-find-method-for-group 
6095                                                  from-newsgroup)))
6096                               gnus-valid-select-methods)))
6097         name entry read info xref-hashtb idlist active num range exps method
6098         nth4)
6099     (save-excursion
6100       (set-buffer gnus-group-buffer)
6101       (if (setq xref-hashtb 
6102                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6103           (mapatoms 
6104            (lambda (group)
6105              (if (string= from-newsgroup (setq name (symbol-name group)))
6106                  ()
6107                (setq idlist (symbol-value group))
6108                ;; Dead groups are not updated.
6109                (if (and (prog1 
6110                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6111                                   info (nth 2 entry))
6112                           (if (stringp (setq nth4 (nth 4 info)))
6113                               (setq nth4 (gnus-server-to-method nth4))))
6114                         ;; Only do the xrefs if the group has the same
6115                         ;; select method as the group we have just read.
6116                         (or (gnus-methods-equal-p 
6117                              nth4 (gnus-find-method-for-group from-newsgroup))
6118                             virtual
6119                             (equal nth4 
6120                                    (setq method (gnus-find-method-for-group 
6121                                                  from-newsgroup)))
6122                             (and (equal (car nth4) (car method))
6123                                  (equal (nth 1 nth4) (nth 1 method))))
6124                         gnus-use-cross-reference
6125                         (or (not (eq gnus-use-cross-reference t))
6126                             virtual
6127                             ;; Only do cross-references on subscribed
6128                             ;; groups, if that is what is wanted.  
6129                             (<= (nth 1 info) gnus-level-subscribed)))
6130                    (progn
6131                      (setq num 0)
6132                      ;; Set the new list of read articles in this group.
6133                      (setq active (gnus-gethash name gnus-active-hashtb))
6134                      ;; First peel off all illegal article numbers.
6135                      (if active
6136                          (let ((ids idlist)
6137                                (ticked (cdr (assq 'tick (nth 3 info))))
6138                                (dormant (cdr (assq 'dormant (nth 3 info))))
6139                                id)
6140                            (setq exps nil)
6141                            (while ids
6142                              (setq id (car ids))
6143                              (if (or (> id (cdr active))
6144                                      (< id (car active))
6145                                      (memq id ticked)
6146                                      (memq id dormant))
6147                                  (setq idlist (delq id idlist)))
6148                              (and (memq id expirable)
6149                                   (setq exps (cons id exps)))
6150                              (setq ids (cdr ids)))))
6151                      ;; Update expirable articles.
6152                      (gnus-add-marked-articles nil 'expirable exps info)
6153                      (and (null (nth 2 info))
6154                           (> (car active) 1)
6155                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6156                      (setcar (nthcdr 2 info)
6157                              (setq range
6158                                    (gnus-add-to-range 
6159                                     (nth 2 info) 
6160                                     (setq idlist (sort idlist '<)))))
6161                      ;; Then we have to re-compute how many unread
6162                      ;; articles there are in this group.
6163                      (if active
6164                          (progn
6165                            (cond 
6166                             ((not range)
6167                              (setq num (- (1+ (cdr active)) (car active))))
6168                             ((not (listp (cdr range)))
6169                              (setq num (- (cdr active) (- (1+ (cdr range)) 
6170                                                           (car range)))))
6171                             (t
6172                              (while range
6173                                (if (numberp (car range))
6174                                    (setq num (1+ num))
6175                                  (setq num (+ num (- (1+ (cdr (car range)))
6176                                                      (car (car range))))))
6177                                (setq range (cdr range)))
6178                              (setq num (- (cdr active) num))))
6179                            ;; Update the number of unread articles.
6180                            (setcar 
6181                             entry 
6182                             (max 0 (- num 
6183                                       (length (cdr (assq 'tick (nth 3 info))))
6184                                       (length 
6185                                        (cdr (assq 'dormant (nth 3 info)))))))
6186                            ;; Update the group buffer.
6187                            (gnus-group-update-group name t)))))))
6188            xref-hashtb)))))
6189
6190 (defun gnus-methods-equal-p (m1 m2)
6191   (let ((m1 (or m1 gnus-select-method))
6192         (m2 (or m2 gnus-select-method)))
6193     (or (equal m1 m2)
6194         (and (eq (car m1) (car m2))
6195              (or (not (memq 'address (assoc (symbol-name (car m1))
6196                                             gnus-valid-select-methods)))
6197                  (equal (nth 1 m1) (nth 1 m2)))))))
6198
6199 (defsubst gnus-header-value ()
6200   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6201
6202 (defun gnus-get-newsgroup-headers ()
6203   (setq gnus-article-internal-prepare-hook nil)
6204   (let ((cur nntp-server-buffer)
6205         (dependencies gnus-newsgroup-dependencies)
6206         (none-id 0)
6207         headers char article id dep end)
6208     (save-excursion
6209       (set-buffer nntp-server-buffer)
6210       (goto-char (point-min))
6211       ;; Search to the beginning of the next header. Error messages
6212       ;; do not begin with 2 or 3.
6213       (while (re-search-forward "^[23][0-9]+ " nil t)
6214         (let ((header (make-vector 9 nil))
6215               (c (following-char))
6216               (case-fold-search t)
6217               (p (point))
6218               from subject in-reply-to references ref)
6219           (setq id nil
6220                 ref nil
6221                 references nil
6222                 subject nil
6223                 from nil)
6224           (header-set-number header (setq article (read cur)))
6225           ;; This implementation of this function, with nine
6226           ;; search-forwards instead of the one re-search-forward and
6227           ;; a case (which basically was the old function) is actually
6228           ;; about twice as fast, even though it looks messier. You
6229           ;; can't have everything, I guess. Speed and elegance
6230           ;; doesn't always come hand in hand.
6231           (save-restriction
6232             (narrow-to-region (point) (save-excursion 
6233                                         (search-forward "\n.\n" nil t)))
6234             (if (search-forward "\nfrom: " nil t)
6235                 (header-set-from header (gnus-header-value))
6236               (header-set-from header "(nobody)"))
6237             (goto-char p)
6238             (if (search-forward "\nsubject: " nil t)
6239                 (header-set-subject header (gnus-header-value))
6240               (header-set-subject header "(none)"))
6241             (goto-char p)
6242             (and (search-forward "\nxref: " nil t)
6243                  (header-set-xref header (gnus-header-value)))
6244             (goto-char p)
6245             (or (numberp (and (search-forward "\nlines: " nil t)
6246                               (header-set-lines header (read cur))))
6247                 (header-set-lines header 0))
6248             (goto-char p)
6249             (and (search-forward "\ndate: " nil t)
6250                  (header-set-date header (gnus-header-value)))
6251             (goto-char p)
6252             (if (search-forward "\nmessage-id: " nil t)
6253                 (header-set-id header (setq id (gnus-header-value)))
6254               ;; If there was no message-id, we just fake one to make
6255               ;; subsequent routines simpler.
6256               (header-set-id 
6257                header 
6258                (setq id (concat "none+" (int-to-string 
6259                                          (setq none-id (1+ none-id)))))))
6260             (goto-char p)
6261             (if (search-forward "\nreferences: " nil t)
6262                 (progn
6263                   (header-set-references header (gnus-header-value))
6264                   (setq end (match-end 0))
6265                   (save-excursion
6266                     (setq ref 
6267                           (downcase
6268                            (buffer-substring
6269                             (progn 
6270                               (end-of-line)
6271                               (search-backward ">" end t)
6272                               (1+ (point)))
6273                             (progn
6274                               (search-backward "<" end t)
6275                               (point)))))))
6276               ;; Get the references from the in-reply-to header if there
6277               ;; ware no references and the in-reply-to header looks
6278               ;; promising. 
6279               (if (and (search-forward "\nin-reply-to: " nil t)
6280                        (setq in-reply-to (gnus-header-value))
6281                        (string-match "<[^>]+>" in-reply-to))
6282                   (progn
6283                     (header-set-references 
6284                      header 
6285                      (setq ref (substring in-reply-to (match-beginning 0)
6286                                           (match-end 0))))
6287                     (setq ref (downcase ref)))
6288                 (setq ref "none")))
6289             ;; We do some threading while we read the headers. The
6290             ;; message-id and the last reference are both entered into
6291             ;; the same hash table. Some tippy-toeing around has to be
6292             ;; done in case an article has arrived before the article
6293             ;; which it refers to.
6294             (if (boundp (setq dep (intern (downcase id) dependencies)))
6295                 (if (car (symbol-value dep))
6296                     ;; An article with this Message-ID has already
6297                     ;; been seen, so we ignore this one, except we add
6298                     ;; any additional Xrefs (in case the two articles
6299                     ;; came from different servers.
6300                     (progn
6301                       (header-set-xref 
6302                        (car (symbol-value dep))
6303                        (concat (or (header-xref (car (symbol-value dep))) "")
6304                                (or (header-xref header) "")))
6305                       (setq header nil))
6306                   (setcar (symbol-value dep) header))
6307               (set dep (list header)))
6308             (if header
6309                 (progn
6310                   (if (boundp (setq dep (intern ref dependencies)))
6311                       (setcdr (symbol-value dep) 
6312                               (cons header (cdr (symbol-value dep))))
6313                     (set dep (list nil header)))
6314                   (setq headers (cons header headers))))
6315             (goto-char (point-max))))))
6316     (nreverse headers)))
6317
6318 ;; The following macros and functions were written by Felix Lee
6319 ;; <flee@cse.psu.edu>. 
6320
6321 (defmacro gnus-nov-read-integer ()
6322   '(prog1
6323        (if (= (following-char) ?\t)
6324            0
6325          (let ((num (read buffer)))
6326            (if (numberp num) num 0)))
6327      (or (eobp) (forward-char 1))))
6328
6329 (defmacro gnus-nov-skip-field ()
6330   '(search-forward "\t" eol 'move))
6331
6332 (defmacro gnus-nov-field ()
6333   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6334
6335 ;; Goes through the xover lines and returns a list of vectors
6336 (defun gnus-get-newsgroup-headers-xover (sequence)
6337   "Parse the news overview data in the server buffer, and return a
6338 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6339   ;; Get the Xref when the users reads the articles since most/some
6340   ;; NNTP servers do not include Xrefs when using XOVER.
6341   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6342   (let ((cur nntp-server-buffer)
6343         (dependencies gnus-newsgroup-dependencies)
6344         (none 0)
6345         number headers header)
6346     (save-excursion
6347       (set-buffer nntp-server-buffer)
6348       (goto-char (point-min))
6349       (while (and sequence (not (eobp)))
6350         (setq number (read cur))
6351         (while (and sequence (< (car sequence) number))
6352           (setq sequence (cdr sequence)))
6353         (and sequence 
6354              (eq number (car sequence))
6355              (progn
6356                (setq sequence (cdr sequence))
6357                (if (setq header 
6358                          (inline (gnus-nov-parse-line number dependencies)))
6359                    (setq headers (cons header headers)))))
6360         (forward-line 1))
6361       (setq headers (nreverse headers)))
6362     headers))
6363
6364 ;; This function has to be called with point after the article number
6365 ;; on the beginning of the line.
6366 (defun gnus-nov-parse-line (number dependencies)
6367   (let ((none 0)
6368         (eol (gnus-point-at-eol)) 
6369         (buffer (current-buffer))
6370         header ref id dep)
6371
6372     ;; overview: [num subject from date id refs chars lines misc]
6373     (narrow-to-region (point) eol)
6374     (forward-char)
6375
6376     (condition-case nil
6377         (setq header
6378               (vector 
6379                number                   ; number
6380                (gnus-nov-field)         ; subject
6381                (gnus-nov-field)         ; from
6382                (gnus-nov-field)         ; date
6383                (setq id (or (gnus-nov-field)
6384                             (concat "none+"
6385                                     (int-to-string 
6386                                      (setq none (1+ none)))))) ; id
6387                (progn
6388                  (save-excursion
6389                    (let ((beg (point)))
6390                      (search-forward "\t" eol)
6391                      (if (search-backward ">" beg t)
6392                          (setq ref 
6393                                (downcase 
6394                                 (buffer-substring 
6395                                  (1+ (point))
6396                                  (progn
6397                                    (search-backward "<" beg t)
6398                                    (point)))))
6399                        (setq ref nil))))
6400                  (gnus-nov-field))      ; refs
6401                (gnus-nov-read-integer)  ; chars
6402                (gnus-nov-read-integer)  ; lines
6403                (if (= (following-char) ?\n)
6404                    nil
6405                  (gnus-nov-field))      ; misc
6406                ))
6407       (quit (progn 
6408                (setq header nil)
6409                (goto-char eol))))
6410
6411     (widen)
6412
6413     ;; We build the thread tree.
6414     (and header
6415          (if (boundp (setq dep (intern (downcase id) dependencies)))
6416              (if (car (symbol-value dep))
6417                  ;; An article with this Message-ID has already been seen,
6418                  ;; so we ignore this one, except we add any additional
6419                  ;; Xrefs (in case the two articles came from different
6420                  ;; servers.
6421                  (progn
6422                    (header-set-xref 
6423                     (car (symbol-value dep))
6424                     (concat (or (header-xref (car (symbol-value dep))) "")
6425                             (or (header-xref header) "")))
6426                    (setq header nil))
6427                (setcar (symbol-value dep) header))
6428            (set dep (list header))))
6429     (if header
6430         (progn
6431           (if (boundp (setq dep (intern (or ref "none") 
6432                                         dependencies)))
6433               (setcdr (symbol-value dep) 
6434                       (cons header (cdr (symbol-value dep))))
6435             (set dep (list nil header)))))
6436     header))
6437
6438 (defun gnus-article-get-xrefs ()
6439   "Fill in the Xref value in `gnus-current-headers', if necessary.
6440 This is meant to be called in `gnus-article-internal-prepare-hook'."
6441   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6442                                  gnus-current-headers)))
6443     (or (not gnus-use-cross-reference)
6444         (and (header-xref headers)
6445              (not (string= (header-xref headers) "")))
6446         (let ((case-fold-search t)
6447               xref)
6448           (save-restriction
6449             (gnus-narrow-to-headers)
6450             (goto-char (point-min))
6451             (if (or (and (eq (downcase (following-char)) ?x)
6452                          (looking-at "Xref:"))
6453                     (search-forward "\nXref:" nil t))
6454                 (progn
6455                   (goto-char (1+ (match-end 0)))
6456                   (setq xref (buffer-substring (point) 
6457                                                (progn (end-of-line) (point))))
6458                   (header-set-xref headers xref))))))))
6459
6460 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6461 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6462
6463 ;; Return a header specified by a NUMBER.
6464 (defun gnus-get-header-by-number (number)
6465   (save-excursion
6466     (set-buffer gnus-summary-buffer)
6467     (or gnus-newsgroup-headers-hashtb-by-number
6468         (gnus-make-headers-hashtable-by-number))
6469     (gnus-gethash (int-to-string number)
6470                   gnus-newsgroup-headers-hashtb-by-number)))
6471
6472 (defun gnus-make-headers-hashtable-by-number ()
6473   "Make hashtable for the variable gnus-newsgroup-headers by number."
6474   (save-excursion
6475     (set-buffer gnus-summary-buffer)
6476     (let ((headers gnus-newsgroup-headers)
6477           header)
6478       (setq gnus-newsgroup-headers-hashtb-by-number
6479             (gnus-make-hashtable (length headers)))
6480       (while headers
6481         (setq header (car headers))
6482         (gnus-sethash (int-to-string (header-number header))
6483                       header gnus-newsgroup-headers-hashtb-by-number)
6484         (setq headers (cdr headers))))))
6485
6486 (defun gnus-more-header-backward ()
6487   "Find new header backward."
6488   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6489         (artnum gnus-newsgroup-begin)
6490         (header nil))
6491     (while (and (not header)
6492                 (> artnum first))
6493       (setq artnum (1- artnum))
6494       (setq header (gnus-read-header artnum)))
6495     header))
6496
6497 (defun gnus-more-header-forward (&optional backward)
6498   "Find new header forward.
6499 If BACKWARD, find new header backward instead."
6500   (if backward
6501       (gnus-more-header-backward)
6502     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6503           (artnum gnus-newsgroup-end)
6504           (header nil))
6505       (while (and (not header)
6506                   (< artnum last))
6507         (setq artnum (1+ artnum))
6508         (setq header (gnus-read-header artnum)))
6509       header)))
6510
6511 (defun gnus-extend-newsgroup (header &optional backward)
6512   "Extend newsgroup selection with HEADER.
6513 Optional argument BACKWARD means extend toward backward."
6514   (if header
6515       (let ((artnum (header-number header)))
6516         (setq gnus-newsgroup-headers
6517               (if backward
6518                   (cons header gnus-newsgroup-headers)
6519                 (nconc gnus-newsgroup-headers (list header))))
6520         (setq gnus-newsgroup-unselected
6521               (delq artnum gnus-newsgroup-unselected))
6522         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6523         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6524
6525 (defun gnus-summary-work-articles (n)
6526   "Return a list of articles to be worked upon. The prefix argument,
6527 the list of process marked articles, and the current article will be
6528 taken into consideration."
6529   (let (articles)
6530     (if (and n (numberp n))
6531         (let ((backward (< n 0))
6532               (n (abs n)))
6533           (save-excursion
6534             (while (and (> n 0)
6535                         (setq articles (cons (gnus-summary-article-number) 
6536                                              articles))
6537                         (gnus-summary-search-forward nil nil backward))
6538               (setq n (1- n))))
6539           (sort articles (function <)))
6540       (or (reverse gnus-newsgroup-processable)
6541           (list (gnus-summary-article-number))))))
6542
6543 (defun gnus-summary-search-group (&optional backward use-level)
6544   "Search for next unread newsgroup.
6545 If optional argument BACKWARD is non-nil, search backward instead."
6546   (save-excursion
6547     (set-buffer gnus-group-buffer)
6548     (save-excursion
6549       ;; We don't want to alter current point of group mode buffer.
6550       (if (gnus-group-search-forward 
6551            backward nil
6552            (if use-level (gnus-group-group-level) nil))
6553           (gnus-group-group-name)))))
6554
6555 (defun gnus-summary-best-group (&optional exclude-group)
6556   "Find the name of the best unread group.
6557 If EXCLUDE-GROUP, do not go to this group."
6558   (save-excursion
6559     (set-buffer gnus-group-buffer)
6560     (save-excursion
6561       (gnus-group-best-unread-group exclude-group))))
6562
6563 (defun gnus-summary-search-subject (&optional backward unread subject)
6564   "Search for article forward.
6565 If BACKWARD is non-nil, search backward.
6566 If UNREAD is non-nil, only unread articles are selected.
6567 If SUBJECT is non-nil, the article which has the same subject will be
6568 searched for." 
6569   (let ((func (if backward 'previous-single-property-change
6570                 'next-single-property-change))
6571         (beg (point))
6572         (did t)
6573         pos)
6574     (beginning-of-line)
6575     (and gnus-summary-check-current unread
6576          (eq (nth 1 (get-text-property (point) 'gnus)) gnus-unread-mark)
6577          (setq did nil))
6578     (if (not did)
6579         ()
6580       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6581       (while
6582           (and 
6583            (setq pos (funcall func (point) 'gnus))
6584            (goto-char (if backward (1- pos) pos))
6585            (setq did
6586                  (not (and
6587                        (or (not unread)
6588                            (eq (nth 1 (get-text-property (point) 'gnus))
6589                                gnus-unread-mark))
6590                        (or (not subject)
6591                            (equal (gnus-simplify-subject-re subject)
6592                                   (gnus-simplify-subject-re
6593                                    (gnus-summary-subject-string)))))))
6594            (if backward (if (bobp) nil (forward-char -1) t)
6595              (if (eobp) nil (forward-char 1) t)))))
6596     (if did
6597         (progn (goto-char beg) nil)
6598       (prog1
6599           (car (get-text-property (point) 'gnus))
6600         (gnus-summary-position-cursor)))))
6601
6602 (defun gnus-summary-search-forward (&optional unread subject backward)
6603   "Search for article forward.
6604 If UNREAD is non-nil, only unread articles are selected.
6605 If SUBJECT is non-nil, the article which has the same subject will be
6606 searched for. 
6607 If BACKWARD is non-nil, the search will be performed backwards instead."
6608   (gnus-summary-search-subject backward unread subject))
6609
6610 (defun gnus-summary-search-backward (&optional unread subject)
6611   "Search for article backward.
6612 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6613 If 2nd optional argument SUBJECT is non-nil, the article which has
6614 the same subject will be searched for."
6615   (gnus-summary-search-forward unread subject t))
6616
6617 (defun gnus-summary-article-number (&optional number-or-nil)
6618   "The article number of the article on the current line.
6619 If there isn's an article number here, then we return the current
6620 article number."
6621   (let* ((p (point))
6622          (number (car (get-text-property 
6623                        (progn (beginning-of-line) 
6624                               (prog1 (point) (goto-char p)))
6625                        'gnus))))
6626     (if number-or-nil number (or number gnus-current-article))))
6627
6628 (defun gnus-summary-thread-level ()
6629   "The thread level of the article on the current line."
6630   (or (nth 2 (get-text-property (gnus-point-at-bol) 'gnus))
6631       0))
6632
6633 (defun gnus-summary-pseudo-article ()
6634   "The thread level of the article on the current line."
6635   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
6636
6637 (defun gnus-summary-article-mark ()
6638   "The mark on the current line."
6639   (nth 1 (get-text-property (gnus-point-at-bol) 'gnus)))
6640
6641 (defun gnus-summary-subject-string ()
6642   "Return current subject string or nil if nothing."
6643   (let ((article (gnus-summary-article-number))
6644         header)
6645     (and article 
6646          (setq header (gnus-get-header-by-number article))
6647          (vectorp header)
6648          (header-subject header))))
6649
6650 (defalias 'gnus-summary-score 'gnus-summary-article-score)
6651 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
6652 (defun gnus-summary-article-score ()
6653   "Return current article score."
6654   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
6655       gnus-summary-default-score 0))
6656
6657 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
6658
6659 (defun gnus-summary-recenter ()
6660   "Center point in the summary window.
6661 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6662 displayed, no centering will be performed." 
6663   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6664   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6665   (let* ((top (cond ((< (window-height) 4) 0)
6666                     ((< (window-height) 6) 1)
6667                     (t 2)))
6668          (height (1- (window-height)))
6669          (bottom (save-excursion (goto-char (point-max))
6670                                  (forward-line (- height))
6671                                  (point)))
6672          (window (get-buffer-window (current-buffer))))
6673     (and 
6674      ;; The user has to want it,
6675      gnus-auto-center-summary 
6676      ;; the article buffer must be displayed,
6677      (get-buffer-window gnus-article-buffer)
6678      ;; Set the window start to either `bottom', which is the biggest
6679      ;; possible valid number, or the second line from the top,
6680      ;; whichever is the least.
6681      (set-window-start
6682       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
6683
6684 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
6685 (defun gnus-short-group-name (group &optional levels)
6686   "Collapse GROUP name LEVELS."
6687   (let ((name "") (foreign "")
6688         (levels (or levels 2)))
6689     (if (string-match ":" group)
6690         (setq foreign (substring group 0 (match-end 0))
6691               group (substring group (match-end 0))))
6692     (while group
6693       (if (and (string-match "\\." group) (> levels 0))
6694           (setq name (concat name (substring group 0 1))
6695                 group (substring group (match-end 0))
6696                 levels (- levels 1)
6697                 name (concat name "."))
6698         (setq name (concat foreign name group)
6699               group nil)))
6700     name))
6701
6702 (defun gnus-summary-jump-to-group (newsgroup)
6703   "Move point to NEWSGROUP in group mode buffer."
6704   ;; Keep update point of group mode buffer if visible.
6705   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6706       (save-window-excursion
6707         ;; Take care of tree window mode.
6708         (if (get-buffer-window gnus-group-buffer)
6709             (pop-to-buffer gnus-group-buffer))
6710         (gnus-group-jump-to-group newsgroup))
6711     (save-excursion
6712       ;; Take care of tree window mode.
6713       (if (get-buffer-window gnus-group-buffer)
6714           (pop-to-buffer gnus-group-buffer)
6715         (set-buffer gnus-group-buffer))
6716       (gnus-group-jump-to-group newsgroup))))
6717
6718 ;; This function returns a list of article numbers based on the
6719 ;; difference between the ranges of read articles in this group and
6720 ;; the range of active articles.
6721 (defun gnus-list-of-unread-articles (group)
6722   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6723          (active (gnus-gethash group gnus-active-hashtb))
6724          (last (cdr active))
6725          unread first nlast unread)
6726     ;; If none are read, then all are unread. 
6727     (if (not read)
6728         (setq first (car active))
6729       ;; If the range of read articles is a single range, then the
6730       ;; first unread article is the article after the last read
6731       ;; article. Sounds logical, doesn't it?
6732       (if (not (listp (cdr read)))
6733           (setq first (1+ (cdr read)))
6734         ;; `read' is a list of ranges.
6735         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
6736                                 (car (car read)))) 1)
6737             (setq first 1))
6738         (while read
6739           (if first 
6740               (while (< first nlast)
6741                 (setq unread (cons first unread))
6742                 (setq first (1+ first))))
6743           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
6744           (setq nlast (if (atom (car (cdr read))) 
6745                           (car (cdr read))
6746                         (car (car (cdr read)))))
6747           (setq read (cdr read)))))
6748     ;; And add the last unread articles.
6749     (while (<= first last)
6750       (setq unread (cons first unread))
6751       (setq first (1+ first)))
6752     ;; Return the list of unread articles.
6753     (nreverse unread)))
6754
6755 (defun gnus-list-of-read-articles (group)
6756   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
6757         (active (gnus-gethash group gnus-active-hashtb)))
6758     (and info active
6759          (gnus-sorted-complement 
6760           (gnus-uncompress-range active) 
6761           (gnus-list-of-unread-articles (nth 2 info))))))
6762
6763 ;; Various summary commands
6764
6765 (defun gnus-summary-universal-argument ()
6766   "Perform any operation on all articles marked with the process mark."
6767   (interactive)
6768   (gnus-set-global-variables)
6769   (let ((articles (reverse gnus-newsgroup-processable))
6770         key func)
6771     (or articles (error "No articles marked"))
6772     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
6773         (error "Undefined key"))
6774     (while articles
6775       (gnus-summary-goto-subject (car articles))
6776       (command-execute func)
6777       (gnus-summary-remove-process-mark (car articles))
6778       (setq articles (cdr articles)))))
6779
6780 (defun gnus-summary-toggle-truncation (arg)
6781   "Toggle truncation of summary lines.
6782 With arg, turn line truncation on iff arg is positive."
6783   (interactive "P")
6784   (setq truncate-lines
6785         (if (null arg) (not truncate-lines)
6786           (> (prefix-numeric-value arg) 0)))
6787   (redraw-display))
6788
6789 (defun gnus-summary-reselect-current-group (all)
6790   "Once exit and then reselect the current newsgroup.
6791 The prefix argument ALL means to select all articles."
6792   (interactive "P")
6793   (gnus-set-global-variables)
6794   (let ((current-subject (gnus-summary-article-number))
6795         (group gnus-newsgroup-name))
6796     (setq gnus-newsgroup-threads nil)
6797     (gnus-summary-exit t)
6798     ;; We have to adjust the point of group mode buffer because the
6799     ;; current point was moved to the next unread newsgroup by
6800     ;; exiting.
6801     (gnus-summary-jump-to-group group)
6802     (gnus-group-read-group all t)
6803     (gnus-summary-goto-subject current-subject)))
6804
6805 (defun gnus-summary-rescan-group (all)
6806   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6807   (interactive "P")
6808   (gnus-set-global-variables)
6809   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
6810   (let ((group gnus-newsgroup-name))
6811     (gnus-summary-exit)
6812     (gnus-summary-jump-to-group group)
6813     (save-excursion
6814       (set-buffer gnus-group-buffer)
6815       (gnus-group-get-new-news-this-group 1))
6816     (gnus-summary-jump-to-group group)
6817     (gnus-group-read-group all)))
6818
6819 (defun gnus-summary-update-info ()
6820   (let* ((group gnus-newsgroup-name)
6821          (method (car (gnus-find-method-for-group group))))
6822     (if gnus-newsgroup-kill-headers
6823         (setq gnus-newsgroup-killed
6824               (gnus-compress-sequence
6825                (nconc
6826                 (gnus-set-sorted-intersection
6827                  (gnus-uncompress-range gnus-newsgroup-killed)
6828                  (setq gnus-newsgroup-unselected
6829                        (sort gnus-newsgroup-unselected '<)))
6830                 (setq gnus-newsgroup-unreads
6831                       (sort gnus-newsgroup-unreads '<))) t)))
6832     (or (listp (cdr gnus-newsgroup-killed))
6833         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6834     (let ((updated nil)
6835           (headers gnus-newsgroup-headers))
6836       (gnus-close-group group)
6837       (run-hooks 'gnus-exit-group-hook)
6838       (gnus-update-read-articles 
6839        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
6840        gnus-newsgroup-marked
6841        t gnus-newsgroup-replied gnus-newsgroup-expirable
6842        gnus-newsgroup-killed gnus-newsgroup-dormant
6843        gnus-newsgroup-bookmarks 
6844        (and gnus-save-score gnus-newsgroup-scored))
6845       (and gnus-use-cross-reference
6846            (gnus-mark-xrefs-as-read 
6847             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
6848       ;; Do adaptive scoring, and possibly save score files.
6849       (and gnus-newsgroup-adaptive
6850            (gnus-score-adaptive))
6851       (and (fboundp 'gnus-score-save)
6852            (funcall 'gnus-score-save))
6853       ;; Do not switch windows but change the buffer to work.
6854       (set-buffer gnus-group-buffer)
6855       (or (eq 'nndigest method)
6856           (gnus-group-update-group group)))))
6857   
6858 (defun gnus-summary-exit (&optional temporary)
6859   "Exit reading current newsgroup, and then return to group selection mode.
6860 gnus-exit-group-hook is called with no arguments if that value is non-nil."
6861   (interactive)
6862   (gnus-set-global-variables)
6863   (gnus-kill-save-kill-buffer)
6864   (let* ((group gnus-newsgroup-name)
6865          (quit-buffer (cdr (assoc 'quit-buffer (gnus-find-method-for-group
6866                                                 gnus-newsgroup-name))))
6867          (mode major-mode)
6868          (method (car (gnus-find-method-for-group group)))
6869          (buf (current-buffer)))
6870     (gnus-summary-update-info) ; Make all changes in this group permanent.
6871     ;; Make sure where I was, and go to next newsgroup.
6872     (if (eq method 'nndigest)
6873         ()
6874       (gnus-group-jump-to-group group)
6875       (gnus-group-next-unread-group 1))
6876     (if temporary
6877         nil                             ;Nothing to do.
6878       ;; We set all buffer-local variables to nil. It is unclear why
6879       ;; this is needed, but if we don't, buffer-local variables are
6880       ;; not garbage-collected, it seems. This would the lead to en
6881       ;; ever-growing Emacs.
6882       (set-buffer buf)
6883       (gnus-summary-clear-local-variables)
6884       ;; We clear the global counterparts of the buffer-local
6885       ;; variables as well, just to be on the safe side.
6886       (gnus-configure-windows 'group)
6887       (gnus-summary-clear-local-variables)
6888       ;; Return to group mode buffer. 
6889       (if (eq mode 'gnus-summary-mode)
6890           (gnus-kill-buffer buf))
6891       (if (get-buffer gnus-article-buffer)
6892           (bury-buffer gnus-article-buffer))
6893       (setq gnus-current-select-method gnus-select-method)
6894       (pop-to-buffer gnus-group-buffer)
6895       (if (eq method 'nndigest)
6896           ()
6897         (gnus-group-jump-to-group group)
6898         (gnus-group-next-unread-group 1))
6899       (if (gnus-buffer-exists-p quit-buffer)
6900           (progn
6901             (switch-to-buffer quit-buffer)
6902             (gnus-set-global-variables)
6903             (gnus-configure-windows 'summary))))))
6904
6905 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6906 (defun gnus-summary-exit-no-update (&optional no-questions)
6907   "Quit reading current newsgroup without updating read article info."
6908   (interactive)
6909   (let* ((group gnus-newsgroup-name)
6910          (quit-buffer (cdr (assoc 'quit-buffer 
6911                                   (gnus-find-method-for-group group)))))
6912     (if (or no-questions
6913             gnus-expert-user
6914             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
6915         (progn
6916           (gnus-close-group group)
6917           (gnus-summary-clear-local-variables)
6918           (set-buffer gnus-group-buffer)
6919           (gnus-summary-clear-local-variables)
6920           ;; Return to group selection mode.
6921           (gnus-configure-windows 'group)
6922           (if (get-buffer gnus-summary-buffer)
6923               (kill-buffer gnus-summary-buffer))
6924           (if (get-buffer gnus-article-buffer)
6925               (bury-buffer gnus-article-buffer))
6926           (if (equal (gnus-group-group-name) group)
6927               (gnus-group-next-unread-group 1))
6928           (if (gnus-buffer-exists-p quit-buffer)
6929               (progn
6930                 (switch-to-buffer quit-buffer)
6931                 (gnus-configure-windows 'summary)))))))
6932
6933 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6934 (defun gnus-summary-fetch-faq (group)
6935   "Fetch the FAQ for the current group."
6936   (interactive (list gnus-newsgroup-name))
6937   (gnus-configure-windows 'summary-faq)
6938   (find-file (concat gnus-group-faq-directory group)))
6939
6940 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6941 (defun gnus-summary-describe-group (force)
6942   "Describe the current newsgroup."
6943   (interactive "P")
6944   (gnus-group-describe-group force gnus-newsgroup-name))
6945
6946 (defun gnus-summary-describe-briefly ()
6947   "Describe summary mode commands briefly."
6948   (interactive)
6949   (gnus-message 6
6950     (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")))
6951
6952 ;; Walking around group mode buffer from summary mode.
6953
6954 (defun gnus-summary-next-group (&optional no-article group backward)
6955   "Exit current newsgroup and then select next unread newsgroup.
6956 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
6957 If BACKWARD, go to previous group instead."
6958   (interactive "P")
6959   (gnus-set-global-variables)
6960   (let ((ingroup gnus-newsgroup-name)
6961         (sumbuf (current-buffer))
6962         num)
6963     (gnus-summary-exit t)               ;Update all information.
6964     (if (and group
6965              (or (and (numberp (setq num (car (gnus-gethash
6966                                                group gnus-newsrc-hashtb))))
6967                       (< num 1))
6968                  (null num)))
6969         (progn
6970           (gnus-group-jump-to-group group)
6971           (setq group nil))
6972       (gnus-group-jump-to-group ingroup))
6973     (gnus-summary-search-group backward)
6974     (let ((group (or group (gnus-summary-search-group backward)))
6975           (buf gnus-summary-buffer))
6976       (if (null group)
6977           (gnus-summary-exit-no-update t)
6978         (gnus-message 5 "Selecting %s..." group)
6979         ;; We are now in group mode buffer.
6980         ;; Make sure group mode buffer point is on GROUP.
6981         (gnus-group-jump-to-group group)
6982         (if (not (eq gnus-auto-select-next 'quietly))
6983             (progn
6984               (gnus-summary-read-group group nil no-article buf)
6985               (and (string= gnus-newsgroup-name ingroup)
6986                    (bufferp sumbuf) (buffer-name sumbuf)
6987                    (progn
6988                      (set-buffer (setq gnus-summary-buffer sumbuf))
6989                      (gnus-summary-exit-no-update t))))
6990           (gnus-summary-read-group group nil no-article buf)
6991           (while (and (string= gnus-newsgroup-name ingroup)
6992                       (bufferp sumbuf) (buffer-name sumbuf))
6993             (set-buffer gnus-group-buffer)
6994             (gnus-summary-read-group 
6995              (gnus-group-group-name) nil no-article buf)))))))
6996
6997 (defun gnus-summary-prev-group (no-article)
6998   "Exit current newsgroup and then select previous unread newsgroup.
6999 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7000   (interactive "P")
7001   (gnus-summary-next-group no-article nil t))
7002
7003 ;; Walking around summary lines.
7004
7005 (defun gnus-summary-first-subject (unread)
7006   "Go to the first unread subject.
7007 If UNREAD is non-nil, go to the first unread article.
7008 Returns nil if there are no unread articles."
7009   (interactive "P")
7010   (let ((begin (point)))
7011     (goto-char (point-min))
7012     (if (not unread)
7013         t
7014       (while (and (not (eq (nth 1 (get-text-property (point) 'gnus))
7015                            gnus-unread-mark))
7016                   (zerop (forward-line 1))))
7017       (prog1
7018           (if (not (eobp))
7019               t
7020             ;; If there is no unread articles, stay where you are.
7021             (goto-char begin)
7022             (gnus-message 3 "No more unread articles")
7023             nil)
7024         (gnus-summary-position-cursor)))))
7025
7026 (defun gnus-summary-next-subject (n &optional unread dont-display)
7027   "Go to next N'th summary line.
7028 If N is negative, go to the previous N'th subject line.
7029 If UNREAD is non-nil, only unread articles are selected.
7030 The difference between N and the actual number of steps taken is
7031 returned."
7032   (interactive "p")
7033   (let ((backward (< n 0))
7034         (n (abs n)))
7035     (while (and (> n 0)
7036                 (gnus-summary-search-forward unread nil backward))
7037       (setq n (1- n)))
7038     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7039                                (if unread " unread" "")))
7040     (or dont-display
7041         (progn
7042           (gnus-summary-recenter)
7043           (gnus-summary-position-cursor)))
7044   n))
7045
7046 (defun gnus-summary-next-unread-subject (n)
7047   "Go to next N'th unread summary line."
7048   (interactive "p")
7049   (gnus-summary-next-subject n t))
7050
7051 (defun gnus-summary-prev-subject (n &optional unread)
7052   "Go to previous N'th summary line.
7053 If optional argument UNREAD is non-nil, only unread article is selected."
7054   (interactive "p")
7055   (gnus-summary-next-subject (- n) unread))
7056
7057 (defun gnus-summary-prev-unread-subject (n)
7058   "Go to previous N'th unread summary line."
7059   (interactive "p")
7060   (gnus-summary-next-subject (- n) t))
7061
7062 (defun gnus-summary-goto-subject (article)
7063   "Go the subject line of ARTICLE."
7064   (interactive
7065    (list
7066     (string-to-int
7067      (completing-read "Article number: "
7068                       (mapcar
7069                        (lambda (headers)
7070                          (list
7071                           (int-to-string (header-number headers))))
7072                        gnus-newsgroup-headers)
7073                       nil 'require-match))))
7074   (or article (error "No article number"))
7075   (let ((b (point)))
7076     (goto-char (point-min))
7077     (while (and (not (eq (car (get-text-property (point) 'gnus)) article))
7078                 (zerop (forward-line 1))))
7079     ;; Skip dummy articles. 
7080     (if (eq (gnus-summary-article-mark) ?Z)
7081         (forward-line 1))
7082     (prog1
7083         (if (not (eobp))
7084             article
7085           (goto-char b)
7086           nil)
7087       (gnus-summary-position-cursor))))
7088
7089 ;; Walking around summary lines with displaying articles.
7090
7091 (defun gnus-summary-expand-window ()
7092   "Expand summary window to show headers full window."
7093   (interactive)
7094   (gnus-set-global-variables)
7095   (gnus-configure-windows 'summary))
7096
7097 (defun gnus-summary-display-article (article &optional all-header)
7098   "Display ARTICLE in article buffer."
7099   (gnus-set-global-variables)
7100   (if (null article)
7101       nil
7102     (gnus-article-prepare article all-header)
7103     (if (eq (gnus-summary-article-mark) ?Z)
7104         (progn
7105           (forward-line 1)
7106           (gnus-summary-position-cursor)))
7107     (run-hooks 'gnus-select-article-hook)
7108     (gnus-summary-recenter)
7109 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
7110 ;    (sit-for 0)
7111     (gnus-summary-goto-subject article)
7112     ;; Successfully display article.
7113     (gnus-summary-update-line)
7114     (let ((bookmark (cdr (assq article gnus-newsgroup-bookmarks))))
7115       (set-window-start 
7116        (get-buffer-window gnus-article-buffer)
7117        (save-excursion
7118          (set-buffer gnus-article-buffer)
7119          (goto-char (point-min))
7120          (cond (bookmark
7121                 (gnus-message 6 "Moved to bookmark")
7122                 (search-forward "\n\n" nil t)
7123                 (forward-line bookmark)
7124                 (point))
7125                (t
7126                 (point-min))))))
7127     t))
7128
7129 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7130   "Select the current article.
7131 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7132 non-nil, the article will be re-fetched even if it already present in
7133 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7134 be displayed."
7135   (and (not pseudo) (gnus-summary-pseudo-article)
7136        (error "This is a pseudo-article."))
7137   (let ((article (or article (gnus-summary-article-number)))
7138         (all-headers (not (not all-headers))) ;Must be T or NIL.
7139         did) 
7140     (prog1
7141         (save-excursion
7142           (set-buffer gnus-summary-buffer)
7143           (if (or (null gnus-current-article)
7144                   (null gnus-article-current)
7145                   (not (eq article (cdr gnus-article-current)))
7146                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7147                   force)
7148               ;; The requested article is different from the current article.
7149               (progn
7150                 (gnus-summary-display-article article all-headers)
7151                 (gnus-configure-windows 'article)
7152                 (setq did article))
7153             (if all-headers (gnus-article-show-all-headers))
7154             (gnus-configure-windows 'article)
7155             nil))
7156       (if (not did)
7157           ()
7158         (let ((bookmark (cdr (assq article gnus-newsgroup-bookmarks))))
7159           (set-window-start 
7160            (get-buffer-window gnus-article-buffer)
7161            (save-excursion
7162              (set-buffer gnus-article-buffer)
7163              (goto-char (point-min))
7164              (cond (bookmark
7165                     (gnus-message 6 "Moved to bookmark")
7166                     (search-forward "\n\n" nil t)
7167                     (forward-line bookmark)
7168                     (point))
7169                    (t
7170                     (point-min))))))))))
7171
7172 (defun gnus-summary-set-current-mark (&optional current-mark)
7173   "Obsolete function."
7174   nil)
7175
7176 (defun gnus-summary-next-article (unread &optional subject backward)
7177   "Select the next article.
7178 If UNREAD, only unread articles are selected.
7179 If SUBJECT, only articles with SUBJECT are selected.
7180 If BACKWARD, the previous article is selected instead of the next."
7181   (interactive "P")
7182   (let ((opoint (point))
7183         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
7184         header)
7185     (cond
7186      ;; Is there such an article?
7187      ((gnus-summary-display-article 
7188        (gnus-summary-search-forward unread subject backward))
7189       (gnus-summary-position-cursor))
7190      ;; If not, we try the first unread, if that is wanted.
7191      ((and subject
7192            gnus-auto-select-same
7193            (gnus-summary-first-unread-article))
7194       (gnus-message 6 "Wrapped"))
7195      ;; Try to get next/previous article not displayed in this group.
7196      ((and gnus-auto-extend-newsgroup
7197            (not unread) (not subject)
7198            (setq header (gnus-more-header-forward backward)))
7199       (gnus-extend-newsgroup header backward)
7200       (let ((buffer-read-only nil))
7201         (goto-char (if backward (point-min) (point-max)))
7202         (gnus-summary-prepare-threads (list header) 0))
7203       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7204                                    gnus-newsgroup-end)))
7205      ;; Go to next/previous group.
7206      (t
7207       (or (eq method 'nndigest)
7208           (gnus-summary-jump-to-group gnus-newsgroup-name))
7209       (let ((cmd (aref (this-command-keys) 0))
7210             (group 
7211              (if (eq gnus-keep-same-level 'best) 
7212                  (gnus-summary-best-group gnus-newsgroup-name)
7213                (gnus-summary-search-group backward gnus-keep-same-level))))
7214         ;; Keep just the event type of CMD.
7215         (and (listp cmd) (setq cmd (car cmd)))
7216         ;; Select next unread newsgroup automagically.
7217         (cond 
7218          ((not gnus-auto-select-next)
7219           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7220          ((eq gnus-auto-select-next 'quietly)
7221           ;; Select quietly.
7222           (if (eq method 'nndigest)
7223               (gnus-summary-exit)
7224             (gnus-message 7 "No more%s articles (%s)..."
7225                           (if unread " unread" "") 
7226                           (if group (concat "selecting " group)
7227                             "exiting"))
7228             (gnus-summary-next-group nil group backward)))
7229          (t
7230           (let ((keystrokes '(?\C-n ?\C-p))
7231                 key)
7232             (while (or (null key) (memq key keystrokes))
7233               (gnus-message 
7234                7 "No more%s articles%s" (if unread " unread" "")
7235                (if (and group (not (eq method 'nndigest)))
7236                    (format " (Type %s for %s [%s])"
7237                            (single-key-description cmd) group
7238                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7239                  (format " (Type %s to exit %s)"
7240                          (single-key-description cmd)
7241                          gnus-newsgroup-name)))
7242               ;; Confirm auto selection.
7243               (let* ((event (read-event)))
7244                 (setq key (if (listp event) (car event) event))
7245                 (if (member key keystrokes)
7246                     (let ((obuf (current-buffer)))
7247                       (switch-to-buffer gnus-group-buffer)
7248                       (gnus-group-jump-to-group group)
7249                       (execute-kbd-macro (char-to-string key))
7250                       (setq group (gnus-group-group-name))
7251                       (switch-to-buffer obuf)))))
7252             (if (eq key cmd)
7253                 (if (or (not group) (eq method 'nndigest))
7254                     (gnus-summary-exit)
7255                   (gnus-summary-next-group nil group backward))
7256               (setq unread-command-events (list key)))))))))))
7257
7258 (defun gnus-summary-next-unread-article ()
7259   "Select unread article after current one."
7260   (interactive)
7261   (gnus-summary-next-article t (and gnus-auto-select-same
7262                                     (gnus-summary-subject-string))))
7263
7264 (defun gnus-summary-prev-article (unread &optional subject)
7265   "Select the article after the current one.
7266 If UNREAD is non-nil, only unread articles are selected."
7267   (interactive "P")
7268   (gnus-summary-next-article unread subject t))
7269
7270 (defun gnus-summary-prev-unread-article ()
7271   "Select unred article before current one."
7272   (interactive)
7273   (gnus-summary-prev-article t (and gnus-auto-select-same
7274                                     (gnus-summary-subject-string))))
7275
7276 (defun gnus-summary-next-page (lines &optional circular)
7277   "Show next page of selected article.
7278 If end of article, select next article.
7279 Argument LINES specifies lines to be scrolled up.
7280 If CIRCULAR is non-nil, go to the start of the article instead of 
7281 instead of selecting the next article when reaching the end of the
7282 current article." 
7283   (interactive "P")
7284   (setq gnus-summary-buffer (current-buffer))
7285   (gnus-set-global-variables)
7286   (let ((article (gnus-summary-article-number))
7287         (endp nil))
7288     (if (or (null gnus-current-article)
7289             (null gnus-article-current)
7290             (/= article (cdr gnus-article-current))
7291             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7292         ;; Selected subject is different from current article's.
7293         (gnus-summary-display-article article)
7294       (gnus-configure-windows 'article)
7295       (gnus-eval-in-buffer-window
7296        gnus-article-buffer
7297        (setq endp (gnus-article-next-page lines)))
7298       (if endp
7299           (cond (circular
7300                  (gnus-summary-beginning-of-article))
7301                 (lines
7302                  (gnus-message 3 "End of message"))
7303                 ((null lines)
7304                  (gnus-summary-next-unread-article)))))))
7305
7306 (defun gnus-summary-prev-page (lines)
7307   "Show previous page of selected article.
7308 Argument LINES specifies lines to be scrolled down."
7309   (interactive "P")
7310   (let ((article (gnus-summary-article-number)))
7311     (if (or (null gnus-current-article)
7312             (null gnus-article-current)
7313             (/= article (cdr gnus-article-current))
7314             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7315         ;; Selected subject is different from current article's.
7316         (gnus-summary-display-article article)
7317       (gnus-configure-windows 'article)
7318       (gnus-eval-in-buffer-window gnus-article-buffer
7319         (gnus-article-prev-page lines))))
7320   (gnus-summary-position-cursor))
7321
7322 (defun gnus-summary-scroll-up (lines)
7323   "Scroll up (or down) one line current article.
7324 Argument LINES specifies lines to be scrolled up (or down if negative)."
7325   (interactive "p")
7326   (or (gnus-summary-select-article nil nil 'pseudo)
7327       (gnus-eval-in-buffer-window 
7328        gnus-article-buffer
7329        (cond ((> lines 0)
7330               (if (gnus-article-next-page lines)
7331                   (gnus-message 3 "End of message")))
7332              ((< lines 0)
7333               (gnus-article-prev-page (- lines))))))
7334   (gnus-summary-position-cursor))
7335
7336 (defun gnus-summary-next-same-subject ()
7337   "Select next article which has the same subject as current one."
7338   (interactive)
7339   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7340
7341 (defun gnus-summary-prev-same-subject ()
7342   "Select previous article which has the same subject as current one."
7343   (interactive)
7344   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7345
7346 (defun gnus-summary-next-unread-same-subject ()
7347   "Select next unread article which has the same subject as current one."
7348   (interactive)
7349   (gnus-summary-next-article t (gnus-summary-subject-string)))
7350
7351 (defun gnus-summary-prev-unread-same-subject ()
7352   "Select previous unread article which has the same subject as current one."
7353   (interactive)
7354   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7355
7356 (defun gnus-summary-first-unread-article ()
7357   "Select the first unread article. 
7358 Return nil if there are no unread articles."
7359   (interactive)
7360   (prog1
7361       (if (gnus-summary-first-subject t)
7362           (gnus-summary-display-article (gnus-summary-article-number)))
7363     (gnus-summary-position-cursor)))
7364
7365 (defun gnus-summary-best-unread-article ()
7366   "Select the unread article with the highest score."
7367   (interactive)
7368   (gnus-set-global-variables)
7369   (let ((scored gnus-newsgroup-scored)
7370         (best -1000000)
7371         article art)
7372     (while scored
7373       (or (> best (cdr (car scored)))
7374           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7375                (not (memq art gnus-newsgroup-marked))
7376                (not (memq art gnus-newsgroup-dormant))
7377                (if (= best (cdr (car scored)))
7378                    (setq article (min art article))
7379                  (setq article art)
7380                  (setq best (cdr (car scored))))))
7381       (setq scored (cdr scored)))
7382     (if article 
7383         (gnus-summary-goto-article article)
7384       (gnus-summary-first-unread-article))
7385     (gnus-summary-position-cursor)))
7386
7387 (defun gnus-summary-goto-article (article &optional all-headers)
7388   "Fetch ARTICLE and display it if it exists.
7389 If ALL-HEADERS is non-nil, no header lines are hidden."
7390   (interactive
7391    (list
7392     (string-to-int
7393      (completing-read 
7394       "Article number: "
7395       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7396               gnus-newsgroup-headers) 
7397       nil 'require-match))))
7398   (prog1
7399       (and (gnus-summary-goto-subject article)
7400            (gnus-summary-display-article article all-headers))
7401     (gnus-summary-position-cursor)))
7402
7403 (defun gnus-summary-goto-last-article ()
7404   "Go to the last article."
7405   (interactive)
7406   (prog1
7407       (and gnus-last-article
7408            (gnus-summary-goto-article gnus-last-article))
7409     (gnus-summary-position-cursor)))
7410
7411 (defun gnus-summary-pop-article (number)
7412   "Pop one article off the history and go to the previous.
7413 NUMBER articles will be popped off."
7414   (interactive "p")
7415   (let (to)
7416     (setq gnus-newsgroup-history
7417           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7418     (if to
7419         (gnus-summary-goto-article (car to))
7420       (error "Article history empty")))
7421   (gnus-summary-position-cursor))
7422
7423 ;; Summary article oriented commands
7424
7425 (defun gnus-summary-refer-parent-article (n)
7426   "Refer parent article N times.
7427 The difference between N and the number of articles fetched is returned."
7428   (interactive "p")
7429   (gnus-set-global-variables)
7430   (while 
7431       (and 
7432        (> n 0)
7433        (let ((ref (header-references (gnus-get-header-by-number
7434                                       (gnus-summary-article-number)))))
7435          (if (and ref (not (equal ref ""))
7436                   (string-match "<[^<>]*>[ \t]*$" ref))
7437              (gnus-summary-refer-article 
7438               (substring ref (match-beginning 0) (match-end 0))))))
7439     (setq n (1- n)))
7440   (or (zerop n) 
7441       (gnus-message 1 "No references in article or expired article."))
7442   (gnus-summary-position-cursor)
7443   n)
7444     
7445 (defun gnus-summary-refer-article (message-id)
7446   "Refer article specified by MESSAGE-ID.
7447 NOTE: This command only works with newsgroup that use NNTP."
7448   (interactive "sMessage-ID: ")
7449   (if (or (not (stringp message-id))
7450           (zerop (length message-id)))
7451       ()
7452     ;; Construct the correct Message-ID if necessary.
7453     ;; Suggested by tale@pawl.rpi.edu.
7454     (or (string-match "^<" message-id)
7455         (setq message-id (concat "<" message-id)))
7456     (or (string-match ">$" message-id)
7457         (setq message-id (concat message-id ">")))
7458     (let ((header (car (gnus-gethash (downcase message-id)
7459                                      gnus-newsgroup-dependencies))))
7460       (if header
7461           (or (gnus-summary-goto-article (header-number header))
7462               ;; The header has been read, but the article had been
7463               ;; expunged, so we insert it again.
7464               (progn
7465                 (gnus-summary-insert-line
7466                  nil header 0 nil gnus-read-mark nil nil
7467                  (header-subject header))
7468                 (forward-line -1)
7469                 (header-number header)))
7470         (let ((gnus-override-method gnus-refer-article-method)
7471               (gnus-ancient-mark gnus-read-mark)
7472               number)
7473           (and gnus-refer-article-method
7474                (or (gnus-server-opened gnus-refer-article-method)
7475                    (gnus-open-server gnus-refer-article-method)))
7476           (if (gnus-article-prepare 
7477                message-id nil (gnus-read-header message-id))
7478               (progn
7479                 (setq number (header-number gnus-current-headers))
7480                 (gnus-rebuild-thread message-id)
7481                 (gnus-summary-goto-subject number)
7482                 message-id)
7483             (gnus-message 1 "No such references")
7484             nil))))))
7485
7486 (defun gnus-summary-enter-digest-group ()
7487   "Enter a digest group based on the current article."
7488   (interactive)
7489   (gnus-set-global-variables)
7490   (gnus-summary-select-article)
7491   ;; We do not want a narrowed article.
7492   (gnus-summary-stop-page-breaking)
7493   (let ((name (format "%s/%d" 
7494                       (gnus-group-prefixed-name 
7495                        gnus-newsgroup-name (list 'nndigest "")) 
7496                       gnus-current-article))
7497         (buf (current-buffer)))
7498     (set-buffer gnus-group-buffer)
7499     (gnus-sethash 
7500      name 
7501      (list t nil (list name gnus-level-default-subscribed nil nil 
7502                        (list 'nndigest gnus-article-buffer
7503                              (cons 'quit-buffer buf))))
7504      gnus-newsrc-hashtb)
7505     (condition-case ()
7506         (gnus-group-read-group t nil name)
7507       (error nil)
7508       (quit nil))
7509     (if (not (equal major-mode 'gnus-group-mode))
7510         ()
7511       (switch-to-buffer buf)
7512       (gnus-set-global-variables)
7513       (gnus-configure-windows 'summary)
7514       (gnus-message 3 "Article not a digest?"))))
7515   
7516 (defun gnus-summary-isearch-article ()
7517   "Do incremental search forward on current article."
7518   (interactive)
7519   (gnus-set-global-variables)
7520   (gnus-summary-select-article)
7521   (gnus-eval-in-buffer-window gnus-article-buffer
7522                               (isearch-forward)))
7523
7524 (defun gnus-summary-search-article-forward (regexp)
7525   "Search for an article containing REGEXP forward.
7526 gnus-select-article-hook is not called during the search."
7527   (interactive
7528    (list (read-string
7529           (concat "Search forward (regexp): "
7530                   (if gnus-last-search-regexp
7531                       (concat "(default " gnus-last-search-regexp ") "))))))
7532   (gnus-set-global-variables)
7533   (if (string-equal regexp "")
7534       (setq regexp (or gnus-last-search-regexp ""))
7535     (setq gnus-last-search-regexp regexp))
7536   (if (gnus-summary-search-article regexp nil)
7537       (gnus-eval-in-buffer-window 
7538        gnus-article-buffer
7539        (recenter 0))
7540     (error "Search failed: \"%s\"" regexp)))
7541
7542 (defun gnus-summary-search-article-backward (regexp)
7543   "Search for an article containing REGEXP backward.
7544 gnus-select-article-hook is not called during the search."
7545   (interactive
7546    (list (read-string
7547           (concat "Search backward (regexp): "
7548                   (if gnus-last-search-regexp
7549                       (concat "(default " gnus-last-search-regexp ") "))))))
7550   (gnus-set-global-variables)
7551   (if (string-equal regexp "")
7552       (setq regexp (or gnus-last-search-regexp ""))
7553     (setq gnus-last-search-regexp regexp))
7554   (if (gnus-summary-search-article regexp t)
7555       (gnus-eval-in-buffer-window
7556        gnus-article-buffer
7557        (recenter 0))
7558     (error "Search failed: \"%s\"" regexp)))
7559
7560 (defun gnus-summary-search-article (regexp &optional backward)
7561   "Search for an article containing REGEXP.
7562 Optional argument BACKWARD means do search for backward.
7563 gnus-select-article-hook is not called during the search."
7564   (let ((gnus-select-article-hook nil)  ;Disable hook.
7565         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7566         (re-search
7567          (if backward
7568              (function re-search-backward) (function re-search-forward)))
7569         (found nil)
7570         (last nil))
7571     ;; Hidden thread subtrees must be searched for ,too.
7572     (gnus-summary-show-all-threads)
7573     (if (eobp) (forward-line -1))
7574     ;; First of all, search current article.
7575     ;; We don't want to read article again from NNTP server nor reset
7576     ;; current point.
7577     (gnus-summary-select-article)
7578     (gnus-message 9 "Searching article: %d..." gnus-current-article)
7579     (setq last gnus-current-article)
7580     (gnus-eval-in-buffer-window gnus-article-buffer
7581       (save-restriction
7582         (widen)
7583         ;; Begin search from current point.
7584         (setq found (funcall re-search regexp nil t))))
7585     ;; Then search next articles.
7586     (while (and (not found)
7587                 (gnus-summary-display-article 
7588                  (gnus-summary-search-subject backward nil nil)))
7589       (gnus-message 9 "Searching article: %d..." gnus-current-article)
7590       (gnus-eval-in-buffer-window gnus-article-buffer
7591         (save-restriction
7592           (widen)
7593           (goto-char (if backward (point-max) (point-min)))
7594           (setq found (funcall re-search regexp nil t)))))
7595     (message "")
7596     ;; Adjust article pointer.
7597     (or (eq last gnus-current-article)
7598         (setq gnus-last-article last))
7599     ;; Return T if found such article.
7600     found))
7601
7602 (defun gnus-summary-execute-command (header regexp command &optional backward)
7603   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7604 If HEADER is an empty string (or nil), the match is done on the entire
7605 article. If BACKWARD (the prefix) is non-nil, search backward instead."
7606   (interactive
7607    (list (let ((completion-ignore-case t))
7608            (completing-read 
7609             "Header name: "
7610             (mapcar (lambda (string) (list string))
7611                     '("Number" "Subject" "From" "Lines" "Date"
7612                       "Message-ID" "Xref" "References"))
7613             nil 'require-match))
7614          (read-string "Regexp: ")
7615          (read-key-sequence "Command: ")
7616          current-prefix-arg))
7617   (gnus-set-global-variables)
7618   ;; Hidden thread subtrees must be searched as well.
7619   (gnus-summary-show-all-threads)
7620   ;; We don't want to change current point nor window configuration.
7621   (save-excursion
7622     (save-window-excursion
7623       (gnus-message 6 "Executing %s..." (key-description command))
7624       ;; We'd like to execute COMMAND interactively so as to give arguments.
7625       (gnus-execute header regexp
7626                     (` (lambda ()
7627                          (call-interactively '(, (key-binding command)))))
7628                     backward)
7629       (gnus-message 6 "Executing %s... done" (key-description command)))))
7630
7631 (defun gnus-summary-beginning-of-article ()
7632   "Scroll the article back to the beginning."
7633   (interactive)
7634   (gnus-set-global-variables)
7635   (gnus-summary-select-article)
7636   (gnus-eval-in-buffer-window
7637    gnus-article-buffer
7638    (widen)
7639    (goto-char (point-min))
7640    (and gnus-break-pages (gnus-narrow-to-page))))
7641
7642 (defun gnus-summary-end-of-article ()
7643   "Scroll to the end of the article."
7644   (interactive)
7645   (gnus-set-global-variables)
7646   (gnus-summary-select-article)
7647   (gnus-eval-in-buffer-window 
7648    gnus-article-buffer
7649    (widen)
7650    (goto-char (point-max))
7651    (and gnus-break-pages (gnus-narrow-to-page))))
7652
7653 (defun gnus-summary-show-article ()
7654   "Force re-fetching of the current article."
7655   (interactive)
7656   (gnus-set-global-variables)
7657   (gnus-summary-select-article gnus-have-all-headers t))
7658
7659 (defun gnus-summary-toggle-header (arg)
7660   "Show the headers if they are hidden, or hide them if they are shown.
7661 If ARG is a positive number, show the entire header.
7662 If ARG is a negative number, hide the unwanted header lines."
7663   (interactive "P")
7664   (gnus-set-global-variables)
7665   (save-excursion
7666     (set-buffer gnus-article-buffer)
7667     (let ((buffer-read-only nil))
7668       (if (numberp arg) 
7669           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
7670                                                 gnus-hidden-properties)
7671             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
7672         (if (text-property-any (point-min) (point-max) 'invisible t)
7673             (remove-text-properties (point-min) (point-max)
7674                                     gnus-hidden-properties)
7675           (let ((gnus-have-all-headers nil))
7676             (run-hooks 'gnus-article-display-hook))))
7677       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
7678
7679 (defun gnus-summary-show-all-headers ()
7680   "Make all header lines visible."
7681   (interactive)
7682   (gnus-set-global-variables)
7683   (gnus-article-show-all-headers))
7684
7685 (defun gnus-summary-toggle-mime (arg)
7686   "Toggle MIME processing.
7687 If ARG is a positive number, turn MIME processing on."
7688   (interactive "P")
7689   (gnus-set-global-variables)
7690   (setq gnus-show-mime
7691         (if (null arg) (not gnus-show-mime)
7692           (> (prefix-numeric-value arg) 0)))
7693   (gnus-summary-select-article t 'force))
7694
7695 (defun gnus-summary-caesar-message (rotnum)
7696   "Caesar rotates all letters of current message by 13/47 places.
7697 With prefix arg, specifies the number of places to rotate each letter forward.
7698 Caesar rotates Japanese letters by 47 places in any case."
7699   (interactive "P")
7700   (gnus-set-global-variables)
7701   (gnus-summary-select-article)
7702   (let ((mail-header-separator "")) ; !!! Is this necessary?
7703     (gnus-overload-functions)
7704     (gnus-eval-in-buffer-window 
7705      gnus-article-buffer
7706      (save-restriction
7707        (widen)
7708        ;; We don't want to jump to the beginning of the message.
7709        ;; `save-excursion' does not do its job.
7710        (move-to-window-line 0)
7711        (let ((last (point)))
7712          (news-caesar-buffer-body rotnum)
7713          (goto-char last)
7714          (recenter 0))))))
7715
7716 (defun gnus-summary-stop-page-breaking ()
7717   "Stop page breaking in the current article."
7718   (interactive)
7719   (gnus-set-global-variables)
7720   (gnus-summary-select-article)
7721   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
7722
7723 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
7724
7725 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
7726   "Move the current article to a different newsgroup.
7727 If N is a positive number, move the N next articles.
7728 If N is a negative number, move the N previous articles.
7729 If N is nil and any articles have been marked with the process mark,
7730 move those articles instead.
7731 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7732 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7733 re-spool using this method.
7734 For this function to work, both the current newsgroup and the
7735 newsgroup that you want to move to have to support the `request-move'
7736 and `request-accept' functions. (Ie. mail newsgroups at present.)"
7737   (interactive "P")
7738   (gnus-set-global-variables)
7739   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
7740       (error "The current newsgroup does not support article moving"))
7741   (let ((articles (gnus-summary-work-articles n))
7742         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7743         art-group)
7744     (if (and (not to-newsgroup) (not select-method))
7745         (setq to-newsgroup
7746               (completing-read 
7747                (format "Where do you want to move %s? %s"
7748                        (if (> (length articles) 1)
7749                            (format "these %d articles" (length articles))
7750                          "this article")
7751                        (if gnus-current-move-group
7752                            (format "(%s default) " gnus-current-move-group)
7753                          ""))
7754                gnus-active-hashtb nil nil prefix)))
7755     (if to-newsgroup
7756         (progn
7757           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7758               (setq to-newsgroup (or gnus-current-move-group "")))
7759           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7760               (gnus-activate-newsgroup to-newsgroup)
7761               (error "No such group: %s" to-newsgroup))
7762           (setq gnus-current-move-group to-newsgroup)))
7763     (or (gnus-check-backend-function 'request-accept-article 
7764                                      (or select-method to-newsgroup))
7765         (error "%s does not support article moving" to-newsgroup))
7766     (gnus-message 6 "Moving to %s: %s..." 
7767                   (or select-method to-newsgroup) articles)
7768     (while articles
7769       (if (setq art-group
7770                 (gnus-request-move-article 
7771                  (car articles)                   ; Article to move
7772                  gnus-newsgroup-name              ; From newsgrouo
7773                  (nth 1 (gnus-find-method-for-group 
7774                          gnus-newsgroup-name))    ; Server
7775                  (list 'gnus-request-accept-article 
7776                        (if select-method
7777                            (list 'quote select-method)
7778                          to-newsgroup)
7779                        (not (cdr articles)))     ; Accept form
7780                  (not (cdr articles))))          ; Only save nov last time
7781           (let* ((buffer-read-only nil)
7782                  (entry 
7783                   (or
7784                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7785                    (gnus-gethash 
7786                     (gnus-group-prefixed-name 
7787                      (car art-group) 
7788                      (if select-method (list select-method "")
7789                        (gnus-find-method-for-group to-newsgroup)))
7790                     gnus-newsrc-hashtb)))
7791                  (info (nth 2 entry))
7792                  (article (car articles))
7793                  (marked (nth 3 info)))
7794             (gnus-summary-goto-subject article)
7795             (beginning-of-line)
7796             (delete-region (point)
7797                            (progn (forward-line 1) (point)))
7798             (if (not (memq article gnus-newsgroup-unreads))
7799                 (setcar (cdr (cdr info))
7800                         (gnus-add-to-range (nth 2 info) 
7801                                            (list (cdr art-group)))))
7802             ;; Copy any marks over to the new group.
7803             (let ((marks '((tick . gnus-newsgroup-marked)
7804                            (dormant . gnus-newsgroup-dormant)
7805                            (expire . gnus-newsgroup-expirable)
7806                            (bookmark . gnus-newsgroup-bookmarks)
7807                         ;   (score . gnus-newsgroup-scored)
7808                            (reply . gnus-newsgroup-replied)))
7809                   (to-article (cdr art-group)))
7810               (while marks
7811                 (if (memq article (symbol-value (cdr (car marks))))
7812                     (gnus-add-marked-articles 
7813                      (car info) (car (car marks)) (list to-article) info))
7814                 (setq marks (cdr marks))))
7815             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7816             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7817             (setq gnus-newsgroup-dormant
7818                   (delq article gnus-newsgroup-dormant)))
7819         (gnus-message 1 "Couldn't move article %s" (car articles)))
7820       (gnus-summary-remove-process-mark (car articles))
7821       (setq articles (cdr articles)))))
7822
7823 (defun gnus-summary-respool-article (n &optional respool-method)
7824   "Respool the current article.
7825 The article will be squeezed through the mail spooling process again,
7826 which means that it will be put in some mail newsgroup or other
7827 depending on `nnmail-split-methods'.
7828 If N is a positive number, respool the N next articles.
7829 If N is a negative number, respool the N previous articles.
7830 If N is nil and any articles have been marked with the process mark,
7831 respool those articles instead.
7832
7833 Respooling can be done both from mail groups and \"real\" newsgroups.
7834 In the former case, the articles in question will be moved from the
7835 current group into whatever groups they are destined to.  In the
7836 latter case, they will be copied into the relevant groups."
7837   (interactive "P")
7838   (gnus-set-global-variables)
7839   (let ((respool-methods (gnus-methods-using 'respool))
7840         (methname 
7841          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
7842     (or respool-method
7843         (setq respool-method
7844               (completing-read
7845                "What method do you want to use when respooling? "
7846                respool-methods nil t methname)))
7847     (or (string= respool-method "")
7848         (if (assoc (symbol-name
7849                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
7850                    respool-methods)
7851             (gnus-summary-move-article n nil (intern respool-method))
7852           (gnus-summary-copy-article n nil (intern respool-method))))))
7853
7854 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
7855 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
7856   "Move the current article to a different newsgroup.
7857 If N is a positive number, move the N next articles.
7858 If N is a negative number, move the N previous articles.
7859 If N is nil and any articles have been marked with the process mark,
7860 move those articles instead.
7861 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7862 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7863 re-spool using this method.
7864 For this function to work, the newsgroup that you want to move to have
7865 to support the `request-move' and `request-accept'
7866 functions. (Ie. mail newsgroups at present.)"
7867   (interactive "P")
7868   (gnus-set-global-variables)
7869   (let ((articles (gnus-summary-work-articles n))
7870         (copy-buf (get-buffer-create "*copy work*"))
7871         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7872         art-group)
7873     (buffer-disable-undo copy-buf)
7874     (if (and (not to-newsgroup) (not select-method))
7875         (setq to-newsgroup
7876               (completing-read 
7877                (format "Where do you want to copy %s? %s"
7878                        (if (> (length articles) 1)
7879                            (format "these %d articles" (length articles))
7880                          "this article")
7881                        (if gnus-current-move-group
7882                            (format "(%s default) " gnus-current-move-group)
7883                          ""))
7884                gnus-active-hashtb nil nil prefix)))
7885     (if to-newsgroup
7886         (progn
7887           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7888               (setq to-newsgroup (or gnus-current-move-group "")))
7889           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7890               (gnus-activate-newsgroup to-newsgroup)
7891               (error "No such group: %s" to-newsgroup))
7892           (setq gnus-current-move-group to-newsgroup)))
7893     (or (gnus-check-backend-function 'request-accept-article 
7894                                      (or select-method to-newsgroup))
7895         (error "%s does not support article copying" to-newsgroup))
7896     (gnus-message 6 "Copying to %s: %s..." 
7897                   (or select-method to-newsgroup) articles)
7898     (while articles
7899       (if (setq art-group
7900                 (save-excursion
7901                   (set-buffer copy-buf)
7902                   (gnus-request-article-this-buffer
7903                    (car articles) gnus-newsgroup-name)
7904                   (gnus-request-accept-article
7905                    (if select-method (quote select-method) to-newsgroup)
7906                    (not (cdr articles)))))
7907           (let* ((entry 
7908                   (or
7909                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7910                    (gnus-gethash 
7911                     (gnus-group-prefixed-name 
7912                      (car art-group) 
7913                      (if select-method (list select-method "")
7914                        (gnus-find-method-for-group to-newsgroup)))
7915                     gnus-newsrc-hashtb)))
7916                  (info (nth 2 entry))
7917                  (article (car articles))
7918                  (marked (nth 3 info)))
7919             (if (not (memq article gnus-newsgroup-unreads))
7920                 (setcar (cdr (cdr info))
7921                         (gnus-add-to-range (nth 2 info) 
7922                                            (list (cdr art-group)))))
7923             ;; Copy any marks over to the new group.
7924             (let ((marks '((tick . gnus-newsgroup-marked)
7925                            (dormant . gnus-newsgroup-dormant)
7926                            (expire . gnus-newsgroup-expirable)
7927                            (bookmark . gnus-newsgroup-bookmarks)
7928                         ;   (score . gnus-newsgroup-scored)
7929                            (reply . gnus-newsgroup-replied)))
7930                   (to-article (cdr art-group)))
7931               (while marks
7932                 (if (memq article (symbol-value (cdr (car marks))))
7933                     (gnus-add-marked-articles 
7934                      (car info) (car (car marks)) (list to-article) info))
7935                 (setq marks (cdr marks)))))
7936         (gnus-message 1 "Couldn't copy article %s" (car articles)))
7937       (gnus-summary-remove-process-mark (car articles))
7938       (setq articles (cdr articles)))
7939     (kill-buffer copy-buf)))
7940
7941 (defun gnus-summary-expire-articles ()
7942   "Expire all articles that are marked as expirable in the current group."
7943   (interactive)
7944   (if (and gnus-newsgroup-expirable
7945            (gnus-check-backend-function 
7946             'request-expire-articles gnus-newsgroup-name))
7947       (let ((expirable gnus-newsgroup-expirable))
7948         ;; The list of articles that weren't expired is returned.
7949         (setq gnus-newsgroup-expirable 
7950               (gnus-request-expire-articles gnus-newsgroup-expirable
7951                                             gnus-newsgroup-name))
7952         ;; We go through the old list of expirable, and mark all
7953         ;; really expired articles as non-existant.
7954         (while expirable
7955           (or (memq (car expirable) gnus-newsgroup-expirable)
7956               (gnus-summary-mark-as-read (car expirable) "%"))
7957           (setq expirable (cdr expirable))))))
7958
7959 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7960 (defun gnus-summary-delete-article (n)
7961   "Delete the N next (mail) articles.
7962 This command actually deletes articles. This is not a marking
7963 command. The article will disappear forever from you life, never to
7964 return. 
7965 If N is negative, delete backwards.
7966 If N is nil and articles have been marked with the process mark,
7967 delete these instead."
7968   (interactive "P")
7969   (or (gnus-check-backend-function 'request-expire-articles 
7970                                    gnus-newsgroup-name)
7971       (error "The current newsgroup does not support article deletion."))
7972   ;; Compute the list of articles to delete.
7973   (let ((articles (gnus-summary-work-articles n)))
7974     (if (and gnus-novice-user
7975              (not (gnus-y-or-n-p 
7976                    (format "Do you really want to delete %s forever? "
7977                            (if (> (length articles) 1) "these articles"
7978                              "this article")))))
7979         ()
7980       ;; Delete the articles.
7981       (setq gnus-newsgroup-expirable 
7982             (gnus-request-expire-articles 
7983              articles gnus-newsgroup-name 'force))
7984       (while articles
7985         (gnus-summary-remove-process-mark (car articles))       
7986         ;; The backend might not have been able to delete the article
7987         ;; after all.  
7988         (or (memq (car articles) gnus-newsgroup-expirable)
7989             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
7990         (setq articles (cdr articles)))))
7991   (gnus-summary-position-cursor))
7992
7993 (defun gnus-summary-edit-article ()
7994   "Enter into a buffer and edit the current article.
7995 This will have permanent effect only in mail groups."
7996   (interactive)
7997   (or (gnus-check-backend-function 
7998        'request-replace-article gnus-newsgroup-name)
7999       (error "The current newsgroup does not support article editing."))
8000   (gnus-summary-select-article t)
8001   (other-window 1)
8002   (gnus-message 6 "C-c C-c to end edits")
8003   (setq buffer-read-only nil)
8004   (text-mode)
8005   (use-local-map (copy-keymap (current-local-map)))
8006   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8007   (goto-char (point-min))
8008   (search-forward "\n\n" nil t))
8009
8010 (defun gnus-summary-edit-article-done ()
8011   "Make edits to the current article permanent."
8012   (interactive)
8013   (if (not (gnus-request-replace-article 
8014             (cdr gnus-article-current) (car gnus-article-current) 
8015             (current-buffer)))
8016       (error "Couldn't replace article.")
8017     (gnus-article-mode)
8018     (use-local-map gnus-article-mode-map)
8019     (setq buffer-read-only t)
8020     (pop-to-buffer gnus-summary-buffer)))      
8021
8022 (defun gnus-summary-fancy-query ()
8023   "Query where the fancy respool algorithm would put this article."
8024   (interactive)
8025   (gnus-summary-select-article)
8026   (save-excursion
8027     (set-buffer gnus-article-buffer)
8028     (save-restriction
8029       (goto-char (point-min))
8030       (search-forward "\n\n")
8031       (narrow-to-region (point-min) (point))
8032       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8033
8034 ;; Summary score commands.
8035
8036 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8037
8038 (defun gnus-summary-raise-score (n)
8039   "Raise the score of the current article by N."
8040   (interactive "p")
8041   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8042
8043 (defun gnus-summary-lower-score (n)
8044   "Lower the score of the current article by N."
8045   (interactive "p")
8046   (gnus-summary-raise-score (- n)))
8047
8048 (defun gnus-summary-set-score (n)
8049   "Set the score of the current article to N."
8050   (interactive "p")
8051   ;; Skip dummy header line.
8052   (save-excursion
8053     (if (eq (gnus-summary-article-mark) ?Z)
8054         (forward-line 1))
8055     (let ((buffer-read-only nil))
8056       ;; Set score.
8057       (gnus-summary-update-mark
8058        (if (= n (or gnus-summary-default-score 0)) ? 
8059          (if (< n (or gnus-summary-default-score 0)) 
8060              gnus-score-below-mark gnus-score-over-mark)) 'score))
8061     (let* ((article (gnus-summary-article-number))
8062            (score (assq article gnus-newsgroup-scored)))
8063       (if score (setcdr score n)
8064         (setq gnus-newsgroup-scored 
8065               (cons (cons article n) gnus-newsgroup-scored))))
8066     (gnus-summary-update-line)))
8067
8068 (defun gnus-summary-current-score ()
8069   "Return the score of the current article."
8070   (interactive)
8071   (message "%s" (gnus-summary-article-score)))
8072
8073 ;; Summary marking commands.
8074
8075 (defun gnus-summary-raise-same-subject-and-select (score)
8076   "Raise articles which has the same subject with SCORE and select the next."
8077   (interactive "p")
8078   (let ((subject (gnus-summary-subject-string)))
8079     (gnus-summary-raise-score score)
8080     (while (gnus-summary-search-subject nil nil subject)
8081       (gnus-summary-raise-score score))
8082     (gnus-summary-next-article t)))
8083
8084 (defun gnus-summary-raise-same-subject (score)
8085   "Raise articles which has the same subject with SCORE."
8086   (interactive "p")
8087   (let ((subject (gnus-summary-subject-string)))
8088     (gnus-summary-raise-score score)
8089     (while (gnus-summary-search-subject nil nil subject)
8090       (gnus-summary-raise-score score))
8091     (gnus-summary-next-subject 1 t)))
8092
8093 (defun gnus-score-default (level)
8094   (if level (prefix-numeric-value level) 
8095     gnus-score-interactive-default-score))
8096
8097 (defun gnus-summary-raise-thread (score)
8098   "Raise articles under current thread with SCORE."
8099   (interactive "P")
8100   (setq score (1- (gnus-score-default score)))
8101   (let (e)
8102     (save-excursion
8103       (let ((level (gnus-summary-thread-level)))
8104         (gnus-summary-raise-score score)
8105         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8106                     (> (gnus-summary-thread-level) level))
8107           (gnus-summary-raise-score score))
8108         (setq e (point))))
8109     (let ((gnus-summary-check-current t))
8110       (or (zerop (gnus-summary-next-subject 1 t))
8111           (goto-char e))))
8112   (gnus-summary-recenter)
8113   (gnus-summary-position-cursor)
8114   (gnus-set-mode-line 'summary))
8115
8116 (defun gnus-summary-lower-same-subject-and-select (score)
8117   "Raise articles which has the same subject with SCORE and select the next."
8118   (interactive "p")
8119   (gnus-summary-raise-same-subject-and-select (- score)))
8120
8121 (defun gnus-summary-lower-same-subject (score)
8122   "Raise articles which has the same subject with SCORE."
8123   (interactive "p")
8124   (gnus-summary-raise-same-subject (- score)))
8125
8126 (defun gnus-summary-lower-thread (score)
8127   "Raise articles under current thread with SCORE."
8128   (interactive "P")
8129   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8130
8131 (defun gnus-summary-kill-same-subject-and-select (unmark)
8132   "Mark articles which has the same subject as read, and then select the next.
8133 If UNMARK is positive, remove any kind of mark.
8134 If UNMARK is negative, tick articles."
8135   (interactive "P")
8136   (if unmark
8137       (setq unmark (prefix-numeric-value unmark)))
8138   (let ((count
8139          (gnus-summary-mark-same-subject
8140           (gnus-summary-subject-string) unmark)))
8141     ;; Select next unread article. If auto-select-same mode, should
8142     ;; select the first unread article.
8143     (gnus-summary-next-article t (and gnus-auto-select-same
8144                                       (gnus-summary-subject-string)))
8145     (gnus-message 7 "%d articles are marked as %s"
8146                   count (if unmark "unread" "read"))))
8147
8148 (defun gnus-summary-kill-same-subject (unmark)
8149   "Mark articles which has the same subject as read. 
8150 If UNMARK is positive, remove any kind of mark.
8151 If UNMARK is negative, tick articles."
8152   (interactive "P")
8153   (if unmark
8154       (setq unmark (prefix-numeric-value unmark)))
8155   (let ((count
8156          (gnus-summary-mark-same-subject
8157           (gnus-summary-subject-string) unmark)))
8158     ;; If marked as read, go to next unread subject.
8159     (if (null unmark)
8160         ;; Go to next unread subject.
8161         (gnus-summary-next-subject 1 t))
8162     (gnus-message 7 "%d articles are marked as %s"
8163                   count (if unmark "unread" "read"))))
8164
8165 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8166   "Mark articles with same SUBJECT as read, and return marked number.
8167 If optional argument UNMARK is positive, remove any kinds of marks.
8168 If optional argument UNMARK is negative, mark articles as unread instead."
8169   (let ((count 1))
8170     (save-excursion
8171       (cond ((null unmark)
8172              (gnus-summary-mark-as-read nil gnus-killed-mark))
8173             ((> unmark 0)
8174              (gnus-summary-tick-article nil t))
8175             (t
8176              (gnus-summary-tick-article)))
8177       (while (and subject
8178                   (gnus-summary-search-forward nil subject))
8179         (cond ((null unmark)
8180                (gnus-summary-mark-as-read nil gnus-killed-mark))
8181               ((> unmark 0)
8182                (gnus-summary-tick-article nil t))
8183               (t
8184                (gnus-summary-tick-article)))
8185         (setq count (1+ count))))
8186     ;; Hide killed thread subtrees.  Does not work properly always.
8187     ;;(and (null unmark)
8188     ;;     gnus-thread-hide-killed
8189     ;;     (gnus-summary-hide-thread))
8190     ;; Return number of articles marked as read.
8191     count))
8192
8193 (defun gnus-summary-mark-as-processable (n &optional unmark)
8194   "Set the process mark on the next N articles.
8195 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8196 the process mark instead.  The difference between N and the actual
8197 number of articles marked is returned."
8198   (interactive "p")
8199   (let ((backward (< n 0))
8200         (n (abs n)))
8201   (while (and 
8202           (> n 0)
8203           (if unmark
8204               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8205             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8206           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8207     (setq n (1- n)))
8208   (if (/= 0 n) (gnus-message 7 "No more articles"))
8209   (gnus-summary-recenter)
8210   (gnus-summary-position-cursor)
8211   n))
8212
8213 (defun gnus-summary-unmark-as-processable (n)
8214   "Remove the process mark from the next N articles.
8215 If N is negative, mark backward instead.  The difference between N and
8216 the actual number of articles marked is returned."
8217   (interactive "p")
8218   (gnus-summary-mark-as-processable n t))
8219
8220 (defun gnus-summary-unmark-all-processable ()
8221   "Remove the process mark from all articles."
8222   (interactive)
8223   (save-excursion
8224     (while gnus-newsgroup-processable
8225       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8226   (gnus-summary-position-cursor))
8227
8228 (defun gnus-summary-mark-as-expirable (n)
8229   "Mark N articles forward as expirable.
8230 If N is negative, mark backward instead. The difference between N and
8231 the actual number of articles marked is returned."
8232   (interactive "p")
8233   (gnus-summary-mark-forward n gnus-expirable-mark))
8234
8235 (defun gnus-summary-mark-article-as-replied (article)
8236   "Mark ARTICLE replied and update the summary line."
8237   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8238   (let ((buffer-read-only nil))
8239     (if (gnus-summary-goto-subject article)
8240         (progn
8241           (gnus-summary-update-mark gnus-replied-mark 'replied)
8242           t))))
8243
8244 (defun gnus-summary-set-bookmark (article)
8245   "Set a bookmark in current article."
8246   (interactive (list (gnus-summary-article-number)))
8247   (if (or (not (get-buffer gnus-article-buffer))
8248           (not gnus-current-article)
8249           (not gnus-article-current)
8250           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8251       (error "No current article selected"))
8252   ;; Remove old bookmark, if one exists.
8253   (let ((old (assq article gnus-newsgroup-bookmarks)))
8254     (if old (setq gnus-newsgroup-bookmarks 
8255                   (delq old gnus-newsgroup-bookmarks))))
8256   ;; Set the new bookmark, which is on the form 
8257   ;; (article-number . line-number-in-body).
8258   (setq gnus-newsgroup-bookmarks 
8259         (cons 
8260          (cons article 
8261                (save-excursion
8262                  (set-buffer gnus-article-buffer)
8263                  (count-lines
8264                   (min (point)
8265                        (save-excursion
8266                          (goto-char (point-min))
8267                          (search-forward "\n\n" nil t)
8268                          (point)))
8269                   (point))))
8270          gnus-newsgroup-bookmarks))
8271   (gnus-message 6 "A bookmark has been added to the current article."))
8272
8273 (defun gnus-summary-remove-bookmark (article)
8274   "Remove the bookmark from the current article."
8275   (interactive (list (gnus-summary-article-number)))
8276   ;; Remove old bookmark, if one exists.
8277   (let ((old (assq article gnus-newsgroup-bookmarks)))
8278     (if old 
8279         (progn
8280           (setq gnus-newsgroup-bookmarks 
8281                 (delq old gnus-newsgroup-bookmarks))
8282           (gnus-message 6 "Removed bookmark."))
8283       (gnus-message 6 "No bookmark in current article."))))
8284
8285 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8286 (defun gnus-summary-mark-as-dormant (n)
8287   "Mark N articles forward as dormant.
8288 If N is negative, mark backward instead.  The difference between N and
8289 the actual number of articles marked is returned."
8290   (interactive "p")
8291   (gnus-summary-mark-forward n gnus-dormant-mark))
8292
8293 (defun gnus-summary-set-process-mark (article)
8294   "Set the process mark on ARTICLE and update the summary line."
8295   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8296   (let ((buffer-read-only nil))
8297     (if (gnus-summary-goto-subject article)
8298         (progn
8299           (and (eq (gnus-summary-article-mark) ?Z)
8300                (forward-line 1))
8301           (gnus-summary-update-mark gnus-process-mark 'replied)
8302           t))))
8303
8304 (defun gnus-summary-remove-process-mark (article)
8305   "Remove the process mark from ARTICLE and update the summary line."
8306   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8307   (let ((buffer-read-only nil))
8308     (if (gnus-summary-goto-subject article)
8309         (progn
8310           (and (eq (gnus-summary-article-mark) ?Z)
8311                (forward-line 1))
8312           (gnus-summary-update-mark ?  'replied)
8313           (if (memq article gnus-newsgroup-replied) 
8314               (gnus-summary-update-mark gnus-replied-mark 'replied))
8315           t))))
8316
8317 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8318   "Mark N articles as read forwards.
8319 If N is negative, mark backwards instead.
8320 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8321 marked as unread. 
8322 The difference between N and the actual number of articles marked is
8323 returned."
8324   (interactive "p")
8325   (gnus-set-global-variables)
8326   (let ((backward (< n 0))
8327         (n (abs n))
8328         (mark (or mark gnus-del-mark)))
8329   (while (and (> n 0)
8330               (gnus-summary-mark-article nil mark no-expire)
8331               (zerop (gnus-summary-next-subject 
8332                       (if backward -1 1) gnus-summary-goto-unread t)))
8333     (setq n (1- n)))
8334   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8335   (gnus-summary-recenter)
8336   (gnus-summary-position-cursor)
8337   (gnus-set-mode-line 'summary)
8338   n))
8339
8340 (defun gnus-summary-mark-article (&optional article mark no-expire)
8341   "Mark ARTICLE with MARK.
8342 MARK can be any character.
8343 Five MARK strings are reserved: ?  (unread), 
8344 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8345 If MARK is nil, then the default character ?D is used.
8346 If ARTICLE is nil, then the article on the current line will be
8347 marked." 
8348   ;; If no mark is given, then we check auto-expiring.
8349   (and (not no-expire)
8350        gnus-newsgroup-auto-expire 
8351        (or (not mark)
8352            (and (numberp mark) (or (= mark gnus-killed-mark)
8353                                    (= mark gnus-del-mark)
8354                                    (= mark gnus-catchup-mark)
8355                                    (= mark gnus-low-score-mark)
8356                                    (= mark gnus-read-mark))))
8357        (setq mark gnus-expirable-mark))
8358   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8359          (article (or article (gnus-summary-article-number))))
8360     (if (or (= mark gnus-unread-mark) 
8361             (= mark gnus-ticked-mark) 
8362             (= mark gnus-dormant-mark))
8363         (gnus-mark-article-as-unread article mark)
8364       (gnus-mark-article-as-read article mark))
8365
8366     ;; See whether the article is to be put in the cache.
8367     (and gnus-use-cache
8368          (save-excursion
8369            (gnus-cache-possibly-enter-article 
8370             gnus-newsgroup-name article 
8371             (gnus-get-header-by-number article)
8372             (= mark gnus-ticked-mark)
8373             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8374
8375     (if (gnus-summary-goto-subject article)
8376         (let ((buffer-read-only nil))
8377           (gnus-summary-show-thread)
8378           (beginning-of-line)
8379           (and (eq (gnus-summary-article-mark) ?Z)
8380                (forward-line 1))
8381           ;; Fix the mark.
8382           (gnus-summary-update-mark mark 'unread)
8383           t))))
8384
8385 (defun gnus-summary-update-mark (mark type)
8386   (beginning-of-line)
8387   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8388         plist)
8389     (if (not forward)
8390         ()
8391       (forward-char forward)
8392       (setq plist (text-properties-at (point)))
8393       (delete-char 1)
8394       (and (memq 'gnus plist) 
8395            (setcar (cdr (car (cdr (memq 'gnus plist)))) mark))
8396       (insert mark)
8397       (and plist (add-text-properties (1- (point)) (point) plist))
8398       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8399   
8400 (defun gnus-mark-article-as-read (article &optional mark)
8401   "Enter ARTICLE in the pertinent lists and remove it from others."
8402   ;; Make the article expirable.
8403   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8404     (if (= mark gnus-expirable-mark)
8405         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8406       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8407     ;; Remove from unread and marked lists.
8408     (setq gnus-newsgroup-unreads
8409           (delq article gnus-newsgroup-unreads))
8410     (setq gnus-newsgroup-marked
8411           (delq article gnus-newsgroup-marked))
8412     (setq gnus-newsgroup-dormant
8413           (delq article gnus-newsgroup-dormant))))
8414
8415 (defun gnus-mark-article-as-unread (article &optional mark)
8416   "Enter ARTICLE in the pertinent lists and remove it from others."
8417   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8418     ;; Add to unread list.
8419     (or (memq article gnus-newsgroup-unreads)
8420         (setq gnus-newsgroup-unreads
8421               (cons article gnus-newsgroup-unreads)))
8422     ;; If CLEAR-MARK is non-nil, the article must be removed from marked
8423     ;; list.  Otherwise, it must be added to the list.
8424     (setq gnus-newsgroup-marked
8425           (delq article gnus-newsgroup-marked))
8426     (setq gnus-newsgroup-dormant
8427           (delq article gnus-newsgroup-dormant))
8428     (setq gnus-newsgroup-expirable 
8429           (delq article gnus-newsgroup-expirable))
8430     (if (= mark gnus-ticked-mark)
8431         (setq gnus-newsgroup-marked 
8432               (cons article gnus-newsgroup-marked)))
8433     (if (= mark gnus-dormant-mark)
8434         (setq gnus-newsgroup-dormant 
8435               (cons article gnus-newsgroup-dormant)))))
8436
8437 (defalias 'gnus-summary-mark-as-unread-forward 
8438   'gnus-summary-tick-article-forward)
8439 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8440                'gnus-summary-tick-article-forward)
8441 (defun gnus-summary-tick-article-forward (n)
8442   "Tick N articles forwards.
8443 If N is negative, tick backwards instead.
8444 The difference between N and the number of articles ticked is returned."
8445   (interactive "p")
8446   (gnus-summary-mark-forward n gnus-ticked-mark))
8447
8448 (defalias 'gnus-summary-mark-as-unread-backward 
8449   'gnus-summary-tick-article-backward)
8450 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8451                'gnus-summary-tick-article-backward)
8452 (defun gnus-summary-tick-article-backward (n)
8453   "Tick N articles backwards.
8454 The difference between N and the number of articles ticked is returned."
8455   (interactive "p")
8456   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8457
8458 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8459 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8460 (defun gnus-summary-tick-article (&optional article clear-mark)
8461   "Mark current article as unread.
8462 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8463 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8464   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8465                                        gnus-ticked-mark)))
8466
8467 (defun gnus-summary-mark-as-read-forward (n)
8468   "Mark N articles as read forwards.
8469 If N is negative, mark backwards instead.
8470 The difference between N and the actual number of articles marked is
8471 returned."
8472   (interactive "p")
8473   (gnus-summary-mark-forward n gnus-del-mark t))
8474
8475 (defun gnus-summary-mark-as-read-backward (n)
8476   "Mark the N articles as read backwards.
8477 The difference between N and the actual number of articles marked is
8478 returned."
8479   (interactive "p")
8480   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8481
8482 (defun gnus-summary-mark-as-read (&optional article mark)
8483   "Mark current article as read.
8484 ARTICLE specifies the article to be marked as read.
8485 MARK specifies a string to be inserted at the beginning of the line."
8486   (gnus-summary-mark-article article mark))
8487
8488 (defun gnus-summary-clear-mark-forward (n)
8489   "Clear marks from N articles forward.
8490 If N is negative, clear backward instead.
8491 The difference between N and the number of marks cleared is returned."
8492   (interactive "p")
8493   (gnus-summary-mark-forward n gnus-unread-mark))
8494
8495 (defun gnus-summary-clear-mark-backward (n)
8496   "Clear marks from N articles backward.
8497 The difference between N and the number of marks cleared is returned."
8498   (interactive "p")
8499   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8500
8501 (defun gnus-summary-mark-unread-as-read ()
8502   "Intended to be used by `gnus-summary-mark-article-hook'."
8503   (or (memq gnus-current-article gnus-newsgroup-marked)
8504       (memq gnus-current-article gnus-newsgroup-dormant)
8505       (memq gnus-current-article gnus-newsgroup-expirable)
8506       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8507
8508 (defun gnus-summary-mark-region-as-read (point mark all)
8509   "Mark all unread articles between point and mark as read.
8510 If given a prefix, mark all articles between point and mark as read,
8511 even ticked and dormant ones."
8512   (interactive "r\nP")
8513   (save-excursion
8514     (goto-char point)
8515     (beginning-of-line)
8516     (while (and 
8517             (< (point) mark)
8518             (progn
8519               (and
8520                (or all
8521                    (and
8522                     (not (memq (gnus-summary-article-number)
8523                                gnus-newsgroup-marked))
8524                     (not (memq (gnus-summary-article-number)
8525                                gnus-newsgroup-dormant))))
8526                (gnus-summary-mark-article
8527                 (gnus-summary-article-number) gnus-del-mark))
8528               t)
8529             (zerop (forward-line 1))))))
8530
8531 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8532 (defalias 'gnus-summary-delete-marked-as-read 
8533   'gnus-summary-remove-lines-marked-as-read)
8534 (make-obsolete 'gnus-summary-delete-marked-as-read 
8535                'gnus-summary-remove-lines-marked-as-read)
8536 (defun gnus-summary-remove-lines-marked-as-read ()
8537   "Remove lines that are marked as read."
8538   (interactive)
8539   (gnus-summary-remove-lines-marked-with 
8540    (concat (mapconcat
8541             (lambda (char) (char-to-string (symbol-value char)))
8542             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
8543               gnus-killed-mark gnus-kill-file-mark
8544               gnus-low-score-mark gnus-expirable-mark)
8545             ""))))
8546
8547 (defalias 'gnus-summary-delete-marked-with 
8548   'gnus-summary-remove-lines-marked-with)
8549 (make-obsolete 'gnus-summary-delete-marked-with 
8550                'gnus-summary-remove-lines-marked-with)
8551 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8552 (defun gnus-summary-remove-lines-marked-with (marks)
8553   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8554   (interactive "sMarks: ")
8555   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8556   (save-excursion
8557     (set-buffer gnus-summary-buffer)
8558     (let ((buffer-read-only nil)
8559           (marks (concat "^[" marks "]")))
8560       (goto-char (point-min))
8561       (if gnus-newsgroup-adaptive
8562           (gnus-score-remove-lines-adaptive marks)
8563         (while (re-search-forward marks (point-max) t)
8564           (beginning-of-line)
8565           (delete-region (point)
8566                          (progn (forward-line 1) (point))))))
8567     (or (zerop (buffer-size))
8568         (if (eobp)
8569             (gnus-summary-prev-subject 1)
8570           (gnus-summary-position-cursor)))))
8571
8572 (defun gnus-summary-expunge-below (score)
8573   "Remove articles with score less than SCORE."
8574   (interactive "P")
8575   (setq score (if score
8576                   (prefix-numeric-value score)
8577                 (or gnus-summary-default-score 0)))
8578   (save-excursion
8579     (set-buffer gnus-summary-buffer)
8580     (goto-char (point-min))
8581     (let ((buffer-read-only nil)
8582           beg)
8583       (while (not (eobp))
8584         (if (< (gnus-summary-article-score) score)
8585             (progn
8586               (setq beg (point))
8587               (forward-line 1)
8588               (delete-region beg (point)))
8589           (forward-line 1)))
8590       ;; Adjust point.
8591       (or (zerop (buffer-size))
8592           (if (eobp)
8593               (gnus-summary-prev-subject 1)
8594             (gnus-summary-position-cursor))))))
8595
8596 (defun gnus-summary-mark-below (score mark)
8597   "Mark articles with score less than SCORE with MARK."
8598   (interactive "P\ncMark: ")
8599   (setq score (if score
8600                   (prefix-numeric-value score)
8601                 (or gnus-summary-default-score 0)))
8602   (save-excursion
8603     (set-buffer gnus-summary-buffer)
8604     (goto-char (point-min))
8605     (while (not (eobp))
8606       (and (< (gnus-summary-article-score) score)
8607            (gnus-summary-mark-article nil mark))
8608       (forward-line 1))))
8609
8610 (defun gnus-summary-kill-below (score)
8611   "Mark articles with score below SCORE as read."
8612   (interactive "P")
8613   (gnus-summary-mark-below score gnus-killed-mark))
8614
8615 (defun gnus-summary-clear-above (score)
8616   "Clear all marks from articles with score above SCORE."
8617   (interactive "P")
8618   (gnus-summary-mark-above score gnus-unread-mark))
8619
8620 (defun gnus-summary-tick-above (score)
8621   "Tick all articles with score above SCORE."
8622   (interactive "P")
8623   (gnus-summary-mark-above score gnus-ticked-mark))
8624
8625 (defun gnus-summary-mark-above (score mark)
8626   "Mark articles with score over SCORE with MARK."
8627   (interactive "P\ncMark: ")
8628   (setq score (if score
8629                   (prefix-numeric-value score)
8630                 (or gnus-summary-default-score 0)))
8631   (save-excursion
8632     (set-buffer gnus-summary-buffer)
8633     (goto-char (point-min))
8634     (while (not (eobp))
8635       (if (> (gnus-summary-article-score) score)
8636           (progn
8637             (gnus-summary-mark-article nil mark)
8638             (forward-line 1))
8639         (forward-line 1)))))
8640
8641 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
8642 (defun gnus-summary-show-all-expunged ()
8643   "Display all the hidden articles that were expunged for low scores."
8644   (interactive)
8645   (let ((buffer-read-only nil))
8646     (let ((scored gnus-newsgroup-scored)
8647           headers h)
8648       (while scored
8649         (or (gnus-summary-goto-subject (car (car scored)))
8650             (and (setq h (gnus-get-header-by-number (car (car scored))))
8651                  (< (cdr (car scored)) gnus-summary-expunge-below)
8652                  (setq headers (cons h headers))))
8653         (setq scored (cdr scored)))
8654       (or headers (error "No expunged articles hidden."))
8655       (goto-char (point-min))
8656       (save-excursion 
8657         (gnus-summary-update-lines 
8658          (point)
8659          (progn
8660            (gnus-summary-prepare-threads (nreverse headers) 0)
8661            (point)))))
8662     (goto-char (point-min))
8663     (gnus-summary-position-cursor)))
8664
8665 (defun gnus-summary-show-all-dormant ()
8666   "Display all the hidden articles that are marked as dormant."
8667   (interactive)
8668   (let ((buffer-read-only nil))
8669     (goto-char (point-min))
8670     (let ((dormant gnus-newsgroup-dormant)
8671           headers h)
8672       (while dormant
8673         (or (gnus-summary-goto-subject (car dormant))
8674             (and (setq h (gnus-get-header-by-number (car dormant)))
8675                  (setq headers (cons h headers))))
8676         (setq dormant (cdr dormant)))
8677       (or headers (error "No dormant articles hidden."))
8678       (save-excursion 
8679         (gnus-summary-update-lines 
8680          (point)
8681          (progn
8682            (gnus-summary-prepare-threads (nreverse headers) 0)
8683            (point)))))
8684     (goto-char (point-min))
8685     (gnus-summary-position-cursor)))
8686
8687 (defun gnus-summary-hide-all-dormant ()
8688   "Hide all dormant articles."
8689   (interactive)
8690   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
8691   (gnus-summary-position-cursor))
8692
8693 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
8694   "Mark all articles not marked as unread in this newsgroup as read.
8695 If prefix argument ALL is non-nil, all articles are marked as read.
8696 If QUIETLY is non-nil, no questions will be asked.
8697 If TO-HERE is non-nil, it should be a point in the buffer. All
8698 articles before this point will be marked as read.
8699 The number of articles marked as read is returned."
8700   (interactive "P")
8701   (prog1
8702       (if (or quietly
8703               (not gnus-interactive-catchup) ;Without confirmation?
8704               gnus-expert-user
8705               (gnus-y-or-n-p
8706                (if all
8707                    "Mark absolutely all articles as read? "
8708                  "Mark all unread articles as read? ")))
8709           (if (and not-mark 
8710                    (not gnus-newsgroup-adaptive)
8711                    (not gnus-newsgroup-auto-expire))
8712               (progn
8713                 (and all (setq gnus-newsgroup-marked nil
8714                                gnus-newsgroup-dormant nil))
8715                 (setq gnus-newsgroup-unreads gnus-newsgroup-marked))
8716             (let ((unreads (length gnus-newsgroup-unreads)))
8717               (if (gnus-summary-first-subject (not all))
8718                   (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
8719                               (if to-here (< (point) to-here) t)
8720                               (gnus-summary-search-subject nil (not all)))))
8721               (- unreads (length gnus-newsgroup-unreads))
8722               (or to-here
8723                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
8724     (gnus-summary-position-cursor)))
8725
8726 (defun gnus-summary-catchup-to-here (&optional all)
8727   "Mark all unticked articles before the current one as read.
8728 If ALL is non-nil, also mark ticked and dormant articles as read."
8729   (interactive)
8730   (beginning-of-line)
8731   (gnus-summary-catchup all t (point))
8732   (gnus-summary-position-cursor))
8733
8734 (defun gnus-summary-catchup-all (&optional quietly)
8735   "Mark all articles in this newsgroup as read."
8736   (interactive)
8737   (gnus-summary-catchup t quietly))
8738
8739 (defun gnus-summary-catchup-and-exit (all &optional quietly)
8740   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8741 If prefix argument ALL is non-nil, all articles are marked as read."
8742   (interactive "P")
8743   (gnus-summary-catchup all quietly nil 'fast)
8744   ;; Select next newsgroup or exit.
8745   (if (eq gnus-auto-select-next 'quietly)
8746       (gnus-summary-next-group nil)
8747     (gnus-summary-exit)))
8748
8749 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8750   "Mark all articles in this newsgroup as read, and then exit."
8751   (interactive)
8752   (gnus-summary-catchup-and-exit t quietly))
8753
8754 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8755 (defun gnus-summary-catchup-and-goto-next-group (all)
8756   "Mark all articles in this group as read and select the next group.
8757 If given a prefix, mark all articles, unread as well as ticked, as
8758 read." 
8759   (interactive "P")
8760   (gnus-summary-catchup all)
8761   (gnus-summary-next-group))
8762
8763 ;; Thread-based commands.
8764
8765 (defun gnus-summary-toggle-threads (arg)
8766   "Toggle showing conversation threads.
8767 If ARG is positive number, turn showing conversation threads on."
8768   (interactive "P")
8769   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8770     (setq gnus-show-threads
8771           (if (null arg) (not gnus-show-threads)
8772             (> (prefix-numeric-value arg) 0)))
8773     (gnus-summary-prepare)
8774     (gnus-summary-goto-subject current)
8775     (gnus-summary-position-cursor)))
8776
8777 (defun gnus-summary-show-all-threads ()
8778   "Show all threads."
8779   (interactive)
8780   (save-excursion
8781     (let ((buffer-read-only nil))
8782       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8783   (gnus-summary-position-cursor))
8784
8785 (defun gnus-summary-show-thread ()
8786   "Show thread subtrees.
8787 Returns nil if no thread was there to be shown."
8788   (interactive)
8789   (prog1
8790       (save-excursion
8791         (let ((buffer-read-only nil)
8792               ;; first goto end then to beg, to have point at beg after let
8793               (end (progn (end-of-line) (point)))
8794               (beg (progn (beginning-of-line) (point))))
8795           (prog1
8796               ;; Any hidden lines here?
8797               (search-forward "\r" end t)
8798             (subst-char-in-region beg end ?\^M ?\n t))))
8799     (gnus-summary-position-cursor)))
8800
8801 (defun gnus-summary-hide-all-threads ()
8802   "Hide all thread subtrees."
8803   (interactive)
8804   (save-excursion
8805     (goto-char (point-min))
8806     (gnus-summary-hide-thread)
8807     (while (and (not (eobp)) (zerop (forward-line 1)))
8808       (gnus-summary-hide-thread)))
8809   (gnus-summary-position-cursor))
8810
8811 (defun gnus-summary-hide-thread ()
8812   "Hide thread subtrees.
8813 Returns nil if no threads were there to be hidden."
8814   (interactive)
8815   (let ((buffer-read-only nil)
8816         (start (point))
8817         (level (gnus-summary-thread-level))
8818         (end (point)))
8819     ;; Go forward until either the buffer ends or the subthread
8820     ;; ends. 
8821     (if (eobp)
8822         ()
8823       (while (and (zerop (forward-line 1))
8824                   (> (gnus-summary-thread-level) level))
8825         (setq end (point)))
8826       (prog1
8827           (save-excursion
8828             (goto-char end)
8829             (search-backward "\n" start t))
8830         (subst-char-in-region start end ?\n ?\^M t)
8831         (forward-line -1)))))
8832
8833 (defun gnus-summary-go-to-next-thread (&optional previous)
8834   "Go to the same level (or less) next thread.
8835 If PREVIOUS is non-nil, go to previous thread instead.
8836 Return the article number moved to, or nil if moving was impossible."
8837   (let ((level (gnus-summary-thread-level))
8838         (article (gnus-summary-article-number)))
8839     (if previous 
8840         (while (and (zerop (gnus-summary-prev-subject 1))
8841                     (> (gnus-summary-thread-level) level)))
8842       (while (and (zerop (gnus-summary-next-subject 1))
8843                   (> (gnus-summary-thread-level) level))))
8844     (gnus-summary-recenter)
8845     (gnus-summary-position-cursor)
8846     (let ((oart (gnus-summary-article-number)))
8847       (and (/= oart article) oart))))
8848
8849 (defun gnus-summary-next-thread (n)
8850   "Go to the same level next N'th thread.
8851 If N is negative, search backward instead.
8852 Returns the difference between N and the number of skips actually
8853 done."
8854   (interactive "p")
8855   (let ((backward (< n 0))
8856         (n (abs n)))
8857   (while (and (> n 0)
8858               (gnus-summary-go-to-next-thread backward))
8859     (setq n (1- n)))
8860   (gnus-summary-position-cursor)
8861   (if (/= 0 n) (gnus-message 7 "No more threads"))
8862   n))
8863
8864 (defun gnus-summary-prev-thread (n)
8865   "Go to the same level previous N'th thread.
8866 Returns the difference between N and the number of skips actually
8867 done."
8868   (interactive "p")
8869   (gnus-summary-next-thread (- n)))
8870
8871 (defun gnus-summary-go-down-thread (&optional same)
8872   "Go down one level in the current thread.
8873 If SAME is non-nil, also move to articles of the same level."
8874   (let ((level (gnus-summary-thread-level))
8875         (start (point)))
8876     (if (and (zerop (forward-line 1))
8877              (> (gnus-summary-thread-level) level))
8878         t
8879       (goto-char start)
8880       nil)))
8881
8882 (defun gnus-summary-go-up-thread ()
8883   "Go up one level in the current thread."
8884   (let ((level (gnus-summary-thread-level))
8885         (start (point)))
8886     (while (and (zerop (forward-line -1))
8887                 (>= (gnus-summary-thread-level) level)))
8888     (if (>= (gnus-summary-thread-level) level)
8889         (progn
8890           (goto-char start)
8891           nil)
8892       t)))
8893
8894 (defun gnus-summary-down-thread (n)
8895   "Go down thread N steps.
8896 If N is negative, go up instead.
8897 Returns the difference between N and how many steps down that were
8898 taken."
8899   (interactive "p")
8900   (let ((up (< n 0))
8901         (n (abs n)))
8902   (while (and (> n 0)
8903               (if up (gnus-summary-go-up-thread)
8904                 (gnus-summary-go-down-thread)))
8905     (setq n (1- n)))
8906   (gnus-summary-position-cursor)
8907   (if (/= 0 n) (gnus-message 7 "Can't go further"))
8908   n))
8909
8910 (defun gnus-summary-up-thread (n)
8911   "Go up thread N steps.
8912 If N is negative, go up instead.
8913 Returns the difference between N and how many steps down that were
8914 taken."
8915   (interactive "p")
8916   (gnus-summary-down-thread (- n)))
8917
8918 (defun gnus-summary-kill-thread (unmark)
8919   "Mark articles under current thread as read.
8920 If the prefix argument is positive, remove any kinds of marks.
8921 If the prefix argument is negative, tick articles instead."
8922   (interactive "P")
8923   (if unmark
8924       (setq unmark (prefix-numeric-value unmark)))
8925   (let ((killing t)
8926         (level (gnus-summary-thread-level)))
8927     (save-excursion
8928       (while killing
8929         ;; Mark the article...
8930         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
8931               ((> unmark 0) (gnus-summary-tick-article nil t))
8932               (t (gnus-summary-tick-article)))
8933         ;; ...and go forward until either the buffer ends or the subtree
8934         ;; ends. 
8935         (if (not (and (zerop (forward-line 1))
8936                       (> (gnus-summary-thread-level) level)))
8937             (setq killing nil))))
8938     ;; Hide killed subtrees.
8939     (and (null unmark)
8940          gnus-thread-hide-killed
8941          (gnus-summary-hide-thread))
8942     ;; If marked as read, go to next unread subject.
8943     (if (null unmark)
8944         ;; Go to next unread subject.
8945         (gnus-summary-next-subject 1 t)))
8946   (gnus-set-mode-line 'summary))
8947
8948 ;; Summary sorting commands
8949
8950 (defun gnus-summary-sort-by-number (&optional reverse)
8951   "Sort summary buffer by article number.
8952 Argument REVERSE means reverse order."
8953   (interactive "P")
8954   (gnus-summary-sort 
8955    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
8956
8957 (defun gnus-summary-sort-by-author (&optional reverse)
8958   "Sort summary buffer by author name alphabetically.
8959 If case-fold-search is non-nil, case of letters is ignored.
8960 Argument REVERSE means reverse order."
8961   (interactive "P")
8962   (gnus-summary-sort
8963    (cons
8964     (lambda ()
8965       (let ((extract (funcall
8966                       gnus-extract-address-components
8967                       (header-from (gnus-get-header-by-number
8968                                     (gnus-summary-article-number))))))
8969         (or (car extract) (cdr extract))))
8970     'gnus-thread-sort-by-author)
8971    reverse))
8972
8973 (defun gnus-summary-sort-by-subject (&optional reverse)
8974   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
8975 If case-fold-search is non-nil, case of letters is ignored.
8976 Argument REVERSE means reverse order."
8977   (interactive "P")
8978   (gnus-summary-sort
8979    (cons
8980     (lambda ()
8981       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
8982     'gnus-thread-sort-by-subject)
8983    reverse))
8984
8985 (defun gnus-summary-sort-by-date (&optional reverse)
8986   "Sort summary buffer by date.
8987 Argument REVERSE means reverse order."
8988   (interactive "P")
8989   (gnus-summary-sort
8990    (cons
8991     (lambda ()
8992       (gnus-sortable-date
8993        (header-date 
8994         (gnus-get-header-by-number (gnus-summary-article-number)))))
8995     'gnus-thread-sort-by-date)
8996    reverse))
8997
8998 (defun gnus-summary-sort-by-score (&optional reverse)
8999   "Sort summary buffer by score.
9000 Argument REVERSE means reverse order."
9001   (interactive "P")
9002   (gnus-summary-sort 
9003    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9004    (not reverse)))
9005
9006 (defvar gnus-summary-already-sorted nil)
9007 (defun gnus-summary-sort (predicate reverse)
9008   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9009   (if gnus-summary-already-sorted
9010       ()
9011     (let (buffer-read-only)
9012       (if (not gnus-show-threads)
9013           (progn
9014             (goto-char (point-min))
9015             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9016         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9017               (gnus-summary-prepare-hook nil)
9018               (gnus-summary-already-sorted nil))
9019           (gnus-summary-prepare)
9020           (and gnus-show-threads
9021                gnus-thread-hide-subtree
9022                (gnus-summary-hide-all-threads)))))))
9023   
9024 (defun gnus-sortable-date (date)
9025   "Make sortable string by string-lessp from DATE.
9026 Timezone package is used."
9027   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9028          (year   (aref date 0))
9029          (month  (aref date 1))
9030          (day    (aref date 2)))
9031     (timezone-make-sortable-date 
9032      year month day 
9033      (timezone-make-time-string
9034       (aref date 3) (aref date 4) (aref date 5)))))
9035
9036
9037 ;; Summary saving commands.
9038
9039 (defun gnus-summary-save-article (n)
9040   "Save the current article using the default saver function.
9041 If N is a positive number, save the N next articles.
9042 If N is a negative number, save the N previous articles.
9043 If N is nil and any articles have been marked with the process mark,
9044 save those articles instead.
9045 The variable `gnus-default-article-saver' specifies the saver function."
9046   (interactive "P")
9047   (let ((articles (gnus-summary-work-articles n)))
9048     (while articles
9049       (let ((header (gnus-get-header-by-number (car articles))))
9050         (if (vectorp header)
9051             (progn
9052               (gnus-summary-select-article t nil nil (car articles))
9053               (or gnus-save-all-headers
9054                   (gnus-article-hide-headers t))
9055               ;; Remove any X-Gnus lines.
9056               (save-excursion
9057                 (save-restriction
9058                   (set-buffer gnus-article-buffer)
9059                   (let ((buffer-read-only nil))
9060                     (goto-char (point-min))
9061                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9062                                                   (point-max)))
9063                     (while (re-search-forward "^X-Gnus" nil t)
9064                       (beginning-of-line)
9065                       (delete-region (point)
9066                                      (progn (forward-line 1) (point))))
9067                     (widen))))
9068               (save-excursion
9069                 (if gnus-default-article-saver
9070                     (funcall gnus-default-article-saver)
9071                   (error "No default saver is defined."))))
9072           (if (assq 'name header)
9073               (gnus-copy-file (cdr (assq 'name header)))
9074             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9075       (gnus-summary-remove-process-mark (car articles))
9076       (setq articles (cdr articles)))
9077     (gnus-summary-position-cursor)
9078     n))
9079
9080 (defun gnus-summary-pipe-output (arg)
9081   "Pipe the current article to a subprocess.
9082 If N is a positive number, pipe the N next articles.
9083 If N is a negative number, pipe the N previous articles.
9084 If N is nil and any articles have been marked with the process mark,
9085 pipe those articles instead."
9086   (interactive "P")
9087   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9088     (gnus-summary-save-article arg)))
9089
9090 (defun gnus-summary-save-article-mail (arg)
9091   "Append the current article to an mail file.
9092 If N is a positive number, save the N next articles.
9093 If N is a negative number, save the N previous articles.
9094 If N is nil and any articles have been marked with the process mark,
9095 save those articles instead."
9096   (interactive "P")
9097   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9098     (gnus-summary-save-article arg)))
9099
9100 (defun gnus-summary-save-article-rmail (arg)
9101   "Append the current article to an rmail file.
9102 If N is a positive number, save the N next articles.
9103 If N is a negative number, save the N previous articles.
9104 If N is nil and any articles have been marked with the process mark,
9105 save those articles instead."
9106   (interactive "P")
9107   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9108     (gnus-summary-save-article arg)))
9109
9110 (defun gnus-summary-save-article-file (arg)
9111   "Append the current article to a file.
9112 If N is a positive number, save the N next articles.
9113 If N is a negative number, save the N previous articles.
9114 If N is nil and any articles have been marked with the process mark,
9115 save those articles instead."
9116   (interactive "P")
9117   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9118     (gnus-summary-save-article arg)))
9119
9120 (defun gnus-summary-save-article-folder (arg)
9121   "Append the current article to an mh folder.
9122 If N is a positive number, save the N next articles.
9123 If N is a negative number, save the N previous articles.
9124 If N is nil and any articles have been marked with the process mark,
9125 save those articles instead."
9126   (interactive "P")
9127   (let ((gnus-default-article-saver 'gnus-summary-save-in-folder))
9128     (gnus-summary-save-article arg)))
9129
9130 (defun gnus-summary-save-article-vm (arg)
9131   "Append the current article to a vm folder.
9132 If N is a positive number, save the N next articles.
9133 If N is a negative number, save the N previous articles.
9134 If N is nil and any articles have been marked with the process mark,
9135 save those articles instead."
9136   (interactive "P")
9137   (let ((gnus-default-article-saver 'gnus-summary-save-in-vm))
9138     (gnus-summary-save-article arg)))
9139
9140 (defun gnus-read-save-file-name (prompt default-name)
9141   (let ((methods gnus-split-methods)
9142         split-name)
9143     (if (not gnus-split-methods)
9144         ()
9145       (save-excursion
9146         (set-buffer gnus-article-buffer)
9147         (gnus-narrow-to-headers)
9148         (while methods
9149           (goto-char (point-min))
9150           (and (condition-case () 
9151                    (re-search-forward (car (car methods)) nil t)
9152                  (error nil))
9153                (setq split-name (cons (nth 1 (car methods)) split-name)))
9154           (setq methods (cdr methods)))
9155         (widen)))
9156     (cond ((null split-name)
9157            (read-file-name
9158             (concat prompt " (default "
9159                     (file-name-nondirectory default-name) ") ")
9160             (file-name-directory default-name)
9161             default-name))
9162           ((= 1 (length split-name))
9163            (read-file-name
9164             (concat prompt " (default " (car split-name) ") ")
9165             gnus-article-save-directory
9166             (concat gnus-article-save-directory (car split-name))))
9167           (t
9168            (setq split-name (mapcar (lambda (el) (list el))
9169                                     (nreverse split-name)))
9170            (let ((result (completing-read 
9171                           (concat prompt " ")
9172                           split-name nil nil)))
9173              (concat gnus-article-save-directory
9174                      (if (string= result "")
9175                          (car (car split-name))
9176                        result)))))))
9177
9178 (defun gnus-summary-save-in-rmail (&optional filename)
9179   "Append this article to Rmail file.
9180 Optional argument FILENAME specifies file name.
9181 Directory to save to is default to `gnus-article-save-directory' which
9182 is initialized from the SAVEDIR environment variable."
9183   (interactive)
9184   (let ((default-name
9185           (funcall gnus-rmail-save-name gnus-newsgroup-name
9186                    gnus-current-headers gnus-newsgroup-last-rmail)))
9187     (or filename
9188         (setq filename (gnus-read-save-file-name 
9189                         "Save in rmail file:" default-name)))
9190     (gnus-make-directory (file-name-directory filename))
9191     (gnus-eval-in-buffer-window 
9192      gnus-article-buffer
9193      (save-excursion
9194        (save-restriction
9195          (widen)
9196          (gnus-output-to-rmail filename))))
9197     ;; Remember the directory name to save articles.
9198     (setq gnus-newsgroup-last-rmail filename)))
9199
9200 (defun gnus-summary-save-in-mail (&optional filename)
9201   "Append this article to Unix mail file.
9202 Optional argument FILENAME specifies file name.
9203 Directory to save to is default to `gnus-article-save-directory' which
9204 is initialized from the SAVEDIR environment variable."
9205   (interactive)
9206   (let ((default-name
9207           (funcall gnus-mail-save-name gnus-newsgroup-name
9208                    gnus-current-headers gnus-newsgroup-last-mail)))
9209     (or filename
9210         (setq filename (gnus-read-save-file-name 
9211                         "Save in Unix mail file:" default-name)))
9212     (setq filename
9213           (expand-file-name filename
9214                             (and default-name
9215                                  (file-name-directory default-name))))
9216     (gnus-make-directory (file-name-directory filename))
9217     (gnus-eval-in-buffer-window 
9218      gnus-article-buffer
9219      (save-excursion
9220        (save-restriction
9221          (widen)
9222          (if (and (file-readable-p filename) (rmail-file-p filename))
9223              (gnus-output-to-rmail filename)
9224            (rmail-output filename 1 t t)))))
9225     ;; Remember the directory name to save articles.
9226     (setq gnus-newsgroup-last-mail filename)))
9227
9228 (defun gnus-summary-save-in-file (&optional filename)
9229   "Append this article to file.
9230 Optional argument FILENAME specifies file name.
9231 Directory to save to is default to `gnus-article-save-directory' which
9232 is initialized from the SAVEDIR environment variable."
9233   (interactive)
9234   (let ((default-name
9235           (funcall gnus-file-save-name gnus-newsgroup-name
9236                    gnus-current-headers gnus-newsgroup-last-file)))
9237     (or filename
9238         (setq filename (gnus-read-save-file-name 
9239                         "Save in file:" default-name)))
9240     (gnus-make-directory (file-name-directory filename))
9241     (gnus-eval-in-buffer-window 
9242      gnus-article-buffer
9243      (save-excursion
9244        (save-restriction
9245          (widen)
9246          (gnus-output-to-file filename))))
9247     ;; Remember the directory name to save articles.
9248     (setq gnus-newsgroup-last-file filename)))
9249
9250 (defun gnus-summary-save-in-pipe (&optional command)
9251   "Pipe this article to subprocess."
9252   (interactive)
9253   (let ((command (read-string "Shell command on article: "
9254                               gnus-last-shell-command)))
9255     (if (string-equal command "")
9256         (setq command gnus-last-shell-command))
9257     (gnus-eval-in-buffer-window 
9258      gnus-article-buffer
9259      (save-restriction
9260        (widen)
9261        (shell-command-on-region (point-min) (point-max) command nil)))
9262     (setq gnus-last-shell-command command)))
9263
9264 ;; Summary extract commands
9265
9266 (defun gnus-summary-insert-pseudos (pslist)
9267   (let ((buffer-read-only nil)
9268         (article (gnus-summary-article-number))
9269         b)
9270     (or (gnus-summary-goto-subject article)
9271         (error (format "No such article: %d" article)))
9272     (or gnus-newsgroup-headers-hashtb-by-number
9273         (gnus-make-headers-hashtable-by-number))
9274     (gnus-summary-position-cursor)
9275     ;; If all commands are to be bunched up on one line, we collect
9276     ;; them here.  
9277     (if gnus-view-pseudos-separately
9278         ()
9279       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9280             files action)
9281         (while ps
9282           (setq action (cdr (assq 'action (car ps))))
9283           (setq files (list (cdr (assq 'name (car ps)))))
9284           (while (and ps (cdr ps)
9285                       (string= (or action "1")
9286                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9287             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9288             (setcdr ps (cdr (cdr ps))))
9289           (if (not files)
9290               ()
9291             (if (not (string-match "%s" action))
9292                 (setq files (cons " " files)))
9293             (setq files (cons " " files))
9294             (and (assq 'execute (car ps))
9295                  (setcdr (assq 'execute (car ps))
9296                          (funcall (if (string-match "%s" action)
9297                                       'format 'concat)
9298                                   action 
9299                                   (mapconcat (lambda (f) f) files " ")))))
9300           (setq ps (cdr ps)))))
9301     (if gnus-view-pseudos
9302         (while pslist
9303           (and (assq 'execute (car pslist))
9304                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9305                                      (eq gnus-view-pseudos 'not-confirm)))
9306           (setq pslist (cdr pslist)))
9307       (save-excursion
9308         (while pslist
9309           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9310                                          (gnus-summary-article-number)))
9311           (forward-line 1)
9312           (setq b (point))
9313           (insert "          " (file-name-nondirectory 
9314                                 (cdr (assq 'name (car pslist))))
9315                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9316           (add-text-properties 
9317            b (1+ b) (list 'gnus (list gnus-reffed-article-number
9318                                       gnus-unread-mark 0)
9319                           'gnus-pseudo (car pslist)))
9320           (forward-line -1)
9321           (gnus-sethash (int-to-string gnus-reffed-article-number)
9322                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9323           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9324           (setq pslist (cdr pslist)))))))
9325
9326 (defun gnus-pseudos< (p1 p2)
9327   (let ((c1 (cdr (assq 'action p1)))
9328         (c2 (cdr (assq 'action p2))))
9329     (and c1 c2 (string< c1 c2))))
9330
9331 (defun gnus-request-pseudo-article (props)
9332   (cond ((assq 'execute props)
9333          (gnus-execute-command (cdr (assq 'execute props)))))
9334   (let ((gnus-current-article (gnus-summary-article-number)))
9335     (run-hooks 'gnus-mark-article-hook)))
9336
9337 (defun gnus-execute-command (command &optional automatic)
9338   (save-excursion
9339     (gnus-article-setup-buffer)
9340     (set-buffer gnus-article-buffer)
9341     (let ((command (if automatic command (read-string "Command: " command)))
9342           (buffer-read-only nil))
9343       (erase-buffer)
9344       (insert "$ " command "\n\n")
9345       (if gnus-view-pseudo-asynchronously
9346           (start-process "gnus-execute" nil "sh" "-c" command)
9347         (call-process "sh" nil t nil "-c" command)))))
9348
9349 (defun gnus-copy-file (file &optional to)
9350   "Copy FILE to TO."
9351   (interactive
9352    (list (read-file-name "Copy file: " default-directory)
9353          (read-file-name "Copy file to: " default-directory)))
9354   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9355   (and (file-directory-p to) 
9356        (setq to (concat (file-name-as-directory to)
9357                         (file-name-nondirectory file))))
9358   (copy-file file to))
9359
9360 ;; Summary kill commands.
9361
9362 (defun gnus-summary-edit-global-kill (article)
9363   "Edit the \"global\" kill file."
9364   (interactive (list (gnus-summary-article-number)))
9365   (gnus-group-edit-global-kill article))
9366
9367 (defun gnus-summary-edit-local-kill ()
9368   "Edit a local kill file applied to the current newsgroup."
9369   (interactive)
9370   (setq gnus-current-headers 
9371         (gnus-gethash 
9372          (int-to-string (gnus-summary-article-number))
9373          gnus-newsgroup-headers-hashtb-by-number))
9374   (gnus-set-global-variables)
9375   (gnus-group-edit-local-kill 
9376    (gnus-summary-article-number) gnus-newsgroup-name))
9377
9378 \f
9379 ;;;
9380 ;;; Gnus article mode
9381 ;;;
9382
9383 (put 'gnus-article-mode 'mode-class 'special)
9384
9385 (if gnus-article-mode-map
9386     nil
9387   (setq gnus-article-mode-map (make-keymap))
9388   (suppress-keymap gnus-article-mode-map)
9389   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9390   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9391   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9392   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9393   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9394   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9395   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9396   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9397   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9398   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9399   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9400   
9401   ;; Duplicate almost all summary keystrokes in the article mode map.
9402   (let ((commands 
9403          (list "#" "\M-#" "\C-c\M-#" "n" "p"
9404                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9405                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9406                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k""x" "X" 
9407                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9408                "t" "\M-t" "C" "S"
9409                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9410                "V" "\C-c\C-d")))
9411     (while commands
9412       (define-key gnus-article-mode-map (car commands) 
9413         'gnus-article-summary-command)
9414       (setq commands (cdr commands))))
9415
9416   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9417     (while commands
9418       (define-key gnus-article-mode-map (car commands) 
9419         'gnus-article-summary-command-nosave)
9420       (setq commands (cdr commands)))))
9421
9422
9423 (defun gnus-article-mode ()
9424   "Major mode for displaying an article.
9425
9426 All normal editing commands are switched off.
9427
9428 The following commands are available:
9429
9430 \\<gnus-article-mode-map>
9431 \\[gnus-article-next-page]\t Scroll the article one page forwards
9432 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9433 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9434 \\[gnus-article-show-summary]\t Display the summary buffer
9435 \\[gnus-article-mail]\t Send a reply to the address near point
9436 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9437 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9438 \\[gnus-info-find-node]\t Go to the Gnus info node"
9439   (interactive)
9440   (if gnus-visual (gnus-article-make-menu-bar))
9441   (kill-all-local-variables)
9442   (setq mode-line-modified "-- ")
9443   (make-local-variable 'mode-line-format)
9444   (setq mode-line-format (copy-sequence mode-line-format))
9445   (and (equal (nth 3 mode-line-format) "   ")
9446        (setcar (nthcdr 3 mode-line-format) ""))
9447   (setq mode-name "Article")
9448   (setq major-mode 'gnus-article-mode)
9449   (make-local-variable 'minor-mode-alist)
9450   (or (assq 'gnus-show-mime minor-mode-alist)
9451       (setq minor-mode-alist
9452             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9453   (use-local-map gnus-article-mode-map)
9454   (make-local-variable 'page-delimiter)
9455   (setq page-delimiter gnus-page-delimiter)
9456   (buffer-disable-undo (current-buffer))
9457   (setq buffer-read-only t)             ;Disable modification
9458   (run-hooks 'gnus-article-mode-hook))
9459
9460 (defun gnus-article-setup-buffer ()
9461   "Initialize article mode buffer."
9462   (or (get-buffer gnus-article-buffer)
9463       (save-excursion
9464         (set-buffer (get-buffer-create gnus-article-buffer))
9465         (gnus-add-current-to-buffer-list)
9466         (gnus-article-mode))))
9467
9468 (defun gnus-request-article-this-buffer (article group)
9469   "Get an article and insert it into this buffer."
9470   (setq group (or group gnus-newsgroup-name))
9471
9472   ;; Open server if it has closed.
9473   (gnus-check-news-server (gnus-find-method-for-group group))
9474
9475   ;; Using `gnus-request-article' directly will insert the article into
9476   ;; `nntp-server-buffer' - so we'll save some time by not having to
9477   ;; copy it from the server buffer into the article buffer.
9478
9479   ;; We only request an article by message-id when we do not have the
9480   ;; headers for it, so we'll have to get those.
9481   (and (stringp article) 
9482        (let ((gnus-override-method gnus-refer-article-method))
9483          (gnus-read-header article)))
9484
9485   ;; If the article number is negative, that means that this article
9486   ;; doesn't belong in this newsgroup (possibly), so we find its
9487   ;; message-id and request it by id instead of number.
9488   (if (and (numberp article) (< article 0))
9489       (save-excursion
9490         (set-buffer gnus-summary-buffer)
9491         (let ((header (gnus-gethash (int-to-string article)
9492                                     gnus-newsgroup-headers-hashtb-by-number)))
9493           (if (vectorp header)
9494               ;; It's a real article.
9495               (setq article (header-id header))
9496             ;; It is an extracted pseudo-article.
9497             (setq article nil)
9498             (gnus-request-pseudo-article header)))))
9499
9500   ;; Check the cache.
9501   (if (and gnus-use-cache
9502            (numberp article)
9503            (gnus-cache-request-article article group))
9504       'article
9505     ;; Get the article and into the article buffer.
9506     (if article
9507         (progn
9508           (erase-buffer)
9509           (let ((gnus-override-method 
9510                  (and (stringp article) gnus-refer-article-method)))
9511             (and (gnus-request-article article group (current-buffer))
9512                  'article)))
9513       'pseudo)))
9514
9515 (defun gnus-read-header (id)
9516   "Read the headers of article ID and enter them into the Gnus system."
9517   (or gnus-newsgroup-headers-hashtb-by-number
9518       (gnus-make-headers-hashtable-by-number))
9519   (let (header)
9520     (if (not (setq header 
9521                    (car (if (let ((gnus-nov-is-evil t))
9522                               (gnus-retrieve-headers 
9523                                (list id) gnus-newsgroup-name))
9524                             (gnus-get-newsgroup-headers)))))
9525         nil
9526       (if (stringp id)
9527           (header-set-number header gnus-reffed-article-number))
9528       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
9529       (gnus-sethash (int-to-string (header-number header)) header
9530                     gnus-newsgroup-headers-hashtb-by-number)
9531       (if (stringp id)
9532           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
9533       (setq gnus-current-headers header)
9534       header)))
9535
9536 (defun gnus-article-prepare (article &optional all-headers header)
9537   "Prepare ARTICLE in article mode buffer.
9538 ARTICLE should either be an article number or a Message-ID.
9539 If ARTICLE is an id, HEADER should be the article headers.
9540 If ALL-HEADERS is non-nil, no headers are hidden."
9541   (save-excursion
9542     ;; Make sure we start in a summary buffer.
9543     (or (eq major-mode 'gnus-summary-mode)
9544         (set-buffer gnus-summary-buffer))
9545     (setq gnus-summary-buffer (current-buffer))
9546     ;; Make sure the connection to the server is alive.
9547     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
9548         (progn
9549           (gnus-check-news-server 
9550            (gnus-find-method-for-group gnus-newsgroup-name))
9551           (gnus-request-group gnus-newsgroup-name t)))
9552     (or gnus-newsgroup-headers-hashtb-by-number
9553         (gnus-make-headers-hashtable-by-number))
9554     (let* ((article (if header (header-number header) article))
9555            (summary-buffer (current-buffer))
9556            (internal-hook gnus-article-internal-prepare-hook)
9557            (group gnus-newsgroup-name)
9558            result)
9559       (save-excursion
9560         (gnus-article-setup-buffer)
9561         (set-buffer gnus-article-buffer)
9562         (if (not (setq result (let ((buffer-read-only nil))
9563                                 (gnus-request-article-this-buffer 
9564                                  article group))))
9565             ;; There is no such article.
9566             (progn
9567               (save-excursion
9568                 (set-buffer gnus-summary-buffer)
9569                 (setq gnus-current-article 0)
9570                 (and (numberp article) 
9571                      (gnus-summary-mark-as-read article gnus-canceled-mark))
9572                 (gnus-message 1 "No such article (may be canceled)")
9573                 (and (numberp article)
9574                      (setq gnus-current-article article))
9575                 (ding))
9576               (and (numberp article)
9577                    (setq gnus-article-current 
9578                          (cons gnus-newsgroup-name article)))
9579               nil)
9580           (if (not (eq result 'article))
9581               (progn
9582                 (save-excursion
9583                   (set-buffer summary-buffer)
9584                   (setq gnus-last-article gnus-current-article
9585                         gnus-newsgroup-history (cons gnus-current-article
9586                                                      gnus-newsgroup-history)
9587                         gnus-current-article 0
9588                         gnus-current-headers nil
9589                         gnus-article-current nil)
9590                   (gnus-configure-windows 'article)
9591                   (gnus-set-global-variables))
9592                 (gnus-set-mode-line 'article))
9593             ;; The result from the `request' was an actual article -
9594             ;; or at least some text that is now displayed in the
9595             ;; article buffer.
9596             (if (and (numberp article)
9597                      (not (eq article gnus-current-article)))
9598                 ;; Seems like a new article has been selected.
9599                 ;; `gnus-current-article' must be an article number.
9600                 (save-excursion
9601                   (set-buffer summary-buffer)
9602                   (setq gnus-last-article gnus-current-article
9603                         gnus-newsgroup-history (cons gnus-current-article
9604                                                      gnus-newsgroup-history)
9605                         gnus-current-article article
9606                         gnus-current-headers 
9607                         (gnus-get-header-by-number gnus-current-article)
9608                         gnus-article-current 
9609                         (cons gnus-newsgroup-name gnus-current-article))
9610                   (run-hooks 'gnus-mark-article-hook)
9611                   (gnus-set-mode-line 'summary)
9612                   (and gnus-visual 
9613                        (run-hooks 'gnus-visual-mark-article-hook))
9614                   ;; Set the global newsgroup variables here.
9615                   ;; Suggested by Jim Sisolak
9616                   ;; <sisolak@trans4.neep.wisc.edu>.
9617                   (gnus-set-global-variables)
9618                   (and gnus-use-cache 
9619                        (gnus-cache-possibly-enter-article
9620                         group article
9621                         (gnus-get-header-by-number article)
9622                         (memq article gnus-newsgroup-marked)
9623                         (memq article gnus-newsgroup-dormant)
9624                         (memq article gnus-newsgroup-unreads)))))
9625             ;; gnus-have-all-headers must be either T or NIL.
9626             (setq gnus-have-all-headers
9627                   (not (not (or all-headers gnus-show-all-headers))))
9628             ;; Hooks for getting information from the article.
9629             ;; This hook must be called before being narrowed.
9630             (run-hooks 'internal-hook)
9631             (run-hooks 'gnus-article-prepare-hook)
9632             ;; Decode MIME message.
9633             (if (and gnus-show-mime
9634                      (gnus-fetch-field "Mime-Version"))
9635                 (funcall gnus-show-mime-method))
9636             ;; Perform the article display hooks.
9637             (let ((buffer-read-only nil))
9638               (run-hooks 'gnus-article-display-hook))
9639             ;; Do page break.
9640             (goto-char (point-min))
9641             (and gnus-break-pages (gnus-narrow-to-page))
9642             (gnus-set-mode-line 'article)
9643             (gnus-configure-windows 'article)
9644             (goto-char (point-min))
9645             t))))))
9646
9647 (defun gnus-article-show-all-headers ()
9648   "Show all article headers in article mode buffer."
9649   (save-excursion 
9650     (gnus-article-setup-buffer)
9651     (set-buffer gnus-article-buffer)
9652     (let ((buffer-read-only nil))
9653       (remove-text-properties (point-min) (point-max) 
9654                               gnus-hidden-properties))))
9655
9656 (defun gnus-article-hide-headers-if-wanted ()
9657   "Hide unwanted headers if `gnus-have-all-headers' is nil.
9658 Provided for backwards compatability."
9659   (or gnus-have-all-headers
9660       (gnus-article-hide-headers)))
9661
9662 (defun gnus-article-hide-headers (&optional delete)
9663   "Hide unwanted headers and possibly sort them as well."
9664   (interactive "P")
9665   (save-excursion
9666     (set-buffer gnus-article-buffer)
9667     (save-restriction
9668       (let ((sorted gnus-sorted-header-list)
9669             (buffer-read-only nil)
9670             want want-list beg want-l)
9671         ;; First we narrow to just the headers.
9672         (widen)
9673         (goto-char (point-min))
9674         ;; Hide any "From " lines at the beginning of (mail) articles. 
9675         (while (looking-at "From ")
9676           (forward-line 1))
9677         (if (bobp) 
9678             (add-text-properties (point-min) (point) gnus-hidden-properties))
9679         ;; Then treat the rest of the header lines.
9680         (narrow-to-region 
9681          (point) 
9682          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
9683         ;; Then we use the two regular expressions
9684         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
9685         ;; select which header lines is to remain visible in the
9686         ;; article buffer.
9687         (goto-char (point-min))
9688         (while (re-search-forward "^[^ \t]*:" nil t)
9689           (beginning-of-line)
9690           ;; We add the headers we want to keep to a list and delete
9691           ;; them from the buffer.
9692           (if (or (and (stringp gnus-visible-headers)
9693                        (looking-at gnus-visible-headers))
9694                   (and (not (stringp gnus-visible-headers))
9695                        (stringp gnus-ignored-headers)
9696                        (not (looking-at gnus-ignored-headers))))
9697               (progn
9698                 (setq beg (point))
9699                 (forward-line 1)
9700                 ;; Be sure to get multi-line headers...
9701                 (re-search-forward "^[^ \t]*:" nil t)
9702                 (beginning-of-line)
9703                 (setq want-list 
9704                       (cons (buffer-substring beg (point)) want-list))
9705                 (delete-region beg (point))
9706                 (goto-char beg))
9707             (forward-line 1)))
9708         ;; Next we perform the sorting by looking at
9709         ;; `gnus-sorted-header-list'. 
9710         (goto-char (point-min))
9711         (while (and sorted want-list)
9712           (setq want-l want-list)
9713           (while (and want-l
9714                       (not (string-match (car sorted) (car want-l))))
9715             (setq want-l (cdr want-l)))
9716           (if want-l 
9717               (progn
9718                 (insert (car want-l))
9719                 (setq want-list (delq (car want-l) want-list))))
9720           (setq sorted (cdr sorted)))
9721         ;; Any headers that were not matched by the sorted list we
9722         ;; just tack on the end of the visible header list.
9723         (while want-list
9724           (insert (car want-list))
9725           (setq want-list (cdr want-list)))
9726         ;; And finally we make the unwanted headers invisible.
9727         (if delete
9728             (delete-region (point) (point-max))
9729           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
9730           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
9731
9732 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
9733 (defun gnus-article-treat-overstrike ()
9734   "Translate overstrikes into bold text."
9735   (interactive)
9736   (save-excursion
9737     (set-buffer gnus-article-buffer)
9738     (let ((buffer-read-only nil))
9739       (while (search-forward "\b" nil t)
9740         (let ((next (following-char))
9741               (previous (char-after (- (point) 2))))
9742           (cond ((eq next previous)
9743                  (delete-region (- (point) 2) (point))
9744                  (put-text-property (point) (1+ (point))
9745                                     'face 'bold))
9746                 ((eq next ?_)
9747                  (delete-region (1- (point)) (1+ (point)))
9748                  (put-text-property (1- (point)) (point)
9749                                     'face 'underline))
9750                 ((eq previous ?_)
9751                  (delete-region (- (point) 2) (point))
9752                  (put-text-property (point) (1+ (point))
9753                                     'face 'underline))))))))
9754
9755 (defun gnus-article-word-wrap ()
9756   "Format too long lines."
9757   (interactive)
9758   (save-excursion
9759     (set-buffer gnus-article-buffer)
9760     (let ((buffer-read-only nil))
9761       (goto-char (point-min))
9762       (search-forward "\n\n" nil t)
9763       (end-of-line 1)
9764       (let ((paragraph-start "^\\W"))
9765         (while (not (eobp))
9766           (and (>= (current-column) (window-width))
9767                (/= (preceding-char) ?:)
9768                (fill-paragraph nil))
9769           (end-of-line 2))))))
9770
9771 (defun gnus-article-remove-cr ()
9772   "Remove carriage returns from an article."
9773   (interactive)
9774   (save-excursion
9775     (set-buffer gnus-article-buffer)
9776     (let ((buffer-read-only nil))
9777       (goto-char (point-min))
9778       (while (search-forward "\r" nil t)
9779         (replace-match "" t t)))))
9780
9781 (defun gnus-article-display-x-face (&optional force)
9782   "Look for an X-Face header and display it if present."
9783   (interactive (list 'force))
9784   (save-excursion
9785     (set-buffer gnus-article-buffer)
9786     (let ((inhibit-point-motion-hooks t)
9787           (case-fold-search nil))
9788       (save-restriction
9789         (goto-char (point-min))
9790         (search-forward "\n\n")
9791         (narrow-to-region (point-min) (point))
9792         (goto-char (point-min))
9793         (if (or (not gnus-article-x-face-command)
9794                 (and (not force)
9795                      (or (not gnus-article-x-face-too-ugly)
9796                          (string-match gnus-article-x-face-too-ugly
9797                                        (mail-fetch-field "from"))))
9798                 (progn
9799                   (goto-char (point-min))
9800                   (not (re-search-forward "^X-Face: " nil t))))
9801             nil
9802           (let ((beg (point))
9803                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
9804             (if (symbolp gnus-article-x-face-command)
9805                 (and (or (fboundp gnus-article-x-face-command)
9806                          (error "%s is not a function"
9807                                 gnus-article-x-face-command))
9808                      (funcall gnus-article-x-face-command beg end))
9809               (call-process-region beg end "sh" nil 0 nil
9810                                    "-c" gnus-article-x-face-command))))))))
9811
9812 (defun gnus-article-de-quoted-unreadable (&optional force)
9813   "Do a naïve translation of a quoted-printable-encoded article.
9814 This is in no way, shape or form meant as a replacement for real MIME
9815 processing, but is simply a stop-gap measure until MIME support is
9816 written.
9817 If FORCE, decode the article whether it is marked as quoted-printable
9818 or not." 
9819   (interactive (list 'force))
9820   (save-excursion
9821     (set-buffer gnus-article-buffer)
9822     (let ((case-fold-search t)
9823           (buffer-read-only nil)
9824           (type (gnus-fetch-field "content-transfer-encoding")))
9825       (if (or force (and type (string-match "quoted-printable" type)))
9826           (progn
9827             (goto-char (point-min))
9828             (search-forward "\n\n" nil 'move)
9829             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
9830
9831 (defun gnus-mime-decode-quoted-printable (from to)
9832   ;; Decode quoted-printable from region between FROM and TO.
9833   (save-excursion
9834     (goto-char from)
9835     (while (search-forward "=" to t)
9836       (cond ((eq (following-char) ?\n)
9837              (delete-char -1)
9838              (delete-char 1))
9839             ((looking-at "[0-9A-F][0-9A-F]")
9840              (delete-char -1)
9841              (insert (hexl-hex-string-to-integer
9842                       (buffer-substring (point) (+ 2 (point)))))
9843              (delete-char 2))
9844             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
9845
9846 (defun gnus-article-date-ut (&optional type)
9847   "Convert DATE date to universal time in the current article.
9848 If TYPE is `local', convert to local time; if it is `lapsed', output
9849 how much time has lapsed since DATE."
9850   (interactive (list 'ut))
9851   (let ((date (header-date (or gnus-current-headers 
9852                                (gnus-get-header-by-number
9853                                 (gnus-summary-article-number)))))
9854         (date-regexp "^Date: \\|^X-Sent: "))
9855     (save-excursion
9856       (set-buffer gnus-article-buffer)
9857       (let ((buffer-read-only nil))
9858         (goto-char (point-min))
9859         (if (and (re-search-forward date-regexp nil t)
9860                  (progn 
9861                    (beginning-of-line)
9862                    (looking-at date-regexp)))
9863             (delete-region (gnus-point-at-bol)
9864                            (progn (end-of-line) (1+ (point))))
9865           (goto-char (point-min))
9866           (goto-char (- (search-forward "\n\n") 2)))
9867         (insert
9868          (cond 
9869           ((eq type 'local)
9870            (concat "Date: " (timezone-make-date-arpa-standard date) "\n"))
9871           ((eq type 'ut)
9872            (concat "Date: " (timezone-make-date-arpa-standard date nil "UT")
9873                    "\n"))
9874           ((eq type 'lapsed)
9875            (let* ((sec (- (gnus-seconds-since-epoch 
9876                            (timezone-make-date-arpa-standard
9877                             (current-time-string) (current-time-zone) "UT"))
9878                           (gnus-seconds-since-epoch 
9879                            (timezone-make-date-arpa-standard date nil "UT"))))
9880                   (units (list (cons 'year (* 365.25 24 60 60))
9881                                (cons 'week (* 7 24 60 60))
9882                                (cons 'day (* 24 60 60))
9883                                (cons 'hour (* 60 60))
9884                                (cons 'minute 60)
9885                                (cons 'second 1)))
9886                   num prev)
9887              (concat
9888               "X-Sent: "
9889               (mapconcat 
9890                (lambda (unit)
9891                  (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
9892                      ""
9893                    (setq sec (- sec (* num (cdr unit))))
9894                    (prog1
9895                        (concat (if prev ", " "") (int-to-string (floor num))
9896                                " " (symbol-name (car unit))
9897                                (if (> num 1) "s" ""))
9898                      (setq prev t))))
9899                units "")
9900               " ago\n")))
9901           (t
9902            (error "Unknown conversion type: %s" type))))))))
9903
9904 (defun gnus-article-date-local ()
9905   "Convert the current article date to the local timezone."
9906   (interactive)
9907   (gnus-article-date-ut 'local))
9908
9909 (defun gnus-article-date-lapsed ()
9910   "Convert the current article date to time lapsed since it was sent."
9911   (interactive)
9912   (gnus-article-date-ut 'lapsed))
9913
9914 (defun gnus-article-maybe-highlight ()
9915   (if gnus-visual (gnus-article-highlight)))
9916
9917 ;; Article savers.
9918
9919 (defun gnus-output-to-rmail (file-name)
9920   "Append the current article to an Rmail file named FILE-NAME."
9921   (require 'rmail)
9922   ;; Most of these codes are borrowed from rmailout.el.
9923   (setq file-name (expand-file-name file-name))
9924   (setq rmail-default-rmail-file file-name)
9925   (let ((artbuf (current-buffer))
9926         (tmpbuf (get-buffer-create " *Gnus-output*")))
9927     (save-excursion
9928       (or (get-file-buffer file-name)
9929           (file-exists-p file-name)
9930           (if (gnus-yes-or-no-p
9931                (concat "\"" file-name "\" does not exist, create it? "))
9932               (let ((file-buffer (create-file-buffer file-name)))
9933                 (save-excursion
9934                   (set-buffer file-buffer)
9935                   (rmail-insert-rmail-file-header)
9936                   (let ((require-final-newline nil))
9937                     (write-region (point-min) (point-max) file-name t 1)))
9938                 (kill-buffer file-buffer))
9939             (error "Output file does not exist")))
9940       (set-buffer tmpbuf)
9941       (buffer-disable-undo (current-buffer))
9942       (erase-buffer)
9943       (insert-buffer-substring artbuf)
9944       (gnus-convert-article-to-rmail)
9945       ;; Decide whether to append to a file or to an Emacs buffer.
9946       (let ((outbuf (get-file-buffer file-name)))
9947         (if (not outbuf)
9948             (append-to-file (point-min) (point-max) file-name)
9949           ;; File has been visited, in buffer OUTBUF.
9950           (set-buffer outbuf)
9951           (let ((buffer-read-only nil)
9952                 (msg (and (boundp 'rmail-current-message)
9953                           (symbol-value 'rmail-current-message))))
9954             ;; If MSG is non-nil, buffer is in RMAIL mode.
9955             (if msg
9956                 (progn (widen)
9957                        (narrow-to-region (point-max) (point-max))))
9958             (insert-buffer-substring tmpbuf)
9959             (if msg
9960                 (progn
9961                   (goto-char (point-min))
9962                   (widen)
9963                   (search-backward "\^_")
9964                   (narrow-to-region (point) (point-max))
9965                   (goto-char (1+ (point-min)))
9966                   (rmail-count-new-messages t)
9967                   (rmail-show-message msg)))))))
9968     (kill-buffer tmpbuf)))
9969
9970 (defun gnus-output-to-file (file-name)
9971   "Append the current article to a file named FILE-NAME."
9972   (setq file-name (expand-file-name file-name))
9973   (let ((artbuf (current-buffer))
9974         (tmpbuf (get-buffer-create " *Gnus-output*")))
9975     (save-excursion
9976       (set-buffer tmpbuf)
9977       (buffer-disable-undo (current-buffer))
9978       (erase-buffer)
9979       (insert-buffer-substring artbuf)
9980       ;; Append newline at end of the buffer as separator, and then
9981       ;; save it to file.
9982       (goto-char (point-max))
9983       (insert "\n")
9984       (append-to-file (point-min) (point-max) file-name))
9985     (kill-buffer tmpbuf)))
9986
9987 (defun gnus-convert-article-to-rmail ()
9988   "Convert article in current buffer to Rmail message format."
9989   (let ((buffer-read-only nil))
9990     ;; Convert article directly into Babyl format.
9991     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
9992     (goto-char (point-min))
9993     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
9994     (while (search-forward "\n\^_" nil t) ;single char
9995       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
9996     (goto-char (point-max))
9997     (insert "\^_")))
9998
9999 (defun gnus-narrow-to-page (&optional arg)
10000   "Make text outside current page invisible except for page delimiter.
10001 A numeric arg specifies to move forward or backward by that many pages,
10002 thus showing a page other than the one point was originally in."
10003   (interactive "P")
10004   (setq arg (if arg (prefix-numeric-value arg) 0))
10005   (save-excursion
10006     (forward-page -1)                   ;Beginning of current page.
10007     (widen)
10008     (if (> arg 0)
10009         (forward-page arg)
10010       (if (< arg 0)
10011           (forward-page (1- arg))))
10012     ;; Find the end of the page.
10013     (forward-page)
10014     ;; If we stopped due to end of buffer, stay there.
10015     ;; If we stopped after a page delimiter, put end of restriction
10016     ;; at the beginning of that line.
10017     ;; These are commented out.
10018     ;;    (if (save-excursion (beginning-of-line)
10019     ;;                  (looking-at page-delimiter))
10020     ;;  (beginning-of-line))
10021     (narrow-to-region (point)
10022                       (progn
10023                         ;; Find the top of the page.
10024                         (forward-page -1)
10025                         ;; If we found beginning of buffer, stay there.
10026                         ;; If extra text follows page delimiter on same line,
10027                         ;; include it.
10028                         ;; Otherwise, show text starting with following line.
10029                         (if (and (eolp) (not (bobp)))
10030                             (forward-line 1))
10031                         (point)))))
10032
10033 (defun gnus-gmt-to-local ()
10034   "Rewrite Date header described in GMT to local in current buffer.
10035 Intended to be used with gnus-article-prepare-hook."
10036   (save-excursion
10037     (save-restriction
10038       (widen)
10039       (goto-char (point-min))
10040       (narrow-to-region (point-min)
10041                         (progn (search-forward "\n\n" nil 'move) (point)))
10042       (goto-char (point-min))
10043       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10044           (let ((buffer-read-only nil)
10045                 (date (buffer-substring (match-beginning 1) (match-end 1))))
10046             (delete-region (match-beginning 1) (match-end 1))
10047             (insert
10048              (timezone-make-date-arpa-standard 
10049               date nil (current-time-zone))))))))
10050
10051
10052 ;; Article mode commands
10053
10054 (defun gnus-article-next-page (lines)
10055   "Show next page of current article.
10056 If end of article, return non-nil. Otherwise return nil.
10057 Argument LINES specifies lines to be scrolled up."
10058   (interactive "P")
10059   (move-to-window-line -1)
10060   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10061   (if (save-excursion
10062         (end-of-line)
10063         (and (pos-visible-in-window-p)  ;Not continuation line.
10064              (eobp)))
10065       ;; Nothing in this page.
10066       (if (or (not gnus-break-pages)
10067               (save-excursion
10068                 (save-restriction
10069                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10070           t                             ;Nothing more.
10071         (gnus-narrow-to-page 1)         ;Go to next page.
10072         nil)
10073     ;; More in this page.
10074     (condition-case ()
10075         (scroll-up lines)
10076       (end-of-buffer
10077        ;; Long lines may cause an end-of-buffer error.
10078        (goto-char (point-max))))
10079     nil))
10080
10081 (defun gnus-article-prev-page (lines)
10082   "Show previous page of current article.
10083 Argument LINES specifies lines to be scrolled down."
10084   (interactive "P")
10085   (move-to-window-line 0)
10086   (if (and gnus-break-pages
10087            (bobp)
10088            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10089       (progn
10090         (gnus-narrow-to-page -1) ;Go to previous page.
10091         (goto-char (point-max))
10092         (recenter -1))
10093     (scroll-down lines)))
10094
10095 (defun gnus-article-refer-article ()
10096   "Read article specified by message-id around point."
10097   (interactive)
10098   (search-forward ">" nil t)    ;Move point to end of "<....>".
10099   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10100       (let ((message-id
10101              (buffer-substring (match-beginning 1) (match-end 1))))
10102         (set-buffer gnus-summary-buffer)
10103         (gnus-summary-refer-article message-id))
10104     (error "No references around point")))
10105
10106 (defun gnus-article-show-summary ()
10107   "Reconfigure windows to show summary buffer."
10108   (interactive)
10109   (gnus-configure-windows 'article)
10110   (gnus-summary-goto-subject gnus-current-article))
10111
10112 (defun gnus-article-describe-briefly ()
10113   "Describe article mode commands briefly."
10114   (interactive)
10115   (gnus-message 6
10116    (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")))
10117
10118 (defun gnus-article-summary-command ()
10119   "Execute the last keystroke in the summary buffer."
10120   (interactive)
10121   (let ((obuf (current-buffer))
10122         (owin (current-window-configuration))
10123         func)
10124     (switch-to-buffer gnus-summary-buffer 'norecord)
10125     (setq func (lookup-key (current-local-map) (this-command-keys)))
10126     (call-interactively func)
10127     (set-buffer obuf)
10128     (let ((npoint (point)))
10129       (set-window-configuration owin)
10130       (set-window-start (get-buffer-window (current-buffer)) (point)))))
10131
10132 (defun gnus-article-summary-command-nosave ()
10133   "Execute the last keystroke in the summary buffer."
10134   (interactive)
10135   (let ((obuf (current-buffer))
10136         (owin (current-window-configuration))
10137         func)
10138     (switch-to-buffer gnus-summary-buffer 'norecord)
10139     (setq func (lookup-key (current-local-map) (this-command-keys)))
10140     (call-interactively func)))
10141
10142 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10143 ;; Modified by tower@prep Nov 86
10144 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10145
10146 (defun gnus-caesar-region (&optional n)
10147   "Caesar rotation of region by N, default 13, for decrypting netnews.
10148 ROT47 will be performed for Japanese text in any case."
10149   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10150                    (list (prefix-numeric-value current-prefix-arg))
10151                  (list nil)))
10152   (cond ((not (numberp n)) (setq n 13))
10153         (t (setq n (mod n 26))))        ;canonicalize N
10154   (if (not (zerop n))           ; no action needed for a rot of 0
10155       (progn
10156         (if (or (not (boundp 'caesar-translate-table))
10157                 (not caesar-translate-table)
10158                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10159             (let ((i 0) 
10160                   (lower "abcdefghijklmnopqrstuvwxyz")
10161                   upper)
10162               (gnus-message 9 "Building caesar-translate-table...")
10163               (setq caesar-translate-table (make-vector 256 0))
10164               (while (< i 256)
10165                 (aset caesar-translate-table i i)
10166                 (setq i (1+ i)))
10167               (setq lower (concat lower lower)
10168                     upper (upcase lower)
10169                     i 0)
10170               (while (< i 26)
10171                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10172                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10173                 (setq i (1+ i)))
10174               ;; ROT47 for Japanese text.
10175               ;; Thanks to ichikawa@flab.fujitsu.junet.
10176               (setq i 161)
10177               (let ((t1 (logior ?O 128))
10178                     (t2 (logior ?! 128))
10179                     (t3 (logior ?~ 128)))
10180                 (while (< i 256)
10181                   (aset caesar-translate-table i
10182                         (let ((v (aref caesar-translate-table i)))
10183                           (if (<= v t1) (if (< v t2) v (+ v 47))
10184                             (if (<= v t3) (- v 47) v))))
10185                   (setq i (1+ i))))
10186               (gnus-message 9 "Building caesar-translate-table... done")))
10187         (let ((from (region-beginning))
10188               (to (region-end))
10189               (i 0) str len)
10190           (setq str (buffer-substring from to))
10191           (setq len (length str))
10192           (while (< i len)
10193             (aset str i (aref caesar-translate-table (aref str i)))
10194             (setq i (1+ i)))
10195           (goto-char from)
10196           (delete-region from to)
10197           (insert str)))))
10198
10199 \f
10200 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10201
10202 ;;;###autoload
10203 (defalias 'gnus-batch-kill 'gnus-batch-score)
10204 ;;;###autoload
10205 (defun gnus-batch-score ()
10206   "Run batched scoring.
10207 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10208 Newsgroups is a list of strings in Bnews format.  If you want to score
10209 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10210 score the alt hierarchy, you'd say \"!alt.all\"."
10211   (interactive)
10212   (let* ((yes-and-no
10213           (gnus-newsrc-parse-options
10214            (apply (function concat)
10215                   (mapcar (lambda (g) (concat g " "))
10216                           command-line-args-left))))
10217          (gnus-expert-user t)
10218          (nnmail-spool-file nil)
10219          (gnus-use-dribble-file nil)
10220          (yes (car yes-and-no))
10221          (no (cdr yes-and-no))
10222          group subscribed newsrc entry
10223          ;; Disable verbose message.
10224          gnus-novice-user gnus-large-newsgroup)
10225     ;; Eat all arguments.
10226     (setq command-line-args-left nil)
10227     ;; Start Gnus.
10228     (gnus)
10229     ;; Apply kills to specified newsgroups in command line arguments.
10230     (setq newsrc (cdr gnus-newsrc-alist))
10231     (while newsrc
10232       (setq group (car (car newsrc)))
10233       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10234       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10235                (and (car entry)
10236                     (or (eq (car entry) t)
10237                         (not (zerop (car entry)))))
10238                (if yes (string-match yes group) t)
10239                (or (null no) (not (string-match no group))))
10240           (progn
10241             (gnus-summary-read-group group nil t)
10242             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10243                  (gnus-summary-exit))))
10244       (setq newsrc (cdr newsrc)))
10245     ;; Exit Emacs.
10246     (switch-to-buffer gnus-group-buffer)
10247     (gnus-group-save-newsrc)))
10248
10249 (defun gnus-apply-kill-file ()
10250   "Apply a kill file to the current newsgroup.
10251 Returns the number of articles marked as read."
10252   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10253           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10254       (gnus-apply-kill-file-internal)
10255     0))
10256
10257 (defun gnus-kill-save-kill-buffer ()
10258   (save-excursion
10259     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10260       (if (get-file-buffer file)
10261           (progn
10262             (set-buffer (get-file-buffer file))
10263             (and (buffer-modified-p) (save-buffer))
10264             (kill-buffer (current-buffer)))))))
10265
10266 (defvar gnus-kill-file-name "KILL"
10267   "Suffix of the kill files.")
10268
10269 (defun gnus-newsgroup-kill-file (newsgroup)
10270   "Return the name of a kill file name for NEWSGROUP.
10271 If NEWSGROUP is nil, return the global kill file name instead."
10272   (cond ((or (null newsgroup)
10273              (string-equal newsgroup ""))
10274          ;; The global KILL file is placed at top of the directory.
10275          (expand-file-name gnus-kill-file-name
10276                            (or gnus-kill-files-directory "~/News")))
10277         ((gnus-use-long-file-name 'not-kill)
10278          ;; Append ".KILL" to newsgroup name.
10279          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10280                            (or gnus-kill-files-directory "~/News")))
10281         (t
10282          ;; Place "KILL" under the hierarchical directory.
10283          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10284                                    "/" gnus-kill-file-name)
10285                            (or gnus-kill-files-directory "~/News")))))
10286
10287 \f
10288 ;;;
10289 ;;; Dribble file
10290 ;;;
10291
10292 (defvar gnus-dribble-ignore nil)
10293
10294 (defun gnus-dribble-file-name ()
10295   (concat gnus-startup-file "-dribble"))
10296
10297 (defun gnus-dribble-open ()
10298   (save-excursion 
10299     (set-buffer 
10300      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10301     (buffer-disable-undo (current-buffer))
10302     (bury-buffer gnus-dribble-buffer)
10303     (auto-save-mode t)
10304     (goto-char (point-max))))
10305
10306 (defun gnus-dribble-enter (string)
10307   (if (and (not gnus-dribble-ignore)
10308            gnus-dribble-buffer
10309            (buffer-name gnus-dribble-buffer))
10310       (let ((obuf (current-buffer)))
10311         (set-buffer gnus-dribble-buffer)
10312         (insert string "\n")
10313         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10314         (set-buffer obuf))))
10315
10316 (defun gnus-dribble-read-file ()
10317   (let ((dribble-file (gnus-dribble-file-name)))
10318     (save-excursion 
10319       (set-buffer (setq gnus-dribble-buffer 
10320                         (get-buffer-create 
10321                          (file-name-nondirectory dribble-file))))
10322       (gnus-add-current-to-buffer-list)
10323       (erase-buffer)
10324       (set-visited-file-name dribble-file)
10325       (buffer-disable-undo (current-buffer))
10326       (bury-buffer (current-buffer))
10327       (set-buffer-modified-p nil)
10328       (let ((auto (make-auto-save-file-name))
10329             (gnus-dribble-ignore t))
10330         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10331             (progn
10332               (if (file-newer-than-file-p auto dribble-file)
10333                   (setq dribble-file auto))
10334               (insert-file-contents dribble-file)
10335               (if (not (zerop (buffer-size)))
10336                   (set-buffer-modified-p t))
10337               (if (gnus-y-or-n-p 
10338                    "Auto-save file exists. Do you want to read it? ")
10339                   (progn
10340                     (gnus-message 5 "Reading %s..." dribble-file) 
10341                     (eval-current-buffer)
10342                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10343
10344 (defun gnus-dribble-delete-file ()
10345   (if (file-exists-p (gnus-dribble-file-name))
10346       (delete-file (gnus-dribble-file-name)))
10347   (if gnus-dribble-buffer
10348       (save-excursion
10349         (set-buffer gnus-dribble-buffer)
10350         (let ((auto (make-auto-save-file-name)))
10351           (if (file-exists-p auto)
10352               (delete-file auto))
10353           (erase-buffer)
10354           (set-buffer-modified-p nil)))))
10355
10356 (defun gnus-dribble-save ()
10357   (if (and gnus-dribble-buffer
10358            (buffer-name gnus-dribble-buffer))
10359       (save-excursion
10360         (set-buffer gnus-dribble-buffer)
10361         (save-buffer))))
10362
10363 (defun gnus-dribble-clear ()
10364   (save-excursion
10365     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10366         (progn
10367           (set-buffer gnus-dribble-buffer)
10368           (erase-buffer)
10369           (set-buffer-modified-p nil)
10370           (setq buffer-saved-size (buffer-size))))))
10371
10372 ;;;
10373 ;;; Server Communication
10374 ;;;
10375
10376 ;; All the Gnus backends have the same interface, and should return
10377 ;; data in a similar format. Below is an overview of what functions
10378 ;; these packages must supply and what results they should return.
10379 ;;
10380 ;; Variables:
10381 ;;
10382 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10383 ;; buffer. 
10384 ;;
10385 ;; Functions for the imaginary backend `choke':
10386 ;;
10387 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10388 ;; Should return all headers for all ARTICLES, or return NOV lines for
10389 ;; the same.
10390 ;;
10391 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10392 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10393 ;; must be returned.
10394 ;;
10395 ;; `choke-close-group GROUP &optional SERVER'
10396 ;; Close group. Most backends won't have to do anything with this
10397 ;; call, but it is an opportunity to clean up, if that is needed. It
10398 ;; is called when Gnus exits a group.
10399 ;;
10400 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10401 ;; Return ARTICLE, which is either an article number or
10402 ;; message-id. Note that not all backends can return articles based on
10403 ;; message-id. 
10404 ;;
10405 ;; `choke-request-list SERVER'
10406 ;; Return a list of all newsgroups on SERVER.
10407 ;;
10408 ;; `choke-request-list-newsgroups SERVER'
10409 ;; Return a list of descriptions of all newsgroups on SERVER.
10410 ;;
10411 ;; `choke-request-newgroups DATE &optional SERVER'
10412 ;; Return a list of all groups that have arrived after DATE on
10413 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10414 ;; always check whether the groups are old or not. Backends that do
10415 ;; not store date information may just return the entire list of
10416 ;; groups, although this might not be a good idea in general.
10417 ;;
10418 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10419 ;; Should return a buffer that is suitable for "posting". nnspool and
10420 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10421 ;; buffer. This function should fill out the appropriate headers. 
10422 ;;
10423 ;; `choke-request-post &optional SERVER'
10424 ;; Function that will be called from a buffer to be posted. 
10425 ;;
10426 ;; `choke-open-server SERVER &optional ARGUMENT'
10427 ;; Open a connection to SERVER.
10428 ;;
10429 ;; `choke-close-server &optional SERVER'
10430 ;; Close the connection to SERVER.
10431 ;;
10432 ;; `choke-server-opened &optional SERVER'
10433 ;; Whether the conenction to SERVER is opened or not.
10434 ;;
10435 ;; `choke-server-status &optional SERVER'
10436 ;; Should return a status string (not in the nntp buffer, but as the
10437 ;; result of the function).
10438 ;;
10439 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10440 ;; Optional function for retrieving active file info on all groups in
10441 ;; GROUPS.  Two return formats are supported: The normal active file
10442 ;; format, and a list of GROUP lines.  This function should return (as
10443 ;; a function value) either `active' or `group', depending on what
10444 ;; format it returns.
10445 ;;
10446 ;; The following functions are optional and apply only to backends
10447 ;; that are able to control the contents of their groups totally
10448 ;; (ie. mail backends.)  Backends that aren't able to do that
10449 ;; shouldn't define these functions at all. Gnus will check for their
10450 ;; presence before attempting to call them.
10451 ;;
10452 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10453 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10454 ;; backends will not be able to expire articles. Should return a list
10455 ;; of all articles that were not expired.
10456 ;;
10457 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10458 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10459 ;; Removes any information it has added to the article (extra headers,
10460 ;; whatever - make it as clean as possible), and then passes the
10461 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
10462 ;; function described below. If the ACCEPT-FORM returns a non-nil
10463 ;; value, the article should then be deleted. If LAST is nil, that
10464 ;; means that there will be further calls to this function. This might
10465 ;; be taken as an advice not to save buffers/internal variables just
10466 ;; yet, but wait until the last call to speed things up.
10467 ;;
10468 ;; `choke-request-accept-article GROUP &optional LAST' 
10469 ;; The contents of the current buffer will be put into GROUP.  There
10470 ;; should, of course, be an article in the current buffer.  This
10471 ;; function is normally only called by the function described above,
10472 ;; and LAST works the same way as in that function.
10473 ;;
10474 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
10475 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
10476 ;; This provides an easy interface for allowing editing of
10477 ;; articles. Note that even headers may be edited, so the backend has
10478 ;; to update any tables (nov buffers, etc) that it maintains after
10479 ;; replacing the article.
10480 ;;
10481 ;; `choke-request-create-group GROUP &optional SERVER'
10482 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
10483 ;; might be a group that already exists, but hasn't been registered
10484 ;; yet. 
10485 ;;
10486 ;; All these functions must return nil if they couldn't service the
10487 ;; request. If the optional arguments are not supplied, some "current"
10488 ;; or "default" values should be used. In short, one should emulate an
10489 ;; NNTP server, in a way.
10490 ;;
10491 ;; If you want to write a new backend, you just have to supply the
10492 ;; functions listed above. In addition, you must enter the new backend
10493 ;; into the list of valid select methods:
10494 ;; (setq gnus-valid-select-methods 
10495 ;;       (cons '("choke" mail) gnus-valid-select-methods))
10496 ;; The first element in this list is the name of the backend. Other
10497 ;; elemnets may be `mail' (for mail groups),  `post' (for news
10498 ;; groups), `none' (neither), `respool' (for groups that can control
10499 ;; their contents). 
10500
10501 (defun gnus-start-news-server (&optional confirm)
10502   "Open a method for getting news.
10503 If CONFIRM is non-nil, the user will be asked for an NNTP server."
10504   (let (how where)
10505     (if gnus-current-select-method
10506         ;; Stream is already opened.
10507         nil
10508       ;; Open NNTP server.
10509       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
10510       (if confirm
10511           (progn
10512             ;; Read server name with completion.
10513             (setq gnus-nntp-server
10514                   (completing-read "NNTP server: "
10515                                    (mapcar (lambda (server) (list server))
10516                                            (cons (list gnus-nntp-server)
10517                                                  gnus-secondary-servers))
10518                                    nil nil gnus-nntp-server))))
10519
10520       (if (and gnus-nntp-server 
10521                (stringp gnus-nntp-server)
10522                (not (string= gnus-nntp-server "")))
10523           (setq gnus-select-method
10524                 (cond ((or (string= gnus-nntp-server "")
10525                            (string= gnus-nntp-server "::"))
10526                        (list 'nnspool (system-name)))
10527                       ((string-match "^:" gnus-nntp-server)
10528                        (list 'nnmh gnus-nntp-server 
10529                              (list 'nnmh-directory 
10530                                    (file-name-as-directory
10531                                     (expand-file-name
10532                                      (concat "~/" (substring
10533                                                    gnus-nntp-server 1)))))))
10534                       (t
10535                        (list 'nntp gnus-nntp-server)))))
10536
10537       (setq how (car gnus-select-method))
10538       (setq where (car (cdr gnus-select-method)))
10539       (cond ((eq how 'nnspool)
10540              (require 'nnspool)
10541              (gnus-message 5 "Looking up local news spool..."))
10542             ((eq how 'nnmh)
10543              (require 'nnmh)
10544              (gnus-message 5 "Looking up mh spool..."))
10545             (t
10546              (require 'nntp)))
10547       (setq gnus-current-select-method gnus-select-method)
10548       (run-hooks 'gnus-open-server-hook)
10549       (or 
10550        ;; gnus-open-server-hook might have opened it
10551        (gnus-server-opened gnus-select-method)  
10552        (gnus-open-server gnus-select-method)
10553        (gnus-y-or-n-p
10554         (format
10555          "%s server on %s can't be opened. Continue? "
10556          (car gnus-select-method) (nth 1 gnus-select-method)))
10557        (progn
10558          (gnus-message 1 "Couldn't open server on %s" 
10559                        (nth 1 gnus-select-method))
10560          (ding)
10561          nil)))))
10562
10563 (defun gnus-check-news-server (&optional method)
10564   "If the news server is down, start it up again."
10565   (let ((method (if method method gnus-select-method)))
10566     (and (stringp method)
10567          (setq method (gnus-server-to-method method)))
10568     (if (gnus-server-opened method)
10569         ;; Stream is already opened.
10570         t
10571       ;; Open server.
10572       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
10573       (run-hooks 'gnus-open-server-hook)
10574       (or (gnus-server-opened method)
10575           (gnus-open-server method))
10576       (message ""))))
10577
10578 (defun gnus-nntp-message (&optional message)
10579   "Check the status of the NNTP server.
10580 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
10581 is returned insted of the status string."
10582   (let ((status (gnus-status-message (gnus-find-method-for-group 
10583                                       gnus-newsgroup-name)))
10584         (message (or message "")))
10585     (if (and (stringp status) (> (length status) 0))
10586         status message)))
10587
10588 (defun gnus-get-function (method function)
10589   (and (stringp method)
10590        (setq method (gnus-server-to-method method)))
10591   (let ((func (intern (format "%s-%s" (car method) function))))
10592     (if (not (fboundp func)) 
10593         (progn
10594           (require (car method))
10595           (if (not (fboundp func)) 
10596               (error "No such function: %s" func))))
10597     func))
10598
10599 ;;; Interface functions to the backends.
10600
10601 (defun gnus-open-server (method)
10602   (funcall (gnus-get-function method 'open-server)
10603            (nth 1 method) (nthcdr 2 method)))
10604
10605 (defun gnus-close-server (method)
10606   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
10607
10608 (defun gnus-request-list (method)
10609   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
10610
10611 (defun gnus-request-list-newsgroups (method)
10612   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
10613
10614 (defun gnus-request-newgroups (date method)
10615   (funcall (gnus-get-function method 'request-newgroups) 
10616            date (nth 1 method)))
10617
10618 (defun gnus-server-opened (method)
10619   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
10620
10621 (defun gnus-status-message (method)
10622   (let ((method (if (stringp method) (gnus-find-method-for-group method)
10623                   method)))
10624     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
10625
10626 (defun gnus-request-group (group &optional dont-check)
10627   (let ((method (gnus-find-method-for-group group)))
10628     (funcall (gnus-get-function method 'request-group) 
10629              (gnus-group-real-name group) (nth 1 method) dont-check)))
10630
10631 (defun gnus-request-asynchronous (group &optional articles)
10632   (let ((method (gnus-find-method-for-group group)))
10633     (funcall (gnus-get-function method 'request-asynchronous) 
10634              (gnus-group-real-name group) (nth 1 method) articles)))
10635
10636 (defun gnus-list-active-group (group)
10637   (let ((method (gnus-find-method-for-group group))
10638         (func 'list-active-group))
10639     (and (gnus-check-backend-function func group)
10640          (funcall (gnus-get-function method func) 
10641                   (gnus-group-real-name group) (nth 1 method)))))
10642
10643 (defun gnus-request-group-description (group)
10644   (let ((method (gnus-find-method-for-group group))
10645         (func 'request-group-description))
10646     (and (gnus-check-backend-function func group)
10647          (funcall (gnus-get-function method func) 
10648                   (gnus-group-real-name group) (nth 1 method)))))
10649
10650 (defun gnus-close-group (group)
10651   (let ((method (gnus-find-method-for-group group)))
10652     (funcall (gnus-get-function method 'close-group) 
10653              (gnus-group-real-name group) (nth 1 method))))
10654
10655 (defun gnus-retrieve-headers (articles group)
10656   (let ((method (gnus-find-method-for-group group)))
10657     (if gnus-use-cache
10658         (gnus-cache-retrieve-headers articles group)
10659       (funcall (gnus-get-function method 'retrieve-headers) 
10660                articles (gnus-group-real-name group) (nth 1 method)))))
10661
10662 (defun gnus-retrieve-groups (groups method)
10663   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
10664
10665 (defun gnus-request-article (article group &optional buffer)
10666   (let ((method (gnus-find-method-for-group group)))
10667     (funcall (gnus-get-function method 'request-article) 
10668              article (gnus-group-real-name group) (nth 1 method) buffer)))
10669
10670 (defun gnus-request-head (article group)
10671   (let ((method (gnus-find-method-for-group group)))
10672     (funcall (gnus-get-function method 'request-head) 
10673              article (gnus-group-real-name group) (nth 1 method))))
10674
10675 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10676 (defun gnus-request-post-buffer (post group subject header artbuf
10677                                       info follow-to respect-poster)
10678    (let* ((info (or info (and group (nth 2 (gnus-gethash 
10679                                             group gnus-newsrc-hashtb)))))
10680           (method
10681            (if (and gnus-post-method
10682                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10683                     (memq 'post (assoc
10684                                  (format "%s" (car (gnus-find-method-for-group
10685                                                     gnus-newsgroup-name)))
10686                                         gnus-valid-select-methods)))
10687                gnus-post-method
10688              (gnus-find-method-for-group gnus-newsgroup-name))))
10689      (or (gnus-server-opened method)
10690          (gnus-open-server method)
10691          (error "Can't open server %s:%s" (car method) (nth 1 method)))
10692      (let ((mail-self-blind nil)
10693            (mail-archive-file-name nil))
10694        (funcall (gnus-get-function method 'request-post-buffer) 
10695                 post group subject header artbuf info follow-to
10696                 respect-poster))))
10697
10698 (defun gnus-request-post (method &optional force)
10699   (and (stringp method)
10700        (setq method (gnus-server-to-method method)))
10701   (and (not force) gnus-post-method
10702        (memq 'post (assoc (format "%s" (car method))
10703                           gnus-valid-select-methods))
10704        (setq method gnus-post-method))
10705   (funcall (gnus-get-function method 'request-post) 
10706            (nth 1 method)))
10707
10708 (defun gnus-request-expire-articles (articles group &optional force)
10709   (let ((method (gnus-find-method-for-group group)))
10710     (funcall (gnus-get-function method 'request-expire-articles) 
10711              articles (gnus-group-real-name group) (nth 1 method)
10712              force)))
10713
10714 (defun gnus-request-move-article 
10715   (article group server accept-function &optional last)
10716   (let ((method (gnus-find-method-for-group group)))
10717     (funcall (gnus-get-function method 'request-move-article) 
10718              article (gnus-group-real-name group) 
10719              (nth 1 method) accept-function last)))
10720
10721 (defun gnus-request-accept-article (group &optional last)
10722   (let ((func (if (symbolp group) group
10723                 (car (gnus-find-method-for-group group)))))
10724     (funcall (intern (format "%s-request-accept-article" func))
10725              (if (stringp group) (gnus-group-real-name group) group)
10726              last)))
10727
10728 (defun gnus-request-replace-article (article group buffer)
10729   (let ((func (car (gnus-find-method-for-group group))))
10730     (funcall (intern (format "%s-request-replace-article" func))
10731              article (gnus-group-real-name group) buffer)))
10732
10733 (defun gnus-request-create-group (group)
10734   (let ((method (gnus-find-method-for-group group)))
10735     (funcall (gnus-get-function method 'request-create-group) 
10736              (gnus-group-real-name group) (nth 1 method))))
10737
10738 (defun gnus-member-of-valid (symbol group)
10739   (memq symbol (assoc
10740                 (format "%s" (car (gnus-find-method-for-group group)))
10741                 gnus-valid-select-methods)))
10742
10743 (defun gnus-find-method-for-group (group &optional info)
10744   (or gnus-override-method
10745       (and (not group)
10746            gnus-select-method)
10747       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
10748             method)
10749         (if (or (not info)
10750                 (not (setq method (nth 4 info))))
10751             (setq method gnus-select-method)
10752           (setq method
10753                 (cond ((stringp method)
10754                        (gnus-server-to-method method))
10755                       ((stringp (car method))
10756                        (gnus-server-extend-method group method))
10757                       (t
10758                        method))))
10759         (gnus-server-add-address method))))
10760
10761 (defun gnus-check-backend-function (func group)
10762   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
10763                  group)))
10764     (fboundp (intern (format "%s-%s" method func)))))
10765
10766 (defun gnus-methods-using (method)
10767   (let ((valids gnus-valid-select-methods)
10768         outs)
10769     (while valids
10770       (if (memq method (car valids)) 
10771           (setq outs (cons (car valids) outs)))
10772       (setq valids (cdr valids)))
10773     outs))
10774
10775 ;;; 
10776 ;;; Active & Newsrc File Handling
10777 ;;;
10778
10779 ;; Newsrc related functions.
10780 ;; Gnus internal format of gnus-newsrc-alist:
10781 ;; (("alt.general" 3 (1 . 1))
10782 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
10783 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
10784 ;; The first item is the group name; the second is the subscription
10785 ;; level; the third is either a range of a list of ranges of read
10786 ;; articles, the optional fourth element is a list of marked articles,
10787 ;; the optional fifth element is the select method.
10788 ;;
10789 ;; Gnus internal format of gnus-newsrc-hashtb:
10790 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
10791 ;; This is the entry for "alt.misc". The first element is the number
10792 ;; of unread articles in "alt.misc". The cdr of this entry is the
10793 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
10794 ;; trivial to remove or add new elements into gnus-newsrc-alist
10795 ;; without scanning the entire list. So, to get the actual information
10796 ;; of "alt.misc", you'd say something like 
10797 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
10798 ;;
10799 ;; Gnus internal format of gnus-active-hashtb:
10800 ;; ((1 . 1))
10801 ;;  (5 . 10))
10802 ;;  (67 . 99)) ...)
10803 ;; The only element in each entry in this hash table is a range of
10804 ;; (possibly) available articles. (Articles in this range may have
10805 ;; been expired or cancelled.)
10806 ;;
10807 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
10808 ;; ("alt.misc" "alt.test" "alt.general" ...)
10809
10810 (defun gnus-setup-news (&optional rawfile level)
10811   "Setup news information.
10812 If RAWFILE is non-nil, the .newsrc file will also be read.
10813 If LEVEL is non-nil, the news will be set up at level LEVEL."
10814   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
10815     ;; Clear some variables to re-initialize news information.
10816     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
10817
10818     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
10819     (if init (gnus-read-newsrc-file rawfile))
10820
10821     ;; Read the active file and create `gnus-active-hashtb'.
10822     ;; If `gnus-read-active-file' is nil, then we just create an empty
10823     ;; hash table. The partial filling out of the hash table will be
10824     ;; done in `gnus-get-unread-articles'.
10825     (if (and gnus-read-active-file 
10826              (not level)
10827              (gnus-server-opened gnus-select-method))
10828         (gnus-read-active-file)
10829       (setq gnus-active-hashtb (make-vector 4095 0)))
10830
10831     (and init gnus-use-dribble-file (gnus-dribble-read-file))
10832
10833     ;; Find the number of unread articles in each non-dead group.
10834     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
10835     ;; Find new newsgroups and treat them.
10836     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
10837              (gnus-server-opened gnus-select-method))
10838         (gnus-find-new-newsgroups))
10839     (if (and init gnus-check-bogus-newsgroups 
10840              gnus-read-active-file (not level)
10841              (gnus-server-opened gnus-select-method))
10842         (gnus-check-bogus-newsgroups))))
10843
10844 (defun gnus-find-new-newsgroups ()
10845   "Search for new newsgroups and add them.
10846 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
10847 The `-n' option line from .newsrc is respected."
10848   (interactive)
10849   (or (gnus-check-first-time-used)
10850       (if (or (consp gnus-check-new-newsgroups)
10851               (eq gnus-check-new-newsgroups 'ask-server))
10852           (gnus-ask-server-for-new-groups)
10853         (let ((groups 0)
10854               group new-newsgroups)
10855           (or gnus-have-read-active-file (gnus-read-active-file))
10856           (setq gnus-newsrc-last-checked-date (current-time-string))
10857           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
10858           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
10859           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
10860           (mapatoms
10861            (lambda (sym)
10862              (setq group (symbol-name sym))
10863              (if (or (gnus-gethash group gnus-killed-hashtb)
10864                      (gnus-gethash group gnus-newsrc-hashtb))
10865                  ()
10866                (let ((do-sub (gnus-matches-options-n group)))
10867                  (cond ((eq do-sub 'subscribe)
10868                         (setq groups (1+ groups))
10869                         (gnus-sethash group group gnus-killed-hashtb)
10870                         (funcall 
10871                          gnus-subscribe-options-newsgroup-method group))
10872                        ((eq do-sub 'ignore)
10873                         nil)
10874                        (t
10875                         (setq groups (1+ groups))
10876                         (gnus-sethash group group gnus-killed-hashtb)
10877                         (if gnus-subscribe-hierarchical-interactive
10878                             (setq new-newsgroups (cons group new-newsgroups))
10879                           (funcall gnus-subscribe-newsgroup-method group)))))))
10880            gnus-active-hashtb)
10881           (if new-newsgroups 
10882               (gnus-subscribe-hierarchical-interactive new-newsgroups))
10883           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10884           (if (> groups 0)
10885               (gnus-message 6 "%d new newsgroup%s arrived." 
10886                             groups (if (> groups 1) "s have" " has")))))))
10887
10888 (defun gnus-matches-options-n (group)
10889   ;; Returns `subscribe' if the group is to be uncoditionally
10890   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
10891   ;; no match for the group.
10892
10893   ;; First we check the two user variables.
10894   (cond
10895    ((and gnus-options-subscribe
10896          (string-match gnus-options-not-subscribe group))
10897     'subscribe)
10898    ((and gnus-options-not-subscribe
10899          (string-match gnus-options-subscribe group))
10900     'ignore)
10901    ;; Then we go through the list that was retrieved from the .newsrc
10902    ;; file.  This list has elements on the form 
10903    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
10904    ;; is in the reverse order of the options line) is returned.
10905    (t
10906     (let ((regs gnus-newsrc-options-n))
10907       (while (and regs
10908                   (not (string-match (car (car gnus-newsrc-options-n)) group)))
10909         (setq regs (cdr regs)))
10910       (and regs (cdr (car regs)))))))
10911
10912 (defun gnus-ask-server-for-new-groups ()
10913   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
10914          (methods (cons gnus-select-method 
10915                         (append
10916                          (and (consp gnus-check-new-newsgroups)
10917                               gnus-check-new-newsgroups)
10918                          gnus-secondary-select-methods)))
10919          (groups 0)
10920          (new-date (current-time-string))
10921          hashtb group new-newsgroups got-new)
10922     ;; Go thorugh both primary and secondary select methods and
10923     ;; request new newsgroups.  
10924     (while methods
10925       (if (gnus-request-newgroups date (car methods))
10926           (save-excursion
10927             (setq got-new t)
10928             (or hashtb (setq hashtb (gnus-make-hashtable 
10929                                      (count-lines (point-min) (point-max)))))
10930             (set-buffer nntp-server-buffer)
10931             ;; Enter all the new groups in a hashtable.
10932             (gnus-active-to-gnus-format (car methods) hashtb)))
10933       (setq methods (cdr methods)))
10934     (and got-new (setq gnus-newsrc-last-checked-date new-date))
10935     ;; Now all new groups from all select methods are in `hashtb'.
10936     (mapatoms
10937      (lambda (group-sym)
10938        (setq group (symbol-name group-sym))
10939        (if (or (gnus-gethash group gnus-newsrc-hashtb)
10940                (member group gnus-zombie-list)
10941                (member group gnus-killed-list))
10942            ;; The group is already known.
10943            ()
10944          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
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      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     got-new))
10967
10968 (defun gnus-check-first-time-used ()
10969   (if (or (> (length gnus-newsrc-alist) 1)
10970           (file-exists-p gnus-startup-file)
10971           (file-exists-p (concat gnus-startup-file ".el"))
10972           (file-exists-p (concat gnus-startup-file ".eld")))
10973       nil
10974     (gnus-message 6 "First time user; subscribing you to default groups")
10975     (or gnus-have-read-active-file (gnus-read-active-file))
10976     (setq gnus-newsrc-last-checked-date (current-time-string))
10977     (let ((groups gnus-default-subscribed-newsgroups)
10978           group)
10979       (if (eq groups t)
10980           nil
10981         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
10982         (mapatoms
10983          (lambda (sym)
10984            (setq group (symbol-name sym))
10985            (let ((do-sub (gnus-matches-options-n group)))
10986              (cond ((eq do-sub 'subscribe)
10987                     (gnus-sethash group group gnus-killed-hashtb)
10988                     (funcall 
10989                      gnus-subscribe-options-newsgroup-method group))
10990                    ((eq do-sub 'ignore)
10991                     nil)
10992                    (t
10993                     (setq gnus-killed-list (cons group gnus-killed-list))))))
10994          gnus-active-hashtb)
10995         (while groups
10996           (if (gnus-gethash (car groups) gnus-active-hashtb)
10997               (gnus-group-change-level 
10998                (car groups) gnus-level-default-subscribed gnus-level-killed))
10999           (setq groups (cdr groups)))
11000         (gnus-group-make-help-group)
11001         (and gnus-novice-user
11002              (gnus-message 7 "`A k' to list killed groups"))))))
11003
11004 ;; `gnus-group-change-level' is the fundamental function for changing
11005 ;; subscription levels of newsgroups. This might mean just changing
11006 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11007 ;; again, which subscribes/unsubscribes a group, which is equally
11008 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11009 ;; from 8-9 to 1-7 means that you remove the group from the list of
11010 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11011 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11012 ;; which is trivial.
11013 ;; ENTRY can either be a string (newsgroup name) or a list (if
11014 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11015 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11016 ;; entries. 
11017 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11018 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11019 ;; after. 
11020 (defun gnus-group-change-level (entry level &optional oldlevel
11021                                       previous fromkilled)
11022   (let ((pinfo entry)
11023         group info active num)
11024     ;; Glean what info we can from the arguments
11025     (if (consp entry)
11026         (if fromkilled (setq group (nth 1 entry))
11027           (setq group (car (nth 2 entry))))
11028       (setq group entry))
11029     (if (and (stringp entry)
11030              oldlevel 
11031              (< oldlevel gnus-level-zombie))
11032         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11033     (if (and (not oldlevel)
11034              (listp entry))
11035         (setq oldlevel (car (cdr (nth 2 entry)))))
11036     (if (stringp previous)
11037         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11038
11039     (gnus-dribble-enter
11040      (format "(gnus-group-change-level %S %S %S %S %S)" 
11041              group level oldlevel (car (nth 2 previous)) fromkilled))
11042     
11043     ;; Then we remove the newgroup from any old structures, if needed.
11044     ;; If the group was killed, we remove it from the killed or zombie
11045     ;; list. If not, and it is in fact going to be killed, we remove
11046     ;; it from the newsrc hash table and assoc.
11047     (cond ((>= oldlevel gnus-level-zombie)
11048            (if (= oldlevel gnus-level-zombie)
11049                (setq gnus-zombie-list (delete group gnus-zombie-list))
11050              (setq gnus-killed-list (delete group gnus-killed-list))))
11051           (t
11052            (if (>= level gnus-level-zombie)
11053                (progn
11054                  (gnus-sethash (car (nth 2 entry))
11055                                nil gnus-newsrc-hashtb)
11056                  (if (nth 3 entry)
11057                      (setcdr (gnus-gethash (car (nth 3 entry))
11058                                            gnus-newsrc-hashtb)
11059                              (cdr entry)))
11060                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11061
11062     ;; Finally we enter (if needed) the list where it is supposed to
11063     ;; go, and change the subscription level. If it is to be killed,
11064     ;; we enter it into the killed or zombie list.
11065     (cond ((>= level gnus-level-zombie)
11066            (and (string= group (gnus-group-real-name group))
11067                 (if (= level gnus-level-zombie)
11068                     (setq gnus-zombie-list (cons group gnus-zombie-list))
11069                   (setq gnus-killed-list (cons group gnus-killed-list)))))
11070           (t
11071            ;; If the list is to be entered into the newsrc assoc, and
11072            ;; it was killed, we have to create an entry in the newsrc
11073            ;; hashtb format and fix the pointers in the newsrc assoc.
11074            (if (>= oldlevel gnus-level-zombie)
11075                (progn
11076                  (if (listp entry)
11077                      (progn
11078                        (setq info (cdr entry))
11079                        (setq num (car entry)))
11080                    (setq active (gnus-gethash group gnus-active-hashtb))
11081                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11082                    ;; Check whether the group is foreign. If so, the
11083                    ;; foreign select method has to be entered into the
11084                    ;; info. 
11085                    (let ((method (gnus-group-method-name group)))
11086                      (if (eq method gnus-select-method)
11087                          (setq info (list group level nil))
11088                        (setq info (list group level nil nil method)))))
11089                  (or previous 
11090                      (setq previous 
11091                            (let ((p gnus-newsrc-alist))
11092                              (while (cdr (cdr p))
11093                                (setq p (cdr p)))
11094                              p)))
11095                  (setq entry (cons info (cdr (cdr previous))))
11096                  (if (cdr previous)
11097                      (progn
11098                        (setcdr (cdr previous) entry)
11099                        (gnus-sethash group (cons num (cdr previous)) 
11100                                      gnus-newsrc-hashtb))
11101                    (setcdr previous entry)
11102                    (gnus-sethash group (cons num previous)
11103                                  gnus-newsrc-hashtb))
11104                  (if (cdr entry)
11105                      (setcdr (gnus-gethash (car (car (cdr entry)))
11106                                            gnus-newsrc-hashtb)
11107                              entry)))
11108              ;; It was alive, and it is going to stay alive, so we
11109              ;; just change the level and don't change any pointers or
11110              ;; hash table entries.
11111              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11112
11113 (defun gnus-kill-newsgroup (newsgroup)
11114   "Obsolete function. Kills a newsgroup."
11115   (gnus-group-change-level
11116    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11117
11118 (defun gnus-check-bogus-newsgroups (&optional confirm)
11119   "Remove bogus newsgroups.
11120 If CONFIRM is non-nil, the user has to confirm the deletion of every
11121 newsgroup." 
11122   (let ((newsrc (cdr gnus-newsrc-alist))
11123         bogus group)
11124     (gnus-message 5 "Checking bogus newsgroups...")
11125     (or gnus-have-read-active-file (gnus-read-active-file))
11126     ;; Find all bogus newsgroup that are subscribed.
11127     (while newsrc
11128       (setq group (car (car newsrc)))
11129       (if (or (gnus-gethash group gnus-active-hashtb)
11130               (nth 4 (car newsrc))
11131               (and confirm
11132                    (not (gnus-y-or-n-p
11133                          (format "Remove bogus newsgroup: %s " group)))))
11134           ;; Active newsgroup.
11135           ()
11136         ;; Found a bogus newsgroup.
11137         (setq bogus (cons group bogus)))
11138       (setq newsrc (cdr newsrc)))
11139     ;; Remove all bogus subscribed groups by first killing them, and
11140     ;; then removing them from the list of killed groups.
11141     (while bogus
11142       (gnus-group-change-level 
11143        (gnus-gethash (car bogus) gnus-newsrc-hashtb) gnus-level-killed)
11144       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
11145       (setq bogus (cdr bogus)))
11146     ;; Then we remove all bogus groups from the list of killed and
11147     ;; zombie groups. They are are removed without confirmation.
11148     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11149           killed)
11150       (while dead-lists
11151         (setq killed (symbol-value (car dead-lists)))
11152         (while killed
11153           (setq group (car killed))
11154           (or (gnus-gethash group gnus-active-hashtb)
11155               ;; The group is bogus.
11156               (set (car dead-lists)
11157                    (delete group (symbol-value (car dead-lists)))))
11158           (setq killed (cdr killed)))
11159         (setq dead-lists (cdr dead-lists))))
11160     (gnus-message 5 "Checking bogus newsgroups... done")))
11161
11162 (defun gnus-check-duplicate-killed-groups ()
11163   "Remove duplicates from the list of killed groups."
11164   (interactive)
11165   (let ((killed gnus-killed-list))
11166     (while killed
11167       (gnus-message 9 "%d" (length killed))
11168       (setcdr killed (delete (car killed) (cdr killed)))
11169       (setq killed (cdr killed)))))
11170
11171 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11172 ;; and compute how many unread articles there are in each group.
11173 (defun gnus-get-unread-articles (&optional level)
11174   (let* ((newsrc (cdr gnus-newsrc-alist))
11175          (conditional level)
11176          (level (or level (1+ gnus-level-subscribed)))
11177          info group active virtuals method)
11178     (gnus-message 5 "Checking new news...")
11179     (while newsrc
11180       (setq info (car newsrc))
11181       (setq group (car info))
11182       (setq active (gnus-gethash group gnus-active-hashtb))
11183
11184       ;; Check newsgroups. If the user doesn't want to check them, or
11185       ;; they can't be checked (for instance, if the news server can't
11186       ;; be reached) we just set the number of unread articles in this
11187       ;; newsgroup to t. This means that Gnus thinks that there are
11188       ;; unread articles, but it has no idea how many.
11189       (if (and (setq method (nth 4 info))
11190                (not (gnus-server-equal gnus-select-method
11191                                        (gnus-server-get-method nil method))))
11192           (if (or (and gnus-activate-foreign-newsgroups 
11193                        (not (numberp gnus-activate-foreign-newsgroups)))
11194                   (and (numberp gnus-activate-foreign-newsgroups)
11195                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
11196                        (<= (nth 1 info) level)))
11197               (if (eq (car (if (stringp method) 
11198                                (gnus-server-to-method method)
11199                              (nth 4 info))) 'nnvirtual)
11200                   (setq virtuals (cons info virtuals))
11201                 (setq active (gnus-activate-newsgroup (car info)))))
11202         (if (and (not gnus-read-active-file)
11203                  (<= (nth 1 info) level))
11204             (progn
11205               (setq active (gnus-activate-newsgroup (car info))))))
11206       
11207       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
11208                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
11209       (and active 
11210            (gnus-get-unread-articles-in-group info active)
11211            ;; Close the groups as we look at them!
11212            (gnus-close-group group))
11213       (setq newsrc (cdr newsrc)))
11214
11215     ;; Activate the virtual groups. This has to be done after all the
11216     ;; other groups. 
11217     ;; !!! If one virtual group contains another virtual group, even
11218     ;; doing it this way might cause problems.
11219    (while virtuals
11220       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11221            (gnus-get-unread-articles-in-group (car virtuals) active))
11222       (setq virtuals (cdr virtuals)))
11223
11224     (gnus-message 5 "Checking new news... done")))
11225
11226 ;; Create a hash table out of the newsrc alist. The `car's of the
11227 ;; alist elements are used as keys.
11228 (defun gnus-make-hashtable-from-newsrc-alist ()
11229   (let ((alist gnus-newsrc-alist)
11230          prev)
11231     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11232     (setq alist 
11233           (setq prev (setq gnus-newsrc-alist 
11234                            (cons (list "dummy.group" 0 nil) alist))))
11235     (while alist
11236       (gnus-sethash (car (car alist)) (cons nil prev) gnus-newsrc-hashtb)
11237       (setq prev alist)
11238       (setq alist (cdr alist)))))
11239
11240 (defun gnus-make-hashtable-from-killed ()
11241   "Create a hash table from the killed and zombie lists."
11242   (let ((lists '(gnus-killed-list gnus-zombie-list))
11243         list)
11244     (setq gnus-killed-hashtb 
11245           (gnus-make-hashtable 
11246            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11247     (while lists
11248       (setq list (symbol-value (car lists)))
11249       (setq lists (cdr lists))
11250       (while list
11251         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11252         (setq list (cdr list))))))
11253
11254 (defun gnus-get-unread-articles-in-group (info active)
11255   (let* ((range (nth 2 info))
11256          (num 0)
11257          (marked (nth 3 info))
11258          srange lowest group highest)
11259     ;; If a cache is present, we may have to alter the active info.
11260     (and gnus-use-cache
11261          (gnus-cache-possibly-alter-active (car info) active))
11262     ;; Modify the list of read articles according to what articles 
11263     ;; are available; then tally the unread articles and add the
11264     ;; number to the group hash table entry.
11265     (cond ((zerop (cdr active))
11266            (setq num 0))
11267           ((not range)
11268            (setq num (- (1+ (cdr active)) (car active))))
11269           ((not (listp (cdr range)))
11270            ;; Fix a single (num . num) range according to the
11271            ;; active hash table.
11272            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11273            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11274            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11275            ;; Compute number of unread articles.
11276            (setq num (max 0 (- (cdr active) 
11277                                (- (1+ (cdr range)) (car range))))))
11278           (t
11279            ;; The read list is a list of ranges. Fix them according to
11280            ;; the active hash table.
11281            ;; First peel off any elements that are below the lower
11282            ;; active limit. 
11283            (while (and (cdr range) 
11284                        (>= (car active) 
11285                            (or (and (atom (car (cdr range))) (car (cdr range)))
11286                                (car (car (cdr range))))))
11287              (if (numberp (car range))
11288                  (setcar range 
11289                          (cons (car range) 
11290                                (or (and (numberp (car (cdr range)))
11291                                         (car (cdr range))) 
11292                                    (cdr (car (cdr range))))))
11293                (setcdr (car range) 
11294                        (or (and (numberp (nth 1 range)) (nth 1 range))
11295                            (cdr (car (cdr range))))))
11296              (setcdr range (cdr (cdr range))))
11297            ;; Adjust the first element to be the same as the lower limit. 
11298            (if (and (not (atom (car range))) 
11299                     (< (cdr (car range)) (car active)))
11300                (setcdr (car range) (1- (car active))))
11301            ;; Then we want to peel off any elements that are higher
11302            ;; than the upper active limit.  
11303            (let ((srange range))
11304              ;; Go past all legal elements.
11305              (while (and (cdr srange) 
11306                          (<= (or (and (atom (car (cdr srange)))
11307                                       (car (cdr srange)))
11308                                  (car (car (cdr srange)))) (cdr active)))
11309                (setq srange (cdr srange)))
11310              (if (cdr srange)
11311                  ;; Nuke all remaining illegal elements.
11312                  (setcdr srange nil))
11313
11314              ;; Adjust the final element.
11315              (if (and (not (atom (car srange)))
11316                       (> (cdr (car srange)) (cdr active)))
11317                  (setcdr (car srange) (cdr active))))
11318            ;; Compute the number of unread articles.
11319            (while range
11320              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11321                                          (cdr (car range))))
11322                                  (or (and (atom (car range)) (car range))
11323                                      (car (car range))))))
11324              (setq range (cdr range)))
11325            (setq num (max 0 (- (cdr active) num)))))
11326     (and info
11327          (progn
11328            (and (assq 'tick marked)
11329                 (inline (gnus-remove-illegal-marked-articles
11330                          (assq 'tick marked) (nth 2 info))))
11331            (and (assq 'dormant marked)
11332                 (inline (gnus-remove-illegal-marked-articles
11333                          (assq 'dormant marked) (nth 2 info))))
11334            (setcar
11335             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11336             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11337                                 (length (cdr (assq 'dormant marked)))))))))
11338     num))
11339
11340 (defun gnus-remove-illegal-marked-articles (marked ranges)
11341   (let ((m (cdr marked)))
11342     ;; Make sure that all ticked articles are a subset of the unread
11343     ;; articles. 
11344     (while m
11345       (if (gnus-member-of-range (car m) ranges)
11346           (setcdr marked (cdr m))
11347         (setq marked m))
11348       (setq m (cdr m)))))
11349
11350 (defun gnus-activate-newsgroup (group)
11351   (let ((method (gnus-find-method-for-group group))
11352         active)
11353     (and (or (gnus-server-opened method) (gnus-open-server method))
11354          (gnus-request-group group)
11355          (save-excursion
11356            (set-buffer nntp-server-buffer)
11357            (goto-char (point-min))
11358            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11359                 (progn
11360                   (goto-char (match-beginning 1))
11361                   (gnus-sethash 
11362                    group (setq active (cons (read (current-buffer))
11363                                             (read (current-buffer))))
11364                    gnus-active-hashtb))
11365                 active)))))
11366
11367 (defun gnus-update-read-articles 
11368   (group unread unselected ticked &optional domarks replied expirable killed
11369          dormant bookmark score)
11370   "Update the list of read and ticked articles in GROUP using the
11371 UNREAD and TICKED lists.
11372 Note: UNSELECTED has to be sorted over `<'.
11373 Returns whether the updating was successful."
11374   (let* ((active (or gnus-newsgroup-active 
11375                      (gnus-gethash group gnus-active-hashtb)))
11376          (entry (gnus-gethash group gnus-newsrc-hashtb))
11377          (number (car entry))
11378          (info (nth 2 entry))
11379          (marked (nth 3 info))
11380          (prev 1)
11381          (unread (sort (copy-sequence unread) (function <)))
11382          last read)
11383     (if (or (not info) (not active))
11384         ;; There is no info on this group if it was, in fact,
11385         ;; killed. Gnus stores no information on killed groups, so
11386         ;; there's nothing to be done. 
11387         ;; One could store the information somewhere temporarily,
11388         ;; perhaps... Hmmm... 
11389         ()
11390       ;; Remove any negative articles numbers.
11391       (while (and unread (< (car unread) 0))
11392         (setq unread (cdr unread)))
11393       (setq unread (sort (append unselected unread) '<))
11394       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11395       (setcar entry (max 0 (- (length unread) (length ticked) 
11396                               (length dormant))))
11397       ;; Compute the ranges of read articles by looking at the list of
11398       ;; unread articles.  
11399       (while unread
11400         (if (/= (car unread) prev)
11401             (setq read (cons (if (= prev (1- (car unread))) prev
11402                                (cons prev (1- (car unread)))) read)))
11403         (setq prev (1+ (car unread)))
11404         (setq unread (cdr unread)))
11405       (if (<= prev (cdr active))
11406           (setq read (cons (cons prev (cdr active)) read)))
11407       ;; Enter this list into the group info.
11408       (setcar (cdr (cdr info)) 
11409               (if (> (length read) 1) (nreverse read) read))
11410       ;; Enter the list of ticked articles.
11411       (gnus-set-marked-articles 
11412        info ticked
11413        (if domarks replied (cdr (assq 'reply marked)))
11414        (if domarks expirable (cdr (assq 'expire marked)))
11415        (if domarks killed (cdr (assq 'killed marked)))
11416        (if domarks dormant (cdr (assq 'dormant marked)))
11417        (if domarks bookmark (cdr (assq 'bookmark marked)))
11418        (if domarks score (cdr (assq 'score marked))))
11419       t)))
11420
11421 (defun gnus-make-articles-unread (group articles)
11422   "Mark ARTICLES in GROUP as unread."
11423   (let ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
11424                          (gnus-gethash (gnus-group-real-name group)
11425                                        gnus-newsrc-hashtb)))))
11426     (setcar (nthcdr 2 info)
11427             (gnus-remove-from-range (nth 2 info) articles))
11428     (gnus-group-update-group group t)))
11429
11430 (defun gnus-read-active-file ()
11431   "Get active file from NNTP server."
11432   (gnus-group-set-mode-line)
11433   (let ((methods (cons gnus-select-method gnus-secondary-select-methods))
11434         (not-first nil)
11435         list-type)
11436     (setq gnus-have-read-active-file nil)
11437     (save-excursion
11438       (set-buffer nntp-server-buffer)
11439       (while methods
11440         (let* ((where (nth 1 (car methods)))
11441                (mesg (format "Reading active file%s via %s..."
11442                              (if (and where (not (zerop (length where))))
11443                                  (concat " from " where) "")
11444                              (car (car methods)))))
11445           (gnus-message 5 mesg)
11446           (gnus-check-news-server (car methods))
11447           (cond 
11448            ((and (eq gnus-read-active-file 'some)
11449                  (gnus-check-backend-function
11450                   'retrieve-groups (car (car methods))))
11451             (let ((newsrc (cdr gnus-newsrc-alist))
11452                   groups)
11453               (while newsrc
11454                 (and (gnus-server-equal 
11455                       (gnus-find-method-for-group
11456                        (car (car newsrc)) (car newsrc))
11457                       (gnus-server-get-method nil (car methods)))
11458                      (setq groups (cons (car (car newsrc)) groups)))
11459                 (setq newsrc (cdr newsrc)))
11460               (setq list-type (gnus-retrieve-groups groups (car methods)))
11461               (cond ((not list-type)
11462                      (gnus-message 
11463                       1 "Cannot read partial active file from %s server." 
11464                       (car (car methods)))
11465                      (ding)
11466                      (sit-for 2))
11467                     ((eq list-type 'active)
11468                      (gnus-active-to-gnus-format (and not-first (car methods)))
11469                      (setq not-first t))
11470                     (t
11471                      (gnus-groups-to-gnus-format (and not-first (car methods)))
11472                      (setq not-first t)))))
11473            (t
11474             (if (not (gnus-request-list (car methods)))
11475                 (progn
11476                   (gnus-message 1 "Cannot read active file from %s server." 
11477                                 (car (car methods)))
11478                   (ding))
11479               (gnus-active-to-gnus-format 
11480                (and gnus-have-read-active-file (car methods)))
11481               (setq gnus-have-read-active-file t)
11482               (gnus-message 5 "%s...done" mesg)))))
11483         (setq methods (cdr methods))))))
11484
11485 ;; Read an active file and place the results in `gnus-active-hashtb'.
11486 (defun gnus-active-to-gnus-format (method &optional hashtb)
11487   (let ((cur (current-buffer))
11488         (hashtb (or hashtb 
11489                     (if method
11490                         gnus-active-hashtb
11491                       (setq gnus-active-hashtb
11492                             (gnus-make-hashtable 
11493                              (count-lines (point-min) (point-max))))))))
11494     ;; Delete unnecessary lines.
11495     (goto-char (point-min))
11496     (while (search-forward "\nto." nil t)
11497       (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
11498     (or (string= gnus-ignored-newsgroups "")
11499         (progn
11500           (goto-char (point-min))
11501           (delete-matching-lines gnus-ignored-newsgroups)))
11502     (and method (not (eq method gnus-select-method))
11503          (let ((prefix (gnus-group-prefixed-name "" method)))
11504            (goto-char (point-min))
11505            (while (and (not (eobp))
11506                        (null (insert prefix))
11507                        (zerop (forward-line 1))))))
11508     (goto-char (point-min))
11509     ;; Store active file in hashtable.
11510     (goto-char (point-min))
11511     (if (string-match "%[oO]" gnus-group-line-format)
11512         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
11513         ;; If we want information on moderated groups, we use this
11514         ;; loop...   
11515         (let* ((mod-hashtb (make-vector 7 0))
11516                (m (intern "m" mod-hashtb))
11517                group max mod)
11518           (while (not (eobp))
11519             (condition-case nil
11520                 (progn
11521                   (narrow-to-region (point) (gnus-point-at-eol))
11522                   (setq group (let ((obarray hashtb)) (read cur)))
11523                   (setq max (read cur))
11524                   (set group (cons (read cur) max))
11525                   ;; Enter moderated groups into a list.
11526                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
11527                       (setq gnus-moderated-list 
11528                             (cons (symbol-name group) gnus-moderated-list))))
11529               (error nil))
11530             (widen)
11531             (forward-line 1)))
11532       ;; And if we do not care about moderation, we use this loop,
11533       ;; which is faster.
11534       (let (group max)
11535         (while (not (eobp))
11536           (condition-case ()
11537               (progn
11538                 (narrow-to-region (point) (gnus-point-at-eol))
11539                 ;; group gets set to a symbol interned in the hash table
11540                 ;; (what a hack!!)
11541                 (setq group (let ((obarray hashtb)) (read cur)))
11542                 (setq max (read cur))
11543                 (set group (cons (read cur) max)))
11544             (error 
11545              (progn (ding) 
11546                     (gnus-message 3 "Illegal active: %s"
11547                                   (buffer-substring 
11548                                    (gnus-point-at-bol) (gnus-point-at-eol)))
11549                     nil)))
11550           (widen)
11551           (forward-line 1))))))
11552
11553 (defun gnus-groups-to-gnus-format (method &optional hashtb)
11554   ;; Parse a "groups" active file.
11555   (let ((cur (current-buffer))
11556         (hashtb (or hashtb 
11557                     (if method
11558                         gnus-active-hashtb
11559                       (setq gnus-active-hashtb
11560                             (gnus-make-hashtable 
11561                              (count-lines (point-min) (point-max)))))))
11562         (prefix (and method (not (eq method gnus-select-method))
11563                      (gnus-group-prefixed-name "" method))))
11564
11565     (goto-char (point-min))
11566     (condition-case ()
11567         ;; We split this into to separate loops, one with the prefix
11568         ;; and one without to speed the reading up somewhat.
11569         (if prefix
11570             (let (min max opoint)
11571               (while (not (eobp))
11572                 (read cur) (read cur)
11573                 (setq min (read cur)
11574                       max (read cur)
11575                       opoint (point))
11576                 (skip-chars-forward " \t")
11577                 (insert prefix)
11578                 (goto-char opoint)
11579                 (set (let ((obarray hashtb)) (read cur)) 
11580                      (cons min max))
11581                 (forward-line 1)))
11582           (let (min max opoint)
11583             (while (not (eobp))
11584               (if (= (following-char) ?2)
11585                   (progn
11586                     (read cur) (read cur)
11587                     (setq min (read cur)
11588                           max (read cur))
11589                     (set (let ((obarray hashtb)) (read cur)) 
11590                          (cons min max))))
11591               (forward-line 1))))
11592       (error 
11593        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
11594
11595 (defun gnus-read-newsrc-file (&optional force)
11596   "Read startup file.
11597 If FORCE is non-nil, the .newsrc file is read."
11598   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
11599   ;; Reset variables that might be defined in the .newsrc.eld file.
11600   (let ((variables gnus-variable-list))
11601     (while variables
11602       (set (car variables) nil)
11603       (setq variables (cdr variables))))
11604   (let* ((newsrc-file gnus-current-startup-file)
11605          (quick-file (concat newsrc-file ".el")))
11606     (save-excursion
11607       ;; We always load the .newsrc.eld file. If always contains
11608       ;; much information that can not be gotten from the .newsrc
11609       ;; file (ticked articles, killed groups, foreign methods, etc.)
11610       (gnus-read-newsrc-el-file quick-file)
11611  
11612       (if (or force
11613               (and (file-newer-than-file-p newsrc-file quick-file)
11614                    (file-newer-than-file-p newsrc-file 
11615                                            (concat quick-file "d")))
11616               (not gnus-newsrc-alist))
11617           ;; We read the .newsrc file. Note that if there if a
11618           ;; .newsrc.eld file exists, it has already been read, and
11619           ;; the `gnus-newsrc-hashtb' has been created. While reading
11620           ;; the .newsrc file, Gnus will only use the information it
11621           ;; can find there for changing the data already read -
11622           ;; ie. reading the .newsrc file will not trash the data
11623           ;; already read (except for read articles).
11624           (save-excursion
11625             (gnus-message 5 "Reading %s..." newsrc-file)
11626             (set-buffer (find-file-noselect newsrc-file))
11627             (buffer-disable-undo (current-buffer))
11628             (gnus-newsrc-to-gnus-format)
11629             (kill-buffer (current-buffer))
11630             (gnus-message 5 "Reading %s... done" newsrc-file))))))
11631
11632 (defun gnus-read-newsrc-el-file (file)
11633   (let ((ding-file (concat file "d")))
11634     ;; We always, always read the .eld file.
11635     (gnus-message 5 "Reading %s..." ding-file)
11636     (let (gnus-newsrc-assoc)
11637       (condition-case nil
11638           (load ding-file t t t)
11639         (error nil))
11640       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
11641     (let ((inhibit-quit t))
11642       (gnus-uncompress-newsrc-assoc))
11643     (gnus-make-hashtable-from-newsrc-alist)
11644     (if (not (file-newer-than-file-p file ding-file))
11645         ()
11646       ;; Old format quick file
11647       (gnus-message 5 "Reading %s..." file)
11648       ;; The .el file is newer than the .eld file, so we read that one
11649       ;; as well. 
11650       (gnus-read-old-newsrc-el-file file))))
11651
11652 ;; Parse the old-style quick startup file
11653 (defun gnus-read-old-newsrc-el-file (file)
11654   (let (newsrc killed marked group g m len info)
11655     (prog1
11656         (let ((gnus-killed-assoc nil)
11657               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
11658           (prog1
11659               (condition-case nil
11660                   (load file t t t)
11661                 (error nil))
11662             (setq newsrc gnus-newsrc-assoc
11663                   killed gnus-killed-assoc
11664                   marked gnus-marked-assoc)))
11665       (setq gnus-newsrc-alist nil)
11666       (while newsrc
11667         (setq group (car newsrc))
11668         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
11669           (if info
11670               (progn
11671                 (setcar (nthcdr 2 info) (cdr (cdr group)))
11672                 (setcar (cdr info)
11673                         (if (nth 1 group) gnus-level-default-subscribed 
11674                           gnus-level-default-unsubscribed))
11675                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
11676             (setq gnus-newsrc-alist
11677                   (cons 
11678                    (setq info
11679                          (list (car group)
11680                                (if (nth 1 group) gnus-level-default-subscribed
11681                                  gnus-level-default-unsubscribed) 
11682                                (cdr (cdr group))))
11683                    gnus-newsrc-alist)))
11684           (if (setq m (assoc (car group) marked))
11685             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
11686         (setq newsrc (cdr newsrc)))
11687       (setq newsrc killed)
11688       (while newsrc
11689         (setcar newsrc (car (car newsrc)))
11690         (setq newsrc (cdr newsrc)))
11691       (setq gnus-killed-list killed))
11692     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
11693     (gnus-make-hashtable-from-newsrc-alist)))
11694       
11695 (defun gnus-make-newsrc-file (file)
11696   "Make server dependent file name by catenating FILE and server host name."
11697   (let* ((file (expand-file-name file nil))
11698          (real-file (concat file "-" (nth 1 gnus-select-method))))
11699     (if (file-exists-p real-file)
11700         real-file file)))
11701
11702 (defun gnus-uncompress-newsrc-assoc ()
11703   ;; Uncompress all lists of marked articles in the newsrc assoc.
11704   (let ((newsrc gnus-newsrc-alist)
11705         marked)
11706     (while newsrc
11707       (if (not (setq marked (nth 3 (car newsrc))))
11708           ()
11709         (while marked
11710           (or (eq 'score (car (car marked)))
11711               (eq 'bookmark (car (car marked)))
11712               (eq 'killed (car (car marked)))
11713               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
11714           (setq marked (cdr marked))))
11715       (setq newsrc (cdr newsrc)))))
11716
11717 (defun gnus-compress-newsrc-assoc ()
11718   ;; Compress all lists of marked articles in the newsrc assoc.
11719   (let ((newsrc gnus-newsrc-alist)
11720         marked)
11721     (while newsrc
11722       (if (not (setq marked (nth 3 (car newsrc))))
11723           ()
11724         (while marked
11725           (or (eq 'score (car (car marked)))
11726               (eq 'bookmark (car (car marked)))
11727               (eq 'killed (car (car marked)))
11728               (setcdr (car marked) 
11729                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
11730           (setq marked (cdr marked))))
11731       (setq newsrc (cdr newsrc)))))
11732
11733 (defun gnus-newsrc-to-gnus-format ()
11734   (setq gnus-newsrc-options "")
11735   (or gnus-active-hashtb
11736       (setq gnus-active-hashtb (make-vector 4095 0)))
11737   (let ((buf (current-buffer))
11738         (already-read (> (length gnus-newsrc-alist) 1))
11739         group level subscribed info options-symbol newsrc
11740         symbol reads num1)
11741     (goto-char (point-min))
11742     ;; We intern the symbol `options' in the active hashtb so that we
11743     ;; can `eq' against it later.
11744     (setq options-symbol (intern "options" gnus-active-hashtb))
11745   
11746     (while (not (eobp))
11747       ;; We first read the first word on the line by narrowing and
11748       ;; then reading into `gnus-active-hashtb'.  Most groups will
11749       ;; already exist in that hashtb, so this will save some string
11750       ;; space.
11751       (narrow-to-region
11752        (point)
11753        (progn (skip-chars-forward "^ \t!:\n") (point)))
11754       (goto-char (point-min))
11755       (setq symbol 
11756             (and (/= (point-min) (point-max))
11757                  (let ((obarray gnus-active-hashtb)) (read buf))))
11758       (widen)
11759       ;; Now, the symbol we have read is either `options' or a group
11760       ;; name.  If it is an options line, we just add it to a string. 
11761       (cond 
11762        ((eq symbol options-symbol)
11763         (setq gnus-newsrc-options
11764               ;; This concatting is quite inefficient, but since our
11765               ;; thorough studies show that approx 99.37% of all
11766               ;; .newsrc files only contain a single options line, we
11767               ;; don't give a damn, frankly, my dear.
11768               (concat gnus-newsrc-options
11769                       (buffer-substring 
11770                        (gnus-point-at-bol)
11771                        ;; Options may continue on the next line.
11772                        (or (and (re-search-forward "^[^ \t]" nil 'move)
11773                                 (progn (beginning-of-line) (point)))
11774                            (point))))))
11775        (symbol
11776         ;; It was a group name.
11777         (setq subscribed (= (following-char) ?:)
11778               group (symbol-name symbol)
11779               reads nil)
11780         (if (eolp)
11781             ;; If the line ends here, this is clearly a buggy line, so
11782             ;; we put point a the beginning of line and let the cond
11783             ;; below do the error handling.
11784             (beginning-of-line)
11785           ;; We skip to the beginning of the ranges.
11786           (skip-chars-forward "!: \t"))
11787         ;; We are now at the beginning of the list of read articles.
11788         ;; We read them range by range.
11789         (while
11790             (cond 
11791              ((looking-at "[0-9]+")
11792               ;; We narrow and read a number instead of buffer-substring/
11793               ;; string-to-int because it's faster. narrow/widen is
11794               ;; faster than save-restriction/narrow, and save-restriction
11795               ;; produces a garbage object.
11796               (setq num1 (progn
11797                            (narrow-to-region (match-beginning 0) (match-end 0))
11798                            (read buf)))
11799               (widen)
11800               ;; If the next character is a dash, then this is a range.
11801               (if (= (following-char) ?-)
11802                   (progn
11803                     ;; We read the upper bound of the range.
11804                     (forward-char 1)
11805                     (if (not (looking-at "[0-9]+"))
11806                         ;; This is a buggy line, by we pretend that
11807                         ;; it's kinda OK. Perhaps the user should be
11808                         ;; dinged? 
11809                         (setq reads (cons num1 reads))
11810                       (setq reads 
11811                             (cons 
11812                              (cons num1 (progn
11813                                           (narrow-to-region (match-beginning 0) 
11814                                                             (match-end 0))
11815                                           (read buf)))
11816                              reads))
11817                       (widen)))
11818                 ;; It was just a simple number, so we add it to the
11819                 ;; list of ranges.
11820                 (setq reads (cons num1 reads)))
11821               ;; If the next char in ?\n, then we have reached the end
11822               ;; of the line and return nil.
11823               (/= (following-char) ?\n))
11824              ((= (following-char) ?\n)
11825               ;; End of line, so we end.
11826               nil)
11827              (t
11828               ;; Not numbers and not eol, so this might be a buggy
11829               ;; line... 
11830               (or (eobp) ; If it was eob instead of ?\n, we allow it.
11831                   (progn
11832                     ;; The line was buggy.
11833                     (setq group nil)
11834                     (gnus-message 3 "Mangled line: %s" 
11835                                   (buffer-substring (gnus-point-at-bol) 
11836                                                     (gnus-point-at-eol)))
11837                     (ding)
11838                     (sit-for 1)))
11839               nil))
11840           ;; Skip past ", ". Spaces are illegal in these ranges, but
11841           ;; we allow them, because it's a common mistake to put a
11842           ;; space after the comma.
11843           (skip-chars-forward ", "))
11844
11845         ;; We have already read .newsrc.eld, so we gently update the
11846         ;; data in the hash table with the information we have just
11847         ;; read. 
11848         (if (not group)
11849             ()
11850           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
11851                 level)
11852             (if info
11853                 ;; There is an entry for this file in the alist.
11854                 (progn
11855                   (setcar (nthcdr 2 info) (nreverse reads))
11856                   ;; We update the level very gently.  In fact, we
11857                   ;; only change it if there's been a status change
11858                   ;; from subscribed to unsubscribed, or vice versa.
11859                   (setq level (nth 1 info))
11860                   (cond ((and (<= level gnus-level-subscribed)
11861                               (not subscribed))
11862                          (setq level (if reads
11863                                          gnus-level-default-unsubscribed 
11864                                        (1+ gnus-level-default-unsubscribed))))
11865                         ((and (> level gnus-level-subscribed) subscribed)
11866                          (setq level gnus-level-default-subscribed)))
11867                   (setcar (cdr info) level))
11868               ;; This is a new group.
11869               (setq info (list group 
11870                                (if subscribed
11871                                    gnus-level-default-subscribed 
11872                                  (if reads
11873                                      (1+ gnus-level-subscribed)
11874                                    gnus-level-default-unsubscribed))
11875                                (nreverse reads))))
11876             (setq newsrc (cons info newsrc))))
11877         (forward-line 1))))
11878     
11879     (setq newsrc (nreverse newsrc))
11880
11881     (if (not already-read)
11882         ()
11883       ;; We now have two newsrc lists - `newsrc', which is what we
11884       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
11885       ;; what we've read from .newsrc.eld. We have to merge these
11886       ;; lists. We do this by "attaching" any (foreign) groups in the
11887       ;; gnus-newsrc-alist to the (native) group that precedes them. 
11888       (let ((rc (cdr gnus-newsrc-alist))
11889             (prev gnus-newsrc-alist)
11890             entry mentry)
11891         (while rc
11892           (or (assoc (car (car rc)) newsrc)
11893               (if (setq entry (assoc (car (car prev)) newsrc))
11894                   (setcdr (setq mentry (memq entry newsrc))
11895                           (cons (car rc) (cdr mentry)))
11896                 (setq newsrc (cons (car rc) newsrc))))
11897           (setq prev rc
11898                 rc (cdr rc)))))
11899
11900     (setq gnus-newsrc-alist newsrc)
11901     ;; We make the newsrc hashtb.
11902     (gnus-make-hashtable-from-newsrc-alist)
11903
11904     ;; Finally, if we read some options lines, we parse them.
11905     (or (string= gnus-newsrc-options "")
11906         (gnus-newsrc-parse-options gnus-newsrc-options))
11907     ))
11908
11909 ;; Parse options lines to find "options -n !all rec.all" and stuff.
11910 ;; The return value will be a list on the form
11911 ;; ((regexp1 . ignore)
11912 ;;  (regexp2 . subscribe)...)
11913 ;; When handling new newsgroups, groups that match a `ignore' regexp
11914 ;; will be ignored, and groups that match a `subscribe' regexp will be
11915 ;; subscribed. A line like
11916 ;; options -n !all rec.all
11917 ;; will lead to a list that looks like
11918 ;; (("^rec\\..+" . subscribe) 
11919 ;;  ("^.+" . ignore))
11920 ;; So all "rec.*" groups will be subscribed, while all the other
11921 ;; groups will be ignored. Note that "options -n !all rec.all" is very
11922 ;; different from "options -n rec.all !all". 
11923 (defun gnus-newsrc-parse-options (options)
11924   (let (out eol)
11925     (save-excursion
11926       (gnus-set-work-buffer)
11927       (insert (regexp-quote options))
11928       ;; First we treat all continuation lines.
11929       (goto-char (point-min))
11930       (while (re-search-forward "\n[ \t]+" nil t)
11931         (replace-match " " t t))
11932       ;; Then we transform all "all"s into ".+"s.
11933       (goto-char (point-min))
11934       (while (re-search-forward "\\ball\\b" nil t)
11935         (replace-match ".+" t t))
11936       (goto-char (point-min))
11937       ;; We remove all other options than the "-n" ones.
11938       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
11939         (replace-match " ")
11940         (forward-char -1))
11941       (goto-char (point-min))
11942
11943       ;; We are only interested in "options -n" lines - we
11944       ;; ignore the other option lines.
11945       (while (re-search-forward "[ \t]-n" nil t)
11946         (setq eol 
11947               (or (save-excursion
11948                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
11949                          (- (point) 2)))
11950                   (gnus-point-at-eol)))
11951         ;; Search for all "words"...
11952         (while (re-search-forward "[^ \t,\n-]+" eol t)
11953           (if (= (char-after (match-beginning 0)) ?!)
11954               ;; If the word begins with a bang (!), this is a "not"
11955               ;; spec. We put this spec (minus the bang) and the
11956               ;; symbol `ignore' into the list.
11957               (setq out (cons (cons (concat 
11958                                      "^" (buffer-substring 
11959                                           (1+ (match-beginning 0))
11960                                           (match-end 0)))
11961                                     'ignore) out))
11962             ;; There was no bang, so this is a "yes" spec.
11963             (setq out (cons (cons (concat 
11964                                    "^" (buffer-substring (match-beginning 0)
11965                                                          (match-end 0)))
11966                                   'subscribe) out)))))
11967     
11968       (setq gnus-newsrc-options-n out))))
11969                
11970
11971 (defun gnus-save-newsrc-file ()
11972   "Save .newsrc file."
11973   ;; Note: We cannot save .newsrc file if all newsgroups are removed
11974   ;; from the variable gnus-newsrc-alist.
11975   (and (or gnus-newsrc-alist gnus-killed-list)
11976        gnus-current-startup-file
11977        (let ((make-backup-files t)
11978              (version-control nil)
11979              (require-final-newline t)) ;Don't ask even if requested.
11980          ;; You can stop or change version control of backup file.
11981          ;; Suggested by jason@violet.berkeley.edu.
11982          (run-hooks 'gnus-save-newsrc-hook)
11983          (save-excursion
11984            (if (or (not gnus-dribble-buffer)
11985                    (not (buffer-name gnus-dribble-buffer))
11986                    (zerop (save-excursion
11987                             (set-buffer gnus-dribble-buffer)
11988                             (buffer-size))))
11989                (gnus-message 4 "(No changes need to be saved)")
11990              (if gnus-save-newsrc-file
11991                  (progn
11992                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
11993                    ;; Make backup file of master newsrc.
11994                    (gnus-gnus-to-newsrc-format)
11995                    (gnus-message 5 "Saving %s... done"
11996                                  gnus-current-startup-file)))
11997              ;; Quickly loadable .newsrc.
11998              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
11999              (gnus-add-current-to-buffer-list)
12000              (buffer-disable-undo (current-buffer))
12001              (erase-buffer)
12002              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12003              (gnus-gnus-to-quick-newsrc-format)
12004              (write-region 1 (point-max) 
12005                            (concat gnus-current-startup-file ".eld") 
12006                            nil 'nomesg)
12007              (kill-buffer (current-buffer))
12008              (gnus-message 5 "Saving %s.eld... done" gnus-current-startup-file)
12009              (gnus-dribble-delete-file))))))
12010
12011 (defun gnus-gnus-to-quick-newsrc-format ()
12012   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12013   (insert ";; (ding) Gnus startup file.\n")
12014   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12015   (insert ";; to read .newsrc.\n")
12016   (let ((variables gnus-variable-list)
12017         (inhibit-quit t)
12018         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12019         variable)
12020     ;; insert lisp expressions.
12021     (gnus-compress-newsrc-assoc)
12022     (while variables
12023       (setq variable (car variables))
12024       (and (boundp variable)
12025            (symbol-value variable)
12026            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12027            (insert "(setq " (symbol-name variable) " '"
12028                    (prin1-to-string (symbol-value variable))
12029                    ")\n"))
12030       (setq variables (cdr variables)))
12031     (gnus-uncompress-newsrc-assoc)))
12032
12033
12034 (defun gnus-gnus-to-newsrc-format ()
12035   ;; Generate and save the .newsrc file.
12036   (let ((newsrc (cdr gnus-newsrc-alist))
12037         info ranges range)
12038     (save-excursion
12039       (set-buffer (create-file-buffer gnus-startup-file))
12040       (buffer-disable-undo (current-buffer))
12041       (erase-buffer)
12042       ;; Write options.
12043       (if gnus-newsrc-options (insert gnus-newsrc-options))
12044       ;; Write subscribed and unsubscribed.
12045       (while newsrc
12046         (setq info (car newsrc))
12047         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12048             (progn
12049               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12050                                      "!" ":"))
12051               (if (setq ranges (nth 2 info))
12052                   (progn
12053                     (insert " ")
12054                     (if (not (listp (cdr ranges)))
12055                         (if (= (car ranges) (cdr ranges))
12056                             (insert (int-to-string (car ranges)))
12057                           (insert (int-to-string (car ranges)) "-" 
12058                                   (int-to-string (cdr ranges))))
12059                       (while ranges
12060                         (setq range (car ranges)
12061                               ranges (cdr ranges))
12062                         (if (or (atom range) (= (car range) (cdr range)))
12063                             (insert (int-to-string 
12064                                      (or (and (atom range) range) 
12065                                          (car range))))
12066                           (insert (int-to-string (car range)) "-"
12067                                   (int-to-string (cdr range))))
12068                         (if ranges (insert ","))))))
12069               (insert "\n")))
12070         (setq newsrc (cdr newsrc)))
12071       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12072       (kill-buffer (current-buffer)))))
12073
12074 (defun gnus-read-descriptions-file ()
12075   (gnus-message 5 "Reading descriptions file...")
12076   (cond 
12077    ((not (or (gnus-server-opened gnus-select-method)
12078              (gnus-open-server gnus-select-method)))
12079     (gnus-message 1 "Couldn't open server")
12080     nil)
12081    ((not (gnus-request-list-newsgroups gnus-select-method))
12082     (gnus-message 1 "Couldn't read newsgroups descriptions")
12083     nil)
12084    (t
12085     (let (group)
12086       (setq gnus-description-hashtb 
12087             (gnus-make-hashtable (length gnus-active-hashtb)))
12088       (save-excursion
12089         (save-restriction
12090           (set-buffer nntp-server-buffer)
12091           (goto-char (point-min))
12092           (delete-non-matching-lines "^[-\\._+A-Za-z0-9]+[ \t]")
12093           (goto-char (point-min))
12094           (if (or (search-forward "\n.\n" nil t)
12095                   (goto-char (point-max)))
12096               (progn
12097                 (beginning-of-line)
12098                 (narrow-to-region (point-min) (point))))
12099           (goto-char (point-min))
12100           (while (not (eobp))
12101             (setq group (let ((obarray gnus-description-hashtb))
12102                           (read (current-buffer))))
12103             (skip-chars-forward " \t")
12104             (and (symbolp group)
12105                  (set group (buffer-substring (point) (gnus-point-at-eol))))
12106             (forward-line 1))))
12107       (gnus-message 5 "Reading descriptions file...done")
12108       t))))
12109
12110 (defun gnus-group-get-description (group)
12111   ;; Get the description of a group by sending XGTITLE to the server.
12112   (and (gnus-request-group-description group)
12113        (save-excursion
12114          (set-buffer nntp-server-buffer)
12115          (goto-char (point-min))
12116          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12117               (buffer-substring (match-beginning 1) (match-end 1))))))
12118
12119 ;;;
12120 ;;; Server
12121 ;;;
12122
12123 (defvar gnus-server-mode-hook nil
12124   "Hook run in `gnus-server-mode' buffers.")
12125
12126 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12127   "Format of server lines.
12128 It works along the same lines as a normal formatting string,
12129 with some simple extensions.")
12130
12131 (defvar gnus-server-mode-line-format "(ding) List of servers"
12132   "The format specification for the server mode line.")
12133
12134 (defconst gnus-server-line-format-alist
12135   (list (list ?h 'how ?s)
12136         (list ?n 'name ?s)
12137         (list ?w 'where ?s)
12138         ))
12139
12140 (defconst gnus-server-mode-line-format-alist 
12141   (list (list ?S 'news-server ?s)
12142         (list ?M 'news-method ?s)
12143         (list ?u 'user-defined ?s)))
12144
12145 (defvar gnus-server-line-format-spec nil)
12146 (defvar gnus-server-mode-line-format-spec nil)
12147 (defvar gnus-server-killed-servers nil)
12148
12149 (defvar gnus-server-mode-map nil)
12150 (put 'gnus-server-mode 'mode-class 'special)
12151
12152 (if gnus-server-mode-map
12153     nil
12154   (setq gnus-server-mode-map (make-sparse-keymap))
12155   (suppress-keymap gnus-server-mode-map)
12156   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12157   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12158   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12159   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12160   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12161   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12162   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12163   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12164   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12165   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12166
12167 (defun gnus-server-mode ()
12168   "Major mode for listing and editing servers.
12169
12170 All normal editing commands are switched off.
12171 \\<gnus-server-mode-map>
12172
12173 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12174
12175 The following commands are available:
12176
12177 \\{gnus-server-mode-map}"
12178   (interactive)
12179   (if gnus-visual (gnus-server-make-menu-bar))
12180   (kill-all-local-variables)
12181   (setq mode-line-modified "-- ")
12182   (make-local-variable 'mode-line-format)
12183   (setq mode-line-format (copy-sequence mode-line-format))
12184   (and (equal (nth 3 mode-line-format) "   ")
12185        (setcar (nthcdr 3 mode-line-format) ""))
12186   (setq major-mode 'gnus-server-mode)
12187   (setq mode-name "Server")
12188 ;  (gnus-group-set-mode-line)
12189   (setq mode-line-process nil)
12190   (use-local-map gnus-server-mode-map)
12191   (buffer-disable-undo (current-buffer))
12192   (setq truncate-lines t)
12193   (setq buffer-read-only t)
12194   (run-hooks 'gnus-server-mode-hook))
12195
12196 (defun gnus-server-insert-server-line (sformat name method)
12197   (let* ((sformat (or sformat gnus-server-line-format-spec))
12198          (how (car method))
12199          (where (nth 1 method))
12200          b)
12201     (beginning-of-line)
12202     (setq b (point))
12203     ;; Insert the text.
12204     (insert (eval sformat))
12205     (add-text-properties 
12206      b (1+ b) (list 'gnus-server (intern name)))))
12207
12208 (defun gnus-server-setup-buffer ()
12209   (if (get-buffer gnus-server-buffer)
12210       ()
12211     (save-excursion
12212       (set-buffer (get-buffer-create gnus-server-buffer))
12213       (gnus-server-mode)
12214       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12215
12216 (defun gnus-server-prepare ()
12217   (setq gnus-server-mode-line-format-spec 
12218         (gnus-parse-format gnus-server-mode-line-format 
12219                            gnus-server-mode-line-format-alist))
12220   (setq gnus-server-line-format-spec 
12221         (gnus-parse-format gnus-server-line-format 
12222                            gnus-server-line-format-alist))
12223   (let ((alist gnus-server-alist)
12224         (buffer-read-only nil))
12225     (erase-buffer)
12226     (while alist
12227       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12228       (setq alist (cdr alist))))
12229   (goto-char (point-min))
12230   (gnus-server-position-cursor))
12231
12232 (defun gnus-server-server-name ()
12233   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12234     (and server (symbol-name server))))
12235
12236 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12237
12238 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12239
12240 (defun gnus-server-update-server (server)
12241   (save-excursion
12242     (set-buffer gnus-server-buffer)
12243     (let ((buffer-read-only nil)
12244           (info (cdr (assoc server gnus-server-alist))))
12245       (gnus-dribble-enter 
12246        (concat "(gnus-server-set-info \"" server "\" '"
12247                (prin1-to-string info) ")"))
12248       ;; Buffer may be narrowed.
12249       (save-restriction
12250         (widen)
12251         (if (gnus-server-goto-server server)
12252             (delete-region (progn (beginning-of-line) (point))
12253                            (progn (forward-line 1) (point))))
12254         (let ((entry (assoc server gnus-server-alist)))
12255           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12256           (gnus-server-position-cursor))))))
12257
12258 (defun gnus-server-set-info (server info)
12259   ;; Enter a select method into the virtual server alist.
12260   (gnus-dribble-enter 
12261    (concat "(gnus-server-set-info \"" server "\" '"
12262            (prin1-to-string info) ")"))
12263   (let* ((server (nth 1 info))
12264          (entry (assoc server gnus-server-alist)))
12265     (if entry (setcdr entry info)
12266       (setq gnus-server-alist
12267             (nconc gnus-server-alist (list (cons server info)))))))
12268
12269 (defun gnus-server-to-method (server)
12270   ;; Map virtual server names to select methods.
12271   (or (and (equal server "native") gnus-select-method)
12272       (cdr (assoc server gnus-server-alist))))
12273
12274 (defun gnus-server-extend-method (group method)
12275   ;; This function "extends" a virtual server.  If the server is
12276   ;; "hello", and the select method is ("hello" (my-var "something")) 
12277   ;; in the group "alt.alt", this will result in a new virtual server
12278   ;; called "helly+alt.alt".
12279   (let ((entry
12280          (gnus-copy-sequence 
12281           (if (equal (car method) "native") gnus-select-method
12282               (cdr (assoc (car method) gnus-server-alist))))))
12283     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12284     (nconc entry (cdr method))))
12285
12286 (defun gnus-server-get-method (group method)
12287   ;; Input either a server name, and extended server name, or a
12288   ;; select method, and return a select method. 
12289   (cond ((stringp method)
12290          (gnus-server-to-method method))
12291         ((stringp (car method))
12292          (gnus-server-extend-method group method))
12293         (t
12294          (gnus-server-add-address method))))
12295
12296 (defun gnus-server-add-address (method)
12297   (let ((method-name (symbol-name (car method))))
12298     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12299              (not (assq (intern (concat method-name "-address")) method)))
12300         (append method (list (list (intern (concat method-name "-address"))
12301                                    (nth 1 method))))
12302       method)))
12303
12304 (defun gnus-server-equal (s1 s2)
12305   (or (equal s1 s2)
12306       (and (= (length s1) (length s2))
12307            (progn
12308              (while (and s1 (member (car s1) s2))
12309                (setq s1 (cdr s1)))
12310              (null s1)))))
12311
12312 ;;; Interactive server functions.
12313
12314 (defun gnus-server-kill-server (server)
12315   "Kill the server on the current line."
12316   (interactive (list (gnus-server-server-name)))
12317   (or (gnus-server-goto-server server)
12318       (if server (error "No such server: %s" server)
12319         (error "No server on the current line")))
12320   (let ((buffer-read-only nil))
12321     (delete-region (progn (beginning-of-line) (point))
12322                    (progn (forward-line 1) (point))))
12323   (setq gnus-server-killed-servers 
12324         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12325   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12326                                 gnus-server-alist))
12327   (gnus-server-position-cursor))
12328
12329 (defun gnus-server-yank-server ()
12330   "Yank the previously killed server."
12331   (interactive)
12332   (or gnus-server-killed-servers
12333       (error "No killed servers to be yanked"))
12334   (let ((alist gnus-server-alist)
12335         (server (gnus-server-server-name))
12336         (killed (car gnus-server-killed-servers)))
12337     (if (not server) 
12338         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12339       (if (string= server (car (car gnus-server-alist)))
12340           (setq gnus-server-alist (cons killed gnus-server-alist))
12341         (while (and (cdr alist)
12342                     (not (string= server (car (car (cdr alist))))))
12343           (setq alist (cdr alist)))
12344         (setcdr alist (cons killed (cdr alist)))))
12345     (gnus-server-update-server (car killed))
12346     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12347     (gnus-server-position-cursor)))
12348
12349 (defun gnus-server-exit ()
12350   "Return to the group buffer."
12351   (interactive)
12352   (kill-buffer (current-buffer))
12353   (switch-to-buffer gnus-group-buffer))
12354
12355 (defun gnus-server-list-servers ()
12356   "List all available servers."
12357   (interactive)
12358   (let ((cur (gnus-server-server-name)))
12359     (gnus-server-prepare)
12360     (if cur (gnus-server-goto-server cur)
12361       (goto-char (point-max))
12362       (forward-line -1))
12363     (gnus-server-position-cursor)))
12364
12365 (defun gnus-server-copy-server (from to)
12366   (interactive
12367    (list
12368     (or (gnus-server-server-name)
12369         (error "No server on the current line"))
12370     (read-string "Copy to: ")))
12371   (or from (error "No server on current line"))
12372   (or (and to (not (string= to ""))) (error "No name to copy to"))
12373   (and (assoc to gnus-server-alist) (error "%s already exists" to))
12374   (or (assoc from gnus-server-alist) 
12375       (error "%s: no such server" from))
12376   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
12377     (setcar to-entry to)
12378     (setcar (nthcdr 2 to-entry) to)
12379     (setq gnus-server-killed-servers 
12380           (cons to-entry gnus-server-killed-servers))
12381     (gnus-server-yank-server)))
12382
12383 (defun gnus-server-add-server (how where)
12384   (interactive 
12385    (list (intern (completing-read "Server method: "
12386                                   gnus-valid-select-methods nil t))
12387          (read-string "Server name: ")))
12388   (setq gnus-server-killed-servers 
12389         (cons (list where how where) gnus-server-killed-servers))
12390   (gnus-server-yank-server))
12391
12392 (defun gnus-server-goto-server (server)
12393   "Jump to a server line."
12394   (interactive
12395    (list (completing-read "Goto server: " gnus-server-alist nil t)))
12396   (let ((to (text-property-any (point-min) (point-max) 
12397                                'gnus-server (intern server))))
12398     (and to
12399          (progn
12400            (goto-char to) 
12401            (gnus-server-position-cursor)))))
12402
12403 (defun gnus-server-edit-server (server)
12404   "Edit the server on the current line."
12405   (interactive (list (gnus-server-server-name)))
12406   (or server
12407       (error "No server on current line"))
12408   (let ((winconf (current-window-configuration)))
12409     (get-buffer-create gnus-server-edit-buffer)
12410     (gnus-configure-windows 'edit-server)
12411     (gnus-add-current-to-buffer-list)
12412     (emacs-lisp-mode)
12413     (make-local-variable 'gnus-prev-winconf)
12414     (setq gnus-prev-winconf winconf)
12415     (use-local-map (copy-keymap (current-local-map)))
12416     (let ((done-func '(lambda () 
12417                         "Exit editing mode and update the information."
12418                         (interactive)
12419                         (gnus-server-edit-server-done 'group))))
12420       (setcar (cdr (nth 4 done-func)) server)
12421       (local-set-key "\C-c\C-c" done-func))
12422     (erase-buffer)
12423     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
12424     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
12425
12426 (defun gnus-server-edit-server-done (server)
12427   (interactive)
12428   (set-buffer (get-buffer-create gnus-server-edit-buffer))
12429   (goto-char (point-min))
12430   (let ((form (read (current-buffer)))
12431         (winconf gnus-prev-winconf))
12432     (gnus-server-set-info server form)
12433     (kill-buffer (current-buffer))
12434     (and winconf (set-window-configuration winconf))
12435     (set-buffer gnus-server-buffer)
12436     (gnus-server-update-server (gnus-server-server-name))
12437     (gnus-server-position-cursor)))
12438
12439 (defun gnus-server-read-server (server)
12440   "Browse a server."
12441   (interactive (list (gnus-server-server-name)))
12442   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
12443
12444 (defun gnus-mouse-pick-server (e)
12445   (interactive "e")
12446   (mouse-set-point e)
12447   (gnus-server-read-server (gnus-server-server-name)))
12448
12449 ;; Allow redefinition of Gnus functions.
12450
12451 (gnus-ems-redefine)
12452
12453 (provide 'gnus)
12454
12455 ;;; gnus.el ends here