a56b6b64a7b42e185b1216ff7ab675a70adf8c58
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select methdod (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use mutliple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algortihm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread nil
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-group-default-list-level gnus-level-subscribed
541   "*Default listing level. 
542 Ignored if `gnus-group-use-permanent-levels' is nil.")
543
544 (defvar gnus-group-use-permanent-levels nil
545   "*If non-nil, once you set a level, Gnus will use this level.")
546
547 (defvar gnus-show-mime nil
548   "*If non-ni, do mime processing of articles.
549 The articles will simply be fed to the function given by
550 `gnus-show-mime-method'.")
551
552 (defvar gnus-show-mime-method (function metamail-buffer)
553   "*Function to process a MIME message.
554 The function is called from the article buffer.")
555
556 (defvar gnus-show-threads t
557   "*If non-nil, display threads in summary mode.")
558
559 (defvar gnus-thread-hide-subtree nil
560   "*If non-nil, hide all threads initially.
561 If threads are hidden, you have to run the command
562 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
563 to expose hidden threads.")
564
565 (defvar gnus-thread-hide-killed t
566   "*If non-nil, hide killed threads automatically.")
567
568 (defvar gnus-thread-ignore-subject nil
569   "*If non-nil, ignore subjects and do all threading based on the Reference header.
570 If nil, which is the default, articles that have different subjects
571 from their parents will start separate threads.")
572
573 (defvar gnus-thread-indent-level 4
574   "*Number that says how much each sub-thread should be indented.")
575
576 (defvar gnus-ignored-newsgroups ""
577   "*A regexp to match uninteresting newsgroups in the active file.
578 Any lines in the active file matching this regular expression are
579 removed from the newsgroup list before anything else is done to it,
580 thus making them effectively non-existant.")
581
582 (defvar gnus-ignored-headers
583   "^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:"
584   "*All headers that match this regexp will be hidden.
585 Also see `gnus-visible-headers'.")
586
587 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
588   "*All headers that do not match this regexp will be hidden.
589 Also see `gnus-ignored-headers'.")
590
591 (defvar gnus-sorted-header-list
592   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
593     "^Cc:" "^Date:" "^Organization:")
594   "*This variable is a list of regular expressions.
595 If it is non-nil, headers that match the regular expressions will
596 be placed first in the article buffer in the sequence specified by
597 this list.")
598
599 (defvar gnus-show-all-headers nil
600   "*If non-nil, don't hide any headers.")
601
602 (defvar gnus-save-all-headers t
603   "*If non-nil, don't remove any headers before saving.")
604
605 (defvar gnus-inhibit-startup-message nil
606   "*If non-nil, the startup message will not be displayed.")
607
608 (defvar gnus-signature-separator "^-- *$"
609   "Regexp matching signature separator.")
610
611 (defvar gnus-auto-extend-newsgroup t
612   "*If non-nil, extend newsgroup forward and backward when requested.")
613
614 (defvar gnus-auto-select-first t
615   "*If non-nil, select the first unread article when entering a group.
616 If you want to prevent automatic selection of the first unread article
617 in some newsgroups, set the variable to nil in
618 `gnus-select-group-hook'.") 
619
620 (defvar gnus-auto-select-next t
621   "*If non-nil, offer to go to the next group from the end of the previous.
622 If the value is t and the next newsgroup is empty, Gnus will exit
623 summary mode and go back to group mode.  If the value is neither nil
624 nor t, Gnus will select the following unread newsgroup.  In
625 particular, if the value is the symbol `quietly', the next unread
626 newsgroup will be selected without any confirmations.")
627
628 (defvar gnus-auto-select-same nil
629   "*If non-nil, select the next article with the same subject.")
630
631 (defvar gnus-summary-check-current nil
632   "*If non-nil, consider the current article when moving.
633 The \"unread\" movement commands will stay on the same line if the
634 current article is unread.")
635
636 (defvar gnus-auto-center-summary t
637   "*If non-nil, always center the current summary buffer.")
638
639 (defvar gnus-break-pages t
640   "*If non-nil, do page breaking on articles.
641 The page delimiter is specified by the `gnus-page-delimiter'
642 variable.")
643
644 (defvar gnus-page-delimiter "^\^L"
645   "*Regexp describing what to use as article page delimiters.
646 The default value is \"^\^L\", which is a form linefeed at the
647 beginning of a line.")
648
649 (defvar gnus-use-full-window t
650   "*If non-nil, use the entire Emacs screen.")
651
652 (defvar gnus-window-configuration nil
653   "Obsolete variable.  See `gnus-buffer-configuration'.")
654
655 (defvar gnus-buffer-configuration
656   '((group ([group 1.0 point] 
657             (if gnus-carpal [group-carpal 4])))
658     (summary ([summary 1.0 point]
659               (if gnus-carpal [summary-carpal 4])))
660     (article ([summary 0.25 point] 
661               (if gnus-carpal [summary-carpal 4]) 
662               [article 1.0]))
663     (server ([server 1.0 point]
664              (if gnus-carpal [server-carpal 2])))
665     (browse ([browse 1.0 point]
666              (if gnus-carpal [browse-carpal 2])))
667     (group-mail ([mail 1.0 point]))
668     (summary-mail ([mail 1.0 point]))
669     (summary-reply ([article 0.5]
670                     [mail 1.0 point]))
671     (info ([nil 1.0 point]))
672     (summary-faq ([summary 0.25]
673                   [article 1.0 point]))
674     (edit-group ([group 0.5]
675                  [edit-group 1.0 point]))
676     (edit-server ([server 0.5]
677                   [edit-server 1.0 point]))
678     (post ([post 1.0 point]))
679     (reply ([article 0.5]
680             [mail 1.0 point]))
681     (mail-forward ([mail 1.0 point]))
682     (post-forward ([post 1.0 point]))
683     (reply-yank ([mail 1.0 point]))
684     (followup ([article 0.5]
685                [post 1.0 point]))
686     (followup-yank ([post 1.0 point])))
687   "Window configuration for all possible Gnus buffers.
688 This variable is a list of lists.  Each of these lists has a NAME and
689 a RULE.  The NAMEs are commonsense names like `group', which names a
690 rule used when displaying the group buffer; `summary', which names a
691 rule for what happens when you enter a group and do not display an
692 article buffer; and so on.  See the value of this variable for a
693 complete list of NAMEs.
694
695 Each RULE is a list of vectors.  The first element in this vector is
696 the name of the buffer to be displayed; the second element is the
697 percentage of the screen this buffer is to occupy (a number in the
698 0.0-0.99 range); the optional third element is `point', which should
699 be present to denote which buffer point is to go to after making this
700 buffer configuration.")
701
702 (defvar gnus-window-to-buffer
703   '((group . gnus-group-buffer)
704     (summary . gnus-summary-buffer)
705     (article . gnus-article-buffer)
706     (server . gnus-server-buffer)
707     (browse . "*Gnus Browse Server*")
708     (edit-group . gnus-group-edit-buffer)
709     (edit-server . gnus-server-edit-buffer)
710     (group-carpal . gnus-carpal-group-buffer)
711     (summary-carpal . gnus-carpal-summary-buffer)
712     (server-carpal . gnus-carpal-server-buffer)
713     (browse-carpal . gnus-carpal-browse-buffer)
714     (mail . "*mail*")
715     (post . gnus-post-news-buffer))
716   "Mapping from short symbols to buffer names or buffer variables.")
717
718 (defvar gnus-carpal nil
719   "*If non-nil, display clickable icons.")
720
721 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
722   "*Function called with a group name when new group is detected.
723 A few pre-made functions are supplied: `gnus-subscribe-randomly'
724 inserts new groups at the beginning of the list of groups;
725 `gnus-subscribe-alphabetically' inserts new groups in strict
726 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
727 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
728 for your decision.")
729
730 ;; Suggested by a bug report by Hallvard B Furuseth.
731 ;; <h.b.furuseth@usit.uio.no>. 
732 (defvar gnus-subscribe-options-newsgroup-method
733   (function gnus-subscribe-alphabetically)
734   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
735 If, for instance, you want to subscribe to all newsgroups in the
736 \"no\" and \"alt\" hierarchies, you'd put the following in your
737 .newsrc file:
738
739 options -n no.all alt.all
740
741 Gnus will the subscribe all new newsgroups in these hierarchies with
742 the subscription method in this variable.")
743
744 (defvar gnus-subscribe-hierarchical-interactive nil
745   "*If non-nil, Gnus will offer to subscribe hierarchically.
746 When a new hierarchy appears, Gnus will ask the user:
747
748 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
749
750 If the user pressed `d', Gnus will descend the hierarchy, `y' will
751 subscribe to all newsgroups in the hierarchy and `s' will skip this
752 hierarchy in its entirety.")
753
754 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
755   "*Function used for sorting the group buffer.
756 This function will be called with group info entries as the arguments
757 for the groups to be sorted.  Pre-made functions include
758 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
759 `gnus-sort-by-level'")
760
761 ;; Mark variables suggested by Thomas Michanek
762 ;; <Thomas.Michanek@telelogic.se>. 
763 (defvar gnus-unread-mark ? 
764   "*Mark used for unread articles.")
765 (defvar gnus-ticked-mark ?!
766   "*Mark used for ticked articles.")
767 (defvar gnus-dormant-mark ??
768   "*Mark used for dormant articles.")
769 (defvar gnus-del-mark ?D
770   "*Mark used for del'd articles.")
771 (defvar gnus-read-mark ?d
772   "*Mark used for read articles.")
773 (defvar gnus-expirable-mark ?E
774   "*Mark used for expirable articles.")
775 (defvar gnus-killed-mark ?K
776   "*Mark used for killed articles.")
777 (defvar gnus-kill-file-mark ?X
778   "*Mark used for articles killed by kill files.")
779 (defvar gnus-low-score-mark ?Y
780   "*Mark used for articles with a low score.")
781 (defvar gnus-catchup-mark ?C
782   "*Mark used for articles that are caught up.")
783 (defvar gnus-replied-mark ?R
784   "*Mark used for articles that have been replied to.")
785 (defvar gnus-process-mark ?# 
786   "*Process mark.")
787 (defvar gnus-ancient-mark ?A
788   "*Mark used for ancient articles.")
789 (defvar gnus-canceled-mark ?G
790   "*Mark used for cancelled articles.")
791 (defvar gnus-score-over-mark ?+
792   "*Score mark used for articles with high scores.")
793 (defvar gnus-score-below-mark ?-
794   "*Score mark used for articles with low scores.")
795 (defvar gnus-empty-thread-mark ? 
796   "*There is no thread under the article.")
797 (defvar gnus-not-empty-thread-mark ?=
798   "*There is a thread under the article.")
799 (defvar gnus-dummy-mark ?Z
800   "*This is a dummy article.")
801
802 (defvar gnus-view-pseudo-asynchronously nil
803   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
804
805 (defvar gnus-view-pseudos nil
806   "*If `automatic', pseudo-articles will be viewed automatically.
807 If `not-confirm', pseudos will be viewed automatically, and the user
808 will not be asked to confirm the command.")
809
810 (defvar gnus-view-pseudos-separately t
811   "*If non-nil, one pseudo-article will be created for each file to be viewed.
812 If nil, all files that use the same viewing command will be given as a
813 list of parameters to that command.")
814
815 (defvar gnus-group-line-format "%M%S%5y: %(%g%)\n"
816   "*Format of group lines.
817 It works along the same lines as a normal formatting string,
818 with some simple extensions.
819
820 %M    Only marked articles (character, \"*\" or \" \")
821 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
822 %L    Level of subscribedness (integer)
823 %N    Number of unread articles (integer)
824 %I    Number of dormant articles (integer)
825 %i    Number of ticked and dormant (integer)
826 %T    Number of ticked articles (integer)
827 %R    Number of read articles (integer)
828 %t    Total number of articles (integer)
829 %y    Number of unread, unticked articles (integer)
830 %G    Group name (string)
831 %g    Qualified group name (string)
832 %D    Group description (string)
833 %s    Select method (string)
834 %o    Moderated group (char, \"m\")
835 %O    Moderated group (string, \"(m)\" or \"\")
836 %n    Select from where (string)
837 %z    A string that look like `<%s:%n>' if a foreign select method is used
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 buffer just like information from any other
843       group specifier.
844
845 Text between %( and %) will be highlighted with `gnus-mouse-face' when
846 the mouse point move inside the area.  There can only be one such area.
847
848 Note that this format specification is not always respected. For
849 reasons of efficiency, when listing killed groups, this specification
850 is ignored altogether. If the spec is changed considerably, your
851 output may end up looking strange when listing both alive and killed
852 groups.
853
854 If you use %o or %O, reading the active file will be slower and quite
855 a bit of extra memory will be used. %D will also worsen performance.
856 Also note that if you change the format specification to include any
857 of these specs, you must probably re-start Gnus to see them go into
858 effect.") 
859
860 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
861   "*The format specification of the lines in the summary buffer.
862
863 It works along the same lines as a normal formatting string,
864 with some simple extensions.
865
866 %N   Article number, left padded with spaces (string)
867 %S   Subject (string)
868 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
869 %n   Name of the poster (string)
870 %A   Address of the poster (string)
871 %F   Contents of the From: header (string)
872 %x   Contents of the Xref: header (string)
873 %D   Date of the article (string)
874 %d   Date of the article (string) in DD-MMM format
875 %M   Message-id of the article (string)
876 %r   References of the article (string)
877 %c   Number of characters in the article (integer)
878 %L   Number of lines in the article (integer)
879 %I   Indentation based on thread level (a string of spaces)
880 %T   A string with two possible values: 80 spaces if the article
881      is on thread level two or larger and 0 spaces on level one
882 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
883 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
884 %[   Opening bracket (character, \"[\" or \"<\")
885 %]   Closing bracket (character, \"]\" or \">\")
886 %>   Spaces of length thread-level (string)
887 %<   Spaces of length (- 20 thread-level) (string)
888 %i   Article score (number)
889 %z   Article zcore (character)
890 %t   Number of articles under the current thread (number).
891 %e   Whether the thread is empty or not (character).
892 %u   User defined specifier. The next character in the format string should
893      be a letter.  Gnus will call the function gnus-user-format-function-X,
894      where X is the letter following %u. The function will be passed the
895      current header as argument. The function should return a string, which
896      will be inserted into the summary just like information from any other
897      summary specifier.
898
899 Text between %( and %) will be highlighted with `gnus-mouse-face'
900 when the mouse point is placed inside the area.  There can only be one
901 such area.
902
903 The %U (status), %R (replied) and %z (zcore) specs have to be handled
904 with care. For reasons of efficiency, Gnus will compute what column
905 these characters will end up in, and \"hard-code\" that. This means that
906 it is illegal to have these specs after a variable-length spec. Well,
907 you might not be arrested, but your summary buffer will look strange,
908 which is bad enough.
909
910 The smart choice is to have these specs as for to the left as
911 possible. 
912
913 This restriction may disappear in later versions of Gnus.")
914
915 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
916   "*The format specification for the dummy roots in the summary buffer.
917 It works along the same lines as a normal formatting string,
918 with some simple extensions.
919
920 %S  The subject")
921
922 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
923   "*The format specification for the summary mode line.")
924
925 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
926   "*The format specification for the article mode line.")
927
928 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
929   "*The format specification for the group mode line.")
930
931 (defvar gnus-valid-select-methods
932   '(("nntp" post address prompt-address)
933     ("nnspool" post)
934     ("nnvirtual" none virtual prompt-address) 
935     ("nnmbox" mail respool) 
936     ("nnml" mail respool)
937     ("nnmh" mail respool) 
938     ("nndir" none prompt-address address)
939     ("nneething" none prompt-address)
940     ("nndigest" none) 
941     ("nndoc" none prompt-address) 
942     ("nnbabyl" mail respool) 
943     ("nnkiboze" none virtual) 
944     ("nnfolder" mail respool))
945   "An alist of valid select methods.
946 The first element of each list lists should be a string with the name
947 of the select method. The other elements may be be the category of
948 this method (ie. `post', `mail', `none' or whatever) or other
949 properties that this method has (like being respoolable).
950 If you implement a new select method, all you should have to change is
951 this variable. I think.")
952
953 (defvar gnus-updated-mode-lines '(group article summary)
954   "*List of buffers that should update their mode lines.
955 The list may contain the symbols `group', `article' and `summary'. If
956 the corresponding symbol is present, Gnus will keep that mode line
957 updated with information that may be pertinent. 
958 If this variable is nil, screen refresh may be quicker.")
959
960 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
961 (defvar gnus-mode-non-string-length 21
962   "*Max length of mode-line non-string contents.
963 If this is nil, Gnus will take space as is needed, leaving the rest
964 of the modeline intact.")
965
966 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
967 (defvar gnus-mouse-face 'highlight
968   "*Face used for mouse highlighting in Gnus.
969 No mouse highlights will be done if `gnus-visual' is nil.")
970
971 (defvar gnus-summary-mark-below nil
972   "*Mark all articles with a score below this variable as read.
973 This variable is local to each summary buffer and usually set by the
974 score file.")  
975
976 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
977   "*List of functions used for sorting threads in the summary buffer.
978 By default, threads are sorted by article number.
979
980 Each function takes two threads and return non-nil if the first thread
981 should be sorted before the other.  If you use more than one function,
982 the primary sort function should be the last.
983
984 Ready-mady functions include `gnus-thread-sort-by-number',
985 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
986 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
987 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
988
989 (defvar gnus-thread-score-function '+
990   "*Function used for calculating the total score of a thread.
991
992 The function is called with the scores of the article and each
993 subthread and should then return the score of the thread.
994
995 Some functions you can use are `+', `max', or `min'.")
996
997 (defvar gnus-options-subscribe nil
998   "*All new groups matching this regexp will be subscribed unconditionally.
999 Note that this variable deals only with new newsgroups.  This variable
1000 does not affect old newsgroups.")
1001
1002 (defvar gnus-options-not-subscribe nil
1003   "*All new groups matching this regexp will be ignored.
1004 Note that this variable deals only with new newsgroups.  This variable
1005 does not affect old (already subscribed) newsgroups.")
1006
1007 (defvar gnus-auto-expirable-newsgroups nil
1008   "*Groups in which to automatically mark read articles as expirable.
1009 If non-nil, this should be a regexp that should match all groups in
1010 which to perform auto-expiry.  This only makes sense for mail groups.")
1011
1012 (defvar gnus-hidden-properties '(invisible t intangible t)
1013   "Property list to use for hiding text.")
1014
1015 ;; Hooks.
1016
1017 (defvar gnus-group-mode-hook nil
1018   "*A hook for Gnus group mode.")
1019
1020 (defvar gnus-summary-mode-hook nil
1021   "*A hook for Gnus summary mode.")
1022
1023 (defvar gnus-article-mode-hook nil
1024   "*A hook for Gnus article mode.")
1025
1026 (defvar gnus-open-server-hook nil
1027   "*A hook called just before opening connection to the news server.")
1028
1029 (defvar gnus-startup-hook nil
1030   "*A hook called at startup.
1031 This hook is called after Gnus is connected to the NNTP server.")
1032
1033 (defvar gnus-get-new-news-hook nil
1034   "*A hook run just before Gnus checks for new news.")
1035
1036 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1037   "*A function that is called to generate the group buffer.
1038 The function is called with three arguments: The first is a number;
1039 all group with a level less or equal to that number should be listed,
1040 if the second is non-nil, empty groups should also be displayed. If
1041 the third is non-nil, it is a number. No groups with a level lower
1042 than this number should be displayed.
1043
1044 The only current function implemented is `gnus-group-prepare-flat'.")
1045
1046 (defvar gnus-group-prepare-hook nil
1047   "*A hook called after the group buffer has been generated.
1048 If you want to modify the group buffer, you can use this hook.")
1049
1050 (defvar gnus-summary-prepare-hook nil
1051   "*A hook called after the summary buffer has been generated.
1052 If you want to modify the summary buffer, you can use this hook.")
1053
1054 (defvar gnus-article-prepare-hook nil
1055   "*A hook called after an article has been prepared in the article buffer.
1056 If you want to run a special decoding program like nkf, use this hook.")
1057
1058 (defvar gnus-article-display-hook nil
1059   "*A hook called after the article is displayed in the article buffer.
1060 The hook is designed to change the contents of the article
1061 buffer. Typical functions that this hook may contain are
1062 `gnus-article-hide-headers' (hide selected headers),
1063 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1064 `gnus-article-hide-signature' (hide signature) and
1065 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1066 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1067 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1068
1069 (defvar gnus-article-x-face-command
1070   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1071   "String or function to be executed to display an X-Face header.
1072 If it is a string, the command will be executed in a sub-shell
1073 asynchronously. The compressed face will be piped to this command.") 
1074
1075 (defvar gnus-article-x-face-too-ugly nil
1076   "Regexp matching posters whose face shouldn't be shown automatically.")
1077
1078 (defvar gnus-select-group-hook nil
1079   "*A hook called when a newsgroup is selected.
1080
1081 If you'd like to simplify subjects like the
1082 `gnus-summary-next-same-subject' command does, you can use the
1083 following hook:
1084
1085  (setq gnus-select-group-hook
1086       (list
1087         (lambda ()
1088           (mapcar (lambda (header)
1089                      (header-set-subject
1090                       header
1091                       (gnus-simplify-subject
1092                        (header-subject header) 're-only)))
1093                   gnus-newsgroup-headers))))")
1094
1095 (defvar gnus-select-article-hook
1096   '(gnus-summary-show-thread)
1097   "*A hook called when an article is selected.
1098 The default hook shows conversation thread subtrees of the selected
1099 article automatically using `gnus-summary-show-thread'.")
1100
1101 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1102   "*A hook called to apply kill files to a group.
1103 This hook is intended to apply a kill file to the selected newsgroup.
1104 The function `gnus-apply-kill-file' is called by default.
1105
1106 Since a general kill file is too heavy to use only for a few
1107 newsgroups, I recommend you to use a lighter hook function. For
1108 example, if you'd like to apply a kill file to articles which contains
1109 a string `rmgroup' in subject in newsgroup `control', you can use the
1110 following hook:
1111
1112 \(setq gnus-apply-kill-hook
1113       (list
1114         (lambda ()
1115           (cond ((string-match \"control\" gnus-newsgroup-name)
1116                  (gnus-kill \"Subject\" \"rmgroup\")
1117                  (gnus-expunge \"X\"))))))")
1118
1119 (defvar gnus-visual-mark-article-hook 
1120   (list 'gnus-highlight-selected-summary)
1121   "*Hook run after selecting an article in the summary buffer.
1122 It is meant to be used for highlighting the article in some way.  It
1123 is not run if `gnus-visual' is nil.")
1124
1125 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1126   "*A hook called after preparing body, but before preparing header headers.
1127 The default hook (`gnus-inews-insert-signature') inserts a signature
1128 file specified by the variable `gnus-signature-file'.")
1129
1130 (defvar gnus-exit-group-hook nil
1131   "*A hook called when exiting (not quitting) summary mode.")
1132
1133 (defvar gnus-suspend-gnus-hook nil
1134   "*A hook called when suspending (not exiting) Gnus.")
1135
1136 (defvar gnus-exit-gnus-hook nil
1137   "*A hook called when exiting Gnus.")
1138
1139 (defvar gnus-save-newsrc-hook nil
1140   "*A hook called when saving the newsrc file.")
1141
1142 (defvar gnus-summary-update-hook 
1143   (list 'gnus-summary-highlight-line)
1144   "*A hook called when a summary line is changed.
1145 The hook will not be called if `gnus-visual' is nil.
1146
1147 The default function `gnus-summary-highlight-line' will
1148 highlight the line according to the `gnus-summary-highlight'
1149 variable.")
1150
1151 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1152   "*A hook called when an article is selected for the first time.
1153 The hook is intended to mark an article as read (or unread)
1154 automatically when it is selected.")
1155
1156 \f
1157 ;; Internal variables
1158
1159 ;; Avoid highlighting in kill files.
1160 (defvar gnus-summary-inhibit-highlight nil)
1161 (defvar gnus-newsgroup-selected-overlay nil)
1162
1163 (defvar gnus-article-mode-map nil)
1164 (defvar caesar-translate-table nil)
1165 (defvar gnus-dribble-buffer nil)
1166 (defvar gnus-headers-retrieved-by nil)
1167 (defvar gnus-article-reply nil)
1168 (defvar gnus-override-method nil)
1169 (defvar gnus-article-check-size nil)
1170
1171 (defvar gnus-current-score-file nil)
1172 (defvar gnus-internal-global-score-files nil)
1173 (defvar gnus-score-file-list nil)
1174
1175
1176 (defvar gnus-current-move-group nil)
1177
1178 (defvar gnus-newsgroup-dependencies nil)
1179 (defvar gnus-newsgroup-threads nil)
1180 (defvar gnus-newsgroup-async nil)
1181 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1182
1183 (defvar gnus-newsgroup-adaptive nil)
1184
1185 (defvar gnus-summary-display-table nil)
1186
1187 (defconst gnus-group-line-format-alist
1188   (list (list ?M 'marked ?c)
1189         (list ?S 'subscribed ?c)
1190         (list ?L 'level ?d)
1191         (list ?N 'number ?s)
1192         (list ?I 'number-of-dormant ?d)
1193         (list ?T 'number-of-ticked ?d)
1194         (list ?R 'number-of-read ?s)
1195         (list ?t 'number-total ?d)
1196         (list ?y 'number-of-unread-unticked ?s)
1197         (list ?i 'number-of-ticked-and-dormant ?d)
1198         (list ?g 'group ?s)
1199         (list ?G 'qualified-group ?s)
1200         (list ?D 'newsgroup-description ?s)
1201         (list ?o 'moderated ?c)
1202         (list ?O 'moderated-string ?s)
1203         (list ?s 'news-server ?s)
1204         (list ?n 'news-method ?s)
1205         (list ?z 'news-method-string ?s)
1206         (list ?u 'user-defined ?s)))
1207
1208 (defconst gnus-summary-line-format-alist 
1209   (list (list ?N 'number ?d)
1210         (list ?S 'subject ?s)
1211         (list ?s 'subject-or-nil ?s)
1212         (list ?n 'name ?s)
1213         (list ?A 'address ?s)
1214         (list ?F 'from ?s)
1215         (list ?x (macroexpand '(header-xref header)) ?s)
1216         (list ?D (macroexpand '(header-date header)) ?s)
1217         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1218         (list ?M (macroexpand '(header-id header)) ?s)
1219         (list ?r (macroexpand '(header-references header)) ?s)
1220         (list ?c '(or (header-chars header) 0) ?d)
1221         (list ?L 'lines ?d)
1222         (list ?I 'indentation ?s)
1223         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1224         (list ?R 'replied ?c)
1225         (list ?\[ 'opening-bracket ?c)
1226         (list ?\] 'closing-bracket ?c)
1227         (list ?\> '(make-string level ? ) ?s)
1228         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1229         (list ?i 'score ?d)
1230         (list ?z 'score-char ?c)
1231         (list ?U 'unread ?c)
1232         (list ?t '(gnus-summary-number-of-articles-in-thread 
1233                    (or (prog1 gnus-tmp-adopt-thread 
1234                          (setq gnus-tmp-adopt-thread nil))
1235                        (if (boundp 'thread) (symbol-value 'thread)
1236                          thread nil)))
1237                    ?d)
1238         (list ?e '(gnus-summary-number-of-articles-in-thread 
1239                    (or gnus-tmp-adopt-thread 
1240                        (if (boundp 'thread) (symbol-value 'thread)
1241                          thread nil)) t)
1242                    ?d)
1243         (list ?u 'user-defined ?s))
1244   "An alist of format specifications that can appear in summary lines,
1245 and what variables they correspond with, along with the type of the
1246 variable (string, integer, character, etc).")
1247
1248 (defconst gnus-summary-dummy-line-format-alist
1249   (list (list ?S 'subject ?s)
1250         (list ?N 'number ?d)
1251         (list ?u 'user-defined ?s)))
1252
1253 (defconst gnus-summary-mode-line-format-alist 
1254   (list (list ?G 'group-name ?s)
1255         (list ?g '(gnus-short-group-name group-name) ?s)
1256         (list ?A 'article-number ?d)
1257         (list ?Z 'unread-and-unselected ?s)
1258         (list ?V 'gnus-version ?s)
1259         (list ?U 'unread ?d)
1260         (list ?S 'subject ?s)
1261         (list ?e 'unselected ?d)
1262         (list ?u 'user-defined ?s)
1263         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1264
1265 (defconst gnus-group-mode-line-format-alist 
1266   (list (list ?S 'news-server ?s)
1267         (list ?M 'news-method ?s)
1268         (list ?u 'user-defined ?s)))
1269
1270 (defvar gnus-have-read-active-file nil)
1271
1272 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls & Boys)"
1273   "The mail address of the Gnus maintainer.")
1274
1275 (defconst gnus-version "(ding) Gnus v0.78"
1276   "Version number for this version of Gnus.")
1277
1278 (defvar gnus-info-nodes
1279   '((gnus-group-mode            "(gnus)The Group Buffer")
1280     (gnus-summary-mode          "(gnus)The Summary Buffer")
1281     (gnus-article-mode          "(gnus)The Article Buffer"))
1282   "Assoc list of major modes and related Info nodes.")
1283
1284 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1285   "The location of the (ding) Gnus documentation group.")
1286
1287 (defvar gnus-group-buffer "*Group*")
1288 (defvar gnus-summary-buffer "*Summary*")
1289 (defvar gnus-article-buffer "*Article*")
1290 (defvar gnus-server-buffer "*Server*")
1291
1292 (defvar gnus-work-buffer " *gnus work*")
1293
1294 (defvar gnus-buffer-list nil
1295   "Gnus buffers that should be killed on exit.")
1296
1297 (defvar gnus-server-alist nil
1298   "List of available servers.")
1299
1300 (defvar gnus-variable-list
1301   '(gnus-newsrc-options gnus-newsrc-options-n
1302     gnus-newsrc-last-checked-date 
1303     gnus-newsrc-alist gnus-server-alist
1304     gnus-killed-list gnus-zombie-list)
1305   "Gnus variables saved in the quick startup file.")
1306
1307 (defvar gnus-overload-functions
1308   '((news-inews gnus-inews-news "rnewspost")
1309     (caesar-region gnus-caesar-region "rnews"))
1310   "Functions overloaded by gnus.
1311 It is a list of `(original overload &optional file)'.")
1312
1313 (defvar gnus-newsrc-options nil
1314   "Options line in the .newsrc file.")
1315
1316 (defvar gnus-newsrc-options-n nil
1317   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1318
1319 (defvar gnus-newsrc-last-checked-date nil
1320   "Date Gnus last asked server for new newsgroups.")
1321
1322 (defvar gnus-newsrc-alist nil
1323   "Assoc list of read articles.
1324 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1325
1326 (defvar gnus-newsrc-hashtb nil
1327   "Hashtable of gnus-newsrc-alist.")
1328
1329 (defvar gnus-killed-list nil
1330   "List of killed newsgroups.")
1331
1332 (defvar gnus-killed-hashtb nil
1333   "Hash table equivalent of gnus-killed-list.")
1334
1335 (defvar gnus-zombie-list nil
1336   "List of almost dead newsgroups.")
1337
1338 (defvar gnus-description-hashtb nil
1339   "Descriptions of newsgroups.")
1340
1341 (defvar gnus-list-of-killed-groups nil
1342   "List of newsgroups that have recently been killed by the user.")
1343
1344 (defvar gnus-active-hashtb nil
1345   "Hashtable of active articles.")
1346
1347 (defvar gnus-moderated-list nil
1348   "List of moderated newsgroups.")
1349
1350 (defvar gnus-group-marked nil)
1351
1352 (defvar gnus-current-startup-file nil
1353   "Startup file for the current host.")
1354
1355 (defvar gnus-last-search-regexp nil
1356   "Default regexp for article search command.")
1357
1358 (defvar gnus-last-shell-command nil
1359   "Default shell command on article.")
1360
1361 (defvar gnus-current-select-method nil
1362   "The current method for selecting a newsgroup.")
1363
1364 (defvar gnus-have-all-newsgroups nil)
1365
1366 (defvar gnus-article-internal-prepare-hook nil)
1367
1368 (defvar gnus-newsgroup-name nil)
1369 (defvar gnus-newsgroup-begin nil)
1370 (defvar gnus-newsgroup-end nil)
1371 (defvar gnus-newsgroup-last-rmail nil)
1372 (defvar gnus-newsgroup-last-mail nil)
1373 (defvar gnus-newsgroup-last-folder nil)
1374 (defvar gnus-newsgroup-last-file nil)
1375 (defvar gnus-newsgroup-auto-expire nil)
1376 (defvar gnus-newsgroup-active nil)
1377
1378 (defvar gnus-newsgroup-unreads nil
1379   "List of unread articles in the current newsgroup.")
1380
1381 (defvar gnus-newsgroup-unselected nil
1382   "List of unselected unread articles in the current newsgroup.")
1383
1384 (defvar gnus-newsgroup-marked nil
1385   "List of ticked articles in the current newsgroup (a subset of unread art).")
1386
1387 (defvar gnus-newsgroup-killed nil
1388   "List of ranges of articles that have been through the scoring process.")
1389
1390 (defvar gnus-newsgroup-kill-headers nil)
1391
1392 (defvar gnus-newsgroup-replied nil
1393   "List of articles that have been replied to in the current newsgroup.")
1394
1395 (defvar gnus-newsgroup-expirable nil
1396   "List of articles in the current newsgroup that can be expired.")
1397
1398 (defvar gnus-newsgroup-processable nil
1399   "List of articles in the current newsgroup that can be processed.")
1400
1401 (defvar gnus-newsgroup-bookmarks nil
1402   "List of articles in the current newsgroup that have bookmarks.")
1403
1404 (defvar gnus-newsgroup-dormant nil
1405   "List of dormant articles in the current newsgroup.")
1406
1407 (defvar gnus-newsgroup-scored nil
1408   "List of scored articles in the current newsgroup.")
1409
1410 (defvar gnus-newsgroup-headers nil
1411   "List of article headers in the current newsgroup.")
1412 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1413
1414 (defvar gnus-newsgroup-ancient nil
1415   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1416
1417 (defvar gnus-current-article nil)
1418 (defvar gnus-article-current nil)
1419 (defvar gnus-current-headers nil)
1420 (defvar gnus-have-all-headers nil)
1421 (defvar gnus-last-article nil)
1422 (defvar gnus-newsgroup-history nil)
1423 (defvar gnus-current-kill-article nil)
1424
1425 ;; Save window configuration.
1426 (defvar gnus-prev-winconf nil)
1427
1428 ;; Format specs
1429 (defvar gnus-summary-line-format-spec nil)
1430 (defvar gnus-summary-dummy-line-format-spec nil)
1431 (defvar gnus-group-line-format-spec nil)
1432 (defvar gnus-summary-mode-line-format-spec nil)
1433 (defvar gnus-article-mode-line-format-spec nil)
1434 (defvar gnus-group-mode-line-format-spec nil)
1435 (defvar gnus-summary-mark-positions nil)
1436
1437 (defvar gnus-summary-expunge-below nil)
1438 (defvar gnus-reffed-article-number nil)
1439
1440 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1441 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1442
1443 (defconst gnus-summary-local-variables 
1444   '(gnus-newsgroup-name 
1445     gnus-newsgroup-begin gnus-newsgroup-end 
1446     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1447     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1448     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1449     gnus-newsgroup-unselected gnus-newsgroup-marked
1450     gnus-newsgroup-replied gnus-newsgroup-expirable
1451     gnus-newsgroup-processable gnus-newsgroup-killed
1452     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1453     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1454     gnus-current-article gnus-current-headers gnus-have-all-headers
1455     gnus-last-article gnus-article-internal-prepare-hook
1456     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1457     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1458     gnus-newsgroup-threads gnus-newsgroup-async
1459     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1460     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1461     gnus-newsgroup-history gnus-newsgroup-ancient
1462     gnus-newsgroup-adaptive)
1463   "Variables that are buffer-local to the summary buffers.")
1464
1465 (defconst gnus-bug-message
1466   "Sending a bug report to the Gnus Towers.
1467 ========================================
1468
1469 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1470 be sent to the Gnus Bug Exterminators. 
1471
1472 At the bottom of the buffer you'll see lots of variable settings.
1473 Please do not delete those.  They will tell the Bug People what your
1474 environment is, so that it will be easier to locate the bugs.
1475
1476 If you have found a bug that makes Emacs go \"beep\", set
1477 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1478 and include the backtrace in your bug report.
1479
1480 Please describe the bug in annoying, painstaking detail.
1481
1482 Thank you for your help in stamping out bugs.
1483 ")
1484
1485 ;;; End of variables.
1486
1487 ;; Define some autoload functions Gnus might use.
1488 (eval-and-compile
1489
1490   ;; Various 
1491   (autoload 'metamail-buffer "metamail")
1492   (autoload 'Info-goto-node "info")
1493   (autoload 'hexl-hex-string-to-integer "hexl")
1494   (autoload 'pp "pp")
1495   (autoload 'pp-to-string "pp")
1496   (autoload 'pp-eval-expression "pp")
1497   (autoload 'mail-extract-address-components "mail-extr")
1498
1499   (autoload 'nnmail-split-fancy "nnmail")
1500
1501   ;; timezone
1502   (autoload 'timezone-make-date-arpa-standard "timezone")
1503   (autoload 'timezone-fix-time "timezone")
1504   (autoload 'timezone-make-sortable-date "timezone")
1505   (autoload 'timezone-make-time-string "timezone")
1506
1507   ;; rmail & friends
1508   (autoload 'mail-position-on-field "sendmail")
1509   (autoload 'mail-setup "sendmail")
1510   (autoload 'rmail-output "rmailout")
1511   (autoload 'news-mail-other-window "rnewspost")
1512   (autoload 'news-reply-yank-original "rnewspost")
1513   (autoload 'news-caesar-buffer-body "rnewspost")
1514   (autoload 'rmail-insert-rmail-file-header "rmail")
1515   (autoload 'rmail-count-new-messages "rmail")
1516   (autoload 'rmail-show-message "rmail")
1517
1518   ;; gnus-mh
1519   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1520   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1521   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1522   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1523   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1524   (autoload 'gnus-Folder-save-name "gnus-mh")
1525   (autoload 'gnus-folder-save-name "gnus-mh")
1526
1527   ;; gnus-vis misc
1528   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1529   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1530   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1531   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1532   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1533   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1534   (autoload 'gnus-summary-highlight-line "gnus-vis")
1535   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1536
1537   ;; gnus-vis article
1538   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1539   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1540   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1541   (autoload 'gnus-article-hide "gnus-vis" nil t)
1542   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1543   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1544   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1545   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1546   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1547   (autoload 'gnus-article-add-button "gnus-vis")
1548
1549   ;; gnus-cite
1550   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1551   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1552   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1553
1554   ;; gnus-kill
1555   (autoload 'gnus-kill "gnus-kill")
1556   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1557   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1558   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1559   (autoload 'gnus-execute "gnus-kill")
1560   (autoload 'gnus-expunge "gnus-kill")
1561
1562   ;; gnus-cache
1563   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1564   (autoload 'gnus-cache-save-buffers "gnus-cache")
1565   (autoload 'gnus-cache-possibly-remove-article "gnus-cache")
1566   (autoload 'gnus-cache-request-article "gnus-cache")
1567   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1568   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1569   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1570
1571   ;; gnus-score
1572   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1573   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1574   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1575   (autoload 'gnus-score-save "gnus-score")
1576   (autoload 'gnus-score-headers "gnus-score")
1577   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1578   (autoload 'gnus-score-adaptive "gnus-score")
1579   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1580   (autoload 'gnus-possibly-score-headers "gnus-score")
1581   (autoload 'gnus-score-find-trace "gnus-score")
1582
1583   ;; gnus-uu
1584   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1585   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1586   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1587   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1588   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1589   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1590   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1591   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1592   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1593   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1594   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1595   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1596   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1597   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1598   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1599   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1600   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1601   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1602   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1603   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1604   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1605   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1606
1607   ;; gnus-msg
1608   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1609   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1610   (autoload 'gnus-group-mail "gnus-msg" nil t)
1611   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1612   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1613   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1614   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1615   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1616   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1617   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1618   (autoload 'gnus-post-news "gnus-msg" nil t)
1619   (autoload 'gnus-inews-news "gnus-msg" nil t)
1620   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1621   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1622   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1623   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1624   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1625   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1626   (autoload 'gnus-mail-yank-original "gnus-msg")
1627   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1628   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1629   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1630   (autoload 'gnus-article-mail-with-original "gnus-msg")
1631   (autoload 'gnus-article-mail "gnus-msg")
1632
1633   ;; gnus-vm
1634   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1635   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1636   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1637   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1638   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1639   (autoload 'gnus-yank-article "gnus-vm" nil t)
1640
1641   )
1642
1643 \f
1644
1645 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1646 ;; If you want the cursor to go somewhere else, set these two
1647 ;; functions in some startup hook to whatever you want.
1648 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1649 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1650
1651 ;;; Various macros and substs.
1652
1653 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1654   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1655   (` (let ((GnusStartBufferWindow (selected-window)))
1656        (unwind-protect
1657            (progn
1658              (pop-to-buffer (, buffer))
1659              (,@ forms))
1660          (select-window GnusStartBufferWindow)))))
1661
1662 (defmacro gnus-gethash (string hashtable)
1663   "Get hash value of STRING in HASHTABLE."
1664   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1665   ;;(` (abbrev-expansion (, string) (, hashtable)))
1666   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1667
1668 (defmacro gnus-sethash (string value hashtable)
1669   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1670   ;; We cannot use define-abbrev since it only accepts string as value.
1671   ;; (set (intern string hashtable) value))
1672   (` (set (intern (, string) (, hashtable)) (, value))))
1673
1674 (defsubst gnus-buffer-substring (beg end)
1675   (buffer-substring (match-beginning beg) (match-end end)))
1676
1677 (defsubst gnus-simplify-subject-re (subject)
1678   "Remove \"Re:\" from subject lines."
1679   (let ((case-fold-search t))
1680     (if (string-match "^re: *" subject)
1681         (substring subject (match-end 0))
1682       subject)))
1683
1684 (defsubst gnus-goto-char (point)
1685   (and point (goto-char point)))
1686
1687 (defmacro gnus-buffer-exists-p (buffer)
1688   (` (and (, buffer)
1689           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1690                    (, buffer)))))
1691
1692 (defmacro gnus-kill-buffer (buffer)
1693   (` (if (gnus-buffer-exists-p (, buffer))
1694          (kill-buffer (, buffer)))))
1695
1696 (defsubst gnus-point-at-bol ()
1697   "Return point at the beginning of line."
1698   (let ((p (point)))
1699     (beginning-of-line)
1700     (prog1
1701         (point)
1702       (goto-char p))))
1703
1704 (defsubst gnus-point-at-eol ()
1705   "Return point at the beginning of line."
1706   (let ((p (point)))
1707     (end-of-line)
1708     (prog1
1709         (point)
1710       (goto-char p))))
1711
1712 ;; Delete the current line (and the next N lines.);
1713 (defmacro gnus-delete-line (&optional n)
1714   (` (delete-region (progn (beginning-of-line) (point))
1715                     (progn (forward-line (, (or n 1))) (point)))))
1716
1717 ;;; Load the compatability functions. 
1718
1719 (require 'gnus-ems)
1720
1721 \f
1722 ;;;
1723 ;;; Gnus Utility Functions
1724 ;;;
1725
1726 (defun gnus-extract-address-components (from)
1727   (let (name address)
1728     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1729         (setq address (substring from (match-beginning 0) (match-end 0))))
1730     (and address
1731          (string-match (concat "<" (regexp-quote address) ">") from)
1732          (setq name (substring from 0 (1- (match-beginning 0)))))
1733     (or name
1734         (and (string-match "(.+)" from)
1735              (setq name (substring from (1+ (match-beginning 0)) 
1736                                    (1- (match-end 0))))))
1737     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1738     (list (or name from) (or address from))))
1739
1740 (defun gnus-fetch-field (field)
1741   "Return the value of the header FIELD of current article."
1742   (save-excursion
1743     (save-restriction
1744       (let ((case-fold-search t))
1745         (gnus-narrow-to-headers)
1746         (mail-fetch-field field)))))
1747
1748 (defun gnus-goto-colon ()
1749   (beginning-of-line)
1750   (search-forward ":" (gnus-point-at-eol) t))
1751
1752 (defun gnus-narrow-to-headers ()
1753   (widen)
1754   (save-excursion
1755     (narrow-to-region
1756      (goto-char (point-min))
1757      (if (search-forward "\n\n" nil t)
1758          (1- (point))
1759        (point-max)))))
1760
1761 (defun gnus-update-format-specifications ()
1762   (gnus-make-thread-indent-array)
1763   (setq gnus-summary-line-format-spec 
1764         (gnus-parse-format
1765          gnus-summary-line-format gnus-summary-line-format-alist))
1766   (gnus-update-summary-mark-positions)
1767   (setq gnus-summary-dummy-line-format-spec 
1768         (gnus-parse-format gnus-summary-dummy-line-format 
1769                            gnus-summary-dummy-line-format-alist))
1770   (setq gnus-group-line-format-spec
1771         (gnus-parse-format 
1772          gnus-group-line-format 
1773          gnus-group-line-format-alist))
1774   (if (and (string-match "%D" gnus-group-line-format)
1775            (not gnus-description-hashtb)
1776            gnus-read-active-file)
1777       (gnus-read-descriptions-file))
1778   (setq gnus-summary-mode-line-format-spec 
1779         (gnus-parse-format gnus-summary-mode-line-format 
1780                            gnus-summary-mode-line-format-alist))
1781   (setq gnus-article-mode-line-format-spec 
1782         (gnus-parse-format gnus-article-mode-line-format 
1783                            gnus-summary-mode-line-format-alist))
1784   (setq gnus-group-mode-line-format-spec 
1785         (gnus-parse-format gnus-group-mode-line-format 
1786                            gnus-group-mode-line-format-alist)))
1787
1788 (defun gnus-update-summary-mark-positions ()
1789   (save-excursion
1790     (let ((gnus-replied-mark 129)
1791           (gnus-score-below-mark 130)
1792           (gnus-score-over-mark 130)
1793           (thread nil)
1794           pos)
1795       (gnus-set-work-buffer)
1796       (gnus-summary-insert-line 
1797        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1798       (goto-char (point-min))
1799       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1800                                          (- (point) 2)))))
1801       (goto-char (point-min))
1802       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1803                                           (- (point) 2))) pos))
1804       (goto-char (point-min))
1805       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1806                                         (- (point) 2))) pos))
1807       (setq gnus-summary-mark-positions pos))))
1808
1809 (defun gnus-format-max-width (form length)
1810   (let* ((val (eval form))
1811          (valstr (if (numberp val) (int-to-string val) val)))
1812     (if (> (length valstr) length)
1813         (substring valstr 0 length)
1814       valstr)))
1815
1816 (defun gnus-set-mouse-face (string)
1817   ;; Set mouse face property on STRING.
1818   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1819   string)
1820
1821 (defun gnus-parse-format (format spec-alist)
1822   ;; This function parses the FORMAT string with the help of the
1823   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1824   ;; string.  If the FORMAT string contains the specifiers %( and %)
1825   ;; the text between them will have the mouse-face text property.
1826   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1827       (if (and gnus-visual gnus-mouse-face)
1828           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1829                 (button (substring format (match-beginning 2) (match-end 2)))
1830                 (post (substring format (match-beginning 3) (match-end 3))))
1831             (list 'concat
1832                   (gnus-parse-simple-format pre spec-alist)
1833                   (list 'gnus-set-mouse-face
1834                         (gnus-parse-simple-format button spec-alist))
1835                   (gnus-parse-simple-format post spec-alist)))
1836         (gnus-parse-simple-format
1837          (concat (substring format (match-beginning 1) (match-end 1))
1838                  (substring format (match-beginning 2) (match-end 2))
1839                  (substring format (match-beginning 3) (match-end 3)))
1840          spec-alist))
1841     (gnus-parse-simple-format format spec-alist)))
1842
1843 (defun gnus-parse-simple-format (format spec-alist)
1844   ;; This function parses the FORMAT string with the help of the
1845   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1846   ;; string. The list will consist of the symbol `format', a format
1847   ;; specification string, and a list of forms depending on the
1848   ;; SPEC-ALIST.
1849   (let ((max-width 0)
1850         spec flist fstring b newspec max-width elem beg)
1851     (save-excursion
1852       (gnus-set-work-buffer)
1853       (insert format)
1854       (goto-char (point-min))
1855       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1856         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1857                                                      (match-end 2))))
1858         ;; First check if there are any specs that look anything like
1859         ;; "%12,12A", ie. with a "max width specification". These have
1860         ;; to be treated specially.
1861         (if (setq beg (match-beginning 1))
1862             (setq max-width 
1863                   (string-to-int 
1864                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1865           (setq max-width 0)
1866           (setq beg (match-beginning 2)))
1867         ;; Find the specification from `spec-alist'.
1868         (if (not (setq elem (cdr (assq spec spec-alist))))
1869             (setq elem '("*" ?s)))
1870         ;; Treat user defined format specifiers specially
1871         (and (eq (car elem) 'user-defined)
1872              (setq elem
1873                    (list 
1874                     (list (intern (concat "gnus-user-format-function-"
1875                                           (buffer-substring
1876                                            (match-beginning 3)
1877                                            (match-end 3))))
1878                           'header)
1879                     ?s))
1880              (delete-region (match-beginning 3) (match-end 3)))
1881         (if (not (zerop max-width))
1882             (let ((el (car elem)))
1883               (cond ((= (car (cdr elem)) ?c) 
1884                      (setq el (list 'char-to-string el)))
1885                     ((= (car (cdr elem)) ?d)
1886                      (numberp el) (setq el (list 'int-to-string el))))
1887               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1888                                 flist))
1889               (setq newspec ?s))
1890           (setq flist (cons (car elem) flist))
1891           (setq newspec (car (cdr elem))))
1892         ;; Remove the old specification (and possibly a ",12" string).
1893         (delete-region beg (match-end 2))
1894         ;; Insert the new specification.
1895         (goto-char beg)
1896         (insert newspec))
1897       (setq fstring (buffer-substring 1 (point-max))))
1898     (cons 'format (cons fstring (nreverse flist)))))
1899
1900 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1901 (defun gnus-read-init-file ()
1902   (and gnus-init-file
1903        (or (and (file-exists-p gnus-init-file) 
1904                 ;; Don't try to load a directory.
1905                 (not (file-directory-p gnus-init-file)))
1906            (file-exists-p (concat gnus-init-file ".el"))
1907            (file-exists-p (concat gnus-init-file ".elc")))
1908        (load gnus-init-file nil t)))
1909
1910 (defun gnus-set-work-buffer ()
1911   (if (get-buffer gnus-work-buffer)
1912       (progn
1913         (set-buffer gnus-work-buffer)
1914         (erase-buffer))
1915     (set-buffer (get-buffer-create gnus-work-buffer))
1916     (buffer-disable-undo (current-buffer))
1917     (gnus-add-current-to-buffer-list)))
1918
1919 ;; Article file names when saving.
1920
1921 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1922   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1923 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1924 Otherwise, it is like ~/News/news/group/num."
1925   (let ((default
1926           (expand-file-name
1927            (concat (if (gnus-use-long-file-name 'not-save)
1928                        (gnus-capitalize-newsgroup newsgroup)
1929                      (gnus-newsgroup-directory-form newsgroup))
1930                    "/" (int-to-string (header-number headers)))
1931            (or gnus-article-save-directory "~/News"))))
1932     (if (and last-file
1933              (string-equal (file-name-directory default)
1934                            (file-name-directory last-file))
1935              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1936         default
1937       (or last-file default))))
1938
1939 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1940   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1941 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1942 Otherwise, it is like ~/News/news/group/num."
1943   (let ((default
1944           (expand-file-name
1945            (concat (if (gnus-use-long-file-name 'not-save)
1946                        newsgroup
1947                      (gnus-newsgroup-directory-form newsgroup))
1948                    "/" (int-to-string (header-number headers)))
1949            (or gnus-article-save-directory "~/News"))))
1950     (if (and last-file
1951              (string-equal (file-name-directory default)
1952                            (file-name-directory last-file))
1953              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1954         default
1955       (or last-file default))))
1956
1957 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1958   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1959 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1960 Otherwise, it is like ~/News/news/group/news."
1961   (or last-file
1962       (expand-file-name
1963        (if (gnus-use-long-file-name 'not-save)
1964            (gnus-capitalize-newsgroup newsgroup)
1965          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1966        (or gnus-article-save-directory "~/News"))))
1967
1968 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1969   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1970 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1971 Otherwise, it is like ~/News/news/group/news."
1972   (or last-file
1973       (expand-file-name
1974        (if (gnus-use-long-file-name 'not-save)
1975            newsgroup
1976          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1977        (or gnus-article-save-directory "~/News"))))
1978
1979 ;; For subscribing new newsgroup
1980
1981 (defun gnus-subscribe-hierarchical-interactive (groups)
1982   (let ((groups (sort groups 'string<))
1983         prefixes prefix start rest ans group starts)
1984     (while groups
1985       (setq prefixes (list "^"))
1986       (while (and groups prefixes)
1987         (while (not (string-match (car prefixes) (car groups)))
1988           (setq prefixes (cdr prefixes)))
1989         (setq prefix (car prefixes))
1990         (setq start (1- (length prefix)))
1991         (if (and (string-match "[^\\.]\\." (car groups) start)
1992                  (cdr groups)
1993                  (setq prefix 
1994                        (concat "^" (substring (car groups) 0 (match-end 0))))
1995                  (string-match prefix (car (cdr groups))))
1996             (progn
1997               (setq prefixes (cons prefix prefixes))
1998               (message "Descend hierarchy %s? ([y]nsq): " 
1999                        (substring prefix 1 (1- (length prefix))))
2000               (setq ans (read-char))
2001               (cond ((= ans ?n)
2002                      (while (and groups 
2003                                  (string-match prefix 
2004                                                (setq group (car groups))))
2005                        (setq gnus-killed-list 
2006                              (cons group gnus-killed-list))
2007                        (gnus-sethash group group gnus-killed-hashtb)
2008                        (setq groups (cdr groups)))
2009                      (setq starts (cdr starts)))
2010                     ((= ans ?s)
2011                      (while (and groups 
2012                                  (string-match prefix 
2013                                                (setq group (car groups))))
2014                        (gnus-sethash group group gnus-killed-hashtb)
2015                        (gnus-subscribe-alphabetically (car groups))
2016                        (setq groups (cdr groups)))
2017                      (setq starts (cdr starts)))
2018                     ((= ans ?q)
2019                      (while groups
2020                        (setq group (car groups))
2021                        (setq gnus-killed-list (cons group gnus-killed-list))
2022                        (gnus-sethash group group gnus-killed-hashtb)
2023                        (setq groups (cdr groups))))
2024                     (t nil)))
2025           (message "Subscribe %s? ([n]yq)" (car groups))
2026           (setq ans (read-char))
2027           (setq group (car groups))
2028           (cond ((= ans ?y)
2029                  (gnus-subscribe-alphabetically (car groups))
2030                  (gnus-sethash group group gnus-killed-hashtb))
2031                 ((= ans ?q)
2032                  (while groups
2033                    (setq group (car groups))
2034                    (setq gnus-killed-list (cons group gnus-killed-list))
2035                    (gnus-sethash group group gnus-killed-hashtb)
2036                    (setq groups (cdr groups))))
2037                 (t 
2038                  (setq gnus-killed-list (cons group gnus-killed-list))
2039                  (gnus-sethash group group gnus-killed-hashtb)))
2040           (setq groups (cdr groups)))))))
2041
2042 (defun gnus-subscribe-randomly (newsgroup)
2043   "Subscribe new NEWSGROUP by making it the first newsgroup."
2044   (gnus-subscribe-newsgroup newsgroup))
2045
2046 (defun gnus-subscribe-alphabetically (newgroup)
2047   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2048   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2049   (let ((groups (cdr gnus-newsrc-alist))
2050         before)
2051     (while (and (not before) groups)
2052       (if (string< newgroup (car (car groups)))
2053           (setq before (car (car groups)))
2054         (setq groups (cdr groups))))
2055     (gnus-subscribe-newsgroup newgroup before)))
2056
2057 (defun gnus-subscribe-hierarchically (newgroup)
2058   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2059   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2060   (save-excursion
2061     (set-buffer (find-file-noselect gnus-current-startup-file))
2062     (let ((groupkey newgroup)
2063           before)
2064       (while (and (not before) groupkey)
2065         (goto-char (point-min))
2066         (let ((groupkey-re
2067                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2068           (while (and (re-search-forward groupkey-re nil t)
2069                       (progn
2070                         (setq before (buffer-substring
2071                                       (match-beginning 1) (match-end 1)))
2072                         (string< before newgroup)))))
2073         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2074         (setq groupkey
2075               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2076                   (substring groupkey (match-beginning 1) (match-end 1)))))
2077       (gnus-subscribe-newsgroup newgroup before))))
2078
2079 (defun gnus-subscribe-interactively (newsgroup)
2080   "Subscribe new NEWSGROUP interactively.
2081 It is inserted in hierarchical newsgroup order if subscribed. If not,
2082 it is killed."
2083   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2084       (gnus-subscribe-hierarchically newsgroup)
2085     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2086
2087 (defun gnus-subscribe-zombies (newsgroup)
2088   "Make new NEWSGROUP a zombie group."
2089   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2090
2091 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2092   "Subscribe new NEWSGROUP.
2093 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2094 the first newsgroup."
2095   ;; We subscribe the group by changing its level to `subscribed'.
2096   (gnus-group-change-level 
2097    newsgroup gnus-level-default-subscribed
2098    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2099   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2100
2101 ;; For directories
2102
2103 (defun gnus-newsgroup-directory-form (newsgroup)
2104   "Make hierarchical directory name from NEWSGROUP name."
2105   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2106         (len (length newsgroup))
2107         idx)
2108     ;; If this is a foreign group, we don't want to translate the
2109     ;; entire name.  
2110     (if (setq idx (string-match ":" newsgroup))
2111         (aset newsgroup idx ?/)
2112       (setq idx 0))
2113     ;; Replace all occurrences of `.' with `/'.
2114     (while (< idx len)
2115       (if (= (aref newsgroup idx) ?.)
2116           (aset newsgroup idx ?/))
2117       (setq idx (1+ idx)))
2118     newsgroup))
2119
2120 (defun gnus-make-directory (dir)
2121   "Make DIRECTORY recursively."
2122   (let* ((dir (expand-file-name dir default-directory))
2123          dirs)
2124     (if (string-match "/$" dir)
2125         (setq dir (substring dir 0 (match-beginning 0))))
2126     (while (not (file-exists-p dir))
2127       (setq dirs (cons dir dirs))
2128       (string-match "/[^/]+$" dir)
2129       (setq dir (substring dir 0 (match-beginning 0))))
2130     (while dirs
2131       (make-directory (car dirs))
2132       (setq dirs (cdr dirs)))))
2133
2134 (defun gnus-capitalize-newsgroup (newsgroup)
2135   "Capitalize NEWSGROUP name."
2136   (and (not (zerop (length newsgroup)))
2137        (concat (char-to-string (upcase (aref newsgroup 0)))
2138                (substring newsgroup 1))))
2139
2140 ;; Var
2141
2142 (defun gnus-simplify-subject (subject &optional re-only)
2143   "Remove `Re:' and words in parentheses.
2144 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2145   (let ((case-fold-search t))           ;Ignore case.
2146     ;; Remove `Re:' and `Re^N:'.
2147     (if (string-match "^re:[ \t]*" subject)
2148         (setq subject (substring subject (match-end 0))))
2149     ;; Remove words in parentheses from end.
2150     (or re-only
2151         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2152           (setq subject (substring subject 0 (match-beginning 0)))))
2153     ;; Return subject string.
2154     subject))
2155
2156 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2157 ;; all whitespace.
2158 (defun gnus-simplify-subject-fuzzy (subject)
2159   (let ((case-fold-search t))
2160     (save-excursion
2161       (gnus-set-work-buffer)
2162       (insert subject)
2163       (gnus-simplify-buffer-fuzzy)
2164       (buffer-string))))
2165
2166 (defun gnus-simplify-buffer-fuzzy ()
2167   (goto-char (point-min))
2168   (while (re-search-forward "^[ \t]*re:[ \t]*" nil t)
2169     (replace-match "" t t))
2170   (goto-char (point-min))
2171   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2172     (replace-match "" t t))
2173   (goto-char (point-min))
2174   (while (re-search-forward "[ \t]+" nil t)
2175     (replace-match " " t t))
2176   (goto-char (point-min))
2177   (while (re-search-forward "[ \t]+$" nil t)
2178     (replace-match "" t t))
2179   (goto-char (point-min))
2180   (while (re-search-forward "^[ \t]+" nil t)
2181     (replace-match "" t t)))
2182
2183 ;; Add the current buffer to the list of buffers to be killed on exit. 
2184 (defun gnus-add-current-to-buffer-list ()
2185   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
2186
2187 (defun gnus-string> (s1 s2)
2188   (not (or (string< s1 s2)
2189            (string= s1 s2))))
2190
2191 ;; Functions accessing headers.
2192 ;; Functions are more convenient than macros in some cases.
2193
2194 (defun gnus-header-number (header)
2195   (header-number header))
2196
2197 (defun gnus-header-subject (header)
2198   (header-subject header))
2199
2200 (defun gnus-header-from (header)
2201   (header-from header))
2202
2203 (defun gnus-header-xref (header)
2204   (header-xref header))
2205
2206 (defun gnus-header-lines (header)
2207   (header-lines header))
2208
2209 (defun gnus-header-date (header)
2210   (header-date header))
2211
2212 (defun gnus-header-id (header)
2213   (header-id header))
2214
2215 (defun gnus-header-references (header)
2216   (header-references header))
2217
2218 ;;; General various misc type functions.
2219
2220 (defun gnus-clear-system ()
2221   "Clear all variables and buffers."
2222   ;; Clear Gnus variables.
2223   (let ((variables gnus-variable-list))
2224     (while variables
2225       (set (car variables) nil)
2226       (setq variables (cdr variables))))
2227   ;; Clear other internal variables.
2228   (setq gnus-list-of-killed-groups nil
2229         gnus-have-read-active-file nil
2230         gnus-newsrc-alist nil
2231         gnus-newsrc-hashtb nil
2232         gnus-killed-list nil
2233         gnus-zombie-list nil
2234         gnus-killed-hashtb nil
2235         gnus-active-hashtb nil
2236         gnus-moderated-list nil
2237         gnus-description-hashtb nil
2238         gnus-newsgroup-headers nil
2239         gnus-newsgroup-headers-hashtb-by-number nil
2240         gnus-newsgroup-name nil
2241         gnus-server-alist nil
2242         gnus-current-select-method nil)
2243   ;; Reset any score variables.
2244   (and (boundp 'gnus-score-cache)
2245        (set 'gnus-score-cache nil))
2246   (and (boundp 'gnus-internal-global-score-files)
2247        (set 'gnus-internal-global-score-files nil))
2248   ;; Kill the startup file.
2249   (and gnus-current-startup-file
2250        (get-file-buffer gnus-current-startup-file)
2251        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2252   ;; Save any cache buffers.
2253   (and gnus-use-cache (gnus-cache-save-buffers))
2254   ;; Clear the dribble buffer.
2255   (gnus-dribble-clear)
2256   ;; Kill global KILL file buffer.
2257   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2258       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2259   (gnus-kill-buffer nntp-server-buffer)
2260   ;; Kill Gnus buffers.
2261   (while gnus-buffer-list
2262     (gnus-kill-buffer (car gnus-buffer-list))
2263     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2264
2265 (defun gnus-windows-old-to-new (setting)
2266   (if (symbolp setting)
2267       (setq setting 
2268             (cond ((eq setting 'SelectArticle)
2269                    'article)
2270                   ((eq setting 'SelectSubject)
2271                    'summary)
2272                   ((eq setting 'SelectNewsgroup)
2273                    'group)
2274                   (t setting))))
2275   (if (or (listp setting)
2276           (not (and gnus-window-configuration
2277                     (memq setting '(group summary article)))))
2278       setting
2279     (let* ((setting (if (eq setting 'group) 
2280                         (if (assq 'newsgroup gnus-window-configuration)
2281                             'newsgroup
2282                           'newsgroups) setting))
2283            (elem (car (cdr (assq setting gnus-window-configuration))))
2284            (total (apply '+ elem))
2285            (types '(group summary article))
2286            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2287            (i 0)
2288            perc
2289            out)
2290       (while (< i 3)
2291         (or (zerop (nth i elem))
2292             (progn
2293               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2294               (setq out (cons (if (eq pbuf (nth i types))
2295                                   (vector (nth i types) perc 'point)
2296                                 (vector (nth i types) perc))
2297                               out))))
2298         (setq i (1+ i)))
2299       (list (nreverse out)))))
2300            
2301 (defun gnus-configure-windows (setting)
2302   (setq setting (gnus-windows-old-to-new setting))
2303   (let ((r (if (symbolp setting)
2304                   (cdr (assq setting gnus-buffer-configuration))
2305                 setting))
2306         (in-buf (current-buffer))
2307         rule val window w height hor ohor heights sub jump-buffer
2308         rel total to-buf)
2309     (or r (error "No such setting: %s" setting))
2310
2311     ;; Either remove all windows or just remove all Gnus windows.
2312     (if gnus-use-full-window
2313         (delete-other-windows)
2314       (gnus-remove-some-windows)
2315       (switch-to-buffer nntp-server-buffer))
2316
2317     (while r
2318       (setq hor (car r)
2319             ohor nil)
2320
2321       ;; We have to do the (possible) horizontal splitting before the
2322       ;; vertical. 
2323       (if (and (listp (car hor)) 
2324                (eq (car (car hor)) 'horizontal))
2325           (progn
2326             (split-window nil (- (frame-width) 
2327                                  (floor (* (frame-width) (nth 1 (car hor)))))
2328                           t)
2329             (setq hor (cdr hor))))
2330
2331       ;; Go through the rules and eval the elements that are to be
2332       ;; evaled.  
2333       (while hor
2334         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2335             (progn
2336               ;; Expand short buffer name.
2337               (setq w (aref val 0))
2338               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2339                    (progn
2340                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2341                      (aset val 0 w)))
2342               (setq ohor (cons val ohor))))
2343         (setq hor (cdr hor)))
2344       (setq rule (cons (nreverse ohor) rule))
2345       (setq r (cdr r)))
2346     (setq rule (nreverse rule))
2347
2348     ;; We tally the window sizes.
2349     (setq total (window-height))
2350     (while rule
2351       (setq hor (car rule))
2352       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2353           (setq hor (cdr hor)))
2354       (setq sub 0)
2355       (while hor
2356         (setq rel (aref (car hor) 1)
2357               heights (cons
2358                        (cond ((and (floatp rel) (= 1.0 rel))
2359                               'x)
2360                              ((integerp rel)
2361                               rel)
2362                              (t
2363                               (max (floor (* total rel)) 4)))
2364                        heights)
2365               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2366               hor (cdr hor)))
2367       (setq heights (nreverse heights)
2368             hor (car rule))
2369       
2370       ;; We then go through these heighs and create windows for them.
2371       (while heights
2372         (setq height (car heights)
2373               heights (cdr heights))
2374         (and (eq height 'x)
2375              (setq height (- total sub)))
2376         (and heights
2377              (split-window nil height))
2378         (setq to-buf (aref (car hor) 0))
2379         (switch-to-buffer 
2380          (cond ((not to-buf)
2381                 in-buf)
2382                ((symbolp to-buf)
2383                 (symbol-value (aref (car hor) 0)))
2384                (t
2385                 (aref (car hor) 0))))
2386         (and (> (length (car hor)) 2)
2387              (eq (aref (car hor) 2) 'point)
2388              (setq jump-buffer (current-buffer)))
2389         (other-window 1)
2390         (setq hor (cdr hor)))
2391       
2392       (setq rule (cdr rule)))
2393
2394     ;; Finally, we pop to the buffer that's supposed to have point. 
2395     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2396
2397     (pop-to-buffer jump-buffer)
2398     jump-buffer))
2399       
2400 (defun gnus-remove-some-windows ()
2401   (let ((buffers gnus-window-to-buffer)
2402         (first t)
2403         buf)
2404     (while buffers
2405       (setq buf (cdr (car buffers)))
2406       (if (symbolp buf)
2407           (setq buf (and (boundp buf) (symbol-value buf))))
2408       (and buf 
2409            (get-buffer-window buf)
2410            (progn
2411              (set-buffer buf)
2412              (if first
2413                  (progn
2414                    (switch-to-buffer nntp-server-buffer)
2415                    (setq first nil))
2416                (delete-window (get-buffer-window buf)))))
2417       (setq buffers (cdr buffers)))
2418     (set-buffer nntp-server-buffer)))
2419
2420 (defun gnus-version ()
2421   "Version numbers of this version of Gnus."
2422   (interactive)
2423   (let ((methods gnus-valid-select-methods)
2424         (mess gnus-version)
2425         meth)
2426     ;; Go through all the legal select methods and add their version
2427     ;; numbers to the total version string. Only the backends that are
2428     ;; currently in use will have their message numbers taken into
2429     ;; consideration. 
2430     (while methods
2431       (setq meth (intern (concat (car (car methods)) "-version")))
2432       (and (boundp meth)
2433            (stringp (symbol-value meth))
2434            (setq mess (concat mess "; " (symbol-value meth))))
2435       (setq methods (cdr methods)))
2436     (gnus-message 2 mess)))
2437
2438 (defun gnus-info-find-node ()
2439   "Find Info documentation of Gnus."
2440   (interactive)
2441   ;; Enlarge info window if needed.
2442   (let ((mode major-mode))
2443     (gnus-configure-windows 'info)
2444     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2445
2446 (defun gnus-bug ()
2447   "Send a bug report to the Gnus maintainers."
2448   (interactive)
2449   (let ((winconf (current-window-configuration)))
2450     (delete-other-windows)
2451     (switch-to-buffer "*Gnus Bug Help*")
2452     (erase-buffer)
2453     (insert gnus-bug-message)
2454     (goto-char (point-min))
2455     (pop-to-buffer "*Gnus Bug*")
2456     (erase-buffer)
2457     (mail-mode)
2458     (mail-setup gnus-maintainer nil nil nil nil nil)
2459     (make-local-variable 'gnus-prev-winconf)
2460     (setq gnus-prev-winconf winconf)
2461     (goto-char (point-min))
2462     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2463     (forward-line 1)
2464     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
2465     (let ((b (point)))
2466       (gnus-debug)
2467       (goto-char (- b 3)))
2468     (message "")))
2469
2470 (defun gnus-debug ()
2471   "Attemps to go through the Gnus source file and report what variables have been changed.
2472 The source file has to be in the Emacs load path."
2473   (interactive)
2474   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el"))
2475         file dirs expr olist)
2476     (save-excursion
2477       (set-buffer (get-buffer-create " *gnus bug info*"))
2478       (buffer-disable-undo (current-buffer))
2479       (message "Please wait while we snoop your variables...")
2480       (sit-for 0)
2481       (while files
2482         (erase-buffer)
2483         (setq dirs load-path)
2484         (while dirs
2485           (if (or (not (car dirs))
2486                   (not (stringp (car dirs)))
2487                   (not (file-exists-p 
2488                         (setq file (concat (file-name-as-directory 
2489                                             (car dirs)) (car files))))))
2490               (setq dirs (cdr dirs))
2491             (setq dirs nil)
2492             (insert-file-contents file)
2493             (goto-char (point-min))
2494             (or (re-search-forward "^;;* Internal variables" nil t)
2495                 (error "Malformed sources in file %s" file))
2496             (narrow-to-region (point-min) (point))
2497             (goto-char (point-min))
2498             (while (setq expr (condition-case () 
2499                                   (read (current-buffer)) (error nil)))
2500               (and (eq (car expr) 'defvar)
2501                    (stringp (nth 3 expr))
2502                    (or (not (boundp (nth 1 expr)))
2503                        (not (equal (eval (nth 2 expr))
2504                                    (symbol-value (nth 1 expr)))))
2505                    (setq olist (cons (nth 1 expr) olist))))))
2506         (setq files (cdr files)))
2507       (kill-buffer (current-buffer)))
2508     (insert "------------------- Environment follows -------------------\n\n")
2509     (while olist
2510       (if (boundp (car olist))
2511           (insert "(setq " (symbol-name (car olist)) " '" 
2512                   (prin1-to-string (symbol-value (car olist))) ")\n")
2513         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2514       (setq olist (cdr olist)))
2515     (insert "\n\n")))
2516
2517 (defun gnus-overload-functions (&optional overloads)
2518   "Overload functions specified by optional argument OVERLOADS.
2519 If nothing is specified, use the variable gnus-overload-functions."
2520   (let ((defs nil)
2521         (overloads (or overloads gnus-overload-functions)))
2522     (while overloads
2523       (setq defs (car overloads))
2524       (setq overloads (cdr overloads))
2525       ;; Load file before overloading function if necessary.  Make
2526       ;; sure we cannot use `require' always.
2527       (and (not (fboundp (car defs)))
2528            (car (cdr (cdr defs)))
2529            (load (car (cdr (cdr defs))) nil 'nomessage))
2530       (fset (car defs) (car (cdr defs))))))
2531
2532 (defun gnus-replace-chars-in-string (string from to)
2533   "Replace characters in STRING from FROM to TO."
2534   (let ((string (substring string 0))   ;Copy string.
2535         (len (length string))
2536         (idx 0))
2537     ;; Replace all occurrences of FROM with TO.
2538     (while (< idx len)
2539       (if (= (aref string idx) from)
2540           (aset string idx to))
2541       (setq idx (1+ idx)))
2542     string))
2543
2544 (defun gnus-days-between (date1 date2)
2545   ;; Return the number of days between date1 and date2.
2546   (- (gnus-day-number date1) (gnus-day-number date2)))
2547
2548 (defun gnus-day-number (date)
2549   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2550                      (timezone-parse-date date))))
2551     (timezone-absolute-from-gregorian 
2552      (nth 1 dat) (nth 2 dat) (car dat))))
2553
2554 ;; Returns a floating point number that says how many seconds have
2555 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2556 (defun gnus-seconds-since-epoch (date)
2557   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2558                         (timezone-parse-date date)))
2559          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2560                         (timezone-parse-time
2561                          (aref (timezone-parse-date date) 3))))
2562          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2563                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2564          (tday (- (timezone-absolute-from-gregorian 
2565                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2566                   (timezone-absolute-from-gregorian 
2567                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2568     (+ (nth 2 ttime)
2569        (* (nth 1 ttime) 60)
2570        (* 1.0 (nth 0 ttime) 60 60)
2571        (* 1.0 tday 60 60 24))))
2572
2573 (defun gnus-file-newer-than (file date)
2574   (let ((fdate (nth 5 (file-attributes file))))
2575     (or (> (car fdate) (car date))
2576         (and (= (car fdate) (car date))
2577              (> (nth 1 fdate) (nth 1 date))))))
2578
2579 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2580 ;; the echo area.
2581 (defun gnus-y-or-n-p (prompt)
2582   (prog1
2583       (y-or-n-p prompt)
2584     (message "")))
2585
2586 (defun gnus-yes-or-no-p (prompt)
2587   (prog1
2588       (yes-or-no-p prompt)
2589     (message "")))
2590
2591 ;; Check whether to use long file names.
2592 (defun gnus-use-long-file-name (symbol)
2593   ;; The variable has to be set...
2594   (and gnus-use-long-file-name
2595        ;; If it isn't a list, then we return t.
2596        (or (not (listp gnus-use-long-file-name))
2597            ;; If it is a list, and the list contains `symbol', we
2598            ;; return nil.  
2599            (not (memq symbol gnus-use-long-file-name)))))
2600
2601 ;; I suspect there's a better way, but I haven't taken the time to do
2602 ;; it yet. -erik selberg@cs.washington.edu
2603 (defun gnus-dd-mmm (messy-date)
2604   "Return a string like DD-MMM from a big messy string"
2605   (let ((datevec (timezone-parse-date messy-date)))
2606     (format "%2s-%s"
2607             (or (aref datevec 2) "??")
2608             (capitalize
2609              (or (car 
2610                   (nth (1- (string-to-number (aref datevec 1)))
2611                        timezone-months-assoc))
2612                  "???")))))
2613
2614 ;; Make a hash table (default and minimum size is 255).
2615 ;; Optional argument HASHSIZE specifies the table size.
2616 (defun gnus-make-hashtable (&optional hashsize)
2617   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2618
2619 ;; Make a number that is suitable for hashing; bigger than MIN and one
2620 ;; less than 2^x.
2621 (defun gnus-create-hash-size (min)
2622   (let ((i 1))
2623     (while (< i min)
2624       (setq i (* 2 i)))
2625     (1- i)))
2626
2627 ;; Show message if message has a lower level than `gnus-verbose'. 
2628 ;; Guide-line for numbers:
2629 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2630 ;; for things that take a long time, 7 - not very important messages
2631 ;; on stuff, 9 - messages inside loops.
2632 (defun gnus-message (level &rest args)
2633   (if (<= level gnus-verbose)
2634       (apply 'message args)
2635     ;; We have to do this format thingie here even if the result isn't
2636     ;; shown - the return value has to be the same as the return value
2637     ;; from `message'.
2638     (apply 'format args)))
2639
2640 ;; Generate a unique new group name.
2641 (defun gnus-generate-new-group-name (leaf)
2642   (let ((name leaf)
2643         (num 0))
2644     (while (gnus-gethash name gnus-newsrc-hashtb)
2645       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2646     name))
2647
2648 ;;; List and range functions
2649
2650 (defun gnus-last-element (list)
2651   "Return last element of LIST."
2652   (while (cdr list)
2653     (setq list (cdr list)))
2654   (car list))
2655
2656 (defun gnus-copy-sequence (list)
2657   "Do a complete, total copy of a list."
2658   (if (and (consp list) (not (consp (cdr list))))
2659       (cons (car list) (cdr list))
2660     (mapcar (lambda (elem) (if (consp elem) 
2661                                (if (consp (cdr elem))
2662                                    (gnus-copy-sequence elem)
2663                                  (cons (car elem) (cdr elem)))
2664                              elem))
2665             list)))
2666
2667 (defun gnus-set-difference (list1 list2)
2668   "Return a list of elements of LIST1 that do not appear in LIST2."
2669   (let ((list1 (copy-sequence list1)))
2670     (while list2
2671       (setq list1 (delq (car list2) list1))
2672       (setq list2 (cdr list2)))
2673     list1))
2674
2675 (defun gnus-sorted-complement (list1 list2)
2676   "Return a list of elements of LIST1 that do not appear in LIST2.
2677 Both lists have to be sorted over <."
2678   (let (out)
2679     (if (or (null list1) (null list2))
2680         (or list1 list2)
2681       (while (and list1 list2)
2682         (cond ((= (car list1) (car list2))
2683                (setq list1 (cdr list1)
2684                      list2 (cdr list2)))
2685               ((< (car list1) (car list2))
2686                (setq out (cons (car list1) out))
2687                (setq list1 (cdr list1)))
2688               (t
2689                (setq out (cons (car list2) out))
2690                (setq list2 (cdr list2)))))
2691       (nconc (nreverse out) (or list1 list2)))))
2692
2693 (defun gnus-intersection (list1 list2)      
2694   (let ((result nil))
2695     (while list2
2696       (if (memq (car list2) list1)
2697           (setq result (cons (car list2) result)))
2698       (setq list2 (cdr list2)))
2699     result))
2700
2701 (defun gnus-sorted-intersection (list1 list2)
2702   ;; LIST1 and LIST2 have to be sorted over <.
2703   (let (out)
2704     (while (and list1 list2)
2705       (cond ((= (car list1) (car list2))
2706              (setq out (cons (car list1) out)
2707                    list1 (cdr list1)
2708                    list2 (cdr list2)))
2709             ((< (car list1) (car list2))
2710              (setq list1 (cdr list1)))
2711             (t
2712              (setq list2 (cdr list2)))))
2713     (nreverse out)))
2714
2715 (defun gnus-set-sorted-intersection (list1 list2)
2716   ;; LIST1 and LIST2 have to be sorted over <.
2717   ;; This function modifies LIST1.
2718   (let* ((top (cons nil list1))
2719          (prev top))
2720   (while (and list1 list2)
2721     (cond ((= (car list1) (car list2))
2722            (setq prev list1
2723                  list1 (cdr list1)
2724                  list2 (cdr list2)))
2725           ((< (car list1) (car list2))
2726            (setcdr prev (cdr list1))
2727            (setq list1 (cdr list1)))
2728           (t
2729            (setq list2 (cdr list2)))))
2730   (setcdr prev nil)
2731   (cdr top)))
2732
2733 (defun gnus-compress-sequence (numbers &optional always-list)
2734   "Convert list of numbers to a list of ranges or a single range.
2735 If ALWAYS-LIST is non-nil, this function will always release a list of
2736 ranges."
2737   (let* ((first (car numbers))
2738          (last (car numbers))
2739          result)
2740     (if (null numbers)
2741         nil
2742       (if (not (listp (cdr numbers)))
2743           numbers
2744         (while numbers
2745           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2746                 ((= (1+ last) (car numbers)) ;Still in sequence
2747                  (setq last (car numbers)))
2748                 (t                      ;End of one sequence
2749                  (setq result 
2750                        (cons (if (= first last) first
2751                                (cons first last)) result))
2752                  (setq first (car numbers))
2753                  (setq last  (car numbers))))
2754           (setq numbers (cdr numbers)))
2755         (if (and (not always-list) (null result))
2756             (if (= first last) (list first) (cons first last))
2757           (nreverse (cons (if (= first last) first (cons first last))
2758                           result)))))))
2759
2760 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2761 (defun gnus-uncompress-range (ranges)
2762   "Expand a list of ranges into a list of numbers.
2763 RANGES is either a single range on the form `(num . num)' or a list of
2764 these ranges."
2765   (let (first last result)
2766     (cond 
2767      ((null ranges)
2768       nil)
2769      ((not (listp (cdr ranges)))
2770       (setq first (car ranges))
2771       (setq last (cdr ranges))
2772       (while (<= first last)
2773         (setq result (cons first result))
2774         (setq first (1+ first)))
2775       (nreverse result))
2776      (t
2777       (while ranges
2778         (if (atom (car ranges))
2779             (if (numberp (car ranges))
2780                 (setq result (cons (car ranges) result)))
2781           (setq first (car (car ranges)))
2782           (setq last  (cdr (car ranges)))
2783           (while (<= first last)
2784             (setq result (cons first result))
2785             (setq first (1+ first))))
2786         (setq ranges (cdr ranges)))
2787       (nreverse result)))))
2788
2789 (defun gnus-add-to-range (ranges list)
2790   "Return a list of ranges that has all articles from both RANGES and LIST.
2791 Note: LIST has to be sorted over `<'."
2792   (if (not ranges)
2793       (gnus-compress-sequence list t)
2794     (setq list (copy-sequence list))
2795     (or (listp (cdr ranges))
2796         (setq ranges (list ranges)))
2797     (let ((out ranges)
2798           ilist lowest highest temp)
2799       (while (and ranges list)
2800         (setq ilist list)
2801         (setq lowest (or (and (atom (car ranges)) (car ranges))
2802                          (car (car ranges))))
2803         (while (and list (cdr list) (< (car (cdr list)) lowest))
2804           (setq list (cdr list)))
2805         (if (< (car ilist) lowest)
2806             (progn
2807               (setq temp list)
2808               (setq list (cdr list))
2809               (setcdr temp nil)
2810               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2811         (setq highest (or (and (atom (car ranges)) (car ranges))
2812                           (cdr (car ranges))))
2813         (while (and list (<= (car list) highest))
2814           (setq list (cdr list)))
2815         (setq ranges (cdr ranges)))
2816       (if list
2817           (setq out (nconc (gnus-compress-sequence list t) out)))
2818       (setq out (sort out (lambda (r1 r2) 
2819                             (< (or (and (atom r1) r1) (car r1))
2820                                (or (and (atom r2) r2) (car r2))))))
2821       (setq ranges out)
2822       (while ranges
2823         (if (atom (car ranges))
2824             (if (cdr ranges)
2825                 (if (atom (car (cdr ranges)))
2826                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2827                         (progn
2828                           (setcar ranges (cons (car ranges) 
2829                                                (car (cdr ranges))))
2830                           (setcdr ranges (cdr (cdr ranges)))))
2831                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2832                       (progn
2833                         (setcar (car (cdr ranges)) (car ranges))
2834                         (setcar ranges (car (cdr ranges)))
2835                         (setcdr ranges (cdr (cdr ranges)))))))
2836           (if (cdr ranges)
2837               (if (atom (car (cdr ranges)))
2838                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2839                       (progn
2840                         (setcdr (car ranges) (car (cdr ranges)))
2841                         (setcdr ranges (cdr (cdr ranges)))))
2842                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2843                     (progn
2844                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2845                       (setcdr ranges (cdr (cdr ranges))))))))
2846         (setq ranges (cdr ranges)))
2847       out)))
2848
2849 (defun gnus-remove-from-range (ranges list)
2850   "Return a list of ranges that has all articles from LIST removed from RANGES.
2851 Note: LIST has to be sorted over `<'."
2852   ;; !!! This function shouldn't look like this, but I've got a headache.
2853   (gnus-compress-sequence 
2854    (gnus-sorted-complement
2855     (gnus-uncompress-range ranges) list)))
2856
2857 (defun gnus-member-of-range (number ranges)
2858   (if (not (listp (cdr ranges)))
2859       (and (>= number (car ranges)) 
2860            (<= number (cdr ranges)))
2861     (let ((not-stop t))
2862       (while (and ranges 
2863                   (if (numberp (car ranges))
2864                       (>= number (car ranges))
2865                     (>= number (car (car ranges))))
2866                   not-stop)
2867         (if (if (numberp (car ranges))
2868                 (= number (car ranges))
2869               (and (>= number (car (car ranges)))
2870                    (<= number (cdr (car ranges)))))
2871             (setq not-stop nil))
2872         (setq ranges (cdr ranges)))
2873       (not not-stop))))
2874
2875 \f
2876 ;;;
2877 ;;; Gnus group mode
2878 ;;;
2879
2880 (defvar gnus-group-mode-map nil)
2881 (defvar gnus-group-group-map nil)
2882 (defvar gnus-group-mark-map nil)
2883 (defvar gnus-group-list-map nil)
2884 (defvar gnus-group-sub-map nil)
2885 (put 'gnus-group-mode 'mode-class 'special)
2886
2887 (if gnus-group-mode-map
2888     nil
2889   (setq gnus-group-mode-map (make-keymap))
2890   (suppress-keymap gnus-group-mode-map)
2891   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2892   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2893   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2894   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2895   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2896   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2897   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2898   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2899   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2900   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2901   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2902   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2903   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2904   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2905   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2906   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2907   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2908   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2909   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2910   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2911   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2912   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2913   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2914   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2915   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2916   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2917   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2918   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2919   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2920   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2921   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2922   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2923   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2924   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2925   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2926   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2927   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2928   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2929   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2930   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2931   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2932   (define-key gnus-group-mode-map "V" 'gnus-version)
2933   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2934   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2935   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2936   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2937   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2938   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2939   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2940   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2941   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2942   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2943   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
2944   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2945   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2946   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
2947   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
2948
2949   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
2950   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
2951   (define-prefix-command 'gnus-group-mark-map)
2952   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
2953   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
2954   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
2955   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
2956
2957   (define-prefix-command 'gnus-group-group-map)
2958   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
2959   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
2960   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
2961   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
2962   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
2963   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
2964   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
2965   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
2966   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
2967   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
2968   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
2969   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
2970   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
2971
2972   (define-prefix-command 'gnus-group-list-map)
2973   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
2974   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
2975   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
2976   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
2977   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
2978   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
2979   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
2980   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
2981   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
2982
2983   (define-prefix-command 'gnus-group-sub-map)
2984   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
2985   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
2986   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
2987   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
2988   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
2989   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
2990   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
2991   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
2992
2993 (defun gnus-group-mode ()
2994   "Major mode for reading news.
2995
2996 All normal editing commands are switched off.
2997 \\<gnus-group-mode-map>
2998 The group buffer lists (some of) the groups available.  For instance,
2999 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3000 lists all zombie groups. 
3001
3002 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3003 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3004
3005 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3006
3007 The following commands are available:
3008
3009 \\{gnus-group-mode-map}"
3010   (interactive)
3011   (if gnus-visual (gnus-group-make-menu-bar))
3012   (kill-all-local-variables)
3013   (setq mode-line-modified "-- ")
3014   (make-local-variable 'mode-line-format)
3015   (setq mode-line-format (copy-sequence mode-line-format))
3016   (and (equal (nth 3 mode-line-format) "   ")
3017        (setcar (nthcdr 3 mode-line-format) ""))
3018   (setq major-mode 'gnus-group-mode)
3019   (setq mode-name "Group")
3020   (gnus-group-set-mode-line)
3021   (setq mode-line-process nil)
3022   (use-local-map gnus-group-mode-map)
3023   (buffer-disable-undo (current-buffer))
3024   (setq truncate-lines t)
3025   (setq buffer-read-only t)
3026   (run-hooks 'gnus-group-mode-hook))
3027
3028 (defun gnus-mouse-pick-group (e)
3029   (interactive "e")
3030   (mouse-set-point e)
3031   (gnus-group-read-group nil))
3032
3033 ;;;###autoload
3034 (defun gnus-no-server (&optional arg)
3035   "Read network news.
3036 If ARG is a positive number, Gnus will use that as the
3037 startup level. If ARG is nil, Gnus will be started at level 2. 
3038 If ARG is non-nil and not a positive number, Gnus will
3039 prompt the user for the name of an NNTP server to use.
3040 As opposed to `gnus', this command will not connect to the local server."
3041   (interactive "P")
3042   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3043
3044 (defalias '\(ding\) 'gnus)
3045
3046 ;;;###autoload
3047 (defun gnus (&optional arg dont-connect)
3048   "Read network news.
3049 If ARG is non-nil and a positive number, Gnus will use that as the
3050 startup level. If ARG is non-nil and not a positive number, Gnus will
3051 prompt the user for the name of an NNTP server to use."
3052   (interactive "P")
3053   (if (get-buffer gnus-group-buffer)
3054       (progn
3055         (switch-to-buffer gnus-group-buffer)
3056         (gnus-group-get-new-news))
3057     (gnus-clear-system)
3058     (nnheader-init-server-buffer)
3059     (gnus-read-init-file)
3060     (let ((level (and arg (numberp arg) (> arg 0) arg))
3061           did-connect)
3062       (unwind-protect
3063           (progn
3064             (gnus-group-setup-buffer)
3065             (or dont-connect 
3066                 (setq did-connect
3067                       (gnus-start-news-server (and arg (not level))))))
3068         (if (and (not dont-connect) 
3069                  (not did-connect))
3070             (gnus-group-quit)
3071           (run-hooks 'gnus-startup-hook)
3072           ;; NNTP server is successfully open. 
3073           (gnus-update-format-specifications)
3074           (gnus-summary-make-display-table)
3075           (let ((buffer-read-only nil))
3076             (erase-buffer)
3077             (if (not gnus-inhibit-startup-message)
3078                 (progn
3079                   (gnus-group-startup-message)
3080                   (sit-for 0))))
3081           (gnus-setup-news nil level)
3082           (and gnus-use-dribble-file (gnus-dribble-open))
3083           (gnus-group-list-groups level)
3084           (gnus-configure-windows 'group))))))
3085
3086 (defun gnus-group-startup-message (&optional x y)
3087   "Insert startup message in current buffer."
3088   ;; Insert the message.
3089   (erase-buffer)
3090   (insert
3091    (format "
3092     %s
3093            A newsreader 
3094       for GNU Emacs
3095
3096         Based on GNUS 
3097              written by 
3098      Masanobu UMEDA
3099
3100     Lars Magne 
3101          Ingebrigtsen 
3102       larsi@ifi.uio.no
3103
3104            gnus-version))
3105   ;; And then hack it.
3106   ;; 18 is the longest line.
3107   (indent-rigidly (point-min) (point-max) 
3108                   (/ (max (- (window-width) (or x 28)) 0) 2))
3109   (goto-char (point-min))
3110   ;; +4 is fuzzy factor.
3111   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3112
3113 (defun gnus-group-setup-buffer ()
3114   (or (get-buffer gnus-group-buffer)
3115       (progn
3116         (switch-to-buffer gnus-group-buffer)
3117         (gnus-add-current-to-buffer-list)
3118         (gnus-group-mode)
3119         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3120
3121 (defun gnus-group-list-groups (level &optional unread)
3122   "List newsgroups with level LEVEL or lower that have unread alticles.
3123 Default is all subscribed groups.
3124 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3125   (interactive (list (and current-prefix-arg
3126                           (prefix-numeric-value current-prefix-arg))))
3127   (if gnus-group-use-permanent-levels
3128       (progn
3129         (setq gnus-group-default-list-level 
3130               (or level gnus-group-default-list-level))
3131         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3132     (setq level (or level gnus-group-default-list-level 
3133                     gnus-level-subscribed)))
3134   (gnus-group-setup-buffer)     ;May call from out of group buffer
3135   (let ((case-fold-search nil)
3136         (group (gnus-group-group-name)))
3137     (funcall gnus-group-prepare-function level unread nil)
3138     (if (zerop (buffer-size))
3139         (gnus-message 5 gnus-no-groups-message)
3140       (goto-char (point-min))
3141       (if (not group)
3142           ;; Go to the first group with unread articles.
3143           (gnus-group-search-forward nil nil nil t)
3144         ;; Find the right group to put point on. If the current group
3145         ;; has disapeared in the new listing, try to find the next
3146         ;; one. If no next one can be found, just leave point at the
3147         ;; first newsgroup in the buffer.
3148         (if (not (gnus-goto-char
3149                   (text-property-any (point-min) (point-max) 
3150                                      'gnus-group (intern group))))
3151             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3152               (while (and newsrc
3153                           (not (gnus-goto-char 
3154                                 (text-property-any 
3155                                  (point-min) (point-max) 'gnus-group 
3156                                  (intern (car (car newsrc)))))))
3157                 (setq newsrc (cdr newsrc)))
3158               (or newsrc (progn (goto-char (point-max))
3159                                 (forward-line -1))))))
3160       ;; Adjust cursor point.
3161       (gnus-group-position-cursor))))
3162
3163 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3164   "List all newsgroups with unread articles of level LEVEL or lower.
3165 If ALL is non-nil, list groups that have no unread articles.
3166 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3167 If REGEXP, only list groups matching REGEXP."
3168   (set-buffer gnus-group-buffer)
3169   (let ((buffer-read-only nil)
3170         (newsrc (cdr gnus-newsrc-alist))
3171         (lowest (or lowest 1))
3172         info clevel unread group)
3173     (erase-buffer)
3174     (if (< lowest gnus-level-zombie)
3175         ;; List living groups.
3176         (while newsrc
3177           (setq info (car newsrc)
3178                 group (car info)
3179                 newsrc (cdr newsrc)
3180                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3181           (and unread ; This group might be bogus
3182                (or (not regexp)
3183                    (string-match regexp group))
3184                (<= (setq clevel (car (cdr info))) level) 
3185                (>= clevel lowest)
3186                (or all            ; We list all groups?
3187                    (eq unread t)  ; We list unactivated groups
3188                    (> unread 0)   ; We list groups with unread articles
3189                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3190                (gnus-group-insert-group-line 
3191                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3192
3193     ;; List dead groups.
3194     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3195          (gnus-group-prepare-flat-list-dead 
3196           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3197           gnus-level-zombie ?Z
3198           regexp))
3199     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3200          (gnus-group-prepare-flat-list-dead 
3201           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3202           gnus-level-killed ?K regexp))
3203
3204     (gnus-group-set-mode-line)
3205     (setq gnus-have-all-newsgroups all)
3206     (run-hooks 'gnus-group-prepare-hook)))
3207
3208 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3209   ;; List zombies and killed lists somehwat faster, which was
3210   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3211   ;; this by ignoring the group format specification altogether.
3212   (let (group beg)
3213     (while groups
3214       (setq group (car groups)
3215             groups (cdr groups))
3216       (if (or (not regexp)
3217               (string-match regexp group))
3218           (progn
3219             (setq beg (point))
3220             (insert (format " %c    *: %s\n" mark group))
3221             (add-text-properties 
3222              beg (1+ beg) 
3223              (list 'gnus-group (intern group)
3224                    'gnus-unread t
3225                    'gnus-level level)))))))
3226
3227 (defun gnus-group-real-name (group)
3228   "Find the real name of a foreign newsgroup."
3229   (if (string-match ":[^:]+$" group)
3230       (substring group (1+ (match-beginning 0)))
3231     group))
3232
3233 (defun gnus-group-prefixed-name (group method)
3234   "Return the whole name from GROUP and METHOD."
3235   (and (stringp method) (setq method (gnus-server-to-method method)))
3236   (concat (format "%s" (car method))
3237           (if (and 
3238                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3239                (not (string= (nth 1 method) "")))
3240               (concat "+" (nth 1 method)))
3241           ":" group))
3242
3243 (defun gnus-group-real-prefix (group)
3244   "Return the prefix of the current group name."
3245   (if (string-match "^[^:]+:" group)
3246       (substring group 0 (match-end 0))
3247     ""))
3248
3249 (defun gnus-group-method-name (group)
3250   "Return the method used for selecting GROUP."
3251   (let ((prefix (gnus-group-real-prefix group)))
3252     (if (equal prefix "")
3253         gnus-select-method
3254       (if (string-match "^[^\\+]+\\+" prefix)
3255           (list (intern (substring prefix 0 (1- (match-end 0))))
3256                 (substring prefix (match-end 0) (1- (length prefix))))
3257         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3258
3259 (defun gnus-group-foreign-p (group)
3260   "Return nil if GROUP is native, non-nil if it is foreign."
3261   (string-match ":" group))
3262
3263 (defun gnus-group-set-info (info &optional method-only-group part)
3264   (let* ((entry (gnus-gethash
3265                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3266          (part-info info)
3267          (info (if method-only-group (nth 2 entry) info)))
3268     (if (not method-only-group)
3269         ()
3270       (or entry
3271           (error "Trying to change non-existant group %s" method-only-group))
3272       ;; We have recevied parts of the actual group info - either the
3273       ;; select method or the group parameters.  We first check
3274       ;; whether we have to extend the info, and if so, do that.
3275       (let ((len (length info))
3276             (total (if (eq part 'method) 5 6)))
3277         (and (< len total)
3278              (setcdr (nthcdr (1- len) info)
3279                      (make-list (- total len) nil)))
3280         ;; Then we enter the new info.
3281         (setcar (nthcdr (1- total) info) part-info)))
3282     ;; We uncompress some lists of marked articles.
3283     (let (marked)
3284       (if (not (setq marked (nth 3 info)))
3285           ()
3286         (while marked
3287           (or (eq 'score (car (car marked)))
3288               (eq 'bookmark (car (car marked)))
3289               (eq 'killed (car (car marked)))
3290               (setcdr (car marked) 
3291                       (gnus-uncompress-range (cdr (car marked)))))
3292           (setq marked (cdr marked)))))
3293     (if entry
3294         ()
3295       ;; This is a new group, so we just create it.
3296       (save-excursion
3297         (set-buffer gnus-group-buffer)
3298         (if (nth 4 info)
3299             ;; It's a foreign group...
3300             (gnus-group-make-group 
3301              (gnus-group-real-name (car info))
3302              (prin1-to-string (car (nth 4 info)))
3303              (nth 1 (nth 4 info)))
3304           ;; It's a native group.
3305           (gnus-group-make-group
3306            (car info)
3307            (prin1-to-string (car gnus-select-method))
3308            (nth 1 gnus-select-method)))
3309         (gnus-message 6 "Note: New group created")
3310         (setq entry 
3311               (gnus-gethash (gnus-group-prefixed-name 
3312                              (gnus-group-real-name (car info))
3313                              (or (nth 4 info) gnus-select-method))
3314                             gnus-newsrc-hashtb))))
3315     ;; Whether it was a new group or not, we now have the entry, so we
3316     ;; can do the update.
3317     (if entry
3318         (progn
3319           (setcar (nthcdr 2 entry) info)
3320           (if (and (not (eq (car entry) t)) 
3321                    (gnus-gethash (car info) gnus-active-hashtb))
3322               (let ((marked (nth 3 info)))
3323                 (setcar entry 
3324                         (max 0 (- (length (gnus-list-of-unread-articles 
3325                                            (car info)))
3326                                   (length (cdr (assq 'tick marked)))
3327                                   (length (cdr (assq 'dormant marked)))))))))
3328       (error "No such group: %s" (car info)))))
3329
3330 (defun gnus-group-set-method-info (group select-method)
3331   (gnus-group-set-info select-method group 'method))
3332
3333 (defun gnus-group-set-params-info (group params)
3334   (gnus-group-set-info params group 'params))
3335
3336 (defun gnus-group-update-group-line ()
3337   "This function updates the current line in the newsgroup buffer and
3338 moves the point to the colon."
3339   (let* ((buffer-read-only nil)
3340          (group (gnus-group-group-name))
3341          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3342     (if entry
3343         (gnus-dribble-enter 
3344          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3345                  ")")))
3346     (beginning-of-line)
3347     (delete-region (point) (progn (forward-line 1) (point)))
3348     (gnus-group-insert-group-line-info group)
3349     (forward-line -1)
3350     (gnus-group-position-cursor)))
3351
3352 (defun gnus-group-insert-group-line-info (group)
3353   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3354         active info)
3355     (if entry
3356         (progn
3357           (setq info (nth 2 entry))
3358           (gnus-group-insert-group-line 
3359            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3360       (setq active (gnus-gethash group gnus-active-hashtb))
3361       (gnus-group-insert-group-line 
3362        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3363                    gnus-level-killed)
3364        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3365
3366 (defun gnus-group-insert-group-line (gformat group level marked number method)
3367   (let* ((gformat (or gformat gnus-group-line-format-spec))
3368          (active (gnus-gethash group gnus-active-hashtb))
3369          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3370          (number-of-dormant (length (cdr (assq 'dormant marked))))
3371          (number-of-ticked (length (cdr (assq 'tick marked))))
3372          (number-of-ticked-and-dormant
3373           (+ number-of-ticked number-of-dormant))
3374          (number-of-unread-unticked 
3375           (if (numberp number) (int-to-string (max 0 number))
3376             "*"))
3377          (number-of-read
3378           (if (numberp number)
3379               (max 0 (- number-total number))
3380             "*"))
3381          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3382                            ((<= level gnus-level-unsubscribed) ?U)
3383                            ((= level gnus-level-zombie) ?Z)
3384                            (t ?K)))
3385          (qualified-group (gnus-group-real-name group))
3386          (newsgroup-description 
3387           (if gnus-description-hashtb
3388               (or (gnus-gethash group gnus-description-hashtb) "")
3389             ""))
3390          (moderated (if (member group gnus-moderated-list) ?m ? ))
3391          (moderated-string (if (eq moderated ?m) "(m)" ""))
3392          (method (gnus-server-get-method group method))
3393          (news-server (or (car (cdr method)) ""))
3394          (news-method (or (car method) ""))
3395          (news-method-string 
3396           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3397          (marked (if (and 
3398                       (numberp number) 
3399                       (zerop number)
3400                       (> number-of-ticked 0))
3401                      ?* ? ))
3402          (number (if (eq number t) "*" (+ number number-of-dormant 
3403                                           number-of-ticked)))
3404          (buffer-read-only nil)
3405          b)
3406     (beginning-of-line)
3407     (setq b (point))
3408     ;; Insert the text.
3409     (insert (eval gformat))
3410
3411     (add-text-properties 
3412      b (1+ b) (list 'gnus-group (intern group)
3413                     'gnus-unread (if (numberp number)
3414                                      (string-to-int number-of-unread-unticked)
3415                                    t)
3416                     'gnus-marked marked
3417                     'gnus-level level))))
3418
3419 (defun gnus-group-update-group (group &optional visible-only)
3420   "Update newsgroup info of GROUP.
3421 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3422   (save-excursion
3423     (set-buffer gnus-group-buffer)
3424     (let ((buffer-read-only nil)
3425           visible)
3426       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3427         (if entry
3428             (gnus-dribble-enter 
3429              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3430                      ")"))))
3431       ;; Buffer may be narrowed.
3432       (save-restriction
3433         (widen)
3434         ;; Search a line to modify.  If the buffer is large, the search
3435         ;; takes long time.  In most cases, current point is on the line
3436         ;; we are looking for.  So, first of all, check current line. 
3437         (if (or (progn
3438                   (beginning-of-line)
3439                   (eq (get-text-property (point) 'gnus-group)
3440                       (intern group)))
3441                 (progn
3442                   (gnus-goto-char 
3443                    (text-property-any 
3444                     (point-min) (point-max) 'gnus-group (intern group)))))
3445             ;; GROUP is listed in current buffer. So, delete old line.
3446             (progn
3447               (setq visible t)
3448               (beginning-of-line)
3449               (delete-region (point) (progn (forward-line 1) (point))))
3450           ;; No such line in the buffer, find out where it's supposed to
3451           ;; go, and insert it there (or at the end of the buffer).
3452           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3453           (or visible-only
3454               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
3455                 (while (and entry
3456                             (car entry)
3457                             (not
3458                              (gnus-goto-char
3459                               (text-property-any
3460                                (point-min) (point-max) 
3461                                'gnus-group (intern (car (car entry)))))))
3462                   (setq entry (cdr entry)))
3463                 (if entry (forward-line 1)
3464                   (goto-char (point-max)))))))
3465       (if (or visible (not visible-only))
3466           (gnus-group-insert-group-line-info group))
3467       (gnus-group-set-mode-line))))
3468
3469 (defun gnus-group-set-mode-line ()
3470   (if (memq 'group gnus-updated-mode-lines)
3471       (let* ((gformat (or gnus-group-mode-line-format-spec
3472                           (setq gnus-group-mode-line-format-spec
3473                                 (gnus-parse-format 
3474                                  gnus-group-mode-line-format 
3475                                  gnus-group-mode-line-format-alist))))
3476              (news-server (car (cdr gnus-select-method)))
3477              (news-method (car gnus-select-method))
3478              (max-len 60)
3479              (mode-string (eval gformat)))
3480         (setq mode-string (eval gformat))
3481         (if (> (length mode-string) max-len) 
3482             (setq mode-string (substring mode-string 0 (- max-len 4))))
3483         (setq mode-line-buffer-identification mode-string)
3484         (set-buffer-modified-p t))))
3485
3486 (defun gnus-group-group-name ()
3487   "Get the name of the newsgroup on the current line."
3488   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3489     (and group (symbol-name group))))
3490
3491 (defun gnus-group-group-level ()
3492   "Get the level of the newsgroup on the current line."
3493   (get-text-property (gnus-point-at-bol) 'gnus-level))
3494
3495 (defun gnus-group-search-forward (&optional backward all level first-too)
3496   "Find the next newsgroup with unread articles.
3497 If BACKWARD is non-nil, find the previous newsgroup instead.
3498 If ALL is non-nil, just find any newsgroup.
3499 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3500 group exists.
3501 If FIRST-TOO, the current line is also eligeble as a target."
3502   (let ((way (if backward -1 1))
3503         (low 10)
3504         (beg (point))
3505         pos found)
3506     (if (and backward (progn (beginning-of-line)) (bobp))
3507         nil
3508       (or first-too (forward-line way))
3509       (while (and 
3510               (not (eobp))
3511               (not (setq 
3512                     found 
3513                     (and (or all
3514                              (and
3515                               (let ((unread 
3516                                      (get-text-property (point) 'gnus-unread)))
3517                                 (or (eq unread t) (and unread (> unread 0))))
3518                               (let ((lev (get-text-property
3519                                           (point) 'gnus-level)))
3520                                 (and lev (<= (get-text-property 
3521                                               (point) 'gnus-level)
3522                                              gnus-level-subscribed)))))
3523                          (or (not level)
3524                              (let ((lev (get-text-property (point) 'gnus-level)))
3525                                (if (and lev (<= lev level))
3526                                    t
3527                                  (if (< lev low)
3528                                      (progn
3529                                        (setq low lev)
3530                                        (setq pos (point))))
3531                                  nil))))))
3532               (zerop (forward-line way)))))
3533     (if found 
3534         (progn (gnus-group-position-cursor) t)
3535       (if pos (goto-char pos) (goto-char beg))
3536       nil)))
3537
3538 ;;; Gnus group mode commands
3539
3540 ;; Group marking.
3541
3542 (defun gnus-group-mark-group (n &optional unmark)
3543   "Mark the current group."
3544   (interactive "p")
3545   (let ((buffer-read-only nil)
3546         group)
3547     (while 
3548         (and (> n 0) 
3549              (setq group (gnus-group-group-name))
3550              (progn
3551                (beginning-of-line)
3552                (forward-char 1)
3553                (delete-char 1)
3554                (if unmark
3555                    (progn
3556                      (insert " ")
3557                      (setq gnus-group-marked (delete group gnus-group-marked)))
3558                  (insert "#")
3559                  (setq gnus-group-marked (cons group gnus-group-marked)))
3560                t)
3561              (zerop (gnus-group-next-group 1)))
3562       (setq n (1- n)))
3563     (gnus-summary-position-cursor)
3564     n))
3565
3566 (defun gnus-group-unmark-group (n)
3567   "Remove the mark from the current group."
3568   (interactive "p")
3569   (gnus-group-mark-group n 'unmark))
3570
3571 (defun gnus-group-mark-region (unmark beg end)
3572   "Mark all groups between point and mark.
3573 If UNMARK, remove the mark instead."
3574   (interactive "P\nr")
3575   (let ((num (count-lines beg end)))
3576     (save-excursion
3577       (goto-char beg)
3578       (- num (gnus-group-mark-group num unmark)))))
3579
3580 (defun gnus-group-remove-mark (group)
3581   (and (gnus-group-goto-group group)
3582        (save-excursion
3583          (gnus-group-mark-group 1 'unmark))))
3584
3585 ;; Return a list of groups to work on.  Take into consideration N (the
3586 ;; prefix) and the list of marked groups.
3587 (defun gnus-group-process-prefix (n)
3588   (cond (n
3589          (setq n (prefix-numeric-value n))
3590          ;; There is a prefix, so we return a list of the N next
3591          ;; groups. 
3592          (let ((way (if (< n 0) -1 1))
3593                (n (abs n))
3594                group groups)
3595            (save-excursion
3596              (while (and (> n 0)
3597                          (setq group (gnus-group-group-name)))
3598                (setq groups (cons group groups))
3599                (setq n (1- n))
3600                (forward-line way)))
3601            (nreverse groups)))
3602         (gnus-group-marked
3603          ;; No prefix, but a list of marked articles.
3604          (reverse gnus-group-marked))
3605         (t
3606          ;; Neither marked articles or a prefix, so we return the
3607          ;; current group.
3608          (let ((group (gnus-group-group-name)))
3609            (and group (list group))))))
3610
3611 ;; Selecting groups.
3612
3613 (defun gnus-group-read-group (all &optional no-article group)
3614   "Read news in this newsgroup.
3615 If argument ALL is non-nil, already read articles become readable.
3616 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3617   (interactive "P")
3618   (let ((group (or group (gnus-group-group-name)))
3619         number active marked entry)
3620     (or group (error "No group on current line"))
3621     (setq marked 
3622           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3623     ;; This group might be a dead group. In that case we have to get
3624     ;; the number of unread articles from `gnus-active-hashtb'.
3625     (if entry
3626         (setq number (car entry))
3627       (if (setq active (gnus-gethash group gnus-active-hashtb))
3628           (setq number (- (1+ (cdr active)) (car active)))))
3629     (gnus-summary-read-group 
3630      group (or all (and (numberp number) 
3631                         (zerop (+ number (length (cdr (assq 'tick marked)))
3632                                   (length (cdr (assq 'dormant marked)))))))
3633      no-article)))
3634
3635 (defun gnus-group-select-group (all)
3636   "Select this newsgroup.
3637 No article is selected automatically.
3638 If argument ALL is non-nil, already read articles become readable."
3639   (interactive "P")
3640   (gnus-group-read-group all t))
3641
3642 ;; Enter a group that is not in the group buffer. Non-nil is returned
3643 ;; if selection was successful.
3644 (defun gnus-group-read-ephemeral-group 
3645   (group method &optional activate quit-config)
3646   (let ((group (if (gnus-group-foreign-p group) group
3647                  (gnus-group-prefixed-name group method))))
3648     (gnus-sethash 
3649      group
3650      (list t nil (list group gnus-level-default-subscribed nil nil 
3651                        (append method
3652                                (list
3653                                 (list 'quit-config 
3654                                       (if quit-config quit-config
3655                                         (cons (current-buffer) 'summary)))))))
3656      gnus-newsrc-hashtb)
3657     (set-buffer gnus-group-buffer)
3658     (or (gnus-server-opened method)
3659         (gnus-open-server method)
3660         (error "Unable to contact server: %s" (gnus-status-message method)))
3661     (if activate (gnus-request-group group))
3662     (condition-case ()
3663         (gnus-group-read-group t t group)
3664       (error nil)
3665       (quit nil))
3666     (not (equal major-mode 'gnus-group-mode))))
3667   
3668 (defun gnus-group-jump-to-group (group)
3669   "Jump to newsgroup GROUP."
3670   (interactive (list (completing-read "Group: " gnus-active-hashtb nil
3671                                       (not (not gnus-read-active-file)))))
3672
3673   (if (equal group "")
3674       (error "empty group name"))
3675
3676   (let ((b (text-property-any (point-min) (point-max) 
3677                               'gnus-group (intern group))))
3678     (if b
3679         ;; Either go to the line in the group buffer...
3680         (goto-char b)
3681       ;; ... or insert the line.
3682       (or
3683        (gnus-gethash group gnus-active-hashtb)
3684        (gnus-activate-newsgroup group)
3685        (error "%s error: %s" group (gnus-status-message group)))
3686
3687       (gnus-group-update-group group)
3688       (goto-char (text-property-any (point-min) (point-max) 
3689                                     'gnus-group (intern group)))))
3690   ;; Adjust cursor point.
3691   (gnus-group-position-cursor))
3692
3693 (defun gnus-group-goto-group (group)
3694   "Goto to newsgroup GROUP."
3695   (let ((b (text-property-any (point-min) (point-max) 
3696                               'gnus-group (intern group))))
3697     (and b (goto-char b))))
3698
3699 (defun gnus-group-next-group (n)
3700   "Go to next N'th newsgroup.
3701 If N is negative, search backward instead.
3702 Returns the difference between N and the number of skips actually
3703 done."
3704   (interactive "p")
3705   (gnus-group-next-unread-group n t))
3706
3707 (defun gnus-group-next-unread-group (n &optional all level)
3708   "Go to next N'th unread newsgroup.
3709 If N is negative, search backward instead.
3710 If ALL is non-nil, choose any newsgroup, unread or not.
3711 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3712 such group can be found, the next group with a level higher than
3713 LEVEL.
3714 Returns the difference between N and the number of skips actually
3715 made."
3716   (interactive "p")
3717   (let ((backward (< n 0))
3718         (n (abs n)))
3719     (while (and (> n 0)
3720                 (gnus-group-search-forward 
3721                  backward (or (not gnus-group-goto-unread) all) level))
3722       (setq n (1- n)))
3723     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3724                                (if level " on this level or higher" "")))
3725     n))
3726
3727 (defun gnus-group-prev-group (n)
3728   "Go to previous N'th newsgroup.
3729 Returns the difference between N and the number of skips actually
3730 done."
3731   (interactive "p")
3732   (gnus-group-next-unread-group (- n) t))
3733
3734 (defun gnus-group-prev-unread-group (n)
3735   "Go to previous N'th unread newsgroup.
3736 Returns the difference between N and the number of skips actually
3737 done."  
3738   (interactive "p")
3739   (gnus-group-next-unread-group (- n)))
3740
3741 (defun gnus-group-next-unread-group-same-level (n)
3742   "Go to next N'th unread newsgroup on the same level.
3743 If N is negative, search backward instead.
3744 Returns the difference between N and the number of skips actually
3745 done."
3746   (interactive "p")
3747   (gnus-group-next-unread-group n t (gnus-group-group-level))
3748   (gnus-group-position-cursor))
3749
3750 (defun gnus-group-prev-unread-group-same-level (n)
3751   "Go to next N'th unread newsgroup on the same level.
3752 Returns the difference between N and the number of skips actually
3753 done."
3754   (interactive "p")
3755   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3756   (gnus-group-position-cursor))
3757
3758 (defun gnus-group-best-unread-group (&optional exclude-group)
3759   "Go to the group with the highest level.
3760 If EXCLUDE-GROUP, do not go to that group."
3761   (interactive)
3762   (goto-char (point-min))
3763   (let ((best 100000)
3764         unread best-point)
3765     (while (setq unread (get-text-property (point) 'gnus-unread))
3766       (if (and (numberp unread) (> unread 0))
3767           (progn
3768             (or best-point (setq best-point (point)))
3769             (if (and (< (get-text-property (point) 'gnus-level) best)
3770                      (or (not exclude-group)
3771                          (not (equal exclude-group (gnus-group-group-name)))))
3772                 (progn 
3773                   (setq best (get-text-property (point) 'gnus-level))
3774                   (setq best-point (point))))))
3775       (forward-line 1))
3776     (if best-point (goto-char best-point))
3777     (gnus-summary-position-cursor)
3778     (and best-point (gnus-group-group-name))))
3779
3780 (defun gnus-group-first-unread-group ()
3781   "Go to the first group with unread articles."
3782   (interactive)
3783   (goto-char (point-min))
3784   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3785       (gnus-group-next-unread-group 1))
3786   (gnus-group-position-cursor))
3787
3788 (defun gnus-group-enter-server-mode ()
3789   "Jump to the server buffer."
3790   (interactive)
3791   (gnus-server-setup-buffer)
3792   (gnus-configure-windows 'server)
3793   (gnus-server-prepare))
3794
3795 (defun gnus-group-make-group (name method &optional address)
3796   "Add a new newsgroup.
3797 The user will be prompted for a NAME, for a select METHOD, and an
3798 ADDRESS."
3799   (interactive
3800    (cons 
3801     (read-string "Group name: ")
3802     (let ((method
3803            (completing-read 
3804             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3805             nil t)))
3806       (if (assoc method gnus-valid-select-methods)
3807           (list method
3808                 (if (memq 'prompt-address
3809                           (assoc method gnus-valid-select-methods))
3810                     (read-string "Address: ")
3811                   ""))
3812         (list method nil)))))
3813   
3814   (let* ((meth (if address (list (intern method) address) method))
3815          (nname (gnus-group-prefixed-name name meth))
3816          info)
3817     (and (gnus-gethash nname gnus-newsrc-hashtb)
3818          (error "Group %s already exists" nname))
3819     (gnus-group-change-level 
3820      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3821      gnus-level-default-subscribed gnus-level-killed 
3822      (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3823                        gnus-newsrc-hashtb) t)
3824     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3825     (gnus-dribble-enter 
3826      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3827     (gnus-group-insert-group-line-info nname)
3828
3829     (and (gnus-check-backend-function 'request-create-group nname)
3830          (gnus-request-create-group nname))))
3831
3832 (defun gnus-group-edit-group (group &optional part)
3833   "Edit the group on the current line."
3834   (interactive (list (gnus-group-group-name)))
3835   (let ((done-func '(lambda () 
3836                       "Exit editing mode and update the information."
3837                       (interactive)
3838                       (gnus-group-edit-group-done 'part 'group)))
3839         (part (or part 'info))
3840         (winconf (current-window-configuration))
3841         info)
3842     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3843       (error "No group on current line"))
3844     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3845     (gnus-configure-windows 'edit-group)
3846     (gnus-add-current-to-buffer-list)
3847     (emacs-lisp-mode)
3848     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3849     (use-local-map (copy-keymap emacs-lisp-mode-map))
3850     (local-set-key "\C-c\C-c" done-func)
3851     (make-local-variable 'gnus-prev-winconf)
3852     (setq gnus-prev-winconf winconf)
3853     ;; We modify the func to let it know what part it is editing.
3854     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3855     (setcar (cdr (cdr (nth 4 done-func))) group)
3856     (erase-buffer)
3857     (insert
3858      (cond 
3859       ((eq part 'method)
3860        ";; Type `C-c C-c' after editing the select method.\n\n")
3861       ((eq part 'params)
3862        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3863       ((eq part 'info)
3864        ";; Type `C-c C-c' after editing the group info.\n\n")))
3865     (let ((cinfo (gnus-copy-sequence info))
3866           marked)
3867       (if (not (setq marked (nth 3 cinfo)))
3868           ()
3869         (while marked
3870           (or (eq 'score (car (car marked)))
3871               (eq 'bookmark (car (car marked)))
3872               (eq 'killed (car (car marked)))
3873               (setcdr (car marked) 
3874                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3875           (setq marked (cdr marked))))
3876       (insert 
3877        (pp-to-string
3878         (cond ((eq part 'method)
3879                (or (nth 4 info) "native"))
3880               ((eq part 'params)
3881                (nth 5 info))
3882               (t
3883                cinfo)))
3884        "\n"))))
3885
3886 (defun gnus-group-edit-group-method (group)
3887   "Edit the select method of GROUP."
3888   (interactive (list (gnus-group-group-name)))
3889   (gnus-group-edit-group group 'method))
3890
3891 (defun gnus-group-edit-group-parameters (group)
3892   "Edit the group parameters of GROUP."
3893   (interactive (list (gnus-group-group-name)))
3894   (gnus-group-edit-group group 'params))
3895
3896 (defun gnus-group-edit-group-done (part group)
3897   "Get info from buffer, update variables and jump to the group buffer."
3898   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3899   (goto-char (point-min))
3900   (let ((form (read (current-buffer)))
3901         (winconf gnus-prev-winconf))
3902     (if (eq part 'info) 
3903         (gnus-group-set-info form)
3904       (gnus-group-set-info form group part))
3905     (kill-buffer (current-buffer))
3906     (and winconf (set-window-configuration winconf))
3907     (set-buffer gnus-group-buffer)
3908     (gnus-group-update-group (gnus-group-group-name))
3909     (gnus-group-position-cursor)))
3910
3911 (defun gnus-group-make-help-group ()
3912   "Create the (ding) Gnus documentation group."
3913   (interactive)
3914   (and (gnus-gethash (gnus-group-prefixed-name "gnus-help" '(nndoc ""))
3915                      gnus-newsrc-hashtb)
3916        (error "Documentation group already exists"))
3917   (let ((path load-path))
3918     (while (and path
3919                 (not (file-exists-p (concat (file-name-as-directory (car path))
3920                                             "doc.txt"))))
3921       (setq path (cdr path)))
3922     (or path (error "Couldn't find doc group"))
3923     (gnus-group-make-group 
3924      "gnus-help" "nndoc" 
3925      (concat (file-name-as-directory (car path)) "doc.txt"))
3926     (gnus-group-position-cursor)))
3927
3928 (defun gnus-group-make-doc-group (file type)
3929   "Create a group that uses a single file as the source."
3930   (interactive 
3931    (list (read-file-name "File name: ") 
3932          (let ((err "")
3933                found char)
3934            (while (not found)
3935              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
3936              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
3937                                ((= char ?b) 'babyl)
3938                                ((= char ?d) 'digest)
3939                                (t (setq mess "%c unknown. " char)
3940                                   nil))))
3941            found)))
3942   (let* ((file (expand-file-name file))
3943          (name (gnus-generate-new-group-name
3944                 (gnus-group-prefixed-name
3945                  (file-name-nondirectory file) '(nndoc "")))))
3946     (gnus-group-make-group 
3947      (gnus-group-real-name name)
3948      (list 'nndoc name
3949            (list 'nndoc-address file)
3950            (list 'nndoc-article-type type)))))
3951
3952 (defun gnus-group-make-archive-group ()
3953   "Create the (ding) Gnus archive group."
3954   (interactive)
3955   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3956                      gnus-newsrc-hashtb)
3957        (error "Archive group already exists"))
3958   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
3959   (gnus-group-position-cursor))
3960
3961 (defun gnus-group-make-directory-group (dir)
3962   "Create an nndir group.
3963 The user will be prompted for a directory. The contents of this
3964 directory will be used as a newsgroup. The directory should contain
3965 mail messages or news articles in files that have numeric names."
3966   (interactive
3967    (list (read-file-name "Create group from directory: ")))
3968   (or (file-exists-p dir) (error "No such directory"))
3969   (or (file-directory-p dir) (error "Not a directory"))
3970   (gnus-group-make-group dir "nndir" dir)
3971   (gnus-group-position-cursor))
3972
3973 (defun gnus-group-make-kiboze-group (group address scores)
3974   "Create an nnkiboze group.
3975 The user will be prompted for a name, a regexp to match groups, and
3976 score file entries for articles to include in the group."
3977   (interactive
3978    (list
3979     (read-string "nnkiboze group name: ")
3980     (read-string "Source groups (regexp): ")
3981     (let ((headers (mapcar (lambda (group) (list group))
3982                            '("subject" "from" "number" "date" "message-id"
3983                              "references" "chars" "lines" "xref")))
3984           scores header regexp regexps)
3985       (while (not (equal "" (setq header (completing-read 
3986                                           "Match on header: " headers nil t))))
3987         (setq regexps nil)
3988         (while (not (equal "" (setq regexp (read-string 
3989                                             (format "Match on %s (string): "
3990                                                     header)))))
3991           (setq regexps (cons (list regexp nil nil 'r) regexps)))
3992         (setq scores (cons (cons header regexps) scores)))
3993       scores)))
3994   (gnus-group-make-group group "nnkiboze" address)
3995   (save-excursion
3996     (gnus-set-work-buffer)
3997     (let (emacs-lisp-mode-hook)
3998       (pp scores (current-buffer)))
3999     (write-region (point-min) (point-max) 
4000                   (concat (or gnus-kill-files-directory "~/News")
4001                           "nnkiboze:" group "." gnus-score-file-suffix)))
4002   (gnus-group-position-cursor))
4003
4004 (defun gnus-group-add-to-virtual (n vgroup)
4005   "Add the current group to a virtual group."
4006   (interactive
4007    (list current-prefix-arg
4008          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4009                           "nnvirtual:")))
4010   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4011       (error "%s is not an nnvirtual group" vgroup))
4012   (let* ((groups (gnus-group-process-prefix n))
4013          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4014     (setcar (cdr method)
4015             (concat 
4016              (nth 1 method) "\\|"
4017              (mapconcat 
4018               (lambda (s) 
4019                 (gnus-group-remove-mark s)
4020                 (concat "\\(^" (regexp-quote s) "$\\)"))
4021               groups "\\|"))))
4022   (gnus-group-position-cursor))
4023
4024 (defun gnus-group-make-empty-virtual (group)
4025   "Create a new, fresh, empty virtual group."
4026   (interactive "sCreate new, empty virtual group: ")
4027   (let* ((method (list 'nnvirtual ""))
4028          (pgroup (gnus-group-prefixed-name group method)))
4029     ;; Check whether it exists already.
4030     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4031          (error "Group %s already exists." pgroup))
4032     ;; Subscribe the new group after the group on the current line.
4033     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4034     (gnus-group-update-group pgroup)
4035     (forward-line -1)
4036     (gnus-group-position-cursor)))
4037
4038 (defun gnus-group-enter-directory (dir)
4039   "Enter an ephemeral nneething group."
4040   (interactive "DDirectory to read: ")
4041   (let* ((method (list 'nneething dir))
4042          (leaf (gnus-group-prefixed-name
4043                 (file-name-nondirectory (directory-file-name dir))
4044                 method))
4045          (name (gnus-generate-new-group-name leaf))
4046          (num 0))
4047     (let ((nneething-read-only t))
4048       (or (gnus-group-read-ephemeral-group 
4049            name method t
4050            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4051                                       'summary 'group)))
4052           (error "Couldn't enter %s" dir)))))
4053
4054 ;; Group sorting commands
4055 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4056
4057 (defun gnus-group-sort-groups ()
4058   "Sort the group buffer using `gnus-group-sort-function'."
4059   (interactive)
4060   (setq gnus-newsrc-alist 
4061         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4062   (gnus-make-hashtable-from-newsrc-alist)
4063   (gnus-group-list-groups nil))
4064
4065 (defun gnus-group-sort-by-alphabet (info1 info2)
4066   (string< (car info1) (car info2)))
4067
4068 (defun gnus-group-sort-by-unread (info1 info2)
4069   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4070         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4071     (< (or (and (numberp n1) n1) 0)
4072        (or (and (numberp n2) n2) 0))))
4073
4074 (defun gnus-group-sort-by-level (info1 info2)
4075   (< (nth 1 info1) (nth 1 info2)))
4076
4077 ;; Group catching up.
4078
4079 (defun gnus-group-catchup-current (n &optional all)
4080   "Mark all articles not marked as unread in current newsgroup as read.
4081 If prefix argument N is numeric, the ARG next newsgroups will be
4082 caught up. If ALL is non-nil, marked articles will also be marked as
4083 read. Cross references (Xref: header) of articles are ignored.
4084 The difference between N and actual number of newsgroups that were
4085 caught up is returned."
4086   (interactive "P")
4087   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4088                gnus-expert-user
4089                (gnus-y-or-n-p
4090                 (if all
4091                     "Do you really want to mark all articles as read? "
4092                   "Mark all unread articles as read? "))))
4093       n
4094     (let ((groups (gnus-group-process-prefix n))
4095           (ret 0))
4096       (while groups
4097         (gnus-group-remove-mark (car groups))
4098         (if (not (gnus-group-goto-group (car groups)))
4099             (setq ret (1+ ret))
4100           (gnus-group-catchup (car groups) all)
4101           (gnus-group-update-group-line))
4102         (setq groups (cdr groups)))
4103       (gnus-group-next-unread-group 1)
4104       ret)))
4105
4106 (defun gnus-group-catchup-current-all (n)
4107   "Mark all articles in current newsgroup as read.
4108 Cross references (Xref: header) of articles are ignored."
4109   (interactive "P")
4110   (gnus-group-catchup-current n 'all))
4111
4112 (defun gnus-group-catchup (group &optional all)
4113   "Mark all articles in GROUP as read.
4114 If ALL is non-nil, all articles are marked as read.
4115 The return value is the number of articles that were marked as read,
4116 or nil if no action could be taken."
4117   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4118          (num (car entry))
4119          (marked (nth 3 (nth 2 entry)))
4120          ticked)
4121     (if (not (numberp (car entry)))
4122         (gnus-message 1 "Can't catch up; non-active group")
4123       ;; Do the updating only if the newsgroup isn't killed.
4124       (if entry
4125           (progn
4126             (setq ticked (if all nil (cdr (assq 'tick marked))))
4127             (gnus-update-read-articles group ticked nil ticked)
4128             (if (and all marked)
4129                 (setcar (nthcdr 3 (nth 2 entry)) 
4130                         (delq (assq 'dormant marked) marked))))))
4131     num))
4132
4133 (defun gnus-group-expire-articles (n)
4134   "Expire all expirable articles in the current newsgroup."
4135   (interactive "P")
4136   (let ((groups (gnus-group-process-prefix n))
4137         group)
4138     (or groups (error "No groups to expire"))
4139     (while groups
4140       (setq group (car groups)
4141             groups (cdr groups))
4142       (gnus-group-remove-mark group)
4143       (if (not (gnus-check-backend-function 'request-expire-articles group))
4144           ()
4145         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4146                (expirable (if (memq 'total-expire (nth 5 info))
4147                               (cons nil (gnus-list-of-read-articles group))
4148                             (assq 'expire (nth 3 info)))))
4149           (and expirable 
4150                (setcdr expirable
4151                        (gnus-request-expire-articles 
4152                         (cdr expirable) group))))))))
4153
4154 (defun gnus-group-expire-all-groups ()
4155   "Expire all expirable articles in all newsgroups."
4156   (interactive)
4157   (gnus-message 5 "Expiring...")
4158   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4159                                    (cdr gnus-newsrc-alist))))
4160     (gnus-group-expire-articles nil))
4161   (gnus-message 5 "Expiring...done"))
4162
4163 (defun gnus-group-set-current-level (n level)
4164   "Set the level of the next N groups to LEVEL."
4165   (interactive "P\nnLevel: ")
4166   (or (and (>= level 1) (<= level gnus-level-killed))
4167       (error "Illegal level: %d" level))
4168   (let ((groups (gnus-group-process-prefix n))
4169         group)
4170     (while groups
4171       (setq group (car groups)
4172             groups (cdr groups))
4173       (gnus-group-remove-mark group)
4174       (gnus-message 6 "Changed level of %s from %d to %d" 
4175                     group (gnus-group-group-level) level)
4176       (gnus-group-change-level group level
4177                                (gnus-group-group-level))
4178       (gnus-group-update-group-line)))
4179   (gnus-group-position-cursor))
4180
4181 (defun gnus-group-unsubscribe-current-group (arg)
4182   "Toggle subscribe from/to unsubscribe current group."
4183   (interactive "P")
4184   (let ((group (gnus-group-group-name)))
4185     (or group (error "No newsgroup on current line"))
4186     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
4187                           gnus-level-default-unsubscribed
4188                         gnus-level-default-subscribed)))
4189     (gnus-group-unsubscribe-group group arg)
4190     (gnus-group-next-group 1)))
4191
4192 (defun gnus-group-unsubscribe-group (group &optional level)
4193   "Toggle subscribe from/to unsubscribe GROUP.
4194 New newsgroup is added to .newsrc automatically."
4195   (interactive
4196    (list (completing-read "Group: " gnus-active-hashtb nil 
4197                           gnus-have-read-active-file)))
4198   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4199     (cond (newsrc
4200            ;; Toggle subscription flag.
4201            (gnus-group-change-level 
4202             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4203                                            gnus-level-subscribed) 
4204                                        (1+ gnus-level-subscribed)
4205                                      gnus-level-default-subscribed)))
4206            (gnus-group-update-group group))
4207           ((and (stringp group)
4208                 (or (not gnus-have-read-active-file)
4209                     (gnus-gethash group gnus-active-hashtb)))
4210            ;; Add new newsgroup.
4211            (gnus-group-change-level 
4212             group 
4213             (if level level gnus-level-default-subscribed) 
4214             (or (and (member group gnus-zombie-list) 
4215                      gnus-level-zombie) 
4216                 gnus-level-killed)
4217             (and (gnus-group-group-name)
4218                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4219            (gnus-group-update-group group))
4220           (t (error "No such newsgroup: %s" group)))
4221     (gnus-group-position-cursor)))
4222
4223 (defun gnus-group-transpose-groups (n)
4224   "Move the current newsgroup up N places.
4225 If given a negative prefix, move down instead. The difference between
4226 N and the number of steps taken is retured." 
4227   (interactive "p")
4228   (or (gnus-group-group-name)
4229       (error "No group on current line"))
4230   (gnus-group-kill-group 1)
4231   (prog1
4232       (forward-line (- n))
4233     (gnus-group-yank-group)
4234     (gnus-group-position-cursor)))
4235
4236 (defun gnus-group-kill-all-zombies ()
4237   "Kill all zombie newsgroups."
4238   (interactive)
4239   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4240   (setq gnus-zombie-list nil)
4241   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4242   (goto-char (point-min))
4243   (gnus-group-position-cursor))
4244
4245 (defun gnus-group-kill-region (begin end)
4246   "Kill newsgroups in current region (excluding current point).
4247 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4248   (interactive "r")
4249   (let ((lines
4250          ;; Exclude a line where current point is on.
4251          (1-
4252           ;; Count lines.
4253           (save-excursion
4254             (count-lines
4255              (progn
4256                (goto-char begin)
4257                (beginning-of-line)
4258                (point))
4259              (progn
4260                (goto-char end)
4261                (end-of-line)
4262                (point)))))))
4263     (goto-char begin)
4264     (beginning-of-line)                 ;Important when LINES < 1
4265     (gnus-group-kill-group lines)))
4266
4267 (defun gnus-group-kill-group (n)
4268   "The the next N groups.
4269 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4270 However, only groups that were alive can be yanked; already killed 
4271 groups or zombie groups can't be yanked.
4272 The return value is the name of the (last) group that was killed."
4273   (interactive "P")
4274   (let ((buffer-read-only nil)
4275         (groups (gnus-group-process-prefix n))
4276         group entry level)
4277     (while groups
4278       (setq group (car groups)
4279             groups (cdr groups))
4280       (gnus-group-remove-mark group)
4281       (setq level (gnus-group-group-level))
4282       (gnus-delete-line)
4283       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4284           (setq gnus-list-of-killed-groups 
4285                 (cons (cons (car entry) (nth 2 entry)) 
4286                       gnus-list-of-killed-groups)))
4287       (gnus-group-change-level 
4288        (if entry entry group) gnus-level-killed (if entry nil level)))
4289     (gnus-group-position-cursor)
4290     group))
4291
4292 (defun gnus-group-yank-group (&optional arg)
4293   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4294 inserting it before the current newsgroup.  The numeric ARG specifies
4295 how many newsgroups are to be yanked.  The name of the (last)
4296 newsgroup yanked is returned."
4297   (interactive "p")
4298   (if (not arg) (setq arg 1))
4299   (let (info group prev)
4300     (while (>= (setq arg (1- arg)) 0)
4301       (if (not (setq info (car gnus-list-of-killed-groups)))
4302           (error "No more newsgroups to yank"))
4303       (setq group (nth 2 info))
4304       ;; Find which newsgroup to insert this one before - search
4305       ;; backward until something suitable is found. If there are no
4306       ;; other newsgroups in this buffer, just make this newsgroup the
4307       ;; first newsgroup.
4308       (setq prev (gnus-group-group-name))
4309       (gnus-group-change-level 
4310        info (nth 2 info) gnus-level-killed 
4311        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4312        t)
4313       (gnus-group-insert-group-line-info (nth 1 info))
4314       (setq gnus-list-of-killed-groups 
4315             (cdr gnus-list-of-killed-groups)))
4316     (forward-line -1)
4317     (gnus-group-position-cursor)
4318     group))
4319       
4320 (defun gnus-group-list-all-groups (arg)
4321   "List all newsgroups with level ARG or lower.
4322 Default is gnus-level-unsubscribed, which lists all subscribed and most
4323 unsubscribed groups."
4324   (interactive "P")
4325   (setq arg (or arg gnus-level-unsubscribed))
4326   (gnus-group-list-groups arg t))
4327
4328 (defun gnus-group-list-killed ()
4329   "List all killed newsgroups in the group buffer."
4330   (interactive)
4331   (if (not gnus-killed-list)
4332       (gnus-message 6 "No killed groups")
4333     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4334     (goto-char (point-min)))
4335   (gnus-group-position-cursor))
4336
4337 (defun gnus-group-list-zombies ()
4338   "List all zombie newsgroups in the group buffer."
4339   (interactive)
4340   (if (not gnus-zombie-list)
4341       (gnus-message 6 "No zombie groups")
4342     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4343     (goto-char (point-min)))
4344   (gnus-group-position-cursor))
4345
4346 (defun gnus-group-get-new-news (&optional arg)
4347   "Get newly arrived articles.
4348 If ARG is non-nil, it should be a number between one and nine to
4349 specify which levels you are interested in re-scanning."
4350   (interactive "P")
4351   (run-hooks 'gnus-get-new-news-hook)
4352   (let ((level arg))
4353     (if gnus-group-use-permanent-levels
4354         (progn
4355           (if level
4356               (setq gnus-group-default-list-level level)
4357             (setq level (or gnus-group-default-list-level 
4358                             gnus-level-subscribed)))))
4359     (if (and gnus-read-active-file (not level))
4360         (progn
4361           (gnus-read-active-file)
4362           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4363       (let ((gnus-read-active-file nil))
4364         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4365     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4366                                 gnus-level-subscribed)
4367                             gnus-have-all-newsgroups)))
4368
4369 (defun gnus-group-get-new-news-this-group (n)
4370   "Check for newly arrived news in the current group (and the N-1 next groups).
4371 The difference between N and the number of newsgroup checked is returned.
4372 If N is negative, this group and the N-1 previous groups will be checked."
4373   (interactive "P")
4374   (let* ((groups (gnus-group-process-prefix n))
4375          (ret (if (numberp n) (- n (length groups)) 0))
4376          (w-p (window-start))
4377          group)
4378     (while groups
4379       (setq group (car groups)
4380             groups (cdr groups))
4381       (gnus-group-remove-mark group)
4382       (or (gnus-get-new-news-in-group group)
4383           (progn 
4384             (ding) 
4385             (message "%s error: %s" group (gnus-status-message group))
4386             (sit-for 2))))
4387     ;; !!! I don't know why the buffer scrolls forward when updating
4388     ;; the first line in the group buffer, but it does. So we set the
4389     ;; window start forcibly.
4390 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4391     (forward-line 1)
4392     (gnus-summary-position-cursor)
4393     ret))
4394
4395 (defun gnus-get-new-news-in-group (group)
4396   (and group 
4397        (gnus-activate-newsgroup group)
4398        (progn
4399          (gnus-get-unread-articles-in-group 
4400           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4401           (gnus-gethash group gnus-active-hashtb))
4402          (gnus-group-update-group-line)
4403          t)))
4404
4405 (defun gnus-group-fetch-faq (group)
4406   "Fetch the FAQ for the current group."
4407   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4408   (or group (error "No group name given"))
4409   (let ((file (concat gnus-group-faq-directory group))) 
4410     (if (not (file-exists-p file))
4411         (error "No such file: %s" file)
4412       (find-file file))))
4413   
4414 (defun gnus-group-describe-group (force &optional group)
4415   "Display a description of the current newsgroup."
4416   (interactive "P")
4417   (and force (setq gnus-description-hashtb nil))
4418   (let ((group (or group (gnus-group-group-name)))
4419         desc)
4420     (or group (error "No group name given"))
4421     (and (or gnus-description-hashtb
4422              (setq desc (gnus-group-get-description group))
4423              (gnus-read-descriptions-file))
4424          (message
4425           (or desc (gnus-gethash group gnus-description-hashtb)
4426               "No description available")))))
4427
4428 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4429 (defun gnus-group-describe-all-groups (force)
4430   "Pop up a buffer with descriptons of all newsgroups."
4431   (interactive "P")
4432   (and force (setq gnus-description-hashtb nil))
4433   (if (not (or gnus-description-hashtb
4434                (gnus-read-descriptions-file)))
4435       (error "Couldn't request descriptions file"))
4436   (let ((buffer-read-only nil)
4437         b)
4438     (erase-buffer)
4439     (mapatoms
4440      (lambda (group)
4441        (setq b (point))
4442        (insert (format "      *: %-20s %s\n" (symbol-name group)
4443                        (symbol-value group)))
4444        (add-text-properties 
4445         b (1+ b) (list 'gnus-group group
4446                        'gnus-unread t 'gnus-marked nil
4447                        'gnus-level (1+ gnus-level-subscribed))))
4448      gnus-description-hashtb)
4449     (goto-char (point-min))
4450     (gnus-group-position-cursor)))
4451
4452 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4453 (defun gnus-group-apropos (regexp &optional search-description)
4454   "List all newsgroups that have names that match a regexp."
4455   (interactive "sGnus apropos (regexp): ")
4456   (let ((prev "")
4457         (obuf (current-buffer))
4458         groups des prev)
4459     ;; Go through all newsgroups that are known to Gnus.
4460     (mapatoms 
4461      (lambda (group)
4462        (and (string-match regexp (symbol-name group))
4463             (setq groups (cons (symbol-name group) groups))))
4464      gnus-active-hashtb)
4465     ;; Go through all descriptions that are known to Gnus. 
4466     (if search-description
4467         (mapatoms 
4468          (lambda (group)
4469            (and (string-match regexp (symbol-value group))
4470                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4471                 (setq groups (cons (symbol-name group) groups))))
4472          gnus-description-hashtb))
4473     (if (not groups)
4474         (gnus-message 3 "No groups matched \"%s\"." regexp)
4475       ;; Print out all the groups.
4476       (save-excursion
4477         (pop-to-buffer "*Gnus Help*")
4478         (buffer-disable-undo (current-buffer))
4479         (erase-buffer)
4480         (setq groups (sort groups 'string<))
4481         (while groups
4482           ;; Groups may be entered twice into the list of groups.
4483           (if (not (string= (car groups) prev))
4484               (progn
4485                 (insert (setq prev (car groups)) "\n")
4486                 (if (and gnus-description-hashtb
4487                          (setq des (gnus-gethash (car groups) 
4488                                                  gnus-description-hashtb)))
4489                     (insert "  " des "\n"))))
4490           (setq groups (cdr groups)))
4491         (goto-char (point-min))))
4492     (pop-to-buffer obuf)))
4493
4494 (defun gnus-group-description-apropos (regexp)
4495   "List all newsgroups that have names or desccriptions that match a regexp."
4496   (interactive "sGnus description apropos (regexp): ")
4497   (if (not (or gnus-description-hashtb
4498                (gnus-read-descriptions-file)))
4499       (error "Couldn't request descriptions file"))
4500   (gnus-group-apropos regexp t))
4501
4502 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4503 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4504   "List all groups with unread articles that match REGEXP.
4505 If the prefix LEVEL is non-nil, it should be a number that says which
4506 level to cut off listing groups. 
4507 If ALL, also list groups with no unread articles.
4508 If LOWEST, don't list groups with level lower than LOWEST."
4509   (interactive "P\nsList newsgroups matching: ")
4510   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4511                            all (or lowest 1) regexp)
4512   (goto-char (point-min))
4513   (gnus-group-position-cursor))
4514
4515 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4516   "List all groups that match REGEXP.
4517 If the prefix LEVEL is non-nil, it should be a number that says which
4518 level to cut off listing groups. 
4519 If LOWEST, don't list groups with level lower than LOWEST."
4520   (interactive "P\nsList newsgroups matching: ")
4521   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4522
4523 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4524 (defun gnus-group-save-newsrc ()
4525   "Save the Gnus startup files."
4526   (interactive)
4527   (gnus-save-newsrc-file))
4528
4529 (defun gnus-group-restart (&optional arg)
4530   "Force Gnus to read the .newsrc file."
4531   (interactive "P")
4532   (gnus-save-newsrc-file)
4533   (gnus-setup-news 'force)
4534   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4535
4536 (defun gnus-group-read-init-file ()
4537   "Read the Gnus elisp init file."
4538   (interactive)
4539   (gnus-read-init-file))
4540
4541 (defun gnus-group-check-bogus-groups ()
4542   "Check bogus newsgroups."
4543   (interactive)
4544   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
4545   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4546
4547 (defun gnus-group-edit-global-kill (article &optional group)
4548   "Edit the global kill file.
4549 If GROUP, edit that local kill file instead."
4550   (interactive "P")
4551   (setq gnus-current-kill-article article)
4552   (gnus-kill-file-edit-file group)
4553   (gnus-message 6
4554    (substitute-command-keys
4555     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4556
4557 (defun gnus-group-edit-local-kill (article group)
4558   "Edit a local kill file."
4559   (interactive (list nil (gnus-group-group-name)))
4560   (gnus-group-edit-global-kill article group))
4561
4562 (defun gnus-group-force-update ()
4563   "Update `.newsrc' file."
4564   (interactive)
4565   (gnus-save-newsrc-file))
4566
4567 (defun gnus-group-suspend ()
4568   "Suspend the current Gnus session.
4569 In fact, cleanup buffers except for group mode buffer.
4570 The hook gnus-suspend-gnus-hook is called before actually suspending."
4571   (interactive)
4572   (run-hooks 'gnus-suspend-gnus-hook)
4573   ;; Kill Gnus buffers except for group mode buffer.
4574   (let ((group-buf (get-buffer gnus-group-buffer)))
4575     ;; Do this on a separate list in case the user does a ^G before we finish
4576     (let ((gnus-buffer-list
4577            (delq group-buf (delq gnus-dribble-buffer
4578                                  (append gnus-buffer-list nil)))))
4579       (while gnus-buffer-list
4580         (gnus-kill-buffer (car gnus-buffer-list))
4581         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4582     (if group-buf
4583         (progn
4584           (setq gnus-buffer-list (list group-buf))
4585           (bury-buffer group-buf)
4586           (delete-windows-on group-buf t)))))
4587
4588 (defun gnus-group-clear-dribble ()
4589   "Clear all information from the dribble buffer."
4590   (interactive)
4591   (gnus-dribble-clear))
4592
4593 (defun gnus-group-exit ()
4594   "Quit reading news after updating .newsrc.eld and .newsrc.
4595 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4596   (interactive)
4597   (if (or noninteractive                ;For gnus-batch-kill
4598           (zerop (buffer-size))         ;No news is good news.
4599           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4600           (not gnus-interactive-exit)   ;Without confirmation
4601           gnus-expert-user
4602           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4603       (progn
4604         (run-hooks 'gnus-exit-gnus-hook)
4605         (gnus-save-newsrc-file)
4606         (gnus-close-backends)
4607         (gnus-clear-system))))
4608
4609 (defun gnus-close-backends ()
4610   ;; Send a close request to all backends that support such a request. 
4611   (let ((methods gnus-valid-select-methods)
4612         func)
4613     (while methods
4614       (if (fboundp (setq func (intern (concat (car (car methods))
4615                                               "-request-close"))))
4616           (funcall func))
4617       (setq methods (cdr methods)))))
4618
4619 (defun gnus-group-quit ()
4620   "Quit reading news without updating .newsrc.eld or .newsrc.
4621 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4622   (interactive)
4623   (if (or noninteractive                ;For gnus-batch-kill
4624           (zerop (buffer-size))
4625           (not (gnus-server-opened gnus-select-method))
4626           gnus-expert-user
4627           (not gnus-current-startup-file)
4628           (gnus-yes-or-no-p
4629            (format "Quit reading news without saving %s? "
4630                    (file-name-nondirectory gnus-current-startup-file))))
4631       (progn
4632         (run-hooks 'gnus-exit-gnus-hook)
4633         (gnus-dribble-save)
4634         (gnus-offer-save-summaries)
4635         (gnus-close-backends)
4636         (gnus-clear-system))))
4637
4638 (defun gnus-offer-save-summaries ()
4639   (let ((buffers (buffer-list)))
4640     (save-excursion
4641       (while buffers
4642         (and 
4643          ;; We look for buffers with "Summary" in the name.
4644          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4645          (progn
4646            (set-buffer (car buffers))
4647            ;; We check that this is, indeed, a summary buffer.
4648            (eq 'major-mode 'gnus-summary-mode)) 
4649          ;; We ask the user whether she wants to save the info.
4650          (not (gnus-y-or-n-p
4651                (format "Discard summary buffer %s? " (buffer-name))))
4652          ;; We do it by simply exiting.
4653          (gnus-summary-exit))
4654         (setq buffers (cdr buffers))))))
4655
4656 (defun gnus-group-describe-briefly ()
4657   "Give a one line description of the group mode commands."
4658   (interactive)
4659   (gnus-message 6
4660    (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")))
4661
4662 (defun gnus-group-browse-foreign-server (method)
4663   "Browse a foreign news server.
4664 If called interactively, this function will ask for a select method
4665  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4666 If not, METHOD should be a list where the first element is the method
4667 and the second element is the address."
4668   (interactive
4669    (list (let ((how (completing-read 
4670                      "Which backend: "
4671                      (append gnus-valid-select-methods gnus-server-alist)
4672                      nil t "nntp")))
4673            ;; We either got a backend name or a virtual server name.
4674            ;; If the first, we also need an address.
4675            (if (assoc how gnus-valid-select-methods)
4676                (list (intern how)
4677                      ;; Suggested by mapjph@bath.ac.uk.
4678                      (completing-read 
4679                       "Address: " 
4680                       (mapcar (lambda (server) (list server))
4681                               gnus-secondary-servers)))
4682              ;; We got a server name, so we find the method.
4683              (gnus-server-to-method how)))))
4684   (gnus-browse-foreign-server method))
4685
4686 \f
4687 ;;;
4688 ;;; Browse Server Mode
4689 ;;;
4690
4691 (defvar gnus-browse-mode-hook nil)
4692 (defvar gnus-browse-mode-map nil)
4693 (put 'gnus-browse-mode 'mode-class 'special)
4694
4695 (if gnus-browse-mode-map
4696     nil
4697   (setq gnus-browse-mode-map (make-keymap))
4698   (suppress-keymap gnus-browse-mode-map)
4699   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4700   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4701   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4702   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4703   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4704   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4705   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4706   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4707   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4708   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4709   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4710   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4711   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4712   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4713   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4714   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4715   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4716   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4717   )
4718
4719 (defvar gnus-browse-current-method nil)
4720 (defvar gnus-browse-return-buffer nil)
4721
4722 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4723
4724 (defun gnus-browse-foreign-server (method &optional return-buffer)
4725   (setq gnus-browse-current-method method)
4726   (setq gnus-browse-return-buffer return-buffer)
4727   (let ((gnus-select-method method)
4728         groups group)
4729     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4730     (or (gnus-server-opened method)
4731         (gnus-open-server method)
4732         (error "Unable to contact server: %s" (gnus-status-message method)))
4733     (or (gnus-request-list method)
4734         (error "Couldn't request list: %s" (gnus-status-message method)))
4735     (get-buffer-create gnus-browse-buffer)
4736     (gnus-add-current-to-buffer-list)
4737     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4738     (gnus-configure-windows 'browse)
4739     (buffer-disable-undo (current-buffer))
4740     (let ((buffer-read-only nil))
4741       (erase-buffer))
4742     (gnus-browse-mode)
4743     (setq mode-line-buffer-identification
4744           (format
4745            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4746     (save-excursion
4747       (set-buffer nntp-server-buffer)
4748       (let ((cur (current-buffer)))
4749         (goto-char (point-min))
4750         (or (string= gnus-ignored-newsgroups "")
4751             (delete-matching-lines gnus-ignored-newsgroups))
4752         (while (re-search-forward 
4753                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4754           (goto-char (match-end 1))
4755           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4756                                                      (match-end 1))
4757                                    (max 0 (- (1+ (read cur)) (read cur))))
4758                              groups)))))
4759     (setq groups (sort groups 
4760                        (lambda (l1 l2)
4761                          (string< (car l1) (car l2)))))
4762     (let ((buffer-read-only nil))
4763       (while groups
4764         (setq group (car groups))
4765         (insert 
4766          (format "K%7d: %s\n" (cdr group) (car group)))
4767         (setq groups (cdr groups))))
4768     (switch-to-buffer (current-buffer))
4769     (goto-char (point-min))
4770     (gnus-group-position-cursor)))
4771
4772 (defun gnus-browse-mode ()
4773   "Major mode for browsing a foreign server.
4774
4775 All normal editing commands are switched off.
4776
4777 \\<gnus-browse-mode-map>
4778 The only things you can do in this buffer is
4779
4780 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4781 The group will be inserted into the group buffer upon exit from this
4782 buffer.  
4783
4784 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4785
4786 3) `\\[gnus-browse-exit]' to return to the group buffer."
4787   (interactive)
4788   (kill-all-local-variables)
4789   (if gnus-visual (gnus-browse-make-menu-bar))
4790   (setq mode-line-modified "-- ")
4791   (make-local-variable 'mode-line-format)
4792   (setq mode-line-format (copy-sequence mode-line-format))
4793   (and (equal (nth 3 mode-line-format) "   ")
4794        (setcar (nthcdr 3 mode-line-format) ""))
4795   (setq major-mode 'gnus-browse-mode)
4796   (setq mode-name "Browse Server")
4797   (setq mode-line-process nil)
4798   (use-local-map gnus-browse-mode-map)
4799   (buffer-disable-undo (current-buffer))
4800   (setq truncate-lines t)
4801   (setq buffer-read-only t)
4802   (run-hooks 'gnus-browse-mode-hook))
4803
4804 (defun gnus-browse-read-group (&optional no-article)
4805   "Enter the group at the current line."
4806   (interactive)
4807   (let ((group (gnus-browse-group-name)))
4808     (or (gnus-group-read-ephemeral-group 
4809          group gnus-browse-current-method nil
4810          (cons (current-buffer) 'browse))
4811         (error "Couldn't enter %s" group))))
4812
4813 (defun gnus-browse-select-group ()
4814   "Select the current group."
4815   (interactive)
4816   (gnus-browse-read-group 'no))
4817
4818 (defun gnus-browse-next-group (n)
4819   "Go to the next group."
4820   (interactive "p")
4821   (prog1
4822       (forward-line n)
4823     (gnus-group-position-cursor)))
4824
4825 (defun gnus-browse-prev-group (n)
4826   "Go to the next group."
4827   (interactive "p")
4828   (gnus-browse-next-group (- n)))
4829
4830 (defun gnus-browse-unsubscribe-current-group (arg)
4831   "(Un)subscribe to the next ARG groups."
4832   (interactive "p")
4833   (and (eobp)
4834        (error "No group at current line."))
4835   (let ((ward (if (< arg 0) -1 1))
4836         (arg (abs arg)))
4837     (while (and (> arg 0)
4838                 (not (eobp))
4839                 (gnus-browse-unsubscribe-group)
4840                 (zerop (gnus-browse-next-group ward)))
4841       (setq arg (1- arg)))
4842     (gnus-group-position-cursor)
4843     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4844     arg))
4845
4846 (defun gnus-browse-group-name ()
4847   (save-excursion
4848     (beginning-of-line)
4849     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4850         ()
4851       (gnus-group-prefixed-name 
4852        (buffer-substring (match-beginning 1) (match-end 1))
4853        gnus-browse-current-method))))
4854   
4855 (defun gnus-browse-unsubscribe-group ()
4856   (let ((sub nil)
4857         (buffer-read-only nil)
4858         group)
4859     (save-excursion
4860       (beginning-of-line)
4861       (if (= (following-char) ?K) (setq sub t))
4862       (setq group (gnus-browse-group-name))
4863       (beginning-of-line)
4864       (delete-char 1)
4865       (if sub
4866           (progn
4867             (gnus-group-change-level 
4868              (list t group gnus-level-default-subscribed
4869                    nil nil gnus-browse-current-method) 
4870              gnus-level-default-subscribed gnus-level-killed
4871              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4872              t)
4873             (insert ? ))
4874         (gnus-group-change-level 
4875          group gnus-level-killed gnus-level-default-subscribed)
4876         (insert ?K)))
4877     t))
4878
4879 (defun gnus-browse-exit ()
4880   "Quit browsing and return to the group buffer."
4881   (interactive)
4882   (if (eq major-mode 'gnus-browse-mode)
4883       (kill-buffer (current-buffer)))
4884   (if gnus-browse-return-buffer
4885       (gnus-configure-windows 'server)
4886     (gnus-configure-windows 'group)))
4887
4888 (defun gnus-browse-describe-briefly ()
4889   "Give a one line description of the group mode commands."
4890   (interactive)
4891   (gnus-message 6
4892    (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")))
4893       
4894 \f
4895 ;;;
4896 ;;; Gnus summary mode
4897 ;;;
4898
4899 (defvar gnus-summary-mode-map nil)
4900 (defvar gnus-summary-mark-map nil)
4901 (defvar gnus-summary-mscore-map nil)
4902 (defvar gnus-summary-article-map nil)
4903 (defvar gnus-summary-thread-map nil)
4904 (defvar gnus-summary-goto-map nil)
4905 (defvar gnus-summary-exit-map nil)
4906 (defvar gnus-summary-various-map nil)
4907 (defvar gnus-summary-interest-map nil)
4908 (defvar gnus-summary-sort-map nil)
4909 (defvar gnus-summary-backend-map nil)
4910 (defvar gnus-summary-save-map nil)
4911 (defvar gnus-summary-wash-map nil)
4912 (defvar gnus-summary-help-map nil)
4913
4914 (put 'gnus-summary-mode 'mode-class 'special)
4915
4916 (if gnus-summary-mode-map
4917     nil
4918   (setq gnus-summary-mode-map (make-keymap))
4919   (suppress-keymap gnus-summary-mode-map)
4920
4921   ;; Non-orthogonal keys
4922
4923   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4924   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4925   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4926   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4927   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4928   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4929   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4930   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4931   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4932   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
4933   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
4934   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
4935   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
4936   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
4937   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
4938   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
4939   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
4940   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
4941   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
4942   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
4943   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
4944   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
4945   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
4946   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
4947   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
4948   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
4949   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
4950   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
4951   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
4952   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
4953   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
4954   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
4955   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
4956   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
4957   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
4958   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
4959   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
4960   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
4961   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
4962   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
4963   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
4964   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
4965   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
4966   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
4967   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
4968   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
4969   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
4970   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
4971   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
4972   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
4973   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
4974   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
4975   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
4976   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
4977   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
4978   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
4979   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
4980   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
4981   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
4982   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
4983   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
4984   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
4985   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
4986   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
4987   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
4988   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
4989   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
4990   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
4991   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
4992   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
4993   (define-key gnus-summary-mode-map "V" 'gnus-version)
4994   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
4995   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
4996   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
4997   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
4998   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
4999   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5000   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5001   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5002 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5003   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5004   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5005   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5006 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5007   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5008   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5009   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5010
5011
5012   ;; Sort of orthogonal keymap
5013   (define-prefix-command 'gnus-summary-mark-map)
5014   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5015   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5016   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5017   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5018   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5019   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5020   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5021   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5022   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5023   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5024   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5025   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5026   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5027   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5028   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5029   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5030   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5031   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5032   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5033   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5034   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5035   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5036   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5037   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5038
5039   (define-prefix-command 'gnus-summary-mscore-map)
5040   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5041   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5042   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5043   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5044   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5045
5046   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5047   
5048   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5049   
5050   (define-prefix-command 'gnus-summary-goto-map)
5051   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5052   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5053   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5054   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5055   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5056   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5057   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5058   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5059   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5060   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5061   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5062   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5063   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5064   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5065
5066
5067   (define-prefix-command 'gnus-summary-thread-map)
5068   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5069   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5070   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5071   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5072   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5073   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5074   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5075   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5076   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5077   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5078   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5079   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5080   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5081   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5082
5083   
5084   (define-prefix-command 'gnus-summary-exit-map)
5085   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5086   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5087   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5088   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5089   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5090   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5091   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5092   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5093   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5094   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5095   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5096
5097
5098   (define-prefix-command 'gnus-summary-article-map)
5099   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5100   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5101   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5102   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5103   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5104   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5105   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5106   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5107   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5108   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5109   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5110   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5111   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5112   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5113   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5114   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5115   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5116   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5117
5118
5119   (define-prefix-command 'gnus-summary-wash-map)
5120   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5121   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5122   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5123   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5124   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5125   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5126   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5127   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5128   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5129   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5130   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5131   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5132
5133   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5134   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5135   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5136   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5137   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5138   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5139
5140
5141   (define-prefix-command 'gnus-summary-help-map)
5142   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5143   (define-key gnus-summary-help-map "v" 'gnus-version)
5144   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5145   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5146   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5147   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5148
5149
5150   (define-prefix-command 'gnus-summary-backend-map)
5151   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5152   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5153   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5154   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5155   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5156   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5157   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5158   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5159   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5160
5161
5162   (define-prefix-command 'gnus-summary-save-map)
5163   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5164   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5165   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5166   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5167   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5168   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5169   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5170   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5171
5172   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5173   
5174   (define-prefix-command 'gnus-summary-various-map)
5175   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5176   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5177   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5178   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5179   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5180   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5181   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5182   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5183   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5184   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5185   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5186
5187   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5188
5189   (define-prefix-command 'gnus-summary-sort-map)
5190   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5191   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5192   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5193   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5194   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5195   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5196
5197   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5198   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5199   )
5200
5201
5202 \f
5203
5204 (defun gnus-summary-mode ()
5205   "Major mode for reading articles.
5206
5207 All normal editing commands are switched off.
5208 \\<gnus-summary-mode-map>
5209 Each line in this buffer represents one article.  To read an
5210 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5211 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5212 respectively.
5213
5214 You can also post articles and send mail from this buffer.  To 
5215 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5216 of an article, type `\\[gnus-summary-reply]'.
5217
5218 There are approx. one gazillion commands you can execute in this 
5219 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5220
5221 The following commands are available:
5222
5223 \\{gnus-summary-mode-map}"
5224   (interactive)
5225   (if gnus-visual (gnus-summary-make-menu-bar))
5226   (kill-all-local-variables)
5227   (let ((locals gnus-summary-local-variables))
5228     (while locals
5229       (if (consp (car locals))
5230           (progn
5231             (make-local-variable (car (car locals)))
5232             (set (car (car locals)) (eval (cdr (car locals)))))
5233         (make-local-variable (car locals))
5234         (set (car locals) nil))
5235       (setq locals (cdr locals))))
5236   (gnus-make-thread-indent-array)
5237   (gnus-update-format-specifications)
5238   (setq mode-line-modified "-- ")
5239   (make-local-variable 'mode-line-format)
5240   (setq mode-line-format (copy-sequence mode-line-format))
5241   (and (equal (nth 3 mode-line-format) "   ")
5242        (setcar (nthcdr 3 mode-line-format) ""))
5243   (setq major-mode 'gnus-summary-mode)
5244   (setq mode-name "Summary")
5245   (make-local-variable 'minor-mode-alist)
5246   (use-local-map gnus-summary-mode-map)
5247   (buffer-disable-undo (current-buffer))
5248   (setq buffer-read-only t)             ;Disable modification
5249   (setq truncate-lines t)
5250   (setq selective-display t)
5251   (setq selective-display-ellipses t)   ;Display `...'
5252   (setq buffer-display-table gnus-summary-display-table)
5253   (run-hooks 'gnus-summary-mode-hook))
5254
5255 (defun gnus-summary-make-display-table ()
5256   ;; Change the display table.  Odd characters have a tendency to mess
5257   ;; up nicely formatted displays - we make all possible glyphs
5258   ;; display only a single character.
5259
5260   ;; We start from the standard display table, if any.
5261   (setq gnus-summary-display-table 
5262         (or (copy-sequence standard-display-table)
5263             (make-display-table)))
5264   ;; Nix out all the control chars...
5265   (let ((i 32))
5266     (while (>= (setq i (1- i)) 0)
5267       (aset gnus-summary-display-table i [??])))
5268   ;; ... but not newline, of course.
5269   (aset gnus-summary-display-table ?\n nil)
5270   ;; We nix out any glyphs over 126 that are not set already.  
5271   (let ((i 256))
5272     (while (>= (setq i (1- i)) 127)
5273       ;; Only modify if the entry is nil.
5274       (or (aref gnus-summary-display-table i) 
5275           (aset gnus-summary-display-table i [??])))))
5276
5277 (defun gnus-summary-clear-local-variables ()
5278   (let ((locals gnus-summary-local-variables))
5279     (while locals
5280       (if (consp (car locals))
5281           (and (vectorp (car (car locals)))
5282                (set (car (car locals)) nil))
5283         (and (vectorp (car locals))
5284              (set (car locals) nil)))
5285       (setq locals (cdr locals)))))
5286
5287 (defun gnus-mouse-pick-article (e)
5288   (interactive "e")
5289   (mouse-set-point e)
5290   (gnus-summary-next-page nil t))
5291
5292 (defun gnus-summary-setup-buffer (group)
5293   "Initialize summary buffer."
5294   (let ((buffer (concat "*Summary " group "*")))
5295     (if (get-buffer buffer)
5296         (progn
5297           (set-buffer buffer)
5298           (not gnus-newsgroup-threads))
5299       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5300       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5301       (gnus-add-current-to-buffer-list)
5302       (gnus-summary-mode)
5303       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5304       (setq gnus-newsgroup-name group)
5305       t)))
5306
5307 (defun gnus-set-global-variables ()
5308   ;; Set the global equivalents of the summary buffer-local variables
5309   ;; to the latest values they had. These reflect the summary buffer
5310   ;; that was in action when the last article was fetched.
5311   (if (eq major-mode 'gnus-summary-mode) 
5312       (progn
5313         (setq gnus-summary-buffer (current-buffer))
5314         (let ((name gnus-newsgroup-name)
5315               (marked gnus-newsgroup-marked)
5316               (unread gnus-newsgroup-unreads)
5317               (headers gnus-current-headers)
5318               (score-file gnus-current-score-file))
5319           (save-excursion
5320             (set-buffer gnus-group-buffer)
5321             (setq gnus-newsgroup-name name)
5322             (setq gnus-newsgroup-marked marked)
5323             (setq gnus-newsgroup-unreads unread)
5324             (setq gnus-current-headers headers)
5325             (setq gnus-current-score-file score-file))))))
5326
5327 (defun gnus-summary-insert-dummy-line (sformat subject number)
5328   (if (not sformat) 
5329       (setq sformat gnus-summary-dummy-line-format-spec))
5330   (let (b)
5331     (beginning-of-line)
5332     (setq b (point))
5333     (insert (eval sformat))
5334     (add-text-properties
5335      b (1+ b)
5336      (list 'gnus-number number 
5337            'gnus-mark gnus-dummy-mark
5338            'gnus-level 0))))
5339
5340 (defvar gnus-thread-indent-array nil)
5341 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5342 (defun gnus-make-thread-indent-array ()
5343   (let ((n 200))
5344     (if (and gnus-thread-indent-array
5345              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5346         nil
5347       (setq gnus-thread-indent-array (make-vector 201 "")
5348             gnus-thread-indent-array-level gnus-thread-indent-level)
5349       (while (>= n 0)
5350         (aset gnus-thread-indent-array n
5351               (make-string (* n gnus-thread-indent-level) ? ))
5352         (setq n (1- n))))))
5353
5354 (defun gnus-summary-insert-line 
5355   (sformat header level current unread replied expirable subject-or-nil
5356            &optional dummy score)
5357   (or sformat (setq sformat gnus-summary-line-format-spec))
5358   (let* ((indentation (aref gnus-thread-indent-array level))
5359          (lines (header-lines header))
5360          (score (or score gnus-summary-default-score 0))
5361          (score-char (if (or (null gnus-summary-default-score)
5362                              (= score gnus-summary-default-score)) ? 
5363                        (if (< score gnus-summary-default-score) 
5364                            gnus-score-below-mark gnus-score-over-mark)))
5365          (replied (if replied gnus-replied-mark ? ))
5366          (from (header-from header))
5367          (name-address (funcall gnus-extract-address-components from))
5368          (address (car (cdr name-address)))
5369          (name (or (car name-address) (car (cdr name-address))))
5370          (subject (header-subject header))
5371          (number (header-number header))
5372          (opening-bracket (if dummy ?\< ?\[))
5373          (closing-bracket (if dummy ?\> ?\]))
5374          (buffer-read-only nil)
5375          (b (progn (beginning-of-line) (point))))
5376     (or (numberp lines) (setq lines 0))
5377     (insert (eval sformat))
5378     (add-text-properties
5379      b (1+ b) (list 'gnus-number number 
5380                     'gnus-mark (or unread gnus-unread-mark)
5381                     'gnus-level level))))
5382
5383 (defun gnus-summary-update-line (&optional dont-update)
5384   ;; Update summary line after change.
5385   (or (not gnus-summary-default-score)
5386       gnus-summary-inhibit-highlight
5387       (let ((gnus-summary-inhibit-highlight t)
5388             (article (gnus-summary-article-number)))
5389         (progn
5390           (or dont-update
5391               (if (and gnus-summary-mark-below
5392                        (< (gnus-summary-article-score)
5393                           gnus-summary-mark-below))
5394                   (and (not (memq article gnus-newsgroup-marked))
5395                        (not (memq article gnus-newsgroup-dormant))
5396                        (memq article gnus-newsgroup-unreads)
5397                        (gnus-summary-mark-article nil gnus-low-score-mark))
5398                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5399                      (gnus-summary-mark-article nil gnus-unread-mark))))
5400           (and gnus-visual
5401                (run-hooks 'gnus-summary-update-hook))))))
5402
5403 (defun gnus-summary-update-lines (&optional beg end)
5404   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5405   (let ((beg (or beg (point-min)))
5406         (end (or end (point-max))))
5407     (save-excursion
5408       (set-buffer gnus-summary-buffer)
5409       (goto-char beg)
5410       (while (and (not (eobp)) (< (point) end))
5411         (gnus-summary-update-line)
5412         (forward-line 1)))))
5413
5414 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5415   ;; Sum up all elements (and sub-elements) in a list.
5416   (let ((number 
5417          (if (listp thread) 
5418              (apply 
5419               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5420            1)))
5421     (if char 
5422         (if (> number 1) gnus-not-empty-thread-mark
5423           gnus-empty-thread-mark)
5424       number)))
5425
5426 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5427   "Start reading news in newsgroup GROUP.
5428 If SHOW-ALL is non-nil, already read articles are also listed.
5429 If NO-ARTICLE is non-nil, no article is selected initially."
5430   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5431   (let* ((new-group (gnus-summary-setup-buffer group))
5432          (did-select (and new-group (gnus-select-newsgroup group show-all)))
5433          (method (car (gnus-find-method-for-group group))))
5434     (cond 
5435      ((not new-group)
5436       (gnus-set-global-variables)
5437       (gnus-kill-buffer kill-buffer)
5438       (gnus-configure-windows 'summary)
5439       (gnus-set-mode-line 'summary)
5440       (gnus-summary-position-cursor)
5441       (message "")
5442       t)
5443      ((null did-select) 
5444       (and (eq major-mode 'gnus-summary-mode)
5445            (not (equal (current-buffer) kill-buffer))
5446            (progn
5447              (kill-buffer (current-buffer))
5448              (set-buffer gnus-group-buffer)
5449              (gnus-group-next-unread-group 1)))
5450       (message "Can't select group")
5451       nil)
5452      ((eq did-select 'quit)
5453       (and (eq major-mode 'gnus-summary-mode)
5454            (not (equal (current-buffer) kill-buffer))
5455            (kill-buffer (current-buffer)))
5456       (gnus-kill-buffer kill-buffer)
5457       (gnus-configure-windows 'group)
5458       (gnus-group-next-unread-group 1)
5459       (signal 'quit nil))
5460      (t
5461       (gnus-set-global-variables)
5462       ;; Save the active value in effect when the group was entered.
5463       (setq gnus-newsgroup-active 
5464             (gnus-copy-sequence
5465              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5466       ;; You can change the subjects in this hook.
5467       (run-hooks 'gnus-select-group-hook)
5468       ;; Do score processing.
5469       (and gnus-use-scoring (gnus-possibly-score-headers))
5470       ;; Update the format specifiers.
5471       (gnus-update-format-specifications)
5472       ;; Generate the summary buffer.
5473       (gnus-summary-prepare)
5474       (if (zerop (buffer-size))
5475           (cond (gnus-newsgroup-dormant
5476                  (gnus-summary-show-all-dormant))
5477                 ((and gnus-newsgroup-scored show-all)
5478                  (gnus-summary-show-all-expunged))))
5479       ;; Function `gnus-apply-kill-file' must be called in this hook.
5480       (run-hooks 'gnus-apply-kill-hook)
5481       (if (zerop (buffer-size))
5482           (progn
5483             ;; This newsgroup is empty.
5484             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5485             (gnus-message 6 "No unread news")
5486             (gnus-kill-buffer kill-buffer)
5487             nil)
5488         (save-excursion
5489           (if kill-buffer
5490               (let ((gnus-summary-buffer kill-buffer))
5491                 (gnus-configure-windows 'group))))
5492         ;; Hide conversation thread subtrees.  We cannot do this in
5493         ;; gnus-summary-prepare-hook since kill processing may not
5494         ;; work with hidden articles.
5495         (and gnus-show-threads
5496              gnus-thread-hide-subtree
5497              (gnus-summary-hide-all-threads))
5498         ;; Show first unread article if requested.
5499         (goto-char (point-min))
5500         (if (and (not no-article)
5501                  gnus-auto-select-first
5502                  (gnus-summary-first-unread-article))
5503             (gnus-configure-windows 'article)
5504           (gnus-configure-windows 'summary))
5505         (gnus-set-mode-line 'summary)
5506         (gnus-summary-position-cursor)
5507         ;; If in async mode, we send some info to the backend.
5508         (and gnus-newsgroup-async
5509              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5510              (gnus-request-asynchronous 
5511               gnus-newsgroup-name
5512               (if (and gnus-asynchronous-article-function
5513                        (fboundp gnus-asynchronous-article-function))
5514                   (funcall gnus-asynchronous-article-function
5515                            gnus-newsgroup-threads)
5516                 gnus-newsgroup-threads)))
5517         (gnus-kill-buffer kill-buffer))
5518       t))))
5519
5520 (defun gnus-summary-prepare ()
5521   ;; Generate the summary buffer.
5522   (let ((buffer-read-only nil))
5523     (erase-buffer)
5524     (gnus-summary-prepare-threads 
5525      (if gnus-show-threads
5526          (gnus-gather-threads 
5527           (gnus-sort-threads 
5528            (if (and gnus-summary-expunge-below
5529                     (not gnus-fetch-old-headers))
5530                (gnus-make-threads-and-expunge)
5531              (gnus-make-threads))))
5532        gnus-newsgroup-headers)
5533      0 nil nil t)
5534     ;; Erase header retrieval message.
5535     (gnus-summary-update-lines)
5536     (message "")
5537     ;; Remove the final newline.
5538     ;;(goto-char (point-max))
5539     ;;(delete-char -1)
5540     ;; Call hooks for modifying summary buffer.
5541     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5542     (goto-char (point-min))
5543     (run-hooks 'gnus-summary-prepare-hook)))
5544
5545 (defun gnus-subject-equal (s1 s2)
5546   (cond 
5547    ((numberp gnus-summary-gather-subject-limit)
5548     (string= (if (> (length s1) gnus-summary-gather-subject-limit)
5549                  (substring s1 0 gnus-summary-gather-subject-limit)
5550                s1)
5551              (if (> (length s2) gnus-summary-gather-subject-limit)
5552                  (substring s2 0 gnus-summary-gather-subject-limit)
5553                s2)))
5554    ((eq 'fuzzy gnus-summary-gather-subject-limit)
5555     (string= (gnus-simplify-subject-fuzzy s1)
5556              (gnus-simplify-subject-fuzzy s2)))
5557    (t
5558     (string= s1 s2))))
5559
5560 (defun gnus-gather-threads (threads)
5561   "Gather threads that have lost their roots."
5562   (if (not gnus-summary-make-false-root)
5563       threads 
5564     (let ((hashtb (gnus-make-hashtable 1023))
5565           (prev threads)
5566           (result threads)
5567           thread subject hthread whole-subject)
5568       (while threads
5569         (setq whole-subject 
5570               (setq subject (header-subject (car (car threads)))))
5571         (if gnus-summary-gather-subject-limit
5572             (or (and (numberp gnus-summary-gather-subject-limit)
5573                      (> (length subject) gnus-summary-gather-subject-limit)
5574                      (setq subject
5575                            (substring subject 0 
5576                                       gnus-summary-gather-subject-limit)))
5577                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5578                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5579           (setq subject (gnus-simplify-subject-re subject)))
5580         (if (setq hthread 
5581                   (gnus-gethash subject hashtb))
5582             (progn
5583               (or (stringp (car (car hthread)))
5584                   (setcar hthread (list whole-subject (car hthread))))
5585               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5586                                            (list (car threads))))
5587               (setcdr prev (cdr threads))
5588               (setq threads prev))
5589           (gnus-sethash subject threads hashtb))
5590         (setq prev threads)
5591         (setq threads (cdr threads)))
5592       result)))
5593
5594 (defun gnus-make-threads ()
5595   ;; This function takes the dependencies already made by 
5596   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5597   ;; through the dependecies in the hash table and finds all the
5598   ;; roots. Roots do not refer back to any valid articles.
5599   (let (roots)
5600     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5601          (gnus-build-old-threads))
5602     (mapatoms
5603      (lambda (refs)
5604        (if (not (car (symbol-value refs)))
5605            (setq roots (append (cdr (symbol-value refs)) roots))
5606          ;; Ok, these refer back to valid articles, but if
5607          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5608          ;; the root has the same subject as its children. The children
5609          ;; that do not are made into roots and removed from the list
5610          ;; of children. 
5611          (or gnus-thread-ignore-subject
5612              (let* ((prev (symbol-value refs))
5613                     (subject (gnus-simplify-subject-re 
5614                               (header-subject (car prev))))
5615                     (headers (cdr prev)))
5616                (while headers
5617                  (if (not (string= subject
5618                                    (gnus-simplify-subject-re 
5619                                     (header-subject (car headers)))))
5620                      (progn
5621                        (setq roots (cons (car headers) roots))
5622                        (setcdr prev (cdr headers)))
5623                    (setq prev headers))
5624                  (setq headers (cdr headers)))))))
5625      gnus-newsgroup-dependencies)
5626     
5627     (mapcar 'gnus-trim-thread
5628             (apply 'append
5629                    (mapcar 'gnus-cut-thread
5630                            (mapcar 'gnus-make-sub-thread roots))))))
5631   
5632 (defun gnus-make-threads-and-expunge ()
5633   ;; This function takes the dependencies already made by 
5634   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5635   ;; through the dependecies in the hash table and finds all the
5636   ;; roots. Roots do not refer back to any valid articles.
5637   (let ((default (or gnus-summary-default-score 0))
5638         (below gnus-summary-expunge-below)
5639         roots article)
5640     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5641          (gnus-build-old-threads))
5642     (mapatoms
5643      (lambda (refs)
5644        (if (not (car (symbol-value refs)))
5645            ;; These articles do not refer back to any other articles -
5646            ;; they are roots.
5647            (let ((headers (cdr (symbol-value refs))))
5648              ;; We weed out the low-scored articles.
5649              (while headers
5650                (if (not (< (or (cdr (assq (header-number (car headers))
5651                                           gnus-newsgroup-scored)) default)
5652                            below))
5653                    ;; It is over.
5654                    (setq roots (cons (car headers) roots))
5655                  ;; It is below, so we mark it as read.
5656                  (setq gnus-newsgroup-unreads
5657                        (delq (header-number (car headers))
5658                              gnus-newsgroup-unreads)))
5659                (setq headers (cdr headers))))
5660          ;; Ok, these refer back to valid articles, but if
5661          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5662          ;; the root has the same subject as its children. The children
5663          ;; that do not are made into roots and removed from the list
5664          ;; of children. 
5665          (or gnus-thread-ignore-subject
5666              (let* ((prev (symbol-value refs))
5667                     (subject (gnus-simplify-subject-re 
5668                               (header-subject (car prev))))
5669                     (headers (cdr prev)))
5670                (while headers
5671                  (if (not (string= subject
5672                                    (gnus-simplify-subject-re 
5673                                     (header-subject (car headers)))))
5674                      (progn
5675                        (if (not (< (or (cdr (assq (header-number (car headers))
5676                                                   gnus-newsgroup-scored))
5677                                        default) below))
5678                            (setq roots (cons (car headers) roots))
5679                          (setq gnus-newsgroup-unreads
5680                                (delq (header-number (car headers))
5681                                      gnus-newsgroup-unreads)))
5682                        (setcdr prev (cdr headers)))
5683                    (setq prev headers))
5684                  (setq headers (cdr headers)))))
5685          ;; If this article is expunged, some of the children might be
5686          ;; roots.  
5687          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5688                                gnus-newsgroup-scored)) default)
5689                 below)
5690              (let* ((prev (symbol-value refs))
5691                     (headers (cdr prev)))
5692                (while headers
5693                  (setq article (header-number (car headers)))
5694                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5695                                  default) below))
5696                      (progn (setq roots (cons (car headers) roots))
5697                             (setq prev headers))
5698                    (setq gnus-newsgroup-unreads 
5699                          (delq article gnus-newsgroup-unreads))
5700                    (setcdr prev (cdr headers)))
5701                  (setq headers (cdr headers))))
5702            ;; It was not expunged, but we look at expunged children.
5703            (let* ((prev (symbol-value refs))
5704                   (headers (cdr prev))
5705                   article id)
5706              (while headers
5707                (setq article (header-number (car headers)))
5708                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5709                                default) below))
5710                    (setq prev headers)
5711                  (setq gnus-newsgroup-unreads 
5712                        (delq article gnus-newsgroup-unreads))
5713                  (setcdr prev (cdr headers)))
5714                (setq headers (cdr headers)))))))
5715      gnus-newsgroup-dependencies)
5716
5717     (mapcar 'gnus-trim-thread
5718             (apply 'append
5719                    (mapcar 'gnus-cut-thread
5720                            (mapcar 'gnus-make-sub-thread roots))))))
5721   
5722 (defun gnus-cut-thread (thread)
5723   ;; Remove leaf dormant or ancient articles from THREAD.
5724   (let ((head (car thread))
5725         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5726     (if (and (null tail)
5727              (let ((number (header-number head)))
5728                (or (memq number gnus-newsgroup-ancient)
5729                    (memq number gnus-newsgroup-dormant)
5730                    (and gnus-summary-expunge-below
5731                         (eq gnus-fetch-old-headers 'some)
5732                         (< (or (cdr (assq number gnus-newsgroup-scored))
5733                                gnus-summary-default-score 0)
5734                            gnus-summary-expunge-below)
5735                         (progn
5736                           (setq gnus-newsgroup-unreads
5737                                 (delq number gnus-newsgroup-unreads))
5738                           t)))))
5739         nil
5740       (list (cons head tail)))))
5741
5742 (defun gnus-trim-thread (thread)
5743   ;; Remove root ancient articles with only one child from THREAD.
5744   (if (and (eq gnus-fetch-old-headers 'some)
5745            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5746            (= (length thread) 2))
5747       (gnus-trim-thread (nth 1 thread))
5748     thread))
5749
5750 (defun gnus-make-sub-thread (root)
5751   ;; This function makes a sub-tree for a node in the tree.
5752   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5753                                               gnus-newsgroup-dependencies)))))
5754     (cons root (mapcar 'gnus-make-sub-thread children))))
5755
5756 (defun gnus-build-old-threads ()
5757   ;; Look at all the articles that refer back to old articles, and
5758   ;; fetch the headers for the articles that aren't there. This will
5759   ;; build complete threads - if the roots haven't been expired by the
5760   ;; server, that is.
5761   (let (id heads)
5762     (mapatoms
5763      (lambda (refs)
5764        (if (not (car (symbol-value refs)))
5765            (progn
5766              (setq heads (cdr (symbol-value refs)))
5767              (while heads
5768                (if (not (memq (header-number (car heads))
5769                               gnus-newsgroup-dormant))
5770                    (progn
5771                      (setq id (symbol-name refs))
5772                      (while (and (setq id (gnus-build-get-header id))
5773                                  (not (car (gnus-gethash 
5774                                             id gnus-newsgroup-dependencies)))))
5775                      (setq heads nil))
5776                  (setq heads (cdr heads)))))))
5777      gnus-newsgroup-dependencies)))
5778
5779 (defun gnus-build-get-header (id)
5780   ;; Look through the buffer of NOV lines and find the header to
5781   ;; ID. Enter this line into the dependencies hash table, and return
5782   ;; the id of the parent article (if any).
5783   (let ((deps gnus-newsgroup-dependencies)
5784         found header)
5785     (prog1
5786         (save-excursion
5787           (set-buffer nntp-server-buffer)
5788           (goto-char (point-min))
5789           (while (and (not found) (search-forward id nil t))
5790             (beginning-of-line)
5791             (setq found (looking-at 
5792                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5793                                  (regexp-quote id))))
5794             (or found (beginning-of-line 2)))
5795           (if found
5796               (let (ref)
5797                 (beginning-of-line)
5798                 (and
5799                  (setq header (gnus-nov-parse-line 
5800                                (read (current-buffer)) deps))
5801                  (setq ref (header-references header))
5802                  (string-match "\\(<[^>]+>\\) *$" ref)
5803                  (substring ref (match-beginning 1) (match-end 1))))))
5804       (and header
5805            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5806                  gnus-newsgroup-ancient (cons (header-number header)
5807                                               gnus-newsgroup-ancient))))))
5808
5809 ;; Re-build the thread containing ID.
5810 (defun gnus-rebuild-thread (id)
5811   (let ((dep gnus-newsgroup-dependencies)
5812         (buffer-read-only nil)
5813         parent headers refs thread art)
5814     (while (and id (setq headers
5815                          (car (setq art (gnus-gethash (downcase id) dep)))))
5816       (setq parent art)
5817       (setq id (and (setq refs (header-references headers))
5818                     (string-match "\\(<[^>]+>\\) *$" refs)
5819                     (substring refs (match-beginning 1) (match-end 1)))))
5820     (setq thread (gnus-make-sub-thread (car parent)))
5821     (gnus-rebuild-remove-articles thread)
5822     (let ((beg (point)))
5823       (gnus-summary-prepare-threads (list thread) 0)
5824       (gnus-summary-update-lines beg (point)))))
5825
5826 ;; Delete all lines in the summary buffer that correspond to articles
5827 ;; in this thread.
5828 (defun gnus-rebuild-remove-articles (thread)
5829   (and (gnus-summary-goto-subject (header-number (car thread)))
5830        (gnus-delete-line))
5831   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5832
5833 (defun gnus-sort-threads (threads)
5834   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5835   (let ((fun gnus-thread-sort-functions))
5836     (while fun
5837       (setq threads (sort threads (car fun))
5838             fun (cdr fun))))
5839   threads)
5840
5841 (defun gnus-thread-header (thread)
5842   ;; Return header of first article in THREAD.
5843   (if (consp thread)
5844       (if (stringp (car thread))
5845           (car (car (cdr thread)))
5846         (car thread))
5847     thread))
5848
5849 (defun gnus-thread-sort-by-number (h1 h2)
5850   "Sort threads by root article number."
5851   (let ((h1 (gnus-thread-header h1))
5852         (h2 (gnus-thread-header h2)))
5853     (< (header-number h1) (header-number h2))))
5854
5855 (defun gnus-thread-sort-by-author (h1 h2)
5856   "Sort threads by root author."
5857   (let ((h1 (gnus-thread-header h1))
5858         (h2 (gnus-thread-header h2)))
5859     (string-lessp
5860      (let ((extract (funcall 
5861                      gnus-extract-address-components (header-from h1))))
5862        (or (car extract) (cdr extract)))
5863      (let ((extract (funcall
5864                      gnus-extract-address-components (header-from h2))))
5865        (or (car extract) (cdr extract))))))
5866
5867 (defun gnus-thread-sort-by-subject (h1 h2)
5868   "Sort threads by root subject."
5869   (let ((h1 (gnus-thread-header h1))
5870         (h2 (gnus-thread-header h2)))
5871     (string-lessp
5872      (downcase (gnus-simplify-subject (header-subject h1)))
5873      (downcase (gnus-simplify-subject (header-subject h2))))))
5874
5875 (defun gnus-thread-sort-by-date (h1 h2)
5876   "Sort threads by root article date."
5877   (let ((h1 (gnus-thread-header h1))
5878         (h2 (gnus-thread-header h2)))
5879     (string-lessp
5880      (gnus-sortable-date (header-date h1))
5881      (gnus-sortable-date (header-date h2)))))
5882
5883 (defun gnus-thread-sort-by-score (h1 h2)
5884   "Sort threads by root article score.
5885 Unscored articles will be counted as havin a score of zero."
5886   (let ((h1 (gnus-thread-header h1))
5887         (h2 (gnus-thread-header h2)))
5888     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5889           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5890       (> (or (cdr s1) gnus-summary-default-score 0)
5891          (or (cdr s2) gnus-summary-default-score 0)))))
5892
5893 (defun gnus-thread-sort-by-total-score (h1 h2)
5894   "Sort threads by the sum of all scores in the thread.
5895 Unscored articles will be counted as havin a score of zero."
5896   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5897
5898 (defun gnus-thread-total-score (thread)
5899   ;;  This function find the total score of THREAD.
5900   (if (consp thread)
5901       (if (stringp (car thread))
5902           (apply gnus-thread-score-function 0
5903                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5904         (gnus-thread-total-score-1 thread))
5905     (gnus-thread-total-score-1 (list thread))))
5906
5907 (defun gnus-thread-total-score-1 (root)
5908   ;; This function find the total score of the thread below ROOT.
5909   (setq root (car root))
5910   (apply gnus-thread-score-function
5911          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5912              gnus-summary-default-score 0)
5913          (mapcar 'gnus-thread-total-score
5914                  (cdr (gnus-gethash (downcase (header-id root))
5915                                     gnus-newsgroup-dependencies)))))
5916
5917 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5918 (defvar gnus-tmp-prev-subject "")
5919 (defvar gnus-tmp-adopt-thread nil)
5920
5921 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
5922 (defun gnus-summary-prepare-threads 
5923   (threads level &optional not-child no-subject cull)
5924   "Prepare summary buffer from THREADS and indentation LEVEL.  
5925 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
5926 or a straight list of headers."
5927   (let (thread header number subject clevel)
5928     (while threads
5929       (setq thread (car threads)
5930             threads (cdr threads))
5931       ;; If `thread' is a cons, hierarchical threads are used.  If not,
5932       ;; `thread' is the header.
5933       (if (consp thread)
5934           (setq header (car thread))
5935         (setq header thread)
5936         (and cull
5937              (or (memq (setq number (header-number header))
5938                        gnus-newsgroup-dormant)
5939                  (and gnus-summary-expunge-below
5940                       (< (or (cdr (assq number gnus-newsgroup-scored))
5941                              gnus-summary-default-score 0)
5942                          gnus-summary-expunge-below)))
5943              (progn
5944                (setq header nil)
5945                (setq gnus-newsgroup-unreads 
5946                      (delq number gnus-newsgroup-unreads)))))
5947       (cond 
5948        ((stringp header)
5949         ;; The header is a dummy root.
5950         (cond ((eq gnus-summary-make-false-root 'adopt)
5951                ;; We let the first article adopt the rest.
5952                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
5953                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
5954                (setq thread (cdr (cdr thread)))
5955                (while thread
5956                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
5957                  (setq thread (cdr thread))))
5958               ((eq gnus-summary-make-false-root 'dummy)
5959                ;; We output a dummy root.
5960                (gnus-summary-insert-dummy-line 
5961                 nil header (header-number (car (car (cdr thread)))))
5962                (setq clevel 1))
5963               ((eq gnus-summary-make-false-root 'empty)
5964                ;; We print the articles with empty subject fields. 
5965                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
5966                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
5967                (setq thread (cdr (cdr thread)))
5968                (while thread
5969                  (gnus-summary-prepare-threads 
5970                   (list (car thread)) 0 nil
5971                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
5972                             (not (string=  
5973                                   (gnus-simplify-subject-re 
5974                                    (header-subject (car (car thread))))
5975                                   (gnus-simplify-subject-re header))))))
5976                  (setq thread (cdr thread))))
5977               (t
5978                ;; We do not make a root for the gathered
5979                ;; sub-threads at all.  
5980                (setq clevel 0)))
5981         ;; Print the sub-threads.
5982         (and (consp thread) (cdr thread)
5983              (gnus-summary-prepare-threads (cdr thread) clevel)))
5984        ;; The header is a real article.
5985        (header
5986         (setq number (header-number header)
5987               subject (header-subject header))
5988         (and gnus-newsgroup-async
5989              (setq gnus-newsgroup-threads
5990                    (cons (cons (header-number header)
5991                                (header-lines header)) gnus-newsgroup-threads)))
5992         (gnus-summary-insert-line
5993          nil header level nil 
5994          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
5995                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
5996                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
5997                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
5998                (t gnus-ancient-mark))
5999          (memq number gnus-newsgroup-replied)
6000          (memq number gnus-newsgroup-expirable)
6001          (if no-subject 
6002              gnus-summary-same-subject
6003            (if (or (zerop level)
6004                    (and gnus-thread-ignore-subject
6005                         (not (string= 
6006                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6007                               (gnus-simplify-subject-re subject)))))
6008                subject
6009              gnus-summary-same-subject))
6010          not-child
6011          (cdr (assq number gnus-newsgroup-scored)))
6012         (setq gnus-tmp-prev-subject subject)
6013         ;; Recursively print subthreads.
6014         (and (consp thread) (cdr thread)
6015              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6016
6017 (defun gnus-select-newsgroup (group &optional read-all)
6018   "Select newsgroup GROUP.
6019 If READ-ALL is non-nil, all articles in the group are selected."
6020   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6021          (info (nth 2 entry))
6022          articles header-marks)
6023     (gnus-check-news-server
6024      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6025
6026     (or (gnus-server-opened gnus-current-select-method)
6027         (gnus-open-server gnus-current-select-method)
6028         (error "Couldn't open server"))
6029     
6030     (or (and (eq (car entry) t)
6031              (gnus-activate-newsgroup (car info)))
6032         (gnus-request-group group t)
6033         (progn
6034           (kill-buffer (current-buffer))
6035           (error "Couldn't request group %s: %s" 
6036                  group (gnus-status-message group))))
6037
6038     (setq gnus-newsgroup-name group)
6039     (setq gnus-newsgroup-unselected nil)
6040     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6041
6042     (and gnus-asynchronous
6043          (gnus-check-backend-function 
6044           'request-asynchronous gnus-newsgroup-name)
6045          (setq gnus-newsgroup-async
6046                (gnus-request-asynchronous gnus-newsgroup-name)))
6047
6048     (setq articles (gnus-articles-to-read group read-all))
6049
6050     (cond 
6051      ((null articles) 
6052       (gnus-message 3 "Couldn't select newsgroup")
6053       'quit)
6054      ((eq articles 0) nil)
6055      (t
6056       ;; Init the dependencies hash table.
6057       (setq gnus-newsgroup-dependencies 
6058             (gnus-make-hashtable (length articles)))
6059       ;; Retrieve the headers and read them in.
6060       (setq gnus-newsgroup-headers 
6061             (if (eq 'nov (setq gnus-headers-retrieved-by
6062                                (gnus-retrieve-headers 
6063                                 (if gnus-fetch-old-headers 
6064                                     (cons 1 articles) articles) 
6065                                 gnus-newsgroup-name)))
6066                 (progn
6067                   (gnus-get-newsgroup-headers-xover articles))
6068               (gnus-get-newsgroup-headers)))
6069       ;; If we were to fetch old headers, but the backend didn't
6070       ;; support XOVER, then it is possible we fetched one article
6071       ;; that we shouldn't have. If that's the case, we pop it off the
6072       ;; list of headers.
6073       (and (not (eq gnus-headers-retrieved-by 'nov))
6074            gnus-fetch-old-headers
6075            gnus-newsgroup-headers
6076            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
6077            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
6078       ;; Remove cancelled articles from the list of unread articles.
6079       (setq gnus-newsgroup-unreads
6080             (gnus-set-sorted-intersection 
6081              gnus-newsgroup-unreads
6082              (mapcar (lambda (headers) (header-number headers))
6083                      gnus-newsgroup-headers)))
6084       ;; Adjust and set lists of article marks.
6085       (and info
6086            (let (marked)
6087              (gnus-adjust-marked-articles info)
6088              (setq gnus-newsgroup-marked 
6089                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6090              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6091              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6092              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6093              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6094              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6095              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6096              (setq gnus-newsgroup-processable nil)))
6097       ;; Check whether auto-expire is to be done in this group.
6098       (setq gnus-newsgroup-auto-expire
6099             (or (and (stringp gnus-auto-expirable-newsgroups)
6100                      (string-match gnus-auto-expirable-newsgroups group))
6101                 (memq 'auto-expire (nth 5 info))))
6102       ;; First and last article in this newsgroup.
6103       (and gnus-newsgroup-headers
6104            (setq gnus-newsgroup-begin 
6105                  (header-number (car gnus-newsgroup-headers)))
6106            (setq gnus-newsgroup-end
6107                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6108       (setq gnus-reffed-article-number -1)
6109       ;; GROUP is successfully selected.
6110       (or gnus-newsgroup-headers t)))))
6111
6112 (defun gnus-articles-to-read (group read-all)
6113   ;; Find out what articles the user wants to read.
6114   (let* ((articles
6115           ;; Select all articles if `read-all' is non-nil, or if all the
6116           ;; unread articles are dormant articles.
6117           (if (or (and read-all (not (numberp read-all)))
6118                   (= (length gnus-newsgroup-unreads) 
6119                      (length gnus-newsgroup-dormant)))
6120               (gnus-uncompress-range 
6121                (gnus-gethash group gnus-active-hashtb))
6122             gnus-newsgroup-unreads))
6123          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6124          (scored (length scored-list))
6125          (number (length articles))
6126          (marked (+ (length gnus-newsgroup-marked)
6127                     (length gnus-newsgroup-dormant)))
6128          (select
6129           (cond 
6130            ((numberp read-all)
6131             read-all)
6132            (t
6133             (condition-case ()
6134                 (cond ((and (or (<= scored marked)
6135                                 (= scored number))
6136                             (numberp gnus-large-newsgroup)
6137                             (> number gnus-large-newsgroup))
6138                        (let ((input
6139                               (read-string
6140                                (format
6141                                 "How many articles from %s (default %d): "
6142                                 gnus-newsgroup-name number))))
6143                          (if (string-equal input "")
6144                              number input)))
6145                       ((and (> scored marked) (< scored number))
6146                        (let ((input
6147                               (read-string
6148                                (format 
6149                                 "%s %s (%d scored, %d total): "
6150                                 "How many articles from"
6151                                 group scored number))))
6152                          (if (string-equal input "")
6153                              number input)))
6154                       (t number))
6155               (quit nil)))))
6156          total-articles)
6157     (setq select (if (stringp select) (string-to-number select) select))
6158     (if (or (null select) (zerop select))
6159         select
6160       (if (and (not (zerop scored)) (<= (abs select) scored))
6161           (progn
6162             (setq articles (sort scored-list '<))
6163             (setq number (length articles)))
6164         (setq articles (copy-sequence articles)))
6165
6166       (setq total-articles articles)
6167       
6168       (if (< (abs select) number)
6169           (if (< select 0) 
6170               ;; Select the N oldest articles.
6171               (setcdr (nthcdr (1- (abs select)) articles) nil)
6172             ;; Select the N most recent articles.
6173             (setq articles (nthcdr (- number select) articles))))
6174       (setq gnus-newsgroup-unselected
6175             (gnus-sorted-intersection
6176              gnus-newsgroup-unreads
6177              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6178       articles)))
6179
6180 (defun gnus-killed-articles (killed articles)
6181   (let (out)
6182     (while articles
6183       (if (inline (gnus-member-of-range (car articles) killed))
6184           (setq out (cons (car articles) out)))
6185       (setq articles (cdr articles)))
6186     out))
6187
6188 (defun gnus-adjust-marked-articles (info &optional active)
6189   "Remove all marked articles that are no longer legal."
6190   (let ((marked-lists (nth 3 info))
6191         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6192         marked m prev)
6193     ;; There are many types of marked articles.
6194     (while marked-lists
6195       (setq m (cdr (setq prev (car marked-lists))))
6196       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6197              ;; Make sure that all ticked articles are a subset of the
6198              ;; unread/unselected articles.
6199              (while m
6200                (if (or (memq (car m) gnus-newsgroup-unreads)
6201                        (memq (car m) gnus-newsgroup-unselected))
6202                    (setq prev m)
6203                  (setcdr prev (cdr m)))
6204                (setq m (cdr m))))
6205             ((eq 'score (car prev))
6206              ;; Scored articles should be a subset of
6207              ;; unread/unselected articles. 
6208              (while m
6209                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6210                        (memq (car (car m)) gnus-newsgroup-unreads))
6211                    (setq prev m)
6212                  (setcdr prev (cdr m)))
6213                (setq m (cdr m))))
6214             ((eq 'bookmark (car prev))
6215              ;; Bookmarks should be a subset of active articles.
6216              (while m
6217                (if (< (car (car m)) (car active))
6218                    (setcdr prev (cdr m))
6219                  (setq prev m))
6220                (setq m (cdr m))))
6221             ((eq 'killed (car prev))
6222              ;; Articles that have been through the kill process are
6223              ;; to be a subset of active articles.
6224              (while (and m (< (or (and (numberp (car m)) (car m))
6225                                   (cdr (car m)))
6226                               (car active)))
6227                (setcdr prev (cdr m))
6228                (setq m (cdr m)))
6229              (if (and m (< (or (and (numberp (car m)) (car m))
6230                                (car (car m)))
6231                            (car active))) 
6232                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6233             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6234              ;; The replied and expirable articles have to be articles
6235              ;; that are active. 
6236              (while m
6237                (if (< (car m) (car active))
6238                    (setcdr prev (cdr m))
6239                  (setq prev m))
6240                (setq m (cdr m)))))
6241       (setq marked-lists (cdr marked-lists)))
6242     ;; Remove all lists that are empty.
6243     (setq marked-lists (nth 3 info))
6244     (if marked-lists
6245         (progn
6246           (while (= 1 (length (car marked-lists)))
6247             (setq marked-lists (cdr marked-lists)))
6248           (setq m (cdr (setq prev marked-lists)))
6249           (while m
6250             (if (= 1 (length (car m)))
6251                 (setcdr prev (cdr m))
6252               (setq prev m))
6253             (setq m (cdr m)))
6254           (setcar (nthcdr 3 info) marked-lists)))
6255     ;; Finally, if there are no marked lists at all left, and if there
6256     ;; are no elements after the lists in the info list, we just chop
6257     ;; the info list off before the marked lists.
6258     (and (null marked-lists) 
6259          (not (nthcdr 4 info))
6260          (setcdr (nthcdr 2 info) nil)))
6261   info)
6262
6263 (defun gnus-set-marked-articles 
6264   (info ticked replied expirable killed dormant bookmark score) 
6265   "Enter the various lists of marked articles into the newsgroup info list."
6266   (let (newmarked)
6267     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6268     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6269     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6270                                          newmarked)))
6271     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6272     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6273     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6274                                         newmarked)))
6275     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6276     (if (nthcdr 3 info)
6277         (if newmarked
6278             (setcar (nthcdr 3 info) newmarked)
6279           (if (not (nthcdr 4 info))
6280               (setcdr (nthcdr 2 info) nil)
6281             (setcar (nthcdr 3 info) nil)))
6282       (if newmarked
6283           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
6284
6285 (defun gnus-add-marked-articles (group type articles &optional info force)
6286   ;; Add ARTICLES of TYPE to the info of GROUP.
6287   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6288   ;; add, but replace marked articles of TYPE with ARTICLES.
6289   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6290         marked m)
6291     (or (not info)
6292         (and (not (setq marked (nthcdr 3 info)))
6293              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6294         (and (not (setq m (assq type (car marked))))
6295              (setcar marked (cons (cons type articles) (car marked))))
6296         (if force
6297             (setcdr m articles)
6298           (nconc m articles)))))
6299          
6300 (defun gnus-set-mode-line (where)
6301   "This function sets the mode line of the article or summary buffers.
6302 If WHERE is `summary', the summary mode line format will be used."
6303   (if (memq where gnus-updated-mode-lines)
6304       (let (mode-string)
6305         (save-excursion
6306           (set-buffer gnus-summary-buffer)
6307           (let* ((mformat (if (eq where 'article) 
6308                               gnus-article-mode-line-format-spec
6309                             gnus-summary-mode-line-format-spec))
6310                  (group-name gnus-newsgroup-name)
6311                  (article-number (or gnus-current-article 0))
6312                  (unread (- (length gnus-newsgroup-unreads)
6313                             (length gnus-newsgroup-dormant)))
6314                  (unread-and-unticked 
6315                   (- unread (length gnus-newsgroup-marked)))
6316                  (unselected (length gnus-newsgroup-unselected))
6317                  (unread-and-unselected
6318                   (cond ((and (zerop unread-and-unticked)
6319                               (zerop unselected)) "")
6320                         ((zerop unselected) 
6321                          (format "{%d more}" unread-and-unticked))
6322                         (t (format "{%d(+%d) more}"
6323                                    unread-and-unticked unselected))))
6324                  (subject
6325                   (if gnus-current-headers
6326                       (header-subject gnus-current-headers) ""))
6327                  (max-len (and gnus-mode-non-string-length
6328                                (- (frame-width) gnus-mode-non-string-length)))
6329                  header) ;; passed as argument to any user-format-funcs
6330             (setq mode-string (eval mformat))
6331             (or (numberp max-len)
6332                 (setq max-len (length mode-string)))
6333             (if (< max-len 4) (setq max-len 4))
6334             (if (> (length mode-string) max-len)
6335                 (setq mode-string 
6336                       (concat (substring mode-string 0 (- max-len 3))
6337                               "...")))
6338             (setq mode-string (format (format "%%-%ds" max-len)
6339                                       mode-string))))
6340         (setq mode-line-buffer-identification mode-string)
6341         (set-buffer-modified-p t))))
6342
6343 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6344   "Go through the HEADERS list and add all Xrefs to a hash table.
6345 The resulting hash table is returned, or nil if no Xrefs were found."
6346   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6347          (prefix (if (and 
6348                       (gnus-group-foreign-p from-newsgroup)
6349                       (not (memq 'virtual 
6350                                  (assoc (symbol-name (car from-method))
6351                                         gnus-valid-select-methods))))
6352                      (gnus-group-real-prefix from-newsgroup)))
6353          (xref-hashtb (make-vector 63 0))
6354          start group entry number xrefs header)
6355     (while headers
6356       (setq header (car headers))
6357       (if (and (setq xrefs (header-xref header))
6358                (not (memq (header-number header) unreads)))
6359           (progn
6360             (setq start 0)
6361             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6362               (setq start (match-end 0))
6363               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6364                                                     (match-end 1))))
6365               (setq number 
6366                     (string-to-int (substring xrefs (match-beginning 2) 
6367                                               (match-end 2))))
6368               (if (setq entry (gnus-gethash group xref-hashtb))
6369                   (setcdr entry (cons number (cdr entry)))
6370                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6371       (setq headers (cdr headers)))
6372     (if start xref-hashtb nil)))
6373
6374 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6375   "Look through all the headers and mark the Xrefs as read."
6376   (let ((virtual (memq 'virtual 
6377                        (assoc (symbol-name (car (gnus-find-method-for-group 
6378                                                  from-newsgroup)))
6379                               gnus-valid-select-methods)))
6380         name entry read info xref-hashtb idlist active num range exps method
6381         nth4)
6382     (save-excursion
6383       (set-buffer gnus-group-buffer)
6384       (if (setq xref-hashtb 
6385                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6386           (mapatoms 
6387            (lambda (group)
6388              (if (string= from-newsgroup (setq name (symbol-name group)))
6389                  ()
6390                (setq idlist (symbol-value group))
6391                ;; Dead groups are not updated.
6392                (if (and (prog1 
6393                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6394                                   info (nth 2 entry))
6395                           (if (stringp (setq nth4 (nth 4 info)))
6396                               (setq nth4 (gnus-server-to-method nth4))))
6397                         ;; Only do the xrefs if the group has the same
6398                         ;; select method as the group we have just read.
6399                         (or (gnus-methods-equal-p 
6400                              nth4 (gnus-find-method-for-group from-newsgroup))
6401                             virtual
6402                             (equal nth4 
6403                                    (setq method (gnus-find-method-for-group 
6404                                                  from-newsgroup)))
6405                             (and (equal (car nth4) (car method))
6406                                  (equal (nth 1 nth4) (nth 1 method))))
6407                         gnus-use-cross-reference
6408                         (or (not (eq gnus-use-cross-reference t))
6409                             virtual
6410                             ;; Only do cross-references on subscribed
6411                             ;; groups, if that is what is wanted.  
6412                             (<= (nth 1 info) gnus-level-subscribed)))
6413                    (progn
6414                      (setq num 0)
6415                      ;; Set the new list of read articles in this group.
6416                      (setq active (gnus-gethash name gnus-active-hashtb))
6417                      ;; First peel off all illegal article numbers.
6418                      (if active
6419                          (let ((ids idlist)
6420                                (ticked (cdr (assq 'tick (nth 3 info))))
6421                                (dormant (cdr (assq 'dormant (nth 3 info))))
6422                                id)
6423                            (setq exps nil)
6424                            (while ids
6425                              (setq id (car ids))
6426                              (if (or (> id (cdr active))
6427                                      (< id (car active))
6428                                      (memq id ticked)
6429                                      (memq id dormant))
6430                                  (setq idlist (delq id idlist)))
6431                              (and (memq id expirable)
6432                                   (setq exps (cons id exps)))
6433                              (setq ids (cdr ids)))))
6434                      ;; Update expirable articles.
6435                      (gnus-add-marked-articles nil 'expirable exps info)
6436                      (and active
6437                           (null (nth 2 info))
6438                           (> (car active) 1)
6439                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6440                      (setcar (nthcdr 2 info)
6441                              (setq range
6442                                    (gnus-add-to-range 
6443                                     (nth 2 info) 
6444                                     (setq idlist (sort idlist '<)))))
6445                      ;; Then we have to re-compute how many unread
6446                      ;; articles there are in this group.
6447                      (if active
6448                          (progn
6449                            (cond 
6450                             ((not range)
6451                              (setq num (- (1+ (cdr active)) (car active))))
6452                             ((not (listp (cdr range)))
6453                              (setq num (- (cdr active) (- (1+ (cdr range)) 
6454                                                           (car range)))))
6455                             (t
6456                              (while range
6457                                (if (numberp (car range))
6458                                    (setq num (1+ num))
6459                                  (setq num (+ num (- (1+ (cdr (car range)))
6460                                                      (car (car range))))))
6461                                (setq range (cdr range)))
6462                              (setq num (- (cdr active) num))))
6463                            ;; Update the number of unread articles.
6464                            (setcar 
6465                             entry 
6466                             (max 0 (- num 
6467                                       (length (cdr (assq 'tick (nth 3 info))))
6468                                       (length 
6469                                        (cdr (assq 'dormant (nth 3 info)))))))
6470                            ;; Update the group buffer.
6471                            (gnus-group-update-group name t)))))))
6472            xref-hashtb)))))
6473
6474 (defun gnus-methods-equal-p (m1 m2)
6475   (let ((m1 (or m1 gnus-select-method))
6476         (m2 (or m2 gnus-select-method)))
6477     (or (equal m1 m2)
6478         (and (eq (car m1) (car m2))
6479              (or (not (memq 'address (assoc (symbol-name (car m1))
6480                                             gnus-valid-select-methods)))
6481                  (equal (nth 1 m1) (nth 1 m2)))))))
6482
6483 (defsubst gnus-header-value ()
6484   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6485
6486 (defvar gnus-newsgroup-none-id 0)
6487
6488 (defun gnus-get-newsgroup-headers ()
6489   (setq gnus-article-internal-prepare-hook nil)
6490   (let ((cur nntp-server-buffer)
6491         (dependencies gnus-newsgroup-dependencies)
6492         headers char article id dep end)
6493     (save-excursion
6494       (set-buffer nntp-server-buffer)
6495       (goto-char (point-min))
6496       ;; Search to the beginning of the next header. Error messages
6497       ;; do not begin with 2 or 3.
6498       (while (re-search-forward "^[23][0-9]+ " nil t)
6499         (let ((header (make-vector 9 nil))
6500               (c (following-char))
6501               (case-fold-search t)
6502               (p (point))
6503               from subject in-reply-to references ref)
6504           (setq id nil
6505                 ref nil
6506                 references nil
6507                 subject nil
6508                 from nil)
6509           (header-set-number header (setq article (read cur)))
6510           ;; This implementation of this function, with nine
6511           ;; search-forwards instead of the one re-search-forward and
6512           ;; a case (which basically was the old function) is actually
6513           ;; about twice as fast, even though it looks messier. You
6514           ;; can't have everything, I guess. Speed and elegance
6515           ;; doesn't always come hand in hand.
6516           (save-restriction
6517             (narrow-to-region (point) (or (save-excursion 
6518                                             (search-forward "\n.\n" nil t))
6519                                           (point)))
6520             (if (search-forward "\nfrom: " nil t)
6521                 (header-set-from header (gnus-header-value))
6522               (header-set-from header "(nobody)"))
6523             (goto-char p)
6524             (if (search-forward "\nsubject: " nil t)
6525                 (header-set-subject header (gnus-header-value))
6526               (header-set-subject header "(none)"))
6527             (goto-char p)
6528             (and (search-forward "\nxref: " nil t)
6529                  (header-set-xref header (gnus-header-value)))
6530             (goto-char p)
6531             (or (numberp (and (search-forward "\nlines: " nil t)
6532                               (header-set-lines header (read cur))))
6533                 (header-set-lines header 0))
6534             (goto-char p)
6535             (and (search-forward "\ndate: " nil t)
6536                  (header-set-date header (gnus-header-value)))
6537             (goto-char p)
6538             (if (search-forward "\nmessage-id: " nil t)
6539                 (header-set-id header (setq id (gnus-header-value)))
6540               ;; If there was no message-id, we just fake one to make
6541               ;; subsequent routines simpler.
6542               (header-set-id 
6543                header 
6544                (setq id (concat "none+" 
6545                                 (int-to-string 
6546                                  (setq gnus-newsgroup-none-id 
6547                                        (1+ gnus-newsgroup-none-id)))))))
6548             (goto-char p)
6549             (if (search-forward "\nreferences: " nil t)
6550                 (progn
6551                   (header-set-references header (gnus-header-value))
6552                   (setq end (match-end 0))
6553                   (save-excursion
6554                     (setq ref 
6555                           (downcase
6556                            (buffer-substring
6557                             (progn 
6558                               (end-of-line)
6559                               (search-backward ">" end t)
6560                               (1+ (point)))
6561                             (progn
6562                               (search-backward "<" end t)
6563                               (point)))))))
6564               ;; Get the references from the in-reply-to header if there
6565               ;; ware no references and the in-reply-to header looks
6566               ;; promising. 
6567               (if (and (search-forward "\nin-reply-to: " nil t)
6568                        (setq in-reply-to (gnus-header-value))
6569                        (string-match "<[^>]+>" in-reply-to))
6570                   (progn
6571                     (header-set-references 
6572                      header 
6573                      (setq ref (substring in-reply-to (match-beginning 0)
6574                                           (match-end 0))))
6575                     (setq ref (downcase ref)))
6576                 (setq ref "none")))
6577             ;; We do some threading while we read the headers. The
6578             ;; message-id and the last reference are both entered into
6579             ;; the same hash table. Some tippy-toeing around has to be
6580             ;; done in case an article has arrived before the article
6581             ;; which it refers to.
6582             (if (boundp (setq dep (intern (downcase id) dependencies)))
6583                 (if (car (symbol-value dep))
6584                     ;; An article with this Message-ID has already
6585                     ;; been seen, so we ignore this one, except we add
6586                     ;; any additional Xrefs (in case the two articles
6587                     ;; came from different servers.
6588                     (progn
6589                       (header-set-xref 
6590                        (car (symbol-value dep))
6591                        (concat (or (header-xref (car (symbol-value dep))) "")
6592                                (or (header-xref header) "")))
6593                       (setq header nil))
6594                   (setcar (symbol-value dep) header))
6595               (set dep (list header)))
6596             (if header
6597                 (progn
6598                   (if (boundp (setq dep (intern ref dependencies)))
6599                       (setcdr (symbol-value dep) 
6600                               (cons header (cdr (symbol-value dep))))
6601                     (set dep (list nil header)))
6602                   (setq headers (cons header headers))))
6603             (goto-char (point-max))))))
6604     (nreverse headers)))
6605
6606 ;; The following macros and functions were written by Felix Lee
6607 ;; <flee@cse.psu.edu>. 
6608
6609 (defmacro gnus-nov-read-integer ()
6610   '(prog1
6611        (if (= (following-char) ?\t)
6612            0
6613          (let ((num (condition-case nil (read buffer) (error nil))))
6614            (if (numberp num) num 0)))
6615      (or (eobp) (forward-char 1))))
6616
6617 (defmacro gnus-nov-skip-field ()
6618   '(search-forward "\t" eol 'move))
6619
6620 (defmacro gnus-nov-field ()
6621   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6622
6623 ;; Goes through the xover lines and returns a list of vectors
6624 (defun gnus-get-newsgroup-headers-xover (sequence)
6625   "Parse the news overview data in the server buffer, and return a
6626 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6627   ;; Get the Xref when the users reads the articles since most/some
6628   ;; NNTP servers do not include Xrefs when using XOVER.
6629   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6630   (let ((cur nntp-server-buffer)
6631         (dependencies gnus-newsgroup-dependencies)
6632         (none 0)
6633         number headers header)
6634     (save-excursion
6635       (set-buffer nntp-server-buffer)
6636       (goto-char (point-min))
6637       (while (and sequence (not (eobp)))
6638         (setq number (read cur))
6639         (while (and sequence (< (car sequence) number))
6640           (setq sequence (cdr sequence)))
6641         (and sequence 
6642              (eq number (car sequence))
6643              (progn
6644                (setq sequence (cdr sequence))
6645                (if (setq header 
6646                          (inline (gnus-nov-parse-line number dependencies)))
6647                    (setq headers (cons header headers)))))
6648         (forward-line 1))
6649       (setq headers (nreverse headers)))
6650     headers))
6651
6652 ;; This function has to be called with point after the article number
6653 ;; on the beginning of the line.
6654 (defun gnus-nov-parse-line (number dependencies)
6655   (let ((none 0)
6656         (eol (gnus-point-at-eol)) 
6657         (buffer (current-buffer))
6658         header ref id dep)
6659
6660     ;; overview: [num subject from date id refs chars lines misc]
6661     (narrow-to-region (point) eol)
6662     (forward-char)
6663
6664     (condition-case nil
6665         (setq header
6666               (vector 
6667                number                   ; number
6668                (gnus-nov-field)         ; subject
6669                (gnus-nov-field)         ; from
6670                (gnus-nov-field)         ; date
6671                (setq id (or (gnus-nov-field)
6672                             (concat "none+"
6673                                     (int-to-string 
6674                                      (setq none (1+ none)))))) ; id
6675                (progn
6676                  (save-excursion
6677                    (let ((beg (point)))
6678                      (search-forward "\t" eol)
6679                      (if (search-backward ">" beg t)
6680                          (setq ref 
6681                                (downcase 
6682                                 (buffer-substring 
6683                                  (1+ (point))
6684                                  (progn
6685                                    (search-backward "<" beg t)
6686                                    (point)))))
6687                        (setq ref nil))))
6688                  (gnus-nov-field))      ; refs
6689                (gnus-nov-read-integer)  ; chars
6690                (gnus-nov-read-integer)  ; lines
6691                (if (= (following-char) ?\n)
6692                    nil
6693                  (gnus-nov-field))      ; misc
6694                ))
6695       (error (progn 
6696                (ding)
6697                (message "Strange nov line.")
6698                (setq header nil)
6699                (goto-char eol))))
6700
6701     (widen)
6702
6703     ;; We build the thread tree.
6704     (and header
6705          (if (boundp (setq dep (intern (downcase id) dependencies)))
6706              (if (car (symbol-value dep))
6707                  ;; An article with this Message-ID has already been seen,
6708                  ;; so we ignore this one, except we add any additional
6709                  ;; Xrefs (in case the two articles came from different
6710                  ;; servers.
6711                  (progn
6712                    (header-set-xref 
6713                     (car (symbol-value dep))
6714                     (concat (or (header-xref (car (symbol-value dep))) "")
6715                             (or (header-xref header) "")))
6716                    (setq header nil))
6717                (setcar (symbol-value dep) header))
6718            (set dep (list header))))
6719     (if header
6720         (progn
6721           (if (boundp (setq dep (intern (or ref "none") 
6722                                         dependencies)))
6723               (setcdr (symbol-value dep) 
6724                       (cons header (cdr (symbol-value dep))))
6725             (set dep (list nil header)))))
6726     header))
6727
6728 (defun gnus-article-get-xrefs ()
6729   "Fill in the Xref value in `gnus-current-headers', if necessary.
6730 This is meant to be called in `gnus-article-internal-prepare-hook'."
6731   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6732                                  gnus-current-headers)))
6733     (or (not gnus-use-cross-reference)
6734         (and (header-xref headers)
6735              (not (string= (header-xref headers) "")))
6736         (let ((case-fold-search t)
6737               xref)
6738           (save-restriction
6739             (gnus-narrow-to-headers)
6740             (goto-char (point-min))
6741             (if (or (and (eq (downcase (following-char)) ?x)
6742                          (looking-at "Xref:"))
6743                     (search-forward "\nXref:" nil t))
6744                 (progn
6745                   (goto-char (1+ (match-end 0)))
6746                   (setq xref (buffer-substring (point) 
6747                                                (progn (end-of-line) (point))))
6748                   (header-set-xref headers xref))))))))
6749
6750 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6751 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6752
6753 ;; Return a header specified by a NUMBER.
6754 (defun gnus-get-header-by-number (number)
6755   (save-excursion
6756     (set-buffer gnus-summary-buffer)
6757     (or gnus-newsgroup-headers-hashtb-by-number
6758         (gnus-make-headers-hashtable-by-number))
6759     (gnus-gethash (int-to-string number)
6760                   gnus-newsgroup-headers-hashtb-by-number)))
6761
6762 (defun gnus-make-headers-hashtable-by-number ()
6763   "Make hashtable for the variable gnus-newsgroup-headers by number."
6764   (save-excursion
6765     (set-buffer gnus-summary-buffer)
6766     (let ((headers gnus-newsgroup-headers)
6767           header)
6768       (setq gnus-newsgroup-headers-hashtb-by-number
6769             (gnus-make-hashtable (length headers)))
6770       (while headers
6771         (setq header (car headers))
6772         (gnus-sethash (int-to-string (header-number header))
6773                       header gnus-newsgroup-headers-hashtb-by-number)
6774         (setq headers (cdr headers))))))
6775
6776 (defun gnus-more-header-backward ()
6777   "Find new header backward."
6778   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6779         (artnum gnus-newsgroup-begin)
6780         (header nil))
6781     (while (and (not header)
6782                 (> artnum first))
6783       (setq artnum (1- artnum))
6784       (setq header (gnus-read-header artnum)))
6785     header))
6786
6787 (defun gnus-more-header-forward (&optional backward)
6788   "Find new header forward.
6789 If BACKWARD, find new header backward instead."
6790   (if backward
6791       (gnus-more-header-backward)
6792     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6793           (artnum gnus-newsgroup-end)
6794           (header nil))
6795       (while (and (not header)
6796                   (< artnum last))
6797         (setq artnum (1+ artnum))
6798         (setq header (gnus-read-header artnum)))
6799       header)))
6800
6801 (defun gnus-extend-newsgroup (header &optional backward)
6802   "Extend newsgroup selection with HEADER.
6803 Optional argument BACKWARD means extend toward backward."
6804   (if header
6805       (let ((artnum (header-number header)))
6806         (setq gnus-newsgroup-headers
6807               (if backward
6808                   (cons header gnus-newsgroup-headers)
6809                 (nconc gnus-newsgroup-headers (list header))))
6810         (setq gnus-newsgroup-unselected
6811               (delq artnum gnus-newsgroup-unselected))
6812         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6813         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6814
6815 (defun gnus-summary-work-articles (n)
6816   "Return a list of articles to be worked upon. The prefix argument,
6817 the list of process marked articles, and the current article will be
6818 taken into consideration."
6819   (let (articles)
6820     (if (and n (numberp n))
6821         (let ((backward (< n 0))
6822               (n (abs n)))
6823           (save-excursion
6824             (while (and (> n 0)
6825                         (setq articles (cons (gnus-summary-article-number) 
6826                                              articles))
6827                         (gnus-summary-search-forward nil nil backward))
6828               (setq n (1- n))))
6829           (sort articles (function <)))
6830       (or (reverse gnus-newsgroup-processable)
6831           (list (gnus-summary-article-number))))))
6832
6833 (defun gnus-summary-search-group (&optional backward use-level)
6834   "Search for next unread newsgroup.
6835 If optional argument BACKWARD is non-nil, search backward instead."
6836   (save-excursion
6837     (set-buffer gnus-group-buffer)
6838     (save-excursion
6839       ;; We don't want to alter current point of group mode buffer.
6840       (if (gnus-group-search-forward 
6841            backward nil
6842            (if use-level (gnus-group-group-level) nil))
6843           (gnus-group-group-name)))))
6844
6845 (defun gnus-summary-best-group (&optional exclude-group)
6846   "Find the name of the best unread group.
6847 If EXCLUDE-GROUP, do not go to this group."
6848   (save-excursion
6849     (set-buffer gnus-group-buffer)
6850     (save-excursion
6851       (gnus-group-best-unread-group exclude-group))))
6852
6853 (defun gnus-summary-search-subject (&optional backward unread subject)
6854   "Search for article forward.
6855 If BACKWARD is non-nil, search backward.
6856 If UNREAD is non-nil, only unread articles are selected.
6857 If SUBJECT is non-nil, the article which has the same subject will be
6858 searched for." 
6859   (let ((func (if backward 'previous-single-property-change
6860                 'next-single-property-change))
6861         (beg (point))
6862         (did t)
6863         pos)
6864     (beginning-of-line)
6865     (and gnus-summary-check-current unread
6866          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
6867          (setq did nil))
6868     (if (not did)
6869         ()
6870       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6871       (while
6872           (and 
6873            (setq pos (funcall func (point) 'gnus-number))
6874            (goto-char (if backward (1- pos) pos))
6875            (setq did
6876                  (not (and
6877                        (or (not unread)
6878                            (eq (get-text-property (point) 'gnus-mark)
6879                                gnus-unread-mark))
6880                        (or (not subject)
6881                            (equal (gnus-simplify-subject-re subject)
6882                                   (gnus-simplify-subject-re
6883                                    (gnus-summary-subject-string)))))))
6884            (if backward (if (bobp) nil (forward-char -1) t)
6885              (if (eobp) nil (forward-char 1) t)))))
6886     (if did
6887         (progn (goto-char beg) nil)
6888       (prog1
6889           (get-text-property (point) 'gnus-number)
6890         (gnus-summary-position-cursor)))))
6891
6892 (defun gnus-summary-search-forward (&optional unread subject backward)
6893   "Search for article forward.
6894 If UNREAD is non-nil, only unread articles are selected.
6895 If SUBJECT is non-nil, the article which has the same subject will be
6896 searched for. 
6897 If BACKWARD is non-nil, the search will be performed backwards instead."
6898   (gnus-summary-search-subject backward unread subject))
6899
6900 (defun gnus-summary-search-backward (&optional unread subject)
6901   "Search for article backward.
6902 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6903 If 2nd optional argument SUBJECT is non-nil, the article which has
6904 the same subject will be searched for."
6905   (gnus-summary-search-forward unread subject t))
6906
6907 (defun gnus-summary-article-number (&optional number-or-nil)
6908   "The article number of the article on the current line.
6909 If there isn's an article number here, then we return the current
6910 article number."
6911   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
6912     (if number-or-nil number (or number gnus-current-article))))
6913
6914 (defun gnus-summary-thread-level ()
6915   "The thread level of the article on the current line."
6916   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
6917       0))
6918
6919 (defun gnus-summary-pseudo-article ()
6920   "The thread level of the article on the current line."
6921   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
6922
6923 (defun gnus-summary-article-mark ()
6924   "The mark on the current line."
6925   (get-text-property (gnus-point-at-bol) 'gnus-mark))
6926
6927 (defun gnus-summary-subject-string ()
6928   "Return current subject string or nil if nothing."
6929   (let ((article (gnus-summary-article-number))
6930         header)
6931     (and article 
6932          (setq header (gnus-get-header-by-number article))
6933          (vectorp header)
6934          (header-subject header))))
6935
6936 (defalias 'gnus-summary-score 'gnus-summary-article-score)
6937 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
6938 (defun gnus-summary-article-score ()
6939   "Return current article score."
6940   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
6941       gnus-summary-default-score 0))
6942
6943 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
6944
6945 (defun gnus-summary-recenter ()
6946   "Center point in the summary window.
6947 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6948 displayed, no centering will be performed." 
6949   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6950   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6951   (let* ((top (cond ((< (window-height) 4) 0)
6952                     ((< (window-height) 6) 1)
6953                     (t 2)))
6954          (height (1- (window-height)))
6955          (bottom (save-excursion (goto-char (point-max))
6956                                  (forward-line (- height))
6957                                  (point)))
6958          (window (get-buffer-window (current-buffer))))
6959     (and 
6960      ;; The user has to want it,
6961      gnus-auto-center-summary 
6962      ;; the article buffer must be displayed,
6963      (get-buffer-window gnus-article-buffer)
6964      ;; Set the window start to either `bottom', which is the biggest
6965      ;; possible valid number, or the second line from the top,
6966      ;; whichever is the least.
6967      (set-window-start
6968       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
6969
6970 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
6971 (defun gnus-short-group-name (group &optional levels)
6972   "Collapse GROUP name LEVELS."
6973   (let* ((name "") (foreign "") (depth -1) (skip 1)
6974          (levels (or levels
6975                      (progn
6976                        (while (string-match "\\." group skip)
6977                          (setq skip (match-end 0)
6978                                depth (+ depth 1)))
6979                        depth))))
6980     (if (string-match ":" group)
6981         (setq foreign (substring group 0 (match-end 0))
6982               group (substring group (match-end 0))))
6983     (while group
6984       (if (and (string-match "\\." group) (> levels 0))
6985           (setq name (concat name (substring group 0 1))
6986                 group (substring group (match-end 0))
6987                 levels (- levels 1)
6988                 name (concat name "."))
6989         (setq name (concat foreign name group)
6990               group nil)))
6991     name))
6992
6993 (defun gnus-summary-jump-to-group (newsgroup)
6994   "Move point to NEWSGROUP in group mode buffer."
6995   ;; Keep update point of group mode buffer if visible.
6996   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6997       (save-window-excursion
6998         ;; Take care of tree window mode.
6999         (if (get-buffer-window gnus-group-buffer)
7000             (pop-to-buffer gnus-group-buffer))
7001         (gnus-group-jump-to-group newsgroup))
7002     (save-excursion
7003       ;; Take care of tree window mode.
7004       (if (get-buffer-window gnus-group-buffer)
7005           (pop-to-buffer gnus-group-buffer)
7006         (set-buffer gnus-group-buffer))
7007       (gnus-group-jump-to-group newsgroup))))
7008
7009 ;; This function returns a list of article numbers based on the
7010 ;; difference between the ranges of read articles in this group and
7011 ;; the range of active articles.
7012 (defun gnus-list-of-unread-articles (group)
7013   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7014          (active (gnus-gethash group gnus-active-hashtb))
7015          (last (cdr active))
7016          unread first nlast unread)
7017     ;; If none are read, then all are unread. 
7018     (if (not read)
7019         (setq first (car active))
7020       ;; If the range of read articles is a single range, then the
7021       ;; first unread article is the article after the last read
7022       ;; article. Sounds logical, doesn't it?
7023       (if (not (listp (cdr read)))
7024           (setq first (1+ (cdr read)))
7025         ;; `read' is a list of ranges.
7026         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7027                                 (car (car read)))) 1)
7028             (setq first 1))
7029         (while read
7030           (if first 
7031               (while (< first nlast)
7032                 (setq unread (cons first unread))
7033                 (setq first (1+ first))))
7034           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7035           (setq nlast (if (atom (car (cdr read))) 
7036                           (car (cdr read))
7037                         (car (car (cdr read)))))
7038           (setq read (cdr read)))))
7039     ;; And add the last unread articles.
7040     (while (<= first last)
7041       (setq unread (cons first unread))
7042       (setq first (1+ first)))
7043     ;; Return the list of unread articles.
7044     (nreverse unread)))
7045
7046 (defun gnus-list-of-read-articles (group)
7047   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7048         (active (gnus-gethash group gnus-active-hashtb)))
7049     (and info active
7050          (gnus-sorted-complement 
7051           (gnus-uncompress-range active) 
7052           (gnus-list-of-unread-articles group)))))
7053
7054 ;; Various summary commands
7055
7056 (defun gnus-summary-universal-argument ()
7057   "Perform any operation on all articles marked with the process mark."
7058   (interactive)
7059   (gnus-set-global-variables)
7060   (let ((articles (reverse gnus-newsgroup-processable))
7061         key func)
7062     (or articles (error "No articles marked"))
7063     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7064         (error "Undefined key"))
7065     (while articles
7066       (gnus-summary-goto-subject (car articles))
7067       (command-execute func)
7068       (gnus-summary-remove-process-mark (car articles))
7069       (setq articles (cdr articles)))))
7070
7071 (defun gnus-summary-toggle-truncation (arg)
7072   "Toggle truncation of summary lines.
7073 With arg, turn line truncation on iff arg is positive."
7074   (interactive "P")
7075   (setq truncate-lines
7076         (if (null arg) (not truncate-lines)
7077           (> (prefix-numeric-value arg) 0)))
7078   (redraw-display))
7079
7080 (defun gnus-summary-reselect-current-group (all)
7081   "Once exit and then reselect the current newsgroup.
7082 The prefix argument ALL means to select all articles."
7083   (interactive "P")
7084   (gnus-set-global-variables)
7085   (let ((current-subject (gnus-summary-article-number))
7086         (group gnus-newsgroup-name))
7087     (setq gnus-newsgroup-threads nil)
7088     (gnus-summary-exit t)
7089     ;; We have to adjust the point of group mode buffer because the
7090     ;; current point was moved to the next unread newsgroup by
7091     ;; exiting.
7092     (gnus-summary-jump-to-group group)
7093     (gnus-group-read-group all t)
7094     (gnus-summary-goto-subject current-subject)))
7095
7096 (defun gnus-summary-rescan-group (all)
7097   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7098   (interactive "P")
7099   (gnus-set-global-variables)
7100   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7101   (let ((group gnus-newsgroup-name))
7102     (gnus-summary-exit)
7103     (gnus-summary-jump-to-group group)
7104     (save-excursion
7105       (set-buffer gnus-group-buffer)
7106       (gnus-group-get-new-news-this-group 1))
7107     (gnus-summary-jump-to-group group)
7108     (gnus-group-read-group all)))
7109
7110 (defun gnus-summary-update-info ()
7111   (let* ((group gnus-newsgroup-name)
7112          (method (car (gnus-find-method-for-group group))))
7113     (if gnus-newsgroup-kill-headers
7114         (setq gnus-newsgroup-killed
7115               (gnus-compress-sequence
7116                (nconc
7117                 (gnus-set-sorted-intersection
7118                  (gnus-uncompress-range gnus-newsgroup-killed)
7119                  (setq gnus-newsgroup-unselected
7120                        (sort gnus-newsgroup-unselected '<)))
7121                 (setq gnus-newsgroup-unreads
7122                       (sort gnus-newsgroup-unreads '<))) t)))
7123     (or (listp (cdr gnus-newsgroup-killed))
7124         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7125     (let ((updated nil)
7126           (headers gnus-newsgroup-headers))
7127       (gnus-close-group group)
7128       (run-hooks 'gnus-exit-group-hook)
7129       (gnus-update-read-articles 
7130        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7131        gnus-newsgroup-marked
7132        t gnus-newsgroup-replied gnus-newsgroup-expirable
7133        gnus-newsgroup-killed gnus-newsgroup-dormant
7134        gnus-newsgroup-bookmarks 
7135        (and gnus-save-score gnus-newsgroup-scored))
7136       (and gnus-use-cross-reference
7137            (gnus-mark-xrefs-as-read 
7138             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7139       ;; Do adaptive scoring, and possibly save score files.
7140       (and gnus-newsgroup-adaptive
7141            (gnus-score-adaptive))
7142       (and gnus-use-scoring 
7143            (fboundp 'gnus-score-save)
7144            (funcall 'gnus-score-save))
7145       ;; Do not switch windows but change the buffer to work.
7146       (set-buffer gnus-group-buffer)
7147       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7148           (gnus-group-update-group group)))))
7149   
7150 (defun gnus-summary-exit (&optional temporary)
7151   "Exit reading current newsgroup, and then return to group selection mode.
7152 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7153   (interactive)
7154   (gnus-set-global-variables)
7155   (gnus-kill-save-kill-buffer)
7156   (let* ((group gnus-newsgroup-name)
7157          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7158                                                   gnus-newsgroup-name))))
7159          (mode major-mode)
7160          (method (car (gnus-find-method-for-group group)))
7161          (buf (current-buffer)))
7162     (gnus-summary-update-info) ; Make all changes in this group permanent.
7163     ;; Make sure where I was, and go to next newsgroup.
7164     (or quit-config
7165         (progn
7166           (gnus-group-jump-to-group group)
7167           (gnus-group-next-unread-group 1)))
7168     (if temporary
7169         nil                             ;Nothing to do.
7170       ;; We set all buffer-local variables to nil. It is unclear why
7171       ;; this is needed, but if we don't, buffer-local variables are
7172       ;; not garbage-collected, it seems. This would the lead to en
7173       ;; ever-growing Emacs.
7174       (set-buffer buf)
7175       (gnus-summary-clear-local-variables)
7176       ;; We clear the global counterparts of the buffer-local
7177       ;; variables as well, just to be on the safe side.
7178       (gnus-configure-windows 'group)
7179       (gnus-summary-clear-local-variables)
7180       ;; Return to group mode buffer. 
7181       (if (eq mode 'gnus-summary-mode)
7182           (gnus-kill-buffer buf))
7183       (if (get-buffer gnus-article-buffer)
7184           (bury-buffer gnus-article-buffer))
7185       (setq gnus-current-select-method gnus-select-method)
7186       (pop-to-buffer gnus-group-buffer)
7187       (if (not quit-config)
7188           (progn
7189             (gnus-group-jump-to-group group)
7190             (gnus-group-next-unread-group 1))
7191         (if (not (buffer-name (car quit-config)))
7192             (gnus-configure-windows 'group)
7193           (set-buffer (car quit-config))
7194           (and (eq major-mode 'gnus-summary-mode)
7195                (gnus-set-global-variables))
7196           (gnus-configure-windows (cdr quit-config)))))))
7197
7198 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7199 (defun gnus-summary-exit-no-update (&optional no-questions)
7200   "Quit reading current newsgroup without updating read article info."
7201   (interactive)
7202   (let* ((group gnus-newsgroup-name)
7203          (quit-config (nth 1 (assoc 'quit-config 
7204                                     (gnus-find-method-for-group group)))))
7205     (if (or no-questions
7206             gnus-expert-user
7207             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7208         (progn
7209           (gnus-close-group group)
7210           (gnus-summary-clear-local-variables)
7211           (set-buffer gnus-group-buffer)
7212           (gnus-summary-clear-local-variables)
7213           ;; Return to group selection mode.
7214           (gnus-configure-windows 'group)
7215           (if (get-buffer gnus-summary-buffer)
7216               (kill-buffer gnus-summary-buffer))
7217           (if (get-buffer gnus-article-buffer)
7218               (bury-buffer gnus-article-buffer))
7219           (if (equal (gnus-group-group-name) group)
7220               (gnus-group-next-unread-group 1))
7221           (if quit-config
7222               (progn
7223                 (if (not (buffer-name (car quit-config)))
7224                     (gnus-configure-windows 'group)
7225                   (set-buffer (car quit-config))
7226                   (and (eq major-mode 'gnus-summary-mode)
7227                        (gnus-set-global-variables))
7228                   (gnus-configure-windows (cdr quit-config)))))))))
7229
7230 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7231 (defun gnus-summary-fetch-faq (group)
7232   "Fetch the FAQ for the current group."
7233   (interactive (list gnus-newsgroup-name))
7234   (gnus-configure-windows 'summary-faq)
7235   (find-file (concat gnus-group-faq-directory group)))
7236
7237 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7238 (defun gnus-summary-describe-group (force)
7239   "Describe the current newsgroup."
7240   (interactive "P")
7241   (gnus-group-describe-group force gnus-newsgroup-name))
7242
7243 (defun gnus-summary-describe-briefly ()
7244   "Describe summary mode commands briefly."
7245   (interactive)
7246   (gnus-message 6
7247     (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")))
7248
7249 ;; Walking around group mode buffer from summary mode.
7250
7251 (defun gnus-summary-next-group (&optional no-article group backward)
7252   "Exit current newsgroup and then select next unread newsgroup.
7253 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7254 If BACKWARD, go to previous group instead."
7255   (interactive "P")
7256   (gnus-set-global-variables)
7257   (let ((ingroup gnus-newsgroup-name)
7258         (sumbuf (current-buffer))
7259         num)
7260     (gnus-summary-exit t)               ;Update all information.
7261     (if (and group
7262              (or (and (numberp (setq num (car (gnus-gethash
7263                                                group gnus-newsrc-hashtb))))
7264                       (< num 1))
7265                  (null num)))
7266         (progn
7267           (gnus-group-jump-to-group group)
7268           (setq group nil))
7269       (gnus-group-jump-to-group ingroup))
7270     (gnus-summary-search-group backward)
7271     (let ((group (or group (gnus-summary-search-group backward)))
7272           (buf gnus-summary-buffer))
7273       (if (null group)
7274           (gnus-summary-exit-no-update t)
7275         (gnus-message 5 "Selecting %s..." group)
7276         ;; We are now in group mode buffer.
7277         ;; Make sure group mode buffer point is on GROUP.
7278         (gnus-group-jump-to-group group)
7279         (if (not (eq gnus-auto-select-next 'quietly))
7280             (progn
7281               (gnus-summary-read-group group nil no-article buf)
7282               (and (string= gnus-newsgroup-name ingroup)
7283                    (bufferp sumbuf) (buffer-name sumbuf)
7284                    (progn
7285                      (set-buffer (setq gnus-summary-buffer sumbuf))
7286                      (gnus-summary-exit-no-update t))))
7287           (let ((prevgroup group))
7288             (gnus-summary-read-group group nil no-article buf)
7289             (while (and (string= gnus-newsgroup-name ingroup)
7290                         (bufferp sumbuf) 
7291                         (buffer-name sumbuf)
7292                         (not (string= prevgroup (gnus-group-group-name))))
7293               (set-buffer gnus-group-buffer)
7294               (gnus-summary-read-group 
7295                (setq prevgroup (gnus-group-group-name)) 
7296                nil no-article buf))
7297             (and (string= prevgroup (gnus-group-group-name))
7298                  ;; We have reached the final group in the group
7299                  ;; buffer.
7300                  (progn
7301                    (set-buffer sumbuf)
7302                    (gnus-summary-exit)))))))))
7303
7304 (defun gnus-summary-prev-group (no-article)
7305   "Exit current newsgroup and then select previous unread newsgroup.
7306 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7307   (interactive "P")
7308   (gnus-summary-next-group no-article nil t))
7309
7310 ;; Walking around summary lines.
7311
7312 (defun gnus-summary-first-subject (unread)
7313   "Go to the first unread subject.
7314 If UNREAD is non-nil, go to the first unread article.
7315 Returns nil if there are no unread articles."
7316   (interactive "P")
7317   (prog1
7318       (if (or (not unread)
7319               (gnus-goto-char 
7320                (text-property-any 
7321                 (point-min) (point-max) 'gnus-mark gnus-unread-mark)))
7322           t 
7323         ;; If there are no unread articles.
7324         (gnus-message 3 "No more unread articles")
7325         nil)
7326     (gnus-summary-position-cursor)))
7327
7328 (defun gnus-summary-next-subject (n &optional unread dont-display)
7329   "Go to next N'th summary line.
7330 If N is negative, go to the previous N'th subject line.
7331 If UNREAD is non-nil, only unread articles are selected.
7332 The difference between N and the actual number of steps taken is
7333 returned."
7334   (interactive "p")
7335   (let ((backward (< n 0))
7336         (n (abs n)))
7337     (while (and (> n 0)
7338                 (gnus-summary-search-forward unread nil backward))
7339       (setq n (1- n)))
7340     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7341                                (if unread " unread" "")))
7342     (or dont-display
7343         (progn
7344           (gnus-summary-recenter)
7345           (gnus-summary-position-cursor)))
7346   n))
7347
7348 (defun gnus-summary-next-unread-subject (n)
7349   "Go to next N'th unread summary line."
7350   (interactive "p")
7351   (gnus-summary-next-subject n t))
7352
7353 (defun gnus-summary-prev-subject (n &optional unread)
7354   "Go to previous N'th summary line.
7355 If optional argument UNREAD is non-nil, only unread article is selected."
7356   (interactive "p")
7357   (gnus-summary-next-subject (- n) unread))
7358
7359 (defun gnus-summary-prev-unread-subject (n)
7360   "Go to previous N'th unread summary line."
7361   (interactive "p")
7362   (gnus-summary-next-subject (- n) t))
7363
7364 (defun gnus-summary-goto-subject (article)
7365   "Go the subject line of ARTICLE."
7366   (interactive
7367    (list
7368     (string-to-int
7369      (completing-read "Article number: "
7370                       (mapcar
7371                        (lambda (headers)
7372                          (list
7373                           (int-to-string (header-number headers))))
7374                        gnus-newsgroup-headers)
7375                       nil 'require-match))))
7376   (or article (error "No article number"))
7377   (let ((b (point)))
7378     (gnus-goto-char (text-property-any (point-min) (point-max)
7379                                        'gnus-number article))
7380     (gnus-summary-show-thread)
7381     ;; Skip dummy articles. 
7382     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7383         (forward-line 1))
7384     (prog1
7385         (if (not (eobp))
7386             article
7387           (goto-char b)
7388           nil)
7389       (gnus-summary-position-cursor))))
7390
7391 ;; Walking around summary lines with displaying articles.
7392
7393 (defun gnus-summary-expand-window ()
7394   "Expand summary window to show headers full window."
7395   (interactive)
7396   (gnus-set-global-variables)
7397   (gnus-configure-windows 'summary))
7398
7399 (defun gnus-summary-display-article (article &optional all-header)
7400   "Display ARTICLE in article buffer."
7401   (gnus-set-global-variables)
7402   (if (null article)
7403       nil
7404     (gnus-article-prepare article all-header)
7405     (gnus-summary-show-thread)
7406     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7407         (progn
7408           (forward-line 1)
7409           (gnus-summary-position-cursor)))
7410     (run-hooks 'gnus-select-article-hook)
7411     (gnus-summary-recenter)
7412 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
7413 ;    (sit-for 0)
7414     (gnus-summary-goto-subject article)
7415     ;; Successfully display article.
7416     (gnus-summary-update-line)
7417     (gnus-article-set-window-start 
7418      (cdr (assq article gnus-newsgroup-bookmarks)))
7419     t))
7420
7421 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7422   "Select the current article.
7423 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7424 non-nil, the article will be re-fetched even if it already present in
7425 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7426 be displayed."
7427   (and (not pseudo) (gnus-summary-pseudo-article)
7428        (error "This is a pseudo-article."))
7429   (let ((article (or article (gnus-summary-article-number)))
7430         (all-headers (not (not all-headers))) ;Must be T or NIL.
7431         did) 
7432     (prog1
7433         (save-excursion
7434           (set-buffer gnus-summary-buffer)
7435           (if (or (null gnus-current-article)
7436                   (null gnus-article-current)
7437                   (not (eq article (cdr gnus-article-current)))
7438                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7439                   force)
7440               ;; The requested article is different from the current article.
7441               (progn
7442                 (gnus-summary-display-article article all-headers)
7443                 (setq did article))
7444             (if all-headers (gnus-article-show-all-headers))
7445             nil))
7446       (if did 
7447           (gnus-article-set-window-start 
7448            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7449
7450 (defun gnus-summary-set-current-mark (&optional current-mark)
7451   "Obsolete function."
7452   nil)
7453
7454 (defun gnus-summary-next-article (unread &optional subject backward)
7455   "Select the next article.
7456 If UNREAD, only unread articles are selected.
7457 If SUBJECT, only articles with SUBJECT are selected.
7458 If BACKWARD, the previous article is selected instead of the next."
7459   (interactive "P")
7460   (let ((opoint (point))
7461         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
7462         header)
7463     (cond
7464      ;; Is there such an article?
7465      ((gnus-summary-display-article 
7466        (gnus-summary-search-forward unread subject backward))
7467       (gnus-summary-position-cursor))
7468      ;; If not, we try the first unread, if that is wanted.
7469      ((and subject
7470            gnus-auto-select-same
7471            (gnus-summary-first-unread-article))
7472       (gnus-message 6 "Wrapped"))
7473      ;; Try to get next/previous article not displayed in this group.
7474      ((and gnus-auto-extend-newsgroup
7475            (not unread) (not subject)
7476            (setq header (gnus-more-header-forward backward)))
7477       (gnus-extend-newsgroup header backward)
7478       (let ((buffer-read-only nil))
7479         (goto-char (if backward (point-min) (point-max)))
7480         (gnus-summary-prepare-threads (list header) 0))
7481       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7482                                    gnus-newsgroup-end)))
7483      ;; Go to next/previous group.
7484      (t
7485       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7486           (gnus-summary-jump-to-group gnus-newsgroup-name))
7487       (let ((cmd (aref (this-command-keys) 0))
7488             (group 
7489              (if (eq gnus-keep-same-level 'best) 
7490                  (gnus-summary-best-group gnus-newsgroup-name)
7491                (gnus-summary-search-group backward gnus-keep-same-level))))
7492         ;; Keep just the event type of CMD.
7493         (and (listp cmd) (setq cmd (car cmd)))
7494         ;; Select next unread newsgroup automagically.
7495         (cond 
7496          ((not gnus-auto-select-next)
7497           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7498          ((eq gnus-auto-select-next 'quietly)
7499           ;; Select quietly.
7500           (if (assoc 'quit-config (gnus-find-method-for-group 
7501                                    gnus-newsgroup-name))
7502               (gnus-summary-exit)
7503             (gnus-message 7 "No more%s articles (%s)..."
7504                           (if unread " unread" "") 
7505                           (if group (concat "selecting " group)
7506                             "exiting"))
7507             (gnus-summary-next-group nil group backward)))
7508          (t
7509           (let ((keystrokes '(?\C-n ?\C-p))
7510                 key)
7511             (while (or (null key) (memq key keystrokes))
7512               (gnus-message 
7513                7 "No more%s articles%s" (if unread " unread" "")
7514                (if (and group (not (assoc 'quit-config
7515                                           (gnus-find-method-for-group 
7516                                            gnus-newsgroup-name))))
7517                    (format " (Type %s for %s [%s])"
7518                            (single-key-description cmd) group
7519                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7520                  (format " (Type %s to exit %s)"
7521                          (single-key-description cmd)
7522                          gnus-newsgroup-name)))
7523               ;; Confirm auto selection.
7524               (let* ((event (read-event)))
7525                 (setq key (if (listp event) (car event) event))
7526                 (if (member key keystrokes)
7527                     (let ((obuf (current-buffer)))
7528                       (switch-to-buffer gnus-group-buffer)
7529                       (gnus-group-jump-to-group group)
7530                       (execute-kbd-macro (char-to-string key))
7531                       (setq group (gnus-group-group-name))
7532                       (switch-to-buffer obuf)))))
7533             (if (eq key cmd)
7534                 (if (or (not group) (assoc 'quit-config
7535                                            (gnus-find-method-for-group
7536                                             gnus-newsgroup-name)))
7537                     (gnus-summary-exit)
7538                   (gnus-summary-next-group nil group backward))
7539               (setq unread-command-events (list key)))))))))))
7540
7541 (defun gnus-summary-next-unread-article ()
7542   "Select unread article after current one."
7543   (interactive)
7544   (gnus-summary-next-article t (and gnus-auto-select-same
7545                                     (gnus-summary-subject-string))))
7546
7547 (defun gnus-summary-prev-article (unread &optional subject)
7548   "Select the article after the current one.
7549 If UNREAD is non-nil, only unread articles are selected."
7550   (interactive "P")
7551   (gnus-summary-next-article unread subject t))
7552
7553 (defun gnus-summary-prev-unread-article ()
7554   "Select unred article before current one."
7555   (interactive)
7556   (gnus-summary-prev-article t (and gnus-auto-select-same
7557                                     (gnus-summary-subject-string))))
7558
7559 (defun gnus-summary-next-page (lines &optional circular)
7560   "Show next page of selected article.
7561 If end of article, select next article.
7562 Argument LINES specifies lines to be scrolled up.
7563 If CIRCULAR is non-nil, go to the start of the article instead of 
7564 instead of selecting the next article when reaching the end of the
7565 current article." 
7566   (interactive "P")
7567   (setq gnus-summary-buffer (current-buffer))
7568   (gnus-set-global-variables)
7569   (let ((article (gnus-summary-article-number))
7570         (endp nil))
7571     (if (or (null gnus-current-article)
7572             (null gnus-article-current)
7573             (/= article (cdr gnus-article-current))
7574             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7575         ;; Selected subject is different from current article's.
7576         (gnus-summary-display-article article)
7577       (gnus-eval-in-buffer-window
7578        gnus-article-buffer
7579        (setq endp (gnus-article-next-page lines)))
7580       (if endp
7581           (cond (circular
7582                  (gnus-summary-beginning-of-article))
7583                 (lines
7584                  (gnus-message 3 "End of message"))
7585                 ((null lines)
7586                  (gnus-summary-next-unread-article)))))
7587     (gnus-configure-windows 'article)
7588     (gnus-summary-recenter)
7589     (gnus-summary-position-cursor)))
7590
7591
7592 (defun gnus-summary-prev-page (lines)
7593   "Show previous page of selected article.
7594 Argument LINES specifies lines to be scrolled down."
7595   (interactive "P")
7596   (gnus-set-global-variables)
7597   (let ((article (gnus-summary-article-number)))
7598     (if (or (null gnus-current-article)
7599             (null gnus-article-current)
7600             (/= article (cdr gnus-article-current))
7601             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7602         ;; Selected subject is different from current article's.
7603         (gnus-summary-display-article article)
7604       (gnus-summary-recenter)
7605       (gnus-eval-in-buffer-window gnus-article-buffer
7606         (gnus-article-prev-page lines))))
7607   (gnus-configure-windows 'article)
7608   (gnus-summary-position-cursor))
7609
7610 (defun gnus-summary-scroll-up (lines)
7611   "Scroll up (or down) one line current article.
7612 Argument LINES specifies lines to be scrolled up (or down if negative)."
7613   (interactive "p")
7614   (gnus-set-global-variables)
7615   (or (gnus-summary-select-article nil nil 'pseudo)
7616       (gnus-eval-in-buffer-window 
7617        gnus-article-buffer
7618        (cond ((> lines 0)
7619               (if (gnus-article-next-page lines)
7620                   (gnus-message 3 "End of message")))
7621              ((< lines 0)
7622               (gnus-article-prev-page (- lines))))))
7623   (gnus-summary-recenter)
7624   (gnus-summary-position-cursor))
7625
7626 (defun gnus-summary-next-same-subject ()
7627   "Select next article which has the same subject as current one."
7628   (interactive)
7629   (gnus-set-global-variables)
7630   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7631
7632 (defun gnus-summary-prev-same-subject ()
7633   "Select previous article which has the same subject as current one."
7634   (interactive)
7635   (gnus-set-global-variables)
7636   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7637
7638 (defun gnus-summary-next-unread-same-subject ()
7639   "Select next unread article which has the same subject as current one."
7640   (interactive)
7641   (gnus-set-global-variables)
7642   (gnus-summary-next-article t (gnus-summary-subject-string)))
7643
7644 (defun gnus-summary-prev-unread-same-subject ()
7645   "Select previous unread article which has the same subject as current one."
7646   (interactive)
7647   (gnus-set-global-variables)
7648   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7649
7650 (defun gnus-summary-first-unread-article ()
7651   "Select the first unread article. 
7652 Return nil if there are no unread articles."
7653   (interactive)
7654   (gnus-set-global-variables)
7655   (prog1
7656       (if (gnus-summary-first-subject t)
7657           (gnus-summary-display-article (gnus-summary-article-number)))
7658     (gnus-summary-position-cursor)))
7659
7660 (defun gnus-summary-best-unread-article ()
7661   "Select the unread article with the highest score."
7662   (interactive)
7663   (gnus-set-global-variables)
7664   (let ((scored gnus-newsgroup-scored)
7665         (best -1000000)
7666         article art)
7667     (while scored
7668       (or (> best (cdr (car scored)))
7669           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7670                (not (memq art gnus-newsgroup-marked))
7671                (not (memq art gnus-newsgroup-dormant))
7672                (if (= best (cdr (car scored)))
7673                    (setq article (min art article))
7674                  (setq article art)
7675                  (setq best (cdr (car scored))))))
7676       (setq scored (cdr scored)))
7677     (if article 
7678         (gnus-summary-goto-article article)
7679       (gnus-summary-first-unread-article))
7680     (gnus-summary-position-cursor)))
7681
7682 (defun gnus-summary-goto-article (article &optional all-headers)
7683   "Fetch ARTICLE and display it if it exists.
7684 If ALL-HEADERS is non-nil, no header lines are hidden."
7685   (interactive
7686    (list
7687     (string-to-int
7688      (completing-read 
7689       "Article number: "
7690       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7691               gnus-newsgroup-headers) 
7692       nil 'require-match))))
7693   (prog1
7694       (and (gnus-summary-goto-subject article)
7695            (gnus-summary-display-article article all-headers))
7696     (gnus-summary-position-cursor)))
7697
7698 (defun gnus-summary-goto-last-article ()
7699   "Go to the last article."
7700   (interactive)
7701   (prog1
7702       (and gnus-last-article
7703            (gnus-summary-goto-article gnus-last-article))
7704     (gnus-summary-position-cursor)))
7705
7706 (defun gnus-summary-pop-article (number)
7707   "Pop one article off the history and go to the previous.
7708 NUMBER articles will be popped off."
7709   (interactive "p")
7710   (let (to)
7711     (setq gnus-newsgroup-history
7712           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7713     (if to
7714         (gnus-summary-goto-article (car to))
7715       (error "Article history empty")))
7716   (gnus-summary-position-cursor))
7717
7718 ;; Summary article oriented commands
7719
7720 (defun gnus-summary-refer-parent-article (n)
7721   "Refer parent article N times.
7722 The difference between N and the number of articles fetched is returned."
7723   (interactive "p")
7724   (gnus-set-global-variables)
7725   (while 
7726       (and 
7727        (> n 0)
7728        (let ((ref (header-references (gnus-get-header-by-number
7729                                       (gnus-summary-article-number)))))
7730          (if (and ref (not (equal ref ""))
7731                   (string-match "<[^<>]*>[ \t]*$" ref))
7732              (gnus-summary-refer-article 
7733               (substring ref (match-beginning 0) (match-end 0))))))
7734     (setq n (1- n)))
7735   (or (zerop n) 
7736       (gnus-message 1 "No references in article or expired article."))
7737   (gnus-summary-position-cursor)
7738   n)
7739     
7740 (defun gnus-summary-refer-article (message-id)
7741   "Refer article specified by MESSAGE-ID.
7742 NOTE: This command only works with newsgroup that use NNTP."
7743   (interactive "sMessage-ID: ")
7744   (if (or (not (stringp message-id))
7745           (zerop (length message-id)))
7746       ()
7747     ;; Construct the correct Message-ID if necessary.
7748     ;; Suggested by tale@pawl.rpi.edu.
7749     (or (string-match "^<" message-id)
7750         (setq message-id (concat "<" message-id)))
7751     (or (string-match ">$" message-id)
7752         (setq message-id (concat message-id ">")))
7753     (let ((header (car (gnus-gethash (downcase message-id)
7754                                      gnus-newsgroup-dependencies))))
7755       (if header
7756           (or (gnus-summary-goto-article (header-number header))
7757               ;; The header has been read, but the article had been
7758               ;; expunged, so we insert it again.
7759               (progn
7760                 (gnus-summary-insert-line
7761                  nil header 0 nil gnus-read-mark nil nil
7762                  (header-subject header))
7763                 (forward-line -1)
7764                 (header-number header)))
7765         (let ((gnus-override-method gnus-refer-article-method)
7766               (gnus-ancient-mark gnus-read-mark)
7767               number)
7768           (and gnus-refer-article-method
7769                (or (gnus-server-opened gnus-refer-article-method)
7770                    (gnus-open-server gnus-refer-article-method)))
7771           (if (gnus-article-prepare 
7772                message-id nil (gnus-read-header message-id))
7773               (progn
7774                 (setq number (header-number gnus-current-headers))
7775                 (gnus-rebuild-thread message-id)
7776                 (gnus-summary-goto-subject number)
7777                 (gnus-article-set-window-start 
7778                  (cdr (assq number gnus-newsgroup-bookmarks)))
7779                 message-id)
7780             (gnus-message 1 "No such references")
7781             nil))))))
7782
7783 (defun gnus-summary-enter-digest-group ()
7784   "Enter a digest group based on the current article."
7785   (interactive)
7786   (gnus-set-global-variables)
7787   (gnus-summary-select-article)
7788   ;; We do not want a narrowed article.
7789   (gnus-summary-stop-page-breaking)
7790   (let ((name (format "%s-%d" 
7791                       (gnus-group-prefixed-name 
7792                        gnus-newsgroup-name (list 'nndoc "")) 
7793                       gnus-current-article))
7794         (buf (current-buffer)))
7795     (if (gnus-group-read-ephemeral-group 
7796          name (list 'nndoc name
7797                     (list 'nndoc-address (get-buffer gnus-article-buffer))
7798                     '(nndoc-article-type digest))
7799          t)
7800         ()
7801       (switch-to-buffer buf)
7802       (gnus-set-global-variables)
7803       (gnus-configure-windows 'summary)
7804       (gnus-message 3 "Article not a digest?"))))
7805
7806 (defun gnus-summary-isearch-article ()
7807   "Do incremental search forward on current article."
7808   (interactive)
7809   (gnus-set-global-variables)
7810   (gnus-summary-select-article)
7811   (gnus-eval-in-buffer-window 
7812    gnus-article-buffer (isearch-forward)))
7813
7814 (defun gnus-summary-search-article-forward (regexp)
7815   "Search for an article containing REGEXP forward.
7816 gnus-select-article-hook is not called during the search."
7817   (interactive
7818    (list (read-string
7819           (concat "Search forward (regexp): "
7820                   (if gnus-last-search-regexp
7821                       (concat "(default " gnus-last-search-regexp ") "))))))
7822   (gnus-set-global-variables)
7823   (if (string-equal regexp "")
7824       (setq regexp (or gnus-last-search-regexp ""))
7825     (setq gnus-last-search-regexp regexp))
7826   (if (gnus-summary-search-article regexp nil)
7827       (gnus-eval-in-buffer-window 
7828        gnus-article-buffer
7829        (recenter 0))
7830     (error "Search failed: \"%s\"" regexp)))
7831
7832 (defun gnus-summary-search-article-backward (regexp)
7833   "Search for an article containing REGEXP backward.
7834 gnus-select-article-hook is not called during the search."
7835   (interactive
7836    (list (read-string
7837           (concat "Search backward (regexp): "
7838                   (if gnus-last-search-regexp
7839                       (concat "(default " gnus-last-search-regexp ") "))))))
7840   (gnus-set-global-variables)
7841   (if (string-equal regexp "")
7842       (setq regexp (or gnus-last-search-regexp ""))
7843     (setq gnus-last-search-regexp regexp))
7844   (if (gnus-summary-search-article regexp t)
7845       (gnus-eval-in-buffer-window
7846        gnus-article-buffer
7847        (recenter 0))
7848     (error "Search failed: \"%s\"" regexp)))
7849
7850 (defun gnus-summary-search-article (regexp &optional backward)
7851   "Search for an article containing REGEXP.
7852 Optional argument BACKWARD means do search for backward.
7853 gnus-select-article-hook is not called during the search."
7854   (let ((gnus-select-article-hook nil)  ;Disable hook.
7855         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7856         (re-search
7857          (if backward
7858              (function re-search-backward) (function re-search-forward)))
7859         (found nil)
7860         (last nil))
7861     ;; Hidden thread subtrees must be searched for ,too.
7862     (gnus-summary-show-all-threads)
7863     (if (eobp) (forward-line -1))
7864     ;; First of all, search current article.
7865     ;; We don't want to read article again from NNTP server nor reset
7866     ;; current point.
7867     (gnus-summary-select-article)
7868     (gnus-message 9 "Searching article: %d..." gnus-current-article)
7869     (setq last gnus-current-article)
7870     (gnus-eval-in-buffer-window gnus-article-buffer
7871       (save-restriction
7872         (widen)
7873         ;; Begin search from current point.
7874         (setq found (funcall re-search regexp nil t))))
7875     ;; Then search next articles.
7876     (while (and (not found)
7877                 (gnus-summary-display-article 
7878                  (gnus-summary-search-subject backward nil nil)))
7879       (gnus-message 9 "Searching article: %d..." gnus-current-article)
7880       (gnus-eval-in-buffer-window gnus-article-buffer
7881         (save-restriction
7882           (widen)
7883           (goto-char (if backward (point-max) (point-min)))
7884           (setq found (funcall re-search regexp nil t)))))
7885     (message "")
7886     ;; Adjust article pointer.
7887     (or (eq last gnus-current-article)
7888         (setq gnus-last-article last))
7889     ;; Return T if found such article.
7890     found))
7891
7892 (defun gnus-summary-execute-command (header regexp command &optional backward)
7893   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7894 If HEADER is an empty string (or nil), the match is done on the entire
7895 article. If BACKWARD (the prefix) is non-nil, search backward instead."
7896   (interactive
7897    (list (let ((completion-ignore-case t))
7898            (completing-read 
7899             "Header name: "
7900             (mapcar (lambda (string) (list string))
7901                     '("Number" "Subject" "From" "Lines" "Date"
7902                       "Message-ID" "Xref" "References"))
7903             nil 'require-match))
7904          (read-string "Regexp: ")
7905          (read-key-sequence "Command: ")
7906          current-prefix-arg))
7907   (gnus-set-global-variables)
7908   ;; Hidden thread subtrees must be searched as well.
7909   (gnus-summary-show-all-threads)
7910   ;; We don't want to change current point nor window configuration.
7911   (save-excursion
7912     (save-window-excursion
7913       (gnus-message 6 "Executing %s..." (key-description command))
7914       ;; We'd like to execute COMMAND interactively so as to give arguments.
7915       (gnus-execute header regexp
7916                     (` (lambda ()
7917                          (call-interactively '(, (key-binding command)))))
7918                     backward)
7919       (gnus-message 6 "Executing %s...done" (key-description command)))))
7920
7921 (defun gnus-summary-beginning-of-article ()
7922   "Scroll the article back to the beginning."
7923   (interactive)
7924   (gnus-set-global-variables)
7925   (gnus-summary-select-article)
7926   (gnus-eval-in-buffer-window
7927    gnus-article-buffer
7928    (widen)
7929    (goto-char (point-min))
7930    (and gnus-break-pages (gnus-narrow-to-page))))
7931
7932 (defun gnus-summary-end-of-article ()
7933   "Scroll to the end of the article."
7934   (interactive)
7935   (gnus-set-global-variables)
7936   (gnus-summary-select-article)
7937   (gnus-eval-in-buffer-window 
7938    gnus-article-buffer
7939    (widen)
7940    (goto-char (point-max))
7941    (and gnus-break-pages (gnus-narrow-to-page))))
7942
7943 (defun gnus-summary-show-article ()
7944   "Force re-fetching of the current article."
7945   (interactive)
7946   (gnus-set-global-variables)
7947   (gnus-summary-select-article gnus-have-all-headers t))
7948
7949 (defun gnus-summary-toggle-header (arg)
7950   "Show the headers if they are hidden, or hide them if they are shown.
7951 If ARG is a positive number, show the entire header.
7952 If ARG is a negative number, hide the unwanted header lines."
7953   (interactive "P")
7954   (gnus-set-global-variables)
7955   (save-excursion
7956     (set-buffer gnus-article-buffer)
7957     (let ((buffer-read-only nil))
7958       (if (numberp arg) 
7959           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
7960                                                 gnus-hidden-properties)
7961             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
7962         (if (text-property-any (point-min) (point-max) 'invisible t)
7963             (remove-text-properties (point-min) (point-max)
7964                                     gnus-hidden-properties)
7965           (let ((gnus-have-all-headers nil))
7966             (run-hooks 'gnus-article-display-hook))))
7967       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
7968
7969 (defun gnus-summary-show-all-headers ()
7970   "Make all header lines visible."
7971   (interactive)
7972   (gnus-set-global-variables)
7973   (gnus-article-show-all-headers))
7974
7975 (defun gnus-summary-toggle-mime (arg)
7976   "Toggle MIME processing.
7977 If ARG is a positive number, turn MIME processing on."
7978   (interactive "P")
7979   (gnus-set-global-variables)
7980   (setq gnus-show-mime
7981         (if (null arg) (not gnus-show-mime)
7982           (> (prefix-numeric-value arg) 0)))
7983   (gnus-summary-select-article t 'force))
7984
7985 (defun gnus-summary-caesar-message (rotnum)
7986   "Caesar rotates all letters of current message by 13/47 places.
7987 With prefix arg, specifies the number of places to rotate each letter forward.
7988 Caesar rotates Japanese letters by 47 places in any case."
7989   (interactive "P")
7990   (gnus-set-global-variables)
7991   (gnus-summary-select-article)
7992   (let ((mail-header-separator "")) ; !!! Is this necessary?
7993     (gnus-overload-functions)
7994     (gnus-eval-in-buffer-window 
7995      gnus-article-buffer
7996      (save-restriction
7997        (widen)
7998        ;; We don't want to jump to the beginning of the message.
7999        ;; `save-excursion' does not do its job.
8000        (move-to-window-line 0)
8001        (let ((last (point)))
8002          (news-caesar-buffer-body rotnum)
8003          (goto-char last)
8004          (recenter 0))))))
8005
8006 (defun gnus-summary-stop-page-breaking ()
8007   "Stop page breaking in the current article."
8008   (interactive)
8009   (gnus-set-global-variables)
8010   (gnus-summary-select-article)
8011   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8012
8013 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8014
8015 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8016   "Move the current article to a different newsgroup.
8017 If N is a positive number, move the N next articles.
8018 If N is a negative number, move the N previous articles.
8019 If N is nil and any articles have been marked with the process mark,
8020 move those articles instead.
8021 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8022 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8023 re-spool using this method.
8024 For this function to work, both the current newsgroup and the
8025 newsgroup that you want to move to have to support the `request-move'
8026 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8027   (interactive "P")
8028   (gnus-set-global-variables)
8029   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8030       (error "The current newsgroup does not support article moving"))
8031   (let ((articles (gnus-summary-work-articles n))
8032         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8033         art-group)
8034     (if (and (not to-newsgroup) (not select-method))
8035         (setq to-newsgroup
8036               (completing-read 
8037                (format "Where do you want to move %s? %s"
8038                        (if (> (length articles) 1)
8039                            (format "these %d articles" (length articles))
8040                          "this article")
8041                        (if gnus-current-move-group
8042                            (format "(%s default) " gnus-current-move-group)
8043                          ""))
8044                gnus-active-hashtb nil nil prefix)))
8045     (if to-newsgroup
8046         (progn
8047           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8048               (setq to-newsgroup (or gnus-current-move-group "")))
8049           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8050               (gnus-activate-newsgroup to-newsgroup)
8051               (error "No such group: %s" to-newsgroup))
8052           (setq gnus-current-move-group to-newsgroup)))
8053     (or (gnus-check-backend-function 'request-accept-article 
8054                                      (or select-method to-newsgroup))
8055         (error "%s does not support article moving" to-newsgroup))
8056     (gnus-message 6 "Moving to %s: %s..." 
8057                   (or select-method to-newsgroup) articles)
8058     (while articles
8059       (if (setq art-group
8060                 (gnus-request-move-article 
8061                  (car articles)                   ; Article to move
8062                  gnus-newsgroup-name              ; From newsgrouo
8063                  (nth 1 (gnus-find-method-for-group 
8064                          gnus-newsgroup-name))    ; Server
8065                  (list 'gnus-request-accept-article 
8066                        (if select-method
8067                            (list 'quote select-method)
8068                          to-newsgroup)
8069                        (not (cdr articles)))     ; Accept form
8070                  (not (cdr articles))))          ; Only save nov last time
8071           (let* ((buffer-read-only nil)
8072                  (entry 
8073                   (or
8074                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8075                    (gnus-gethash 
8076                     (gnus-group-prefixed-name 
8077                      (car art-group) 
8078                      (if select-method (list select-method "")
8079                        (gnus-find-method-for-group to-newsgroup)))
8080                     gnus-newsrc-hashtb)))
8081                  (info (nth 2 entry))
8082                  (article (car articles))
8083                  (marked (nth 3 info)))
8084             (gnus-summary-goto-subject article)
8085             (beginning-of-line)
8086             (delete-region (point)
8087                            (progn (forward-line 1) (point)))
8088             (if (not (memq article gnus-newsgroup-unreads))
8089                 (setcar (cdr (cdr info))
8090                         (gnus-add-to-range (nth 2 info) 
8091                                            (list (cdr art-group)))))
8092             ;; Copy any marks over to the new group.
8093             (let ((marks '((tick . gnus-newsgroup-marked)
8094                            (dormant . gnus-newsgroup-dormant)
8095                            (expire . gnus-newsgroup-expirable)
8096                            (bookmark . gnus-newsgroup-bookmarks)
8097                         ;   (score . gnus-newsgroup-scored)
8098                            (reply . gnus-newsgroup-replied)))
8099                   (to-article (cdr art-group)))
8100               (while marks
8101                 (if (memq article (symbol-value (cdr (car marks))))
8102                     (gnus-add-marked-articles 
8103                      (car info) (car (car marks)) (list to-article) info))
8104                 (setq marks (cdr marks))))
8105             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8106             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8107             (setq gnus-newsgroup-dormant
8108                   (delq article gnus-newsgroup-dormant)))
8109         (gnus-message 1 "Couldn't move article %s" (car articles)))
8110       (gnus-summary-remove-process-mark (car articles))
8111       (setq articles (cdr articles)))))
8112
8113 (defun gnus-summary-respool-article (n &optional respool-method)
8114   "Respool the current article.
8115 The article will be squeezed through the mail spooling process again,
8116 which means that it will be put in some mail newsgroup or other
8117 depending on `nnmail-split-methods'.
8118 If N is a positive number, respool the N next articles.
8119 If N is a negative number, respool the N previous articles.
8120 If N is nil and any articles have been marked with the process mark,
8121 respool those articles instead.
8122
8123 Respooling can be done both from mail groups and \"real\" newsgroups.
8124 In the former case, the articles in question will be moved from the
8125 current group into whatever groups they are destined to.  In the
8126 latter case, they will be copied into the relevant groups."
8127   (interactive "P")
8128   (gnus-set-global-variables)
8129   (let ((respool-methods (gnus-methods-using 'respool))
8130         (methname 
8131          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8132     (or respool-method
8133         (setq respool-method
8134               (completing-read
8135                "What method do you want to use when respooling? "
8136                respool-methods nil t methname)))
8137     (or (string= respool-method "")
8138         (if (assoc (symbol-name
8139                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8140                    respool-methods)
8141             (gnus-summary-move-article n nil (intern respool-method))
8142           (gnus-summary-copy-article n nil (intern respool-method))))))
8143
8144 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8145 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8146   "Move the current article to a different newsgroup.
8147 If N is a positive number, move the N next articles.
8148 If N is a negative number, move the N previous articles.
8149 If N is nil and any articles have been marked with the process mark,
8150 move those articles instead.
8151 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8152 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8153 re-spool using this method.
8154 For this function to work, the newsgroup that you want to move to have
8155 to support the `request-move' and `request-accept'
8156 functions. (Ie. mail newsgroups at present.)"
8157   (interactive "P")
8158   (gnus-set-global-variables)
8159   (let ((articles (gnus-summary-work-articles n))
8160         (copy-buf (get-buffer-create "*copy work*"))
8161         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8162         art-group)
8163     (buffer-disable-undo copy-buf)
8164     (if (and (not to-newsgroup) (not select-method))
8165         (setq to-newsgroup
8166               (completing-read 
8167                (format "Where do you want to copy %s? %s"
8168                        (if (> (length articles) 1)
8169                            (format "these %d articles" (length articles))
8170                          "this article")
8171                        (if gnus-current-move-group
8172                            (format "(%s default) " gnus-current-move-group)
8173                          ""))
8174                gnus-active-hashtb nil nil prefix)))
8175     (if to-newsgroup
8176         (progn
8177           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8178               (setq to-newsgroup (or gnus-current-move-group "")))
8179           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8180               (gnus-activate-newsgroup to-newsgroup)
8181               (error "No such group: %s" to-newsgroup))
8182           (setq gnus-current-move-group to-newsgroup)))
8183     (or (gnus-check-backend-function 'request-accept-article 
8184                                      (or select-method to-newsgroup))
8185         (error "%s does not support article copying" to-newsgroup))
8186     (gnus-message 6 "Copying to %s: %s..." 
8187                   (or select-method to-newsgroup) articles)
8188     (while articles
8189       (if (setq art-group
8190                 (save-excursion
8191                   (set-buffer copy-buf)
8192                   (gnus-request-article-this-buffer
8193                    (car articles) gnus-newsgroup-name)
8194                   (gnus-request-accept-article
8195                    (if select-method (quote select-method) to-newsgroup)
8196                    (not (cdr articles)))))
8197           (let* ((entry 
8198                   (or
8199                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8200                    (gnus-gethash 
8201                     (gnus-group-prefixed-name 
8202                      (car art-group) 
8203                      (if select-method (list select-method "")
8204                        (gnus-find-method-for-group to-newsgroup)))
8205                     gnus-newsrc-hashtb)))
8206                  (info (nth 2 entry))
8207                  (article (car articles))
8208                  (marked (nth 3 info)))
8209             (if (not (memq article gnus-newsgroup-unreads))
8210                 (setcar (cdr (cdr info))
8211                         (gnus-add-to-range (nth 2 info) 
8212                                            (list (cdr art-group)))))
8213             ;; Copy any marks over to the new group.
8214             (let ((marks '((tick . gnus-newsgroup-marked)
8215                            (dormant . gnus-newsgroup-dormant)
8216                            (expire . gnus-newsgroup-expirable)
8217                            (bookmark . gnus-newsgroup-bookmarks)
8218                         ;   (score . gnus-newsgroup-scored)
8219                            (reply . gnus-newsgroup-replied)))
8220                   (to-article (cdr art-group)))
8221               (while marks
8222                 (if (memq article (symbol-value (cdr (car marks))))
8223                     (gnus-add-marked-articles 
8224                      (car info) (car (car marks)) (list to-article) info))
8225                 (setq marks (cdr marks)))))
8226         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8227       (gnus-summary-remove-process-mark (car articles))
8228       (setq articles (cdr articles)))
8229     (kill-buffer copy-buf)))
8230
8231 (defun gnus-summary-import-article (file)
8232   "Import a random file into a mail newsgroup."
8233   (interactive "fImport file: ")
8234   (let ((group gnus-newsgroup-name)
8235         attrib)
8236     (or (gnus-check-backend-function 'request-accept-article group)
8237         (error "%s does not support article importing" group))
8238     (or (file-readable-p file)
8239         (not (file-regular-p file))
8240         (error "Can't read %s" file))
8241     (save-excursion
8242       (set-buffer (get-buffer-create " *import file*"))
8243       (buffer-disable-undo (current-buffer))
8244       (erase-buffer)
8245       (insert-file-contents file)
8246       (goto-char (point-min))
8247       (setq attrib (file-attributes file))
8248       (insert "From: " (read-string "From: ")))))
8249
8250
8251 (defun gnus-summary-expire-articles ()
8252   "Expire all articles that are marked as expirable in the current group."
8253   (interactive)
8254   (if (and gnus-newsgroup-expirable
8255            (gnus-check-backend-function 
8256             'request-expire-articles gnus-newsgroup-name))
8257       (let ((expirable gnus-newsgroup-expirable))
8258         ;; The list of articles that weren't expired is returned.
8259         (setq gnus-newsgroup-expirable 
8260               (gnus-request-expire-articles gnus-newsgroup-expirable
8261                                             gnus-newsgroup-name))
8262         ;; We go through the old list of expirable, and mark all
8263         ;; really expired articles as non-existant.
8264         (while expirable
8265           (or (memq (car expirable) gnus-newsgroup-expirable)
8266               (gnus-summary-mark-as-read (car expirable) "%"))
8267           (setq expirable (cdr expirable))))))
8268
8269 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8270 (defun gnus-summary-delete-article (n)
8271   "Delete the N next (mail) articles.
8272 This command actually deletes articles. This is not a marking
8273 command. The article will disappear forever from you life, never to
8274 return. 
8275 If N is negative, delete backwards.
8276 If N is nil and articles have been marked with the process mark,
8277 delete these instead."
8278   (interactive "P")
8279   (or (gnus-check-backend-function 'request-expire-articles 
8280                                    gnus-newsgroup-name)
8281       (error "The current newsgroup does not support article deletion."))
8282   ;; Compute the list of articles to delete.
8283   (let ((articles (gnus-summary-work-articles n))
8284         not-deleted)
8285     (if (and gnus-novice-user
8286              (not (gnus-y-or-n-p 
8287                    (format "Do you really want to delete %s forever? "
8288                            (if (> (length articles) 1) "these articles"
8289                              "this article")))))
8290         ()
8291       ;; Delete the articles.
8292       (setq not-deleted (gnus-request-expire-articles 
8293                          articles gnus-newsgroup-name 'force))
8294       (while articles
8295         (gnus-summary-remove-process-mark (car articles))       
8296         ;; The backend might not have been able to delete the article
8297         ;; after all.  
8298         (or (memq (car articles) not-deleted)
8299             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8300         (setq articles (cdr articles))))
8301     (gnus-summary-position-cursor)
8302     not-deleted))
8303
8304 (defun gnus-summary-edit-article ()
8305   "Enter into a buffer and edit the current article.
8306 This will have permanent effect only in mail groups."
8307   (interactive)
8308   (or (gnus-check-backend-function 
8309        'request-replace-article gnus-newsgroup-name)
8310       (error "The current newsgroup does not support article editing."))
8311   (gnus-summary-select-article t)
8312   (other-window 1)
8313   (gnus-message 6 "C-c C-c to end edits")
8314   (setq buffer-read-only nil)
8315   (text-mode)
8316   (use-local-map (copy-keymap (current-local-map)))
8317   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8318   (goto-char (point-min))
8319   (search-forward "\n\n" nil t))
8320
8321 (defun gnus-summary-edit-article-done ()
8322   "Make edits to the current article permanent."
8323   (interactive)
8324   (if (not (gnus-request-replace-article 
8325             (cdr gnus-article-current) (car gnus-article-current) 
8326             (current-buffer)))
8327       (error "Couldn't replace article.")
8328     (gnus-article-mode)
8329     (use-local-map gnus-article-mode-map)
8330     (setq buffer-read-only t)
8331     (pop-to-buffer gnus-summary-buffer)))      
8332
8333 (defun gnus-summary-fancy-query ()
8334   "Query where the fancy respool algorithm would put this article."
8335   (interactive)
8336   (gnus-summary-select-article)
8337   (save-excursion
8338     (set-buffer gnus-article-buffer)
8339     (save-restriction
8340       (goto-char (point-min))
8341       (search-forward "\n\n")
8342       (narrow-to-region (point-min) (point))
8343       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8344
8345 ;; Summary score commands.
8346
8347 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8348
8349 (defun gnus-summary-raise-score (n)
8350   "Raise the score of the current article by N."
8351   (interactive "p")
8352   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8353
8354 (defun gnus-summary-lower-score (n)
8355   "Lower the score of the current article by N."
8356   (interactive "p")
8357   (gnus-summary-raise-score (- n)))
8358
8359 (defun gnus-summary-set-score (n)
8360   "Set the score of the current article to N."
8361   (interactive "p")
8362   ;; Skip dummy header line.
8363   (save-excursion
8364     (gnus-summary-show-thread)
8365     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8366         (forward-line 1))
8367     (let ((buffer-read-only nil))
8368       ;; Set score.
8369       (gnus-summary-update-mark
8370        (if (= n (or gnus-summary-default-score 0)) ? 
8371          (if (< n (or gnus-summary-default-score 0)) 
8372              gnus-score-below-mark gnus-score-over-mark)) 'score))
8373     (let* ((article (gnus-summary-article-number))
8374            (score (assq article gnus-newsgroup-scored)))
8375       (if score (setcdr score n)
8376         (setq gnus-newsgroup-scored 
8377               (cons (cons article n) gnus-newsgroup-scored))))
8378     (gnus-summary-update-line)))
8379
8380 (defun gnus-summary-current-score ()
8381   "Return the score of the current article."
8382   (interactive)
8383   (message "%s" (gnus-summary-article-score)))
8384
8385 ;; Summary marking commands.
8386
8387 (defun gnus-summary-raise-same-subject-and-select (score)
8388   "Raise articles which has the same subject with SCORE and select the next."
8389   (interactive "p")
8390   (let ((subject (gnus-summary-subject-string)))
8391     (gnus-summary-raise-score score)
8392     (while (gnus-summary-search-subject nil nil subject)
8393       (gnus-summary-raise-score score))
8394     (gnus-summary-next-article t)))
8395
8396 (defun gnus-summary-raise-same-subject (score)
8397   "Raise articles which has the same subject with SCORE."
8398   (interactive "p")
8399   (let ((subject (gnus-summary-subject-string)))
8400     (gnus-summary-raise-score score)
8401     (while (gnus-summary-search-subject nil nil subject)
8402       (gnus-summary-raise-score score))
8403     (gnus-summary-next-subject 1 t)))
8404
8405 (defun gnus-score-default (level)
8406   (if level (prefix-numeric-value level) 
8407     gnus-score-interactive-default-score))
8408
8409 (defun gnus-summary-raise-thread (score)
8410   "Raise articles under current thread with SCORE."
8411   (interactive "P")
8412   (setq score (1- (gnus-score-default score)))
8413   (let (e)
8414     (save-excursion
8415       (let ((level (gnus-summary-thread-level)))
8416         (gnus-summary-raise-score score)
8417         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8418                     (> (gnus-summary-thread-level) level))
8419           (gnus-summary-raise-score score))
8420         (setq e (point))))
8421     (let ((gnus-summary-check-current t))
8422       (or (zerop (gnus-summary-next-subject 1 t))
8423           (goto-char e))))
8424   (gnus-summary-recenter)
8425   (gnus-summary-position-cursor)
8426   (gnus-set-mode-line 'summary))
8427
8428 (defun gnus-summary-lower-same-subject-and-select (score)
8429   "Raise articles which has the same subject with SCORE and select the next."
8430   (interactive "p")
8431   (gnus-summary-raise-same-subject-and-select (- score)))
8432
8433 (defun gnus-summary-lower-same-subject (score)
8434   "Raise articles which has the same subject with SCORE."
8435   (interactive "p")
8436   (gnus-summary-raise-same-subject (- score)))
8437
8438 (defun gnus-summary-lower-thread (score)
8439   "Raise articles under current thread with SCORE."
8440   (interactive "P")
8441   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8442
8443 (defun gnus-summary-kill-same-subject-and-select (unmark)
8444   "Mark articles which has the same subject as read, and then select the next.
8445 If UNMARK is positive, remove any kind of mark.
8446 If UNMARK is negative, tick articles."
8447   (interactive "P")
8448   (if unmark
8449       (setq unmark (prefix-numeric-value unmark)))
8450   (let ((count
8451          (gnus-summary-mark-same-subject
8452           (gnus-summary-subject-string) unmark)))
8453     ;; Select next unread article. If auto-select-same mode, should
8454     ;; select the first unread article.
8455     (gnus-summary-next-article t (and gnus-auto-select-same
8456                                       (gnus-summary-subject-string)))
8457     (gnus-message 7 "%d articles are marked as %s"
8458                   count (if unmark "unread" "read"))))
8459
8460 (defun gnus-summary-kill-same-subject (unmark)
8461   "Mark articles which has the same subject as read. 
8462 If UNMARK is positive, remove any kind of mark.
8463 If UNMARK is negative, tick articles."
8464   (interactive "P")
8465   (if unmark
8466       (setq unmark (prefix-numeric-value unmark)))
8467   (let ((count
8468          (gnus-summary-mark-same-subject
8469           (gnus-summary-subject-string) unmark)))
8470     ;; If marked as read, go to next unread subject.
8471     (if (null unmark)
8472         ;; Go to next unread subject.
8473         (gnus-summary-next-subject 1 t))
8474     (gnus-message 7 "%d articles are marked as %s"
8475                   count (if unmark "unread" "read"))))
8476
8477 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8478   "Mark articles with same SUBJECT as read, and return marked number.
8479 If optional argument UNMARK is positive, remove any kinds of marks.
8480 If optional argument UNMARK is negative, mark articles as unread instead."
8481   (let ((count 1))
8482     (save-excursion
8483       (cond ((null unmark)
8484              (gnus-summary-mark-as-read nil gnus-killed-mark))
8485             ((> unmark 0)
8486              (gnus-summary-tick-article nil t))
8487             (t
8488              (gnus-summary-tick-article)))
8489       (while (and subject
8490                   (gnus-summary-search-forward nil subject))
8491         (cond ((null unmark)
8492                (gnus-summary-mark-as-read nil gnus-killed-mark))
8493               ((> unmark 0)
8494                (gnus-summary-tick-article nil t))
8495               (t
8496                (gnus-summary-tick-article)))
8497         (setq count (1+ count))))
8498     ;; Hide killed thread subtrees.  Does not work properly always.
8499     ;;(and (null unmark)
8500     ;;     gnus-thread-hide-killed
8501     ;;     (gnus-summary-hide-thread))
8502     ;; Return number of articles marked as read.
8503     count))
8504
8505 (defun gnus-summary-mark-as-processable (n &optional unmark)
8506   "Set the process mark on the next N articles.
8507 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8508 the process mark instead.  The difference between N and the actual
8509 number of articles marked is returned."
8510   (interactive "p")
8511   (let ((backward (< n 0))
8512         (n (abs n)))
8513   (while (and 
8514           (> n 0)
8515           (if unmark
8516               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8517             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8518           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8519     (setq n (1- n)))
8520   (if (/= 0 n) (gnus-message 7 "No more articles"))
8521   (gnus-summary-recenter)
8522   (gnus-summary-position-cursor)
8523   n))
8524
8525 (defun gnus-summary-unmark-as-processable (n)
8526   "Remove the process mark from the next N articles.
8527 If N is negative, mark backward instead.  The difference between N and
8528 the actual number of articles marked is returned."
8529   (interactive "p")
8530   (gnus-summary-mark-as-processable n t))
8531
8532 (defun gnus-summary-unmark-all-processable ()
8533   "Remove the process mark from all articles."
8534   (interactive)
8535   (save-excursion
8536     (while gnus-newsgroup-processable
8537       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8538   (gnus-summary-position-cursor))
8539
8540 (defun gnus-summary-mark-as-expirable (n)
8541   "Mark N articles forward as expirable.
8542 If N is negative, mark backward instead. The difference between N and
8543 the actual number of articles marked is returned."
8544   (interactive "p")
8545   (gnus-summary-mark-forward n gnus-expirable-mark))
8546
8547 (defun gnus-summary-mark-article-as-replied (article)
8548   "Mark ARTICLE replied and update the summary line."
8549   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8550   (let ((buffer-read-only nil))
8551     (if (gnus-summary-goto-subject article)
8552         (progn
8553           (gnus-summary-update-mark gnus-replied-mark 'replied)
8554           t))))
8555
8556 (defun gnus-summary-set-bookmark (article)
8557   "Set a bookmark in current article."
8558   (interactive (list (gnus-summary-article-number)))
8559   (if (or (not (get-buffer gnus-article-buffer))
8560           (not gnus-current-article)
8561           (not gnus-article-current)
8562           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8563       (error "No current article selected"))
8564   ;; Remove old bookmark, if one exists.
8565   (let ((old (assq article gnus-newsgroup-bookmarks)))
8566     (if old (setq gnus-newsgroup-bookmarks 
8567                   (delq old gnus-newsgroup-bookmarks))))
8568   ;; Set the new bookmark, which is on the form 
8569   ;; (article-number . line-number-in-body).
8570   (setq gnus-newsgroup-bookmarks 
8571         (cons 
8572          (cons article 
8573                (save-excursion
8574                  (set-buffer gnus-article-buffer)
8575                  (count-lines
8576                   (min (point)
8577                        (save-excursion
8578                          (goto-char (point-min))
8579                          (search-forward "\n\n" nil t)
8580                          (point)))
8581                   (point))))
8582          gnus-newsgroup-bookmarks))
8583   (gnus-message 6 "A bookmark has been added to the current article."))
8584
8585 (defun gnus-summary-remove-bookmark (article)
8586   "Remove the bookmark from the current article."
8587   (interactive (list (gnus-summary-article-number)))
8588   ;; Remove old bookmark, if one exists.
8589   (let ((old (assq article gnus-newsgroup-bookmarks)))
8590     (if old 
8591         (progn
8592           (setq gnus-newsgroup-bookmarks 
8593                 (delq old gnus-newsgroup-bookmarks))
8594           (gnus-message 6 "Removed bookmark."))
8595       (gnus-message 6 "No bookmark in current article."))))
8596
8597 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8598 (defun gnus-summary-mark-as-dormant (n)
8599   "Mark N articles forward as dormant.
8600 If N is negative, mark backward instead.  The difference between N and
8601 the actual number of articles marked is returned."
8602   (interactive "p")
8603   (gnus-summary-mark-forward n gnus-dormant-mark))
8604
8605 (defun gnus-summary-set-process-mark (article)
8606   "Set the process mark on ARTICLE and update the summary line."
8607   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8608   (let ((buffer-read-only nil))
8609     (if (gnus-summary-goto-subject article)
8610         (progn
8611           (gnus-summary-show-thread)
8612           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8613                (forward-line 1))
8614           (gnus-summary-update-mark gnus-process-mark 'replied)
8615           t))))
8616
8617 (defun gnus-summary-remove-process-mark (article)
8618   "Remove the process mark from ARTICLE and update the summary line."
8619   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8620   (let ((buffer-read-only nil))
8621     (if (gnus-summary-goto-subject article)
8622         (progn
8623           (gnus-summary-show-thread)
8624           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8625                (forward-line 1))
8626           (gnus-summary-update-mark ?  'replied)
8627           (if (memq article gnus-newsgroup-replied) 
8628               (gnus-summary-update-mark gnus-replied-mark 'replied))
8629           t))))
8630
8631 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8632   "Mark N articles as read forwards.
8633 If N is negative, mark backwards instead.
8634 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8635 marked as unread. 
8636 The difference between N and the actual number of articles marked is
8637 returned."
8638   (interactive "p")
8639   (gnus-set-global-variables)
8640   (let ((backward (< n 0))
8641         (n (abs n))
8642         (mark (or mark gnus-del-mark)))
8643   (while (and (> n 0)
8644               (gnus-summary-mark-article nil mark no-expire)
8645               (zerop (gnus-summary-next-subject 
8646                       (if backward -1 1) gnus-summary-goto-unread t)))
8647     (setq n (1- n)))
8648   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8649   (gnus-summary-recenter)
8650   (gnus-summary-position-cursor)
8651   (gnus-set-mode-line 'summary)
8652   n))
8653
8654 (defun gnus-summary-mark-article (&optional article mark no-expire)
8655   "Mark ARTICLE with MARK.
8656 MARK can be any character.
8657 Five MARK strings are reserved: ?  (unread), 
8658 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8659 If MARK is nil, then the default character ?D is used.
8660 If ARTICLE is nil, then the article on the current line will be
8661 marked." 
8662   ;; If no mark is given, then we check auto-expiring.
8663   (and (not no-expire)
8664        gnus-newsgroup-auto-expire 
8665        (or (not mark)
8666            (and (numberp mark) (or (= mark gnus-killed-mark)
8667                                    (= mark gnus-del-mark)
8668                                    (= mark gnus-catchup-mark)
8669                                    (= mark gnus-low-score-mark)
8670                                    (= mark gnus-read-mark))))
8671        (setq mark gnus-expirable-mark))
8672   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8673          (article (or article (gnus-summary-article-number))))
8674     (if (or (= mark gnus-unread-mark) 
8675             (= mark gnus-ticked-mark) 
8676             (= mark gnus-dormant-mark))
8677         (gnus-mark-article-as-unread article mark)
8678       (gnus-mark-article-as-read article mark))
8679
8680     ;; See whether the article is to be put in the cache.
8681     (and gnus-use-cache
8682          (save-excursion
8683            (gnus-cache-possibly-enter-article 
8684             gnus-newsgroup-name article 
8685             (gnus-get-header-by-number article)
8686             (= mark gnus-ticked-mark)
8687             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8688
8689     (if (gnus-summary-goto-subject article)
8690         (let ((buffer-read-only nil))
8691           (gnus-summary-show-thread)
8692           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8693                (forward-line 1))
8694           ;; Fix the mark.
8695           (gnus-summary-update-mark mark 'unread)
8696           t))))
8697
8698 (defun gnus-summary-update-mark (mark type)
8699   (beginning-of-line)
8700   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8701         plist)
8702     (if (not forward)
8703         ()
8704       (forward-char forward)
8705       (setq plist (text-properties-at (point)))
8706       (delete-char 1)
8707       (insert mark)
8708       (and plist (add-text-properties (1- (point)) (point) plist))
8709       (add-text-properties (1- (point)) (point) (list 'gnus-mark mark))
8710       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8711   
8712 (defun gnus-mark-article-as-read (article &optional mark)
8713   "Enter ARTICLE in the pertinent lists and remove it from others."
8714   ;; Make the article expirable.
8715   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8716     (if (= mark gnus-expirable-mark)
8717         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8718       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8719     ;; Remove from unread and marked lists.
8720     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8721     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8722     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))))
8723
8724 (defun gnus-mark-article-as-unread (article &optional mark)
8725   "Enter ARTICLE in the pertinent lists and remove it from others."
8726   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8727     ;; Add to unread list.
8728     (or (memq article gnus-newsgroup-unreads)
8729         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
8730     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
8731     ;; lists.  Otherwise, it must be added to the list.
8732     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8733     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8734     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8735     (if (= mark gnus-ticked-mark)
8736         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
8737     (if (= mark gnus-dormant-mark)
8738         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
8739
8740 (defalias 'gnus-summary-mark-as-unread-forward 
8741   'gnus-summary-tick-article-forward)
8742 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8743                'gnus-summary-tick-article-forward)
8744 (defun gnus-summary-tick-article-forward (n)
8745   "Tick N articles forwards.
8746 If N is negative, tick backwards instead.
8747 The difference between N and the number of articles ticked is returned."
8748   (interactive "p")
8749   (gnus-summary-mark-forward n gnus-ticked-mark))
8750
8751 (defalias 'gnus-summary-mark-as-unread-backward 
8752   'gnus-summary-tick-article-backward)
8753 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8754                'gnus-summary-tick-article-backward)
8755 (defun gnus-summary-tick-article-backward (n)
8756   "Tick N articles backwards.
8757 The difference between N and the number of articles ticked is returned."
8758   (interactive "p")
8759   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8760
8761 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8762 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8763 (defun gnus-summary-tick-article (&optional article clear-mark)
8764   "Mark current article as unread.
8765 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8766 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8767   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8768                                        gnus-ticked-mark)))
8769
8770 (defun gnus-summary-mark-as-read-forward (n)
8771   "Mark N articles as read forwards.
8772 If N is negative, mark backwards instead.
8773 The difference between N and the actual number of articles marked is
8774 returned."
8775   (interactive "p")
8776   (gnus-summary-mark-forward n gnus-del-mark t))
8777
8778 (defun gnus-summary-mark-as-read-backward (n)
8779   "Mark the N articles as read backwards.
8780 The difference between N and the actual number of articles marked is
8781 returned."
8782   (interactive "p")
8783   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8784
8785 (defun gnus-summary-mark-as-read (&optional article mark)
8786   "Mark current article as read.
8787 ARTICLE specifies the article to be marked as read.
8788 MARK specifies a string to be inserted at the beginning of the line."
8789   (gnus-summary-mark-article article mark))
8790
8791 (defun gnus-summary-clear-mark-forward (n)
8792   "Clear marks from N articles forward.
8793 If N is negative, clear backward instead.
8794 The difference between N and the number of marks cleared is returned."
8795   (interactive "p")
8796   (gnus-summary-mark-forward n gnus-unread-mark))
8797
8798 (defun gnus-summary-clear-mark-backward (n)
8799   "Clear marks from N articles backward.
8800 The difference between N and the number of marks cleared is returned."
8801   (interactive "p")
8802   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8803
8804 (defun gnus-summary-mark-unread-as-read ()
8805   "Intended to be used by `gnus-summary-mark-article-hook'."
8806   (or (memq gnus-current-article gnus-newsgroup-marked)
8807       (memq gnus-current-article gnus-newsgroup-dormant)
8808       (memq gnus-current-article gnus-newsgroup-expirable)
8809       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8810
8811 (defun gnus-summary-mark-region-as-read (point mark all)
8812   "Mark all unread articles between point and mark as read.
8813 If given a prefix, mark all articles between point and mark as read,
8814 even ticked and dormant ones."
8815   (interactive "r\nP")
8816   (save-excursion
8817     (goto-char point)
8818     (beginning-of-line)
8819     (while (and 
8820             (< (point) mark)
8821             (progn
8822               (and
8823                (or all
8824                    (and
8825                     (not (memq (gnus-summary-article-number)
8826                                gnus-newsgroup-marked))
8827                     (not (memq (gnus-summary-article-number)
8828                                gnus-newsgroup-dormant))))
8829                (gnus-summary-mark-article
8830                 (gnus-summary-article-number) gnus-del-mark))
8831               t)
8832             (zerop (forward-line 1))))))
8833
8834 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8835 (defalias 'gnus-summary-delete-marked-as-read 
8836   'gnus-summary-remove-lines-marked-as-read)
8837 (make-obsolete 'gnus-summary-delete-marked-as-read 
8838                'gnus-summary-remove-lines-marked-as-read)
8839 (defun gnus-summary-remove-lines-marked-as-read ()
8840   "Remove lines that are marked as read."
8841   (interactive)
8842   (gnus-summary-remove-lines-marked-with 
8843    (concat (mapconcat
8844             (lambda (char) (char-to-string (symbol-value char)))
8845             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
8846               gnus-killed-mark gnus-kill-file-mark
8847               gnus-low-score-mark gnus-expirable-mark)
8848             ""))))
8849
8850 (defalias 'gnus-summary-delete-marked-with 
8851   'gnus-summary-remove-lines-marked-with)
8852 (make-obsolete 'gnus-summary-delete-marked-with 
8853                'gnus-summary-remove-lines-marked-with)
8854 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8855 (defun gnus-summary-remove-lines-marked-with (marks)
8856   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8857   (interactive "sMarks: ")
8858   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8859   (gnus-set-global-variables)
8860   (let ((buffer-read-only nil)
8861         (marks (concat "^[" marks "]")))
8862     (goto-char (point-min))
8863     (if gnus-newsgroup-adaptive
8864         (gnus-score-remove-lines-adaptive marks)
8865       (while (re-search-forward marks nil t)
8866         (gnus-delete-line)))
8867     ;; If we use dummy roots, we have to do an additional sweep over
8868     ;; the buffer.
8869     (if (not (eq gnus-summary-make-false-root 'dummy))
8870         ()
8871       (goto-char (point-min))
8872       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
8873       (while (re-search-forward marks nil t)
8874         (if (gnus-subject-equal
8875              (gnus-summary-subject-string)
8876              (progn
8877                (forward-line 1)
8878                (gnus-summary-subject-string)))
8879             ()
8880           (forward-line -1)
8881           (gnus-delete-line))))))
8882   (or (zerop (buffer-size))
8883       (if (eobp)
8884           (gnus-summary-prev-subject 1)
8885         (gnus-summary-position-cursor)))
8886
8887 (defun gnus-summary-expunge-below (score)
8888   "Remove articles with score less than SCORE."
8889   (interactive "P")
8890   (gnus-set-global-variables)
8891   (setq score (if score
8892                   (prefix-numeric-value score)
8893                 (or gnus-summary-default-score 0)))
8894   (save-excursion
8895     (set-buffer gnus-summary-buffer)
8896     (goto-char (point-min))
8897     (let ((buffer-read-only nil)
8898           beg)
8899       (while (not (eobp))
8900         (if (< (gnus-summary-article-score) score)
8901             (progn
8902               (setq beg (point))
8903               (forward-line 1)
8904               (delete-region beg (point)))
8905           (forward-line 1)))
8906       ;; Adjust point.
8907       (or (zerop (buffer-size))
8908           (if (eobp)
8909               (gnus-summary-prev-subject 1)
8910             (gnus-summary-position-cursor))))))
8911
8912 (defun gnus-summary-mark-below (score mark)
8913   "Mark articles with score less than SCORE with MARK."
8914   (interactive "P\ncMark: ")
8915   (gnus-set-global-variables)
8916   (setq score (if score
8917                   (prefix-numeric-value score)
8918                 (or gnus-summary-default-score 0)))
8919   (save-excursion
8920     (set-buffer gnus-summary-buffer)
8921     (goto-char (point-min))
8922     (while (not (eobp))
8923       (and (< (gnus-summary-article-score) score)
8924            (gnus-summary-mark-article nil mark))
8925       (forward-line 1))))
8926
8927 (defun gnus-summary-kill-below (score)
8928   "Mark articles with score below SCORE as read."
8929   (interactive "P")
8930   (gnus-summary-mark-below score gnus-killed-mark))
8931
8932 (defun gnus-summary-clear-above (score)
8933   "Clear all marks from articles with score above SCORE."
8934   (interactive "P")
8935   (gnus-summary-mark-above score gnus-unread-mark))
8936
8937 (defun gnus-summary-tick-above (score)
8938   "Tick all articles with score above SCORE."
8939   (interactive "P")
8940   (gnus-summary-mark-above score gnus-ticked-mark))
8941
8942 (defun gnus-summary-mark-above (score mark)
8943   "Mark articles with score over SCORE with MARK."
8944   (interactive "P\ncMark: ")
8945   (setq score (if score
8946                   (prefix-numeric-value score)
8947                 (or gnus-summary-default-score 0)))
8948   (save-excursion
8949     (set-buffer gnus-summary-buffer)
8950     (goto-char (point-min))
8951     (while (not (eobp))
8952       (if (> (gnus-summary-article-score) score)
8953           (progn
8954             (gnus-summary-mark-article nil mark)
8955             (forward-line 1))
8956         (forward-line 1)))))
8957
8958 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
8959 (defun gnus-summary-show-all-expunged ()
8960   "Display all the hidden articles that were expunged for low scores."
8961   (interactive)
8962   (let ((buffer-read-only nil))
8963     (let ((scored gnus-newsgroup-scored)
8964           headers h)
8965       (while scored
8966         (or (gnus-summary-goto-subject (car (car scored)))
8967             (and (setq h (gnus-get-header-by-number (car (car scored))))
8968                  (< (cdr (car scored)) gnus-summary-expunge-below)
8969                  (setq headers (cons h headers))))
8970         (setq scored (cdr scored)))
8971       (or headers (error "No expunged articles hidden."))
8972       (goto-char (point-min))
8973       (save-excursion 
8974         (gnus-summary-update-lines 
8975          (point)
8976          (progn
8977            (gnus-summary-prepare-threads (nreverse headers) 0)
8978            (point)))))
8979     (goto-char (point-min))
8980     (gnus-summary-position-cursor)))
8981
8982 (defun gnus-summary-show-all-dormant ()
8983   "Display all the hidden articles that are marked as dormant."
8984   (interactive)
8985   (let ((buffer-read-only nil))
8986     (goto-char (point-min))
8987     (let ((dormant gnus-newsgroup-dormant)
8988           headers h)
8989       (while dormant
8990         (or (gnus-summary-goto-subject (car dormant))
8991             (and (setq h (gnus-get-header-by-number (car dormant)))
8992                  (setq headers (cons h headers))))
8993         (setq dormant (cdr dormant)))
8994       (or headers (error "No dormant articles hidden."))
8995       (save-excursion 
8996         (gnus-summary-update-lines 
8997          (point)
8998          (progn
8999            (gnus-summary-prepare-threads (nreverse headers) 0)
9000            (point)))))
9001     (goto-char (point-min))
9002     (gnus-summary-position-cursor)))
9003
9004 (defun gnus-summary-hide-all-dormant ()
9005   "Hide all dormant articles."
9006   (interactive)
9007   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9008   (gnus-summary-position-cursor))
9009
9010 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9011   "Mark all articles not marked as unread in this newsgroup as read.
9012 If prefix argument ALL is non-nil, all articles are marked as read.
9013 If QUIETLY is non-nil, no questions will be asked.
9014 If TO-HERE is non-nil, it should be a point in the buffer. All
9015 articles before this point will be marked as read.
9016 The number of articles marked as read is returned."
9017   (interactive "P")
9018   (prog1
9019       (if (or quietly
9020               (not gnus-interactive-catchup) ;Without confirmation?
9021               gnus-expert-user
9022               (gnus-y-or-n-p
9023                (if all
9024                    "Mark absolutely all articles as read? "
9025                  "Mark all unread articles as read? ")))
9026           (if (and not-mark 
9027                    (not gnus-newsgroup-adaptive)
9028                    (not gnus-newsgroup-auto-expire))
9029               (progn
9030                 (and all (setq gnus-newsgroup-marked nil
9031                                gnus-newsgroup-dormant nil))
9032                 (setq gnus-newsgroup-unreads gnus-newsgroup-marked))
9033             (let ((unreads (length gnus-newsgroup-unreads)))
9034               (if (gnus-summary-first-subject (not all))
9035                   (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
9036                               (if to-here (< (point) to-here) t)
9037                               (gnus-summary-search-subject nil (not all)))))
9038               (- unreads (length gnus-newsgroup-unreads))
9039               (or to-here
9040                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9041     (gnus-summary-position-cursor)))
9042
9043 (defun gnus-summary-catchup-to-here (&optional all)
9044   "Mark all unticked articles before the current one as read.
9045 If ALL is non-nil, also mark ticked and dormant articles as read."
9046   (interactive)
9047   (beginning-of-line)
9048   (gnus-summary-catchup all t (point))
9049   (gnus-summary-position-cursor))
9050
9051 (defun gnus-summary-catchup-all (&optional quietly)
9052   "Mark all articles in this newsgroup as read."
9053   (interactive)
9054   (gnus-summary-catchup t quietly))
9055
9056 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9057   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9058 If prefix argument ALL is non-nil, all articles are marked as read."
9059   (interactive "P")
9060   (gnus-summary-catchup all quietly nil 'fast)
9061   ;; Select next newsgroup or exit.
9062   (if (eq gnus-auto-select-next 'quietly)
9063       (gnus-summary-next-group nil)
9064     (gnus-summary-exit)))
9065
9066 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9067   "Mark all articles in this newsgroup as read, and then exit."
9068   (interactive)
9069   (gnus-summary-catchup-and-exit t quietly))
9070
9071 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9072 (defun gnus-summary-catchup-and-goto-next-group (all)
9073   "Mark all articles in this group as read and select the next group.
9074 If given a prefix, mark all articles, unread as well as ticked, as
9075 read." 
9076   (interactive "P")
9077   (gnus-summary-catchup all)
9078   (gnus-summary-next-group))
9079
9080 ;; Thread-based commands.
9081
9082 (defun gnus-summary-toggle-threads (arg)
9083   "Toggle showing conversation threads.
9084 If ARG is positive number, turn showing conversation threads on."
9085   (interactive "P")
9086   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9087     (setq gnus-show-threads
9088           (if (null arg) (not gnus-show-threads)
9089             (> (prefix-numeric-value arg) 0)))
9090     (gnus-summary-prepare)
9091     (gnus-summary-goto-subject current)
9092     (gnus-summary-position-cursor)))
9093
9094 (defun gnus-summary-show-all-threads ()
9095   "Show all threads."
9096   (interactive)
9097   (save-excursion
9098     (let ((buffer-read-only nil))
9099       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9100   (gnus-summary-position-cursor))
9101
9102 (defun gnus-summary-show-thread ()
9103   "Show thread subtrees.
9104 Returns nil if no thread was there to be shown."
9105   (interactive)
9106   (prog1
9107       (save-excursion
9108         (let ((buffer-read-only nil)
9109               ;; first goto end then to beg, to have point at beg after let
9110               (end (progn (end-of-line) (point)))
9111               (beg (progn (beginning-of-line) (point))))
9112           (prog1
9113               ;; Any hidden lines here?
9114               (search-forward "\r" end t)
9115             (subst-char-in-region beg end ?\^M ?\n t))))
9116     (gnus-summary-position-cursor)))
9117
9118 (defun gnus-summary-hide-all-threads ()
9119   "Hide all thread subtrees."
9120   (interactive)
9121   (save-excursion
9122     (goto-char (point-min))
9123     (gnus-summary-hide-thread)
9124     (while (and (not (eobp)) (zerop (forward-line 1)))
9125       (gnus-summary-hide-thread)))
9126   (gnus-summary-position-cursor))
9127
9128 (defun gnus-summary-hide-thread ()
9129   "Hide thread subtrees.
9130 Returns nil if no threads were there to be hidden."
9131   (interactive)
9132   (let ((buffer-read-only nil)
9133         (start (point))
9134         (level (gnus-summary-thread-level))
9135         (end (point)))
9136     ;; Go forward until either the buffer ends or the subthread
9137     ;; ends. 
9138     (if (eobp)
9139         ()
9140       (while (and (zerop (forward-line 1))
9141                   (> (gnus-summary-thread-level) level))
9142         (setq end (point)))
9143       (prog1
9144           (save-excursion
9145             (goto-char end)
9146             (search-backward "\n" start t))
9147         (subst-char-in-region start end ?\n ?\^M t)
9148         (forward-line -1)))))
9149
9150 (defun gnus-summary-go-to-next-thread (&optional previous)
9151   "Go to the same level (or less) next thread.
9152 If PREVIOUS is non-nil, go to previous thread instead.
9153 Return the article number moved to, or nil if moving was impossible."
9154   (let ((level (gnus-summary-thread-level))
9155         (article (gnus-summary-article-number)))
9156     (if previous 
9157         (while (and (zerop (gnus-summary-prev-subject 1))
9158                     (> (gnus-summary-thread-level) level)))
9159       (while (and (zerop (gnus-summary-next-subject 1))
9160                   (> (gnus-summary-thread-level) level))))
9161     (gnus-summary-recenter)
9162     (gnus-summary-position-cursor)
9163     (let ((oart (gnus-summary-article-number)))
9164       (and (/= oart article) oart))))
9165
9166 (defun gnus-summary-next-thread (n)
9167   "Go to the same level next N'th thread.
9168 If N is negative, search backward instead.
9169 Returns the difference between N and the number of skips actually
9170 done."
9171   (interactive "p")
9172   (let ((backward (< n 0))
9173         (n (abs n)))
9174   (while (and (> n 0)
9175               (gnus-summary-go-to-next-thread backward))
9176     (setq n (1- n)))
9177   (gnus-summary-position-cursor)
9178   (if (/= 0 n) (gnus-message 7 "No more threads"))
9179   n))
9180
9181 (defun gnus-summary-prev-thread (n)
9182   "Go to the same level previous N'th thread.
9183 Returns the difference between N and the number of skips actually
9184 done."
9185   (interactive "p")
9186   (gnus-summary-next-thread (- n)))
9187
9188 (defun gnus-summary-go-down-thread (&optional same)
9189   "Go down one level in the current thread.
9190 If SAME is non-nil, also move to articles of the same level."
9191   (let ((level (gnus-summary-thread-level))
9192         (start (point)))
9193     (if (and (zerop (forward-line 1))
9194              (> (gnus-summary-thread-level) level))
9195         t
9196       (goto-char start)
9197       nil)))
9198
9199 (defun gnus-summary-go-up-thread ()
9200   "Go up one level in the current thread."
9201   (let ((level (gnus-summary-thread-level))
9202         (start (point)))
9203     (while (and (zerop (forward-line -1))
9204                 (>= (gnus-summary-thread-level) level)))
9205     (if (>= (gnus-summary-thread-level) level)
9206         (progn
9207           (goto-char start)
9208           nil)
9209       t)))
9210
9211 (defun gnus-summary-down-thread (n)
9212   "Go down thread N steps.
9213 If N is negative, go up instead.
9214 Returns the difference between N and how many steps down that were
9215 taken."
9216   (interactive "p")
9217   (let ((up (< n 0))
9218         (n (abs n)))
9219   (while (and (> n 0)
9220               (if up (gnus-summary-go-up-thread)
9221                 (gnus-summary-go-down-thread)))
9222     (setq n (1- n)))
9223   (gnus-summary-position-cursor)
9224   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9225   n))
9226
9227 (defun gnus-summary-up-thread (n)
9228   "Go up thread N steps.
9229 If N is negative, go up instead.
9230 Returns the difference between N and how many steps down that were
9231 taken."
9232   (interactive "p")
9233   (gnus-summary-down-thread (- n)))
9234
9235 (defun gnus-summary-kill-thread (unmark)
9236   "Mark articles under current thread as read.
9237 If the prefix argument is positive, remove any kinds of marks.
9238 If the prefix argument is negative, tick articles instead."
9239   (interactive "P")
9240   (if unmark
9241       (setq unmark (prefix-numeric-value unmark)))
9242   (let ((killing t)
9243         (level (gnus-summary-thread-level)))
9244     (save-excursion
9245       (while killing
9246         ;; Mark the article...
9247         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9248               ((> unmark 0) (gnus-summary-tick-article nil t))
9249               (t (gnus-summary-tick-article)))
9250         ;; ...and go forward until either the buffer ends or the subtree
9251         ;; ends. 
9252         (if (not (and (zerop (forward-line 1))
9253                       (> (gnus-summary-thread-level) level)))
9254             (setq killing nil))))
9255     ;; Hide killed subtrees.
9256     (and (null unmark)
9257          gnus-thread-hide-killed
9258          (gnus-summary-hide-thread))
9259     ;; If marked as read, go to next unread subject.
9260     (if (null unmark)
9261         ;; Go to next unread subject.
9262         (gnus-summary-next-subject 1 t)))
9263   (gnus-set-mode-line 'summary))
9264
9265 ;; Summary sorting commands
9266
9267 (defun gnus-summary-sort-by-number (&optional reverse)
9268   "Sort summary buffer by article number.
9269 Argument REVERSE means reverse order."
9270   (interactive "P")
9271   (gnus-summary-sort 
9272    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9273
9274 (defun gnus-summary-sort-by-author (&optional reverse)
9275   "Sort summary buffer by author name alphabetically.
9276 If case-fold-search is non-nil, case of letters is ignored.
9277 Argument REVERSE means reverse order."
9278   (interactive "P")
9279   (gnus-summary-sort
9280    (cons
9281     (lambda ()
9282       (let ((extract (funcall
9283                       gnus-extract-address-components
9284                       (header-from (gnus-get-header-by-number
9285                                     (gnus-summary-article-number))))))
9286         (or (car extract) (cdr extract))))
9287     'gnus-thread-sort-by-author)
9288    reverse))
9289
9290 (defun gnus-summary-sort-by-subject (&optional reverse)
9291   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9292 If case-fold-search is non-nil, case of letters is ignored.
9293 Argument REVERSE means reverse order."
9294   (interactive "P")
9295   (gnus-summary-sort
9296    (cons
9297     (lambda ()
9298       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9299     'gnus-thread-sort-by-subject)
9300    reverse))
9301
9302 (defun gnus-summary-sort-by-date (&optional reverse)
9303   "Sort summary buffer by date.
9304 Argument REVERSE means reverse order."
9305   (interactive "P")
9306   (gnus-summary-sort
9307    (cons
9308     (lambda ()
9309       (gnus-sortable-date
9310        (header-date 
9311         (gnus-get-header-by-number (gnus-summary-article-number)))))
9312     'gnus-thread-sort-by-date)
9313    reverse))
9314
9315 (defun gnus-summary-sort-by-score (&optional reverse)
9316   "Sort summary buffer by score.
9317 Argument REVERSE means reverse order."
9318   (interactive "P")
9319   (gnus-summary-sort 
9320    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9321    (not reverse)))
9322
9323 (defvar gnus-summary-already-sorted nil)
9324 (defun gnus-summary-sort (predicate reverse)
9325   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9326   (if gnus-summary-already-sorted
9327       ()
9328     (let (buffer-read-only)
9329       (if (not gnus-show-threads)
9330           (progn
9331             (goto-char (point-min))
9332             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9333         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9334               (gnus-summary-prepare-hook nil)
9335               (gnus-summary-already-sorted nil))
9336           (gnus-summary-prepare)
9337           (and gnus-show-threads
9338                gnus-thread-hide-subtree
9339                (gnus-summary-hide-all-threads)))))))
9340   
9341 (defun gnus-sortable-date (date)
9342   "Make sortable string by string-lessp from DATE.
9343 Timezone package is used."
9344   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9345          (year   (aref date 0))
9346          (month  (aref date 1))
9347          (day    (aref date 2)))
9348     (timezone-make-sortable-date 
9349      year month day 
9350      (timezone-make-time-string
9351       (aref date 3) (aref date 4) (aref date 5)))))
9352
9353
9354 ;; Summary saving commands.
9355
9356 (defun gnus-summary-save-article (n)
9357   "Save the current article using the default saver function.
9358 If N is a positive number, save the N next articles.
9359 If N is a negative number, save the N previous articles.
9360 If N is nil and any articles have been marked with the process mark,
9361 save those articles instead.
9362 The variable `gnus-default-article-saver' specifies the saver function."
9363   (interactive "P")
9364   (let ((articles (gnus-summary-work-articles n)))
9365     (while articles
9366       (let ((header (gnus-get-header-by-number (car articles))))
9367         (if (vectorp header)
9368             (progn
9369               (gnus-summary-select-article t nil nil (car articles))
9370               (or gnus-save-all-headers
9371                   (gnus-article-hide-headers t))
9372               ;; Remove any X-Gnus lines.
9373               (save-excursion
9374                 (save-restriction
9375                   (set-buffer gnus-article-buffer)
9376                   (let ((buffer-read-only nil))
9377                     (goto-char (point-min))
9378                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9379                                                   (point-max)))
9380                     (while (re-search-forward "^X-Gnus" nil t)
9381                       (beginning-of-line)
9382                       (delete-region (point)
9383                                      (progn (forward-line 1) (point))))
9384                     (widen))))
9385               (save-excursion
9386                 (if gnus-default-article-saver
9387                     (funcall gnus-default-article-saver)
9388                   (error "No default saver is defined."))))
9389           (if (assq 'name header)
9390               (gnus-copy-file (cdr (assq 'name header)))
9391             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9392       (gnus-summary-remove-process-mark (car articles))
9393       (setq articles (cdr articles)))
9394     (gnus-summary-position-cursor)
9395     n))
9396
9397 (defun gnus-summary-pipe-output (arg)
9398   "Pipe the current article to a subprocess.
9399 If N is a positive number, pipe the N next articles.
9400 If N is a negative number, pipe the N previous articles.
9401 If N is nil and any articles have been marked with the process mark,
9402 pipe those articles instead."
9403   (interactive "P")
9404   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9405     (gnus-summary-save-article arg)))
9406
9407 (defun gnus-summary-save-article-mail (arg)
9408   "Append the current article to an mail file.
9409 If N is a positive number, save the N next articles.
9410 If N is a negative number, save the N previous articles.
9411 If N is nil and any articles have been marked with the process mark,
9412 save those articles instead."
9413   (interactive "P")
9414   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9415     (gnus-summary-save-article arg)))
9416
9417 (defun gnus-summary-save-article-rmail (arg)
9418   "Append the current article to an rmail file.
9419 If N is a positive number, save the N next articles.
9420 If N is a negative number, save the N previous articles.
9421 If N is nil and any articles have been marked with the process mark,
9422 save those articles instead."
9423   (interactive "P")
9424   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9425     (gnus-summary-save-article arg)))
9426
9427 (defun gnus-summary-save-article-file (arg)
9428   "Append the current article to a file.
9429 If N is a positive number, save the N next articles.
9430 If N is a negative number, save the N previous articles.
9431 If N is nil and any articles have been marked with the process mark,
9432 save those articles instead."
9433   (interactive "P")
9434   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9435     (gnus-summary-save-article arg)))
9436
9437 (defun gnus-read-save-file-name (prompt default-name)
9438   (let ((methods gnus-split-methods)
9439         split-name)
9440     (if (not gnus-split-methods)
9441         ()
9442       (save-excursion
9443         (set-buffer gnus-article-buffer)
9444         (gnus-narrow-to-headers)
9445         (while methods
9446           (goto-char (point-min))
9447           (and (condition-case () 
9448                    (re-search-forward (car (car methods)) nil t)
9449                  (error nil))
9450                (setq split-name (cons (nth 1 (car methods)) split-name)))
9451           (setq methods (cdr methods)))
9452         (widen)))
9453     (cond ((null split-name)
9454            (read-file-name
9455             (concat prompt " (default "
9456                     (file-name-nondirectory default-name) ") ")
9457             (file-name-directory default-name)
9458             default-name))
9459           ((= 1 (length split-name))
9460            (read-file-name
9461             (concat prompt " (default " (car split-name) ") ")
9462             gnus-article-save-directory
9463             (concat gnus-article-save-directory (car split-name))))
9464           (t
9465            (setq split-name (mapcar (lambda (el) (list el))
9466                                     (nreverse split-name)))
9467            (let ((result (completing-read 
9468                           (concat prompt " ")
9469                           split-name nil nil)))
9470              (concat gnus-article-save-directory
9471                      (if (string= result "")
9472                          (car (car split-name))
9473                        result)))))))
9474
9475 (defun gnus-summary-save-in-rmail (&optional filename)
9476   "Append this article to Rmail file.
9477 Optional argument FILENAME specifies file name.
9478 Directory to save to is default to `gnus-article-save-directory' which
9479 is initialized from the SAVEDIR environment variable."
9480   (interactive)
9481   (let ((default-name
9482           (funcall gnus-rmail-save-name gnus-newsgroup-name
9483                    gnus-current-headers gnus-newsgroup-last-rmail)))
9484     (or filename
9485         (setq filename (gnus-read-save-file-name 
9486                         "Save in rmail file:" default-name)))
9487     (gnus-make-directory (file-name-directory filename))
9488     (gnus-eval-in-buffer-window 
9489      gnus-article-buffer
9490      (save-excursion
9491        (save-restriction
9492          (widen)
9493          (gnus-output-to-rmail filename))))
9494     ;; Remember the directory name to save articles.
9495     (setq gnus-newsgroup-last-rmail filename)))
9496
9497 (defun gnus-summary-save-in-mail (&optional filename)
9498   "Append this article to Unix mail file.
9499 Optional argument FILENAME specifies file name.
9500 Directory to save to is default to `gnus-article-save-directory' which
9501 is initialized from the SAVEDIR environment variable."
9502   (interactive)
9503   (let ((default-name
9504           (funcall gnus-mail-save-name gnus-newsgroup-name
9505                    gnus-current-headers gnus-newsgroup-last-mail)))
9506     (or filename
9507         (setq filename (gnus-read-save-file-name 
9508                         "Save in Unix mail file:" default-name)))
9509     (setq filename
9510           (expand-file-name filename
9511                             (and default-name
9512                                  (file-name-directory default-name))))
9513     (gnus-make-directory (file-name-directory filename))
9514     (gnus-eval-in-buffer-window 
9515      gnus-article-buffer
9516      (save-excursion
9517        (save-restriction
9518          (widen)
9519          (if (and (file-readable-p filename) (rmail-file-p filename))
9520              (gnus-output-to-rmail filename)
9521            (rmail-output filename 1 t t)))))
9522     ;; Remember the directory name to save articles.
9523     (setq gnus-newsgroup-last-mail filename)))
9524
9525 (defun gnus-summary-save-in-file (&optional filename)
9526   "Append this article to file.
9527 Optional argument FILENAME specifies file name.
9528 Directory to save to is default to `gnus-article-save-directory' which
9529 is initialized from the SAVEDIR environment variable."
9530   (interactive)
9531   (let ((default-name
9532           (funcall gnus-file-save-name gnus-newsgroup-name
9533                    gnus-current-headers gnus-newsgroup-last-file)))
9534     (or filename
9535         (setq filename (gnus-read-save-file-name 
9536                         "Save in file:" default-name)))
9537     (gnus-make-directory (file-name-directory filename))
9538     (gnus-eval-in-buffer-window 
9539      gnus-article-buffer
9540      (save-excursion
9541        (save-restriction
9542          (widen)
9543          (gnus-output-to-file filename))))
9544     ;; Remember the directory name to save articles.
9545     (setq gnus-newsgroup-last-file filename)))
9546
9547 (defun gnus-summary-save-in-pipe (&optional command)
9548   "Pipe this article to subprocess."
9549   (interactive)
9550   (let ((command (read-string "Shell command on article: "
9551                               gnus-last-shell-command)))
9552     (if (string-equal command "")
9553         (setq command gnus-last-shell-command))
9554     (gnus-eval-in-buffer-window 
9555      gnus-article-buffer
9556      (save-restriction
9557        (widen)
9558        (shell-command-on-region (point-min) (point-max) command nil)))
9559     (setq gnus-last-shell-command command)))
9560
9561 ;; Summary extract commands
9562
9563 (defun gnus-summary-insert-pseudos (pslist)
9564   (let ((buffer-read-only nil)
9565         (article (gnus-summary-article-number))
9566         b)
9567     (or (gnus-summary-goto-subject article)
9568         (error (format "No such article: %d" article)))
9569     (or gnus-newsgroup-headers-hashtb-by-number
9570         (gnus-make-headers-hashtable-by-number))
9571     (gnus-summary-position-cursor)
9572     ;; If all commands are to be bunched up on one line, we collect
9573     ;; them here.  
9574     (if gnus-view-pseudos-separately
9575         ()
9576       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9577             files action)
9578         (while ps
9579           (setq action (cdr (assq 'action (car ps))))
9580           (setq files (list (cdr (assq 'name (car ps)))))
9581           (while (and ps (cdr ps)
9582                       (string= (or action "1")
9583                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9584             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9585             (setcdr ps (cdr (cdr ps))))
9586           (if (not files)
9587               ()
9588             (if (not (string-match "%s" action))
9589                 (setq files (cons " " files)))
9590             (setq files (cons " " files))
9591             (and (assq 'execute (car ps))
9592                  (setcdr (assq 'execute (car ps))
9593                          (funcall (if (string-match "%s" action)
9594                                       'format 'concat)
9595                                   action 
9596                                   (mapconcat (lambda (f) f) files " ")))))
9597           (setq ps (cdr ps)))))
9598     (if gnus-view-pseudos
9599         (while pslist
9600           (and (assq 'execute (car pslist))
9601                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9602                                      (eq gnus-view-pseudos 'not-confirm)))
9603           (setq pslist (cdr pslist)))
9604       (save-excursion
9605         (while pslist
9606           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9607                                          (gnus-summary-article-number)))
9608           (forward-line 1)
9609           (setq b (point))
9610           (insert "          " (file-name-nondirectory 
9611                                 (cdr (assq 'name (car pslist))))
9612                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9613           (add-text-properties 
9614            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9615                           'gnus-mark gnus-unread-mark 
9616                           'gnus-level 0
9617                           'gnus-pseudo (car pslist)))
9618           (forward-line -1)
9619           (gnus-sethash (int-to-string gnus-reffed-article-number)
9620                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9621           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9622           (setq pslist (cdr pslist)))))))
9623
9624 (defun gnus-pseudos< (p1 p2)
9625   (let ((c1 (cdr (assq 'action p1)))
9626         (c2 (cdr (assq 'action p2))))
9627     (and c1 c2 (string< c1 c2))))
9628
9629 (defun gnus-request-pseudo-article (props)
9630   (cond ((assq 'execute props)
9631          (gnus-execute-command (cdr (assq 'execute props)))))
9632   (let ((gnus-current-article (gnus-summary-article-number)))
9633     (run-hooks 'gnus-mark-article-hook)))
9634
9635 (defun gnus-execute-command (command &optional automatic)
9636   (save-excursion
9637     (gnus-article-setup-buffer)
9638     (set-buffer gnus-article-buffer)
9639     (let ((command (if automatic command (read-string "Command: " command)))
9640           (buffer-read-only nil))
9641       (erase-buffer)
9642       (insert "$ " command "\n\n")
9643       (if gnus-view-pseudo-asynchronously
9644           (start-process "gnus-execute" nil "sh" "-c" command)
9645         (call-process "sh" nil t nil "-c" command)))))
9646
9647 (defun gnus-copy-file (file &optional to)
9648   "Copy FILE to TO."
9649   (interactive
9650    (list (read-file-name "Copy file: " default-directory)
9651          (read-file-name "Copy file to: " default-directory)))
9652   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9653   (and (file-directory-p to) 
9654        (setq to (concat (file-name-as-directory to)
9655                         (file-name-nondirectory file))))
9656   (copy-file file to))
9657
9658 ;; Summary kill commands.
9659
9660 (defun gnus-summary-edit-global-kill (article)
9661   "Edit the \"global\" kill file."
9662   (interactive (list (gnus-summary-article-number)))
9663   (gnus-group-edit-global-kill article))
9664
9665 (defun gnus-summary-edit-local-kill ()
9666   "Edit a local kill file applied to the current newsgroup."
9667   (interactive)
9668   (setq gnus-current-headers 
9669         (gnus-gethash 
9670          (int-to-string (gnus-summary-article-number))
9671          gnus-newsgroup-headers-hashtb-by-number))
9672   (gnus-set-global-variables)
9673   (gnus-group-edit-local-kill 
9674    (gnus-summary-article-number) gnus-newsgroup-name))
9675
9676 \f
9677 ;;;
9678 ;;; Gnus article mode
9679 ;;;
9680
9681 (put 'gnus-article-mode 'mode-class 'special)
9682
9683 (if gnus-article-mode-map
9684     nil
9685   (setq gnus-article-mode-map (make-keymap))
9686   (suppress-keymap gnus-article-mode-map)
9687   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9688   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9689   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9690   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9691   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9692   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9693   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9694   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9695   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9696   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9697   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9698   
9699   ;; Duplicate almost all summary keystrokes in the article mode map.
9700   (let ((commands 
9701          (list "#" "\M-#" "\C-c\M-#" "n" "p"
9702                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9703                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9704                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k""x" "X" 
9705                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9706                "t" "\M-t" "C" "S"
9707                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9708                "V" "\C-c\C-d")))
9709     (while commands
9710       (define-key gnus-article-mode-map (car commands) 
9711         'gnus-article-summary-command)
9712       (setq commands (cdr commands))))
9713
9714   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9715     (while commands
9716       (define-key gnus-article-mode-map (car commands) 
9717         'gnus-article-summary-command-nosave)
9718       (setq commands (cdr commands)))))
9719
9720
9721 (defun gnus-article-mode ()
9722   "Major mode for displaying an article.
9723
9724 All normal editing commands are switched off.
9725
9726 The following commands are available:
9727
9728 \\<gnus-article-mode-map>
9729 \\[gnus-article-next-page]\t Scroll the article one page forwards
9730 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9731 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9732 \\[gnus-article-show-summary]\t Display the summary buffer
9733 \\[gnus-article-mail]\t Send a reply to the address near point
9734 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9735 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9736 \\[gnus-info-find-node]\t Go to the Gnus info node"
9737   (interactive)
9738   (if gnus-visual (gnus-article-make-menu-bar))
9739   (kill-all-local-variables)
9740   (setq mode-line-modified "-- ")
9741   (make-local-variable 'mode-line-format)
9742   (setq mode-line-format (copy-sequence mode-line-format))
9743   (and (equal (nth 3 mode-line-format) "   ")
9744        (setcar (nthcdr 3 mode-line-format) ""))
9745   (setq mode-name "Article")
9746   (setq major-mode 'gnus-article-mode)
9747   (make-local-variable 'minor-mode-alist)
9748   (or (assq 'gnus-show-mime minor-mode-alist)
9749       (setq minor-mode-alist
9750             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9751   (use-local-map gnus-article-mode-map)
9752   (make-local-variable 'page-delimiter)
9753   (setq page-delimiter gnus-page-delimiter)
9754   (buffer-disable-undo (current-buffer))
9755   (setq buffer-read-only t)             ;Disable modification
9756   (run-hooks 'gnus-article-mode-hook))
9757
9758 (defun gnus-article-setup-buffer ()
9759   "Initialize article mode buffer."
9760   (if (get-buffer gnus-article-buffer)
9761       (save-excursion
9762         (set-buffer gnus-article-buffer)
9763         (or (eq major-mode 'gnus-article-mode)
9764             (gnus-article-mode)))
9765     (save-excursion
9766       (set-buffer (get-buffer-create gnus-article-buffer))
9767       (gnus-add-current-to-buffer-list)
9768       (gnus-article-mode))))
9769
9770 ;; Set article window start at LINE, where LINE is the number of lines
9771 ;; from the head of the article.
9772 (defun gnus-article-set-window-start (&optional line)
9773   (set-window-start 
9774    (get-buffer-window gnus-article-buffer)
9775    (save-excursion
9776      (set-buffer gnus-article-buffer)
9777      (goto-char (point-min))
9778      (if (not line)
9779          (point-min)
9780        (gnus-message 6 "Moved to bookmark")
9781        (search-forward "\n\n" nil t)
9782        (forward-line line)
9783        (point)))))
9784
9785 (defun gnus-request-article-this-buffer (article group)
9786   "Get an article and insert it into this buffer."
9787   (setq group (or group gnus-newsgroup-name))
9788
9789   ;; Open server if it has closed.
9790   (gnus-check-news-server (gnus-find-method-for-group group))
9791
9792   ;; Using `gnus-request-article' directly will insert the article into
9793   ;; `nntp-server-buffer' - so we'll save some time by not having to
9794   ;; copy it from the server buffer into the article buffer.
9795
9796   ;; We only request an article by message-id when we do not have the
9797   ;; headers for it, so we'll have to get those.
9798   (and (stringp article) 
9799        (let ((gnus-override-method gnus-refer-article-method))
9800          (gnus-read-header article)))
9801
9802   ;; If the article number is negative, that means that this article
9803   ;; doesn't belong in this newsgroup (possibly), so we find its
9804   ;; message-id and request it by id instead of number.
9805   (if (not (numberp article))
9806       ()
9807     (save-excursion
9808       (set-buffer gnus-summary-buffer)
9809       (let ((header (gnus-get-header-by-number article)))
9810         (if (< article 0)
9811             (if (vectorp header)
9812                 ;; It's a real article.
9813                 (setq article (header-id header))
9814               ;; It is an extracted pseudo-article.
9815               (setq article 'pseudo)
9816               (gnus-request-pseudo-article header)))
9817
9818         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9819           (if (not (eq (car method) 'nneething))
9820               ()
9821             (let ((dir (concat (file-name-as-directory (nth 1 method))
9822                                (header-subject header))))
9823               (if (file-directory-p dir)
9824                   (progn
9825                     (setq article 'nneething)
9826                     (gnus-group-enter-directory dir)))))))))
9827
9828   ;; Check the cache.
9829   (if (and gnus-use-cache
9830            (numberp article)
9831            (gnus-cache-request-article article group))
9832       'article
9833     ;; Get the article and into the article buffer.
9834     (if (or (stringp article) (numberp article))
9835         (progn
9836           (erase-buffer)
9837           (let ((gnus-override-method 
9838                  (and (stringp article) gnus-refer-article-method)))
9839             (and (gnus-request-article article group (current-buffer))
9840                  'article)))
9841       article)))
9842
9843 (defun gnus-read-header (id)
9844   "Read the headers of article ID and enter them into the Gnus system."
9845   (or gnus-newsgroup-headers-hashtb-by-number
9846       (gnus-make-headers-hashtable-by-number))
9847   (let (header)
9848     (if (not (setq header 
9849                    (car (if (let ((gnus-nov-is-evil t))
9850                               (gnus-retrieve-headers 
9851                                (list id) gnus-newsgroup-name))
9852                             (gnus-get-newsgroup-headers)))))
9853         nil
9854       (if (stringp id)
9855           (header-set-number header gnus-reffed-article-number))
9856       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
9857       (gnus-sethash (int-to-string (header-number header)) header
9858                     gnus-newsgroup-headers-hashtb-by-number)
9859       (if (stringp id)
9860           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
9861       (setq gnus-current-headers header)
9862       header)))
9863
9864 (defun gnus-article-prepare (article &optional all-headers header)
9865   "Prepare ARTICLE in article mode buffer.
9866 ARTICLE should either be an article number or a Message-ID.
9867 If ARTICLE is an id, HEADER should be the article headers.
9868 If ALL-HEADERS is non-nil, no headers are hidden."
9869   (save-excursion
9870     ;; Make sure we start in a summary buffer.
9871     (or (eq major-mode 'gnus-summary-mode)
9872         (set-buffer gnus-summary-buffer))
9873     (setq gnus-summary-buffer (current-buffer))
9874     ;; Make sure the connection to the server is alive.
9875     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
9876         (progn
9877           (gnus-check-news-server 
9878            (gnus-find-method-for-group gnus-newsgroup-name))
9879           (gnus-request-group gnus-newsgroup-name t)))
9880     (or gnus-newsgroup-headers-hashtb-by-number
9881         (gnus-make-headers-hashtable-by-number))
9882     (let* ((article (if header (header-number header) article))
9883            (summary-buffer (current-buffer))
9884            (internal-hook gnus-article-internal-prepare-hook)
9885            (group gnus-newsgroup-name)
9886            result)
9887       (save-excursion
9888         (gnus-article-setup-buffer)
9889         (set-buffer gnus-article-buffer)
9890         (if (not (setq result (let ((buffer-read-only nil))
9891                                 (gnus-request-article-this-buffer 
9892                                  article group))))
9893             ;; There is no such article.
9894             (progn
9895               (save-excursion
9896                 (set-buffer gnus-summary-buffer)
9897                 (setq gnus-current-article 0)
9898                 (and (numberp article) 
9899                      (gnus-summary-mark-as-read article gnus-canceled-mark))
9900                 (gnus-message 1 "No such article (may be canceled)")
9901                 (and (numberp article)
9902                      (setq gnus-current-article article))
9903                 (ding))
9904               (and (numberp article)
9905                    (setq gnus-article-current 
9906                          (cons gnus-newsgroup-name article)))
9907               nil)
9908           (if (or (eq result 'pseudo) (eq result 'nneething))
9909               (progn
9910                 (save-excursion
9911                   (set-buffer summary-buffer)
9912                   (setq gnus-last-article gnus-current-article
9913                         gnus-newsgroup-history (cons gnus-current-article
9914                                                      gnus-newsgroup-history)
9915                         gnus-current-article 0
9916                         gnus-current-headers nil
9917                         gnus-article-current nil)
9918                   (if (eq result 'nneething)
9919                       (gnus-configure-windows 'summary)
9920                     (gnus-configure-windows 'article))
9921                   (gnus-set-global-variables))
9922                 (gnus-set-mode-line 'article))
9923             ;; The result from the `request' was an actual article -
9924             ;; or at least some text that is now displayed in the
9925             ;; article buffer.
9926             (if (and (numberp article)
9927                      (not (eq article gnus-current-article)))
9928                 ;; Seems like a new article has been selected.
9929                 ;; `gnus-current-article' must be an article number.
9930                 (save-excursion
9931                   (set-buffer summary-buffer)
9932                   (setq gnus-last-article gnus-current-article
9933                         gnus-newsgroup-history (cons gnus-current-article
9934                                                      gnus-newsgroup-history)
9935                         gnus-current-article article
9936                         gnus-current-headers 
9937                         (gnus-get-header-by-number gnus-current-article)
9938                         gnus-article-current 
9939                         (cons gnus-newsgroup-name gnus-current-article))
9940                   (gnus-summary-show-thread)
9941                   (run-hooks 'gnus-mark-article-hook)
9942                   (gnus-set-mode-line 'summary)
9943                   (and gnus-visual 
9944                        (run-hooks 'gnus-visual-mark-article-hook))
9945                   ;; Set the global newsgroup variables here.
9946                   ;; Suggested by Jim Sisolak
9947                   ;; <sisolak@trans4.neep.wisc.edu>.
9948                   (gnus-set-global-variables)
9949                   (and gnus-use-cache 
9950                        (gnus-cache-possibly-enter-article
9951                         group article
9952                         (gnus-get-header-by-number article)
9953                         (memq article gnus-newsgroup-marked)
9954                         (memq article gnus-newsgroup-dormant)
9955                         (memq article gnus-newsgroup-unreads)))))
9956             ;; gnus-have-all-headers must be either T or NIL.
9957             (setq gnus-have-all-headers
9958                   (not (not (or all-headers gnus-show-all-headers))))
9959             ;; Hooks for getting information from the article.
9960             ;; This hook must be called before being narrowed.
9961             (let (buffer-read-only)
9962               (run-hooks 'internal-hook)
9963               (run-hooks 'gnus-article-prepare-hook)
9964               ;; Decode MIME message.
9965               (if (and gnus-show-mime
9966                        (gnus-fetch-field "Mime-Version"))
9967                   (funcall gnus-show-mime-method))
9968               ;; Perform the article display hooks.
9969               (run-hooks 'gnus-article-display-hook))
9970             ;; Do page break.
9971             (goto-char (point-min))
9972             (and gnus-break-pages (gnus-narrow-to-page))
9973             (gnus-set-mode-line 'article)
9974             (gnus-configure-windows 'article)
9975             (goto-char (point-min))
9976             t))))))
9977
9978 (defun gnus-article-show-all-headers ()
9979   "Show all article headers in article mode buffer."
9980   (save-excursion 
9981     (gnus-article-setup-buffer)
9982     (set-buffer gnus-article-buffer)
9983     (let ((buffer-read-only nil))
9984       (remove-text-properties (point-min) (point-max) 
9985                               gnus-hidden-properties))))
9986
9987 (defun gnus-article-hide-headers-if-wanted ()
9988   "Hide unwanted headers if `gnus-have-all-headers' is nil.
9989 Provided for backwards compatability."
9990   (or gnus-have-all-headers
9991       (gnus-article-hide-headers)))
9992
9993 (defun gnus-article-hide-headers (&optional delete)
9994   "Hide unwanted headers and possibly sort them as well."
9995   (interactive "P")
9996   (save-excursion
9997     (set-buffer gnus-article-buffer)
9998     (save-restriction
9999       (let ((sorted gnus-sorted-header-list)
10000             (buffer-read-only nil)
10001             want want-list beg want-l)
10002         ;; First we narrow to just the headers.
10003         (widen)
10004         (goto-char (point-min))
10005         ;; Hide any "From " lines at the beginning of (mail) articles. 
10006         (while (looking-at "From ")
10007           (forward-line 1))
10008         (if (bobp) 
10009             (add-text-properties (point-min) (point) gnus-hidden-properties))
10010         ;; Then treat the rest of the header lines.
10011         (narrow-to-region 
10012          (point) 
10013          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10014         ;; Then we use the two regular expressions
10015         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10016         ;; select which header lines is to remain visible in the
10017         ;; article buffer.
10018         (goto-char (point-min))
10019         (while (re-search-forward "^[^ \t]*:" nil t)
10020           (beginning-of-line)
10021           ;; We add the headers we want to keep to a list and delete
10022           ;; them from the buffer.
10023           (if (or (and (stringp gnus-visible-headers)
10024                        (looking-at gnus-visible-headers))
10025                   (and (not (stringp gnus-visible-headers))
10026                        (stringp gnus-ignored-headers)
10027                        (not (looking-at gnus-ignored-headers))))
10028               (progn
10029                 (setq beg (point))
10030                 (forward-line 1)
10031                 ;; Be sure to get multi-line headers...
10032                 (re-search-forward "^[^ \t]*:" nil t)
10033                 (beginning-of-line)
10034                 (setq want-list 
10035                       (cons (buffer-substring beg (point)) want-list))
10036                 (delete-region beg (point))
10037                 (goto-char beg))
10038             (forward-line 1)))
10039         ;; Next we perform the sorting by looking at
10040         ;; `gnus-sorted-header-list'. 
10041         (goto-char (point-min))
10042         (while (and sorted want-list)
10043           (setq want-l want-list)
10044           (while (and want-l
10045                       (not (string-match (car sorted) (car want-l))))
10046             (setq want-l (cdr want-l)))
10047           (if want-l 
10048               (progn
10049                 (insert (car want-l))
10050                 (setq want-list (delq (car want-l) want-list))))
10051           (setq sorted (cdr sorted)))
10052         ;; Any headers that were not matched by the sorted list we
10053         ;; just tack on the end of the visible header list.
10054         (while want-list
10055           (insert (car want-list))
10056           (setq want-list (cdr want-list)))
10057         ;; And finally we make the unwanted headers invisible.
10058         (if delete
10059             (delete-region (point) (point-max))
10060           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10061           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10062
10063 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10064 (defun gnus-article-treat-overstrike ()
10065   "Translate overstrikes into bold text."
10066   (interactive)
10067   (save-excursion
10068     (set-buffer gnus-article-buffer)
10069     (let ((buffer-read-only nil))
10070       (while (search-forward "\b" nil t)
10071         (let ((next (following-char))
10072               (previous (char-after (- (point) 2))))
10073           (cond ((eq next previous)
10074                  (delete-region (- (point) 2) (point))
10075                  (put-text-property (point) (1+ (point))
10076                                     'face 'bold))
10077                 ((eq next ?_)
10078                  (delete-region (1- (point)) (1+ (point)))
10079                  (put-text-property (1- (point)) (point)
10080                                     'face 'underline))
10081                 ((eq previous ?_)
10082                  (delete-region (- (point) 2) (point))
10083                  (put-text-property (point) (1+ (point))
10084                                     'face 'underline))))))))
10085
10086 (defun gnus-article-word-wrap ()
10087   "Format too long lines."
10088   (interactive)
10089   (save-excursion
10090     (set-buffer gnus-article-buffer)
10091     (let ((buffer-read-only nil))
10092       (goto-char (point-min))
10093       (search-forward "\n\n" nil t)
10094       (end-of-line 1)
10095       (let ((paragraph-start "^\\W"))
10096         (while (not (eobp))
10097           (and (>= (current-column) (window-width))
10098                (/= (preceding-char) ?:)
10099                (fill-paragraph nil))
10100           (end-of-line 2))))))
10101
10102 (defun gnus-article-remove-cr ()
10103   "Remove carriage returns from an article."
10104   (interactive)
10105   (save-excursion
10106     (set-buffer gnus-article-buffer)
10107     (let ((buffer-read-only nil))
10108       (goto-char (point-min))
10109       (while (search-forward "\r" nil t)
10110         (replace-match "" t t)))))
10111
10112 (defun gnus-article-display-x-face (&optional force)
10113   "Look for an X-Face header and display it if present."
10114   (interactive (list 'force))
10115   (save-excursion
10116     (set-buffer gnus-article-buffer)
10117     (let ((inhibit-point-motion-hooks t)
10118           (case-fold-search nil))
10119       (save-restriction
10120         (goto-char (point-min))
10121         (search-forward "\n\n")
10122         (narrow-to-region (point-min) (point))
10123         (goto-char (point-min))
10124         (if (or (not gnus-article-x-face-command)
10125                 (and (not force)
10126                      (or (not gnus-article-x-face-too-ugly)
10127                          (string-match gnus-article-x-face-too-ugly
10128                                        (mail-fetch-field "from"))))
10129                 (progn
10130                   (goto-char (point-min))
10131                   (not (re-search-forward "^X-Face: " nil t))))
10132             nil
10133           (let ((beg (point))
10134                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10135             (if (symbolp gnus-article-x-face-command)
10136                 (and (or (fboundp gnus-article-x-face-command)
10137                          (error "%s is not a function"
10138                                 gnus-article-x-face-command))
10139                      (funcall gnus-article-x-face-command beg end))
10140               (call-process-region beg end "sh" nil 0 nil
10141                                    "-c" gnus-article-x-face-command))))))))
10142
10143 (defun gnus-article-de-quoted-unreadable (&optional force)
10144   "Do a naïve translation of a quoted-printable-encoded article.
10145 This is in no way, shape or form meant as a replacement for real MIME
10146 processing, but is simply a stop-gap measure until MIME support is
10147 written.
10148 If FORCE, decode the article whether it is marked as quoted-printable
10149 or not." 
10150   (interactive (list 'force))
10151   (save-excursion
10152     (set-buffer gnus-article-buffer)
10153     (let ((case-fold-search t)
10154           (buffer-read-only nil)
10155           (type (gnus-fetch-field "content-transfer-encoding")))
10156       (if (or force (and type (string-match "quoted-printable" type)))
10157           (progn
10158             (goto-char (point-min))
10159             (search-forward "\n\n" nil 'move)
10160             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10161
10162 (defun gnus-mime-decode-quoted-printable (from to)
10163   ;; Decode quoted-printable from region between FROM and TO.
10164   (save-excursion
10165     (goto-char from)
10166     (while (search-forward "=" to t)
10167       (cond ((eq (following-char) ?\n)
10168              (delete-char -1)
10169              (delete-char 1))
10170             ((looking-at "[0-9A-F][0-9A-F]")
10171              (delete-char -1)
10172              (insert (hexl-hex-string-to-integer
10173                       (buffer-substring (point) (+ 2 (point)))))
10174              (delete-char 2))
10175             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10176
10177 (defun gnus-article-date-ut (&optional type)
10178   "Convert DATE date to universal time in the current article.
10179 If TYPE is `local', convert to local time; if it is `lapsed', output
10180 how much time has lapsed since DATE."
10181   (interactive (list 'ut))
10182   (let ((date (header-date (or gnus-current-headers 
10183                                (gnus-get-header-by-number
10184                                 (gnus-summary-article-number))"")))
10185         (date-regexp "^Date: \\|^X-Sent: "))
10186     (if (not date)
10187         ()
10188       (save-excursion
10189         (set-buffer gnus-article-buffer)
10190         (let ((buffer-read-only nil))
10191           (goto-char (point-min))
10192           (if (and (re-search-forward date-regexp nil t)
10193                    (progn 
10194                      (beginning-of-line)
10195                      (looking-at date-regexp)))
10196               (delete-region (gnus-point-at-bol)
10197                              (progn (end-of-line) (1+ (point))))
10198             (goto-char (point-min))
10199             (goto-char (- (search-forward "\n\n") 2)))
10200           (insert
10201            (cond 
10202             ((eq type 'local)
10203              (concat "Date: " (timezone-make-date-arpa-standard date) "\n"))
10204             ((eq type 'ut)
10205              (concat "Date: " (timezone-make-date-arpa-standard date nil "UT")
10206                      "\n"))
10207             ((eq type 'lapsed)
10208              (let* ((sec (- (gnus-seconds-since-epoch 
10209                              (timezone-make-date-arpa-standard
10210                               (current-time-string) (current-time-zone) "UT"))
10211                             (gnus-seconds-since-epoch 
10212                              (timezone-make-date-arpa-standard date nil "UT"))))
10213                     (units (list (cons 'year (* 365.25 24 60 60))
10214                                  (cons 'week (* 7 24 60 60))
10215                                  (cons 'day (* 24 60 60))
10216                                  (cons 'hour (* 60 60))
10217                                  (cons 'minute 60)
10218                                  (cons 'second 1)))
10219                     num prev)
10220                (concat
10221                 "X-Sent: "
10222                 (mapconcat 
10223                  (lambda (unit)
10224                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10225                        ""
10226                      (setq sec (- sec (* num (cdr unit))))
10227                      (prog1
10228                          (concat (if prev ", " "") (int-to-string (floor num))
10229                                  " " (symbol-name (car unit))
10230                                  (if (> num 1) "s" ""))
10231                        (setq prev t))))
10232                  units "")
10233                 " ago\n")))
10234             (t
10235              (error "Unknown conversion type: %s" type)))))))))
10236
10237 (defun gnus-article-date-local ()
10238   "Convert the current article date to the local timezone."
10239   (interactive)
10240   (gnus-article-date-ut 'local))
10241
10242 (defun gnus-article-date-lapsed ()
10243   "Convert the current article date to time lapsed since it was sent."
10244   (interactive)
10245   (gnus-article-date-ut 'lapsed))
10246
10247 (defun gnus-article-maybe-highlight ()
10248   (if gnus-visual (gnus-article-highlight)))
10249
10250 ;; Article savers.
10251
10252 (defun gnus-output-to-rmail (file-name)
10253   "Append the current article to an Rmail file named FILE-NAME."
10254   (require 'rmail)
10255   ;; Most of these codes are borrowed from rmailout.el.
10256   (setq file-name (expand-file-name file-name))
10257   (setq rmail-default-rmail-file file-name)
10258   (let ((artbuf (current-buffer))
10259         (tmpbuf (get-buffer-create " *Gnus-output*")))
10260     (save-excursion
10261       (or (get-file-buffer file-name)
10262           (file-exists-p file-name)
10263           (if (gnus-yes-or-no-p
10264                (concat "\"" file-name "\" does not exist, create it? "))
10265               (let ((file-buffer (create-file-buffer file-name)))
10266                 (save-excursion
10267                   (set-buffer file-buffer)
10268                   (rmail-insert-rmail-file-header)
10269                   (let ((require-final-newline nil))
10270                     (write-region (point-min) (point-max) file-name t 1)))
10271                 (kill-buffer file-buffer))
10272             (error "Output file does not exist")))
10273       (set-buffer tmpbuf)
10274       (buffer-disable-undo (current-buffer))
10275       (erase-buffer)
10276       (insert-buffer-substring artbuf)
10277       (gnus-convert-article-to-rmail)
10278       ;; Decide whether to append to a file or to an Emacs buffer.
10279       (let ((outbuf (get-file-buffer file-name)))
10280         (if (not outbuf)
10281             (append-to-file (point-min) (point-max) file-name)
10282           ;; File has been visited, in buffer OUTBUF.
10283           (set-buffer outbuf)
10284           (let ((buffer-read-only nil)
10285                 (msg (and (boundp 'rmail-current-message)
10286                           (symbol-value 'rmail-current-message))))
10287             ;; If MSG is non-nil, buffer is in RMAIL mode.
10288             (if msg
10289                 (progn (widen)
10290                        (narrow-to-region (point-max) (point-max))))
10291             (insert-buffer-substring tmpbuf)
10292             (if msg
10293                 (progn
10294                   (goto-char (point-min))
10295                   (widen)
10296                   (search-backward "\^_")
10297                   (narrow-to-region (point) (point-max))
10298                   (goto-char (1+ (point-min)))
10299                   (rmail-count-new-messages t)
10300                   (rmail-show-message msg)))))))
10301     (kill-buffer tmpbuf)))
10302
10303 (defun gnus-output-to-file (file-name)
10304   "Append the current article to a file named FILE-NAME."
10305   (setq file-name (expand-file-name file-name))
10306   (let ((artbuf (current-buffer))
10307         (tmpbuf (get-buffer-create " *Gnus-output*")))
10308     (save-excursion
10309       (set-buffer tmpbuf)
10310       (buffer-disable-undo (current-buffer))
10311       (erase-buffer)
10312       (insert-buffer-substring artbuf)
10313       ;; Append newline at end of the buffer as separator, and then
10314       ;; save it to file.
10315       (goto-char (point-max))
10316       (insert "\n")
10317       (append-to-file (point-min) (point-max) file-name))
10318     (kill-buffer tmpbuf)))
10319
10320 (defun gnus-convert-article-to-rmail ()
10321   "Convert article in current buffer to Rmail message format."
10322   (let ((buffer-read-only nil))
10323     ;; Convert article directly into Babyl format.
10324     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10325     (goto-char (point-min))
10326     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10327     (while (search-forward "\n\^_" nil t) ;single char
10328       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10329     (goto-char (point-max))
10330     (insert "\^_")))
10331
10332 (defun gnus-narrow-to-page (&optional arg)
10333   "Make text outside current page invisible except for page delimiter.
10334 A numeric arg specifies to move forward or backward by that many pages,
10335 thus showing a page other than the one point was originally in."
10336   (interactive "P")
10337   (setq arg (if arg (prefix-numeric-value arg) 0))
10338   (save-excursion
10339     (forward-page -1)                   ;Beginning of current page.
10340     (widen)
10341     (if (> arg 0)
10342         (forward-page arg)
10343       (if (< arg 0)
10344           (forward-page (1- arg))))
10345     ;; Find the end of the page.
10346     (forward-page)
10347     ;; If we stopped due to end of buffer, stay there.
10348     ;; If we stopped after a page delimiter, put end of restriction
10349     ;; at the beginning of that line.
10350     ;; These are commented out.
10351     ;;    (if (save-excursion (beginning-of-line)
10352     ;;                  (looking-at page-delimiter))
10353     ;;  (beginning-of-line))
10354     (narrow-to-region (point)
10355                       (progn
10356                         ;; Find the top of the page.
10357                         (forward-page -1)
10358                         ;; If we found beginning of buffer, stay there.
10359                         ;; If extra text follows page delimiter on same line,
10360                         ;; include it.
10361                         ;; Otherwise, show text starting with following line.
10362                         (if (and (eolp) (not (bobp)))
10363                             (forward-line 1))
10364                         (point)))))
10365
10366 (defun gnus-gmt-to-local ()
10367   "Rewrite Date header described in GMT to local in current buffer.
10368 Intended to be used with gnus-article-prepare-hook."
10369   (save-excursion
10370     (save-restriction
10371       (widen)
10372       (goto-char (point-min))
10373       (narrow-to-region (point-min)
10374                         (progn (search-forward "\n\n" nil 'move) (point)))
10375       (goto-char (point-min))
10376       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10377           (let ((buffer-read-only nil)
10378                 (date (buffer-substring (match-beginning 1) (match-end 1))))
10379             (delete-region (match-beginning 1) (match-end 1))
10380             (insert
10381              (timezone-make-date-arpa-standard 
10382               date nil (current-time-zone))))))))
10383
10384
10385 ;; Article mode commands
10386
10387 (defun gnus-article-next-page (lines)
10388   "Show next page of current article.
10389 If end of article, return non-nil. Otherwise return nil.
10390 Argument LINES specifies lines to be scrolled up."
10391   (interactive "P")
10392   (move-to-window-line -1)
10393   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10394   (if (save-excursion
10395         (end-of-line)
10396         (and (pos-visible-in-window-p)  ;Not continuation line.
10397              (eobp)))
10398       ;; Nothing in this page.
10399       (if (or (not gnus-break-pages)
10400               (save-excursion
10401                 (save-restriction
10402                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10403           t                             ;Nothing more.
10404         (gnus-narrow-to-page 1)         ;Go to next page.
10405         nil)
10406     ;; More in this page.
10407     (condition-case ()
10408         (scroll-up lines)
10409       (end-of-buffer
10410        ;; Long lines may cause an end-of-buffer error.
10411        (goto-char (point-max))))
10412     nil))
10413
10414 (defun gnus-article-prev-page (lines)
10415   "Show previous page of current article.
10416 Argument LINES specifies lines to be scrolled down."
10417   (interactive "P")
10418   (move-to-window-line 0)
10419   (if (and gnus-break-pages
10420            (bobp)
10421            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10422       (progn
10423         (gnus-narrow-to-page -1) ;Go to previous page.
10424         (goto-char (point-max))
10425         (recenter -1))
10426     (scroll-down lines)))
10427
10428 (defun gnus-article-refer-article ()
10429   "Read article specified by message-id around point."
10430   (interactive)
10431   (search-forward ">" nil t)    ;Move point to end of "<....>".
10432   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10433       (let ((message-id
10434              (buffer-substring (match-beginning 1) (match-end 1))))
10435         (set-buffer gnus-summary-buffer)
10436         (gnus-summary-refer-article message-id))
10437     (error "No references around point")))
10438
10439 (defun gnus-article-show-summary ()
10440   "Reconfigure windows to show summary buffer."
10441   (interactive)
10442   (gnus-configure-windows 'article)
10443   (gnus-summary-goto-subject gnus-current-article))
10444
10445 (defun gnus-article-describe-briefly ()
10446   "Describe article mode commands briefly."
10447   (interactive)
10448   (gnus-message 6
10449    (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")))
10450
10451 (defun gnus-article-summary-command ()
10452   "Execute the last keystroke in the summary buffer."
10453   (interactive)
10454   (let ((obuf (current-buffer))
10455         (owin (current-window-configuration))
10456         func)
10457     (switch-to-buffer gnus-summary-buffer 'norecord)
10458     (setq func (lookup-key (current-local-map) (this-command-keys)))
10459     (call-interactively func)
10460     (set-buffer obuf)
10461     (let ((npoint (point)))
10462       (set-window-configuration owin)
10463       (set-window-start (get-buffer-window (current-buffer)) (point)))))
10464
10465 (defun gnus-article-summary-command-nosave ()
10466   "Execute the last keystroke in the summary buffer."
10467   (interactive)
10468   (let ((obuf (current-buffer))
10469         (owin (current-window-configuration))
10470         func)
10471     (switch-to-buffer gnus-summary-buffer 'norecord)
10472     (setq func (lookup-key (current-local-map) (this-command-keys)))
10473     (call-interactively func)))
10474
10475 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10476 ;; Modified by tower@prep Nov 86
10477 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10478
10479 (defun gnus-caesar-region (&optional n)
10480   "Caesar rotation of region by N, default 13, for decrypting netnews.
10481 ROT47 will be performed for Japanese text in any case."
10482   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10483                    (list (prefix-numeric-value current-prefix-arg))
10484                  (list nil)))
10485   (cond ((not (numberp n)) (setq n 13))
10486         (t (setq n (mod n 26))))        ;canonicalize N
10487   (if (not (zerop n))           ; no action needed for a rot of 0
10488       (progn
10489         (if (or (not (boundp 'caesar-translate-table))
10490                 (not caesar-translate-table)
10491                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10492             (let ((i 0) 
10493                   (lower "abcdefghijklmnopqrstuvwxyz")
10494                   upper)
10495               (gnus-message 9 "Building caesar-translate-table...")
10496               (setq caesar-translate-table (make-vector 256 0))
10497               (while (< i 256)
10498                 (aset caesar-translate-table i i)
10499                 (setq i (1+ i)))
10500               (setq lower (concat lower lower)
10501                     upper (upcase lower)
10502                     i 0)
10503               (while (< i 26)
10504                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10505                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10506                 (setq i (1+ i)))
10507               ;; ROT47 for Japanese text.
10508               ;; Thanks to ichikawa@flab.fujitsu.junet.
10509               (setq i 161)
10510               (let ((t1 (logior ?O 128))
10511                     (t2 (logior ?! 128))
10512                     (t3 (logior ?~ 128)))
10513                 (while (< i 256)
10514                   (aset caesar-translate-table i
10515                         (let ((v (aref caesar-translate-table i)))
10516                           (if (<= v t1) (if (< v t2) v (+ v 47))
10517                             (if (<= v t3) (- v 47) v))))
10518                   (setq i (1+ i))))
10519               (gnus-message 9 "Building caesar-translate-table...done")))
10520         (let ((from (region-beginning))
10521               (to (region-end))
10522               (i 0) str len)
10523           (setq str (buffer-substring from to))
10524           (setq len (length str))
10525           (while (< i len)
10526             (aset str i (aref caesar-translate-table (aref str i)))
10527             (setq i (1+ i)))
10528           (goto-char from)
10529           (delete-region from to)
10530           (insert str)))))
10531
10532 \f
10533 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10534
10535 ;;;###autoload
10536 (defalias 'gnus-batch-kill 'gnus-batch-score)
10537 ;;;###autoload
10538 (defun gnus-batch-score ()
10539   "Run batched scoring.
10540 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10541 Newsgroups is a list of strings in Bnews format.  If you want to score
10542 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10543 score the alt hierarchy, you'd say \"!alt.all\"."
10544   (interactive)
10545   (let* ((yes-and-no
10546           (gnus-newsrc-parse-options
10547            (apply (function concat)
10548                   (mapcar (lambda (g) (concat g " "))
10549                           command-line-args-left))))
10550          (gnus-expert-user t)
10551          (nnmail-spool-file nil)
10552          (gnus-use-dribble-file nil)
10553          (yes (car yes-and-no))
10554          (no (cdr yes-and-no))
10555          group subscribed newsrc entry
10556          ;; Disable verbose message.
10557          gnus-novice-user gnus-large-newsgroup)
10558     ;; Eat all arguments.
10559     (setq command-line-args-left nil)
10560     ;; Start Gnus.
10561     (gnus)
10562     ;; Apply kills to specified newsgroups in command line arguments.
10563     (setq newsrc (cdr gnus-newsrc-alist))
10564     (while newsrc
10565       (setq group (car (car newsrc)))
10566       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10567       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10568                (and (car entry)
10569                     (or (eq (car entry) t)
10570                         (not (zerop (car entry)))))
10571                (if yes (string-match yes group) t)
10572                (or (null no) (not (string-match no group))))
10573           (progn
10574             (gnus-summary-read-group group nil t)
10575             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10576                  (gnus-summary-exit))))
10577       (setq newsrc (cdr newsrc)))
10578     ;; Exit Emacs.
10579     (switch-to-buffer gnus-group-buffer)
10580     (gnus-group-save-newsrc)))
10581
10582 (defun gnus-apply-kill-file ()
10583   "Apply a kill file to the current newsgroup.
10584 Returns the number of articles marked as read."
10585   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10586           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10587       (gnus-apply-kill-file-internal)
10588     0))
10589
10590 (defun gnus-kill-save-kill-buffer ()
10591   (save-excursion
10592     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10593       (if (get-file-buffer file)
10594           (progn
10595             (set-buffer (get-file-buffer file))
10596             (and (buffer-modified-p) (save-buffer))
10597             (kill-buffer (current-buffer)))))))
10598
10599 (defvar gnus-kill-file-name "KILL"
10600   "Suffix of the kill files.")
10601
10602 (defun gnus-newsgroup-kill-file (newsgroup)
10603   "Return the name of a kill file name for NEWSGROUP.
10604 If NEWSGROUP is nil, return the global kill file name instead."
10605   (cond ((or (null newsgroup)
10606              (string-equal newsgroup ""))
10607          ;; The global KILL file is placed at top of the directory.
10608          (expand-file-name gnus-kill-file-name
10609                            (or gnus-kill-files-directory "~/News")))
10610         ((gnus-use-long-file-name 'not-kill)
10611          ;; Append ".KILL" to newsgroup name.
10612          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10613                            (or gnus-kill-files-directory "~/News")))
10614         (t
10615          ;; Place "KILL" under the hierarchical directory.
10616          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10617                                    "/" gnus-kill-file-name)
10618                            (or gnus-kill-files-directory "~/News")))))
10619
10620 \f
10621 ;;;
10622 ;;; Dribble file
10623 ;;;
10624
10625 (defvar gnus-dribble-ignore nil)
10626
10627 (defun gnus-dribble-file-name ()
10628   (concat gnus-startup-file "-dribble"))
10629
10630 (defun gnus-dribble-open ()
10631   (save-excursion 
10632     (set-buffer 
10633      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10634     (buffer-disable-undo (current-buffer))
10635     (bury-buffer gnus-dribble-buffer)
10636     (auto-save-mode t)
10637     (goto-char (point-max))))
10638
10639 (defun gnus-dribble-enter (string)
10640   (if (and (not gnus-dribble-ignore)
10641            gnus-dribble-buffer
10642            (buffer-name gnus-dribble-buffer))
10643       (let ((obuf (current-buffer)))
10644         (set-buffer gnus-dribble-buffer)
10645         (insert string "\n")
10646         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10647         (set-buffer obuf))))
10648
10649 (defun gnus-dribble-read-file ()
10650   (let ((dribble-file (gnus-dribble-file-name)))
10651     (save-excursion 
10652       (set-buffer (setq gnus-dribble-buffer 
10653                         (get-buffer-create 
10654                          (file-name-nondirectory dribble-file))))
10655       (gnus-add-current-to-buffer-list)
10656       (erase-buffer)
10657       (set-visited-file-name dribble-file)
10658       (buffer-disable-undo (current-buffer))
10659       (bury-buffer (current-buffer))
10660       (set-buffer-modified-p nil)
10661       (let ((auto (make-auto-save-file-name))
10662             (gnus-dribble-ignore t))
10663         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10664             (progn
10665               (if (file-newer-than-file-p auto dribble-file)
10666                   (setq dribble-file auto))
10667               (insert-file-contents dribble-file)
10668               (if (not (zerop (buffer-size)))
10669                   (set-buffer-modified-p t))
10670               (if (gnus-y-or-n-p 
10671                    "Auto-save file exists. Do you want to read it? ")
10672                   (progn
10673                     (gnus-message 5 "Reading %s..." dribble-file) 
10674                     (eval-current-buffer)
10675                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10676
10677 (defun gnus-dribble-delete-file ()
10678   (if (file-exists-p (gnus-dribble-file-name))
10679       (delete-file (gnus-dribble-file-name)))
10680   (if gnus-dribble-buffer
10681       (save-excursion
10682         (set-buffer gnus-dribble-buffer)
10683         (let ((auto (make-auto-save-file-name)))
10684           (if (file-exists-p auto)
10685               (delete-file auto))
10686           (erase-buffer)
10687           (set-buffer-modified-p nil)))))
10688
10689 (defun gnus-dribble-save ()
10690   (if (and gnus-dribble-buffer
10691            (buffer-name gnus-dribble-buffer))
10692       (save-excursion
10693         (set-buffer gnus-dribble-buffer)
10694         (save-buffer))))
10695
10696 (defun gnus-dribble-clear ()
10697   (save-excursion
10698     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10699         (progn
10700           (set-buffer gnus-dribble-buffer)
10701           (erase-buffer)
10702           (set-buffer-modified-p nil)
10703           (setq buffer-saved-size (buffer-size))))))
10704
10705 ;;;
10706 ;;; Server Communication
10707 ;;;
10708
10709 ;; All the Gnus backends have the same interface, and should return
10710 ;; data in a similar format. Below is an overview of what functions
10711 ;; these packages must supply and what results they should return.
10712 ;;
10713 ;; Variables:
10714 ;;
10715 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10716 ;; buffer. 
10717 ;;
10718 ;; Functions for the imaginary backend `choke':
10719 ;;
10720 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10721 ;; Should return all headers for all ARTICLES, or return NOV lines for
10722 ;; the same.
10723 ;;
10724 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10725 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10726 ;; must be returned.
10727 ;;
10728 ;; `choke-close-group GROUP &optional SERVER'
10729 ;; Close group. Most backends won't have to do anything with this
10730 ;; call, but it is an opportunity to clean up, if that is needed. It
10731 ;; is called when Gnus exits a group.
10732 ;;
10733 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10734 ;; Return ARTICLE, which is either an article number or
10735 ;; message-id. Note that not all backends can return articles based on
10736 ;; message-id. 
10737 ;;
10738 ;; `choke-request-list SERVER'
10739 ;; Return a list of all newsgroups on SERVER.
10740 ;;
10741 ;; `choke-request-list-newsgroups SERVER'
10742 ;; Return a list of descriptions of all newsgroups on SERVER.
10743 ;;
10744 ;; `choke-request-newgroups DATE &optional SERVER'
10745 ;; Return a list of all groups that have arrived after DATE on
10746 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10747 ;; always check whether the groups are old or not. Backends that do
10748 ;; not store date information may just return the entire list of
10749 ;; groups, although this might not be a good idea in general.
10750 ;;
10751 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10752 ;; Should return a buffer that is suitable for "posting". nnspool and
10753 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10754 ;; buffer. This function should fill out the appropriate headers. 
10755 ;;
10756 ;; `choke-request-post &optional SERVER'
10757 ;; Function that will be called from a buffer to be posted. 
10758 ;;
10759 ;; `choke-open-server SERVER &optional ARGUMENT'
10760 ;; Open a connection to SERVER.
10761 ;;
10762 ;; `choke-close-server &optional SERVER'
10763 ;; Close the connection to SERVER.
10764 ;;
10765 ;; `choke-server-opened &optional SERVER'
10766 ;; Whether the conenction to SERVER is opened or not.
10767 ;;
10768 ;; `choke-server-status &optional SERVER'
10769 ;; Should return a status string (not in the nntp buffer, but as the
10770 ;; result of the function).
10771 ;;
10772 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10773 ;; Optional function for retrieving active file info on all groups in
10774 ;; GROUPS.  Two return formats are supported: The normal active file
10775 ;; format, and a list of GROUP lines.  This function should return (as
10776 ;; a function value) either `active' or `group', depending on what
10777 ;; format it returns.
10778 ;;
10779 ;; The following functions are optional and apply only to backends
10780 ;; that are able to control the contents of their groups totally
10781 ;; (ie. mail backends.)  Backends that aren't able to do that
10782 ;; shouldn't define these functions at all. Gnus will check for their
10783 ;; presence before attempting to call them.
10784 ;;
10785 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10786 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10787 ;; backends will not be able to expire articles. Should return a list
10788 ;; of all articles that were not expired.
10789 ;;
10790 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10791 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10792 ;; Removes any information it has added to the article (extra headers,
10793 ;; whatever - make it as clean as possible), and then passes the
10794 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
10795 ;; function described below. If the ACCEPT-FORM returns a non-nil
10796 ;; value, the article should then be deleted. If LAST is nil, that
10797 ;; means that there will be further calls to this function. This might
10798 ;; be taken as an advice not to save buffers/internal variables just
10799 ;; yet, but wait until the last call to speed things up.
10800 ;;
10801 ;; `choke-request-accept-article GROUP &optional LAST' 
10802 ;; The contents of the current buffer will be put into GROUP.  There
10803 ;; should, of course, be an article in the current buffer.  This
10804 ;; function is normally only called by the function described above,
10805 ;; and LAST works the same way as in that function.
10806 ;;
10807 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
10808 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
10809 ;; This provides an easy interface for allowing editing of
10810 ;; articles. Note that even headers may be edited, so the backend has
10811 ;; to update any tables (nov buffers, etc) that it maintains after
10812 ;; replacing the article.
10813 ;;
10814 ;; `choke-request-create-group GROUP &optional SERVER'
10815 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
10816 ;; might be a group that already exists, but hasn't been registered
10817 ;; yet. 
10818 ;;
10819 ;; All these functions must return nil if they couldn't service the
10820 ;; request. If the optional arguments are not supplied, some "current"
10821 ;; or "default" values should be used. In short, one should emulate an
10822 ;; NNTP server, in a way.
10823 ;;
10824 ;; If you want to write a new backend, you just have to supply the
10825 ;; functions listed above. In addition, you must enter the new backend
10826 ;; into the list of valid select methods:
10827 ;; (setq gnus-valid-select-methods 
10828 ;;       (cons '("choke" mail) gnus-valid-select-methods))
10829 ;; The first element in this list is the name of the backend. Other
10830 ;; elemnets may be `mail' (for mail groups),  `post' (for news
10831 ;; groups), `none' (neither), `respool' (for groups that can control
10832 ;; their contents). 
10833
10834 (defun gnus-start-news-server (&optional confirm)
10835   "Open a method for getting news.
10836 If CONFIRM is non-nil, the user will be asked for an NNTP server."
10837   (let (how where)
10838     (if gnus-current-select-method
10839         ;; Stream is already opened.
10840         nil
10841       ;; Open NNTP server.
10842       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
10843       (if confirm
10844           (progn
10845             ;; Read server name with completion.
10846             (setq gnus-nntp-server
10847                   (completing-read "NNTP server: "
10848                                    (mapcar (lambda (server) (list server))
10849                                            (cons (list gnus-nntp-server)
10850                                                  gnus-secondary-servers))
10851                                    nil nil gnus-nntp-server))))
10852
10853       (if (and gnus-nntp-server 
10854                (stringp gnus-nntp-server)
10855                (not (string= gnus-nntp-server "")))
10856           (setq gnus-select-method
10857                 (cond ((or (string= gnus-nntp-server "")
10858                            (string= gnus-nntp-server "::"))
10859                        (list 'nnspool (system-name)))
10860                       ((string-match "^:" gnus-nntp-server)
10861                        (list 'nnmh gnus-nntp-server 
10862                              (list 'nnmh-directory 
10863                                    (file-name-as-directory
10864                                     (expand-file-name
10865                                      (concat "~/" (substring
10866                                                    gnus-nntp-server 1)))))
10867                              (list 'nnmh-get-new-mail nil)))
10868                       (t
10869                        (list 'nntp gnus-nntp-server)))))
10870
10871       (setq how (car gnus-select-method))
10872       (setq where (car (cdr gnus-select-method)))
10873       (cond ((eq how 'nnspool)
10874              (require 'nnspool)
10875              (gnus-message 5 "Looking up local news spool..."))
10876             ((eq how 'nnmh)
10877              (require 'nnmh)
10878              (gnus-message 5 "Looking up mh spool..."))
10879             (t
10880              (require 'nntp)))
10881       (setq gnus-current-select-method gnus-select-method)
10882       (run-hooks 'gnus-open-server-hook)
10883       (or 
10884        ;; gnus-open-server-hook might have opened it
10885        (gnus-server-opened gnus-select-method)  
10886        (gnus-open-server gnus-select-method)
10887        (gnus-y-or-n-p
10888         (format
10889          "%s server on %s can't be opened. Continue? "
10890          (car gnus-select-method) (nth 1 gnus-select-method)))
10891        (progn
10892          (gnus-message 1 "Couldn't open server on %s" 
10893                        (nth 1 gnus-select-method))
10894          (ding)
10895          nil)))))
10896
10897 (defun gnus-check-news-server (&optional method)
10898   "If the news server is down, start it up again."
10899   (let ((method (if method method gnus-select-method)))
10900     (and (stringp method)
10901          (setq method (gnus-server-to-method method)))
10902     (if (gnus-server-opened method)
10903         ;; Stream is already opened.
10904         t
10905       ;; Open server.
10906       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
10907       (run-hooks 'gnus-open-server-hook)
10908       (or (gnus-server-opened method)
10909           (gnus-open-server method))
10910       (message ""))))
10911
10912 (defun gnus-nntp-message (&optional message)
10913   "Check the status of the NNTP server.
10914 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
10915 is returned insted of the status string."
10916   (let ((status (gnus-status-message (gnus-find-method-for-group 
10917                                       gnus-newsgroup-name)))
10918         (message (or message "")))
10919     (if (and (stringp status) (> (length status) 0))
10920         status message)))
10921
10922 (defun gnus-get-function (method function)
10923   (and (stringp method)
10924        (setq method (gnus-server-to-method method)))
10925   (let ((func (intern (format "%s-%s" (car method) function))))
10926     (if (not (fboundp func)) 
10927         (progn
10928           (require (car method))
10929           (if (not (fboundp func)) 
10930               (error "No such function: %s" func))))
10931     func))
10932
10933 ;;; Interface functions to the backends.
10934
10935 (defun gnus-open-server (method)
10936   (funcall (gnus-get-function method 'open-server)
10937            (nth 1 method) (nthcdr 2 method)))
10938
10939 (defun gnus-close-server (method)
10940   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
10941
10942 (defun gnus-request-list (method)
10943   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
10944
10945 (defun gnus-request-list-newsgroups (method)
10946   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
10947
10948 (defun gnus-request-newgroups (date method)
10949   (funcall (gnus-get-function method 'request-newgroups) 
10950            date (nth 1 method)))
10951
10952 (defun gnus-server-opened (method)
10953   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
10954
10955 (defun gnus-status-message (method)
10956   (let ((method (if (stringp method) (gnus-find-method-for-group method)
10957                   method)))
10958     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
10959
10960 (defun gnus-request-group (group &optional dont-check)
10961   (let ((method (gnus-find-method-for-group group)))
10962     (funcall (gnus-get-function method 'request-group) 
10963              (gnus-group-real-name group) (nth 1 method) dont-check)))
10964
10965 (defun gnus-request-asynchronous (group &optional articles)
10966   (let ((method (gnus-find-method-for-group group)))
10967     (funcall (gnus-get-function method 'request-asynchronous) 
10968              (gnus-group-real-name group) (nth 1 method) articles)))
10969
10970 (defun gnus-list-active-group (group)
10971   (let ((method (gnus-find-method-for-group group))
10972         (func 'list-active-group))
10973     (and (gnus-check-backend-function func group)
10974          (funcall (gnus-get-function method func) 
10975                   (gnus-group-real-name group) (nth 1 method)))))
10976
10977 (defun gnus-request-group-description (group)
10978   (let ((method (gnus-find-method-for-group group))
10979         (func 'request-group-description))
10980     (and (gnus-check-backend-function func group)
10981          (funcall (gnus-get-function method func) 
10982                   (gnus-group-real-name group) (nth 1 method)))))
10983
10984 (defun gnus-close-group (group)
10985   (let ((method (gnus-find-method-for-group group)))
10986     (funcall (gnus-get-function method 'close-group) 
10987              (gnus-group-real-name group) (nth 1 method))))
10988
10989 (defun gnus-retrieve-headers (articles group)
10990   (let ((method (gnus-find-method-for-group group)))
10991     (if gnus-use-cache
10992         (gnus-cache-retrieve-headers articles group)
10993       (funcall (gnus-get-function method 'retrieve-headers) 
10994                articles (gnus-group-real-name group) (nth 1 method)))))
10995
10996 (defun gnus-retrieve-groups (groups method)
10997   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
10998
10999 (defun gnus-request-article (article group &optional buffer)
11000   (let ((method (gnus-find-method-for-group group)))
11001     (funcall (gnus-get-function method 'request-article) 
11002              article (gnus-group-real-name group) (nth 1 method) buffer)))
11003
11004 (defun gnus-request-head (article group)
11005   (let ((method (gnus-find-method-for-group group)))
11006     (funcall (gnus-get-function method 'request-head) 
11007              article (gnus-group-real-name group) (nth 1 method))))
11008
11009 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11010 (defun gnus-request-post-buffer (post group subject header artbuf
11011                                       info follow-to respect-poster)
11012    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11013                                             group gnus-newsrc-hashtb)))))
11014           (method
11015            (if (and gnus-post-method
11016                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11017                     (memq 'post (assoc
11018                                  (format "%s" (car (gnus-find-method-for-group
11019                                                     gnus-newsgroup-name)))
11020                                         gnus-valid-select-methods)))
11021                gnus-post-method
11022              (gnus-find-method-for-group gnus-newsgroup-name))))
11023      (or (gnus-server-opened method)
11024          (gnus-open-server method)
11025          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11026      (let ((mail-self-blind nil)
11027            (mail-archive-file-name nil))
11028        (funcall (gnus-get-function method 'request-post-buffer) 
11029                 post group subject header artbuf info follow-to
11030                 respect-poster))))
11031
11032 (defun gnus-request-post (method &optional force)
11033   (and (stringp method)
11034        (setq method (gnus-server-to-method method)))
11035   (and (not force) gnus-post-method
11036        (memq 'post (assoc (format "%s" (car method))
11037                           gnus-valid-select-methods))
11038        (setq method gnus-post-method))
11039   (funcall (gnus-get-function method 'request-post) 
11040            (nth 1 method)))
11041
11042 (defun gnus-request-expire-articles (articles group &optional force)
11043   (let ((method (gnus-find-method-for-group group)))
11044     (funcall (gnus-get-function method 'request-expire-articles) 
11045              articles (gnus-group-real-name group) (nth 1 method)
11046              force)))
11047
11048 (defun gnus-request-move-article 
11049   (article group server accept-function &optional last)
11050   (let ((method (gnus-find-method-for-group group)))
11051     (funcall (gnus-get-function method 'request-move-article) 
11052              article (gnus-group-real-name group) 
11053              (nth 1 method) accept-function last)))
11054
11055 (defun gnus-request-accept-article (group &optional last)
11056   (let ((func (if (symbolp group) group
11057                 (car (gnus-find-method-for-group group)))))
11058     (funcall (intern (format "%s-request-accept-article" func))
11059              (if (stringp group) (gnus-group-real-name group) group)
11060              last)))
11061
11062 (defun gnus-request-replace-article (article group buffer)
11063   (let ((func (car (gnus-find-method-for-group group))))
11064     (funcall (intern (format "%s-request-replace-article" func))
11065              article (gnus-group-real-name group) buffer)))
11066
11067 (defun gnus-request-create-group (group)
11068   (let ((method (gnus-find-method-for-group group)))
11069     (funcall (gnus-get-function method 'request-create-group) 
11070              (gnus-group-real-name group) (nth 1 method))))
11071
11072 (defun gnus-member-of-valid (symbol group)
11073   (memq symbol (assoc
11074                 (format "%s" (car (gnus-find-method-for-group group)))
11075                 gnus-valid-select-methods)))
11076
11077 (defun gnus-find-method-for-group (group &optional info)
11078   (or gnus-override-method
11079       (and (not group)
11080            gnus-select-method)
11081       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11082             method)
11083         (if (or (not info)
11084                 (not (setq method (nth 4 info))))
11085             (setq method gnus-select-method)
11086           (setq method
11087                 (cond ((stringp method)
11088                        (gnus-server-to-method method))
11089                       ((stringp (car method))
11090                        (gnus-server-extend-method group method))
11091                       (t
11092                        method))))
11093         (gnus-server-add-address method))))
11094
11095 (defun gnus-check-backend-function (func group)
11096   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11097                  group)))
11098     (fboundp (intern (format "%s-%s" method func)))))
11099
11100 (defun gnus-methods-using (method)
11101   (let ((valids gnus-valid-select-methods)
11102         outs)
11103     (while valids
11104       (if (memq method (car valids)) 
11105           (setq outs (cons (car valids) outs)))
11106       (setq valids (cdr valids)))
11107     outs))
11108
11109 ;;; 
11110 ;;; Active & Newsrc File Handling
11111 ;;;
11112
11113 ;; Newsrc related functions.
11114 ;; Gnus internal format of gnus-newsrc-alist:
11115 ;; (("alt.general" 3 (1 . 1))
11116 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11117 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11118 ;; The first item is the group name; the second is the subscription
11119 ;; level; the third is either a range of a list of ranges of read
11120 ;; articles, the optional fourth element is a list of marked articles,
11121 ;; the optional fifth element is the select method.
11122 ;;
11123 ;; Gnus internal format of gnus-newsrc-hashtb:
11124 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11125 ;; This is the entry for "alt.misc". The first element is the number
11126 ;; of unread articles in "alt.misc". The cdr of this entry is the
11127 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11128 ;; trivial to remove or add new elements into gnus-newsrc-alist
11129 ;; without scanning the entire list. So, to get the actual information
11130 ;; of "alt.misc", you'd say something like 
11131 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11132 ;;
11133 ;; Gnus internal format of gnus-active-hashtb:
11134 ;; ((1 . 1))
11135 ;;  (5 . 10))
11136 ;;  (67 . 99)) ...)
11137 ;; The only element in each entry in this hash table is a range of
11138 ;; (possibly) available articles. (Articles in this range may have
11139 ;; been expired or cancelled.)
11140 ;;
11141 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11142 ;; ("alt.misc" "alt.test" "alt.general" ...)
11143
11144 (defun gnus-setup-news (&optional rawfile level)
11145   "Setup news information.
11146 If RAWFILE is non-nil, the .newsrc file will also be read.
11147 If LEVEL is non-nil, the news will be set up at level LEVEL."
11148   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11149     ;; Clear some variables to re-initialize news information.
11150     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11151
11152     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11153     (if init (gnus-read-newsrc-file rawfile))
11154
11155     ;; Read the active file and create `gnus-active-hashtb'.
11156     ;; If `gnus-read-active-file' is nil, then we just create an empty
11157     ;; hash table. The partial filling out of the hash table will be
11158     ;; done in `gnus-get-unread-articles'.
11159     (if (and gnus-read-active-file 
11160              (not level)
11161              (gnus-server-opened gnus-select-method))
11162         (gnus-read-active-file)
11163       (setq gnus-active-hashtb (make-vector 4095 0)))
11164
11165     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11166
11167     ;; Find the number of unread articles in each non-dead group.
11168     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11169     ;; Find new newsgroups and treat them.
11170     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11171              (gnus-server-opened gnus-select-method))
11172         (gnus-find-new-newsgroups))
11173     (if (and init gnus-check-bogus-newsgroups 
11174              gnus-read-active-file (not level)
11175              (gnus-server-opened gnus-select-method))
11176         (gnus-check-bogus-newsgroups))))
11177
11178 (defun gnus-find-new-newsgroups ()
11179   "Search for new newsgroups and add them.
11180 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11181 The `-n' option line from .newsrc is respected."
11182   (interactive)
11183   (or (gnus-check-first-time-used)
11184       (if (or (consp gnus-check-new-newsgroups)
11185               (eq gnus-check-new-newsgroups 'ask-server))
11186           (gnus-ask-server-for-new-groups)
11187         (let ((groups 0)
11188               group new-newsgroups)
11189           (or gnus-have-read-active-file (gnus-read-active-file))
11190           (setq gnus-newsrc-last-checked-date (current-time-string))
11191           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11192           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11193           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11194           (mapatoms
11195            (lambda (sym)
11196              (setq group (symbol-name sym))
11197              (if (or (gnus-gethash group gnus-killed-hashtb)
11198                      (gnus-gethash group gnus-newsrc-hashtb))
11199                  ()
11200                (let ((do-sub (gnus-matches-options-n group)))
11201                  (cond ((eq do-sub 'subscribe)
11202                         (setq groups (1+ groups))
11203                         (gnus-sethash group group gnus-killed-hashtb)
11204                         (funcall 
11205                          gnus-subscribe-options-newsgroup-method group))
11206                        ((eq do-sub 'ignore)
11207                         nil)
11208                        (t
11209                         (setq groups (1+ groups))
11210                         (gnus-sethash group group gnus-killed-hashtb)
11211                         (if gnus-subscribe-hierarchical-interactive
11212                             (setq new-newsgroups (cons group new-newsgroups))
11213                           (funcall gnus-subscribe-newsgroup-method group)))))))
11214            gnus-active-hashtb)
11215           (if new-newsgroups 
11216               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11217           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11218           (if (> groups 0)
11219               (gnus-message 6 "%d new newsgroup%s arrived." 
11220                             groups (if (> groups 1) "s have" " has")))))))
11221
11222 (defun gnus-matches-options-n (group)
11223   ;; Returns `subscribe' if the group is to be uncoditionally
11224   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11225   ;; no match for the group.
11226
11227   ;; First we check the two user variables.
11228   (cond
11229    ((and gnus-options-subscribe
11230          (string-match gnus-options-not-subscribe group))
11231     'subscribe)
11232    ((and gnus-options-not-subscribe
11233          (string-match gnus-options-subscribe group))
11234     'ignore)
11235    ;; Then we go through the list that was retrieved from the .newsrc
11236    ;; file.  This list has elements on the form 
11237    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11238    ;; is in the reverse order of the options line) is returned.
11239    (t
11240     (let ((regs gnus-newsrc-options-n))
11241       (while (and regs
11242                   (not (string-match (car (car regs)) group)))
11243         (setq regs (cdr regs)))
11244       (and regs (cdr (car regs)))))))
11245
11246 (defun gnus-ask-server-for-new-groups ()
11247   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11248          (methods (cons gnus-select-method 
11249                         (append
11250                          (and (consp gnus-check-new-newsgroups)
11251                               gnus-check-new-newsgroups)
11252                          gnus-secondary-select-methods)))
11253          (groups 0)
11254          (new-date (current-time-string))
11255          hashtb group new-newsgroups got-new)
11256     ;; Go thorugh both primary and secondary select methods and
11257     ;; request new newsgroups.  
11258     (while methods
11259       (if (gnus-request-newgroups date (car methods))
11260           (save-excursion
11261             (setq got-new t)
11262             (or hashtb (setq hashtb (gnus-make-hashtable 
11263                                      (count-lines (point-min) (point-max)))))
11264             (set-buffer nntp-server-buffer)
11265             ;; Enter all the new groups in a hashtable.
11266             (gnus-active-to-gnus-format (car methods) hashtb)))
11267       (setq methods (cdr methods)))
11268     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11269     ;; Now all new groups from all select methods are in `hashtb'.
11270     (mapatoms
11271      (lambda (group-sym)
11272        (setq group (symbol-name group-sym))
11273        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11274                (member group gnus-zombie-list)
11275                (member group gnus-killed-list))
11276            ;; The group is already known.
11277            ()
11278          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
11279          (let ((do-sub (gnus-matches-options-n group)))
11280            (cond ((eq do-sub 'subscribe)
11281                   (setq groups (1+ groups))
11282                   (gnus-sethash group group gnus-killed-hashtb)
11283                   (funcall 
11284                    gnus-subscribe-options-newsgroup-method group))
11285                  ((eq do-sub 'ignore)
11286                   nil)
11287                  (t
11288                   (setq groups (1+ groups))
11289                   (gnus-sethash group group gnus-killed-hashtb)
11290                   (if gnus-subscribe-hierarchical-interactive
11291                       (setq new-newsgroups (cons group new-newsgroups))
11292                     (funcall gnus-subscribe-newsgroup-method group)))))))
11293      hashtb)
11294     (if new-newsgroups 
11295         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11296     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11297     (if (> groups 0)
11298         (gnus-message 6 "%d new newsgroup%s arrived." 
11299                       groups (if (> groups 1) "s have" " has")))
11300     got-new))
11301
11302 (defun gnus-check-first-time-used ()
11303   (if (or (> (length gnus-newsrc-alist) 1)
11304           (file-exists-p gnus-startup-file)
11305           (file-exists-p (concat gnus-startup-file ".el"))
11306           (file-exists-p (concat gnus-startup-file ".eld")))
11307       nil
11308     (gnus-message 6 "First time user; subscribing you to default groups")
11309     (or gnus-have-read-active-file (gnus-read-active-file))
11310     (setq gnus-newsrc-last-checked-date (current-time-string))
11311     (let ((groups gnus-default-subscribed-newsgroups)
11312           group)
11313       (if (eq groups t)
11314           nil
11315         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11316         (mapatoms
11317          (lambda (sym)
11318            (setq group (symbol-name sym))
11319            (let ((do-sub (gnus-matches-options-n group)))
11320              (cond ((eq do-sub 'subscribe)
11321                     (gnus-sethash group group gnus-killed-hashtb)
11322                     (funcall 
11323                      gnus-subscribe-options-newsgroup-method group))
11324                    ((eq do-sub 'ignore)
11325                     nil)
11326                    (t
11327                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11328          gnus-active-hashtb)
11329         (while groups
11330           (if (gnus-gethash (car groups) gnus-active-hashtb)
11331               (gnus-group-change-level 
11332                (car groups) gnus-level-default-subscribed gnus-level-killed))
11333           (setq groups (cdr groups)))
11334         (gnus-group-make-help-group)
11335         (and gnus-novice-user
11336              (gnus-message 7 "`A k' to list killed groups"))))))
11337
11338 (defun gnus-subscribe-group (group previous &optional method)
11339   (gnus-group-change-level 
11340    (if method
11341        (list t group gnus-level-default-subscribed nil nil method)
11342      group) 
11343    gnus-level-default-subscribed gnus-level-killed previous t))
11344
11345 ;; `gnus-group-change-level' is the fundamental function for changing
11346 ;; subscription levels of newsgroups. This might mean just changing
11347 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11348 ;; again, which subscribes/unsubscribes a group, which is equally
11349 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11350 ;; from 8-9 to 1-7 means that you remove the group from the list of
11351 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11352 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11353 ;; which is trivial.
11354 ;; ENTRY can either be a string (newsgroup name) or a list (if
11355 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11356 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11357 ;; entries. 
11358 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11359 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11360 ;; after. 
11361 (defun gnus-group-change-level (entry level &optional oldlevel
11362                                       previous fromkilled)
11363   (let ((pinfo entry)
11364         group info active num)
11365     ;; Glean what info we can from the arguments
11366     (if (consp entry)
11367         (if fromkilled (setq group (nth 1 entry))
11368           (setq group (car (nth 2 entry))))
11369       (setq group entry))
11370     (if (and (stringp entry)
11371              oldlevel 
11372              (< oldlevel gnus-level-zombie))
11373         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11374     (if (and (not oldlevel)
11375              (listp entry))
11376         (setq oldlevel (car (cdr (nth 2 entry)))))
11377     (if (stringp previous)
11378         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11379
11380     (gnus-dribble-enter
11381      (format "(gnus-group-change-level %S %S %S %S %S)" 
11382              group level oldlevel (car (nth 2 previous)) fromkilled))
11383     
11384     ;; Then we remove the newgroup from any old structures, if needed.
11385     ;; If the group was killed, we remove it from the killed or zombie
11386     ;; list. If not, and it is in fact going to be killed, we remove
11387     ;; it from the newsrc hash table and assoc.
11388     (cond ((>= oldlevel gnus-level-zombie)
11389            (if (= oldlevel gnus-level-zombie)
11390                (setq gnus-zombie-list (delete group gnus-zombie-list))
11391              (setq gnus-killed-list (delete group gnus-killed-list))))
11392           (t
11393            (if (>= level gnus-level-zombie)
11394                (progn
11395                  (gnus-sethash (car (nth 2 entry))
11396                                nil gnus-newsrc-hashtb)
11397                  (if (nth 3 entry)
11398                      (setcdr (gnus-gethash (car (nth 3 entry))
11399                                            gnus-newsrc-hashtb)
11400                              (cdr entry)))
11401                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11402
11403     ;; Finally we enter (if needed) the list where it is supposed to
11404     ;; go, and change the subscription level. If it is to be killed,
11405     ;; we enter it into the killed or zombie list.
11406     (cond ((>= level gnus-level-zombie)
11407            ;; Remove from the hash table.
11408            (gnus-sethash group nil gnus-newsrc-hashtb)
11409            (or (gnus-group-foreign-p group)
11410                ;; We do not enter foreign groups into the list of dead
11411                ;; groups.  
11412                (if (= level gnus-level-zombie)
11413                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11414                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11415           (t
11416            ;; If the list is to be entered into the newsrc assoc, and
11417            ;; it was killed, we have to create an entry in the newsrc
11418            ;; hashtb format and fix the pointers in the newsrc assoc.
11419            (if (>= oldlevel gnus-level-zombie)
11420                (progn
11421                  (if (listp entry)
11422                      (progn
11423                        (setq info (cdr entry))
11424                        (setq num (car entry)))
11425                    (setq active (gnus-gethash group gnus-active-hashtb))
11426                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11427                    ;; Check whether the group is foreign. If so, the
11428                    ;; foreign select method has to be entered into the
11429                    ;; info. 
11430                    (let ((method (gnus-group-method-name group)))
11431                      (if (eq method gnus-select-method)
11432                          (setq info (list group level nil))
11433                        (setq info (list group level nil nil method)))))
11434                  (or previous 
11435                      (setq previous 
11436                            (let ((p gnus-newsrc-alist))
11437                              (while (cdr (cdr p))
11438                                (setq p (cdr p)))
11439                              p)))
11440                  (setq entry (cons info (cdr (cdr previous))))
11441                  (if (cdr previous)
11442                      (progn
11443                        (setcdr (cdr previous) entry)
11444                        (gnus-sethash group (cons num (cdr previous)) 
11445                                      gnus-newsrc-hashtb))
11446                    (setcdr previous entry)
11447                    (gnus-sethash group (cons num previous)
11448                                  gnus-newsrc-hashtb))
11449                  (if (cdr entry)
11450                      (setcdr (gnus-gethash (car (car (cdr entry)))
11451                                            gnus-newsrc-hashtb)
11452                              entry)))
11453              ;; It was alive, and it is going to stay alive, so we
11454              ;; just change the level and don't change any pointers or
11455              ;; hash table entries.
11456              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11457
11458 (defun gnus-kill-newsgroup (newsgroup)
11459   "Obsolete function. Kills a newsgroup."
11460   (gnus-group-change-level
11461    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11462
11463 (defun gnus-check-bogus-newsgroups (&optional confirm)
11464   "Remove bogus newsgroups.
11465 If CONFIRM is non-nil, the user has to confirm the deletion of every
11466 newsgroup." 
11467   (let ((newsrc (cdr gnus-newsrc-alist))
11468         bogus group)
11469     (gnus-message 5 "Checking bogus newsgroups...")
11470     (or gnus-have-read-active-file (gnus-read-active-file))
11471     ;; Find all bogus newsgroup that are subscribed.
11472     (while newsrc
11473       (setq group (car (car newsrc)))
11474       (if (or (gnus-gethash group gnus-active-hashtb)
11475               (nth 4 (car newsrc))
11476               (and confirm
11477                    (not (gnus-y-or-n-p
11478                          (format "Remove bogus newsgroup: %s " group)))))
11479           ;; Active newsgroup.
11480           ()
11481         ;; Found a bogus newsgroup.
11482         (setq bogus (cons group bogus)))
11483       (setq newsrc (cdr newsrc)))
11484     ;; Remove all bogus subscribed groups by first killing them, and
11485     ;; then removing them from the list of killed groups.
11486     (while bogus
11487       (gnus-group-change-level 
11488        (gnus-gethash (car bogus) gnus-newsrc-hashtb) gnus-level-killed)
11489       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
11490       (setq bogus (cdr bogus)))
11491     ;; Then we remove all bogus groups from the list of killed and
11492     ;; zombie groups. They are are removed without confirmation.
11493     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11494           killed)
11495       (while dead-lists
11496         (setq killed (symbol-value (car dead-lists)))
11497         (while killed
11498           (setq group (car killed))
11499           (or (gnus-gethash group gnus-active-hashtb)
11500               ;; The group is bogus.
11501               (set (car dead-lists)
11502                    (delete group (symbol-value (car dead-lists)))))
11503           (setq killed (cdr killed)))
11504         (setq dead-lists (cdr dead-lists))))
11505     (gnus-message 5 "Checking bogus newsgroups...done")))
11506
11507 (defun gnus-check-duplicate-killed-groups ()
11508   "Remove duplicates from the list of killed groups."
11509   (interactive)
11510   (let ((killed gnus-killed-list))
11511     (while killed
11512       (gnus-message 9 "%d" (length killed))
11513       (setcdr killed (delete (car killed) (cdr killed)))
11514       (setq killed (cdr killed)))))
11515
11516 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11517 ;; and compute how many unread articles there are in each group.
11518 (defun gnus-get-unread-articles (&optional level)
11519   (let* ((newsrc (cdr gnus-newsrc-alist))
11520          (conditional level)
11521          (level (or level (1+ gnus-level-subscribed)))
11522          info group active virtuals method)
11523     (gnus-message 5 "Checking new news...")
11524     (while newsrc
11525       (setq info (car newsrc))
11526       (setq group (car info))
11527       (setq active (gnus-gethash group gnus-active-hashtb))
11528
11529       ;; Check newsgroups. If the user doesn't want to check them, or
11530       ;; they can't be checked (for instance, if the news server can't
11531       ;; be reached) we just set the number of unread articles in this
11532       ;; newsgroup to t. This means that Gnus thinks that there are
11533       ;; unread articles, but it has no idea how many.
11534       (if (and (setq method (nth 4 info))
11535                (not (gnus-server-equal gnus-select-method
11536                                        (gnus-server-get-method nil method))))
11537           (if (or (and gnus-activate-foreign-newsgroups 
11538                        (not (numberp gnus-activate-foreign-newsgroups)))
11539                   (and (numberp gnus-activate-foreign-newsgroups)
11540                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
11541                        (<= (nth 1 info) level)))
11542               (if (eq (car (if (stringp method) 
11543                                (gnus-server-to-method method)
11544                              (nth 4 info))) 'nnvirtual)
11545                   (setq virtuals (cons info virtuals))
11546                 (setq active (gnus-activate-newsgroup (car info)))))
11547         (if (and (not gnus-read-active-file)
11548                  (<= (nth 1 info) level))
11549             (progn
11550               (setq active (gnus-activate-newsgroup (car info))))))
11551       
11552       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
11553                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
11554       (and active 
11555            (gnus-get-unread-articles-in-group info active)
11556            ;; Close the groups as we look at them!
11557            (gnus-close-group group))
11558       (setq newsrc (cdr newsrc)))
11559
11560     ;; Activate the virtual groups. This has to be done after all the
11561     ;; other groups. 
11562     ;; !!! If one virtual group contains another virtual group, even
11563     ;; doing it this way might cause problems.
11564    (while virtuals
11565       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11566            (gnus-get-unread-articles-in-group (car virtuals) active))
11567       (setq virtuals (cdr virtuals)))
11568
11569     (gnus-message 5 "Checking new news...done")))
11570
11571 ;; Create a hash table out of the newsrc alist. The `car's of the
11572 ;; alist elements are used as keys.
11573 (defun gnus-make-hashtable-from-newsrc-alist ()
11574   (let ((alist gnus-newsrc-alist)
11575          prev)
11576     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11577     (setq alist 
11578           (setq prev (setq gnus-newsrc-alist 
11579                            (cons (list "dummy.group" 0 nil) alist))))
11580     (while alist
11581       (gnus-sethash (car (car alist)) (cons nil prev) gnus-newsrc-hashtb)
11582       (setq prev alist)
11583       (setq alist (cdr alist)))))
11584
11585 (defun gnus-make-hashtable-from-killed ()
11586   "Create a hash table from the killed and zombie lists."
11587   (let ((lists '(gnus-killed-list gnus-zombie-list))
11588         list)
11589     (setq gnus-killed-hashtb 
11590           (gnus-make-hashtable 
11591            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11592     (while lists
11593       (setq list (symbol-value (car lists)))
11594       (setq lists (cdr lists))
11595       (while list
11596         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11597         (setq list (cdr list))))))
11598
11599 (defun gnus-get-unread-articles-in-group (info active)
11600   (let* ((range (nth 2 info))
11601          (num 0)
11602          (marked (nth 3 info))
11603          srange lowest group highest)
11604     ;; If a cache is present, we may have to alter the active info.
11605     (and gnus-use-cache
11606          (gnus-cache-possibly-alter-active (car info) active))
11607     ;; Modify the list of read articles according to what articles 
11608     ;; are available; then tally the unread articles and add the
11609     ;; number to the group hash table entry.
11610     (cond ((zerop (cdr active))
11611            (setq num 0))
11612           ((not range)
11613            (setq num (- (1+ (cdr active)) (car active))))
11614           ((not (listp (cdr range)))
11615            ;; Fix a single (num . num) range according to the
11616            ;; active hash table.
11617            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11618            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11619            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11620            ;; Compute number of unread articles.
11621            (setq num (max 0 (- (cdr active) 
11622                                (- (1+ (cdr range)) (car range))))))
11623           (t
11624            ;; The read list is a list of ranges. Fix them according to
11625            ;; the active hash table.
11626            ;; First peel off any elements that are below the lower
11627            ;; active limit. 
11628            (while (and (cdr range) 
11629                        (>= (car active) 
11630                            (or (and (atom (car (cdr range))) (car (cdr range)))
11631                                (car (car (cdr range))))))
11632              (if (numberp (car range))
11633                  (setcar range 
11634                          (cons (car range) 
11635                                (or (and (numberp (car (cdr range)))
11636                                         (car (cdr range))) 
11637                                    (cdr (car (cdr range))))))
11638                (setcdr (car range) 
11639                        (or (and (numberp (nth 1 range)) (nth 1 range))
11640                            (cdr (car (cdr range))))))
11641              (setcdr range (cdr (cdr range))))
11642            ;; Adjust the first element to be the same as the lower limit. 
11643            (if (and (not (atom (car range))) 
11644                     (< (cdr (car range)) (car active)))
11645                (setcdr (car range) (1- (car active))))
11646            ;; Then we want to peel off any elements that are higher
11647            ;; than the upper active limit.  
11648            (let ((srange range))
11649              ;; Go past all legal elements.
11650              (while (and (cdr srange) 
11651                          (<= (or (and (atom (car (cdr srange)))
11652                                       (car (cdr srange)))
11653                                  (car (car (cdr srange)))) (cdr active)))
11654                (setq srange (cdr srange)))
11655              (if (cdr srange)
11656                  ;; Nuke all remaining illegal elements.
11657                  (setcdr srange nil))
11658
11659              ;; Adjust the final element.
11660              (if (and (not (atom (car srange)))
11661                       (> (cdr (car srange)) (cdr active)))
11662                  (setcdr (car srange) (cdr active))))
11663            ;; Compute the number of unread articles.
11664            (while range
11665              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11666                                          (cdr (car range))))
11667                                  (or (and (atom (car range)) (car range))
11668                                      (car (car range))))))
11669              (setq range (cdr range)))
11670            (setq num (max 0 (- (cdr active) num)))))
11671     (and info
11672          (progn
11673            (and (assq 'tick marked)
11674                 (inline (gnus-remove-illegal-marked-articles
11675                          (assq 'tick marked) (nth 2 info))))
11676            (and (assq 'dormant marked)
11677                 (inline (gnus-remove-illegal-marked-articles
11678                          (assq 'dormant marked) (nth 2 info))))
11679            (setcar
11680             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11681             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11682                                 (length (cdr (assq 'dormant marked)))))))))
11683     num))
11684
11685 (defun gnus-remove-illegal-marked-articles (marked ranges)
11686   (let ((m (cdr marked)))
11687     ;; Make sure that all ticked articles are a subset of the unread
11688     ;; articles. 
11689     (while m
11690       (if (gnus-member-of-range (car m) ranges)
11691           (setcdr marked (cdr m))
11692         (setq marked m))
11693       (setq m (cdr m)))))
11694
11695 (defun gnus-activate-newsgroup (group)
11696   (let ((method (gnus-find-method-for-group group))
11697         active)
11698     (and (or (gnus-server-opened method) (gnus-open-server method))
11699          (gnus-request-group group)
11700          (save-excursion
11701            (set-buffer nntp-server-buffer)
11702            (goto-char (point-min))
11703            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11704                 (progn
11705                   (goto-char (match-beginning 1))
11706                   (gnus-sethash 
11707                    group (setq active (cons (read (current-buffer))
11708                                             (read (current-buffer))))
11709                    gnus-active-hashtb))
11710                 active)))))
11711
11712 (defun gnus-update-read-articles 
11713   (group unread unselected ticked &optional domarks replied expirable killed
11714          dormant bookmark score)
11715   "Update the list of read and ticked articles in GROUP using the
11716 UNREAD and TICKED lists.
11717 Note: UNSELECTED has to be sorted over `<'.
11718 Returns whether the updating was successful."
11719   (let* ((active (or gnus-newsgroup-active 
11720                      (gnus-gethash group gnus-active-hashtb)))
11721          (entry (gnus-gethash group gnus-newsrc-hashtb))
11722          (number (car entry))
11723          (info (nth 2 entry))
11724          (marked (nth 3 info))
11725          (prev 1)
11726          (unread (sort (copy-sequence unread) (function <)))
11727          last read)
11728     (if (or (not info) (not active))
11729         ;; There is no info on this group if it was, in fact,
11730         ;; killed. Gnus stores no information on killed groups, so
11731         ;; there's nothing to be done. 
11732         ;; One could store the information somewhere temporarily,
11733         ;; perhaps... Hmmm... 
11734         ()
11735       ;; Remove any negative articles numbers.
11736       (while (and unread (< (car unread) 0))
11737         (setq unread (cdr unread)))
11738       ;; Remove any expired article numbers
11739       (while (and unread (< (car unread) (car active)))
11740         (setq unread (cdr unread)))
11741       (while (and ticked (< (car ticked) (car active)))
11742         (setq ticked (cdr ticked)))
11743       (while (and dormant (< (car dormant) (car active)))
11744         (setq dormant (cdr dormant)))
11745       (setq unread (sort (append unselected unread) '<))
11746       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11747       (setcar entry (max 0 (- (length unread) (length ticked) 
11748                               (length dormant))))
11749       ;; Compute the ranges of read articles by looking at the list of
11750       ;; unread articles.  
11751       (while unread
11752         (if (/= (car unread) prev)
11753             (setq read (cons (if (= prev (1- (car unread))) prev
11754                                (cons prev (1- (car unread)))) read)))
11755         (setq prev (1+ (car unread)))
11756         (setq unread (cdr unread)))
11757       (if (<= prev (cdr active))
11758           (setq read (cons (cons prev (cdr active)) read)))
11759       ;; Enter this list into the group info.
11760       (setcar (cdr (cdr info)) 
11761               (if (> (length read) 1) (nreverse read) read))
11762       ;; Enter the list of ticked articles.
11763       (gnus-set-marked-articles 
11764        info ticked
11765        (if domarks replied (cdr (assq 'reply marked)))
11766        (if domarks expirable (cdr (assq 'expire marked)))
11767        (if domarks killed (cdr (assq 'killed marked)))
11768        (if domarks dormant (cdr (assq 'dormant marked)))
11769        (if domarks bookmark (cdr (assq 'bookmark marked)))
11770        (if domarks score (cdr (assq 'score marked))))
11771       t)))
11772
11773 (defun gnus-make-articles-unread (group articles)
11774   "Mark ARTICLES in GROUP as unread."
11775   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
11776                           (gnus-gethash (gnus-group-real-name group)
11777                                         gnus-newsrc-hashtb))))
11778          (ranges (nth 2 info))
11779          news)
11780     (while articles
11781       (and (gnus-member-of-range (car articles) ranges)
11782            (setq news (cons (car articles) news)))
11783       (setq articles (cdr articles)))
11784     (if (not news)
11785         ()
11786       (setcar (nthcdr 2 info)
11787               (gnus-remove-from-range (nth 2 info) (nreverse news)))
11788       (gnus-group-update-group group t))))
11789
11790 (defun gnus-read-active-file ()
11791   "Get active file from NNTP server."
11792   (gnus-group-set-mode-line)
11793   (let ((methods (cons gnus-select-method gnus-secondary-select-methods))
11794         (not-first nil)
11795         list-type)
11796     (setq gnus-have-read-active-file nil)
11797     (save-excursion
11798       (set-buffer nntp-server-buffer)
11799       (while methods
11800         (let* ((where (nth 1 (car methods)))
11801                (mesg (format "Reading active file%s via %s..."
11802                              (if (and where (not (zerop (length where))))
11803                                  (concat " from " where) "")
11804                              (car (car methods)))))
11805           (gnus-message 5 mesg)
11806           (gnus-check-news-server (car methods))
11807           (cond 
11808            ((and (eq gnus-read-active-file 'some)
11809                  (gnus-check-backend-function
11810                   'retrieve-groups (car (car methods))))
11811             (let ((newsrc (cdr gnus-newsrc-alist))
11812                   groups)
11813               (while newsrc
11814                 (and (gnus-server-equal 
11815                       (gnus-find-method-for-group
11816                        (car (car newsrc)) (car newsrc))
11817                       (gnus-server-get-method nil (car methods)))
11818                      (setq groups (cons (car (car newsrc)) groups)))
11819                 (setq newsrc (cdr newsrc)))
11820               (setq list-type (gnus-retrieve-groups groups (car methods)))
11821               (cond ((not list-type)
11822                      (gnus-message 
11823                       1 "Cannot read partial active file from %s server." 
11824                       (car (car methods)))
11825                      (ding)
11826                      (sit-for 2))
11827                     ((eq list-type 'active)
11828                      (gnus-active-to-gnus-format (and not-first (car methods)))
11829                      (setq not-first t))
11830                     (t
11831                      (gnus-groups-to-gnus-format (and not-first (car methods)))
11832                      (setq not-first t)))))
11833            (t
11834             (if (not (gnus-request-list (car methods)))
11835                 (progn
11836                   (gnus-message 1 "Cannot read active file from %s server." 
11837                                 (car (car methods)))
11838                   (ding))
11839               (gnus-active-to-gnus-format 
11840                (and gnus-have-read-active-file (car methods)))
11841               (setq gnus-have-read-active-file t)
11842               (gnus-message 5 "%s...done" mesg)))))
11843         (setq methods (cdr methods))))))
11844
11845 ;; Read an active file and place the results in `gnus-active-hashtb'.
11846 (defun gnus-active-to-gnus-format (method &optional hashtb)
11847   (let ((cur (current-buffer))
11848         (hashtb (or hashtb 
11849                     (if method
11850                         gnus-active-hashtb
11851                       (setq gnus-active-hashtb
11852                             (gnus-make-hashtable 
11853                              (count-lines (point-min) (point-max))))))))
11854     ;; Delete unnecessary lines.
11855     (goto-char (point-min))
11856     (while (search-forward "\nto." nil t)
11857       (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
11858     (or (string= gnus-ignored-newsgroups "")
11859         (progn
11860           (goto-char (point-min))
11861           (delete-matching-lines gnus-ignored-newsgroups)))
11862     (and method (not (eq method gnus-select-method))
11863          (let ((prefix (gnus-group-prefixed-name "" method)))
11864            (goto-char (point-min))
11865            (while (and (not (eobp))
11866                        (null (insert prefix))
11867                        (zerop (forward-line 1))))))
11868     (goto-char (point-min))
11869     ;; Store active file in hashtable.
11870     (goto-char (point-min))
11871     (if (string-match "%[oO]" gnus-group-line-format)
11872         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
11873         ;; If we want information on moderated groups, we use this
11874         ;; loop...   
11875         (let* ((mod-hashtb (make-vector 7 0))
11876                (m (intern "m" mod-hashtb))
11877                group max mod min)
11878           (while (not (eobp))
11879             (condition-case nil
11880                 (progn
11881                   (narrow-to-region (point) (gnus-point-at-eol))
11882                   (setq group (let ((obarray hashtb)) (read cur)))
11883                   (and (numberp (setq max (read cur)))
11884                        (numberp (setq min (read cur)))
11885                        (set group (cons min max)))
11886                   ;; Enter moderated groups into a list.
11887                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
11888                       (setq gnus-moderated-list 
11889                             (cons (symbol-name group) gnus-moderated-list))))
11890               (error nil))
11891             (widen)
11892             (forward-line 1)))
11893       ;; And if we do not care about moderation, we use this loop,
11894       ;; which is faster.
11895       (let (group max min)
11896         (while (not (eobp))
11897           (condition-case ()
11898               (progn
11899                 (narrow-to-region (point) (gnus-point-at-eol))
11900                 ;; group gets set to a symbol interned in the hash table
11901                 ;; (what a hack!!)
11902                 (setq group (let ((obarray hashtb)) (read cur)))
11903                 (and (numberp (setq max (read cur)))
11904                      (numberp (setq min (read cur)))
11905                      (set group (cons min max))))
11906             (error 
11907              (progn (ding) 
11908                     (gnus-message 3 "Warning - illegal active: %s"
11909                                   (buffer-substring 
11910                                    (gnus-point-at-bol) (gnus-point-at-eol)))
11911                     nil)))
11912           (widen)
11913           (forward-line 1))))))
11914
11915 (defun gnus-groups-to-gnus-format (method &optional hashtb)
11916   ;; Parse a "groups" active file.
11917   (let ((cur (current-buffer))
11918         (hashtb (or hashtb 
11919                     (if method
11920                         gnus-active-hashtb
11921                       (setq gnus-active-hashtb
11922                             (gnus-make-hashtable 
11923                              (count-lines (point-min) (point-max)))))))
11924         (prefix (and method (not (eq method gnus-select-method))
11925                      (gnus-group-prefixed-name "" method))))
11926
11927     (goto-char (point-min))
11928     (condition-case ()
11929         ;; We split this into to separate loops, one with the prefix
11930         ;; and one without to speed the reading up somewhat.
11931         (if prefix
11932             (let (min max opoint)
11933               (while (not (eobp))
11934                 (read cur) (read cur)
11935                 (setq min (read cur)
11936                       max (read cur)
11937                       opoint (point))
11938                 (skip-chars-forward " \t")
11939                 (insert prefix)
11940                 (goto-char opoint)
11941                 (set (let ((obarray hashtb)) (read cur)) 
11942                      (cons min max))
11943                 (forward-line 1)))
11944           (let (min max opoint)
11945             (while (not (eobp))
11946               (if (= (following-char) ?2)
11947                   (progn
11948                     (read cur) (read cur)
11949                     (setq min (read cur)
11950                           max (read cur))
11951                     (set (let ((obarray hashtb)) (read cur)) 
11952                          (cons min max))))
11953               (forward-line 1))))
11954       (error 
11955        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
11956
11957 (defun gnus-read-newsrc-file (&optional force)
11958   "Read startup file.
11959 If FORCE is non-nil, the .newsrc file is read."
11960   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
11961   ;; Reset variables that might be defined in the .newsrc.eld file.
11962   (let ((variables gnus-variable-list))
11963     (while variables
11964       (set (car variables) nil)
11965       (setq variables (cdr variables))))
11966   (let* ((newsrc-file gnus-current-startup-file)
11967          (quick-file (concat newsrc-file ".el")))
11968     (save-excursion
11969       ;; We always load the .newsrc.eld file. If always contains
11970       ;; much information that can not be gotten from the .newsrc
11971       ;; file (ticked articles, killed groups, foreign methods, etc.)
11972       (gnus-read-newsrc-el-file quick-file)
11973  
11974       (if (or force
11975               (and (file-newer-than-file-p newsrc-file quick-file)
11976                    (file-newer-than-file-p newsrc-file 
11977                                            (concat quick-file "d")))
11978               (not gnus-newsrc-alist))
11979           ;; We read the .newsrc file. Note that if there if a
11980           ;; .newsrc.eld file exists, it has already been read, and
11981           ;; the `gnus-newsrc-hashtb' has been created. While reading
11982           ;; the .newsrc file, Gnus will only use the information it
11983           ;; can find there for changing the data already read -
11984           ;; ie. reading the .newsrc file will not trash the data
11985           ;; already read (except for read articles).
11986           (save-excursion
11987             (gnus-message 5 "Reading %s..." newsrc-file)
11988             (set-buffer (find-file-noselect newsrc-file))
11989             (buffer-disable-undo (current-buffer))
11990             (gnus-newsrc-to-gnus-format)
11991             (kill-buffer (current-buffer))
11992             (gnus-message 5 "Reading %s...done" newsrc-file))))))
11993
11994 (defun gnus-read-newsrc-el-file (file)
11995   (let ((ding-file (concat file "d")))
11996     ;; We always, always read the .eld file.
11997     (gnus-message 5 "Reading %s..." ding-file)
11998     (let (gnus-newsrc-assoc)
11999       (condition-case nil
12000           (load ding-file t t t)
12001         (error nil))
12002       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12003     (let ((inhibit-quit t))
12004       (gnus-uncompress-newsrc-assoc))
12005     (gnus-make-hashtable-from-newsrc-alist)
12006     (if (not (file-newer-than-file-p file ding-file))
12007         ()
12008       ;; Old format quick file
12009       (gnus-message 5 "Reading %s..." file)
12010       ;; The .el file is newer than the .eld file, so we read that one
12011       ;; as well. 
12012       (gnus-read-old-newsrc-el-file file))))
12013
12014 ;; Parse the old-style quick startup file
12015 (defun gnus-read-old-newsrc-el-file (file)
12016   (let (newsrc killed marked group g m len info)
12017     (prog1
12018         (let ((gnus-killed-assoc nil)
12019               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12020           (prog1
12021               (condition-case nil
12022                   (load file t t t)
12023                 (error nil))
12024             (setq newsrc gnus-newsrc-assoc
12025                   killed gnus-killed-assoc
12026                   marked gnus-marked-assoc)))
12027       (setq gnus-newsrc-alist nil)
12028       (while newsrc
12029         (setq group (car newsrc))
12030         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12031           (if info
12032               (progn
12033                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12034                 (setcar (cdr info)
12035                         (if (nth 1 group) gnus-level-default-subscribed 
12036                           gnus-level-default-unsubscribed))
12037                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12038             (setq gnus-newsrc-alist
12039                   (cons 
12040                    (setq info
12041                          (list (car group)
12042                                (if (nth 1 group) gnus-level-default-subscribed
12043                                  gnus-level-default-unsubscribed) 
12044                                (cdr (cdr group))))
12045                    gnus-newsrc-alist)))
12046           (if (setq m (assoc (car group) marked))
12047             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12048         (setq newsrc (cdr newsrc)))
12049       (setq newsrc killed)
12050       (while newsrc
12051         (setcar newsrc (car (car newsrc)))
12052         (setq newsrc (cdr newsrc)))
12053       (setq gnus-killed-list killed))
12054     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12055     (gnus-make-hashtable-from-newsrc-alist)))
12056       
12057 (defun gnus-make-newsrc-file (file)
12058   "Make server dependent file name by catenating FILE and server host name."
12059   (let* ((file (expand-file-name file nil))
12060          (real-file (concat file "-" (nth 1 gnus-select-method))))
12061     (if (file-exists-p real-file)
12062         real-file file)))
12063
12064 (defun gnus-uncompress-newsrc-assoc ()
12065   ;; Uncompress all lists of marked articles in the newsrc assoc.
12066   (let ((newsrc gnus-newsrc-alist)
12067         marked)
12068     (while newsrc
12069       (if (not (setq marked (nth 3 (car newsrc))))
12070           ()
12071         (while marked
12072           (or (eq 'score (car (car marked)))
12073               (eq 'bookmark (car (car marked)))
12074               (eq 'killed (car (car marked)))
12075               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12076           (setq marked (cdr marked))))
12077       (setq newsrc (cdr newsrc)))))
12078
12079 (defun gnus-compress-newsrc-assoc ()
12080   ;; Compress all lists of marked articles in the newsrc assoc.
12081   (let ((newsrc gnus-newsrc-alist)
12082         marked)
12083     (while newsrc
12084       (if (not (setq marked (nth 3 (car newsrc))))
12085           ()
12086         (while marked
12087           (or (eq 'score (car (car marked)))
12088               (eq 'bookmark (car (car marked)))
12089               (eq 'killed (car (car marked)))
12090               (setcdr (car marked) 
12091                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
12092           (setq marked (cdr marked))))
12093       (setq newsrc (cdr newsrc)))))
12094
12095 (defun gnus-newsrc-to-gnus-format ()
12096   (setq gnus-newsrc-options "")
12097   (or gnus-active-hashtb
12098       (setq gnus-active-hashtb (make-vector 4095 0)))
12099   (let ((buf (current-buffer))
12100         (already-read (> (length gnus-newsrc-alist) 1))
12101         group level subscribed info options-symbol newsrc
12102         symbol reads num1)
12103     (goto-char (point-min))
12104     ;; We intern the symbol `options' in the active hashtb so that we
12105     ;; can `eq' against it later.
12106     (setq options-symbol (intern "options" gnus-active-hashtb))
12107   
12108     (while (not (eobp))
12109       ;; We first read the first word on the line by narrowing and
12110       ;; then reading into `gnus-active-hashtb'.  Most groups will
12111       ;; already exist in that hashtb, so this will save some string
12112       ;; space.
12113       (narrow-to-region
12114        (point)
12115        (progn (skip-chars-forward "^ \t!:\n") (point)))
12116       (goto-char (point-min))
12117       (setq symbol 
12118             (and (/= (point-min) (point-max))
12119                  (let ((obarray gnus-active-hashtb)) (read buf))))
12120       (widen)
12121       ;; Now, the symbol we have read is either `options' or a group
12122       ;; name.  If it is an options line, we just add it to a string. 
12123       (cond 
12124        ((eq symbol options-symbol)
12125         (setq gnus-newsrc-options
12126               ;; This concatting is quite inefficient, but since our
12127               ;; thorough studies show that approx 99.37% of all
12128               ;; .newsrc files only contain a single options line, we
12129               ;; don't give a damn, frankly, my dear.
12130               (concat gnus-newsrc-options
12131                       (buffer-substring 
12132                        (gnus-point-at-bol)
12133                        ;; Options may continue on the next line.
12134                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12135                                 (progn (beginning-of-line) (point)))
12136                            (point))))))
12137        (symbol
12138         ;; It was a group name.
12139         (setq subscribed (= (following-char) ?:)
12140               group (symbol-name symbol)
12141               reads nil)
12142         (if (eolp)
12143             ;; If the line ends here, this is clearly a buggy line, so
12144             ;; we put point a the beginning of line and let the cond
12145             ;; below do the error handling.
12146             (beginning-of-line)
12147           ;; We skip to the beginning of the ranges.
12148           (skip-chars-forward "!: \t"))
12149         ;; We are now at the beginning of the list of read articles.
12150         ;; We read them range by range.
12151         (while
12152             (cond 
12153              ((looking-at "[0-9]+")
12154               ;; We narrow and read a number instead of buffer-substring/
12155               ;; string-to-int because it's faster. narrow/widen is
12156               ;; faster than save-restriction/narrow, and save-restriction
12157               ;; produces a garbage object.
12158               (setq num1 (progn
12159                            (narrow-to-region (match-beginning 0) (match-end 0))
12160                            (read buf)))
12161               (widen)
12162               ;; If the next character is a dash, then this is a range.
12163               (if (= (following-char) ?-)
12164                   (progn
12165                     ;; We read the upper bound of the range.
12166                     (forward-char 1)
12167                     (if (not (looking-at "[0-9]+"))
12168                         ;; This is a buggy line, by we pretend that
12169                         ;; it's kinda OK. Perhaps the user should be
12170                         ;; dinged? 
12171                         (setq reads (cons num1 reads))
12172                       (setq reads 
12173                             (cons 
12174                              (cons num1 (progn
12175                                           (narrow-to-region (match-beginning 0) 
12176                                                             (match-end 0))
12177                                           (read buf)))
12178                              reads))
12179                       (widen)))
12180                 ;; It was just a simple number, so we add it to the
12181                 ;; list of ranges.
12182                 (setq reads (cons num1 reads)))
12183               ;; If the next char in ?\n, then we have reached the end
12184               ;; of the line and return nil.
12185               (/= (following-char) ?\n))
12186              ((= (following-char) ?\n)
12187               ;; End of line, so we end.
12188               nil)
12189              (t
12190               ;; Not numbers and not eol, so this might be a buggy
12191               ;; line... 
12192               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12193                   (progn
12194                     ;; The line was buggy.
12195                     (setq group nil)
12196                     (gnus-message 3 "Mangled line: %s" 
12197                                   (buffer-substring (gnus-point-at-bol) 
12198                                                     (gnus-point-at-eol)))
12199                     (ding)
12200                     (sit-for 1)))
12201               nil))
12202           ;; Skip past ", ". Spaces are illegal in these ranges, but
12203           ;; we allow them, because it's a common mistake to put a
12204           ;; space after the comma.
12205           (skip-chars-forward ", "))
12206
12207         ;; We have already read .newsrc.eld, so we gently update the
12208         ;; data in the hash table with the information we have just
12209         ;; read. 
12210         (if (not group)
12211             ()
12212           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12213                 level)
12214             (if info
12215                 ;; There is an entry for this file in the alist.
12216                 (progn
12217                   (setcar (nthcdr 2 info) (nreverse reads))
12218                   ;; We update the level very gently.  In fact, we
12219                   ;; only change it if there's been a status change
12220                   ;; from subscribed to unsubscribed, or vice versa.
12221                   (setq level (nth 1 info))
12222                   (cond ((and (<= level gnus-level-subscribed)
12223                               (not subscribed))
12224                          (setq level (if reads
12225                                          gnus-level-default-unsubscribed 
12226                                        (1+ gnus-level-default-unsubscribed))))
12227                         ((and (> level gnus-level-subscribed) subscribed)
12228                          (setq level gnus-level-default-subscribed)))
12229                   (setcar (cdr info) level))
12230               ;; This is a new group.
12231               (setq info (list group 
12232                                (if subscribed
12233                                    gnus-level-default-subscribed 
12234                                  (if reads
12235                                      (1+ gnus-level-subscribed)
12236                                    gnus-level-default-unsubscribed))
12237                                (nreverse reads))))
12238             (setq newsrc (cons info newsrc))))
12239         (forward-line 1))))
12240     
12241     (setq newsrc (nreverse newsrc))
12242
12243     (if (not already-read)
12244         ()
12245       ;; We now have two newsrc lists - `newsrc', which is what we
12246       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12247       ;; what we've read from .newsrc.eld. We have to merge these
12248       ;; lists. We do this by "attaching" any (foreign) groups in the
12249       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12250       (let ((rc (cdr gnus-newsrc-alist))
12251             (prev gnus-newsrc-alist)
12252             entry mentry)
12253         (while rc
12254           (or (assoc (car (car rc)) newsrc)
12255               (if (setq entry (assoc (car (car prev)) newsrc))
12256                   (setcdr (setq mentry (memq entry newsrc))
12257                           (cons (car rc) (cdr mentry)))
12258                 (setq newsrc (cons (car rc) newsrc))))
12259           (setq prev rc
12260                 rc (cdr rc)))))
12261
12262     (setq gnus-newsrc-alist newsrc)
12263     ;; We make the newsrc hashtb.
12264     (gnus-make-hashtable-from-newsrc-alist)
12265
12266     ;; Finally, if we read some options lines, we parse them.
12267     (or (string= gnus-newsrc-options "")
12268         (gnus-newsrc-parse-options gnus-newsrc-options))
12269     ))
12270
12271 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12272 ;; The return value will be a list on the form
12273 ;; ((regexp1 . ignore)
12274 ;;  (regexp2 . subscribe)...)
12275 ;; When handling new newsgroups, groups that match a `ignore' regexp
12276 ;; will be ignored, and groups that match a `subscribe' regexp will be
12277 ;; subscribed. A line like
12278 ;; options -n !all rec.all
12279 ;; will lead to a list that looks like
12280 ;; (("^rec\\..+" . subscribe) 
12281 ;;  ("^.+" . ignore))
12282 ;; So all "rec.*" groups will be subscribed, while all the other
12283 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12284 ;; different from "options -n rec.all !all". 
12285 (defun gnus-newsrc-parse-options (options)
12286   (let (out eol)
12287     (save-excursion
12288       (gnus-set-work-buffer)
12289       (insert (regexp-quote options))
12290       ;; First we treat all continuation lines.
12291       (goto-char (point-min))
12292       (while (re-search-forward "\n[ \t]+" nil t)
12293         (replace-match " " t t))
12294       ;; Then we transform all "all"s into ".+"s.
12295       (goto-char (point-min))
12296       (while (re-search-forward "\\ball\\b" nil t)
12297         (replace-match ".+" t t))
12298       (goto-char (point-min))
12299       ;; We remove all other options than the "-n" ones.
12300       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12301         (replace-match " ")
12302         (forward-char -1))
12303       (goto-char (point-min))
12304
12305       ;; We are only interested in "options -n" lines - we
12306       ;; ignore the other option lines.
12307       (while (re-search-forward "[ \t]-n" nil t)
12308         (setq eol 
12309               (or (save-excursion
12310                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12311                          (- (point) 2)))
12312                   (gnus-point-at-eol)))
12313         ;; Search for all "words"...
12314         (while (re-search-forward "[^ \t,\n-]+" eol t)
12315           (if (= (char-after (match-beginning 0)) ?!)
12316               ;; If the word begins with a bang (!), this is a "not"
12317               ;; spec. We put this spec (minus the bang) and the
12318               ;; symbol `ignore' into the list.
12319               (setq out (cons (cons (concat 
12320                                      "^" (buffer-substring 
12321                                           (1+ (match-beginning 0))
12322                                           (match-end 0)))
12323                                     'ignore) out))
12324             ;; There was no bang, so this is a "yes" spec.
12325             (setq out (cons (cons (concat 
12326                                    "^" (buffer-substring (match-beginning 0)
12327                                                          (match-end 0)))
12328                                   'subscribe) out)))))
12329     
12330       (setq gnus-newsrc-options-n out))))
12331                
12332
12333 (defun gnus-save-newsrc-file ()
12334   "Save .newsrc file."
12335   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12336   ;; from the variable gnus-newsrc-alist.
12337   (and (or gnus-newsrc-alist gnus-killed-list)
12338        gnus-current-startup-file
12339        (let ((make-backup-files t)
12340              (version-control nil)
12341              (require-final-newline t)) ;Don't ask even if requested.
12342          ;; You can stop or change version control of backup file.
12343          ;; Suggested by jason@violet.berkeley.edu.
12344          (run-hooks 'gnus-save-newsrc-hook)
12345          (save-excursion
12346            (if (or (not gnus-dribble-buffer)
12347                    (not (buffer-name gnus-dribble-buffer))
12348                    (zerop (save-excursion
12349                             (set-buffer gnus-dribble-buffer)
12350                             (buffer-size))))
12351                (gnus-message 4 "(No changes need to be saved)")
12352              (if gnus-save-newsrc-file
12353                  (progn
12354                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12355                    ;; Make backup file of master newsrc.
12356                    (gnus-gnus-to-newsrc-format)
12357                    (gnus-message 5 "Saving %s...done"
12358                                  gnus-current-startup-file)))
12359              ;; Quickly loadable .newsrc.
12360              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12361              (gnus-add-current-to-buffer-list)
12362              (buffer-disable-undo (current-buffer))
12363              (erase-buffer)
12364              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12365              (gnus-gnus-to-quick-newsrc-format)
12366              (write-region 1 (point-max) 
12367                            (concat gnus-current-startup-file ".eld") 
12368                            nil 'nomesg)
12369              (kill-buffer (current-buffer))
12370              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12371              (gnus-dribble-delete-file))))))
12372
12373 (defun gnus-gnus-to-quick-newsrc-format ()
12374   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12375   (insert ";; (ding) Gnus startup file.\n")
12376   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12377   (insert ";; to read .newsrc.\n")
12378   (let ((variables gnus-variable-list)
12379         (inhibit-quit t)
12380         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12381         variable)
12382     ;; insert lisp expressions.
12383     (gnus-compress-newsrc-assoc)
12384     (while variables
12385       (setq variable (car variables))
12386       (and (boundp variable)
12387            (symbol-value variable)
12388            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12389            (insert "(setq " (symbol-name variable) " '"
12390                    (prin1-to-string (symbol-value variable))
12391                    ")\n"))
12392       (setq variables (cdr variables)))
12393     (gnus-uncompress-newsrc-assoc)))
12394
12395
12396 (defun gnus-gnus-to-newsrc-format ()
12397   ;; Generate and save the .newsrc file.
12398   (let ((newsrc (cdr gnus-newsrc-alist))
12399         info ranges range)
12400     (save-excursion
12401       (set-buffer (create-file-buffer gnus-startup-file))
12402       (buffer-disable-undo (current-buffer))
12403       (erase-buffer)
12404       ;; Write options.
12405       (if gnus-newsrc-options (insert gnus-newsrc-options))
12406       ;; Write subscribed and unsubscribed.
12407       (while newsrc
12408         (setq info (car newsrc))
12409         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12410             (progn
12411               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12412                                      "!" ":"))
12413               (if (setq ranges (nth 2 info))
12414                   (progn
12415                     (insert " ")
12416                     (if (not (listp (cdr ranges)))
12417                         (if (= (car ranges) (cdr ranges))
12418                             (insert (int-to-string (car ranges)))
12419                           (insert (int-to-string (car ranges)) "-" 
12420                                   (int-to-string (cdr ranges))))
12421                       (while ranges
12422                         (setq range (car ranges)
12423                               ranges (cdr ranges))
12424                         (if (or (atom range) (= (car range) (cdr range)))
12425                             (insert (int-to-string 
12426                                      (or (and (atom range) range) 
12427                                          (car range))))
12428                           (insert (int-to-string (car range)) "-"
12429                                   (int-to-string (cdr range))))
12430                         (if ranges (insert ","))))))
12431               (insert "\n")))
12432         (setq newsrc (cdr newsrc)))
12433       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12434       (kill-buffer (current-buffer)))))
12435
12436 (defun gnus-read-descriptions-file ()
12437   (gnus-message 5 "Reading descriptions file...")
12438   (cond 
12439    ((not (or (gnus-server-opened gnus-select-method)
12440              (gnus-open-server gnus-select-method)))
12441     (gnus-message 1 "Couldn't open server")
12442     nil)
12443    ((not (gnus-request-list-newsgroups gnus-select-method))
12444     (gnus-message 1 "Couldn't read newsgroups descriptions")
12445     nil)
12446    (t
12447     (let (group)
12448       (setq gnus-description-hashtb 
12449             (gnus-make-hashtable (length gnus-active-hashtb)))
12450       (save-excursion
12451         (save-restriction
12452           (set-buffer nntp-server-buffer)
12453           (goto-char (point-min))
12454           (if (or (search-forward "\n.\n" nil t)
12455                   (goto-char (point-max)))
12456               (progn
12457                 (beginning-of-line)
12458                 (narrow-to-region (point-min) (point))))
12459           (goto-char (point-min))
12460           (while (not (eobp))
12461             ;; If we get an error, we set group to 0, which is not a
12462             ;; symbol... 
12463             (setq group 
12464                   (condition-case ()
12465                       (let ((obarray gnus-description-hashtb))
12466                         ;; Group is set to a symbol interned in this
12467                         ;; hash table.
12468                         (read nntp-server-buffer))
12469                     (error 0)))
12470             (skip-chars-forward " \t")
12471             ;; ... which leads to this line being effectively ignored.
12472             (and (symbolp group)
12473                  (set group (buffer-substring 
12474                              (point) (progn (end-of-line) (point)))))
12475             (forward-line 1))))
12476       (gnus-message 5 "Reading descriptions file...done")
12477       t))))
12478
12479 (defun gnus-group-get-description (group)
12480   ;; Get the description of a group by sending XGTITLE to the server.
12481   (and (gnus-request-group-description group)
12482        (save-excursion
12483          (set-buffer nntp-server-buffer)
12484          (goto-char (point-min))
12485          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12486               (buffer-substring (match-beginning 1) (match-end 1))))))
12487
12488 ;;;
12489 ;;; Server
12490 ;;;
12491
12492 (defvar gnus-server-mode-hook nil
12493   "Hook run in `gnus-server-mode' buffers.")
12494
12495 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12496   "Format of server lines.
12497 It works along the same lines as a normal formatting string,
12498 with some simple extensions.")
12499
12500 (defvar gnus-server-mode-line-format "(ding) List of servers"
12501   "The format specification for the server mode line.")
12502
12503 (defconst gnus-server-line-format-alist
12504   (list (list ?h 'how ?s)
12505         (list ?n 'name ?s)
12506         (list ?w 'where ?s)
12507         ))
12508
12509 (defconst gnus-server-mode-line-format-alist 
12510   (list (list ?S 'news-server ?s)
12511         (list ?M 'news-method ?s)
12512         (list ?u 'user-defined ?s)))
12513
12514 (defvar gnus-server-line-format-spec nil)
12515 (defvar gnus-server-mode-line-format-spec nil)
12516 (defvar gnus-server-killed-servers nil)
12517
12518 (defvar gnus-server-mode-map nil)
12519 (put 'gnus-server-mode 'mode-class 'special)
12520
12521 (if gnus-server-mode-map
12522     nil
12523   (setq gnus-server-mode-map (make-sparse-keymap))
12524   (suppress-keymap gnus-server-mode-map)
12525   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12526   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12527   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12528   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12529   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12530   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12531   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12532   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12533   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12534   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12535
12536 (defun gnus-server-mode ()
12537   "Major mode for listing and editing servers.
12538
12539 All normal editing commands are switched off.
12540 \\<gnus-server-mode-map>
12541
12542 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12543
12544 The following commands are available:
12545
12546 \\{gnus-server-mode-map}"
12547   (interactive)
12548   (if gnus-visual (gnus-server-make-menu-bar))
12549   (kill-all-local-variables)
12550   (setq mode-line-modified "-- ")
12551   (make-local-variable 'mode-line-format)
12552   (setq mode-line-format (copy-sequence mode-line-format))
12553   (and (equal (nth 3 mode-line-format) "   ")
12554        (setcar (nthcdr 3 mode-line-format) ""))
12555   (setq major-mode 'gnus-server-mode)
12556   (setq mode-name "Server")
12557 ;  (gnus-group-set-mode-line)
12558   (setq mode-line-process nil)
12559   (use-local-map gnus-server-mode-map)
12560   (buffer-disable-undo (current-buffer))
12561   (setq truncate-lines t)
12562   (setq buffer-read-only t)
12563   (run-hooks 'gnus-server-mode-hook))
12564
12565 (defun gnus-server-insert-server-line (sformat name method)
12566   (let* ((sformat (or sformat gnus-server-line-format-spec))
12567          (how (car method))
12568          (where (nth 1 method))
12569          b)
12570     (beginning-of-line)
12571     (setq b (point))
12572     ;; Insert the text.
12573     (insert (eval sformat))
12574     (add-text-properties 
12575      b (1+ b) (list 'gnus-server (intern name)))))
12576
12577 (defun gnus-server-setup-buffer ()
12578   (if (get-buffer gnus-server-buffer)
12579       ()
12580     (save-excursion
12581       (set-buffer (get-buffer-create gnus-server-buffer))
12582       (gnus-server-mode)
12583       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12584
12585 (defun gnus-server-prepare ()
12586   (setq gnus-server-mode-line-format-spec 
12587         (gnus-parse-format gnus-server-mode-line-format 
12588                            gnus-server-mode-line-format-alist))
12589   (setq gnus-server-line-format-spec 
12590         (gnus-parse-format gnus-server-line-format 
12591                            gnus-server-line-format-alist))
12592   (let ((alist gnus-server-alist)
12593         (buffer-read-only nil))
12594     (erase-buffer)
12595     (while alist
12596       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12597       (setq alist (cdr alist))))
12598   (goto-char (point-min))
12599   (gnus-server-position-cursor))
12600
12601 (defun gnus-server-server-name ()
12602   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12603     (and server (symbol-name server))))
12604
12605 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12606
12607 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12608
12609 (defun gnus-server-update-server (server)
12610   (save-excursion
12611     (set-buffer gnus-server-buffer)
12612     (let ((buffer-read-only nil)
12613           (info (cdr (assoc server gnus-server-alist))))
12614       (gnus-dribble-enter 
12615        (concat "(gnus-server-set-info \"" server "\" '"
12616                (prin1-to-string info) ")"))
12617       ;; Buffer may be narrowed.
12618       (save-restriction
12619         (widen)
12620         (if (gnus-server-goto-server server)
12621             (delete-region (progn (beginning-of-line) (point))
12622                            (progn (forward-line 1) (point))))
12623         (let ((entry (assoc server gnus-server-alist)))
12624           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12625           (gnus-server-position-cursor))))))
12626
12627 (defun gnus-server-set-info (server info)
12628   ;; Enter a select method into the virtual server alist.
12629   (gnus-dribble-enter 
12630    (concat "(gnus-server-set-info \"" server "\" '"
12631            (prin1-to-string info) ")"))
12632   (let* ((server (nth 1 info))
12633          (entry (assoc server gnus-server-alist)))
12634     (if entry (setcdr entry info)
12635       (setq gnus-server-alist
12636             (nconc gnus-server-alist (list (cons server info)))))))
12637
12638 (defun gnus-server-to-method (server)
12639   ;; Map virtual server names to select methods.
12640   (or (and (equal server "native") gnus-select-method)
12641       (cdr (assoc server gnus-server-alist))))
12642
12643 (defun gnus-server-extend-method (group method)
12644   ;; This function "extends" a virtual server.  If the server is
12645   ;; "hello", and the select method is ("hello" (my-var "something")) 
12646   ;; in the group "alt.alt", this will result in a new virtual server
12647   ;; called "helly+alt.alt".
12648   (let ((entry
12649          (gnus-copy-sequence 
12650           (if (equal (car method) "native") gnus-select-method
12651               (cdr (assoc (car method) gnus-server-alist))))))
12652     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12653     (nconc entry (cdr method))))
12654
12655 (defun gnus-server-get-method (group method)
12656   ;; Input either a server name, and extended server name, or a
12657   ;; select method, and return a select method. 
12658   (cond ((stringp method)
12659          (gnus-server-to-method method))
12660         ((stringp (car method))
12661          (gnus-server-extend-method group method))
12662         (t
12663          (gnus-server-add-address method))))
12664
12665 (defun gnus-server-add-address (method)
12666   (let ((method-name (symbol-name (car method))))
12667     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12668              (not (assq (intern (concat method-name "-address")) method)))
12669         (append method (list (list (intern (concat method-name "-address"))
12670                                    (nth 1 method))))
12671       method)))
12672
12673 (defun gnus-server-equal (s1 s2)
12674   (or (equal s1 s2)
12675       (and (= (length s1) (length s2))
12676            (progn
12677              (while (and s1 (member (car s1) s2))
12678                (setq s1 (cdr s1)))
12679              (null s1)))))
12680
12681 ;;; Interactive server functions.
12682
12683 (defun gnus-server-kill-server (server)
12684   "Kill the server on the current line."
12685   (interactive (list (gnus-server-server-name)))
12686   (or (gnus-server-goto-server server)
12687       (if server (error "No such server: %s" server)
12688         (error "No server on the current line")))
12689   (let ((buffer-read-only nil))
12690     (delete-region (progn (beginning-of-line) (point))
12691                    (progn (forward-line 1) (point))))
12692   (setq gnus-server-killed-servers 
12693         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12694   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12695                                 gnus-server-alist))
12696   (gnus-server-position-cursor))
12697
12698 (defun gnus-server-yank-server ()
12699   "Yank the previously killed server."
12700   (interactive)
12701   (or gnus-server-killed-servers
12702       (error "No killed servers to be yanked"))
12703   (let ((alist gnus-server-alist)
12704         (server (gnus-server-server-name))
12705         (killed (car gnus-server-killed-servers)))
12706     (if (not server) 
12707         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12708       (if (string= server (car (car gnus-server-alist)))
12709           (setq gnus-server-alist (cons killed gnus-server-alist))
12710         (while (and (cdr alist)
12711                     (not (string= server (car (car (cdr alist))))))
12712           (setq alist (cdr alist)))
12713         (setcdr alist (cons killed (cdr alist)))))
12714     (gnus-server-update-server (car killed))
12715     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12716     (gnus-server-position-cursor)))
12717
12718 (defun gnus-server-exit ()
12719   "Return to the group buffer."
12720   (interactive)
12721   (kill-buffer (current-buffer))
12722   (switch-to-buffer gnus-group-buffer))
12723
12724 (defun gnus-server-list-servers ()
12725   "List all available servers."
12726   (interactive)
12727   (let ((cur (gnus-server-server-name)))
12728     (gnus-server-prepare)
12729     (if cur (gnus-server-goto-server cur)
12730       (goto-char (point-max))
12731       (forward-line -1))
12732     (gnus-server-position-cursor)))
12733
12734 (defun gnus-server-copy-server (from to)
12735   (interactive
12736    (list
12737     (or (gnus-server-server-name)
12738         (error "No server on the current line"))
12739     (read-string "Copy to: ")))
12740   (or from (error "No server on current line"))
12741   (or (and to (not (string= to ""))) (error "No name to copy to"))
12742   (and (assoc to gnus-server-alist) (error "%s already exists" to))
12743   (or (assoc from gnus-server-alist) 
12744       (error "%s: no such server" from))
12745   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
12746     (setcar to-entry to)
12747     (setcar (nthcdr 2 to-entry) to)
12748     (setq gnus-server-killed-servers 
12749           (cons to-entry gnus-server-killed-servers))
12750     (gnus-server-yank-server)))
12751
12752 (defun gnus-server-add-server (how where)
12753   (interactive 
12754    (list (intern (completing-read "Server method: "
12755                                   gnus-valid-select-methods nil t))
12756          (read-string "Server name: ")))
12757   (setq gnus-server-killed-servers 
12758         (cons (list where how where) gnus-server-killed-servers))
12759   (gnus-server-yank-server))
12760
12761 (defun gnus-server-goto-server (server)
12762   "Jump to a server line."
12763   (interactive
12764    (list (completing-read "Goto server: " gnus-server-alist nil t)))
12765   (let ((to (text-property-any (point-min) (point-max) 
12766                                'gnus-server (intern server))))
12767     (and to
12768          (progn
12769            (goto-char to) 
12770            (gnus-server-position-cursor)))))
12771
12772 (defun gnus-server-edit-server (server)
12773   "Edit the server on the current line."
12774   (interactive (list (gnus-server-server-name)))
12775   (or server
12776       (error "No server on current line"))
12777   (let ((winconf (current-window-configuration)))
12778     (get-buffer-create gnus-server-edit-buffer)
12779     (gnus-configure-windows 'edit-server)
12780     (gnus-add-current-to-buffer-list)
12781     (emacs-lisp-mode)
12782     (make-local-variable 'gnus-prev-winconf)
12783     (setq gnus-prev-winconf winconf)
12784     (use-local-map (copy-keymap (current-local-map)))
12785     (let ((done-func '(lambda () 
12786                         "Exit editing mode and update the information."
12787                         (interactive)
12788                         (gnus-server-edit-server-done 'group))))
12789       (setcar (cdr (nth 4 done-func)) server)
12790       (local-set-key "\C-c\C-c" done-func))
12791     (erase-buffer)
12792     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
12793     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
12794
12795 (defun gnus-server-edit-server-done (server)
12796   (interactive)
12797   (set-buffer (get-buffer-create gnus-server-edit-buffer))
12798   (goto-char (point-min))
12799   (let ((form (read (current-buffer)))
12800         (winconf gnus-prev-winconf))
12801     (gnus-server-set-info server form)
12802     (kill-buffer (current-buffer))
12803     (and winconf (set-window-configuration winconf))
12804     (set-buffer gnus-server-buffer)
12805     (gnus-server-update-server (gnus-server-server-name))
12806     (gnus-server-position-cursor)))
12807
12808 (defun gnus-server-read-server (server)
12809   "Browse a server."
12810   (interactive (list (gnus-server-server-name)))
12811   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
12812
12813 (defun gnus-mouse-pick-server (e)
12814   (interactive "e")
12815   (mouse-set-point e)
12816   (gnus-server-read-server (gnus-server-server-name)))
12817
12818 ;;;
12819 ;;; entry points into gnus-score.el
12820 ;;;
12821
12822 ;;; Finding score files. 
12823
12824 (defvar gnus-global-score-files nil
12825   "*List of global score files and directories.
12826 Set this variable if you want to use people's score files.  One entry
12827 for each score file or each score file directory.  Gnus will decide
12828 by itself what score files are applicable to which group.
12829
12830 Say you want to use the single score file
12831 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
12832 score files in the \"/ftp.some-where:/pub/score\" directory.
12833
12834  (setq gnus-global-score-files
12835        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
12836          \"/ftp.some-where:/pub/score\"))")
12837
12838 (defun gnus-score-score-files (group)
12839   "Return a list of all possible score files."
12840   ;; Search and set any global score files.
12841   (and gnus-global-score-files 
12842        (or gnus-internal-global-score-files
12843            (gnus-score-search-global-directories gnus-global-score-files)))
12844   ;; Fix the kill-file dir variable.
12845   (setq gnus-kill-files-directory 
12846         (file-name-as-directory
12847          (or gnus-kill-files-directory "~/News/")))
12848   ;; If er can't read it, there's no score files.
12849   (if (not (file-readable-p (expand-file-name gnus-kill-files-directory)))
12850       (setq gnus-score-file-list nil)
12851     (if (gnus-use-long-file-name 'not-score)
12852         ;; We want long file names.
12853         (if (or (not gnus-score-file-list)
12854                 (not (car gnus-score-file-list))
12855                 (gnus-file-newer-than gnus-kill-files-directory
12856                                       (car gnus-score-file-list)))
12857               (setq gnus-score-file-list 
12858                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
12859                           (nreverse 
12860                            (directory-files 
12861                             gnus-kill-files-directory t 
12862                             (gnus-score-file-regexp))))))
12863       ;; We do not use long file names, so we have to do some
12864       ;; directory traversing.  
12865       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
12866             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
12867             dir files suffix)
12868         (while suffixes
12869           (setq dir (expand-file-name
12870                      (concat gnus-kill-files-directory
12871                              (gnus-replace-chars-in-string group ?. ?/))))
12872           (setq suffix (car suffixes)
12873                 suffixes (cdr suffixes))
12874           (if (file-exists-p (concat dir "/" suffix))
12875               (setq files (cons (concat dir "/" suffix) files)))
12876           (while (>= (1+ (length dir)) mdir)
12877             (and (file-exists-p (concat dir "/all/" suffix))
12878                  (setq files (cons (concat dir "/all/" suffix) files)))
12879             (string-match "/[^/]*$" dir)
12880             (setq dir (substring dir 0 (match-beginning 0)))))
12881         (setq gnus-score-file-list 
12882               (cons nil (nreverse files)))))
12883     (cdr gnus-score-file-list)))
12884
12885 (defun gnus-score-file-regexp ()
12886   (concat "\\(" gnus-score-file-suffix 
12887           "\\|" gnus-adaptive-file-suffix "\\)$"))
12888         
12889 (defun gnus-score-find-bnews (group)
12890   "Return a list of score files for GROUP.
12891 The score files are those files in the ~/News directory which matches
12892 GROUP using BNews sys file syntax."
12893   (let* ((sfiles (append (gnus-score-score-files group)
12894                          gnus-internal-global-score-files))
12895          (kill-dir (file-name-as-directory 
12896                     (expand-file-name gnus-kill-files-directory)))
12897          (klen (length kill-dir))
12898          ofiles not-match regexp)
12899     (save-excursion
12900       (set-buffer (get-buffer-create "*gnus score files*"))
12901       (buffer-disable-undo (current-buffer))
12902       ;; Go through all score file names and create regexp with them
12903       ;; as the source.  
12904       (while sfiles
12905         (erase-buffer)
12906         (insert (car sfiles))
12907         (goto-char (point-min))
12908         ;; First remove the suffix itself.
12909         (re-search-forward (concat "." (gnus-score-file-regexp)))
12910         (replace-match "" t t) 
12911         (goto-char (point-min))
12912         (if (looking-at (regexp-quote kill-dir))
12913             ;; If the file name was just "SCORE", `klen' is one character
12914             ;; too much.
12915             (delete-char (min (1- (point-max)) klen))
12916           (goto-char (point-max))
12917           (search-backward "/")
12918           (delete-region (1+ (point)) (point-min)))
12919         ;; If short file names were used, we have to translate slashes.
12920         (goto-char (point-min))
12921         (while (search-forward "/" nil t)
12922           (replace-match "." t t))
12923         ;; Translate "all" to ".*".
12924         (while (search-forward "all" nil t)
12925           (replace-match ".*" t t))
12926         (goto-char (point-min))
12927         ;; Deal with "not."s.
12928         (if (looking-at "not.")
12929             (progn
12930               (setq not-match t)
12931               (setq regexp (buffer-substring 5 (point-max))))
12932           (setq regexp (buffer-substring 1 (point-max)))
12933           (setq not-match nil))
12934         ;; Finally - if this resulting regexp matches the group name,
12935         ;; we add this score file to the list of score files
12936         ;; applicable to this group.
12937         (if (or (and not-match
12938                      (not (string-match regexp group)))
12939                 (and (not not-match)
12940                      (string-match regexp group)))
12941             (setq ofiles (cons (car sfiles) ofiles)))
12942         (setq sfiles (cdr sfiles)))
12943       (kill-buffer (current-buffer))
12944       ;; Slight kludge here - the last score file returned should be
12945       ;; the local score file, whether it exists or not. This is so
12946       ;; that any score commands the user enters will go to the right
12947       ;; file, and not end up in some global score file.
12948       (let ((localscore
12949              (expand-file-name
12950               (if (gnus-use-long-file-name 'not-score)
12951                   (concat gnus-kill-files-directory group "." 
12952                           gnus-score-file-suffix)
12953                 (concat gnus-kill-files-directory
12954                         (gnus-replace-chars-in-string group ?. ?/)
12955                         "/" gnus-score-file-suffix)))))
12956         (and (member localscore ofiles)
12957              (delete localscore ofiles))
12958         (setq ofiles (cons localscore ofiles)))
12959       (nreverse ofiles))))
12960
12961 (defun gnus-score-find-single (group)
12962   "Return list containing the score file for GROUP."
12963   (list (gnus-score-file-name group)))
12964
12965 (defun gnus-score-find-hierarchical (group)
12966   "Return list of score files for GROUP.
12967 This includes the score file for the group and all its parents."
12968   (let ((all (copy-sequence '(nil)))
12969         (start 0))
12970     (while (string-match "\\." group (1+ start))
12971       (setq start (match-beginning 0))
12972       (setq all (cons (substring group 0 start) all)))
12973     (setq all (cons group all))
12974     (mapcar 'gnus-score-file-name (nreverse all))))
12975
12976 (defvar gnus-score-file-alist-cache nil)
12977
12978 (defun gnus-score-find-alist (group)
12979   "Return list of score files for GROUP.
12980 The list is determined from the variable gnus-score-file-alist."
12981   (let ((alist gnus-score-file-multiple-match-alist)
12982         score-files)
12983     ;; if this group has been seen before, return the cached entry
12984     (if (setq score-files (assoc group gnus-score-file-alist-cache))
12985         (cdr score-files)       ; ensures caching of groups with no matches
12986       ;; handle the multiple match alist
12987       (while alist
12988         (and (string-match (car (car alist)) group)
12989              (setq score-files
12990                    (nconc score-files (cdr (car alist)))))
12991         (setq alist (cdr alist)))
12992       (setq alist gnus-score-file-single-match-alist)
12993       ;; handle the single match alist
12994       (catch 'done
12995         (while alist
12996           (and (string-match (car (car alist)) group)
12997                ;; progn used just in case ("regexp") has no files
12998                ;; and score-files is still nil. -sj
12999                ;; this can be construed as a "stop searching here" feature :>
13000                ;; and used to simplify regexps in the single-alist 
13001                (progn
13002                  (setq score-files
13003                        (nconc score-files (cdr (car alist))))
13004                  (throw 'done nil)))
13005           (setq alist (cdr alist))))
13006       ;; cache the score files
13007       (setq gnus-score-file-alist-cache
13008             (cons (cons group score-files) gnus-score-file-alist-cache))
13009       score-files)))
13010
13011
13012 (defun gnus-possibly-score-headers (&optional trace)
13013   (let ((func gnus-score-find-score-files-function)
13014         score-files scores)
13015     (and func (not (listp func))
13016          (setq func (list func)))
13017     ;; Go through all the functions for finding score files (or actual
13018     ;; scores) and add them to a list.
13019     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
13020     (while func
13021       (and (symbolp (car func))
13022            (fboundp (car func))
13023            (setq score-files 
13024                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13025       (setq func (cdr func)))
13026     (if score-files (gnus-score-headers score-files trace))))
13027
13028 (defun gnus-score-file-name (newsgroup &optional suffix)
13029   "Return the name of a score file for NEWSGROUP."
13030   (let ((suffix (or suffix gnus-score-file-suffix)))
13031     (cond  ((or (null newsgroup)
13032                 (string-equal newsgroup ""))
13033             ;; The global score file is placed at top of the directory.
13034             (expand-file-name 
13035              suffix (or gnus-kill-files-directory "~/News")))
13036            ((gnus-use-long-file-name 'not-score)
13037             ;; Append ".SCORE" to newsgroup name.
13038             (expand-file-name (concat newsgroup "." suffix)
13039                               (or gnus-kill-files-directory "~/News")))
13040            (t
13041             ;; Place "SCORE" under the hierarchical directory.
13042             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13043                                       "/" suffix)
13044                               (or gnus-kill-files-directory "~/News"))))))
13045
13046 (defun gnus-score-search-global-directories (files)
13047   "Scan all global score directories for score files."
13048   ;; Set the variable `gnus-internal-global-score-files' to all
13049   ;; available global score files.
13050   (interactive (list gnus-global-score-files))
13051   (let (out)
13052     (while files
13053       (if (string-match "/$" (car files))
13054           (setq out (nconc (directory-files 
13055                             (car files) t
13056                             (concat (gnus-score-file-regexp) "$"))))
13057         (setq out (cons (car files) out)))
13058       (setq files (cdr files)))
13059     (setq gnus-internal-global-score-files out)))
13060
13061 ;; Allow redefinition of Gnus functions.
13062
13063 (gnus-ems-redefine)
13064
13065 (provide 'gnus)
13066
13067 ;;; gnus.el ends here