*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select methdod (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-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%p%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 %p    Process mark (char)
836 %O    Moderated group (string, \"(m)\" or \"\")
837 %n    Select from where (string)
838 %z    A string that look like `<%s:%n>' if a foreign select method is used
839 %u    User defined specifier. The next character in the format string should
840       be a letter.  Gnus will call the function gnus-user-format-function-X,
841       where X is the letter following %u. The function will be passed the
842       current header as argument. The function should return a string, which
843       will be inserted into the buffer just like information from any other
844       group specifier.
845
846 Text between %( and %) will be highlighted with `gnus-mouse-face' when
847 the mouse point move inside the area.  There can only be one such area.
848
849 Note that this format specification is not always respected. For
850 reasons of efficiency, when listing killed groups, this specification
851 is ignored altogether. If the spec is changed considerably, your
852 output may end up looking strange when listing both alive and killed
853 groups.
854
855 If you use %o or %O, reading the active file will be slower and quite
856 a bit of extra memory will be used. %D will also worsen performance.
857 Also note that if you change the format specification to include any
858 of these specs, you must probably re-start Gnus to see them go into
859 effect.") 
860
861 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
862   "*The format specification of the lines in the summary buffer.
863
864 It works along the same lines as a normal formatting string,
865 with some simple extensions.
866
867 %N   Article number, left padded with spaces (string)
868 %S   Subject (string)
869 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
870 %n   Name of the poster (string)
871 %A   Address of the poster (string)
872 %F   Contents of the From: header (string)
873 %x   Contents of the Xref: header (string)
874 %D   Date of the article (string)
875 %d   Date of the article (string) in DD-MMM format
876 %M   Message-id of the article (string)
877 %r   References of the article (string)
878 %c   Number of characters in the article (integer)
879 %L   Number of lines in the article (integer)
880 %I   Indentation based on thread level (a string of spaces)
881 %T   A string with two possible values: 80 spaces if the article
882      is on thread level two or larger and 0 spaces on level one
883 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
884 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
885 %[   Opening bracket (character, \"[\" or \"<\")
886 %]   Closing bracket (character, \"]\" or \">\")
887 %>   Spaces of length thread-level (string)
888 %<   Spaces of length (- 20 thread-level) (string)
889 %i   Article score (number)
890 %z   Article zcore (character)
891 %t   Number of articles under the current thread (number).
892 %e   Whether the thread is empty or not (character).
893 %u   User defined specifier. The next character in the format string should
894      be a letter.  Gnus will call the function gnus-user-format-function-X,
895      where X is the letter following %u. The function will be passed the
896      current header as argument. The function should return a string, which
897      will be inserted into the summary just like information from any other
898      summary specifier.
899
900 Text between %( and %) will be highlighted with `gnus-mouse-face'
901 when the mouse point is placed inside the area.  There can only be one
902 such area.
903
904 The %U (status), %R (replied) and %z (zcore) specs have to be handled
905 with care. For reasons of efficiency, Gnus will compute what column
906 these characters will end up in, and \"hard-code\" that. This means that
907 it is illegal to have these specs after a variable-length spec. Well,
908 you might not be arrested, but your summary buffer will look strange,
909 which is bad enough.
910
911 The smart choice is to have these specs as for to the left as
912 possible. 
913
914 This restriction may disappear in later versions of Gnus.")
915
916 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
917   "*The format specification for the dummy roots in the summary buffer.
918 It works along the same lines as a normal formatting string,
919 with some simple extensions.
920
921 %S  The subject")
922
923 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
924   "*The format specification for the summary mode line.")
925
926 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
927   "*The format specification for the article mode line.")
928
929 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
930   "*The format specification for the group mode line.")
931
932 (defvar gnus-valid-select-methods
933   '(("nntp" post address prompt-address)
934     ("nnspool" post)
935     ("nnvirtual" none virtual prompt-address) 
936     ("nnmbox" mail respool) 
937     ("nnml" mail respool)
938     ("nnmh" mail respool) 
939     ("nndir" none prompt-address address)
940     ("nneething" none prompt-address)
941     ("nndigest" none) 
942     ("nndoc" none prompt-address) 
943     ("nnbabyl" mail respool) 
944     ("nnkiboze" none virtual) 
945     ("nnsoup" post)
946     ("nnfolder" mail respool))
947   "An alist of valid select methods.
948 The first element of each list lists should be a string with the name
949 of the select method. The other elements may be be the category of
950 this method (ie. `post', `mail', `none' or whatever) or other
951 properties that this method has (like being respoolable).
952 If you implement a new select method, all you should have to change is
953 this variable. I think.")
954
955 (defvar gnus-updated-mode-lines '(group article summary)
956   "*List of buffers that should update their mode lines.
957 The list may contain the symbols `group', `article' and `summary'. If
958 the corresponding symbol is present, Gnus will keep that mode line
959 updated with information that may be pertinent. 
960 If this variable is nil, screen refresh may be quicker.")
961
962 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
963 (defvar gnus-mode-non-string-length 21
964   "*Max length of mode-line non-string contents.
965 If this is nil, Gnus will take space as is needed, leaving the rest
966 of the modeline intact.")
967
968 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
969 (defvar gnus-mouse-face 'highlight
970   "*Face used for mouse highlighting in Gnus.
971 No mouse highlights will be done if `gnus-visual' is nil.")
972
973 (defvar gnus-summary-mark-below nil
974   "*Mark all articles with a score below this variable as read.
975 This variable is local to each summary buffer and usually set by the
976 score file.")  
977
978 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
979   "*List of functions used for sorting threads in the summary buffer.
980 By default, threads are sorted by article number.
981
982 Each function takes two threads and return non-nil if the first thread
983 should be sorted before the other.  If you use more than one function,
984 the primary sort function should be the last.
985
986 Ready-mady functions include `gnus-thread-sort-by-number',
987 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
988 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
989 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
990
991 (defvar gnus-thread-score-function '+
992   "*Function used for calculating the total score of a thread.
993
994 The function is called with the scores of the article and each
995 subthread and should then return the score of the thread.
996
997 Some functions you can use are `+', `max', or `min'.")
998
999 (defvar gnus-options-subscribe nil
1000   "*All new groups matching this regexp will be subscribed unconditionally.
1001 Note that this variable deals only with new newsgroups.  This variable
1002 does not affect old newsgroups.")
1003
1004 (defvar gnus-options-not-subscribe nil
1005   "*All new groups matching this regexp will be ignored.
1006 Note that this variable deals only with new newsgroups.  This variable
1007 does not affect old (already subscribed) newsgroups.")
1008
1009 (defvar gnus-auto-expirable-newsgroups nil
1010   "*Groups in which to automatically mark read articles as expirable.
1011 If non-nil, this should be a regexp that should match all groups in
1012 which to perform auto-expiry.  This only makes sense for mail groups.")
1013
1014 (defvar gnus-hidden-properties '(invisible t intangible t)
1015   "Property list to use for hiding text.")
1016
1017 ;; Hooks.
1018
1019 (defvar gnus-group-mode-hook nil
1020   "*A hook for Gnus group mode.")
1021
1022 (defvar gnus-summary-mode-hook nil
1023   "*A hook for Gnus summary mode.")
1024
1025 (defvar gnus-article-mode-hook nil
1026   "*A hook for Gnus article mode.")
1027
1028 (defvar gnus-open-server-hook nil
1029   "*A hook called just before opening connection to the news server.")
1030
1031 (defvar gnus-startup-hook nil
1032   "*A hook called at startup.
1033 This hook is called after Gnus is connected to the NNTP server.")
1034
1035 (defvar gnus-get-new-news-hook nil
1036   "*A hook run just before Gnus checks for new news.")
1037
1038 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1039   "*A function that is called to generate the group buffer.
1040 The function is called with three arguments: The first is a number;
1041 all group with a level less or equal to that number should be listed,
1042 if the second is non-nil, empty groups should also be displayed. If
1043 the third is non-nil, it is a number. No groups with a level lower
1044 than this number should be displayed.
1045
1046 The only current function implemented is `gnus-group-prepare-flat'.")
1047
1048 (defvar gnus-group-prepare-hook nil
1049   "*A hook called after the group buffer has been generated.
1050 If you want to modify the group buffer, you can use this hook.")
1051
1052 (defvar gnus-summary-prepare-hook nil
1053   "*A hook called after the summary buffer has been generated.
1054 If you want to modify the summary buffer, you can use this hook.")
1055
1056 (defvar gnus-article-prepare-hook nil
1057   "*A hook called after an article has been prepared in the article buffer.
1058 If you want to run a special decoding program like nkf, use this hook.")
1059
1060 (defvar gnus-article-display-hook nil
1061   "*A hook called after the article is displayed in the article buffer.
1062 The hook is designed to change the contents of the article
1063 buffer. Typical functions that this hook may contain are
1064 `gnus-article-hide-headers' (hide selected headers),
1065 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1066 `gnus-article-hide-signature' (hide signature) and
1067 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1068 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1069 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1070
1071 (defvar gnus-article-x-face-command
1072   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1073   "String or function to be executed to display an X-Face header.
1074 If it is a string, the command will be executed in a sub-shell
1075 asynchronously. The compressed face will be piped to this command.") 
1076
1077 (defvar gnus-article-x-face-too-ugly nil
1078   "Regexp matching posters whose face shouldn't be shown automatically.")
1079
1080 (defvar gnus-select-group-hook nil
1081   "*A hook called when a newsgroup is selected.
1082
1083 If you'd like to simplify subjects like the
1084 `gnus-summary-next-same-subject' command does, you can use the
1085 following hook:
1086
1087  (setq gnus-select-group-hook
1088       (list
1089         (lambda ()
1090           (mapcar (lambda (header)
1091                      (header-set-subject
1092                       header
1093                       (gnus-simplify-subject
1094                        (header-subject header) 're-only)))
1095                   gnus-newsgroup-headers))))")
1096
1097 (defvar gnus-select-article-hook
1098   '(gnus-summary-show-thread)
1099   "*A hook called when an article is selected.
1100 The default hook shows conversation thread subtrees of the selected
1101 article automatically using `gnus-summary-show-thread'.")
1102
1103 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1104   "*A hook called to apply kill files to a group.
1105 This hook is intended to apply a kill file to the selected newsgroup.
1106 The function `gnus-apply-kill-file' is called by default.
1107
1108 Since a general kill file is too heavy to use only for a few
1109 newsgroups, I recommend you to use a lighter hook function. For
1110 example, if you'd like to apply a kill file to articles which contains
1111 a string `rmgroup' in subject in newsgroup `control', you can use the
1112 following hook:
1113
1114 \(setq gnus-apply-kill-hook
1115       (list
1116         (lambda ()
1117           (cond ((string-match \"control\" gnus-newsgroup-name)
1118                  (gnus-kill \"Subject\" \"rmgroup\")
1119                  (gnus-expunge \"X\"))))))")
1120
1121 (defvar gnus-visual-mark-article-hook 
1122   (list 'gnus-highlight-selected-summary)
1123   "*Hook run after selecting an article in the summary buffer.
1124 It is meant to be used for highlighting the article in some way.  It
1125 is not run if `gnus-visual' is nil.")
1126
1127 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1128   "*A hook called after preparing body, but before preparing header headers.
1129 The default hook (`gnus-inews-insert-signature') inserts a signature
1130 file specified by the variable `gnus-signature-file'.")
1131
1132 (defvar gnus-exit-group-hook nil
1133   "*A hook called when exiting (not quitting) summary mode.")
1134
1135 (defvar gnus-suspend-gnus-hook nil
1136   "*A hook called when suspending (not exiting) Gnus.")
1137
1138 (defvar gnus-exit-gnus-hook nil
1139   "*A hook called when exiting Gnus.")
1140
1141 (defvar gnus-save-newsrc-hook nil
1142   "*A hook called when saving the newsrc file.")
1143
1144 (defvar gnus-summary-update-hook 
1145   (list 'gnus-summary-highlight-line)
1146   "*A hook called when a summary line is changed.
1147 The hook will not be called if `gnus-visual' is nil.
1148
1149 The default function `gnus-summary-highlight-line' will
1150 highlight the line according to the `gnus-summary-highlight'
1151 variable.")
1152
1153 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1154   "*A hook called when an article is selected for the first time.
1155 The hook is intended to mark an article as read (or unread)
1156 automatically when it is selected.")
1157
1158 \f
1159 ;; Internal variables
1160
1161 ;; Avoid highlighting in kill files.
1162 (defvar gnus-summary-inhibit-highlight nil)
1163 (defvar gnus-newsgroup-selected-overlay nil)
1164
1165 (defvar gnus-article-mode-map nil)
1166 (defvar caesar-translate-table nil)
1167 (defvar gnus-dribble-buffer nil)
1168 (defvar gnus-headers-retrieved-by nil)
1169 (defvar gnus-article-reply nil)
1170 (defvar gnus-override-method nil)
1171 (defvar gnus-article-check-size nil)
1172
1173 (defvar gnus-current-score-file nil)
1174 (defvar gnus-internal-global-score-files nil)
1175 (defvar gnus-score-file-list nil)
1176
1177
1178 (defvar gnus-current-move-group nil)
1179
1180 (defvar gnus-newsgroup-dependencies nil)
1181 (defvar gnus-newsgroup-threads nil)
1182 (defvar gnus-newsgroup-async nil)
1183 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1184
1185 (defvar gnus-newsgroup-adaptive nil)
1186
1187 (defvar gnus-summary-display-table nil)
1188
1189 (defconst gnus-group-line-format-alist
1190   (list (list ?M 'marked ?c)
1191         (list ?S 'subscribed ?c)
1192         (list ?L 'level ?d)
1193         (list ?N 'number ?s)
1194         (list ?I 'number-of-dormant ?d)
1195         (list ?T 'number-of-ticked ?d)
1196         (list ?R 'number-of-read ?s)
1197         (list ?t 'number-total ?d)
1198         (list ?y 'number-of-unread-unticked ?s)
1199         (list ?i 'number-of-ticked-and-dormant ?d)
1200         (list ?g 'group ?s)
1201         (list ?G 'qualified-group ?s)
1202         (list ?D 'newsgroup-description ?s)
1203         (list ?o 'moderated ?c)
1204         (list ?O 'moderated-string ?s)
1205         (list ?p 'process-marked ?c)
1206         (list ?s 'news-server ?s)
1207         (list ?n 'news-method ?s)
1208         (list ?z 'news-method-string ?s)
1209         (list ?u 'user-defined ?s)))
1210
1211 (defconst gnus-summary-line-format-alist 
1212   (list (list ?N 'number ?d)
1213         (list ?S 'subject ?s)
1214         (list ?s 'subject-or-nil ?s)
1215         (list ?n 'name ?s)
1216         (list ?A 'address ?s)
1217         (list ?F 'from ?s)
1218         (list ?x (macroexpand '(header-xref header)) ?s)
1219         (list ?D (macroexpand '(header-date header)) ?s)
1220         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1221         (list ?M (macroexpand '(header-id header)) ?s)
1222         (list ?r (macroexpand '(header-references header)) ?s)
1223         (list ?c '(or (header-chars header) 0) ?d)
1224         (list ?L 'lines ?d)
1225         (list ?I 'indentation ?s)
1226         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1227         (list ?R 'replied ?c)
1228         (list ?\[ 'opening-bracket ?c)
1229         (list ?\] 'closing-bracket ?c)
1230         (list ?\> '(make-string level ? ) ?s)
1231         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1232         (list ?i 'score ?d)
1233         (list ?z 'score-char ?c)
1234         (list ?U 'unread ?c)
1235         (list ?t '(gnus-summary-number-of-articles-in-thread 
1236                    (or (prog1 gnus-tmp-adopt-thread 
1237                          (setq gnus-tmp-adopt-thread nil))
1238                        (if (boundp 'thread) (symbol-value 'thread)
1239                          thread nil)))
1240                    ?d)
1241         (list ?e '(gnus-summary-number-of-articles-in-thread 
1242                    (or gnus-tmp-adopt-thread 
1243                        (if (boundp 'thread) (symbol-value 'thread)
1244                          thread nil)) t)
1245                    ?d)
1246         (list ?u 'user-defined ?s))
1247   "An alist of format specifications that can appear in summary lines,
1248 and what variables they correspond with, along with the type of the
1249 variable (string, integer, character, etc).")
1250
1251 (defconst gnus-summary-dummy-line-format-alist
1252   (list (list ?S 'subject ?s)
1253         (list ?N 'number ?d)
1254         (list ?u 'user-defined ?s)))
1255
1256 (defconst gnus-summary-mode-line-format-alist 
1257   (list (list ?G 'group-name ?s)
1258         (list ?g '(gnus-short-group-name group-name) ?s)
1259         (list ?A 'article-number ?d)
1260         (list ?Z 'unread-and-unselected ?s)
1261         (list ?V 'gnus-version ?s)
1262         (list ?U 'unread ?d)
1263         (list ?S 'subject ?s)
1264         (list ?e 'unselected ?d)
1265         (list ?u 'user-defined ?s)
1266         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1267
1268 (defconst gnus-group-mode-line-format-alist 
1269   (list (list ?S 'news-server ?s)
1270         (list ?M 'news-method ?s)
1271         (list ?u 'user-defined ?s)))
1272
1273 (defvar gnus-have-read-active-file nil)
1274
1275 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls & Boys)"
1276   "The mail address of the Gnus maintainer.")
1277
1278 (defconst gnus-version "(ding) Gnus v0.80"
1279   "Version number for this version of Gnus.")
1280
1281 (defvar gnus-info-nodes
1282   '((gnus-group-mode            "(gnus)The Group Buffer")
1283     (gnus-summary-mode          "(gnus)The Summary Buffer")
1284     (gnus-article-mode          "(gnus)The Article Buffer"))
1285   "Assoc list of major modes and related Info nodes.")
1286
1287 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1288   "The location of the (ding) Gnus documentation group.")
1289
1290 (defvar gnus-group-buffer "*Group*")
1291 (defvar gnus-summary-buffer "*Summary*")
1292 (defvar gnus-article-buffer "*Article*")
1293 (defvar gnus-server-buffer "*Server*")
1294
1295 (defvar gnus-work-buffer " *gnus work*")
1296
1297 (defvar gnus-buffer-list nil
1298   "Gnus buffers that should be killed on exit.")
1299
1300 (defvar gnus-server-alist nil
1301   "List of available servers.")
1302
1303 (defvar gnus-variable-list
1304   '(gnus-newsrc-options gnus-newsrc-options-n
1305     gnus-newsrc-last-checked-date 
1306     gnus-newsrc-alist gnus-server-alist
1307     gnus-killed-list gnus-zombie-list)
1308   "Gnus variables saved in the quick startup file.")
1309
1310 (defvar gnus-overload-functions
1311   '((news-inews gnus-inews-news "rnewspost")
1312     (caesar-region gnus-caesar-region "rnews"))
1313   "Functions overloaded by gnus.
1314 It is a list of `(original overload &optional file)'.")
1315
1316 (defvar gnus-newsrc-options nil
1317   "Options line in the .newsrc file.")
1318
1319 (defvar gnus-newsrc-options-n nil
1320   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1321
1322 (defvar gnus-newsrc-last-checked-date nil
1323   "Date Gnus last asked server for new newsgroups.")
1324
1325 (defvar gnus-newsrc-alist nil
1326   "Assoc list of read articles.
1327 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1328
1329 (defvar gnus-newsrc-hashtb nil
1330   "Hashtable of gnus-newsrc-alist.")
1331
1332 (defvar gnus-killed-list nil
1333   "List of killed newsgroups.")
1334
1335 (defvar gnus-killed-hashtb nil
1336   "Hash table equivalent of gnus-killed-list.")
1337
1338 (defvar gnus-zombie-list nil
1339   "List of almost dead newsgroups.")
1340
1341 (defvar gnus-description-hashtb nil
1342   "Descriptions of newsgroups.")
1343
1344 (defvar gnus-list-of-killed-groups nil
1345   "List of newsgroups that have recently been killed by the user.")
1346
1347 (defvar gnus-active-hashtb nil
1348   "Hashtable of active articles.")
1349
1350 (defvar gnus-moderated-list nil
1351   "List of moderated newsgroups.")
1352
1353 (defvar gnus-group-marked nil)
1354
1355 (defvar gnus-current-startup-file nil
1356   "Startup file for the current host.")
1357
1358 (defvar gnus-last-search-regexp nil
1359   "Default regexp for article search command.")
1360
1361 (defvar gnus-last-shell-command nil
1362   "Default shell command on article.")
1363
1364 (defvar gnus-current-select-method nil
1365   "The current method for selecting a newsgroup.")
1366
1367 (defvar gnus-have-all-newsgroups nil)
1368
1369 (defvar gnus-article-internal-prepare-hook nil)
1370
1371 (defvar gnus-newsgroup-name nil)
1372 (defvar gnus-newsgroup-begin nil)
1373 (defvar gnus-newsgroup-end nil)
1374 (defvar gnus-newsgroup-last-rmail nil)
1375 (defvar gnus-newsgroup-last-mail nil)
1376 (defvar gnus-newsgroup-last-folder nil)
1377 (defvar gnus-newsgroup-last-file nil)
1378 (defvar gnus-newsgroup-auto-expire nil)
1379 (defvar gnus-newsgroup-active nil)
1380
1381 (defvar gnus-newsgroup-unreads nil
1382   "List of unread articles in the current newsgroup.")
1383
1384 (defvar gnus-newsgroup-unselected nil
1385   "List of unselected unread articles in the current newsgroup.")
1386
1387 (defvar gnus-newsgroup-marked nil
1388   "List of ticked articles in the current newsgroup (a subset of unread art).")
1389
1390 (defvar gnus-newsgroup-killed nil
1391   "List of ranges of articles that have been through the scoring process.")
1392
1393 (defvar gnus-newsgroup-kill-headers nil)
1394
1395 (defvar gnus-newsgroup-replied nil
1396   "List of articles that have been replied to in the current newsgroup.")
1397
1398 (defvar gnus-newsgroup-expirable nil
1399   "List of articles in the current newsgroup that can be expired.")
1400
1401 (defvar gnus-newsgroup-processable nil
1402   "List of articles in the current newsgroup that can be processed.")
1403
1404 (defvar gnus-newsgroup-bookmarks nil
1405   "List of articles in the current newsgroup that have bookmarks.")
1406
1407 (defvar gnus-newsgroup-dormant nil
1408   "List of dormant articles in the current newsgroup.")
1409
1410 (defvar gnus-newsgroup-scored nil
1411   "List of scored articles in the current newsgroup.")
1412
1413 (defvar gnus-newsgroup-headers nil
1414   "List of article headers in the current newsgroup.")
1415 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1416
1417 (defvar gnus-newsgroup-ancient nil
1418   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1419
1420 (defvar gnus-current-article nil)
1421 (defvar gnus-article-current nil)
1422 (defvar gnus-current-headers nil)
1423 (defvar gnus-have-all-headers nil)
1424 (defvar gnus-last-article nil)
1425 (defvar gnus-newsgroup-history nil)
1426 (defvar gnus-current-kill-article nil)
1427
1428 ;; Save window configuration.
1429 (defvar gnus-prev-winconf nil)
1430
1431 ;; Format specs
1432 (defvar gnus-summary-line-format-spec nil)
1433 (defvar gnus-summary-dummy-line-format-spec nil)
1434 (defvar gnus-group-line-format-spec nil)
1435 (defvar gnus-summary-mode-line-format-spec nil)
1436 (defvar gnus-article-mode-line-format-spec nil)
1437 (defvar gnus-group-mode-line-format-spec nil)
1438 (defvar gnus-summary-mark-positions nil)
1439
1440 (defvar gnus-summary-expunge-below nil)
1441 (defvar gnus-reffed-article-number nil)
1442
1443 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1444 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1445
1446 (defconst gnus-summary-local-variables 
1447   '(gnus-newsgroup-name 
1448     gnus-newsgroup-begin gnus-newsgroup-end 
1449     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1450     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1451     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1452     gnus-newsgroup-unselected gnus-newsgroup-marked
1453     gnus-newsgroup-replied gnus-newsgroup-expirable
1454     gnus-newsgroup-processable gnus-newsgroup-killed
1455     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1456     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1457     gnus-current-article gnus-current-headers gnus-have-all-headers
1458     gnus-last-article gnus-article-internal-prepare-hook
1459     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1460     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1461     gnus-newsgroup-threads gnus-newsgroup-async
1462     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1463     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1464     gnus-newsgroup-history gnus-newsgroup-ancient
1465     gnus-newsgroup-adaptive)
1466   "Variables that are buffer-local to the summary buffers.")
1467
1468 (defconst gnus-bug-message
1469   "Sending a bug report to the Gnus Towers.
1470 ========================================
1471
1472 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1473 be sent to the Gnus Bug Exterminators. 
1474
1475 At the bottom of the buffer you'll see lots of variable settings.
1476 Please do not delete those.  They will tell the Bug People what your
1477 environment is, so that it will be easier to locate the bugs.
1478
1479 If you have found a bug that makes Emacs go \"beep\", set
1480 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1481 and include the backtrace in your bug report.
1482
1483 Please describe the bug in annoying, painstaking detail.
1484
1485 Thank you for your help in stamping out bugs.
1486 ")
1487
1488 ;;; End of variables.
1489
1490 ;; Define some autoload functions Gnus might use.
1491 (eval-and-compile
1492
1493   ;; Various 
1494   (autoload 'metamail-buffer "metamail")
1495   (autoload 'Info-goto-node "info")
1496   (autoload 'hexl-hex-string-to-integer "hexl")
1497   (autoload 'pp "pp")
1498   (autoload 'pp-to-string "pp")
1499   (autoload 'pp-eval-expression "pp")
1500   (autoload 'mail-extract-address-components "mail-extr")
1501
1502   (autoload 'nnmail-split-fancy "nnmail")
1503
1504   ;; timezone
1505   (autoload 'timezone-make-date-arpa-standard "timezone")
1506   (autoload 'timezone-fix-time "timezone")
1507   (autoload 'timezone-make-sortable-date "timezone")
1508   (autoload 'timezone-make-time-string "timezone")
1509
1510   ;; rmail & friends
1511   (autoload 'mail-position-on-field "sendmail")
1512   (autoload 'mail-setup "sendmail")
1513   (autoload 'rmail-output "rmailout")
1514   (autoload 'news-mail-other-window "rnewspost")
1515   (autoload 'news-reply-yank-original "rnewspost")
1516   (autoload 'news-caesar-buffer-body "rnewspost")
1517   (autoload 'rmail-insert-rmail-file-header "rmail")
1518   (autoload 'rmail-count-new-messages "rmail")
1519   (autoload 'rmail-show-message "rmail")
1520
1521   ;; gnus-soup
1522   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1523   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1524   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1525
1526   ;; gnus-mh
1527   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1528   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1529   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1530   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1531   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1532   (autoload 'gnus-Folder-save-name "gnus-mh")
1533   (autoload 'gnus-folder-save-name "gnus-mh")
1534
1535   ;; gnus-vis misc
1536   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1537   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1538   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1539   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1540   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1541   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1542   (autoload 'gnus-summary-highlight-line "gnus-vis")
1543   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1544
1545   ;; gnus-vis article
1546   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1547   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1548   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1549   (autoload 'gnus-article-hide "gnus-vis" nil t)
1550   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1551   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1552   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1553   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1554   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1555   (autoload 'gnus-article-add-button "gnus-vis")
1556
1557   ;; gnus-cite
1558   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1559   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1560   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1561
1562   ;; gnus-kill
1563   (autoload 'gnus-kill "gnus-kill")
1564   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1565   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1566   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1567   (autoload 'gnus-execute "gnus-kill")
1568   (autoload 'gnus-expunge "gnus-kill")
1569
1570   ;; gnus-cache
1571   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1572   (autoload 'gnus-cache-save-buffers "gnus-cache")
1573   (autoload 'gnus-cache-possibly-remove-article "gnus-cache")
1574   (autoload 'gnus-cache-request-article "gnus-cache")
1575   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1576   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1577   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1578
1579   ;; gnus-score
1580   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1581   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1582   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1583   (autoload 'gnus-score-save "gnus-score")
1584   (autoload 'gnus-score-headers "gnus-score")
1585   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1586   (autoload 'gnus-score-adaptive "gnus-score")
1587   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1588   (autoload 'gnus-possibly-score-headers "gnus-score")
1589   (autoload 'gnus-score-find-trace "gnus-score")
1590
1591   ;; gnus-uu
1592   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1593   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1594   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1595   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1596   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1597   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1598   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1599   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1600   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1601   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1602   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1603   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1604   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1605   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1606   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1607   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1608   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1609   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1610   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1611   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1612   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1613   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1614
1615   ;; gnus-msg
1616   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1617   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1618   (autoload 'gnus-group-mail "gnus-msg" nil t)
1619   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1620   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1621   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1622   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1623   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1624   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1625   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1626   (autoload 'gnus-post-news "gnus-msg" nil t)
1627   (autoload 'gnus-inews-news "gnus-msg" nil t)
1628   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1629   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1630   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1631   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1632   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1633   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1634   (autoload 'gnus-mail-yank-original "gnus-msg")
1635   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1636   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1637   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1638   (autoload 'gnus-article-mail-with-original "gnus-msg")
1639   (autoload 'gnus-article-mail "gnus-msg")
1640
1641   ;; gnus-vm
1642   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1643   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1644   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1645   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1646   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1647   (autoload 'gnus-yank-article "gnus-vm" nil t)
1648
1649   )
1650
1651 \f
1652
1653 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1654 ;; If you want the cursor to go somewhere else, set these two
1655 ;; functions in some startup hook to whatever you want.
1656 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1657 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1658
1659 ;;; Various macros and substs.
1660
1661 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1662   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1663   (` (let ((GnusStartBufferWindow (selected-window)))
1664        (unwind-protect
1665            (progn
1666              (pop-to-buffer (, buffer))
1667              (,@ forms))
1668          (select-window GnusStartBufferWindow)))))
1669
1670 (defmacro gnus-gethash (string hashtable)
1671   "Get hash value of STRING in HASHTABLE."
1672   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1673   ;;(` (abbrev-expansion (, string) (, hashtable)))
1674   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1675
1676 (defmacro gnus-sethash (string value hashtable)
1677   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1678   ;; We cannot use define-abbrev since it only accepts string as value.
1679   ;; (set (intern string hashtable) value))
1680   (` (set (intern (, string) (, hashtable)) (, value))))
1681
1682 (defsubst gnus-buffer-substring (beg end)
1683   (buffer-substring (match-beginning beg) (match-end end)))
1684
1685 (defsubst gnus-simplify-subject-re (subject)
1686   "Remove \"Re:\" from subject lines."
1687   (let ((case-fold-search t))
1688     (if (string-match "^re: *" subject)
1689         (substring subject (match-end 0))
1690       subject)))
1691
1692 (defsubst gnus-goto-char (point)
1693   (and point (goto-char point)))
1694
1695 (defmacro gnus-buffer-exists-p (buffer)
1696   (` (and (, buffer)
1697           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1698                    (, buffer)))))
1699
1700 (defmacro gnus-kill-buffer (buffer)
1701   (` (if (gnus-buffer-exists-p (, buffer))
1702          (kill-buffer (, buffer)))))
1703
1704 (defsubst gnus-point-at-bol ()
1705   "Return point at the beginning of line."
1706   (let ((p (point)))
1707     (beginning-of-line)
1708     (prog1
1709         (point)
1710       (goto-char p))))
1711
1712 (defsubst gnus-point-at-eol ()
1713   "Return point at the beginning of line."
1714   (let ((p (point)))
1715     (end-of-line)
1716     (prog1
1717         (point)
1718       (goto-char p))))
1719
1720 ;; Delete the current line (and the next N lines.);
1721 (defmacro gnus-delete-line (&optional n)
1722   (` (delete-region (progn (beginning-of-line) (point))
1723                     (progn (forward-line (, (or n 1))) (point)))))
1724
1725 ;;; Load the compatability functions. 
1726
1727 (require 'gnus-ems)
1728
1729 \f
1730 ;;;
1731 ;;; Gnus Utility Functions
1732 ;;;
1733
1734 (defun gnus-extract-address-components (from)
1735   (let (name address)
1736     ;; First find the address - the thing with the @ in it.  This may
1737     ;; not be accurate in mail addresses, but does the trick most of
1738     ;; the time in news messages.
1739     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1740         (setq address (substring from (match-beginning 0) (match-end 0))))
1741     ;; Then we check whether the "name <address>" format is used.
1742     (and address
1743          (string-match (concat "<" (regexp-quote address) ">") from)
1744          (and (setq name (substring from 0 (1- (match-beginning 0))))
1745               ;; Strip any quotes from the name.
1746               (string-match "\".*\"" name)
1747               (setq name (substring name 1 (1- (match-end 0))))))
1748     ;; If not, then "address (name)" is used.
1749     (or name
1750         (and (string-match "(.+)" from)
1751              (setq name (substring from (1+ (match-beginning 0)) 
1752                                    (1- (match-end 0))))))
1753     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1754     (list (or name from) (or address from))))
1755
1756 (defun gnus-fetch-field (field)
1757   "Return the value of the header FIELD of current article."
1758   (save-excursion
1759     (save-restriction
1760       (let ((case-fold-search t))
1761         (gnus-narrow-to-headers)
1762         (mail-fetch-field field)))))
1763
1764 (defun gnus-goto-colon ()
1765   (beginning-of-line)
1766   (search-forward ":" (gnus-point-at-eol) t))
1767
1768 (defun gnus-narrow-to-headers ()
1769   (widen)
1770   (save-excursion
1771     (narrow-to-region
1772      (goto-char (point-min))
1773      (if (search-forward "\n\n" nil t)
1774          (1- (point))
1775        (point-max)))))
1776
1777 (defun gnus-update-format-specifications ()
1778   (gnus-make-thread-indent-array)
1779   (setq gnus-summary-line-format-spec 
1780         (gnus-parse-format
1781          gnus-summary-line-format gnus-summary-line-format-alist))
1782   (gnus-update-summary-mark-positions)
1783   (setq gnus-summary-dummy-line-format-spec 
1784         (gnus-parse-format gnus-summary-dummy-line-format 
1785                            gnus-summary-dummy-line-format-alist))
1786   (setq gnus-group-line-format-spec
1787         (gnus-parse-format 
1788          gnus-group-line-format 
1789          gnus-group-line-format-alist))
1790   (if (and (string-match "%D" gnus-group-line-format)
1791            (not gnus-description-hashtb)
1792            gnus-read-active-file)
1793       (gnus-read-descriptions-file))
1794   (setq gnus-summary-mode-line-format-spec 
1795         (gnus-parse-format gnus-summary-mode-line-format 
1796                            gnus-summary-mode-line-format-alist))
1797   (setq gnus-article-mode-line-format-spec 
1798         (gnus-parse-format gnus-article-mode-line-format 
1799                            gnus-summary-mode-line-format-alist))
1800   (setq gnus-group-mode-line-format-spec 
1801         (gnus-parse-format gnus-group-mode-line-format 
1802                            gnus-group-mode-line-format-alist)))
1803
1804 (defun gnus-update-summary-mark-positions ()
1805   (save-excursion
1806     (let ((gnus-replied-mark 129)
1807           (gnus-score-below-mark 130)
1808           (gnus-score-over-mark 130)
1809           (thread nil)
1810           pos)
1811       (gnus-set-work-buffer)
1812       (gnus-summary-insert-line 
1813        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1814       (goto-char (point-min))
1815       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1816                                          (- (point) 2)))))
1817       (goto-char (point-min))
1818       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1819                                           (- (point) 2))) pos))
1820       (goto-char (point-min))
1821       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1822                                         (- (point) 2))) pos))
1823       (setq gnus-summary-mark-positions pos))))
1824
1825 (defun gnus-format-max-width (form length)
1826   (let* ((val (eval form))
1827          (valstr (if (numberp val) (int-to-string val) val)))
1828     (if (> (length valstr) length)
1829         (substring valstr 0 length)
1830       valstr)))
1831
1832 (defun gnus-set-mouse-face (string)
1833   ;; Set mouse face property on STRING.
1834   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1835   string)
1836
1837 (defun gnus-parse-format (format spec-alist)
1838   ;; This function parses the FORMAT string with the help of the
1839   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1840   ;; string.  If the FORMAT string contains the specifiers %( and %)
1841   ;; the text between them will have the mouse-face text property.
1842   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1843       (if (and gnus-visual gnus-mouse-face)
1844           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1845                 (button (substring format (match-beginning 2) (match-end 2)))
1846                 (post (substring format (match-beginning 3) (match-end 3))))
1847             (list 'concat
1848                   (gnus-parse-simple-format pre spec-alist)
1849                   (list 'gnus-set-mouse-face
1850                         (gnus-parse-simple-format button spec-alist))
1851                   (gnus-parse-simple-format post spec-alist)))
1852         (gnus-parse-simple-format
1853          (concat (substring format (match-beginning 1) (match-end 1))
1854                  (substring format (match-beginning 2) (match-end 2))
1855                  (substring format (match-beginning 3) (match-end 3)))
1856          spec-alist))
1857     (gnus-parse-simple-format format spec-alist)))
1858
1859 (defun gnus-parse-simple-format (format spec-alist)
1860   ;; This function parses the FORMAT string with the help of the
1861   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1862   ;; string. The list will consist of the symbol `format', a format
1863   ;; specification string, and a list of forms depending on the
1864   ;; SPEC-ALIST.
1865   (let ((max-width 0)
1866         spec flist fstring b newspec max-width elem beg)
1867     (save-excursion
1868       (gnus-set-work-buffer)
1869       (insert format)
1870       (goto-char (point-min))
1871       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1872         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1873                                                      (match-end 2))))
1874         ;; First check if there are any specs that look anything like
1875         ;; "%12,12A", ie. with a "max width specification". These have
1876         ;; to be treated specially.
1877         (if (setq beg (match-beginning 1))
1878             (setq max-width 
1879                   (string-to-int 
1880                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1881           (setq max-width 0)
1882           (setq beg (match-beginning 2)))
1883         ;; Find the specification from `spec-alist'.
1884         (if (not (setq elem (cdr (assq spec spec-alist))))
1885             (setq elem '("*" ?s)))
1886         ;; Treat user defined format specifiers specially
1887         (and (eq (car elem) 'user-defined)
1888              (setq elem
1889                    (list 
1890                     (list (intern (concat "gnus-user-format-function-"
1891                                           (buffer-substring
1892                                            (match-beginning 3)
1893                                            (match-end 3))))
1894                           'header)
1895                     ?s))
1896              (delete-region (match-beginning 3) (match-end 3)))
1897         (if (not (zerop max-width))
1898             (let ((el (car elem)))
1899               (cond ((= (car (cdr elem)) ?c) 
1900                      (setq el (list 'char-to-string el)))
1901                     ((= (car (cdr elem)) ?d)
1902                      (numberp el) (setq el (list 'int-to-string el))))
1903               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1904                                 flist))
1905               (setq newspec ?s))
1906           (setq flist (cons (car elem) flist))
1907           (setq newspec (car (cdr elem))))
1908         ;; Remove the old specification (and possibly a ",12" string).
1909         (delete-region beg (match-end 2))
1910         ;; Insert the new specification.
1911         (goto-char beg)
1912         (insert newspec))
1913       (setq fstring (buffer-substring 1 (point-max))))
1914     (cons 'format (cons fstring (nreverse flist)))))
1915
1916 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1917 (defun gnus-read-init-file ()
1918   (and gnus-init-file
1919        (or (and (file-exists-p gnus-init-file) 
1920                 ;; Don't try to load a directory.
1921                 (not (file-directory-p gnus-init-file)))
1922            (file-exists-p (concat gnus-init-file ".el"))
1923            (file-exists-p (concat gnus-init-file ".elc")))
1924        (load gnus-init-file nil t)))
1925
1926 (defun gnus-set-work-buffer ()
1927   (if (get-buffer gnus-work-buffer)
1928       (progn
1929         (set-buffer gnus-work-buffer)
1930         (erase-buffer))
1931     (set-buffer (get-buffer-create gnus-work-buffer))
1932     (kill-all-local-variables)
1933     (buffer-disable-undo (current-buffer))
1934     (gnus-add-current-to-buffer-list)))
1935
1936 ;; Article file names when saving.
1937
1938 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1939   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1940 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1941 Otherwise, it is like ~/News/news/group/num."
1942   (let ((default
1943           (expand-file-name
1944            (concat (if (gnus-use-long-file-name 'not-save)
1945                        (gnus-capitalize-newsgroup newsgroup)
1946                      (gnus-newsgroup-directory-form newsgroup))
1947                    "/" (int-to-string (header-number headers)))
1948            (or gnus-article-save-directory "~/News"))))
1949     (if (and last-file
1950              (string-equal (file-name-directory default)
1951                            (file-name-directory last-file))
1952              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1953         default
1954       (or last-file default))))
1955
1956 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1957   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1958 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1959 Otherwise, it is like ~/News/news/group/num."
1960   (let ((default
1961           (expand-file-name
1962            (concat (if (gnus-use-long-file-name 'not-save)
1963                        newsgroup
1964                      (gnus-newsgroup-directory-form newsgroup))
1965                    "/" (int-to-string (header-number headers)))
1966            (or gnus-article-save-directory "~/News"))))
1967     (if (and last-file
1968              (string-equal (file-name-directory default)
1969                            (file-name-directory last-file))
1970              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1971         default
1972       (or last-file default))))
1973
1974 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1975   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1976 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1977 Otherwise, it is like ~/News/news/group/news."
1978   (or last-file
1979       (expand-file-name
1980        (if (gnus-use-long-file-name 'not-save)
1981            (gnus-capitalize-newsgroup newsgroup)
1982          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1983        (or gnus-article-save-directory "~/News"))))
1984
1985 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1986   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1987 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1988 Otherwise, it is like ~/News/news/group/news."
1989   (or last-file
1990       (expand-file-name
1991        (if (gnus-use-long-file-name 'not-save)
1992            newsgroup
1993          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1994        (or gnus-article-save-directory "~/News"))))
1995
1996 ;; For subscribing new newsgroup
1997
1998 (defun gnus-subscribe-hierarchical-interactive (groups)
1999   (let ((groups (sort groups 'string<))
2000         prefixes prefix start rest ans group starts)
2001     (while groups
2002       (setq prefixes (list "^"))
2003       (while (and groups prefixes)
2004         (while (not (string-match (car prefixes) (car groups)))
2005           (setq prefixes (cdr prefixes)))
2006         (setq prefix (car prefixes))
2007         (setq start (1- (length prefix)))
2008         (if (and (string-match "[^\\.]\\." (car groups) start)
2009                  (cdr groups)
2010                  (setq prefix 
2011                        (concat "^" (substring (car groups) 0 (match-end 0))))
2012                  (string-match prefix (car (cdr groups))))
2013             (progn
2014               (setq prefixes (cons prefix prefixes))
2015               (message "Descend hierarchy %s? ([y]nsq): " 
2016                        (substring prefix 1 (1- (length prefix))))
2017               (setq ans (read-char))
2018               (cond ((= ans ?n)
2019                      (while (and groups 
2020                                  (string-match prefix 
2021                                                (setq group (car groups))))
2022                        (setq gnus-killed-list 
2023                              (cons group gnus-killed-list))
2024                        (gnus-sethash group group gnus-killed-hashtb)
2025                        (setq groups (cdr groups)))
2026                      (setq starts (cdr starts)))
2027                     ((= ans ?s)
2028                      (while (and groups 
2029                                  (string-match prefix 
2030                                                (setq group (car groups))))
2031                        (gnus-sethash group group gnus-killed-hashtb)
2032                        (gnus-subscribe-alphabetically (car groups))
2033                        (setq groups (cdr groups)))
2034                      (setq starts (cdr starts)))
2035                     ((= ans ?q)
2036                      (while groups
2037                        (setq group (car groups))
2038                        (setq gnus-killed-list (cons group gnus-killed-list))
2039                        (gnus-sethash group group gnus-killed-hashtb)
2040                        (setq groups (cdr groups))))
2041                     (t nil)))
2042           (message "Subscribe %s? ([n]yq)" (car groups))
2043           (setq ans (read-char))
2044           (setq group (car groups))
2045           (cond ((= ans ?y)
2046                  (gnus-subscribe-alphabetically (car groups))
2047                  (gnus-sethash group group gnus-killed-hashtb))
2048                 ((= ans ?q)
2049                  (while groups
2050                    (setq group (car groups))
2051                    (setq gnus-killed-list (cons group gnus-killed-list))
2052                    (gnus-sethash group group gnus-killed-hashtb)
2053                    (setq groups (cdr groups))))
2054                 (t 
2055                  (setq gnus-killed-list (cons group gnus-killed-list))
2056                  (gnus-sethash group group gnus-killed-hashtb)))
2057           (setq groups (cdr groups)))))))
2058
2059 (defun gnus-subscribe-randomly (newsgroup)
2060   "Subscribe new NEWSGROUP by making it the first newsgroup."
2061   (gnus-subscribe-newsgroup newsgroup))
2062
2063 (defun gnus-subscribe-alphabetically (newgroup)
2064   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2065   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2066   (let ((groups (cdr gnus-newsrc-alist))
2067         before)
2068     (while (and (not before) groups)
2069       (if (string< newgroup (car (car groups)))
2070           (setq before (car (car groups)))
2071         (setq groups (cdr groups))))
2072     (gnus-subscribe-newsgroup newgroup before)))
2073
2074 (defun gnus-subscribe-hierarchically (newgroup)
2075   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2076   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2077   (save-excursion
2078     (set-buffer (find-file-noselect gnus-current-startup-file))
2079     (let ((groupkey newgroup)
2080           before)
2081       (while (and (not before) groupkey)
2082         (goto-char (point-min))
2083         (let ((groupkey-re
2084                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2085           (while (and (re-search-forward groupkey-re nil t)
2086                       (progn
2087                         (setq before (buffer-substring
2088                                       (match-beginning 1) (match-end 1)))
2089                         (string< before newgroup)))))
2090         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2091         (setq groupkey
2092               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2093                   (substring groupkey (match-beginning 1) (match-end 1)))))
2094       (gnus-subscribe-newsgroup newgroup before))))
2095
2096 (defun gnus-subscribe-interactively (newsgroup)
2097   "Subscribe new NEWSGROUP interactively.
2098 It is inserted in hierarchical newsgroup order if subscribed. If not,
2099 it is killed."
2100   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2101       (gnus-subscribe-hierarchically newsgroup)
2102     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2103
2104 (defun gnus-subscribe-zombies (newsgroup)
2105   "Make new NEWSGROUP a zombie group."
2106   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2107
2108 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2109   "Subscribe new NEWSGROUP.
2110 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2111 the first newsgroup."
2112   ;; We subscribe the group by changing its level to `subscribed'.
2113   (gnus-group-change-level 
2114    newsgroup gnus-level-default-subscribed
2115    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2116   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2117
2118 ;; For directories
2119
2120 (defun gnus-newsgroup-directory-form (newsgroup)
2121   "Make hierarchical directory name from NEWSGROUP name."
2122   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2123         (len (length newsgroup))
2124         idx)
2125     ;; If this is a foreign group, we don't want to translate the
2126     ;; entire name.  
2127     (if (setq idx (string-match ":" newsgroup))
2128         (aset newsgroup idx ?/)
2129       (setq idx 0))
2130     ;; Replace all occurrences of `.' with `/'.
2131     (while (< idx len)
2132       (if (= (aref newsgroup idx) ?.)
2133           (aset newsgroup idx ?/))
2134       (setq idx (1+ idx)))
2135     newsgroup))
2136
2137 (defun gnus-make-directory (dir)
2138   "Make DIRECTORY recursively."
2139   (let* ((dir (expand-file-name dir default-directory))
2140          dirs)
2141     (if (string-match "/$" dir)
2142         (setq dir (substring dir 0 (match-beginning 0))))
2143     (while (not (file-exists-p dir))
2144       (setq dirs (cons dir dirs))
2145       (string-match "/[^/]+$" dir)
2146       (setq dir (substring dir 0 (match-beginning 0))))
2147     (while dirs
2148       (make-directory (car dirs))
2149       (setq dirs (cdr dirs)))))
2150
2151 (defun gnus-capitalize-newsgroup (newsgroup)
2152   "Capitalize NEWSGROUP name."
2153   (and (not (zerop (length newsgroup)))
2154        (concat (char-to-string (upcase (aref newsgroup 0)))
2155                (substring newsgroup 1))))
2156
2157 ;; Var
2158
2159 (defun gnus-simplify-subject (subject &optional re-only)
2160   "Remove `Re:' and words in parentheses.
2161 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2162   (let ((case-fold-search t))           ;Ignore case.
2163     ;; Remove `Re:' and `Re^N:'.
2164     (if (string-match "^re:[ \t]*" subject)
2165         (setq subject (substring subject (match-end 0))))
2166     ;; Remove words in parentheses from end.
2167     (or re-only
2168         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2169           (setq subject (substring subject 0 (match-beginning 0)))))
2170     ;; Return subject string.
2171     subject))
2172
2173 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2174 ;; all whitespace.
2175 (defun gnus-simplify-subject-fuzzy (subject)
2176   (let ((case-fold-search t))
2177     (save-excursion
2178       (gnus-set-work-buffer)
2179       (insert subject)
2180       (gnus-simplify-buffer-fuzzy)
2181       (buffer-string))))
2182
2183 (defun gnus-simplify-buffer-fuzzy ()
2184   (goto-char (point-min))
2185   (while (re-search-forward "^[ \t]*re:[ \t]*" nil t)
2186     (replace-match "" t t))
2187   (goto-char (point-min))
2188   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2189     (replace-match "" t t))
2190   (goto-char (point-min))
2191   (while (re-search-forward "[ \t]+" nil t)
2192     (replace-match " " t t))
2193   (goto-char (point-min))
2194   (while (re-search-forward "[ \t]+$" nil t)
2195     (replace-match "" t t))
2196   (goto-char (point-min))
2197   (while (re-search-forward "^[ \t]+" nil t)
2198     (replace-match "" t t)))
2199
2200 ;; Add the current buffer to the list of buffers to be killed on exit. 
2201 (defun gnus-add-current-to-buffer-list ()
2202   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
2203
2204 (defun gnus-string> (s1 s2)
2205   (not (or (string< s1 s2)
2206            (string= s1 s2))))
2207
2208 ;; Functions accessing headers.
2209 ;; Functions are more convenient than macros in some cases.
2210
2211 (defun gnus-header-number (header)
2212   (header-number header))
2213
2214 (defun gnus-header-subject (header)
2215   (header-subject header))
2216
2217 (defun gnus-header-from (header)
2218   (header-from header))
2219
2220 (defun gnus-header-xref (header)
2221   (header-xref header))
2222
2223 (defun gnus-header-lines (header)
2224   (header-lines header))
2225
2226 (defun gnus-header-date (header)
2227   (header-date header))
2228
2229 (defun gnus-header-id (header)
2230   (header-id header))
2231
2232 (defun gnus-header-references (header)
2233   (header-references header))
2234
2235 ;;; General various misc type functions.
2236
2237 (defun gnus-clear-system ()
2238   "Clear all variables and buffers."
2239   ;; Clear Gnus variables.
2240   (let ((variables gnus-variable-list))
2241     (while variables
2242       (set (car variables) nil)
2243       (setq variables (cdr variables))))
2244   ;; Clear other internal variables.
2245   (setq gnus-list-of-killed-groups nil
2246         gnus-have-read-active-file nil
2247         gnus-newsrc-alist nil
2248         gnus-newsrc-hashtb nil
2249         gnus-killed-list nil
2250         gnus-zombie-list nil
2251         gnus-killed-hashtb nil
2252         gnus-active-hashtb nil
2253         gnus-moderated-list nil
2254         gnus-description-hashtb nil
2255         gnus-newsgroup-headers nil
2256         gnus-newsgroup-headers-hashtb-by-number nil
2257         gnus-newsgroup-name nil
2258         gnus-server-alist nil
2259         gnus-current-select-method nil)
2260   ;; Reset any score variables.
2261   (and (boundp 'gnus-score-cache)
2262        (set 'gnus-score-cache nil))
2263   (and (boundp 'gnus-internal-global-score-files)
2264        (set 'gnus-internal-global-score-files nil))
2265   ;; Kill the startup file.
2266   (and gnus-current-startup-file
2267        (get-file-buffer gnus-current-startup-file)
2268        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2269   ;; Save any cache buffers.
2270   (and gnus-use-cache (gnus-cache-save-buffers))
2271   ;; Clear the dribble buffer.
2272   (gnus-dribble-clear)
2273   ;; Kill global KILL file buffer.
2274   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2275       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2276   (gnus-kill-buffer nntp-server-buffer)
2277   ;; Kill Gnus buffers.
2278   (while gnus-buffer-list
2279     (gnus-kill-buffer (car gnus-buffer-list))
2280     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2281
2282 (defun gnus-windows-old-to-new (setting)
2283   (if (symbolp setting)
2284       (setq setting 
2285             (cond ((eq setting 'SelectArticle)
2286                    'article)
2287                   ((eq setting 'SelectSubject)
2288                    'summary)
2289                   ((eq setting 'SelectNewsgroup)
2290                    'group)
2291                   (t setting))))
2292   (if (or (listp setting)
2293           (not (and gnus-window-configuration
2294                     (memq setting '(group summary article)))))
2295       setting
2296     (let* ((setting (if (eq setting 'group) 
2297                         (if (assq 'newsgroup gnus-window-configuration)
2298                             'newsgroup
2299                           'newsgroups) setting))
2300            (elem (car (cdr (assq setting gnus-window-configuration))))
2301            (total (apply '+ elem))
2302            (types '(group summary article))
2303            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2304            (i 0)
2305            perc
2306            out)
2307       (while (< i 3)
2308         (or (zerop (nth i elem))
2309             (progn
2310               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2311               (setq out (cons (if (eq pbuf (nth i types))
2312                                   (vector (nth i types) perc 'point)
2313                                 (vector (nth i types) perc))
2314                               out))))
2315         (setq i (1+ i)))
2316       (list (nreverse out)))))
2317            
2318 (defun gnus-configure-windows (setting)
2319   (setq setting (gnus-windows-old-to-new setting))
2320   (let ((r (if (symbolp setting)
2321                   (cdr (assq setting gnus-buffer-configuration))
2322                 setting))
2323         (in-buf (current-buffer))
2324         rule val window w height hor ohor heights sub jump-buffer
2325         rel total to-buf)
2326     (or r (error "No such setting: %s" setting))
2327
2328     ;; Either remove all windows or just remove all Gnus windows.
2329     (if gnus-use-full-window
2330         (delete-other-windows)
2331       (gnus-remove-some-windows)
2332       (switch-to-buffer nntp-server-buffer))
2333
2334     (while r
2335       (setq hor (car r)
2336             ohor nil)
2337
2338       ;; We have to do the (possible) horizontal splitting before the
2339       ;; vertical. 
2340       (if (and (listp (car hor)) 
2341                (eq (car (car hor)) 'horizontal))
2342           (progn
2343             (split-window nil (- (frame-width) 
2344                                  (floor (* (frame-width) (nth 1 (car hor)))))
2345                           t)
2346             (setq hor (cdr hor))))
2347
2348       ;; Go through the rules and eval the elements that are to be
2349       ;; evaled.  
2350       (while hor
2351         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2352             (progn
2353               ;; Expand short buffer name.
2354               (setq w (aref val 0))
2355               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2356                    (progn
2357                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2358                      (aset val 0 w)))
2359               (setq ohor (cons val ohor))))
2360         (setq hor (cdr hor)))
2361       (setq rule (cons (nreverse ohor) rule))
2362       (setq r (cdr r)))
2363     (setq rule (nreverse rule))
2364
2365     ;; We tally the window sizes.
2366     (setq total (window-height))
2367     (while rule
2368       (setq hor (car rule))
2369       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2370           (setq hor (cdr hor)))
2371       (setq sub 0)
2372       (while hor
2373         (setq rel (aref (car hor) 1)
2374               heights (cons
2375                        (cond ((and (floatp rel) (= 1.0 rel))
2376                               'x)
2377                              ((integerp rel)
2378                               rel)
2379                              (t
2380                               (max (floor (* total rel)) 4)))
2381                        heights)
2382               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2383               hor (cdr hor)))
2384       (setq heights (nreverse heights)
2385             hor (car rule))
2386
2387       ;; We then go through these heighs and create windows for them.
2388       (while heights
2389         (setq height (car heights)
2390               heights (cdr heights))
2391         (and (eq height 'x)
2392              (setq height (- total sub)))
2393         (and heights
2394              (split-window nil height))
2395         (setq to-buf (aref (car hor) 0))
2396         (switch-to-buffer 
2397          (cond ((not to-buf)
2398                 in-buf)
2399                ((symbolp to-buf)
2400                 (symbol-value (aref (car hor) 0)))
2401                (t
2402                 (aref (car hor) 0))))
2403         (and (> (length (car hor)) 2)
2404              (eq (aref (car hor) 2) 'point)
2405              (setq jump-buffer (current-buffer)))
2406         (other-window 1)
2407         (setq hor (cdr hor)))
2408       
2409       (setq rule (cdr rule)))
2410
2411     ;; Finally, we pop to the buffer that's supposed to have point. 
2412     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2413
2414     (select-window (get-buffer-window jump-buffer))
2415     jump-buffer))
2416       
2417 (defun gnus-remove-some-windows ()
2418   (let ((buffers gnus-window-to-buffer)
2419         (first t)
2420         buf)
2421     (while buffers
2422       (setq buf (cdr (car buffers)))
2423       (if (symbolp buf)
2424           (setq buf (and (boundp buf) (symbol-value buf))))
2425       (and buf 
2426            (get-buffer-window buf)
2427            (progn
2428              (set-buffer buf)
2429              (if first
2430                  (progn
2431                    (switch-to-buffer nntp-server-buffer)
2432                    (setq first nil))
2433                (delete-window (get-buffer-window buf)))))
2434       (setq buffers (cdr buffers)))
2435     (set-buffer nntp-server-buffer)))
2436
2437 (defun gnus-version ()
2438   "Version numbers of this version of Gnus."
2439   (interactive)
2440   (let ((methods gnus-valid-select-methods)
2441         (mess gnus-version)
2442         meth)
2443     ;; Go through all the legal select methods and add their version
2444     ;; numbers to the total version string. Only the backends that are
2445     ;; currently in use will have their message numbers taken into
2446     ;; consideration. 
2447     (while methods
2448       (setq meth (intern (concat (car (car methods)) "-version")))
2449       (and (boundp meth)
2450            (stringp (symbol-value meth))
2451            (setq mess (concat mess "; " (symbol-value meth))))
2452       (setq methods (cdr methods)))
2453     (gnus-message 2 mess)))
2454
2455 (defun gnus-info-find-node ()
2456   "Find Info documentation of Gnus."
2457   (interactive)
2458   ;; Enlarge info window if needed.
2459   (let ((mode major-mode))
2460     (gnus-configure-windows 'info)
2461     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2462
2463 (defun gnus-bug ()
2464   "Send a bug report to the Gnus maintainers."
2465   (interactive)
2466   (let ((winconf (current-window-configuration)))
2467     (delete-other-windows)
2468     (switch-to-buffer "*Gnus Bug Help*")
2469     (erase-buffer)
2470     (insert gnus-bug-message)
2471     (goto-char (point-min))
2472     (pop-to-buffer "*Gnus Bug*")
2473     (erase-buffer)
2474     (mail-mode)
2475     (mail-setup gnus-maintainer nil nil nil nil nil)
2476     (make-local-variable 'gnus-prev-winconf)
2477     (setq gnus-prev-winconf winconf)
2478     (goto-char (point-min))
2479     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2480     (forward-line 1)
2481     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
2482     (let ((b (point)))
2483       (gnus-debug)
2484       (goto-char (- b 3)))
2485     (message "")))
2486
2487 (defun gnus-debug ()
2488   "Attemps to go through the Gnus source file and report what variables have been changed.
2489 The source file has to be in the Emacs load path."
2490   (interactive)
2491   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el"))
2492         file dirs expr olist)
2493     (save-excursion
2494       (set-buffer (get-buffer-create " *gnus bug info*"))
2495       (buffer-disable-undo (current-buffer))
2496       (message "Please wait while we snoop your variables...")
2497       (sit-for 0)
2498       (while files
2499         (erase-buffer)
2500         (setq dirs load-path)
2501         (while dirs
2502           (if (or (not (car dirs))
2503                   (not (stringp (car dirs)))
2504                   (not (file-exists-p 
2505                         (setq file (concat (file-name-as-directory 
2506                                             (car dirs)) (car files))))))
2507               (setq dirs (cdr dirs))
2508             (setq dirs nil)
2509             (insert-file-contents file)
2510             (goto-char (point-min))
2511             (or (re-search-forward "^;;* Internal variables" nil t)
2512                 (error "Malformed sources in file %s" file))
2513             (narrow-to-region (point-min) (point))
2514             (goto-char (point-min))
2515             (while (setq expr (condition-case () 
2516                                   (read (current-buffer)) (error nil)))
2517               (and (eq (car expr) 'defvar)
2518                    (stringp (nth 3 expr))
2519                    (or (not (boundp (nth 1 expr)))
2520                        (not (equal (eval (nth 2 expr))
2521                                    (symbol-value (nth 1 expr)))))
2522                    (setq olist (cons (nth 1 expr) olist))))))
2523         (setq files (cdr files)))
2524       (kill-buffer (current-buffer)))
2525     (insert "------------------- Environment follows -------------------\n\n")
2526     (while olist
2527       (if (boundp (car olist))
2528           (insert "(setq " (symbol-name (car olist)) " '" 
2529                   (prin1-to-string (symbol-value (car olist))) ")\n")
2530         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2531       (setq olist (cdr olist)))
2532     (insert "\n\n")))
2533
2534 (defun gnus-overload-functions (&optional overloads)
2535   "Overload functions specified by optional argument OVERLOADS.
2536 If nothing is specified, use the variable gnus-overload-functions."
2537   (let ((defs nil)
2538         (overloads (or overloads gnus-overload-functions)))
2539     (while overloads
2540       (setq defs (car overloads))
2541       (setq overloads (cdr overloads))
2542       ;; Load file before overloading function if necessary.  Make
2543       ;; sure we cannot use `require' always.
2544       (and (not (fboundp (car defs)))
2545            (car (cdr (cdr defs)))
2546            (load (car (cdr (cdr defs))) nil 'nomessage))
2547       (fset (car defs) (car (cdr defs))))))
2548
2549 (defun gnus-replace-chars-in-string (string from to)
2550   "Replace characters in STRING from FROM to TO."
2551   (let ((string (substring string 0))   ;Copy string.
2552         (len (length string))
2553         (idx 0))
2554     ;; Replace all occurrences of FROM with TO.
2555     (while (< idx len)
2556       (if (= (aref string idx) from)
2557           (aset string idx to))
2558       (setq idx (1+ idx)))
2559     string))
2560
2561 (defun gnus-days-between (date1 date2)
2562   ;; Return the number of days between date1 and date2.
2563   (- (gnus-day-number date1) (gnus-day-number date2)))
2564
2565 (defun gnus-day-number (date)
2566   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2567                      (timezone-parse-date date))))
2568     (timezone-absolute-from-gregorian 
2569      (nth 1 dat) (nth 2 dat) (car dat))))
2570
2571 ;; Returns a floating point number that says how many seconds have
2572 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2573 (defun gnus-seconds-since-epoch (date)
2574   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2575                         (timezone-parse-date date)))
2576          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2577                         (timezone-parse-time
2578                          (aref (timezone-parse-date date) 3))))
2579          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2580                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2581          (tday (- (timezone-absolute-from-gregorian 
2582                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2583                   (timezone-absolute-from-gregorian 
2584                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2585     (+ (nth 2 ttime)
2586        (* (nth 1 ttime) 60)
2587        (* 1.0 (nth 0 ttime) 60 60)
2588        (* 1.0 tday 60 60 24))))
2589
2590 (defun gnus-file-newer-than (file date)
2591   (let ((fdate (nth 5 (file-attributes file))))
2592     (or (> (car fdate) (car date))
2593         (and (= (car fdate) (car date))
2594              (> (nth 1 fdate) (nth 1 date))))))
2595
2596 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2597 ;; the echo area.
2598 (defun gnus-y-or-n-p (prompt)
2599   (prog1
2600       (y-or-n-p prompt)
2601     (message "")))
2602
2603 (defun gnus-yes-or-no-p (prompt)
2604   (prog1
2605       (yes-or-no-p prompt)
2606     (message "")))
2607
2608 ;; Check whether to use long file names.
2609 (defun gnus-use-long-file-name (symbol)
2610   ;; The variable has to be set...
2611   (and gnus-use-long-file-name
2612        ;; If it isn't a list, then we return t.
2613        (or (not (listp gnus-use-long-file-name))
2614            ;; If it is a list, and the list contains `symbol', we
2615            ;; return nil.  
2616            (not (memq symbol gnus-use-long-file-name)))))
2617
2618 ;; I suspect there's a better way, but I haven't taken the time to do
2619 ;; it yet. -erik selberg@cs.washington.edu
2620 (defun gnus-dd-mmm (messy-date)
2621   "Return a string like DD-MMM from a big messy string"
2622   (let ((datevec (timezone-parse-date messy-date)))
2623     (format "%2s-%s"
2624             (or (aref datevec 2) "??")
2625             (capitalize
2626              (or (car 
2627                   (nth (1- (string-to-number (aref datevec 1)))
2628                        timezone-months-assoc))
2629                  "???")))))
2630
2631 ;; Make a hash table (default and minimum size is 255).
2632 ;; Optional argument HASHSIZE specifies the table size.
2633 (defun gnus-make-hashtable (&optional hashsize)
2634   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2635
2636 ;; Make a number that is suitable for hashing; bigger than MIN and one
2637 ;; less than 2^x.
2638 (defun gnus-create-hash-size (min)
2639   (let ((i 1))
2640     (while (< i min)
2641       (setq i (* 2 i)))
2642     (1- i)))
2643
2644 ;; Show message if message has a lower level than `gnus-verbose'. 
2645 ;; Guide-line for numbers:
2646 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2647 ;; for things that take a long time, 7 - not very important messages
2648 ;; on stuff, 9 - messages inside loops.
2649 (defun gnus-message (level &rest args)
2650   (if (<= level gnus-verbose)
2651       (apply 'message args)
2652     ;; We have to do this format thingie here even if the result isn't
2653     ;; shown - the return value has to be the same as the return value
2654     ;; from `message'.
2655     (apply 'format args)))
2656
2657 ;; Generate a unique new group name.
2658 (defun gnus-generate-new-group-name (leaf)
2659   (let ((name leaf)
2660         (num 0))
2661     (while (gnus-gethash name gnus-newsrc-hashtb)
2662       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2663     name))
2664
2665 ;;; List and range functions
2666
2667 (defun gnus-last-element (list)
2668   "Return last element of LIST."
2669   (while (cdr list)
2670     (setq list (cdr list)))
2671   (car list))
2672
2673 (defun gnus-copy-sequence (list)
2674   "Do a complete, total copy of a list."
2675   (if (and (consp list) (not (consp (cdr list))))
2676       (cons (car list) (cdr list))
2677     (mapcar (lambda (elem) (if (consp elem) 
2678                                (if (consp (cdr elem))
2679                                    (gnus-copy-sequence elem)
2680                                  (cons (car elem) (cdr elem)))
2681                              elem))
2682             list)))
2683
2684 (defun gnus-set-difference (list1 list2)
2685   "Return a list of elements of LIST1 that do not appear in LIST2."
2686   (let ((list1 (copy-sequence list1)))
2687     (while list2
2688       (setq list1 (delq (car list2) list1))
2689       (setq list2 (cdr list2)))
2690     list1))
2691
2692 (defun gnus-sorted-complement (list1 list2)
2693   "Return a list of elements of LIST1 that do not appear in LIST2.
2694 Both lists have to be sorted over <."
2695   (let (out)
2696     (if (or (null list1) (null list2))
2697         (or list1 list2)
2698       (while (and list1 list2)
2699         (cond ((= (car list1) (car list2))
2700                (setq list1 (cdr list1)
2701                      list2 (cdr list2)))
2702               ((< (car list1) (car list2))
2703                (setq out (cons (car list1) out))
2704                (setq list1 (cdr list1)))
2705               (t
2706                (setq out (cons (car list2) out))
2707                (setq list2 (cdr list2)))))
2708       (nconc (nreverse out) (or list1 list2)))))
2709
2710 (defun gnus-intersection (list1 list2)      
2711   (let ((result nil))
2712     (while list2
2713       (if (memq (car list2) list1)
2714           (setq result (cons (car list2) result)))
2715       (setq list2 (cdr list2)))
2716     result))
2717
2718 (defun gnus-sorted-intersection (list1 list2)
2719   ;; LIST1 and LIST2 have to be sorted over <.
2720   (let (out)
2721     (while (and list1 list2)
2722       (cond ((= (car list1) (car list2))
2723              (setq out (cons (car list1) out)
2724                    list1 (cdr list1)
2725                    list2 (cdr list2)))
2726             ((< (car list1) (car list2))
2727              (setq list1 (cdr list1)))
2728             (t
2729              (setq list2 (cdr list2)))))
2730     (nreverse out)))
2731
2732 (defun gnus-set-sorted-intersection (list1 list2)
2733   ;; LIST1 and LIST2 have to be sorted over <.
2734   ;; This function modifies LIST1.
2735   (let* ((top (cons nil list1))
2736          (prev top))
2737   (while (and list1 list2)
2738     (cond ((= (car list1) (car list2))
2739            (setq prev list1
2740                  list1 (cdr list1)
2741                  list2 (cdr list2)))
2742           ((< (car list1) (car list2))
2743            (setcdr prev (cdr list1))
2744            (setq list1 (cdr list1)))
2745           (t
2746            (setq list2 (cdr list2)))))
2747   (setcdr prev nil)
2748   (cdr top)))
2749
2750 (defun gnus-compress-sequence (numbers &optional always-list)
2751   "Convert list of numbers to a list of ranges or a single range.
2752 If ALWAYS-LIST is non-nil, this function will always release a list of
2753 ranges."
2754   (let* ((first (car numbers))
2755          (last (car numbers))
2756          result)
2757     (if (null numbers)
2758         nil
2759       (if (not (listp (cdr numbers)))
2760           numbers
2761         (while numbers
2762           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2763                 ((= (1+ last) (car numbers)) ;Still in sequence
2764                  (setq last (car numbers)))
2765                 (t                      ;End of one sequence
2766                  (setq result 
2767                        (cons (if (= first last) first
2768                                (cons first last)) result))
2769                  (setq first (car numbers))
2770                  (setq last  (car numbers))))
2771           (setq numbers (cdr numbers)))
2772         (if (and (not always-list) (null result))
2773             (if (= first last) (list first) (cons first last))
2774           (nreverse (cons (if (= first last) first (cons first last))
2775                           result)))))))
2776
2777 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2778 (defun gnus-uncompress-range (ranges)
2779   "Expand a list of ranges into a list of numbers.
2780 RANGES is either a single range on the form `(num . num)' or a list of
2781 these ranges."
2782   (let (first last result)
2783     (cond 
2784      ((null ranges)
2785       nil)
2786      ((not (listp (cdr ranges)))
2787       (setq first (car ranges))
2788       (setq last (cdr ranges))
2789       (while (<= first last)
2790         (setq result (cons first result))
2791         (setq first (1+ first)))
2792       (nreverse result))
2793      (t
2794       (while ranges
2795         (if (atom (car ranges))
2796             (if (numberp (car ranges))
2797                 (setq result (cons (car ranges) result)))
2798           (setq first (car (car ranges)))
2799           (setq last  (cdr (car ranges)))
2800           (while (<= first last)
2801             (setq result (cons first result))
2802             (setq first (1+ first))))
2803         (setq ranges (cdr ranges)))
2804       (nreverse result)))))
2805
2806 (defun gnus-add-to-range (ranges list)
2807   "Return a list of ranges that has all articles from both RANGES and LIST.
2808 Note: LIST has to be sorted over `<'."
2809   (if (not ranges)
2810       (gnus-compress-sequence list t)
2811     (setq list (copy-sequence list))
2812     (or (listp (cdr ranges))
2813         (setq ranges (list ranges)))
2814     (let ((out ranges)
2815           ilist lowest highest temp)
2816       (while (and ranges list)
2817         (setq ilist list)
2818         (setq lowest (or (and (atom (car ranges)) (car ranges))
2819                          (car (car ranges))))
2820         (while (and list (cdr list) (< (car (cdr list)) lowest))
2821           (setq list (cdr list)))
2822         (if (< (car ilist) lowest)
2823             (progn
2824               (setq temp list)
2825               (setq list (cdr list))
2826               (setcdr temp nil)
2827               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2828         (setq highest (or (and (atom (car ranges)) (car ranges))
2829                           (cdr (car ranges))))
2830         (while (and list (<= (car list) highest))
2831           (setq list (cdr list)))
2832         (setq ranges (cdr ranges)))
2833       (if list
2834           (setq out (nconc (gnus-compress-sequence list t) out)))
2835       (setq out (sort out (lambda (r1 r2) 
2836                             (< (or (and (atom r1) r1) (car r1))
2837                                (or (and (atom r2) r2) (car r2))))))
2838       (setq ranges out)
2839       (while ranges
2840         (if (atom (car ranges))
2841             (if (cdr ranges)
2842                 (if (atom (car (cdr ranges)))
2843                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2844                         (progn
2845                           (setcar ranges (cons (car ranges) 
2846                                                (car (cdr ranges))))
2847                           (setcdr ranges (cdr (cdr ranges)))))
2848                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2849                       (progn
2850                         (setcar (car (cdr ranges)) (car ranges))
2851                         (setcar ranges (car (cdr ranges)))
2852                         (setcdr ranges (cdr (cdr ranges)))))))
2853           (if (cdr ranges)
2854               (if (atom (car (cdr ranges)))
2855                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2856                       (progn
2857                         (setcdr (car ranges) (car (cdr ranges)))
2858                         (setcdr ranges (cdr (cdr ranges)))))
2859                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2860                     (progn
2861                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2862                       (setcdr ranges (cdr (cdr ranges))))))))
2863         (setq ranges (cdr ranges)))
2864       out)))
2865
2866 (defun gnus-remove-from-range (ranges list)
2867   "Return a list of ranges that has all articles from LIST removed from RANGES.
2868 Note: LIST has to be sorted over `<'."
2869   ;; !!! This function shouldn't look like this, but I've got a headache.
2870   (gnus-compress-sequence 
2871    (gnus-sorted-complement
2872     (gnus-uncompress-range ranges) list)))
2873
2874 (defun gnus-member-of-range (number ranges)
2875   (if (not (listp (cdr ranges)))
2876       (and (>= number (car ranges)) 
2877            (<= number (cdr ranges)))
2878     (let ((not-stop t))
2879       (while (and ranges 
2880                   (if (numberp (car ranges))
2881                       (>= number (car ranges))
2882                     (>= number (car (car ranges))))
2883                   not-stop)
2884         (if (if (numberp (car ranges))
2885                 (= number (car ranges))
2886               (and (>= number (car (car ranges)))
2887                    (<= number (cdr (car ranges)))))
2888             (setq not-stop nil))
2889         (setq ranges (cdr ranges)))
2890       (not not-stop))))
2891
2892 \f
2893 ;;;
2894 ;;; Gnus group mode
2895 ;;;
2896
2897 (defvar gnus-group-mode-map nil)
2898 (defvar gnus-group-group-map nil)
2899 (defvar gnus-group-mark-map nil)
2900 (defvar gnus-group-list-map nil)
2901 (defvar gnus-group-sub-map nil)
2902 (put 'gnus-group-mode 'mode-class 'special)
2903
2904 (if gnus-group-mode-map
2905     nil
2906   (setq gnus-group-mode-map (make-keymap))
2907   (suppress-keymap gnus-group-mode-map)
2908   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2909   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2910   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2911   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2912   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2913   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2914   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2915   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2916   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2917   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2918   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2919   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2920   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2921   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2922   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2923   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2924   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2925   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2926   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2927   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2928   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2929   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2930   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2931   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2932   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2933   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2934   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2935   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2936   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2937   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2938   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2939   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2940   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2941   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2942   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2943   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2944   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2945   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2946   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2947   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2948   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2949   (define-key gnus-group-mode-map "V" 'gnus-version)
2950   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2951   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2952   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2953   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2954   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2955   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2956   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2957   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2958   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2959   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2960   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
2961   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2962   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2963   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
2964   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
2965
2966   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
2967   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
2968   (define-prefix-command 'gnus-group-mark-map)
2969   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
2970   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
2971   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
2972   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
2973
2974   (define-prefix-command 'gnus-group-group-map)
2975   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
2976   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
2977   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
2978   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
2979   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
2980   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
2981   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
2982   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
2983   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
2984   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
2985   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
2986   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
2987   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
2988
2989   (define-prefix-command 'gnus-group-list-map)
2990   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
2991   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
2992   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
2993   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
2994   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
2995   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
2996   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
2997   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
2998   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
2999
3000   (define-prefix-command 'gnus-group-sub-map)
3001   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3002   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3003   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3004   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3005   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3006   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3007   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3008   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3009
3010 (defun gnus-group-mode ()
3011   "Major mode for reading news.
3012
3013 All normal editing commands are switched off.
3014 \\<gnus-group-mode-map>
3015 The group buffer lists (some of) the groups available.  For instance,
3016 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3017 lists all zombie groups. 
3018
3019 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3020 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3021
3022 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3023
3024 The following commands are available:
3025
3026 \\{gnus-group-mode-map}"
3027   (interactive)
3028   (if gnus-visual (gnus-group-make-menu-bar))
3029   (kill-all-local-variables)
3030   (setq mode-line-modified "-- ")
3031   (make-local-variable 'mode-line-format)
3032   (setq mode-line-format (copy-sequence mode-line-format))
3033   (and (equal (nth 3 mode-line-format) "   ")
3034        (setcar (nthcdr 3 mode-line-format) ""))
3035   (setq major-mode 'gnus-group-mode)
3036   (setq mode-name "Group")
3037   (gnus-group-set-mode-line)
3038   (setq mode-line-process nil)
3039   (use-local-map gnus-group-mode-map)
3040   (buffer-disable-undo (current-buffer))
3041   (setq truncate-lines t)
3042   (setq buffer-read-only t)
3043   (run-hooks 'gnus-group-mode-hook))
3044
3045 (defun gnus-mouse-pick-group (e)
3046   (interactive "e")
3047   (mouse-set-point e)
3048   (gnus-group-read-group nil))
3049
3050 ;;;###autoload
3051 (defun gnus-no-server (&optional arg)
3052   "Read network news.
3053 If ARG is a positive number, Gnus will use that as the
3054 startup level. If ARG is nil, Gnus will be started at level 2. 
3055 If ARG is non-nil and not a positive number, Gnus will
3056 prompt the user for the name of an NNTP server to use.
3057 As opposed to `gnus', this command will not connect to the local server."
3058   (interactive "P")
3059   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3060
3061 (defalias '\(ding\) 'gnus)
3062
3063 ;;;###autoload
3064 (defun gnus (&optional arg dont-connect)
3065   "Read network news.
3066 If ARG is non-nil and a positive number, Gnus will use that as the
3067 startup level. If ARG is non-nil and not a positive number, Gnus will
3068 prompt the user for the name of an NNTP server to use."
3069   (interactive "P")
3070   (if (get-buffer gnus-group-buffer)
3071       (progn
3072         (switch-to-buffer gnus-group-buffer)
3073         (gnus-group-get-new-news))
3074     (gnus-clear-system)
3075     (nnheader-init-server-buffer)
3076     (gnus-read-init-file)
3077     (let ((level (and arg (numberp arg) (> arg 0) arg))
3078           did-connect)
3079       (unwind-protect
3080           (progn
3081             (gnus-group-setup-buffer)
3082             (or dont-connect 
3083                 (setq did-connect
3084                       (gnus-start-news-server (and arg (not level))))))
3085         (if (and (not dont-connect) 
3086                  (not did-connect))
3087             (gnus-group-quit)
3088           (run-hooks 'gnus-startup-hook)
3089           ;; NNTP server is successfully open. 
3090           (gnus-update-format-specifications)
3091           (gnus-summary-make-display-table)
3092           (let ((buffer-read-only nil))
3093             (erase-buffer)
3094             (if (not gnus-inhibit-startup-message)
3095                 (progn
3096                   (gnus-group-startup-message)
3097                   (sit-for 0))))
3098           (gnus-setup-news nil level)
3099           (and gnus-use-dribble-file (gnus-dribble-open))
3100           (gnus-group-list-groups level)
3101           (gnus-configure-windows 'group))))))
3102
3103 (defun gnus-group-startup-message (&optional x y)
3104   "Insert startup message in current buffer."
3105   ;; Insert the message.
3106   (erase-buffer)
3107   (insert
3108    (format "
3109     %s
3110            A newsreader 
3111       for GNU Emacs
3112
3113         Based on GNUS 
3114              written by 
3115      Masanobu UMEDA
3116
3117     Lars Magne 
3118          Ingebrigtsen 
3119       larsi@ifi.uio.no
3120
3121            gnus-version))
3122   ;; And then hack it.
3123   ;; 18 is the longest line.
3124   (indent-rigidly (point-min) (point-max) 
3125                   (/ (max (- (window-width) (or x 28)) 0) 2))
3126   (goto-char (point-min))
3127   ;; +4 is fuzzy factor.
3128   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3129
3130 (defun gnus-group-setup-buffer ()
3131   (or (get-buffer gnus-group-buffer)
3132       (progn
3133         (switch-to-buffer gnus-group-buffer)
3134         (gnus-add-current-to-buffer-list)
3135         (gnus-group-mode)
3136         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3137
3138 (defun gnus-group-list-groups (level &optional unread)
3139   "List newsgroups with level LEVEL or lower that have unread alticles.
3140 Default is all subscribed groups.
3141 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3142   (interactive (list (and current-prefix-arg
3143                           (prefix-numeric-value current-prefix-arg))))
3144   (if gnus-group-use-permanent-levels
3145       (progn
3146         (setq gnus-group-default-list-level 
3147               (or level gnus-group-default-list-level))
3148         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3149     (setq level (or level gnus-group-default-list-level 
3150                     gnus-level-subscribed)))
3151   (gnus-group-setup-buffer)     ;May call from out of group buffer
3152   (let ((case-fold-search nil)
3153         (group (gnus-group-group-name)))
3154     (funcall gnus-group-prepare-function level unread nil)
3155     (if (zerop (buffer-size))
3156         (gnus-message 5 gnus-no-groups-message)
3157       (goto-char (point-min))
3158       (if (not group)
3159           ;; Go to the first group with unread articles.
3160           (gnus-group-search-forward nil nil nil t)
3161         ;; Find the right group to put point on. If the current group
3162         ;; has disapeared in the new listing, try to find the next
3163         ;; one. If no next one can be found, just leave point at the
3164         ;; first newsgroup in the buffer.
3165         (if (not (gnus-goto-char
3166                   (text-property-any (point-min) (point-max) 
3167                                      'gnus-group (intern group))))
3168             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3169               (while (and newsrc
3170                           (not (gnus-goto-char 
3171                                 (text-property-any 
3172                                  (point-min) (point-max) 'gnus-group 
3173                                  (intern (car (car newsrc)))))))
3174                 (setq newsrc (cdr newsrc)))
3175               (or newsrc (progn (goto-char (point-max))
3176                                 (forward-line -1))))))
3177       ;; Adjust cursor point.
3178       (gnus-group-position-cursor))))
3179
3180 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3181   "List all newsgroups with unread articles of level LEVEL or lower.
3182 If ALL is non-nil, list groups that have no unread articles.
3183 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3184 If REGEXP, only list groups matching REGEXP."
3185   (set-buffer gnus-group-buffer)
3186   (let ((buffer-read-only nil)
3187         (newsrc (cdr gnus-newsrc-alist))
3188         (lowest (or lowest 1))
3189         info clevel unread group)
3190     (erase-buffer)
3191     (if (< lowest gnus-level-zombie)
3192         ;; List living groups.
3193         (while newsrc
3194           (setq info (car newsrc)
3195                 group (car info)
3196                 newsrc (cdr newsrc)
3197                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3198           (and unread ; This group might be bogus
3199                (or (not regexp)
3200                    (string-match regexp group))
3201                (<= (setq clevel (car (cdr info))) level) 
3202                (>= clevel lowest)
3203                (or all            ; We list all groups?
3204                    (eq unread t)  ; We list unactivated groups
3205                    (> unread 0)   ; We list groups with unread articles
3206                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3207                (gnus-group-insert-group-line 
3208                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3209
3210     ;; List dead groups.
3211     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3212          (gnus-group-prepare-flat-list-dead 
3213           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3214           gnus-level-zombie ?Z
3215           regexp))
3216     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3217          (gnus-group-prepare-flat-list-dead 
3218           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3219           gnus-level-killed ?K regexp))
3220
3221     (gnus-group-set-mode-line)
3222     (setq gnus-have-all-newsgroups all)
3223     (run-hooks 'gnus-group-prepare-hook)))
3224
3225 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3226   ;; List zombies and killed lists somehwat faster, which was
3227   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3228   ;; this by ignoring the group format specification altogether.
3229   (let (group beg)
3230     (while groups
3231       (setq group (car groups)
3232             groups (cdr groups))
3233       (if (or (not regexp)
3234               (string-match regexp group))
3235           (progn
3236             (setq beg (point))
3237             (insert (format " %c    *: %s\n" mark group))
3238             (add-text-properties 
3239              beg (1+ beg) 
3240              (list 'gnus-group (intern group)
3241                    'gnus-unread t
3242                    'gnus-level level)))))))
3243
3244 (defun gnus-group-real-name (group)
3245   "Find the real name of a foreign newsgroup."
3246   (if (string-match ":[^:]+$" group)
3247       (substring group (1+ (match-beginning 0)))
3248     group))
3249
3250 (defun gnus-group-prefixed-name (group method)
3251   "Return the whole name from GROUP and METHOD."
3252   (and (stringp method) (setq method (gnus-server-to-method method)))
3253   (concat (format "%s" (car method))
3254           (if (and 
3255                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3256                (not (string= (nth 1 method) "")))
3257               (concat "+" (nth 1 method)))
3258           ":" group))
3259
3260 (defun gnus-group-real-prefix (group)
3261   "Return the prefix of the current group name."
3262   (if (string-match "^[^:]+:" group)
3263       (substring group 0 (match-end 0))
3264     ""))
3265
3266 (defun gnus-group-method-name (group)
3267   "Return the method used for selecting GROUP."
3268   (let ((prefix (gnus-group-real-prefix group)))
3269     (if (equal prefix "")
3270         gnus-select-method
3271       (if (string-match "^[^\\+]+\\+" prefix)
3272           (list (intern (substring prefix 0 (1- (match-end 0))))
3273                 (substring prefix (match-end 0) (1- (length prefix))))
3274         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3275
3276 (defun gnus-group-foreign-p (group)
3277   "Return nil if GROUP is native, non-nil if it is foreign."
3278   (string-match ":" group))
3279
3280 (defun gnus-group-set-info (info &optional method-only-group part)
3281   (let* ((entry (gnus-gethash
3282                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3283          (part-info info)
3284          (info (if method-only-group (nth 2 entry) info)))
3285     (if (not method-only-group)
3286         ()
3287       (or entry
3288           (error "Trying to change non-existant group %s" method-only-group))
3289       ;; We have recevied parts of the actual group info - either the
3290       ;; select method or the group parameters.  We first check
3291       ;; whether we have to extend the info, and if so, do that.
3292       (let ((len (length info))
3293             (total (if (eq part 'method) 5 6)))
3294         (and (< len total)
3295              (setcdr (nthcdr (1- len) info)
3296                      (make-list (- total len) nil)))
3297         ;; Then we enter the new info.
3298         (setcar (nthcdr (1- total) info) part-info)))
3299     ;; We uncompress some lists of marked articles.
3300     (let (marked)
3301       (if (not (setq marked (nth 3 info)))
3302           ()
3303         (while marked
3304           (or (eq 'score (car (car marked)))
3305               (eq 'bookmark (car (car marked)))
3306               (eq 'killed (car (car marked)))
3307               (setcdr (car marked) 
3308                       (gnus-uncompress-range (cdr (car marked)))))
3309           (setq marked (cdr marked)))))
3310     (if entry
3311         ()
3312       ;; This is a new group, so we just create it.
3313       (save-excursion
3314         (set-buffer gnus-group-buffer)
3315         (if (nth 4 info)
3316             ;; It's a foreign group...
3317             (gnus-group-make-group 
3318              (gnus-group-real-name (car info))
3319              (prin1-to-string (car (nth 4 info)))
3320              (nth 1 (nth 4 info)))
3321           ;; It's a native group.
3322           (gnus-group-make-group
3323            (car info)
3324            (prin1-to-string (car gnus-select-method))
3325            (nth 1 gnus-select-method)))
3326         (gnus-message 6 "Note: New group created")
3327         (setq entry 
3328               (gnus-gethash (gnus-group-prefixed-name 
3329                              (gnus-group-real-name (car info))
3330                              (or (nth 4 info) gnus-select-method))
3331                             gnus-newsrc-hashtb))))
3332     ;; Whether it was a new group or not, we now have the entry, so we
3333     ;; can do the update.
3334     (if entry
3335         (progn
3336           (setcar (nthcdr 2 entry) info)
3337           (if (and (not (eq (car entry) t)) 
3338                    (gnus-gethash (car info) gnus-active-hashtb))
3339               (let ((marked (nth 3 info)))
3340                 (setcar entry 
3341                         (max 0 (- (length (gnus-list-of-unread-articles 
3342                                            (car info)))
3343                                   (length (cdr (assq 'tick marked)))
3344                                   (length (cdr (assq 'dormant marked)))))))))
3345       (error "No such group: %s" (car info)))))
3346
3347 (defun gnus-group-set-method-info (group select-method)
3348   (gnus-group-set-info select-method group 'method))
3349
3350 (defun gnus-group-set-params-info (group params)
3351   (gnus-group-set-info params group 'params))
3352
3353 (defun gnus-group-update-group-line ()
3354   "This function updates the current line in the newsgroup buffer and
3355 moves the point to the colon."
3356   (let* ((buffer-read-only nil)
3357          (group (gnus-group-group-name))
3358          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3359     (if entry
3360         (gnus-dribble-enter 
3361          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3362                  ")")))
3363     (beginning-of-line)
3364     (delete-region (point) (progn (forward-line 1) (point)))
3365     (gnus-group-insert-group-line-info group)
3366     (forward-line -1)
3367     (gnus-group-position-cursor)))
3368
3369 (defun gnus-group-insert-group-line-info (group)
3370   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3371         active info)
3372     (if entry
3373         (progn
3374           (setq info (nth 2 entry))
3375           (gnus-group-insert-group-line 
3376            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3377       (setq active (gnus-gethash group gnus-active-hashtb))
3378       (gnus-group-insert-group-line 
3379        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3380                    gnus-level-killed)
3381        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3382
3383 (defun gnus-group-insert-group-line (gformat group level marked number method)
3384   (let* ((gformat (or gformat gnus-group-line-format-spec))
3385          (active (gnus-gethash group gnus-active-hashtb))
3386          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3387          (number-of-dormant (length (cdr (assq 'dormant marked))))
3388          (number-of-ticked (length (cdr (assq 'tick marked))))
3389          (number-of-ticked-and-dormant
3390           (+ number-of-ticked number-of-dormant))
3391          (number-of-unread-unticked 
3392           (if (numberp number) (int-to-string (max 0 number))
3393             "*"))
3394          (number-of-read
3395           (if (numberp number)
3396               (max 0 (- number-total number))
3397             "*"))
3398          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3399                            ((<= level gnus-level-unsubscribed) ?U)
3400                            ((= level gnus-level-zombie) ?Z)
3401                            (t ?K)))
3402          (qualified-group (gnus-group-real-name group))
3403          (newsgroup-description 
3404           (if gnus-description-hashtb
3405               (or (gnus-gethash group gnus-description-hashtb) "")
3406             ""))
3407          (moderated (if (member group gnus-moderated-list) ?m ? ))
3408          (moderated-string (if (eq moderated ?m) "(m)" ""))
3409          (method (gnus-server-get-method group method))
3410          (news-server (or (car (cdr method)) ""))
3411          (news-method (or (car method) ""))
3412          (news-method-string 
3413           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3414          (marked (if (and 
3415                       (numberp number) 
3416                       (zerop number)
3417                       (> number-of-ticked 0))
3418                      ?* ? ))
3419          (number (if (eq number t) "*" (+ number number-of-dormant 
3420                                           number-of-ticked)))
3421          (process-marked (if (member qualified-group gnus-group-marked)
3422                              gnus-process-mark ? ))
3423          (buffer-read-only nil)
3424          b)
3425     (beginning-of-line)
3426     (setq b (point))
3427     ;; Insert the text.
3428     (insert (eval gformat))
3429
3430     (add-text-properties 
3431      b (1+ b) (list 'gnus-group (intern group)
3432                     'gnus-unread (if (numberp number)
3433                                      (string-to-int number-of-unread-unticked)
3434                                    t)
3435                     'gnus-marked marked
3436                     'gnus-level level))))
3437
3438 (defun gnus-group-update-group (group &optional visible-only)
3439   "Update newsgroup info of GROUP.
3440 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3441   (save-excursion
3442     (set-buffer gnus-group-buffer)
3443     (let ((buffer-read-only nil)
3444           visible)
3445       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3446         (if entry
3447             (gnus-dribble-enter 
3448              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3449                      ")"))))
3450       ;; Buffer may be narrowed.
3451       (save-restriction
3452         (widen)
3453         ;; Search a line to modify.  If the buffer is large, the search
3454         ;; takes long time.  In most cases, current point is on the line
3455         ;; we are looking for.  So, first of all, check current line. 
3456         (if (or (progn
3457                   (beginning-of-line)
3458                   (eq (get-text-property (point) 'gnus-group)
3459                       (intern group)))
3460                 (progn
3461                   (gnus-goto-char 
3462                    (text-property-any 
3463                     (point-min) (point-max) 'gnus-group (intern group)))))
3464             ;; GROUP is listed in current buffer. So, delete old line.
3465             (progn
3466               (setq visible t)
3467               (beginning-of-line)
3468               (delete-region (point) (progn (forward-line 1) (point))))
3469           ;; No such line in the buffer, find out where it's supposed to
3470           ;; go, and insert it there (or at the end of the buffer).
3471           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3472           (or visible-only
3473               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
3474                 (while (and entry
3475                             (car entry)
3476                             (not
3477                              (gnus-goto-char
3478                               (text-property-any
3479                                (point-min) (point-max) 
3480                                'gnus-group (intern (car (car entry)))))))
3481                   (setq entry (cdr entry)))
3482                 (if entry (forward-line 1)
3483                   (goto-char (point-max)))))))
3484       (if (or visible (not visible-only))
3485           (gnus-group-insert-group-line-info group))
3486       (gnus-group-set-mode-line))))
3487
3488 (defun gnus-group-set-mode-line ()
3489   (if (memq 'group gnus-updated-mode-lines)
3490       (let* ((gformat (or gnus-group-mode-line-format-spec
3491                           (setq gnus-group-mode-line-format-spec
3492                                 (gnus-parse-format 
3493                                  gnus-group-mode-line-format 
3494                                  gnus-group-mode-line-format-alist))))
3495              (news-server (car (cdr gnus-select-method)))
3496              (news-method (car gnus-select-method))
3497              (max-len 60)
3498              (mode-string (eval gformat)))
3499         (setq mode-string (eval gformat))
3500         (if (> (length mode-string) max-len) 
3501             (setq mode-string (substring mode-string 0 (- max-len 4))))
3502         (setq mode-line-buffer-identification mode-string)
3503         (set-buffer-modified-p t))))
3504
3505 (defun gnus-group-group-name ()
3506   "Get the name of the newsgroup on the current line."
3507   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3508     (and group (symbol-name group))))
3509
3510 (defun gnus-group-group-level ()
3511   "Get the level of the newsgroup on the current line."
3512   (get-text-property (gnus-point-at-bol) 'gnus-level))
3513
3514 (defun gnus-group-search-forward (&optional backward all level first-too)
3515   "Find the next newsgroup with unread articles.
3516 If BACKWARD is non-nil, find the previous newsgroup instead.
3517 If ALL is non-nil, just find any newsgroup.
3518 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3519 group exists.
3520 If FIRST-TOO, the current line is also eligeble as a target."
3521   (let ((way (if backward -1 1))
3522         (low 10)
3523         (beg (point))
3524         pos found)
3525     (if (and backward (progn (beginning-of-line)) (bobp))
3526         nil
3527       (or first-too (forward-line way))
3528       (while (and 
3529               (not (eobp))
3530               (not (setq 
3531                     found 
3532                     (and (or all
3533                              (and
3534                               (let ((unread 
3535                                      (get-text-property (point) 'gnus-unread)))
3536                                 (or (eq unread t) (and unread (> unread 0))))
3537                               (let ((lev (get-text-property
3538                                           (point) 'gnus-level)))
3539                                 (and lev (<= (get-text-property 
3540                                               (point) 'gnus-level)
3541                                              gnus-level-subscribed)))))
3542                          (or (not level)
3543                              (let ((lev (get-text-property (point) 'gnus-level)))
3544                                (if (and lev (<= lev level))
3545                                    t
3546                                  (if (< lev low)
3547                                      (progn
3548                                        (setq low lev)
3549                                        (setq pos (point))))
3550                                  nil))))))
3551               (zerop (forward-line way)))))
3552     (if found 
3553         (progn (gnus-group-position-cursor) t)
3554       (if pos (goto-char pos) (goto-char beg))
3555       nil)))
3556
3557 ;;; Gnus group mode commands
3558
3559 ;; Group marking.
3560
3561 (defun gnus-group-mark-group (n &optional unmark)
3562   "Mark the current group."
3563   (interactive "p")
3564   (let ((buffer-read-only nil)
3565         group)
3566     (while 
3567         (and (> n 0) 
3568              (setq group (gnus-group-group-name))
3569              (progn
3570                (beginning-of-line)
3571                (forward-char 2)
3572                (delete-char 1)
3573                (if unmark
3574                    (progn
3575                      (insert " ")
3576                      (setq gnus-group-marked (delete group gnus-group-marked)))
3577                  (insert "#")
3578                  (setq gnus-group-marked
3579                        (cons group (delete group gnus-group-marked))))
3580                t)
3581              (zerop (gnus-group-next-group 1)))
3582       (setq n (1- n)))
3583     (gnus-summary-position-cursor)
3584     n))
3585
3586 (defun gnus-group-unmark-group (n)
3587   "Remove the mark from the current group."
3588   (interactive "p")
3589   (gnus-group-mark-group n 'unmark))
3590
3591 (defun gnus-group-mark-region (unmark beg end)
3592   "Mark all groups between point and mark.
3593 If UNMARK, remove the mark instead."
3594   (interactive "P\nr")
3595   (let ((num (count-lines beg end)))
3596     (save-excursion
3597       (goto-char beg)
3598       (- num (gnus-group-mark-group num unmark)))))
3599
3600 (defun gnus-group-remove-mark (group)
3601   (and (gnus-group-goto-group group)
3602        (save-excursion
3603          (gnus-group-mark-group 1 'unmark))))
3604
3605 ;; Return a list of groups to work on.  Take into consideration N (the
3606 ;; prefix) and the list of marked groups.
3607 (defun gnus-group-process-prefix (n)
3608   (cond (n
3609          (setq n (prefix-numeric-value n))
3610          ;; There is a prefix, so we return a list of the N next
3611          ;; groups. 
3612          (let ((way (if (< n 0) -1 1))
3613                (n (abs n))
3614                group groups)
3615            (save-excursion
3616              (while (and (> n 0)
3617                          (setq group (gnus-group-group-name)))
3618                (setq groups (cons group groups))
3619                (setq n (1- n))
3620                (forward-line way)))
3621            (nreverse groups)))
3622         (gnus-group-marked
3623          ;; No prefix, but a list of marked articles.
3624          (reverse gnus-group-marked))
3625         (t
3626          ;; Neither marked articles or a prefix, so we return the
3627          ;; current group.
3628          (let ((group (gnus-group-group-name)))
3629            (and group (list group))))))
3630
3631 ;; Selecting groups.
3632
3633 (defun gnus-group-read-group (all &optional no-article group)
3634   "Read news in this newsgroup.
3635 If argument ALL is non-nil, already read articles become readable.
3636 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3637   (interactive "P")
3638   (let ((group (or group (gnus-group-group-name)))
3639         number active marked entry)
3640     (or group (error "No group on current line"))
3641     (setq marked 
3642           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3643     ;; This group might be a dead group. In that case we have to get
3644     ;; the number of unread articles from `gnus-active-hashtb'.
3645     (if entry
3646         (setq number (car entry))
3647       (if (setq active (gnus-gethash group gnus-active-hashtb))
3648           (setq number (- (1+ (cdr active)) (car active)))))
3649     (gnus-summary-read-group 
3650      group (or all (and (numberp number) 
3651                         (zerop (+ number (length (cdr (assq 'tick marked)))
3652                                   (length (cdr (assq 'dormant marked)))))))
3653      no-article)))
3654
3655 (defun gnus-group-select-group (all)
3656   "Select this newsgroup.
3657 No article is selected automatically.
3658 If argument ALL is non-nil, already read articles become readable."
3659   (interactive "P")
3660   (gnus-group-read-group all t))
3661
3662 ;; Enter a group that is not in the group buffer. Non-nil is returned
3663 ;; if selection was successful.
3664 (defun gnus-group-read-ephemeral-group 
3665   (group method &optional activate quit-config)
3666   (let ((group (if (gnus-group-foreign-p group) group
3667                  (gnus-group-prefixed-name group method))))
3668     (gnus-sethash 
3669      group
3670      (list t nil (list group gnus-level-default-subscribed nil nil 
3671                        (append method
3672                                (list
3673                                 (list 'quit-config 
3674                                       (if quit-config quit-config
3675                                         (cons (current-buffer) 'summary)))))))
3676      gnus-newsrc-hashtb)
3677     (set-buffer gnus-group-buffer)
3678     (or (gnus-server-opened method)
3679         (gnus-open-server method)
3680         (error "Unable to contact server: %s" (gnus-status-message method)))
3681     (if activate (gnus-request-group group))
3682     (condition-case ()
3683         (gnus-group-read-group t t group)
3684       (error nil)
3685       (quit nil))
3686     (not (equal major-mode 'gnus-group-mode))))
3687   
3688 (defun gnus-group-jump-to-group (group)
3689   "Jump to newsgroup GROUP."
3690   (interactive (list (completing-read "Group: " gnus-active-hashtb nil
3691                                       (not (not gnus-read-active-file)))))
3692
3693   (if (equal group "")
3694       (error "empty group name"))
3695
3696   (let ((b (text-property-any (point-min) (point-max) 
3697                               'gnus-group (intern group))))
3698     (if b
3699         ;; Either go to the line in the group buffer...
3700         (goto-char b)
3701       ;; ... or insert the line.
3702       (or
3703        (gnus-gethash group gnus-active-hashtb)
3704        (gnus-activate-newsgroup group)
3705        (error "%s error: %s" group (gnus-status-message group)))
3706
3707       (gnus-group-update-group group)
3708       (goto-char (text-property-any (point-min) (point-max) 
3709                                     'gnus-group (intern group)))))
3710   ;; Adjust cursor point.
3711   (gnus-group-position-cursor))
3712
3713 (defun gnus-group-goto-group (group)
3714   "Goto to newsgroup GROUP."
3715   (let ((b (text-property-any (point-min) (point-max) 
3716                               'gnus-group (intern group))))
3717     (and b (goto-char b))))
3718
3719 (defun gnus-group-next-group (n)
3720   "Go to next N'th newsgroup.
3721 If N is negative, search backward instead.
3722 Returns the difference between N and the number of skips actually
3723 done."
3724   (interactive "p")
3725   (gnus-group-next-unread-group n t))
3726
3727 (defun gnus-group-next-unread-group (n &optional all level)
3728   "Go to next N'th unread newsgroup.
3729 If N is negative, search backward instead.
3730 If ALL is non-nil, choose any newsgroup, unread or not.
3731 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3732 such group can be found, the next group with a level higher than
3733 LEVEL.
3734 Returns the difference between N and the number of skips actually
3735 made."
3736   (interactive "p")
3737   (let ((backward (< n 0))
3738         (n (abs n)))
3739     (while (and (> n 0)
3740                 (gnus-group-search-forward 
3741                  backward (or (not gnus-group-goto-unread) all) level))
3742       (setq n (1- n)))
3743     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3744                                (if level " on this level or higher" "")))
3745     n))
3746
3747 (defun gnus-group-prev-group (n)
3748   "Go to previous N'th newsgroup.
3749 Returns the difference between N and the number of skips actually
3750 done."
3751   (interactive "p")
3752   (gnus-group-next-unread-group (- n) t))
3753
3754 (defun gnus-group-prev-unread-group (n)
3755   "Go to previous N'th unread newsgroup.
3756 Returns the difference between N and the number of skips actually
3757 done."  
3758   (interactive "p")
3759   (gnus-group-next-unread-group (- n)))
3760
3761 (defun gnus-group-next-unread-group-same-level (n)
3762   "Go to next N'th unread newsgroup on the same level.
3763 If N is negative, search backward instead.
3764 Returns the difference between N and the number of skips actually
3765 done."
3766   (interactive "p")
3767   (gnus-group-next-unread-group n t (gnus-group-group-level))
3768   (gnus-group-position-cursor))
3769
3770 (defun gnus-group-prev-unread-group-same-level (n)
3771   "Go to next N'th unread newsgroup on the same level.
3772 Returns the difference between N and the number of skips actually
3773 done."
3774   (interactive "p")
3775   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3776   (gnus-group-position-cursor))
3777
3778 (defun gnus-group-best-unread-group (&optional exclude-group)
3779   "Go to the group with the highest level.
3780 If EXCLUDE-GROUP, do not go to that group."
3781   (interactive)
3782   (goto-char (point-min))
3783   (let ((best 100000)
3784         unread best-point)
3785     (while (setq unread (get-text-property (point) 'gnus-unread))
3786       (if (and (numberp unread) (> unread 0))
3787           (progn
3788             (or best-point (setq best-point (point)))
3789             (if (and (< (get-text-property (point) 'gnus-level) best)
3790                      (or (not exclude-group)
3791                          (not (equal exclude-group (gnus-group-group-name)))))
3792                 (progn 
3793                   (setq best (get-text-property (point) 'gnus-level))
3794                   (setq best-point (point))))))
3795       (forward-line 1))
3796     (if best-point (goto-char best-point))
3797     (gnus-summary-position-cursor)
3798     (and best-point (gnus-group-group-name))))
3799
3800 (defun gnus-group-first-unread-group ()
3801   "Go to the first group with unread articles."
3802   (interactive)
3803   (goto-char (point-min))
3804   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3805       (gnus-group-next-unread-group 1))
3806   (gnus-group-position-cursor))
3807
3808 (defun gnus-group-enter-server-mode ()
3809   "Jump to the server buffer."
3810   (interactive)
3811   (gnus-server-setup-buffer)
3812   (gnus-configure-windows 'server)
3813   (gnus-server-prepare))
3814
3815 (defun gnus-group-make-group (name method &optional address)
3816   "Add a new newsgroup.
3817 The user will be prompted for a NAME, for a select METHOD, and an
3818 ADDRESS."
3819   (interactive
3820    (cons 
3821     (read-string "Group name: ")
3822     (let ((method
3823            (completing-read 
3824             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3825             nil t)))
3826       (if (assoc method gnus-valid-select-methods)
3827           (list method
3828                 (if (memq 'prompt-address
3829                           (assoc method gnus-valid-select-methods))
3830                     (read-string "Address: ")
3831                   ""))
3832         (list method nil)))))
3833   
3834   (let* ((meth (if address (list (intern method) address) method))
3835          (nname (gnus-group-prefixed-name name meth))
3836          info)
3837     (and (gnus-gethash nname gnus-newsrc-hashtb)
3838          (error "Group %s already exists" nname))
3839     (gnus-group-change-level 
3840      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3841      gnus-level-default-subscribed gnus-level-killed 
3842      (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3843                        gnus-newsrc-hashtb) t)
3844     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3845     (gnus-dribble-enter 
3846      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3847     (gnus-group-insert-group-line-info nname)
3848
3849     (and (gnus-check-backend-function 'request-create-group nname)
3850          (gnus-request-create-group nname))))
3851
3852 (defun gnus-group-edit-group (group &optional part)
3853   "Edit the group on the current line."
3854   (interactive (list (gnus-group-group-name)))
3855   (let ((done-func '(lambda () 
3856                       "Exit editing mode and update the information."
3857                       (interactive)
3858                       (gnus-group-edit-group-done 'part 'group)))
3859         (part (or part 'info))
3860         (winconf (current-window-configuration))
3861         info)
3862     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3863       (error "No group on current line"))
3864     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3865     (gnus-configure-windows 'edit-group)
3866     (gnus-add-current-to-buffer-list)
3867     (emacs-lisp-mode)
3868     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3869     (use-local-map (copy-keymap emacs-lisp-mode-map))
3870     (local-set-key "\C-c\C-c" done-func)
3871     (make-local-variable 'gnus-prev-winconf)
3872     (setq gnus-prev-winconf winconf)
3873     ;; We modify the func to let it know what part it is editing.
3874     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3875     (setcar (cdr (cdr (nth 4 done-func))) group)
3876     (erase-buffer)
3877     (insert
3878      (cond 
3879       ((eq part 'method)
3880        ";; Type `C-c C-c' after editing the select method.\n\n")
3881       ((eq part 'params)
3882        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3883       ((eq part 'info)
3884        ";; Type `C-c C-c' after editing the group info.\n\n")))
3885     (let ((cinfo (gnus-copy-sequence info))
3886           marked)
3887       (if (not (setq marked (nth 3 cinfo)))
3888           ()
3889         (while marked
3890           (or (eq 'score (car (car marked)))
3891               (eq 'bookmark (car (car marked)))
3892               (eq 'killed (car (car marked)))
3893               (setcdr (car marked) 
3894                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3895           (setq marked (cdr marked))))
3896       (insert 
3897        (pp-to-string
3898         (cond ((eq part 'method)
3899                (or (nth 4 info) "native"))
3900               ((eq part 'params)
3901                (nth 5 info))
3902               (t
3903                cinfo)))
3904        "\n"))))
3905
3906 (defun gnus-group-edit-group-method (group)
3907   "Edit the select method of GROUP."
3908   (interactive (list (gnus-group-group-name)))
3909   (gnus-group-edit-group group 'method))
3910
3911 (defun gnus-group-edit-group-parameters (group)
3912   "Edit the group parameters of GROUP."
3913   (interactive (list (gnus-group-group-name)))
3914   (gnus-group-edit-group group 'params))
3915
3916 (defun gnus-group-edit-group-done (part group)
3917   "Get info from buffer, update variables and jump to the group buffer."
3918   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3919   (goto-char (point-min))
3920   (let ((form (read (current-buffer)))
3921         (winconf gnus-prev-winconf))
3922     (if (eq part 'info) 
3923         (gnus-group-set-info form)
3924       (gnus-group-set-info form group part))
3925     (kill-buffer (current-buffer))
3926     (and winconf (set-window-configuration winconf))
3927     (set-buffer gnus-group-buffer)
3928     (gnus-group-update-group (gnus-group-group-name))
3929     (gnus-group-position-cursor)))
3930
3931 (defun gnus-group-make-help-group ()
3932   "Create the (ding) Gnus documentation group."
3933   (interactive)
3934   (let ((path load-path))
3935     (and (gnus-gethash (setq name (gnus-group-prefixed-name
3936                                    "gnus-help" '(nndoc "gnus-help")))
3937                        gnus-newsrc-hashtb)
3938          (error "Documentation group already exists"))
3939     (while (and path
3940                 (not (file-exists-p (concat (file-name-as-directory (car path))
3941                                             "doc.txt"))))
3942       (setq path (cdr path)))
3943     (or path (error "Couldn't find doc group"))
3944     (gnus-group-make-group 
3945      (gnus-group-real-name name)
3946      (list 'nndoc name
3947            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
3948            (list 'nndoc-article-type 'mbox))))
3949   (gnus-group-position-cursor))
3950
3951 (defun gnus-group-make-doc-group (file type)
3952   "Create a group that uses a single file as the source."
3953   (interactive 
3954    (list (read-file-name "File name: ") 
3955          (let ((err "")
3956                found char)
3957            (while (not found)
3958              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
3959              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
3960                                ((= char ?b) 'babyl)
3961                                ((= char ?d) 'digest)
3962                                (t (setq mess "%c unknown. " char)
3963                                   nil))))
3964            found)))
3965   (let* ((file (expand-file-name file))
3966          (name (gnus-generate-new-group-name
3967                 (gnus-group-prefixed-name
3968                  (file-name-nondirectory file) '(nndoc "")))))
3969     (gnus-group-make-group 
3970      (gnus-group-real-name name)
3971      (list 'nndoc name
3972            (list 'nndoc-address file)
3973            (list 'nndoc-article-type type)))))
3974
3975 (defun gnus-group-make-archive-group ()
3976   "Create the (ding) Gnus archive group."
3977   (interactive)
3978   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3979                      gnus-newsrc-hashtb)
3980        (error "Archive group already exists"))
3981   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
3982   (gnus-group-position-cursor))
3983
3984 (defun gnus-group-make-directory-group (dir)
3985   "Create an nndir group.
3986 The user will be prompted for a directory. The contents of this
3987 directory will be used as a newsgroup. The directory should contain
3988 mail messages or news articles in files that have numeric names."
3989   (interactive
3990    (list (read-file-name "Create group from directory: ")))
3991   (or (file-exists-p dir) (error "No such directory"))
3992   (or (file-directory-p dir) (error "Not a directory"))
3993   (gnus-group-make-group dir "nndir" dir)
3994   (gnus-group-position-cursor))
3995
3996 (defun gnus-group-make-kiboze-group (group address scores)
3997   "Create an nnkiboze group.
3998 The user will be prompted for a name, a regexp to match groups, and
3999 score file entries for articles to include in the group."
4000   (interactive
4001    (list
4002     (read-string "nnkiboze group name: ")
4003     (read-string "Source groups (regexp): ")
4004     (let ((headers (mapcar (lambda (group) (list group))
4005                            '("subject" "from" "number" "date" "message-id"
4006                              "references" "chars" "lines" "xref")))
4007           scores header regexp regexps)
4008       (while (not (equal "" (setq header (completing-read 
4009                                           "Match on header: " headers nil t))))
4010         (setq regexps nil)
4011         (while (not (equal "" (setq regexp (read-string 
4012                                             (format "Match on %s (string): "
4013                                                     header)))))
4014           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4015         (setq scores (cons (cons header regexps) scores)))
4016       scores)))
4017   (gnus-group-make-group group "nnkiboze" address)
4018   (save-excursion
4019     (gnus-set-work-buffer)
4020     (let (emacs-lisp-mode-hook)
4021       (pp scores (current-buffer)))
4022     (write-region (point-min) (point-max) 
4023                   (concat (or gnus-kill-files-directory "~/News")
4024                           "nnkiboze:" group "." gnus-score-file-suffix)))
4025   (gnus-group-position-cursor))
4026
4027 (defun gnus-group-add-to-virtual (n vgroup)
4028   "Add the current group to a virtual group."
4029   (interactive
4030    (list current-prefix-arg
4031          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4032                           "nnvirtual:")))
4033   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4034       (error "%s is not an nnvirtual group" vgroup))
4035   (let* ((groups (gnus-group-process-prefix n))
4036          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4037     (setcar (cdr method)
4038             (concat 
4039              (nth 1 method) "\\|"
4040              (mapconcat 
4041               (lambda (s) 
4042                 (gnus-group-remove-mark s)
4043                 (concat "\\(^" (regexp-quote s) "$\\)"))
4044               groups "\\|"))))
4045   (gnus-group-position-cursor))
4046
4047 (defun gnus-group-make-empty-virtual (group)
4048   "Create a new, fresh, empty virtual group."
4049   (interactive "sCreate new, empty virtual group: ")
4050   (let* ((method (list 'nnvirtual ""))
4051          (pgroup (gnus-group-prefixed-name group method)))
4052     ;; Check whether it exists already.
4053     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4054          (error "Group %s already exists." pgroup))
4055     ;; Subscribe the new group after the group on the current line.
4056     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4057     (gnus-group-update-group pgroup)
4058     (forward-line -1)
4059     (gnus-group-position-cursor)))
4060
4061 (defun gnus-group-enter-directory (dir)
4062   "Enter an ephemeral nneething group."
4063   (interactive "DDirectory to read: ")
4064   (let* ((method (list 'nneething dir))
4065          (leaf (gnus-group-prefixed-name
4066                 (file-name-nondirectory (directory-file-name dir))
4067                 method))
4068          (name (gnus-generate-new-group-name leaf))
4069          (num 0))
4070     (let ((nneething-read-only t))
4071       (or (gnus-group-read-ephemeral-group 
4072            name method t
4073            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4074                                       'summary 'group)))
4075           (error "Couldn't enter %s" dir)))))
4076
4077 ;; Group sorting commands
4078 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4079
4080 (defun gnus-group-sort-groups ()
4081   "Sort the group buffer using `gnus-group-sort-function'."
4082   (interactive)
4083   (setq gnus-newsrc-alist 
4084         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4085   (gnus-make-hashtable-from-newsrc-alist)
4086   (gnus-group-list-groups nil))
4087
4088 (defun gnus-group-sort-by-alphabet (info1 info2)
4089   (string< (car info1) (car info2)))
4090
4091 (defun gnus-group-sort-by-unread (info1 info2)
4092   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4093         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4094     (< (or (and (numberp n1) n1) 0)
4095        (or (and (numberp n2) n2) 0))))
4096
4097 (defun gnus-group-sort-by-level (info1 info2)
4098   (< (nth 1 info1) (nth 1 info2)))
4099
4100 ;; Group catching up.
4101
4102 (defun gnus-group-catchup-current (n &optional all)
4103   "Mark all articles not marked as unread in current newsgroup as read.
4104 If prefix argument N is numeric, the ARG next newsgroups will be
4105 caught up. If ALL is non-nil, marked articles will also be marked as
4106 read. Cross references (Xref: header) of articles are ignored.
4107 The difference between N and actual number of newsgroups that were
4108 caught up is returned."
4109   (interactive "P")
4110   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4111                gnus-expert-user
4112                (gnus-y-or-n-p
4113                 (if all
4114                     "Do you really want to mark all articles as read? "
4115                   "Mark all unread articles as read? "))))
4116       n
4117     (let ((groups (gnus-group-process-prefix n))
4118           (ret 0))
4119       (while groups
4120         (gnus-group-remove-mark (car groups))
4121         (if (not (gnus-group-goto-group (car groups)))
4122             (setq ret (1+ ret))
4123           (gnus-group-catchup (car groups) all)
4124           (gnus-group-update-group-line))
4125         (setq groups (cdr groups)))
4126       (gnus-group-next-unread-group 1)
4127       ret)))
4128
4129 (defun gnus-group-catchup-current-all (n)
4130   "Mark all articles in current newsgroup as read.
4131 Cross references (Xref: header) of articles are ignored."
4132   (interactive "P")
4133   (gnus-group-catchup-current n 'all))
4134
4135 (defun gnus-group-catchup (group &optional all)
4136   "Mark all articles in GROUP as read.
4137 If ALL is non-nil, all articles are marked as read.
4138 The return value is the number of articles that were marked as read,
4139 or nil if no action could be taken."
4140   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4141          (num (car entry))
4142          (marked (nth 3 (nth 2 entry)))
4143          ticked)
4144     (if (not (numberp (car entry)))
4145         (gnus-message 1 "Can't catch up; non-active group")
4146       ;; Do the updating only if the newsgroup isn't killed.
4147       (if entry
4148           (progn
4149             (setq ticked (if all nil (cdr (assq 'tick marked))))
4150             (gnus-update-read-articles group ticked nil ticked)
4151             (if (and all marked)
4152                 (setcar (nthcdr 3 (nth 2 entry)) 
4153                         (delq (assq 'dormant marked) marked))))))
4154     num))
4155
4156 (defun gnus-group-expire-articles (n)
4157   "Expire all expirable articles in the current newsgroup."
4158   (interactive "P")
4159   (let ((groups (gnus-group-process-prefix n))
4160         group)
4161     (or groups (error "No groups to expire"))
4162     (while groups
4163       (setq group (car groups)
4164             groups (cdr groups))
4165       (gnus-group-remove-mark group)
4166       (if (not (gnus-check-backend-function 'request-expire-articles group))
4167           ()
4168         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4169                (expirable (if (memq 'total-expire (nth 5 info))
4170                               (cons nil (gnus-list-of-read-articles group))
4171                             (assq 'expire (nth 3 info)))))
4172           (and expirable 
4173                (setcdr expirable
4174                        (gnus-request-expire-articles 
4175                         (cdr expirable) group))))))))
4176
4177 (defun gnus-group-expire-all-groups ()
4178   "Expire all expirable articles in all newsgroups."
4179   (interactive)
4180   (gnus-message 5 "Expiring...")
4181   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4182                                    (cdr gnus-newsrc-alist))))
4183     (gnus-group-expire-articles nil))
4184   (gnus-message 5 "Expiring...done"))
4185
4186 (defun gnus-group-set-current-level (n level)
4187   "Set the level of the next N groups to LEVEL."
4188   (interactive "P\nnLevel: ")
4189   (or (and (>= level 1) (<= level gnus-level-killed))
4190       (error "Illegal level: %d" level))
4191   (let ((groups (gnus-group-process-prefix n))
4192         group)
4193     (while groups
4194       (setq group (car groups)
4195             groups (cdr groups))
4196       (gnus-group-remove-mark group)
4197       (gnus-message 6 "Changed level of %s from %d to %d" 
4198                     group (gnus-group-group-level) level)
4199       (gnus-group-change-level group level
4200                                (gnus-group-group-level))
4201       (gnus-group-update-group-line)))
4202   (gnus-group-position-cursor))
4203
4204 (defun gnus-group-unsubscribe-current-group (arg)
4205   "Toggle subscribe from/to unsubscribe current group."
4206   (interactive "P")
4207   (let ((group (gnus-group-group-name)))
4208     (or group (error "No newsgroup on current line"))
4209     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
4210                           gnus-level-default-unsubscribed
4211                         gnus-level-default-subscribed)))
4212     (gnus-group-unsubscribe-group group arg)
4213     (gnus-group-next-group 1)))
4214
4215 (defun gnus-group-unsubscribe-group (group &optional level)
4216   "Toggle subscribe from/to unsubscribe GROUP.
4217 New newsgroup is added to .newsrc automatically."
4218   (interactive
4219    (list (completing-read "Group: " gnus-active-hashtb nil 
4220                           gnus-have-read-active-file)))
4221   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4222     (cond (newsrc
4223            ;; Toggle subscription flag.
4224            (gnus-group-change-level 
4225             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4226                                            gnus-level-subscribed) 
4227                                        (1+ gnus-level-subscribed)
4228                                      gnus-level-default-subscribed)))
4229            (gnus-group-update-group group))
4230           ((and (stringp group)
4231                 (or (not gnus-have-read-active-file)
4232                     (gnus-gethash group gnus-active-hashtb)))
4233            ;; Add new newsgroup.
4234            (gnus-group-change-level 
4235             group 
4236             (if level level gnus-level-default-subscribed) 
4237             (or (and (member group gnus-zombie-list) 
4238                      gnus-level-zombie) 
4239                 gnus-level-killed)
4240             (and (gnus-group-group-name)
4241                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4242            (gnus-group-update-group group))
4243           (t (error "No such newsgroup: %s" group)))
4244     (gnus-group-position-cursor)))
4245
4246 (defun gnus-group-transpose-groups (n)
4247   "Move the current newsgroup up N places.
4248 If given a negative prefix, move down instead. The difference between
4249 N and the number of steps taken is retured." 
4250   (interactive "p")
4251   (or (gnus-group-group-name)
4252       (error "No group on current line"))
4253   (gnus-group-kill-group 1)
4254   (prog1
4255       (forward-line (- n))
4256     (gnus-group-yank-group)
4257     (gnus-group-position-cursor)))
4258
4259 (defun gnus-group-kill-all-zombies ()
4260   "Kill all zombie newsgroups."
4261   (interactive)
4262   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4263   (setq gnus-zombie-list nil)
4264   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4265   (goto-char (point-min))
4266   (gnus-group-position-cursor))
4267
4268 (defun gnus-group-kill-region (begin end)
4269   "Kill newsgroups in current region (excluding current point).
4270 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4271   (interactive "r")
4272   (let ((lines
4273          ;; Exclude a line where current point is on.
4274          (1-
4275           ;; Count lines.
4276           (save-excursion
4277             (count-lines
4278              (progn
4279                (goto-char begin)
4280                (beginning-of-line)
4281                (point))
4282              (progn
4283                (goto-char end)
4284                (end-of-line)
4285                (point)))))))
4286     (goto-char begin)
4287     (beginning-of-line)                 ;Important when LINES < 1
4288     (gnus-group-kill-group lines)))
4289
4290 (defun gnus-group-kill-group (n)
4291   "The the next N groups.
4292 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4293 However, only groups that were alive can be yanked; already killed 
4294 groups or zombie groups can't be yanked.
4295 The return value is the name of the (last) group that was killed."
4296   (interactive "P")
4297   (let ((buffer-read-only nil)
4298         (groups (gnus-group-process-prefix n))
4299         group entry level)
4300     (while groups
4301       (setq group (car groups)
4302             groups (cdr groups))
4303       (gnus-group-remove-mark group)
4304       (setq level (gnus-group-group-level))
4305       (gnus-delete-line)
4306       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4307           (setq gnus-list-of-killed-groups 
4308                 (cons (cons (car entry) (nth 2 entry)) 
4309                       gnus-list-of-killed-groups)))
4310       (gnus-group-change-level 
4311        (if entry entry group) gnus-level-killed (if entry nil level)))
4312     (gnus-group-position-cursor)
4313     group))
4314
4315 (defun gnus-group-yank-group (&optional arg)
4316   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4317 inserting it before the current newsgroup.  The numeric ARG specifies
4318 how many newsgroups are to be yanked.  The name of the (last)
4319 newsgroup yanked is returned."
4320   (interactive "p")
4321   (if (not arg) (setq arg 1))
4322   (let (info group prev)
4323     (while (>= (setq arg (1- arg)) 0)
4324       (if (not (setq info (car gnus-list-of-killed-groups)))
4325           (error "No more newsgroups to yank"))
4326       (setq group (nth 2 info))
4327       ;; Find which newsgroup to insert this one before - search
4328       ;; backward until something suitable is found. If there are no
4329       ;; other newsgroups in this buffer, just make this newsgroup the
4330       ;; first newsgroup.
4331       (setq prev (gnus-group-group-name))
4332       (gnus-group-change-level 
4333        info (nth 2 info) gnus-level-killed 
4334        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4335        t)
4336       (gnus-group-insert-group-line-info (nth 1 info))
4337       (setq gnus-list-of-killed-groups 
4338             (cdr gnus-list-of-killed-groups)))
4339     (forward-line -1)
4340     (gnus-group-position-cursor)
4341     group))
4342       
4343 (defun gnus-group-list-all-groups (arg)
4344   "List all newsgroups with level ARG or lower.
4345 Default is gnus-level-unsubscribed, which lists all subscribed and most
4346 unsubscribed groups."
4347   (interactive "P")
4348   (setq arg (or arg gnus-level-unsubscribed))
4349   (gnus-group-list-groups arg t))
4350
4351 (defun gnus-group-list-killed ()
4352   "List all killed newsgroups in the group buffer."
4353   (interactive)
4354   (if (not gnus-killed-list)
4355       (gnus-message 6 "No killed groups")
4356     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4357     (goto-char (point-min)))
4358   (gnus-group-position-cursor))
4359
4360 (defun gnus-group-list-zombies ()
4361   "List all zombie newsgroups in the group buffer."
4362   (interactive)
4363   (if (not gnus-zombie-list)
4364       (gnus-message 6 "No zombie groups")
4365     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4366     (goto-char (point-min)))
4367   (gnus-group-position-cursor))
4368
4369 (defun gnus-group-get-new-news (&optional arg)
4370   "Get newly arrived articles.
4371 If ARG is non-nil, it should be a number between one and nine to
4372 specify which levels you are interested in re-scanning."
4373   (interactive "P")
4374   (run-hooks 'gnus-get-new-news-hook)
4375   (let ((level arg))
4376     (if gnus-group-use-permanent-levels
4377         (if level
4378             (setq gnus-group-default-list-level level)
4379           (setq level (or gnus-group-default-list-level 
4380                           gnus-level-subscribed))))
4381     (if (and gnus-read-active-file (not level))
4382         (progn
4383           (gnus-read-active-file)
4384           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4385       (let ((gnus-read-active-file nil))
4386         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4387     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4388                                 gnus-group-default-list-level
4389                                 gnus-level-subscribed)
4390                             gnus-have-all-newsgroups)))
4391
4392 (defun gnus-group-get-new-news-this-group (n)
4393   "Check for newly arrived news in the current group (and the N-1 next groups).
4394 The difference between N and the number of newsgroup checked is returned.
4395 If N is negative, this group and the N-1 previous groups will be checked."
4396   (interactive "P")
4397   (let* ((groups (gnus-group-process-prefix n))
4398          (ret (if (numberp n) (- n (length groups)) 0))
4399          (w-p (window-start))
4400          group)
4401     (while groups
4402       (setq group (car groups)
4403             groups (cdr groups))
4404       (gnus-group-remove-mark group)
4405       (or (gnus-get-new-news-in-group group)
4406           (progn 
4407             (ding) 
4408             (message "%s error: %s" group (gnus-status-message group))
4409             (sit-for 2))))
4410     ;; !!! I don't know why the buffer scrolls forward when updating
4411     ;; the first line in the group buffer, but it does. So we set the
4412     ;; window start forcibly.
4413 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4414     (forward-line 1)
4415     (gnus-summary-position-cursor)
4416     ret))
4417
4418 (defun gnus-get-new-news-in-group (group)
4419   (and group 
4420        (gnus-activate-newsgroup group)
4421        (progn
4422          (gnus-get-unread-articles-in-group 
4423           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4424           (gnus-gethash group gnus-active-hashtb))
4425          (gnus-group-update-group-line)
4426          t)))
4427
4428 (defun gnus-group-fetch-faq (group)
4429   "Fetch the FAQ for the current group."
4430   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4431   (or group (error "No group name given"))
4432   (let ((file (concat gnus-group-faq-directory group))) 
4433     (if (not (file-exists-p file))
4434         (error "No such file: %s" file)
4435       (find-file file))))
4436   
4437 (defun gnus-group-describe-group (force &optional group)
4438   "Display a description of the current newsgroup."
4439   (interactive "P")
4440   (and force (setq gnus-description-hashtb nil))
4441   (let ((group (or group (gnus-group-group-name)))
4442         desc)
4443     (or group (error "No group name given"))
4444     (and (or gnus-description-hashtb
4445              (setq desc (gnus-group-get-description group))
4446              (gnus-read-descriptions-file))
4447          (message
4448           (or desc (gnus-gethash group gnus-description-hashtb)
4449               "No description available")))))
4450
4451 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4452 (defun gnus-group-describe-all-groups (force)
4453   "Pop up a buffer with descriptons of all newsgroups."
4454   (interactive "P")
4455   (and force (setq gnus-description-hashtb nil))
4456   (if (not (or gnus-description-hashtb
4457                (gnus-read-descriptions-file)))
4458       (error "Couldn't request descriptions file"))
4459   (let ((buffer-read-only nil)
4460         b)
4461     (erase-buffer)
4462     (mapatoms
4463      (lambda (group)
4464        (setq b (point))
4465        (insert (format "      *: %-20s %s\n" (symbol-name group)
4466                        (symbol-value group)))
4467        (add-text-properties 
4468         b (1+ b) (list 'gnus-group group
4469                        'gnus-unread t 'gnus-marked nil
4470                        'gnus-level (1+ gnus-level-subscribed))))
4471      gnus-description-hashtb)
4472     (goto-char (point-min))
4473     (gnus-group-position-cursor)))
4474
4475 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4476 (defun gnus-group-apropos (regexp &optional search-description)
4477   "List all newsgroups that have names that match a regexp."
4478   (interactive "sGnus apropos (regexp): ")
4479   (let ((prev "")
4480         (obuf (current-buffer))
4481         groups des prev)
4482     ;; Go through all newsgroups that are known to Gnus.
4483     (mapatoms 
4484      (lambda (group)
4485        (and (string-match regexp (symbol-name group))
4486             (setq groups (cons (symbol-name group) groups))))
4487      gnus-active-hashtb)
4488     ;; Go through all descriptions that are known to Gnus. 
4489     (if search-description
4490         (mapatoms 
4491          (lambda (group)
4492            (and (string-match regexp (symbol-value group))
4493                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4494                 (setq groups (cons (symbol-name group) groups))))
4495          gnus-description-hashtb))
4496     (if (not groups)
4497         (gnus-message 3 "No groups matched \"%s\"." regexp)
4498       ;; Print out all the groups.
4499       (save-excursion
4500         (pop-to-buffer "*Gnus Help*")
4501         (buffer-disable-undo (current-buffer))
4502         (erase-buffer)
4503         (setq groups (sort groups 'string<))
4504         (while groups
4505           ;; Groups may be entered twice into the list of groups.
4506           (if (not (string= (car groups) prev))
4507               (progn
4508                 (insert (setq prev (car groups)) "\n")
4509                 (if (and gnus-description-hashtb
4510                          (setq des (gnus-gethash (car groups) 
4511                                                  gnus-description-hashtb)))
4512                     (insert "  " des "\n"))))
4513           (setq groups (cdr groups)))
4514         (goto-char (point-min))))
4515     (pop-to-buffer obuf)))
4516
4517 (defun gnus-group-description-apropos (regexp)
4518   "List all newsgroups that have names or desccriptions that match a regexp."
4519   (interactive "sGnus description apropos (regexp): ")
4520   (if (not (or gnus-description-hashtb
4521                (gnus-read-descriptions-file)))
4522       (error "Couldn't request descriptions file"))
4523   (gnus-group-apropos regexp t))
4524
4525 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4526 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4527   "List all groups with unread articles that match REGEXP.
4528 If the prefix LEVEL is non-nil, it should be a number that says which
4529 level to cut off listing groups. 
4530 If ALL, also list groups with no unread articles.
4531 If LOWEST, don't list groups with level lower than LOWEST."
4532   (interactive "P\nsList newsgroups matching: ")
4533   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4534                            all (or lowest 1) regexp)
4535   (goto-char (point-min))
4536   (gnus-group-position-cursor))
4537
4538 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4539   "List all groups that match REGEXP.
4540 If the prefix LEVEL is non-nil, it should be a number that says which
4541 level to cut off listing groups. 
4542 If LOWEST, don't list groups with level lower than LOWEST."
4543   (interactive "P\nsList newsgroups matching: ")
4544   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4545
4546 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4547 (defun gnus-group-save-newsrc ()
4548   "Save the Gnus startup files."
4549   (interactive)
4550   (gnus-save-newsrc-file))
4551
4552 (defun gnus-group-restart (&optional arg)
4553   "Force Gnus to read the .newsrc file."
4554   (interactive "P")
4555   (gnus-save-newsrc-file)
4556   (gnus-setup-news 'force)
4557   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4558
4559 (defun gnus-group-read-init-file ()
4560   "Read the Gnus elisp init file."
4561   (interactive)
4562   (gnus-read-init-file))
4563
4564 (defun gnus-group-check-bogus-groups ()
4565   "Check bogus newsgroups."
4566   (interactive)
4567   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
4568   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4569
4570 (defun gnus-group-edit-global-kill (article &optional group)
4571   "Edit the global kill file.
4572 If GROUP, edit that local kill file instead."
4573   (interactive "P")
4574   (setq gnus-current-kill-article article)
4575   (gnus-kill-file-edit-file group)
4576   (gnus-message 6
4577    (substitute-command-keys
4578     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4579
4580 (defun gnus-group-edit-local-kill (article group)
4581   "Edit a local kill file."
4582   (interactive (list nil (gnus-group-group-name)))
4583   (gnus-group-edit-global-kill article group))
4584
4585 (defun gnus-group-force-update ()
4586   "Update `.newsrc' file."
4587   (interactive)
4588   (gnus-save-newsrc-file))
4589
4590 (defun gnus-group-suspend ()
4591   "Suspend the current Gnus session.
4592 In fact, cleanup buffers except for group mode buffer.
4593 The hook gnus-suspend-gnus-hook is called before actually suspending."
4594   (interactive)
4595   (run-hooks 'gnus-suspend-gnus-hook)
4596   ;; Kill Gnus buffers except for group mode buffer.
4597   (let ((group-buf (get-buffer gnus-group-buffer)))
4598     ;; Do this on a separate list in case the user does a ^G before we finish
4599     (let ((gnus-buffer-list
4600            (delq group-buf (delq gnus-dribble-buffer
4601                                  (append gnus-buffer-list nil)))))
4602       (while gnus-buffer-list
4603         (gnus-kill-buffer (car gnus-buffer-list))
4604         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4605     (if group-buf
4606         (progn
4607           (setq gnus-buffer-list (list group-buf))
4608           (bury-buffer group-buf)
4609           (delete-windows-on group-buf t)))))
4610
4611 (defun gnus-group-clear-dribble ()
4612   "Clear all information from the dribble buffer."
4613   (interactive)
4614   (gnus-dribble-clear))
4615
4616 (defun gnus-group-exit ()
4617   "Quit reading news after updating .newsrc.eld and .newsrc.
4618 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4619   (interactive)
4620   (if (or noninteractive                ;For gnus-batch-kill
4621           (zerop (buffer-size))         ;No news is good news.
4622           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4623           (not gnus-interactive-exit)   ;Without confirmation
4624           gnus-expert-user
4625           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4626       (progn
4627         (run-hooks 'gnus-exit-gnus-hook)
4628         (gnus-save-newsrc-file)
4629         (gnus-offer-save-summaries)
4630         (gnus-close-backends)
4631         (gnus-clear-system))))
4632
4633 (defun gnus-close-backends ()
4634   ;; Send a close request to all backends that support such a request. 
4635   (let ((methods gnus-valid-select-methods)
4636         func)
4637     (while methods
4638       (if (fboundp (setq func (intern (concat (car (car methods))
4639                                               "-request-close"))))
4640           (funcall func))
4641       (setq methods (cdr methods)))))
4642
4643 (defun gnus-group-quit ()
4644   "Quit reading news without updating .newsrc.eld or .newsrc.
4645 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4646   (interactive)
4647   (if (or noninteractive                ;For gnus-batch-kill
4648           (zerop (buffer-size))
4649           (not (gnus-server-opened gnus-select-method))
4650           gnus-expert-user
4651           (not gnus-current-startup-file)
4652           (gnus-yes-or-no-p
4653            (format "Quit reading news without saving %s? "
4654                    (file-name-nondirectory gnus-current-startup-file))))
4655       (progn
4656         (run-hooks 'gnus-exit-gnus-hook)
4657         (gnus-dribble-save)
4658         (gnus-close-backends)
4659         (gnus-clear-system))))
4660
4661 (defun gnus-offer-save-summaries ()
4662   (let ((buffers (buffer-list)))
4663     (save-excursion
4664       (while buffers
4665         (and 
4666          ;; We look for buffers with "Summary" in the name.
4667          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4668          (progn
4669            (set-buffer (car buffers))
4670            ;; We check that this is, indeed, a summary buffer.
4671            (eq major-mode 'gnus-summary-mode)) 
4672          ;; We ask the user whether she wants to save the info.
4673          (gnus-y-or-n-p
4674                (format "Update summary buffer %s? " (buffer-name)))
4675          ;; We do it by simply exiting.
4676          (gnus-summary-exit))
4677         (setq buffers (cdr buffers))))))
4678
4679 (defun gnus-group-describe-briefly ()
4680   "Give a one line description of the group mode commands."
4681   (interactive)
4682   (gnus-message 6
4683    (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")))
4684
4685 (defun gnus-group-browse-foreign-server (method)
4686   "Browse a foreign news server.
4687 If called interactively, this function will ask for a select method
4688  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4689 If not, METHOD should be a list where the first element is the method
4690 and the second element is the address."
4691   (interactive
4692    (list (let ((how (completing-read 
4693                      "Which backend: "
4694                      (append gnus-valid-select-methods gnus-server-alist)
4695                      nil t "nntp")))
4696            ;; We either got a backend name or a virtual server name.
4697            ;; If the first, we also need an address.
4698            (if (assoc how gnus-valid-select-methods)
4699                (list (intern how)
4700                      ;; Suggested by mapjph@bath.ac.uk.
4701                      (completing-read 
4702                       "Address: " 
4703                       (mapcar (lambda (server) (list server))
4704                               gnus-secondary-servers)))
4705              ;; We got a server name, so we find the method.
4706              (gnus-server-to-method how)))))
4707   (gnus-browse-foreign-server method))
4708
4709 \f
4710 ;;;
4711 ;;; Browse Server Mode
4712 ;;;
4713
4714 (defvar gnus-browse-mode-hook nil)
4715 (defvar gnus-browse-mode-map nil)
4716 (put 'gnus-browse-mode 'mode-class 'special)
4717
4718 (if gnus-browse-mode-map
4719     nil
4720   (setq gnus-browse-mode-map (make-keymap))
4721   (suppress-keymap gnus-browse-mode-map)
4722   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4723   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4724   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4725   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4726   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4727   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4728   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4729   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4730   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4731   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4732   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4733   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4734   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4735   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4736   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4737   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4738   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4739   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4740   )
4741
4742 (defvar gnus-browse-current-method nil)
4743 (defvar gnus-browse-return-buffer nil)
4744
4745 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4746
4747 (defun gnus-browse-foreign-server (method &optional return-buffer)
4748   (setq gnus-browse-current-method method)
4749   (setq gnus-browse-return-buffer return-buffer)
4750   (let ((gnus-select-method method)
4751         groups group)
4752     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4753     (or (gnus-server-opened method)
4754         (gnus-open-server method)
4755         (error "Unable to contact server: %s" (gnus-status-message method)))
4756     (or (gnus-request-list method)
4757         (error "Couldn't request list: %s" (gnus-status-message method)))
4758     (get-buffer-create gnus-browse-buffer)
4759     (gnus-add-current-to-buffer-list)
4760     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4761     (gnus-configure-windows 'browse)
4762     (buffer-disable-undo (current-buffer))
4763     (let ((buffer-read-only nil))
4764       (erase-buffer))
4765     (gnus-browse-mode)
4766     (setq mode-line-buffer-identification
4767           (format
4768            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4769     (save-excursion
4770       (set-buffer nntp-server-buffer)
4771       (let ((cur (current-buffer)))
4772         (goto-char (point-min))
4773         (or (string= gnus-ignored-newsgroups "")
4774             (delete-matching-lines gnus-ignored-newsgroups))
4775         (while (re-search-forward 
4776                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4777           (goto-char (match-end 1))
4778           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4779                                                      (match-end 1))
4780                                    (max 0 (- (1+ (read cur)) (read cur))))
4781                              groups)))))
4782     (setq groups (sort groups 
4783                        (lambda (l1 l2)
4784                          (string< (car l1) (car l2)))))
4785     (let ((buffer-read-only nil))
4786       (while groups
4787         (setq group (car groups))
4788         (insert 
4789          (format "K%7d: %s\n" (cdr group) (car group)))
4790         (setq groups (cdr groups))))
4791     (switch-to-buffer (current-buffer))
4792     (goto-char (point-min))
4793     (gnus-group-position-cursor)))
4794
4795 (defun gnus-browse-mode ()
4796   "Major mode for browsing a foreign server.
4797
4798 All normal editing commands are switched off.
4799
4800 \\<gnus-browse-mode-map>
4801 The only things you can do in this buffer is
4802
4803 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4804 The group will be inserted into the group buffer upon exit from this
4805 buffer.  
4806
4807 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4808
4809 3) `\\[gnus-browse-exit]' to return to the group buffer."
4810   (interactive)
4811   (kill-all-local-variables)
4812   (if gnus-visual (gnus-browse-make-menu-bar))
4813   (setq mode-line-modified "-- ")
4814   (make-local-variable 'mode-line-format)
4815   (setq mode-line-format (copy-sequence mode-line-format))
4816   (and (equal (nth 3 mode-line-format) "   ")
4817        (setcar (nthcdr 3 mode-line-format) ""))
4818   (setq major-mode 'gnus-browse-mode)
4819   (setq mode-name "Browse Server")
4820   (setq mode-line-process nil)
4821   (use-local-map gnus-browse-mode-map)
4822   (buffer-disable-undo (current-buffer))
4823   (setq truncate-lines t)
4824   (setq buffer-read-only t)
4825   (run-hooks 'gnus-browse-mode-hook))
4826
4827 (defun gnus-browse-read-group (&optional no-article)
4828   "Enter the group at the current line."
4829   (interactive)
4830   (let ((group (gnus-browse-group-name)))
4831     (or (gnus-group-read-ephemeral-group 
4832          group gnus-browse-current-method nil
4833          (cons (current-buffer) 'browse))
4834         (error "Couldn't enter %s" group))))
4835
4836 (defun gnus-browse-select-group ()
4837   "Select the current group."
4838   (interactive)
4839   (gnus-browse-read-group 'no))
4840
4841 (defun gnus-browse-next-group (n)
4842   "Go to the next group."
4843   (interactive "p")
4844   (prog1
4845       (forward-line n)
4846     (gnus-group-position-cursor)))
4847
4848 (defun gnus-browse-prev-group (n)
4849   "Go to the next group."
4850   (interactive "p")
4851   (gnus-browse-next-group (- n)))
4852
4853 (defun gnus-browse-unsubscribe-current-group (arg)
4854   "(Un)subscribe to the next ARG groups."
4855   (interactive "p")
4856   (and (eobp)
4857        (error "No group at current line."))
4858   (let ((ward (if (< arg 0) -1 1))
4859         (arg (abs arg)))
4860     (while (and (> arg 0)
4861                 (not (eobp))
4862                 (gnus-browse-unsubscribe-group)
4863                 (zerop (gnus-browse-next-group ward)))
4864       (setq arg (1- arg)))
4865     (gnus-group-position-cursor)
4866     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4867     arg))
4868
4869 (defun gnus-browse-group-name ()
4870   (save-excursion
4871     (beginning-of-line)
4872     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4873         ()
4874       (gnus-group-prefixed-name 
4875        (buffer-substring (match-beginning 1) (match-end 1))
4876        gnus-browse-current-method))))
4877   
4878 (defun gnus-browse-unsubscribe-group ()
4879   (let ((sub nil)
4880         (buffer-read-only nil)
4881         group)
4882     (save-excursion
4883       (beginning-of-line)
4884       (if (= (following-char) ?K) (setq sub t))
4885       (setq group (gnus-browse-group-name))
4886       (beginning-of-line)
4887       (delete-char 1)
4888       (if sub
4889           (progn
4890             (gnus-group-change-level 
4891              (list t group gnus-level-default-subscribed
4892                    nil nil gnus-browse-current-method) 
4893              gnus-level-default-subscribed gnus-level-killed
4894              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4895              t)
4896             (insert ? ))
4897         (gnus-group-change-level 
4898          group gnus-level-killed gnus-level-default-subscribed)
4899         (insert ?K)))
4900     t))
4901
4902 (defun gnus-browse-exit ()
4903   "Quit browsing and return to the group buffer."
4904   (interactive)
4905   (if (eq major-mode 'gnus-browse-mode)
4906       (kill-buffer (current-buffer)))
4907   (if gnus-browse-return-buffer
4908       (gnus-configure-windows 'server)
4909     (gnus-configure-windows 'group)))
4910
4911 (defun gnus-browse-describe-briefly ()
4912   "Give a one line description of the group mode commands."
4913   (interactive)
4914   (gnus-message 6
4915    (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")))
4916       
4917 \f
4918 ;;;
4919 ;;; Gnus summary mode
4920 ;;;
4921
4922 (defvar gnus-summary-mode-map nil)
4923 (defvar gnus-summary-mark-map nil)
4924 (defvar gnus-summary-mscore-map nil)
4925 (defvar gnus-summary-article-map nil)
4926 (defvar gnus-summary-thread-map nil)
4927 (defvar gnus-summary-goto-map nil)
4928 (defvar gnus-summary-exit-map nil)
4929 (defvar gnus-summary-various-map nil)
4930 (defvar gnus-summary-interest-map nil)
4931 (defvar gnus-summary-sort-map nil)
4932 (defvar gnus-summary-backend-map nil)
4933 (defvar gnus-summary-save-map nil)
4934 (defvar gnus-summary-wash-map nil)
4935 (defvar gnus-summary-help-map nil)
4936
4937 (put 'gnus-summary-mode 'mode-class 'special)
4938
4939 (if gnus-summary-mode-map
4940     nil
4941   (setq gnus-summary-mode-map (make-keymap))
4942   (suppress-keymap gnus-summary-mode-map)
4943
4944   ;; Non-orthogonal keys
4945
4946   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4947   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4948   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4949   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4950   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4951   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4952   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4953   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4954   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4955   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
4956   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
4957   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
4958   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
4959   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
4960   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
4961   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
4962   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
4963   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
4964   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
4965   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
4966   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
4967   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
4968   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
4969   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
4970   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
4971   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
4972   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
4973   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
4974   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
4975   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
4976   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
4977   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
4978   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
4979   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
4980   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
4981   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
4982   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
4983   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
4984   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
4985   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
4986   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
4987   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
4988   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
4989   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
4990   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
4991   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
4992   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
4993   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
4994   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
4995   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
4996   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
4997   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
4998   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
4999   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5000   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5001   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5002   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5003   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5004   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5005   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5006   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5007   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5008   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5009   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5010   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5011   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5012   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5013   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5014   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5015   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5016   (define-key gnus-summary-mode-map "V" 'gnus-version)
5017   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5018   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5019   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5020   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5021   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5022   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5023   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5024   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5025 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5026   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5027   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5028   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5029 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5030   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5031   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5032   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5033
5034
5035   ;; Sort of orthogonal keymap
5036   (define-prefix-command 'gnus-summary-mark-map)
5037   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5038   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5039   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5040   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5041   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5042   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5043   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5044   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5045   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5046   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5047   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5048   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5049   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5050   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5051   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5052   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5053   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5054   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5055   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5056   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5057   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5058   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5059   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5060   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5061
5062   (define-prefix-command 'gnus-summary-mscore-map)
5063   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5064   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5065   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5066   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5067   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5068
5069   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5070   
5071   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5072   
5073   (define-prefix-command 'gnus-summary-goto-map)
5074   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5075   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5076   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5077   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5078   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5079   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5080   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5081   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5082   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5083   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5084   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5085   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5086   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5087   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5088
5089
5090   (define-prefix-command 'gnus-summary-thread-map)
5091   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5092   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5093   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5094   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5095   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5096   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5097   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5098   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5099   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5100   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5101   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5102   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5103   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5104   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5105
5106   
5107   (define-prefix-command 'gnus-summary-exit-map)
5108   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5109   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5110   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5111   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5112   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5113   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5114   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5115   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5116   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5117   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5118   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5119
5120
5121   (define-prefix-command 'gnus-summary-article-map)
5122   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5123   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5124   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5125   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5126   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5127   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5128   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5129   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5130   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5131   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5132   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5133   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5134   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5135   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5136   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5137   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5138   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5139   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5140
5141
5142   (define-prefix-command 'gnus-summary-wash-map)
5143   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5144   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5145   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5146   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5147   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5148   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5149   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5150   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5151   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5152   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5153   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5154   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5155
5156   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5157   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5158   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5159   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5160   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5161   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5162
5163
5164   (define-prefix-command 'gnus-summary-help-map)
5165   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5166   (define-key gnus-summary-help-map "v" 'gnus-version)
5167   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5168   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5169   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5170   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5171
5172
5173   (define-prefix-command 'gnus-summary-backend-map)
5174   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5175   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5176   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5177   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5178   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5179   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5180   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5181   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5182   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5183
5184
5185   (define-prefix-command 'gnus-summary-save-map)
5186   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5187   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5188   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5189   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5190   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5191   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5192   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5193   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5194 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5195
5196   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5197   
5198   (define-prefix-command 'gnus-summary-various-map)
5199   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5200   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5201   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5202   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5203   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5204   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5205   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5206   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5207   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5208   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5209   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5210
5211   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5212
5213   (define-prefix-command 'gnus-summary-sort-map)
5214   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5215   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5216   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5217   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5218   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5219   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5220
5221   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5222   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5223   )
5224
5225
5226 \f
5227
5228 (defun gnus-summary-mode ()
5229   "Major mode for reading articles.
5230
5231 All normal editing commands are switched off.
5232 \\<gnus-summary-mode-map>
5233 Each line in this buffer represents one article.  To read an
5234 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5235 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5236 respectively.
5237
5238 You can also post articles and send mail from this buffer.  To 
5239 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5240 of an article, type `\\[gnus-summary-reply]'.
5241
5242 There are approx. one gazillion commands you can execute in this 
5243 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5244
5245 The following commands are available:
5246
5247 \\{gnus-summary-mode-map}"
5248   (interactive)
5249   (if gnus-visual (gnus-summary-make-menu-bar))
5250   (kill-all-local-variables)
5251   (let ((locals gnus-summary-local-variables))
5252     (while locals
5253       (if (consp (car locals))
5254           (progn
5255             (make-local-variable (car (car locals)))
5256             (set (car (car locals)) (eval (cdr (car locals)))))
5257         (make-local-variable (car locals))
5258         (set (car locals) nil))
5259       (setq locals (cdr locals))))
5260   (gnus-make-thread-indent-array)
5261   (gnus-update-format-specifications)
5262   (setq mode-line-modified "-- ")
5263   (make-local-variable 'mode-line-format)
5264   (setq mode-line-format (copy-sequence mode-line-format))
5265   (and (equal (nth 3 mode-line-format) "   ")
5266        (setcar (nthcdr 3 mode-line-format) ""))
5267   (setq major-mode 'gnus-summary-mode)
5268   (setq mode-name "Summary")
5269   (make-local-variable 'minor-mode-alist)
5270   (use-local-map gnus-summary-mode-map)
5271   (buffer-disable-undo (current-buffer))
5272   (setq buffer-read-only t)             ;Disable modification
5273   (setq truncate-lines t)
5274   (setq selective-display t)
5275   (setq selective-display-ellipses t)   ;Display `...'
5276   (setq buffer-display-table gnus-summary-display-table)
5277   (run-hooks 'gnus-summary-mode-hook))
5278
5279 (defun gnus-summary-make-display-table ()
5280   ;; Change the display table.  Odd characters have a tendency to mess
5281   ;; up nicely formatted displays - we make all possible glyphs
5282   ;; display only a single character.
5283
5284   ;; We start from the standard display table, if any.
5285   (setq gnus-summary-display-table 
5286         (or (copy-sequence standard-display-table)
5287             (make-display-table)))
5288   ;; Nix out all the control chars...
5289   (let ((i 32))
5290     (while (>= (setq i (1- i)) 0)
5291       (aset gnus-summary-display-table i [??])))
5292   ;; ... but not newline, of course.
5293   (aset gnus-summary-display-table ?\n nil)
5294   ;; We nix out any glyphs over 126 that are not set already.  
5295   (let ((i 256))
5296     (while (>= (setq i (1- i)) 127)
5297       ;; Only modify if the entry is nil.
5298       (or (aref gnus-summary-display-table i) 
5299           (aset gnus-summary-display-table i [??])))))
5300
5301 (defun gnus-summary-clear-local-variables ()
5302   (let ((locals gnus-summary-local-variables))
5303     (while locals
5304       (if (consp (car locals))
5305           (and (vectorp (car (car locals)))
5306                (set (car (car locals)) nil))
5307         (and (vectorp (car locals))
5308              (set (car locals) nil)))
5309       (setq locals (cdr locals)))))
5310
5311 (defun gnus-mouse-pick-article (e)
5312   (interactive "e")
5313   (mouse-set-point e)
5314   (gnus-summary-next-page nil t))
5315
5316 (defun gnus-summary-setup-buffer (group)
5317   "Initialize summary buffer."
5318   (let ((buffer (concat "*Summary " group "*")))
5319     (if (get-buffer buffer)
5320         (progn
5321           (set-buffer buffer)
5322           (not gnus-newsgroup-threads))
5323       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5324       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5325       (gnus-add-current-to-buffer-list)
5326       (gnus-summary-mode)
5327       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5328       (setq gnus-newsgroup-name group)
5329       t)))
5330
5331 (defun gnus-set-global-variables ()
5332   ;; Set the global equivalents of the summary buffer-local variables
5333   ;; to the latest values they had. These reflect the summary buffer
5334   ;; that was in action when the last article was fetched.
5335   (if (eq major-mode 'gnus-summary-mode) 
5336       (progn
5337         (setq gnus-summary-buffer (current-buffer))
5338         (let ((name gnus-newsgroup-name)
5339               (marked gnus-newsgroup-marked)
5340               (unread gnus-newsgroup-unreads)
5341               (headers gnus-current-headers)
5342               (score-file gnus-current-score-file))
5343           (save-excursion
5344             (set-buffer gnus-group-buffer)
5345             (setq gnus-newsgroup-name name)
5346             (setq gnus-newsgroup-marked marked)
5347             (setq gnus-newsgroup-unreads unread)
5348             (setq gnus-current-headers headers)
5349             (setq gnus-current-score-file score-file))))))
5350
5351 (defun gnus-summary-insert-dummy-line (sformat subject number)
5352   (if (not sformat) 
5353       (setq sformat gnus-summary-dummy-line-format-spec))
5354   (let (b)
5355     (beginning-of-line)
5356     (setq b (point))
5357     (insert (eval sformat))
5358     (add-text-properties
5359      b (1+ b)
5360      (list 'gnus-number number 
5361            'gnus-mark gnus-dummy-mark
5362            'gnus-level 0))))
5363
5364 (defvar gnus-thread-indent-array nil)
5365 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5366 (defun gnus-make-thread-indent-array ()
5367   (let ((n 200))
5368     (if (and gnus-thread-indent-array
5369              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5370         nil
5371       (setq gnus-thread-indent-array (make-vector 201 "")
5372             gnus-thread-indent-array-level gnus-thread-indent-level)
5373       (while (>= n 0)
5374         (aset gnus-thread-indent-array n
5375               (make-string (* n gnus-thread-indent-level) ? ))
5376         (setq n (1- n))))))
5377
5378 (defun gnus-summary-insert-line 
5379   (sformat header level current unread replied expirable subject-or-nil
5380            &optional dummy score)
5381   (or sformat (setq sformat gnus-summary-line-format-spec))
5382   (let* ((indentation (aref gnus-thread-indent-array level))
5383          (lines (header-lines header))
5384          (score (or score gnus-summary-default-score 0))
5385          (score-char (if (or (null gnus-summary-default-score)
5386                              (= score gnus-summary-default-score)) ? 
5387                        (if (< score gnus-summary-default-score) 
5388                            gnus-score-below-mark gnus-score-over-mark)))
5389          (replied (if replied gnus-replied-mark ? ))
5390          (from (header-from header))
5391          (name-address (funcall gnus-extract-address-components from))
5392          (address (car (cdr name-address)))
5393          (name (or (car name-address) (car (cdr name-address))))
5394          (subject (header-subject header))
5395          (number (header-number header))
5396          (opening-bracket (if dummy ?\< ?\[))
5397          (closing-bracket (if dummy ?\> ?\]))
5398          (buffer-read-only nil)
5399          (b (progn (beginning-of-line) (point))))
5400     (or (numberp lines) (setq lines 0))
5401     (insert (eval sformat))
5402     (add-text-properties
5403      b (1+ b) (list 'gnus-number number 
5404                     'gnus-mark (or unread gnus-unread-mark)
5405                     'gnus-level level))))
5406
5407 (defun gnus-summary-update-line (&optional dont-update)
5408   ;; Update summary line after change.
5409   (or (not gnus-summary-default-score)
5410       gnus-summary-inhibit-highlight
5411       (let ((gnus-summary-inhibit-highlight t)
5412             (article (gnus-summary-article-number)))
5413         (progn
5414           (or dont-update
5415               (if (and gnus-summary-mark-below
5416                        (< (gnus-summary-article-score)
5417                           gnus-summary-mark-below))
5418                   (and (not (memq article gnus-newsgroup-marked))
5419                        (not (memq article gnus-newsgroup-dormant))
5420                        (memq article gnus-newsgroup-unreads)
5421                        (gnus-summary-mark-article nil gnus-low-score-mark))
5422                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5423                      (gnus-summary-mark-article nil gnus-unread-mark))))
5424           (and gnus-visual
5425                (run-hooks 'gnus-summary-update-hook))))))
5426
5427 (defun gnus-summary-update-lines (&optional beg end)
5428   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5429   (let ((beg (or beg (point-min)))
5430         (end (or end (point-max))))
5431     (save-excursion
5432       (set-buffer gnus-summary-buffer)
5433       (goto-char beg)
5434       (while (and (not (eobp)) (< (point) end))
5435         (gnus-summary-update-line)
5436         (forward-line 1)))))
5437
5438 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5439   ;; Sum up all elements (and sub-elements) in a list.
5440   (let ((number 
5441          (if (listp thread) 
5442              (apply 
5443               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5444            1)))
5445     (if char 
5446         (if (> number 1) gnus-not-empty-thread-mark
5447           gnus-empty-thread-mark)
5448       number)))
5449
5450 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5451   "Start reading news in newsgroup GROUP.
5452 If SHOW-ALL is non-nil, already read articles are also listed.
5453 If NO-ARTICLE is non-nil, no article is selected initially."
5454   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5455   (let* ((new-group (gnus-summary-setup-buffer group))
5456          (did-select (and new-group (gnus-select-newsgroup group show-all)))
5457          (method (car (gnus-find-method-for-group group))))
5458     (cond 
5459      ((not new-group)
5460       (gnus-set-global-variables)
5461       (gnus-kill-buffer kill-buffer)
5462       (gnus-configure-windows 'summary)
5463       (gnus-set-mode-line 'summary)
5464       (gnus-summary-position-cursor)
5465       (message "")
5466       t)
5467      ((null did-select) 
5468       (and (eq major-mode 'gnus-summary-mode)
5469            (not (equal (current-buffer) kill-buffer))
5470            (progn
5471              (kill-buffer (current-buffer))
5472              (set-buffer gnus-group-buffer)
5473              (gnus-group-next-unread-group 1)))
5474       (message "Can't select group")
5475       nil)
5476      ((eq did-select 'quit)
5477       (and (eq major-mode 'gnus-summary-mode)
5478            (not (equal (current-buffer) kill-buffer))
5479            (kill-buffer (current-buffer)))
5480       (gnus-kill-buffer kill-buffer)
5481       (gnus-configure-windows 'group)
5482       (gnus-group-next-unread-group 1)
5483       (signal 'quit nil))
5484      (t
5485       (gnus-set-global-variables)
5486       ;; Save the active value in effect when the group was entered.
5487       (setq gnus-newsgroup-active 
5488             (gnus-copy-sequence
5489              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5490       ;; You can change the subjects in this hook.
5491       (run-hooks 'gnus-select-group-hook)
5492       ;; Do score processing.
5493       (and gnus-use-scoring (gnus-possibly-score-headers))
5494       ;; Update the format specifiers.
5495       (gnus-update-format-specifications)
5496       ;; Generate the summary buffer.
5497       (gnus-summary-prepare)
5498       (if (zerop (buffer-size))
5499           (cond (gnus-newsgroup-dormant
5500                  (gnus-summary-show-all-dormant))
5501                 ((and gnus-newsgroup-scored show-all)
5502                  (gnus-summary-show-all-expunged))))
5503       ;; Function `gnus-apply-kill-file' must be called in this hook.
5504       (run-hooks 'gnus-apply-kill-hook)
5505       (if (zerop (buffer-size))
5506           (progn
5507             ;; This newsgroup is empty.
5508             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5509             (gnus-message 6 "No unread news")
5510             (gnus-kill-buffer kill-buffer)
5511             nil)
5512         ;;(save-excursion
5513         ;;  (if kill-buffer
5514         ;;      (let ((gnus-summary-buffer kill-buffer))
5515         ;;      (gnus-configure-windows 'group))))
5516         ;; Hide conversation thread subtrees.  We cannot do this in
5517         ;; gnus-summary-prepare-hook since kill processing may not
5518         ;; work with hidden articles.
5519         (and gnus-show-threads
5520              gnus-thread-hide-subtree
5521              (gnus-summary-hide-all-threads))
5522         ;; Show first unread article if requested.
5523         (goto-char (point-min))
5524         (if (and (not no-article)
5525                  gnus-auto-select-first
5526                  (gnus-summary-first-unread-article))
5527             ()
5528           (gnus-configure-windows 'summary))
5529         (gnus-set-mode-line 'summary)
5530         (gnus-summary-position-cursor)
5531         ;; If in async mode, we send some info to the backend.
5532         (and gnus-newsgroup-async
5533              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5534              (gnus-request-asynchronous 
5535               gnus-newsgroup-name
5536               (if (and gnus-asynchronous-article-function
5537                        (fboundp gnus-asynchronous-article-function))
5538                   (funcall gnus-asynchronous-article-function
5539                            gnus-newsgroup-threads)
5540                 gnus-newsgroup-threads)))
5541         (gnus-kill-buffer kill-buffer)
5542         (if (not (get-buffer-window gnus-group-buffer))
5543             ()
5544           ;; gotta use windows, because recenter does wierd stuff if
5545           ;; the current buffer ain't the displayed window.
5546           (let ((owin (selected-window))) 
5547             (select-window (get-buffer-window gnus-group-buffer))
5548             (and (gnus-group-goto-group group)
5549                  (recenter))
5550             (select-window owin))))
5551       t))))
5552
5553 (defun gnus-summary-prepare ()
5554   ;; Generate the summary buffer.
5555   (let ((buffer-read-only nil))
5556     (erase-buffer)
5557     (gnus-summary-prepare-threads 
5558      (if gnus-show-threads
5559          (gnus-gather-threads 
5560           (gnus-sort-threads 
5561            (if (and gnus-summary-expunge-below
5562                     (not gnus-fetch-old-headers))
5563                (gnus-make-threads-and-expunge)
5564              (gnus-make-threads))))
5565        gnus-newsgroup-headers)
5566      0 nil nil t)
5567     ;; Erase header retrieval message.
5568     (gnus-summary-update-lines)
5569     (message "")
5570     ;; Remove the final newline.
5571     ;;(goto-char (point-max))
5572     ;;(delete-char -1)
5573     ;; Call hooks for modifying summary buffer.
5574     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5575     (goto-char (point-min))
5576     (run-hooks 'gnus-summary-prepare-hook)))
5577
5578 (defun gnus-subject-equal (s1 s2)
5579   (cond 
5580    ((numberp gnus-summary-gather-subject-limit)
5581     (string= (if (> (length s1) gnus-summary-gather-subject-limit)
5582                  (substring s1 0 gnus-summary-gather-subject-limit)
5583                s1)
5584              (if (> (length s2) gnus-summary-gather-subject-limit)
5585                  (substring s2 0 gnus-summary-gather-subject-limit)
5586                s2)))
5587    ((eq 'fuzzy gnus-summary-gather-subject-limit)
5588     (string= (gnus-simplify-subject-fuzzy s1)
5589              (gnus-simplify-subject-fuzzy s2)))
5590    (t
5591     (string= s1 s2))))
5592
5593 (defun gnus-gather-threads (threads)
5594   "Gather threads that have lost their roots."
5595   (if (not gnus-summary-make-false-root)
5596       threads 
5597     (let ((hashtb (gnus-make-hashtable 1023))
5598           (prev threads)
5599           (result threads)
5600           thread subject hthread whole-subject)
5601       (while threads
5602         (setq whole-subject 
5603               (setq subject (header-subject (car (car threads)))))
5604         (if gnus-summary-gather-subject-limit
5605             (or (and (numberp gnus-summary-gather-subject-limit)
5606                      (> (length subject) gnus-summary-gather-subject-limit)
5607                      (setq subject
5608                            (substring subject 0 
5609                                       gnus-summary-gather-subject-limit)))
5610                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5611                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5612           (setq subject (gnus-simplify-subject-re subject)))
5613         (if (setq hthread 
5614                   (gnus-gethash subject hashtb))
5615             (progn
5616               (or (stringp (car (car hthread)))
5617                   (setcar hthread (list whole-subject (car hthread))))
5618               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5619                                            (list (car threads))))
5620               (setcdr prev (cdr threads))
5621               (setq threads prev))
5622           (gnus-sethash subject threads hashtb))
5623         (setq prev threads)
5624         (setq threads (cdr threads)))
5625       result)))
5626
5627 (defun gnus-make-threads ()
5628   ;; This function takes the dependencies already made by 
5629   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5630   ;; through the dependecies in the hash table and finds all the
5631   ;; roots. Roots do not refer back to any valid articles.
5632   (let (roots)
5633     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5634          (gnus-build-old-threads))
5635     (mapatoms
5636      (lambda (refs)
5637        (if (not (car (symbol-value refs)))
5638            (setq roots (append (cdr (symbol-value refs)) roots))
5639          ;; Ok, these refer back to valid articles, but if
5640          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5641          ;; the root has the same subject as its children. The children
5642          ;; that do not are made into roots and removed from the list
5643          ;; of children. 
5644          (or gnus-thread-ignore-subject
5645              (let* ((prev (symbol-value refs))
5646                     (subject (gnus-simplify-subject-re 
5647                               (header-subject (car prev))))
5648                     (headers (cdr prev)))
5649                (while headers
5650                  (if (not (string= subject
5651                                    (gnus-simplify-subject-re 
5652                                     (header-subject (car headers)))))
5653                      (progn
5654                        (setq roots (cons (car headers) roots))
5655                        (setcdr prev (cdr headers)))
5656                    (setq prev headers))
5657                  (setq headers (cdr headers)))))))
5658      gnus-newsgroup-dependencies)
5659     
5660     (mapcar 'gnus-trim-thread
5661             (apply 'append
5662                    (mapcar 'gnus-cut-thread
5663                            (mapcar 'gnus-make-sub-thread roots))))))
5664   
5665 (defun gnus-make-threads-and-expunge ()
5666   ;; This function takes the dependencies already made by 
5667   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5668   ;; through the dependecies in the hash table and finds all the
5669   ;; roots. Roots do not refer back to any valid articles.
5670   (let ((default (or gnus-summary-default-score 0))
5671         (below gnus-summary-expunge-below)
5672         roots article)
5673     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5674          (gnus-build-old-threads))
5675     (mapatoms
5676      (lambda (refs)
5677        (if (not (car (symbol-value refs)))
5678            ;; These articles do not refer back to any other articles -
5679            ;; they are roots.
5680            (let ((headers (cdr (symbol-value refs))))
5681              ;; We weed out the low-scored articles.
5682              (while headers
5683                (if (not (< (or (cdr (assq (header-number (car headers))
5684                                           gnus-newsgroup-scored)) default)
5685                            below))
5686                    ;; It is over.
5687                    (setq roots (cons (car headers) roots))
5688                  ;; It is below, so we mark it as read.
5689                  (setq gnus-newsgroup-unreads
5690                        (delq (header-number (car headers))
5691                              gnus-newsgroup-unreads)))
5692                (setq headers (cdr headers))))
5693          ;; Ok, these refer back to valid articles, but if
5694          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5695          ;; the root has the same subject as its children. The children
5696          ;; that do not are made into roots and removed from the list
5697          ;; of children. 
5698          (or gnus-thread-ignore-subject
5699              (let* ((prev (symbol-value refs))
5700                     (subject (gnus-simplify-subject-re 
5701                               (header-subject (car prev))))
5702                     (headers (cdr prev)))
5703                (while headers
5704                  (if (not (string= subject
5705                                    (gnus-simplify-subject-re 
5706                                     (header-subject (car headers)))))
5707                      (progn
5708                        (if (not (< (or (cdr (assq (header-number (car headers))
5709                                                   gnus-newsgroup-scored))
5710                                        default) below))
5711                            (setq roots (cons (car headers) roots))
5712                          (setq gnus-newsgroup-unreads
5713                                (delq (header-number (car headers))
5714                                      gnus-newsgroup-unreads)))
5715                        (setcdr prev (cdr headers)))
5716                    (setq prev headers))
5717                  (setq headers (cdr headers)))))
5718          ;; If this article is expunged, some of the children might be
5719          ;; roots.  
5720          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5721                                gnus-newsgroup-scored)) default)
5722                 below)
5723              (let* ((prev (symbol-value refs))
5724                     (headers (cdr prev)))
5725                (while headers
5726                  (setq article (header-number (car headers)))
5727                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5728                                  default) below))
5729                      (progn (setq roots (cons (car headers) roots))
5730                             (setq prev headers))
5731                    (setq gnus-newsgroup-unreads 
5732                          (delq article gnus-newsgroup-unreads))
5733                    (setcdr prev (cdr headers)))
5734                  (setq headers (cdr headers))))
5735            ;; It was not expunged, but we look at expunged children.
5736            (let* ((prev (symbol-value refs))
5737                   (headers (cdr prev))
5738                   article id)
5739              (while headers
5740                (setq article (header-number (car headers)))
5741                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5742                                default) below))
5743                    (setq prev headers)
5744                  (setq gnus-newsgroup-unreads 
5745                        (delq article gnus-newsgroup-unreads))
5746                  (setcdr prev (cdr headers)))
5747                (setq headers (cdr headers)))))))
5748      gnus-newsgroup-dependencies)
5749
5750     (mapcar 'gnus-trim-thread
5751             (apply 'append
5752                    (mapcar 'gnus-cut-thread
5753                            (mapcar 'gnus-make-sub-thread roots))))))
5754   
5755 (defun gnus-cut-thread (thread)
5756   ;; Remove leaf dormant or ancient articles from THREAD.
5757   (let ((head (car thread))
5758         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5759     (if (and (null tail)
5760              (let ((number (header-number head)))
5761                (or (memq number gnus-newsgroup-ancient)
5762                    (memq number gnus-newsgroup-dormant)
5763                    (and gnus-summary-expunge-below
5764                         (eq gnus-fetch-old-headers 'some)
5765                         (< (or (cdr (assq number gnus-newsgroup-scored))
5766                                gnus-summary-default-score 0)
5767                            gnus-summary-expunge-below)
5768                         (progn
5769                           (setq gnus-newsgroup-unreads
5770                                 (delq number gnus-newsgroup-unreads))
5771                           t)))))
5772         nil
5773       (list (cons head tail)))))
5774
5775 (defun gnus-trim-thread (thread)
5776   ;; Remove root ancient articles with only one child from THREAD.
5777   (if (and (eq gnus-fetch-old-headers 'some)
5778            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5779            (= (length thread) 2))
5780       (gnus-trim-thread (nth 1 thread))
5781     thread))
5782
5783 (defun gnus-make-sub-thread (root)
5784   ;; This function makes a sub-tree for a node in the tree.
5785   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5786                                               gnus-newsgroup-dependencies)))))
5787     (cons root (mapcar 'gnus-make-sub-thread children))))
5788
5789 (defun gnus-build-old-threads ()
5790   ;; Look at all the articles that refer back to old articles, and
5791   ;; fetch the headers for the articles that aren't there. This will
5792   ;; build complete threads - if the roots haven't been expired by the
5793   ;; server, that is.
5794   (let (id heads)
5795     (mapatoms
5796      (lambda (refs)
5797        (if (not (car (symbol-value refs)))
5798            (progn
5799              (setq heads (cdr (symbol-value refs)))
5800              (while heads
5801                (if (not (memq (header-number (car heads))
5802                               gnus-newsgroup-dormant))
5803                    (progn
5804                      (setq id (symbol-name refs))
5805                      (while (and (setq id (gnus-build-get-header id))
5806                                  (not (car (gnus-gethash 
5807                                             id gnus-newsgroup-dependencies)))))
5808                      (setq heads nil))
5809                  (setq heads (cdr heads)))))))
5810      gnus-newsgroup-dependencies)))
5811
5812 (defun gnus-build-get-header (id)
5813   ;; Look through the buffer of NOV lines and find the header to
5814   ;; ID. Enter this line into the dependencies hash table, and return
5815   ;; the id of the parent article (if any).
5816   (let ((deps gnus-newsgroup-dependencies)
5817         found header)
5818     (prog1
5819         (save-excursion
5820           (set-buffer nntp-server-buffer)
5821           (goto-char (point-min))
5822           (while (and (not found) (search-forward id nil t))
5823             (beginning-of-line)
5824             (setq found (looking-at 
5825                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5826                                  (regexp-quote id))))
5827             (or found (beginning-of-line 2)))
5828           (if found
5829               (let (ref)
5830                 (beginning-of-line)
5831                 (and
5832                  (setq header (gnus-nov-parse-line 
5833                                (read (current-buffer)) deps))
5834                  (setq ref (header-references header))
5835                  (string-match "\\(<[^>]+>\\) *$" ref)
5836                  (substring ref (match-beginning 1) (match-end 1))))))
5837       (and header
5838            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5839                  gnus-newsgroup-ancient (cons (header-number header)
5840                                               gnus-newsgroup-ancient))))))
5841
5842 ;; Re-build the thread containing ID.
5843 (defun gnus-rebuild-thread (id)
5844   (let ((dep gnus-newsgroup-dependencies)
5845         (buffer-read-only nil)
5846         parent headers refs thread art)
5847     (while (and id (setq headers
5848                          (car (setq art (gnus-gethash (downcase id) dep)))))
5849       (setq parent art)
5850       (setq id (and (setq refs (header-references headers))
5851                     (string-match "\\(<[^>]+>\\) *$" refs)
5852                     (substring refs (match-beginning 1) (match-end 1)))))
5853     (setq thread (gnus-make-sub-thread (car parent)))
5854     (gnus-rebuild-remove-articles thread)
5855     (let ((beg (point)))
5856       (gnus-summary-prepare-threads (list thread) 0)
5857       (gnus-summary-update-lines beg (point)))))
5858
5859 ;; Delete all lines in the summary buffer that correspond to articles
5860 ;; in this thread.
5861 (defun gnus-rebuild-remove-articles (thread)
5862   (and (gnus-summary-goto-subject (header-number (car thread)))
5863        (gnus-delete-line))
5864   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5865
5866 (defun gnus-sort-threads (threads)
5867   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5868   (let ((fun gnus-thread-sort-functions))
5869     (while fun
5870       (setq threads (sort threads (car fun))
5871             fun (cdr fun))))
5872   threads)
5873
5874 (defun gnus-thread-header (thread)
5875   ;; Return header of first article in THREAD.
5876   (if (consp thread)
5877       (if (stringp (car thread))
5878           (car (car (cdr thread)))
5879         (car thread))
5880     thread))
5881
5882 (defun gnus-thread-sort-by-number (h1 h2)
5883   "Sort threads by root article number."
5884   (let ((h1 (gnus-thread-header h1))
5885         (h2 (gnus-thread-header h2)))
5886     (< (header-number h1) (header-number h2))))
5887
5888 (defun gnus-thread-sort-by-author (h1 h2)
5889   "Sort threads by root author."
5890   (let ((h1 (gnus-thread-header h1))
5891         (h2 (gnus-thread-header h2)))
5892     (string-lessp
5893      (let ((extract (funcall 
5894                      gnus-extract-address-components (header-from h1))))
5895        (or (car extract) (cdr extract)))
5896      (let ((extract (funcall
5897                      gnus-extract-address-components (header-from h2))))
5898        (or (car extract) (cdr extract))))))
5899
5900 (defun gnus-thread-sort-by-subject (h1 h2)
5901   "Sort threads by root subject."
5902   (let ((h1 (gnus-thread-header h1))
5903         (h2 (gnus-thread-header h2)))
5904     (string-lessp
5905      (downcase (gnus-simplify-subject (header-subject h1)))
5906      (downcase (gnus-simplify-subject (header-subject h2))))))
5907
5908 (defun gnus-thread-sort-by-date (h1 h2)
5909   "Sort threads by root article date."
5910   (let ((h1 (gnus-thread-header h1))
5911         (h2 (gnus-thread-header h2)))
5912     (string-lessp
5913      (gnus-sortable-date (header-date h1))
5914      (gnus-sortable-date (header-date h2)))))
5915
5916 (defun gnus-thread-sort-by-score (h1 h2)
5917   "Sort threads by root article score.
5918 Unscored articles will be counted as havin a score of zero."
5919   (let ((h1 (gnus-thread-header h1))
5920         (h2 (gnus-thread-header h2)))
5921     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5922           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5923       (> (or (cdr s1) gnus-summary-default-score 0)
5924          (or (cdr s2) gnus-summary-default-score 0)))))
5925
5926 (defun gnus-thread-sort-by-total-score (h1 h2)
5927   "Sort threads by the sum of all scores in the thread.
5928 Unscored articles will be counted as havin a score of zero."
5929   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5930
5931 (defun gnus-thread-total-score (thread)
5932   ;;  This function find the total score of THREAD.
5933   (if (consp thread)
5934       (if (stringp (car thread))
5935           (apply gnus-thread-score-function 0
5936                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5937         (gnus-thread-total-score-1 thread))
5938     (gnus-thread-total-score-1 (list thread))))
5939
5940 (defun gnus-thread-total-score-1 (root)
5941   ;; This function find the total score of the thread below ROOT.
5942   (setq root (car root))
5943   (apply gnus-thread-score-function
5944          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5945              gnus-summary-default-score 0)
5946          (mapcar 'gnus-thread-total-score
5947                  (cdr (gnus-gethash (downcase (header-id root))
5948                                     gnus-newsgroup-dependencies)))))
5949
5950 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5951 (defvar gnus-tmp-prev-subject "")
5952 (defvar gnus-tmp-adopt-thread nil)
5953
5954 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
5955 (defun gnus-summary-prepare-threads 
5956   (threads level &optional not-child no-subject cull)
5957   "Prepare summary buffer from THREADS and indentation LEVEL.  
5958 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
5959 or a straight list of headers."
5960   (let (thread header number subject clevel)
5961     (while threads
5962       (setq thread (car threads)
5963             threads (cdr threads))
5964       ;; If `thread' is a cons, hierarchical threads are used.  If not,
5965       ;; `thread' is the header.
5966       (if (consp thread)
5967           (setq header (car thread))
5968         (setq header thread)
5969         (and cull
5970              (or (memq (setq number (header-number header))
5971                        gnus-newsgroup-dormant)
5972                  (and gnus-summary-expunge-below
5973                       (< (or (cdr (assq number gnus-newsgroup-scored))
5974                              gnus-summary-default-score 0)
5975                          gnus-summary-expunge-below)))
5976              (progn
5977                (setq header nil)
5978                (setq gnus-newsgroup-unreads 
5979                      (delq number gnus-newsgroup-unreads)))))
5980       (cond 
5981        ((stringp header)
5982         ;; The header is a dummy root.
5983         (cond ((eq gnus-summary-make-false-root 'adopt)
5984                ;; We let the first article adopt the rest.
5985                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
5986                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
5987                (setq thread (cdr (cdr thread)))
5988                (while thread
5989                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
5990                  (setq thread (cdr thread))))
5991               ((eq gnus-summary-make-false-root 'dummy)
5992                ;; We output a dummy root.
5993                (gnus-summary-insert-dummy-line 
5994                 nil header (header-number (car (car (cdr thread)))))
5995                (setq clevel 1))
5996               ((eq gnus-summary-make-false-root 'empty)
5997                ;; We print the articles with empty subject fields. 
5998                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
5999                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6000                (setq thread (cdr (cdr thread)))
6001                (while thread
6002                  (gnus-summary-prepare-threads 
6003                   (list (car thread)) 0 nil
6004                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6005                             (not (string=  
6006                                   (gnus-simplify-subject-re 
6007                                    (header-subject (car (car thread))))
6008                                   (gnus-simplify-subject-re header))))))
6009                  (setq thread (cdr thread))))
6010               (t
6011                ;; We do not make a root for the gathered
6012                ;; sub-threads at all.  
6013                (setq clevel 0)))
6014         ;; Print the sub-threads.
6015         (and (consp thread) (cdr thread)
6016              (gnus-summary-prepare-threads (cdr thread) clevel)))
6017        ;; The header is a real article.
6018        (header
6019         (setq number (header-number header)
6020               subject (header-subject header))
6021         (and gnus-newsgroup-async
6022              (setq gnus-newsgroup-threads
6023                    (cons (cons (header-number header)
6024                                (header-lines header)) gnus-newsgroup-threads)))
6025         (gnus-summary-insert-line
6026          nil header level nil 
6027          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6028                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6029                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6030                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6031                (t gnus-ancient-mark))
6032          (memq number gnus-newsgroup-replied)
6033          (memq number gnus-newsgroup-expirable)
6034          (if no-subject 
6035              gnus-summary-same-subject
6036            (if (or (zerop level)
6037                    (and gnus-thread-ignore-subject
6038                         (not (string= 
6039                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6040                               (gnus-simplify-subject-re subject)))))
6041                subject
6042              gnus-summary-same-subject))
6043          not-child
6044          (cdr (assq number gnus-newsgroup-scored)))
6045         (setq gnus-tmp-prev-subject subject)
6046         ;; Recursively print subthreads.
6047         (and (consp thread) (cdr thread)
6048              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6049
6050 (defun gnus-select-newsgroup (group &optional read-all)
6051   "Select newsgroup GROUP.
6052 If READ-ALL is non-nil, all articles in the group are selected."
6053   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6054          (info (nth 2 entry))
6055          articles header-marks)
6056     (gnus-check-news-server
6057      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6058
6059     (or (gnus-server-opened gnus-current-select-method)
6060         (gnus-open-server gnus-current-select-method)
6061         (error "Couldn't open server"))
6062     
6063     (or (and (eq (car entry) t)
6064              (gnus-activate-newsgroup (car info)))
6065         (gnus-request-group group t)
6066         (progn
6067           (kill-buffer (current-buffer))
6068           (error "Couldn't request group %s: %s" 
6069                  group (gnus-status-message group))))
6070
6071     (setq gnus-newsgroup-name group)
6072     (setq gnus-newsgroup-unselected nil)
6073     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6074
6075     (and gnus-asynchronous
6076          (gnus-check-backend-function 
6077           'request-asynchronous gnus-newsgroup-name)
6078          (setq gnus-newsgroup-async
6079                (gnus-request-asynchronous gnus-newsgroup-name)))
6080
6081     (setq articles (gnus-articles-to-read group read-all))
6082
6083     (cond 
6084      ((null articles) 
6085       (gnus-message 3 "Couldn't select newsgroup")
6086       'quit)
6087      ((eq articles 0) nil)
6088      (t
6089       ;; Init the dependencies hash table.
6090       (setq gnus-newsgroup-dependencies 
6091             (gnus-make-hashtable (length articles)))
6092       ;; Retrieve the headers and read them in.
6093       (setq gnus-newsgroup-headers 
6094             (if (eq 'nov (setq gnus-headers-retrieved-by
6095                                (gnus-retrieve-headers 
6096                                 (if gnus-fetch-old-headers 
6097                                     (cons 1 articles) articles) 
6098                                 gnus-newsgroup-name)))
6099                 (progn
6100                   (gnus-get-newsgroup-headers-xover articles))
6101               (gnus-get-newsgroup-headers)))
6102       ;; If we were to fetch old headers, but the backend didn't
6103       ;; support XOVER, then it is possible we fetched one article
6104       ;; that we shouldn't have. If that's the case, we pop it off the
6105       ;; list of headers.
6106       (and (not (eq gnus-headers-retrieved-by 'nov))
6107            gnus-fetch-old-headers
6108            gnus-newsgroup-headers
6109            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
6110            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
6111       ;; Remove cancelled articles from the list of unread articles.
6112       (setq gnus-newsgroup-unreads
6113             (gnus-set-sorted-intersection 
6114              gnus-newsgroup-unreads
6115              (mapcar (lambda (headers) (header-number headers))
6116                      gnus-newsgroup-headers)))
6117       ;; Adjust and set lists of article marks.
6118       (and info
6119            (let (marked)
6120              (gnus-adjust-marked-articles info)
6121              (setq gnus-newsgroup-marked 
6122                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6123              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6124              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6125              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6126              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6127              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6128              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6129              (setq gnus-newsgroup-processable nil)))
6130       ;; Check whether auto-expire is to be done in this group.
6131       (setq gnus-newsgroup-auto-expire
6132             (or (and (stringp gnus-auto-expirable-newsgroups)
6133                      (string-match gnus-auto-expirable-newsgroups group))
6134                 (memq 'auto-expire (nth 5 info))))
6135       ;; First and last article in this newsgroup.
6136       (and gnus-newsgroup-headers
6137            (setq gnus-newsgroup-begin 
6138                  (header-number (car gnus-newsgroup-headers)))
6139            (setq gnus-newsgroup-end
6140                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6141       (setq gnus-reffed-article-number -1)
6142       ;; GROUP is successfully selected.
6143       (or gnus-newsgroup-headers t)))))
6144
6145 (defun gnus-articles-to-read (group read-all)
6146   ;; Find out what articles the user wants to read.
6147   (let* ((articles
6148           ;; Select all articles if `read-all' is non-nil, or if all the
6149           ;; unread articles are dormant articles.
6150           (if (or (and read-all (not (numberp read-all)))
6151                   (= (length gnus-newsgroup-unreads) 
6152                      (length gnus-newsgroup-dormant)))
6153               (gnus-uncompress-range 
6154                (gnus-gethash group gnus-active-hashtb))
6155             gnus-newsgroup-unreads))
6156          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6157          (scored (length scored-list))
6158          (number (length articles))
6159          (marked (+ (length gnus-newsgroup-marked)
6160                     (length gnus-newsgroup-dormant)))
6161          (select
6162           (cond 
6163            ((numberp read-all)
6164             read-all)
6165            (t
6166             (condition-case ()
6167                 (cond ((and (or (<= scored marked)
6168                                 (= scored number))
6169                             (numberp gnus-large-newsgroup)
6170                             (> number gnus-large-newsgroup))
6171                        (let ((input
6172                               (read-string
6173                                (format
6174                                 "How many articles from %s (default %d): "
6175                                 gnus-newsgroup-name number))))
6176                          (if (string-equal input "")
6177                              number input)))
6178                       ((and (> scored marked) (< scored number))
6179                        (let ((input
6180                               (read-string
6181                                (format 
6182                                 "%s %s (%d scored, %d total): "
6183                                 "How many articles from"
6184                                 group scored number))))
6185                          (if (string-equal input "")
6186                              number input)))
6187                       (t number))
6188               (quit nil)))))
6189          total-articles)
6190     (setq select (if (stringp select) (string-to-number select) select))
6191     (if (or (null select) (zerop select))
6192         select
6193       (if (and (not (zerop scored)) (<= (abs select) scored))
6194           (progn
6195             (setq articles (sort scored-list '<))
6196             (setq number (length articles)))
6197         (setq articles (copy-sequence articles)))
6198
6199       (setq total-articles articles)
6200       
6201       (if (< (abs select) number)
6202           (if (< select 0) 
6203               ;; Select the N oldest articles.
6204               (setcdr (nthcdr (1- (abs select)) articles) nil)
6205             ;; Select the N most recent articles.
6206             (setq articles (nthcdr (- number select) articles))))
6207       (setq gnus-newsgroup-unselected
6208             (gnus-sorted-intersection
6209              gnus-newsgroup-unreads
6210              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6211       articles)))
6212
6213 (defun gnus-killed-articles (killed articles)
6214   (let (out)
6215     (while articles
6216       (if (inline (gnus-member-of-range (car articles) killed))
6217           (setq out (cons (car articles) out)))
6218       (setq articles (cdr articles)))
6219     out))
6220
6221 (defun gnus-adjust-marked-articles (info &optional active)
6222   "Remove all marked articles that are no longer legal."
6223   (let ((marked-lists (nth 3 info))
6224         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6225         marked m prev)
6226     ;; There are many types of marked articles.
6227     (while marked-lists
6228       (setq m (cdr (setq prev (car marked-lists))))
6229       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6230              ;; Make sure that all ticked articles are a subset of the
6231              ;; unread/unselected articles.
6232              (while m
6233                (if (or (memq (car m) gnus-newsgroup-unreads)
6234                        (memq (car m) gnus-newsgroup-unselected))
6235                    (setq prev m)
6236                  (setcdr prev (cdr m)))
6237                (setq m (cdr m))))
6238             ((eq 'score (car prev))
6239              ;; Scored articles should be a subset of
6240              ;; unread/unselected articles. 
6241              (while m
6242                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6243                        (memq (car (car m)) gnus-newsgroup-unreads))
6244                    (setq prev m)
6245                  (setcdr prev (cdr m)))
6246                (setq m (cdr m))))
6247             ((eq 'bookmark (car prev))
6248              ;; Bookmarks should be a subset of active articles.
6249              (while m
6250                (if (< (car (car m)) (car active))
6251                    (setcdr prev (cdr m))
6252                  (setq prev m))
6253                (setq m (cdr m))))
6254             ((eq 'killed (car prev))
6255              ;; Articles that have been through the kill process are
6256              ;; to be a subset of active articles.
6257              (while (and m (< (or (and (numberp (car m)) (car m))
6258                                   (cdr (car m)))
6259                               (car active)))
6260                (setcdr prev (cdr m))
6261                (setq m (cdr m)))
6262              (if (and m (< (or (and (numberp (car m)) (car m))
6263                                (car (car m)))
6264                            (car active))) 
6265                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6266             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6267              ;; The replied and expirable articles have to be articles
6268              ;; that are active. 
6269              (while m
6270                (if (< (car m) (car active))
6271                    (setcdr prev (cdr m))
6272                  (setq prev m))
6273                (setq m (cdr m)))))
6274       (setq marked-lists (cdr marked-lists)))
6275     ;; Remove all lists that are empty.
6276     (setq marked-lists (nth 3 info))
6277     (if marked-lists
6278         (progn
6279           (while (= 1 (length (car marked-lists)))
6280             (setq marked-lists (cdr marked-lists)))
6281           (setq m (cdr (setq prev marked-lists)))
6282           (while m
6283             (if (= 1 (length (car m)))
6284                 (setcdr prev (cdr m))
6285               (setq prev m))
6286             (setq m (cdr m)))
6287           (setcar (nthcdr 3 info) marked-lists)))
6288     ;; Finally, if there are no marked lists at all left, and if there
6289     ;; are no elements after the lists in the info list, we just chop
6290     ;; the info list off before the marked lists.
6291     (and (null marked-lists) 
6292          (not (nthcdr 4 info))
6293          (setcdr (nthcdr 2 info) nil)))
6294   info)
6295
6296 (defun gnus-set-marked-articles 
6297   (info ticked replied expirable killed dormant bookmark score) 
6298   "Enter the various lists of marked articles into the newsgroup info list."
6299   (let (newmarked)
6300     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6301     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6302     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6303                                          newmarked)))
6304     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6305     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6306     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6307                                         newmarked)))
6308     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6309     (if (nthcdr 3 info)
6310         (if newmarked
6311             (setcar (nthcdr 3 info) newmarked)
6312           (if (not (nthcdr 4 info))
6313               (setcdr (nthcdr 2 info) nil)
6314             (setcar (nthcdr 3 info) nil)))
6315       (if newmarked
6316           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
6317
6318 (defun gnus-add-marked-articles (group type articles &optional info force)
6319   ;; Add ARTICLES of TYPE to the info of GROUP.
6320   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6321   ;; add, but replace marked articles of TYPE with ARTICLES.
6322   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6323         marked m)
6324     (or (not info)
6325         (and (not (setq marked (nthcdr 3 info)))
6326              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6327         (and (not (setq m (assq type (car marked))))
6328              (setcar marked (cons (cons type articles) (car marked))))
6329         (if force
6330             (setcdr m articles)
6331           (nconc m articles)))))
6332          
6333 (defun gnus-set-mode-line (where)
6334   "This function sets the mode line of the article or summary buffers.
6335 If WHERE is `summary', the summary mode line format will be used."
6336   (if (memq where gnus-updated-mode-lines)
6337       (let (mode-string)
6338         (save-excursion
6339           (set-buffer gnus-summary-buffer)
6340           (let* ((mformat (if (eq where 'article) 
6341                               gnus-article-mode-line-format-spec
6342                             gnus-summary-mode-line-format-spec))
6343                  (group-name gnus-newsgroup-name)
6344                  (article-number (or gnus-current-article 0))
6345                  (unread (- (length gnus-newsgroup-unreads)
6346                             (length gnus-newsgroup-dormant)))
6347                  (unread-and-unticked 
6348                   (- unread (length gnus-newsgroup-marked)))
6349                  (unselected (length gnus-newsgroup-unselected))
6350                  (unread-and-unselected
6351                   (cond ((and (zerop unread-and-unticked)
6352                               (zerop unselected)) "")
6353                         ((zerop unselected) 
6354                          (format "{%d more}" unread-and-unticked))
6355                         (t (format "{%d(+%d) more}"
6356                                    unread-and-unticked unselected))))
6357                  (subject
6358                   (if gnus-current-headers
6359                       (header-subject gnus-current-headers) ""))
6360                  (max-len (and gnus-mode-non-string-length
6361                                (- (frame-width) gnus-mode-non-string-length)))
6362                  header) ;; passed as argument to any user-format-funcs
6363             (setq mode-string (eval mformat))
6364             (or (numberp max-len)
6365                 (setq max-len (length mode-string)))
6366             (if (< max-len 4) (setq max-len 4))
6367             (if (> (length mode-string) max-len)
6368                 (setq mode-string 
6369                       (concat (substring mode-string 0 (- max-len 3))
6370                               "...")))
6371             (setq mode-string (format (format "%%-%ds" max-len)
6372                                       mode-string))))
6373         (setq mode-line-buffer-identification mode-string)
6374         (set-buffer-modified-p t))))
6375
6376 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6377   "Go through the HEADERS list and add all Xrefs to a hash table.
6378 The resulting hash table is returned, or nil if no Xrefs were found."
6379   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6380          (prefix (if (and 
6381                       (gnus-group-foreign-p from-newsgroup)
6382                       (not (memq 'virtual 
6383                                  (assoc (symbol-name (car from-method))
6384                                         gnus-valid-select-methods))))
6385                      (gnus-group-real-prefix from-newsgroup)))
6386          (xref-hashtb (make-vector 63 0))
6387          start group entry number xrefs header)
6388     (while headers
6389       (setq header (car headers))
6390       (if (and (setq xrefs (header-xref header))
6391                (not (memq (header-number header) unreads)))
6392           (progn
6393             (setq start 0)
6394             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6395               (setq start (match-end 0))
6396               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6397                                                     (match-end 1))))
6398               (setq number 
6399                     (string-to-int (substring xrefs (match-beginning 2) 
6400                                               (match-end 2))))
6401               (if (setq entry (gnus-gethash group xref-hashtb))
6402                   (setcdr entry (cons number (cdr entry)))
6403                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6404       (setq headers (cdr headers)))
6405     (if start xref-hashtb nil)))
6406
6407 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6408   "Look through all the headers and mark the Xrefs as read."
6409   (let ((virtual (memq 'virtual 
6410                        (assoc (symbol-name (car (gnus-find-method-for-group 
6411                                                  from-newsgroup)))
6412                               gnus-valid-select-methods)))
6413         name entry read info xref-hashtb idlist active num range exps method
6414         nth4)
6415     (save-excursion
6416       (set-buffer gnus-group-buffer)
6417       (if (setq xref-hashtb 
6418                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6419           (mapatoms 
6420            (lambda (group)
6421              (if (string= from-newsgroup (setq name (symbol-name group)))
6422                  ()
6423                (setq idlist (symbol-value group))
6424                ;; Dead groups are not updated.
6425                (if (and (prog1 
6426                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6427                                   info (nth 2 entry))
6428                           (if (stringp (setq nth4 (nth 4 info)))
6429                               (setq nth4 (gnus-server-to-method nth4))))
6430                         ;; Only do the xrefs if the group has the same
6431                         ;; select method as the group we have just read.
6432                         (or (gnus-methods-equal-p 
6433                              nth4 (gnus-find-method-for-group from-newsgroup))
6434                             virtual
6435                             (equal nth4 
6436                                    (setq method (gnus-find-method-for-group 
6437                                                  from-newsgroup)))
6438                             (and (equal (car nth4) (car method))
6439                                  (equal (nth 1 nth4) (nth 1 method))))
6440                         gnus-use-cross-reference
6441                         (or (not (eq gnus-use-cross-reference t))
6442                             virtual
6443                             ;; Only do cross-references on subscribed
6444                             ;; groups, if that is what is wanted.  
6445                             (<= (nth 1 info) gnus-level-subscribed)))
6446                    (progn
6447                      (setq num 0)
6448                      ;; Set the new list of read articles in this group.
6449                      (setq active (gnus-gethash name gnus-active-hashtb))
6450                      ;; First peel off all illegal article numbers.
6451                      (if active
6452                          (let ((ids idlist)
6453                                (ticked (cdr (assq 'tick (nth 3 info))))
6454                                (dormant (cdr (assq 'dormant (nth 3 info))))
6455                                id)
6456                            (setq exps nil)
6457                            (while ids
6458                              (setq id (car ids))
6459                              (if (or (> id (cdr active))
6460                                      (< id (car active))
6461                                      (memq id ticked)
6462                                      (memq id dormant))
6463                                  (setq idlist (delq id idlist)))
6464                              (and (memq id expirable)
6465                                   (setq exps (cons id exps)))
6466                              (setq ids (cdr ids)))))
6467                      ;; Update expirable articles.
6468                      (gnus-add-marked-articles nil 'expirable exps info)
6469                      (and active
6470                           (null (nth 2 info))
6471                           (> (car active) 1)
6472                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6473                      (setcar (nthcdr 2 info)
6474                              (setq range
6475                                    (gnus-add-to-range 
6476                                     (nth 2 info) 
6477                                     (setq idlist (sort idlist '<)))))
6478                      ;; Then we have to re-compute how many unread
6479                      ;; articles there are in this group.
6480                      (if active
6481                          (progn
6482                            (cond 
6483                             ((not range)
6484                              (setq num (- (1+ (cdr active)) (car active))))
6485                             ((not (listp (cdr range)))
6486                              (setq num (- (cdr active) (- (1+ (cdr range)) 
6487                                                           (car range)))))
6488                             (t
6489                              (while range
6490                                (if (numberp (car range))
6491                                    (setq num (1+ num))
6492                                  (setq num (+ num (- (1+ (cdr (car range)))
6493                                                      (car (car range))))))
6494                                (setq range (cdr range)))
6495                              (setq num (- (cdr active) num))))
6496                            ;; Update the number of unread articles.
6497                            (setcar 
6498                             entry 
6499                             (max 0 (- num 
6500                                       (length (cdr (assq 'tick (nth 3 info))))
6501                                       (length 
6502                                        (cdr (assq 'dormant (nth 3 info)))))))
6503                            ;; Update the group buffer.
6504                            (gnus-group-update-group name t)))))))
6505            xref-hashtb)))))
6506
6507 (defun gnus-methods-equal-p (m1 m2)
6508   (let ((m1 (or m1 gnus-select-method))
6509         (m2 (or m2 gnus-select-method)))
6510     (or (equal m1 m2)
6511         (and (eq (car m1) (car m2))
6512              (or (not (memq 'address (assoc (symbol-name (car m1))
6513                                             gnus-valid-select-methods)))
6514                  (equal (nth 1 m1) (nth 1 m2)))))))
6515
6516 (defsubst gnus-header-value ()
6517   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6518
6519 (defvar gnus-newsgroup-none-id 0)
6520
6521 (defun gnus-get-newsgroup-headers ()
6522   (setq gnus-article-internal-prepare-hook nil)
6523   (let ((cur nntp-server-buffer)
6524         (dependencies gnus-newsgroup-dependencies)
6525         headers char article id dep end)
6526     (save-excursion
6527       (set-buffer nntp-server-buffer)
6528       (goto-char (point-min))
6529       ;; Search to the beginning of the next header. Error messages
6530       ;; do not begin with 2 or 3.
6531       (while (re-search-forward "^[23][0-9]+ " nil t)
6532         (let ((header (make-vector 9 nil))
6533               (c (following-char))
6534               (case-fold-search t)
6535               (p (point))
6536               from subject in-reply-to references ref)
6537           (setq id nil
6538                 ref nil
6539                 references nil
6540                 subject nil
6541                 from nil)
6542           (header-set-number header (setq article (read cur)))
6543           ;; This implementation of this function, with nine
6544           ;; search-forwards instead of the one re-search-forward and
6545           ;; a case (which basically was the old function) is actually
6546           ;; about twice as fast, even though it looks messier. You
6547           ;; can't have everything, I guess. Speed and elegance
6548           ;; doesn't always come hand in hand.
6549           (save-restriction
6550             (narrow-to-region (point) (or (save-excursion 
6551                                             (search-forward "\n.\n" nil t))
6552                                           (point)))
6553             (if (search-forward "\nfrom: " nil t)
6554                 (header-set-from header (gnus-header-value))
6555               (header-set-from header "(nobody)"))
6556             (goto-char p)
6557             (if (search-forward "\nsubject: " nil t)
6558                 (header-set-subject header (gnus-header-value))
6559               (header-set-subject header "(none)"))
6560             (goto-char p)
6561             (and (search-forward "\nxref: " nil t)
6562                  (header-set-xref header (gnus-header-value)))
6563             (goto-char p)
6564             (or (numberp (and (search-forward "\nlines: " nil t)
6565                               (header-set-lines header (read cur))))
6566                 (header-set-lines header 0))
6567             (goto-char p)
6568             (and (search-forward "\ndate: " nil t)
6569                  (header-set-date header (gnus-header-value)))
6570             (goto-char p)
6571             (if (search-forward "\nmessage-id: " nil t)
6572                 (header-set-id header (setq id (gnus-header-value)))
6573               ;; If there was no message-id, we just fake one to make
6574               ;; subsequent routines simpler.
6575               (header-set-id 
6576                header 
6577                (setq id (concat "none+" 
6578                                 (int-to-string 
6579                                  (setq gnus-newsgroup-none-id 
6580                                        (1+ gnus-newsgroup-none-id)))))))
6581             (goto-char p)
6582             (if (search-forward "\nreferences: " nil t)
6583                 (progn
6584                   (header-set-references header (gnus-header-value))
6585                   (setq end (match-end 0))
6586                   (save-excursion
6587                     (setq ref 
6588                           (downcase
6589                            (buffer-substring
6590                             (progn 
6591                               (end-of-line)
6592                               (search-backward ">" end t)
6593                               (1+ (point)))
6594                             (progn
6595                               (search-backward "<" end t)
6596                               (point)))))))
6597               ;; Get the references from the in-reply-to header if there
6598               ;; ware no references and the in-reply-to header looks
6599               ;; promising. 
6600               (if (and (search-forward "\nin-reply-to: " nil t)
6601                        (setq in-reply-to (gnus-header-value))
6602                        (string-match "<[^>]+>" in-reply-to))
6603                   (progn
6604                     (header-set-references 
6605                      header 
6606                      (setq ref (substring in-reply-to (match-beginning 0)
6607                                           (match-end 0))))
6608                     (setq ref (downcase ref)))
6609                 (setq ref "none")))
6610             ;; We do some threading while we read the headers. The
6611             ;; message-id and the last reference are both entered into
6612             ;; the same hash table. Some tippy-toeing around has to be
6613             ;; done in case an article has arrived before the article
6614             ;; which it refers to.
6615             (if (boundp (setq dep (intern (downcase id) dependencies)))
6616                 (if (car (symbol-value dep))
6617                     ;; An article with this Message-ID has already
6618                     ;; been seen, so we ignore this one, except we add
6619                     ;; any additional Xrefs (in case the two articles
6620                     ;; came from different servers.
6621                     (progn
6622                       (header-set-xref 
6623                        (car (symbol-value dep))
6624                        (concat (or (header-xref (car (symbol-value dep))) "")
6625                                (or (header-xref header) "")))
6626                       (setq header nil))
6627                   (setcar (symbol-value dep) header))
6628               (set dep (list header)))
6629             (if header
6630                 (progn
6631                   (if (boundp (setq dep (intern ref dependencies)))
6632                       (setcdr (symbol-value dep) 
6633                               (cons header (cdr (symbol-value dep))))
6634                     (set dep (list nil header)))
6635                   (setq headers (cons header headers))))
6636             (goto-char (point-max))))))
6637     (nreverse headers)))
6638
6639 ;; The following macros and functions were written by Felix Lee
6640 ;; <flee@cse.psu.edu>. 
6641
6642 (defmacro gnus-nov-read-integer ()
6643   '(prog1
6644        (if (= (following-char) ?\t)
6645            0
6646          (let ((num (condition-case nil (read buffer) (error nil))))
6647            (if (numberp num) num 0)))
6648      (or (eobp) (forward-char 1))))
6649
6650 (defmacro gnus-nov-skip-field ()
6651   '(search-forward "\t" eol 'move))
6652
6653 (defmacro gnus-nov-field ()
6654   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6655
6656 ;; Goes through the xover lines and returns a list of vectors
6657 (defun gnus-get-newsgroup-headers-xover (sequence)
6658   "Parse the news overview data in the server buffer, and return a
6659 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6660   ;; Get the Xref when the users reads the articles since most/some
6661   ;; NNTP servers do not include Xrefs when using XOVER.
6662   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6663   (let ((cur nntp-server-buffer)
6664         (dependencies gnus-newsgroup-dependencies)
6665         (none 0)
6666         number headers header)
6667     (save-excursion
6668       (set-buffer nntp-server-buffer)
6669       (goto-char (point-min))
6670       (while (and sequence (not (eobp)))
6671         (setq number (read cur))
6672         (while (and sequence (< (car sequence) number))
6673           (setq sequence (cdr sequence)))
6674         (and sequence 
6675              (eq number (car sequence))
6676              (progn
6677                (setq sequence (cdr sequence))
6678                (if (setq header 
6679                          (inline (gnus-nov-parse-line number dependencies)))
6680                    (setq headers (cons header headers)))))
6681         (forward-line 1))
6682       (setq headers (nreverse headers)))
6683     headers))
6684
6685 ;; This function has to be called with point after the article number
6686 ;; on the beginning of the line.
6687 (defun gnus-nov-parse-line (number dependencies)
6688   (let ((none 0)
6689         (eol (gnus-point-at-eol)) 
6690         (buffer (current-buffer))
6691         header ref id dep)
6692
6693     ;; overview: [num subject from date id refs chars lines misc]
6694     (narrow-to-region (point) eol)
6695     (forward-char)
6696
6697     (condition-case nil
6698         (setq header
6699               (vector 
6700                number                   ; number
6701                (gnus-nov-field)         ; subject
6702                (gnus-nov-field)         ; from
6703                (gnus-nov-field)         ; date
6704                (setq id (or (gnus-nov-field)
6705                             (concat "none+"
6706                                     (int-to-string 
6707                                      (setq none (1+ none)))))) ; id
6708                (progn
6709                  (save-excursion
6710                    (let ((beg (point)))
6711                      (search-forward "\t" eol)
6712                      (if (search-backward ">" beg t)
6713                          (setq ref 
6714                                (downcase 
6715                                 (buffer-substring 
6716                                  (1+ (point))
6717                                  (progn
6718                                    (search-backward "<" beg t)
6719                                    (point)))))
6720                        (setq ref nil))))
6721                  (gnus-nov-field))      ; refs
6722                (gnus-nov-read-integer)  ; chars
6723                (gnus-nov-read-integer)  ; lines
6724                (if (= (following-char) ?\n)
6725                    nil
6726                  (gnus-nov-field))      ; misc
6727                ))
6728       (error (progn 
6729                (ding)
6730                (message "Strange nov line.")
6731                (setq header nil)
6732                (goto-char eol))))
6733
6734     (widen)
6735
6736     ;; We build the thread tree.
6737     (and header
6738          (if (boundp (setq dep (intern (downcase id) dependencies)))
6739              (if (car (symbol-value dep))
6740                  ;; An article with this Message-ID has already been seen,
6741                  ;; so we ignore this one, except we add any additional
6742                  ;; Xrefs (in case the two articles came from different
6743                  ;; servers.
6744                  (progn
6745                    (header-set-xref 
6746                     (car (symbol-value dep))
6747                     (concat (or (header-xref (car (symbol-value dep))) "")
6748                             (or (header-xref header) "")))
6749                    (setq header nil))
6750                (setcar (symbol-value dep) header))
6751            (set dep (list header))))
6752     (if header
6753         (progn
6754           (if (boundp (setq dep (intern (or ref "none") 
6755                                         dependencies)))
6756               (setcdr (symbol-value dep) 
6757                       (cons header (cdr (symbol-value dep))))
6758             (set dep (list nil header)))))
6759     header))
6760
6761 (defun gnus-article-get-xrefs ()
6762   "Fill in the Xref value in `gnus-current-headers', if necessary.
6763 This is meant to be called in `gnus-article-internal-prepare-hook'."
6764   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6765                                  gnus-current-headers)))
6766     (or (not gnus-use-cross-reference)
6767         (and (header-xref headers)
6768              (not (string= (header-xref headers) "")))
6769         (let ((case-fold-search t)
6770               xref)
6771           (save-restriction
6772             (gnus-narrow-to-headers)
6773             (goto-char (point-min))
6774             (if (or (and (eq (downcase (following-char)) ?x)
6775                          (looking-at "Xref:"))
6776                     (search-forward "\nXref:" nil t))
6777                 (progn
6778                   (goto-char (1+ (match-end 0)))
6779                   (setq xref (buffer-substring (point) 
6780                                                (progn (end-of-line) (point))))
6781                   (header-set-xref headers xref))))))))
6782
6783 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6784 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6785
6786 ;; Return a header specified by a NUMBER.
6787 (defun gnus-get-header-by-number (number)
6788   (save-excursion
6789     (set-buffer gnus-summary-buffer)
6790     (or gnus-newsgroup-headers-hashtb-by-number
6791         (gnus-make-headers-hashtable-by-number))
6792     (gnus-gethash (int-to-string number)
6793                   gnus-newsgroup-headers-hashtb-by-number)))
6794
6795 (defun gnus-make-headers-hashtable-by-number ()
6796   "Make hashtable for the variable gnus-newsgroup-headers by number."
6797   (save-excursion
6798     (set-buffer gnus-summary-buffer)
6799     (let ((headers gnus-newsgroup-headers)
6800           header)
6801       (setq gnus-newsgroup-headers-hashtb-by-number
6802             (gnus-make-hashtable (length headers)))
6803       (while headers
6804         (setq header (car headers))
6805         (gnus-sethash (int-to-string (header-number header))
6806                       header gnus-newsgroup-headers-hashtb-by-number)
6807         (setq headers (cdr headers))))))
6808
6809 (defun gnus-more-header-backward ()
6810   "Find new header backward."
6811   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6812         (artnum gnus-newsgroup-begin)
6813         (header nil))
6814     (while (and (not header)
6815                 (> artnum first))
6816       (setq artnum (1- artnum))
6817       (setq header (gnus-read-header artnum)))
6818     header))
6819
6820 (defun gnus-more-header-forward (&optional backward)
6821   "Find new header forward.
6822 If BACKWARD, find new header backward instead."
6823   (if backward
6824       (gnus-more-header-backward)
6825     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6826           (artnum gnus-newsgroup-end)
6827           (header nil))
6828       (while (and (not header)
6829                   (< artnum last))
6830         (setq artnum (1+ artnum))
6831         (setq header (gnus-read-header artnum)))
6832       header)))
6833
6834 (defun gnus-extend-newsgroup (header &optional backward)
6835   "Extend newsgroup selection with HEADER.
6836 Optional argument BACKWARD means extend toward backward."
6837   (if header
6838       (let ((artnum (header-number header)))
6839         (setq gnus-newsgroup-headers
6840               (if backward
6841                   (cons header gnus-newsgroup-headers)
6842                 (nconc gnus-newsgroup-headers (list header))))
6843         (setq gnus-newsgroup-unselected
6844               (delq artnum gnus-newsgroup-unselected))
6845         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6846         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6847
6848 (defun gnus-summary-work-articles (n)
6849   "Return a list of articles to be worked upon. The prefix argument,
6850 the list of process marked articles, and the current article will be
6851 taken into consideration."
6852   (let (articles)
6853     (if (and n (numberp n))
6854         (let ((backward (< n 0))
6855               (n (abs n)))
6856           (save-excursion
6857             (while (and (> n 0)
6858                         (setq articles (cons (gnus-summary-article-number) 
6859                                              articles))
6860                         (gnus-summary-search-forward nil nil backward))
6861               (setq n (1- n))))
6862           (sort articles (function <)))
6863       (or (reverse gnus-newsgroup-processable)
6864           (list (gnus-summary-article-number))))))
6865
6866 (defun gnus-summary-search-group (&optional backward use-level)
6867   "Search for next unread newsgroup.
6868 If optional argument BACKWARD is non-nil, search backward instead."
6869   (save-excursion
6870     (set-buffer gnus-group-buffer)
6871     (save-excursion
6872       ;; We don't want to alter current point of group mode buffer.
6873       (if (gnus-group-search-forward 
6874            backward nil
6875            (if use-level (gnus-group-group-level) nil))
6876           (gnus-group-group-name)))))
6877
6878 (defun gnus-summary-best-group (&optional exclude-group)
6879   "Find the name of the best unread group.
6880 If EXCLUDE-GROUP, do not go to this group."
6881   (save-excursion
6882     (set-buffer gnus-group-buffer)
6883     (save-excursion
6884       (gnus-group-best-unread-group exclude-group))))
6885
6886 (defun gnus-summary-search-subject (&optional backward unread subject)
6887   "Search for article forward.
6888 If BACKWARD is non-nil, search backward.
6889 If UNREAD is non-nil, only unread articles are selected.
6890 If SUBJECT is non-nil, the article which has the same subject will be
6891 searched for." 
6892   (let ((func (if backward 'previous-single-property-change
6893                 'next-single-property-change))
6894         (beg (point))
6895         (did t)
6896         pos)
6897     (beginning-of-line)
6898     (and gnus-summary-check-current unread
6899          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
6900          (setq did nil))
6901     (if (not did)
6902         ()
6903       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6904       (while
6905           (and 
6906            (setq pos (funcall func (point) 'gnus-number))
6907            (goto-char (if backward (1- pos) pos))
6908            (setq did
6909                  (not (and
6910                        (or (not unread)
6911                            (eq (get-text-property (point) 'gnus-mark)
6912                                gnus-unread-mark))
6913                        (or (not subject)
6914                            (equal (gnus-simplify-subject-re subject)
6915                                   (gnus-simplify-subject-re
6916                                    (gnus-summary-subject-string)))))))
6917            (if backward (if (bobp) nil (forward-char -1) t)
6918              (if (eobp) nil (forward-char 1) t)))))
6919     (if did
6920         (progn (goto-char beg) nil)
6921       (prog1
6922           (get-text-property (point) 'gnus-number)
6923         (gnus-summary-position-cursor)))))
6924
6925 (defun gnus-summary-search-forward (&optional unread subject backward)
6926   "Search for article forward.
6927 If UNREAD is non-nil, only unread articles are selected.
6928 If SUBJECT is non-nil, the article which has the same subject will be
6929 searched for. 
6930 If BACKWARD is non-nil, the search will be performed backwards instead."
6931   (gnus-summary-search-subject backward unread subject))
6932
6933 (defun gnus-summary-search-backward (&optional unread subject)
6934   "Search for article backward.
6935 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6936 If 2nd optional argument SUBJECT is non-nil, the article which has
6937 the same subject will be searched for."
6938   (gnus-summary-search-forward unread subject t))
6939
6940 (defun gnus-summary-article-number (&optional number-or-nil)
6941   "The article number of the article on the current line.
6942 If there isn's an article number here, then we return the current
6943 article number."
6944   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
6945     (if number-or-nil number (or number gnus-current-article))))
6946
6947 (defun gnus-summary-thread-level ()
6948   "The thread level of the article on the current line."
6949   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
6950       0))
6951
6952 (defun gnus-summary-pseudo-article ()
6953   "The thread level of the article on the current line."
6954   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
6955
6956 (defun gnus-summary-article-mark ()
6957   "The mark on the current line."
6958   (get-text-property (gnus-point-at-bol) 'gnus-mark))
6959
6960 (defun gnus-summary-subject-string ()
6961   "Return current subject string or nil if nothing."
6962   (let ((article (gnus-summary-article-number))
6963         header)
6964     (and article 
6965          (setq header (gnus-get-header-by-number article))
6966          (vectorp header)
6967          (header-subject header))))
6968
6969 (defalias 'gnus-summary-score 'gnus-summary-article-score)
6970 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
6971 (defun gnus-summary-article-score ()
6972   "Return current article score."
6973   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
6974       gnus-summary-default-score 0))
6975
6976 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
6977
6978 (defun gnus-summary-recenter ()
6979   "Center point in the summary window.
6980 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6981 displayed, no centering will be performed." 
6982   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6983   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6984   (let* ((top (cond ((< (window-height) 4) 0)
6985                     ((< (window-height) 6) 1)
6986                     (t 2)))
6987          (height (1- (window-height)))
6988          (bottom (save-excursion (goto-char (point-max))
6989                                  (forward-line (- height))
6990                                  (point)))
6991          (window (get-buffer-window (current-buffer))))
6992     (and 
6993      ;; The user has to want it,
6994      gnus-auto-center-summary 
6995      ;; the article buffer must be displayed,
6996      (get-buffer-window gnus-article-buffer)
6997      ;; Set the window start to either `bottom', which is the biggest
6998      ;; possible valid number, or the second line from the top,
6999      ;; whichever is the least.
7000      (set-window-start
7001       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7002
7003 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7004 (defun gnus-short-group-name (group &optional levels)
7005   "Collapse GROUP name LEVELS."
7006   (let* ((name "") (foreign "") (depth -1) (skip 1)
7007          (levels (or levels
7008                      (progn
7009                        (while (string-match "\\." group skip)
7010                          (setq skip (match-end 0)
7011                                depth (+ depth 1)))
7012                        depth))))
7013     (if (string-match ":" group)
7014         (setq foreign (substring group 0 (match-end 0))
7015               group (substring group (match-end 0))))
7016     (while group
7017       (if (and (string-match "\\." group) (> levels 0))
7018           (setq name (concat name (substring group 0 1))
7019                 group (substring group (match-end 0))
7020                 levels (- levels 1)
7021                 name (concat name "."))
7022         (setq name (concat foreign name group)
7023               group nil)))
7024     name))
7025
7026 (defun gnus-summary-jump-to-group (newsgroup)
7027   "Move point to NEWSGROUP in group mode buffer."
7028   ;; Keep update point of group mode buffer if visible.
7029   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7030       (save-window-excursion
7031         ;; Take care of tree window mode.
7032         (if (get-buffer-window gnus-group-buffer)
7033             (pop-to-buffer gnus-group-buffer))
7034         (gnus-group-jump-to-group newsgroup))
7035     (save-excursion
7036       ;; Take care of tree window mode.
7037       (if (get-buffer-window gnus-group-buffer)
7038           (pop-to-buffer gnus-group-buffer)
7039         (set-buffer gnus-group-buffer))
7040       (gnus-group-jump-to-group newsgroup))))
7041
7042 ;; This function returns a list of article numbers based on the
7043 ;; difference between the ranges of read articles in this group and
7044 ;; the range of active articles.
7045 (defun gnus-list-of-unread-articles (group)
7046   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7047          (active (gnus-gethash group gnus-active-hashtb))
7048          (last (cdr active))
7049          unread first nlast unread)
7050     ;; If none are read, then all are unread. 
7051     (if (not read)
7052         (setq first (car active))
7053       ;; If the range of read articles is a single range, then the
7054       ;; first unread article is the article after the last read
7055       ;; article. Sounds logical, doesn't it?
7056       (if (not (listp (cdr read)))
7057           (setq first (1+ (cdr read)))
7058         ;; `read' is a list of ranges.
7059         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7060                                 (car (car read)))) 1)
7061             (setq first 1))
7062         (while read
7063           (if first 
7064               (while (< first nlast)
7065                 (setq unread (cons first unread))
7066                 (setq first (1+ first))))
7067           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7068           (setq nlast (if (atom (car (cdr read))) 
7069                           (car (cdr read))
7070                         (car (car (cdr read)))))
7071           (setq read (cdr read)))))
7072     ;; And add the last unread articles.
7073     (while (<= first last)
7074       (setq unread (cons first unread))
7075       (setq first (1+ first)))
7076     ;; Return the list of unread articles.
7077     (nreverse unread)))
7078
7079 (defun gnus-list-of-read-articles (group)
7080   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7081         (active (gnus-gethash group gnus-active-hashtb)))
7082     (and info active
7083          (gnus-sorted-complement 
7084           (gnus-uncompress-range active) 
7085           (gnus-list-of-unread-articles group)))))
7086
7087 ;; Various summary commands
7088
7089 (defun gnus-summary-universal-argument ()
7090   "Perform any operation on all articles marked with the process mark."
7091   (interactive)
7092   (gnus-set-global-variables)
7093   (let ((articles (reverse gnus-newsgroup-processable))
7094         key func)
7095     (or articles (error "No articles marked"))
7096     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7097         (error "Undefined key"))
7098     (while articles
7099       (gnus-summary-goto-subject (car articles))
7100       (command-execute func)
7101       (gnus-summary-remove-process-mark (car articles))
7102       (setq articles (cdr articles)))))
7103
7104 (defun gnus-summary-toggle-truncation (arg)
7105   "Toggle truncation of summary lines.
7106 With arg, turn line truncation on iff arg is positive."
7107   (interactive "P")
7108   (setq truncate-lines
7109         (if (null arg) (not truncate-lines)
7110           (> (prefix-numeric-value arg) 0)))
7111   (redraw-display))
7112
7113 (defun gnus-summary-reselect-current-group (all)
7114   "Once exit and then reselect the current newsgroup.
7115 The prefix argument ALL means to select all articles."
7116   (interactive "P")
7117   (gnus-set-global-variables)
7118   (let ((current-subject (gnus-summary-article-number))
7119         (group gnus-newsgroup-name))
7120     (setq gnus-newsgroup-threads nil)
7121     (gnus-summary-exit t)
7122     ;; We have to adjust the point of group mode buffer because the
7123     ;; current point was moved to the next unread newsgroup by
7124     ;; exiting.
7125     (gnus-summary-jump-to-group group)
7126     (gnus-group-read-group all t)
7127     (gnus-summary-goto-subject current-subject)))
7128
7129 (defun gnus-summary-rescan-group (all)
7130   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7131   (interactive "P")
7132   (gnus-set-global-variables)
7133   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7134   (let ((group gnus-newsgroup-name))
7135     (gnus-summary-exit)
7136     (gnus-summary-jump-to-group group)
7137     (save-excursion
7138       (set-buffer gnus-group-buffer)
7139       (gnus-group-get-new-news-this-group 1))
7140     (gnus-summary-jump-to-group group)
7141     (gnus-group-read-group all)))
7142
7143 (defun gnus-summary-update-info ()
7144   (let* ((group gnus-newsgroup-name)
7145          (method (car (gnus-find-method-for-group group))))
7146     (if gnus-newsgroup-kill-headers
7147         (setq gnus-newsgroup-killed
7148               (gnus-compress-sequence
7149                (nconc
7150                 (gnus-set-sorted-intersection
7151                  (gnus-uncompress-range gnus-newsgroup-killed)
7152                  (setq gnus-newsgroup-unselected
7153                        (sort gnus-newsgroup-unselected '<)))
7154                 (setq gnus-newsgroup-unreads
7155                       (sort gnus-newsgroup-unreads '<))) t)))
7156     (or (listp (cdr gnus-newsgroup-killed))
7157         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7158     (let ((updated nil)
7159           (headers gnus-newsgroup-headers))
7160       (gnus-close-group group)
7161       (run-hooks 'gnus-exit-group-hook)
7162       (gnus-update-read-articles 
7163        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7164        gnus-newsgroup-marked
7165        t gnus-newsgroup-replied gnus-newsgroup-expirable
7166        gnus-newsgroup-killed gnus-newsgroup-dormant
7167        gnus-newsgroup-bookmarks 
7168        (and gnus-save-score gnus-newsgroup-scored))
7169       (and gnus-use-cross-reference
7170            (gnus-mark-xrefs-as-read 
7171             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7172       ;; Do adaptive scoring, and possibly save score files.
7173       (and gnus-newsgroup-adaptive
7174            (gnus-score-adaptive))
7175       (and gnus-use-scoring 
7176            (fboundp 'gnus-score-save)
7177            (funcall 'gnus-score-save))
7178       ;; Do not switch windows but change the buffer to work.
7179       (set-buffer gnus-group-buffer)
7180       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7181           (gnus-group-update-group group)))))
7182   
7183 (defun gnus-summary-exit (&optional temporary)
7184   "Exit reading current newsgroup, and then return to group selection mode.
7185 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7186   (interactive)
7187   (gnus-set-global-variables)
7188   (gnus-kill-save-kill-buffer)
7189   (let* ((group gnus-newsgroup-name)
7190          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7191                                                   gnus-newsgroup-name))))
7192          (mode major-mode)
7193          (method (car (gnus-find-method-for-group group)))
7194          (buf (current-buffer)))
7195     (gnus-summary-update-info) ; Make all changes in this group permanent.
7196     ;; Make sure where I was, and go to next newsgroup.
7197     (or quit-config
7198         (progn
7199           (gnus-group-jump-to-group group)
7200           (gnus-group-next-unread-group 1)))
7201     (if temporary
7202         nil                             ;Nothing to do.
7203       ;; We set all buffer-local variables to nil. It is unclear why
7204       ;; this is needed, but if we don't, buffer-local variables are
7205       ;; not garbage-collected, it seems. This would the lead to en
7206       ;; ever-growing Emacs.
7207       (set-buffer buf)
7208       (gnus-summary-clear-local-variables)
7209       ;; We clear the global counterparts of the buffer-local
7210       ;; variables as well, just to be on the safe side.
7211       (gnus-configure-windows 'group)
7212       (gnus-summary-clear-local-variables)
7213       ;; Return to group mode buffer. 
7214       (if (eq mode 'gnus-summary-mode)
7215           (gnus-kill-buffer buf))
7216       (if (get-buffer gnus-article-buffer)
7217           (bury-buffer gnus-article-buffer))
7218       (setq gnus-current-select-method gnus-select-method)
7219       (pop-to-buffer gnus-group-buffer)
7220       (if (not quit-config)
7221           (progn
7222             (gnus-group-jump-to-group group)
7223             (gnus-group-next-unread-group 1))
7224         (if (not (buffer-name (car quit-config)))
7225             (gnus-configure-windows 'group)
7226           (set-buffer (car quit-config))
7227           (and (eq major-mode 'gnus-summary-mode)
7228                (gnus-set-global-variables))
7229           (gnus-configure-windows (cdr quit-config)))))))
7230
7231 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7232 (defun gnus-summary-exit-no-update (&optional no-questions)
7233   "Quit reading current newsgroup without updating read article info."
7234   (interactive)
7235   (let* ((group gnus-newsgroup-name)
7236          (quit-config (nth 1 (assoc 'quit-config 
7237                                     (gnus-find-method-for-group group)))))
7238     (if (or no-questions
7239             gnus-expert-user
7240             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7241         (progn
7242           (gnus-close-group group)
7243           (gnus-summary-clear-local-variables)
7244           (set-buffer gnus-group-buffer)
7245           (gnus-summary-clear-local-variables)
7246           ;; Return to group selection mode.
7247           (gnus-configure-windows 'group)
7248           (if (get-buffer gnus-summary-buffer)
7249               (kill-buffer gnus-summary-buffer))
7250           (if (get-buffer gnus-article-buffer)
7251               (bury-buffer gnus-article-buffer))
7252           (if (equal (gnus-group-group-name) group)
7253               (gnus-group-next-unread-group 1))
7254           (if quit-config
7255               (progn
7256                 (if (not (buffer-name (car quit-config)))
7257                     (gnus-configure-windows 'group)
7258                   (set-buffer (car quit-config))
7259                   (and (eq major-mode 'gnus-summary-mode)
7260                        (gnus-set-global-variables))
7261                   (gnus-configure-windows (cdr quit-config)))))))))
7262
7263 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7264 (defun gnus-summary-fetch-faq (group)
7265   "Fetch the FAQ for the current group."
7266   (interactive (list gnus-newsgroup-name))
7267   (gnus-configure-windows 'summary-faq)
7268   (find-file (concat gnus-group-faq-directory group)))
7269
7270 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7271 (defun gnus-summary-describe-group (force)
7272   "Describe the current newsgroup."
7273   (interactive "P")
7274   (gnus-group-describe-group force gnus-newsgroup-name))
7275
7276 (defun gnus-summary-describe-briefly ()
7277   "Describe summary mode commands briefly."
7278   (interactive)
7279   (gnus-message 6
7280     (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")))
7281
7282 ;; Walking around group mode buffer from summary mode.
7283
7284 (defun gnus-summary-next-group (&optional no-article group backward)
7285   "Exit current newsgroup and then select next unread newsgroup.
7286 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7287 If BACKWARD, go to previous group instead."
7288   (interactive "P")
7289   (gnus-set-global-variables)
7290   (let ((ingroup gnus-newsgroup-name)
7291         (sumbuf (current-buffer))
7292         num)
7293     (gnus-summary-exit t)               ;Update all information.
7294     (if (and group
7295              (or (and (numberp (setq num (car (gnus-gethash
7296                                                group gnus-newsrc-hashtb))))
7297                       (< num 1))
7298                  (null num)))
7299         (progn
7300           (gnus-group-jump-to-group group)
7301           (setq group nil))
7302       (gnus-group-jump-to-group ingroup))
7303     (gnus-summary-search-group backward)
7304     (let ((group (or group (gnus-summary-search-group backward)))
7305           (buf gnus-summary-buffer))
7306       (if (null group)
7307           (gnus-summary-exit-no-update t)
7308         (gnus-message 5 "Selecting %s..." group)
7309         ;; We are now in group mode buffer.
7310         ;; Make sure group mode buffer point is on GROUP.
7311         (gnus-group-jump-to-group group)
7312         (if (not (eq gnus-auto-select-next 'quietly))
7313             (progn
7314               (gnus-summary-read-group group nil no-article buf)
7315               (and (string= gnus-newsgroup-name ingroup)
7316                    (bufferp sumbuf) (buffer-name sumbuf)
7317                    (progn
7318                      (set-buffer (setq gnus-summary-buffer sumbuf))
7319                      (gnus-summary-exit-no-update t))))
7320           (let ((prevgroup group))
7321             (gnus-summary-read-group group nil no-article buf)
7322             (while (and (string= gnus-newsgroup-name ingroup)
7323                         (bufferp sumbuf) 
7324                         (buffer-name sumbuf)
7325                         (not (string= prevgroup (gnus-group-group-name))))
7326               (set-buffer gnus-group-buffer)
7327               (gnus-summary-read-group 
7328                (setq prevgroup (gnus-group-group-name)) 
7329                nil no-article buf))
7330             (and (string= prevgroup (gnus-group-group-name))
7331                  ;; We have reached the final group in the group
7332                  ;; buffer.
7333                  (progn
7334                    (set-buffer sumbuf)
7335                    (gnus-summary-exit)))))))))
7336
7337 (defun gnus-summary-prev-group (no-article)
7338   "Exit current newsgroup and then select previous unread newsgroup.
7339 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7340   (interactive "P")
7341   (gnus-summary-next-group no-article nil t))
7342
7343 ;; Walking around summary lines.
7344
7345 (defun gnus-summary-first-subject (unread)
7346   "Go to the first unread subject.
7347 If UNREAD is non-nil, go to the first unread article.
7348 Returns nil if there are no unread articles."
7349   (interactive "P")
7350   (prog1
7351       (if (or (not unread)
7352               (gnus-goto-char 
7353                (text-property-any 
7354                 (point-min) (point-max) 'gnus-mark gnus-unread-mark)))
7355           t 
7356         ;; If there are no unread articles.
7357         (gnus-message 3 "No more unread articles")
7358         nil)
7359     (gnus-summary-position-cursor)))
7360
7361 (defun gnus-summary-next-subject (n &optional unread dont-display)
7362   "Go to next N'th summary line.
7363 If N is negative, go to the previous N'th subject line.
7364 If UNREAD is non-nil, only unread articles are selected.
7365 The difference between N and the actual number of steps taken is
7366 returned."
7367   (interactive "p")
7368   (let ((backward (< n 0))
7369         (n (abs n)))
7370     (while (and (> n 0)
7371                 (gnus-summary-search-forward unread nil backward))
7372       (setq n (1- n)))
7373     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7374                                (if unread " unread" "")))
7375     (or dont-display
7376         (progn
7377           (gnus-summary-recenter)
7378           (gnus-summary-position-cursor)))
7379   n))
7380
7381 (defun gnus-summary-next-unread-subject (n)
7382   "Go to next N'th unread summary line."
7383   (interactive "p")
7384   (gnus-summary-next-subject n t))
7385
7386 (defun gnus-summary-prev-subject (n &optional unread)
7387   "Go to previous N'th summary line.
7388 If optional argument UNREAD is non-nil, only unread article is selected."
7389   (interactive "p")
7390   (gnus-summary-next-subject (- n) unread))
7391
7392 (defun gnus-summary-prev-unread-subject (n)
7393   "Go to previous N'th unread summary line."
7394   (interactive "p")
7395   (gnus-summary-next-subject (- n) t))
7396
7397 (defun gnus-summary-goto-subject (article)
7398   "Go the subject line of ARTICLE."
7399   (interactive
7400    (list
7401     (string-to-int
7402      (completing-read "Article number: "
7403                       (mapcar
7404                        (lambda (headers)
7405                          (list
7406                           (int-to-string (header-number headers))))
7407                        gnus-newsgroup-headers)
7408                       nil 'require-match))))
7409   (or article (error "No article number"))
7410   (let ((b (point)))
7411     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7412                                                 'gnus-number article)))
7413         ()
7414       (gnus-summary-show-thread)
7415       ;; Skip dummy articles. 
7416       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7417           (forward-line 1))
7418       (prog1
7419           (if (not (eobp))
7420               article
7421             (goto-char b)
7422             nil)
7423         (gnus-summary-position-cursor)))))
7424
7425 ;; Walking around summary lines with displaying articles.
7426
7427 (defun gnus-summary-expand-window ()
7428   "Expand summary window to show headers full window."
7429   (interactive)
7430   (gnus-set-global-variables)
7431   (gnus-configure-windows 'summary))
7432
7433 (defun gnus-summary-display-article (article &optional all-header)
7434   "Display ARTICLE in article buffer."
7435   (gnus-set-global-variables)
7436   (if (null article)
7437       nil
7438     (gnus-article-prepare article all-header)
7439     (gnus-summary-show-thread)
7440     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7441         (progn
7442           (forward-line 1)
7443           (gnus-summary-position-cursor)))
7444     (run-hooks 'gnus-select-article-hook)
7445     (gnus-summary-recenter)
7446 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
7447 ;    (sit-for 0)
7448     (gnus-summary-goto-subject article)
7449     ;; Successfully display article.
7450     (gnus-summary-update-line)
7451     (gnus-article-set-window-start 
7452      (cdr (assq article gnus-newsgroup-bookmarks)))
7453     t))
7454
7455 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7456   "Select the current article.
7457 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7458 non-nil, the article will be re-fetched even if it already present in
7459 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7460 be displayed."
7461   (and (not pseudo) (gnus-summary-pseudo-article)
7462        (error "This is a pseudo-article."))
7463   (let ((article (or article (gnus-summary-article-number)))
7464         (all-headers (not (not all-headers))) ;Must be T or NIL.
7465         did) 
7466     (prog1
7467         (save-excursion
7468           (set-buffer gnus-summary-buffer)
7469           (if (or (null gnus-current-article)
7470                   (null gnus-article-current)
7471                   (not (eq article (cdr gnus-article-current)))
7472                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7473                   force)
7474               ;; The requested article is different from the current article.
7475               (progn
7476                 (gnus-summary-display-article article all-headers)
7477                 (setq did article))
7478             (if all-headers (gnus-article-show-all-headers))
7479             nil))
7480       (if did 
7481           (gnus-article-set-window-start 
7482            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7483
7484 (defun gnus-summary-set-current-mark (&optional current-mark)
7485   "Obsolete function."
7486   nil)
7487
7488 (defun gnus-summary-next-article (unread &optional subject backward)
7489   "Select the next article.
7490 If UNREAD, only unread articles are selected.
7491 If SUBJECT, only articles with SUBJECT are selected.
7492 If BACKWARD, the previous article is selected instead of the next."
7493   (interactive "P")
7494   (let ((opoint (point))
7495         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
7496         header)
7497     (cond
7498      ;; Is there such an article?
7499      ((gnus-summary-display-article 
7500        (gnus-summary-search-forward unread subject backward))
7501       (gnus-summary-position-cursor))
7502      ;; If not, we try the first unread, if that is wanted.
7503      ((and subject
7504            gnus-auto-select-same
7505            (gnus-summary-first-unread-article))
7506       (gnus-message 6 "Wrapped"))
7507      ;; Try to get next/previous article not displayed in this group.
7508      ((and gnus-auto-extend-newsgroup
7509            (not unread) (not subject)
7510            (setq header (gnus-more-header-forward backward)))
7511       (gnus-extend-newsgroup header backward)
7512       (let ((buffer-read-only nil))
7513         (goto-char (if backward (point-min) (point-max)))
7514         (gnus-summary-prepare-threads (list header) 0))
7515       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7516                                    gnus-newsgroup-end)))
7517      ;; Go to next/previous group.
7518      (t
7519       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7520           (gnus-summary-jump-to-group gnus-newsgroup-name))
7521       (let ((cmd (aref (this-command-keys) 0))
7522             (group 
7523              (if (eq gnus-keep-same-level 'best) 
7524                  (gnus-summary-best-group gnus-newsgroup-name)
7525                (gnus-summary-search-group backward gnus-keep-same-level))))
7526         ;; Keep just the event type of CMD.
7527         (and (listp cmd) (setq cmd (car cmd)))
7528         ;; Select next unread newsgroup automagically.
7529         (cond 
7530          ((not gnus-auto-select-next)
7531           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7532          ((eq gnus-auto-select-next 'quietly)
7533           ;; Select quietly.
7534           (if (assoc 'quit-config (gnus-find-method-for-group 
7535                                    gnus-newsgroup-name))
7536               (gnus-summary-exit)
7537             (gnus-message 7 "No more%s articles (%s)..."
7538                           (if unread " unread" "") 
7539                           (if group (concat "selecting " group)
7540                             "exiting"))
7541             (gnus-summary-next-group nil group backward)))
7542          (t
7543           (let ((keystrokes '(?\C-n ?\C-p))
7544                 key)
7545             (while (or (null key) (memq key keystrokes))
7546               (gnus-message 
7547                7 "No more%s articles%s" (if unread " unread" "")
7548                (if (and group (not (assoc 'quit-config
7549                                           (gnus-find-method-for-group 
7550                                            gnus-newsgroup-name))))
7551                    (format " (Type %s for %s [%s])"
7552                            (single-key-description cmd) group
7553                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7554                  (format " (Type %s to exit %s)"
7555                          (single-key-description cmd)
7556                          gnus-newsgroup-name)))
7557               ;; Confirm auto selection.
7558               (let* ((event (read-event)))
7559                 (setq key (if (listp event) (car event) event))
7560                 (if (member key keystrokes)
7561                     (let ((obuf (current-buffer)))
7562                       (switch-to-buffer gnus-group-buffer)
7563                       (gnus-group-jump-to-group group)
7564                       (execute-kbd-macro (char-to-string key))
7565                       (setq group (gnus-group-group-name))
7566                       (switch-to-buffer obuf)))))
7567             (if (eq key cmd)
7568                 (if (or (not group) (assoc 'quit-config
7569                                            (gnus-find-method-for-group
7570                                             gnus-newsgroup-name)))
7571                     (gnus-summary-exit)
7572                   (gnus-summary-next-group nil group backward))
7573               (setq unread-command-events (list key)))))))))))
7574
7575 (defun gnus-summary-next-unread-article ()
7576   "Select unread article after current one."
7577   (interactive)
7578   (gnus-summary-next-article t (and gnus-auto-select-same
7579                                     (gnus-summary-subject-string))))
7580
7581 (defun gnus-summary-prev-article (unread &optional subject)
7582   "Select the article after the current one.
7583 If UNREAD is non-nil, only unread articles are selected."
7584   (interactive "P")
7585   (gnus-summary-next-article unread subject t))
7586
7587 (defun gnus-summary-prev-unread-article ()
7588   "Select unred article before current one."
7589   (interactive)
7590   (gnus-summary-prev-article t (and gnus-auto-select-same
7591                                     (gnus-summary-subject-string))))
7592
7593 (defun gnus-summary-next-page (lines &optional circular)
7594   "Show next page of selected article.
7595 If end of article, select next article.
7596 Argument LINES specifies lines to be scrolled up.
7597 If CIRCULAR is non-nil, go to the start of the article instead of 
7598 instead of selecting the next article when reaching the end of the
7599 current article." 
7600   (interactive "P")
7601   (setq gnus-summary-buffer (current-buffer))
7602   (gnus-set-global-variables)
7603   (let ((article (gnus-summary-article-number))
7604         (endp nil))
7605     (gnus-configure-windows 'article)
7606     (if (or (null gnus-current-article)
7607             (null gnus-article-current)
7608             (/= article (cdr gnus-article-current))
7609             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7610         ;; Selected subject is different from current article's.
7611         (gnus-summary-display-article article)
7612       (gnus-eval-in-buffer-window
7613        gnus-article-buffer
7614        (setq endp (gnus-article-next-page lines)))
7615       (if endp
7616           (cond (circular
7617                  (gnus-summary-beginning-of-article))
7618                 (lines
7619                  (gnus-message 3 "End of message"))
7620                 ((null lines)
7621                  (gnus-summary-next-unread-article)))))
7622     (gnus-summary-recenter)
7623     (gnus-summary-position-cursor)))
7624
7625 (defun gnus-summary-prev-page (lines)
7626   "Show previous page of selected article.
7627 Argument LINES specifies lines to be scrolled down."
7628   (interactive "P")
7629   (gnus-set-global-variables)
7630   (let ((article (gnus-summary-article-number)))
7631     (gnus-configure-windows 'article)
7632     (if (or (null gnus-current-article)
7633             (null gnus-article-current)
7634             (/= article (cdr gnus-article-current))
7635             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7636         ;; Selected subject is different from current article's.
7637         (gnus-summary-display-article article)
7638       (gnus-summary-recenter)
7639       (gnus-eval-in-buffer-window gnus-article-buffer
7640         (gnus-article-prev-page lines))))
7641   (gnus-summary-position-cursor))
7642
7643 (defun gnus-summary-scroll-up (lines)
7644   "Scroll up (or down) one line current article.
7645 Argument LINES specifies lines to be scrolled up (or down if negative)."
7646   (interactive "p")
7647   (gnus-set-global-variables)
7648   (gnus-configure-windows 'article)
7649   (or (gnus-summary-select-article nil nil 'pseudo)
7650       (gnus-eval-in-buffer-window 
7651        gnus-article-buffer
7652        (cond ((> lines 0)
7653               (if (gnus-article-next-page lines)
7654                   (gnus-message 3 "End of message")))
7655              ((< lines 0)
7656               (gnus-article-prev-page (- lines))))))
7657   (gnus-summary-recenter)
7658   (gnus-summary-position-cursor))
7659
7660 (defun gnus-summary-next-same-subject ()
7661   "Select next article which has the same subject as current one."
7662   (interactive)
7663   (gnus-set-global-variables)
7664   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7665
7666 (defun gnus-summary-prev-same-subject ()
7667   "Select previous article which has the same subject as current one."
7668   (interactive)
7669   (gnus-set-global-variables)
7670   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7671
7672 (defun gnus-summary-next-unread-same-subject ()
7673   "Select next unread article which has the same subject as current one."
7674   (interactive)
7675   (gnus-set-global-variables)
7676   (gnus-summary-next-article t (gnus-summary-subject-string)))
7677
7678 (defun gnus-summary-prev-unread-same-subject ()
7679   "Select previous unread article which has the same subject as current one."
7680   (interactive)
7681   (gnus-set-global-variables)
7682   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7683
7684 (defun gnus-summary-first-unread-article ()
7685   "Select the first unread article. 
7686 Return nil if there are no unread articles."
7687   (interactive)
7688   (gnus-set-global-variables)
7689   (prog1
7690       (if (gnus-summary-first-subject t)
7691           (gnus-summary-display-article (gnus-summary-article-number)))
7692     (gnus-summary-position-cursor)))
7693
7694 (defun gnus-summary-best-unread-article ()
7695   "Select the unread article with the highest score."
7696   (interactive)
7697   (gnus-set-global-variables)
7698   (let ((scored gnus-newsgroup-scored)
7699         (best -1000000)
7700         article art)
7701     (while scored
7702       (or (> best (cdr (car scored)))
7703           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7704                (not (memq art gnus-newsgroup-marked))
7705                (not (memq art gnus-newsgroup-dormant))
7706                (if (= best (cdr (car scored)))
7707                    (setq article (min art article))
7708                  (setq article art)
7709                  (setq best (cdr (car scored))))))
7710       (setq scored (cdr scored)))
7711     (if article 
7712         (gnus-summary-goto-article article)
7713       (gnus-summary-first-unread-article))
7714     (gnus-summary-position-cursor)))
7715
7716 (defun gnus-summary-goto-article (article &optional all-headers)
7717   "Fetch ARTICLE and display it if it exists.
7718 If ALL-HEADERS is non-nil, no header lines are hidden."
7719   (interactive
7720    (list
7721     (string-to-int
7722      (completing-read 
7723       "Article number: "
7724       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7725               gnus-newsgroup-headers) 
7726       nil 'require-match))))
7727   (prog1
7728       (and (gnus-summary-goto-subject article)
7729            (gnus-summary-display-article article all-headers))
7730     (gnus-summary-position-cursor)))
7731
7732 (defun gnus-summary-goto-last-article ()
7733   "Go to the last article."
7734   (interactive)
7735   (prog1
7736       (and gnus-last-article
7737            (gnus-summary-goto-article gnus-last-article))
7738     (gnus-summary-position-cursor)))
7739
7740 (defun gnus-summary-pop-article (number)
7741   "Pop one article off the history and go to the previous.
7742 NUMBER articles will be popped off."
7743   (interactive "p")
7744   (let (to)
7745     (setq gnus-newsgroup-history
7746           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7747     (if to
7748         (gnus-summary-goto-article (car to))
7749       (error "Article history empty")))
7750   (gnus-summary-position-cursor))
7751
7752 ;; Summary article oriented commands
7753
7754 (defun gnus-summary-refer-parent-article (n)
7755   "Refer parent article N times.
7756 The difference between N and the number of articles fetched is returned."
7757   (interactive "p")
7758   (gnus-set-global-variables)
7759   (while 
7760       (and 
7761        (> n 0)
7762        (let ((ref (header-references (gnus-get-header-by-number
7763                                       (gnus-summary-article-number)))))
7764          (if (and ref (not (equal ref ""))
7765                   (string-match "<[^<>]*>[ \t]*$" ref))
7766              (gnus-summary-refer-article 
7767               (substring ref (match-beginning 0) (match-end 0))))))
7768     (setq n (1- n)))
7769   (or (zerop n) 
7770       (gnus-message 1 "No references in article or expired article."))
7771   (gnus-summary-position-cursor)
7772   n)
7773     
7774 (defun gnus-summary-refer-article (message-id)
7775   "Refer article specified by MESSAGE-ID.
7776 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7777   (interactive "sMessage-ID: ")
7778   (if (or (not (stringp message-id))
7779           (zerop (length message-id)))
7780       ()
7781     ;; Construct the correct Message-ID if necessary.
7782     ;; Suggested by tale@pawl.rpi.edu.
7783     (or (string-match "^<" message-id)
7784         (setq message-id (concat "<" message-id)))
7785     (or (string-match ">$" message-id)
7786         (setq message-id (concat message-id ">")))
7787     (let ((header (car (gnus-gethash (downcase message-id)
7788                                      gnus-newsgroup-dependencies))))
7789       (if header
7790           (or (gnus-summary-goto-article (header-number header))
7791               ;; The header has been read, but the article had been
7792               ;; expunged, so we insert it again.
7793               (progn
7794                 (gnus-summary-insert-line
7795                  nil header 0 nil gnus-read-mark nil nil
7796                  (header-subject header))
7797                 (forward-line -1)
7798                 (header-number header)))
7799         (let ((gnus-override-method gnus-refer-article-method)
7800               (gnus-ancient-mark gnus-read-mark)
7801               number)
7802           (and gnus-refer-article-method
7803                (or (gnus-server-opened gnus-refer-article-method)
7804                    (gnus-open-server gnus-refer-article-method)))
7805           (if (gnus-article-prepare 
7806                message-id nil (gnus-read-header message-id))
7807               (progn
7808                 (setq number (header-number gnus-current-headers))
7809                 (gnus-rebuild-thread message-id)
7810                 (gnus-summary-goto-subject number)
7811                 (gnus-article-set-window-start 
7812                  (cdr (assq number gnus-newsgroup-bookmarks)))
7813                 message-id)
7814             (gnus-message 1 "No such references")
7815             nil))))))
7816
7817 (defun gnus-summary-enter-digest-group ()
7818   "Enter a digest group based on the current article."
7819   (interactive)
7820   (gnus-set-global-variables)
7821   (gnus-summary-select-article)
7822   ;; We do not want a narrowed article.
7823   (gnus-summary-stop-page-breaking)
7824   (let ((name (format "%s-%d" 
7825                       (gnus-group-prefixed-name 
7826                        gnus-newsgroup-name (list 'nndoc "")) 
7827                       gnus-current-article))
7828         (buf (current-buffer)))
7829     (if (gnus-group-read-ephemeral-group 
7830          name (list 'nndoc name
7831                     (list 'nndoc-address (get-buffer gnus-article-buffer))
7832                     '(nndoc-article-type digest))
7833          t)
7834         ()
7835       (switch-to-buffer buf)
7836       (gnus-set-global-variables)
7837       (gnus-configure-windows 'summary)
7838       (gnus-message 3 "Article not a digest?"))))
7839
7840 (defun gnus-summary-isearch-article ()
7841   "Do incremental search forward on current article."
7842   (interactive)
7843   (gnus-set-global-variables)
7844   (gnus-summary-select-article)
7845   (gnus-eval-in-buffer-window 
7846    gnus-article-buffer (isearch-forward)))
7847
7848 (defun gnus-summary-search-article-forward (regexp)
7849   "Search for an article containing REGEXP forward.
7850 gnus-select-article-hook is not called during the search."
7851   (interactive
7852    (list (read-string
7853           (concat "Search forward (regexp): "
7854                   (if gnus-last-search-regexp
7855                       (concat "(default " gnus-last-search-regexp ") "))))))
7856   (gnus-set-global-variables)
7857   (if (string-equal regexp "")
7858       (setq regexp (or gnus-last-search-regexp ""))
7859     (setq gnus-last-search-regexp regexp))
7860   (if (gnus-summary-search-article regexp nil)
7861       (gnus-eval-in-buffer-window 
7862        gnus-article-buffer
7863        (recenter 0))
7864     (error "Search failed: \"%s\"" regexp)))
7865
7866 (defun gnus-summary-search-article-backward (regexp)
7867   "Search for an article containing REGEXP backward.
7868 gnus-select-article-hook is not called during the search."
7869   (interactive
7870    (list (read-string
7871           (concat "Search backward (regexp): "
7872                   (if gnus-last-search-regexp
7873                       (concat "(default " gnus-last-search-regexp ") "))))))
7874   (gnus-set-global-variables)
7875   (if (string-equal regexp "")
7876       (setq regexp (or gnus-last-search-regexp ""))
7877     (setq gnus-last-search-regexp regexp))
7878   (if (gnus-summary-search-article regexp t)
7879       (gnus-eval-in-buffer-window
7880        gnus-article-buffer
7881        (recenter 0))
7882     (error "Search failed: \"%s\"" regexp)))
7883
7884 (defun gnus-summary-search-article (regexp &optional backward)
7885   "Search for an article containing REGEXP.
7886 Optional argument BACKWARD means do search for backward.
7887 gnus-select-article-hook is not called during the search."
7888   (let ((gnus-select-article-hook nil)  ;Disable hook.
7889         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7890         (re-search
7891          (if backward
7892              (function re-search-backward) (function re-search-forward)))
7893         (found nil)
7894         (last nil))
7895     ;; Hidden thread subtrees must be searched for ,too.
7896     (gnus-summary-show-all-threads)
7897     (if (eobp) (forward-line -1))
7898     ;; First of all, search current article.
7899     ;; We don't want to read article again from NNTP server nor reset
7900     ;; current point.
7901     (gnus-summary-select-article)
7902     (gnus-message 9 "Searching article: %d..." gnus-current-article)
7903     (setq last gnus-current-article)
7904     (gnus-eval-in-buffer-window gnus-article-buffer
7905       (save-restriction
7906         (widen)
7907         ;; Begin search from current point.
7908         (setq found (funcall re-search regexp nil t))))
7909     ;; Then search next articles.
7910     (while (and (not found)
7911                 (gnus-summary-display-article 
7912                  (gnus-summary-search-subject backward nil nil)))
7913       (gnus-message 9 "Searching article: %d..." gnus-current-article)
7914       (gnus-eval-in-buffer-window gnus-article-buffer
7915         (save-restriction
7916           (widen)
7917           (goto-char (if backward (point-max) (point-min)))
7918           (setq found (funcall re-search regexp nil t)))))
7919     (message "")
7920     ;; Adjust article pointer.
7921     (or (eq last gnus-current-article)
7922         (setq gnus-last-article last))
7923     ;; Return T if found such article.
7924     found))
7925
7926 (defun gnus-summary-execute-command (header regexp command &optional backward)
7927   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7928 If HEADER is an empty string (or nil), the match is done on the entire
7929 article. If BACKWARD (the prefix) is non-nil, search backward instead."
7930   (interactive
7931    (list (let ((completion-ignore-case t))
7932            (completing-read 
7933             "Header name: "
7934             (mapcar (lambda (string) (list string))
7935                     '("Number" "Subject" "From" "Lines" "Date"
7936                       "Message-ID" "Xref" "References"))
7937             nil 'require-match))
7938          (read-string "Regexp: ")
7939          (read-key-sequence "Command: ")
7940          current-prefix-arg))
7941   (gnus-set-global-variables)
7942   ;; Hidden thread subtrees must be searched as well.
7943   (gnus-summary-show-all-threads)
7944   ;; We don't want to change current point nor window configuration.
7945   (save-excursion
7946     (save-window-excursion
7947       (gnus-message 6 "Executing %s..." (key-description command))
7948       ;; We'd like to execute COMMAND interactively so as to give arguments.
7949       (gnus-execute header regexp
7950                     (` (lambda ()
7951                          (call-interactively '(, (key-binding command)))))
7952                     backward)
7953       (gnus-message 6 "Executing %s...done" (key-description command)))))
7954
7955 (defun gnus-summary-beginning-of-article ()
7956   "Scroll the article back to the beginning."
7957   (interactive)
7958   (gnus-set-global-variables)
7959   (gnus-summary-select-article)
7960   (gnus-eval-in-buffer-window
7961    gnus-article-buffer
7962    (widen)
7963    (goto-char (point-min))
7964    (and gnus-break-pages (gnus-narrow-to-page))))
7965
7966 (defun gnus-summary-end-of-article ()
7967   "Scroll to the end of the article."
7968   (interactive)
7969   (gnus-set-global-variables)
7970   (gnus-summary-select-article)
7971   (gnus-eval-in-buffer-window 
7972    gnus-article-buffer
7973    (widen)
7974    (goto-char (point-max))
7975    (and gnus-break-pages (gnus-narrow-to-page))))
7976
7977 (defun gnus-summary-show-article ()
7978   "Force re-fetching of the current article."
7979   (interactive)
7980   (gnus-set-global-variables)
7981   (gnus-summary-select-article gnus-have-all-headers t))
7982
7983 (defun gnus-summary-toggle-header (arg)
7984   "Show the headers if they are hidden, or hide them if they are shown.
7985 If ARG is a positive number, show the entire header.
7986 If ARG is a negative number, hide the unwanted header lines."
7987   (interactive "P")
7988   (gnus-set-global-variables)
7989   (save-excursion
7990     (set-buffer gnus-article-buffer)
7991     (let ((buffer-read-only nil))
7992       (if (numberp arg) 
7993           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
7994                                                 gnus-hidden-properties)
7995             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
7996         (if (text-property-any (point-min) (point-max) 'invisible t)
7997             (remove-text-properties (point-min) (point-max)
7998                                     gnus-hidden-properties)
7999           (let ((gnus-have-all-headers nil))
8000             (run-hooks 'gnus-article-display-hook))))
8001       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8002
8003 (defun gnus-summary-show-all-headers ()
8004   "Make all header lines visible."
8005   (interactive)
8006   (gnus-set-global-variables)
8007   (gnus-article-show-all-headers))
8008
8009 (defun gnus-summary-toggle-mime (arg)
8010   "Toggle MIME processing.
8011 If ARG is a positive number, turn MIME processing on."
8012   (interactive "P")
8013   (gnus-set-global-variables)
8014   (setq gnus-show-mime
8015         (if (null arg) (not gnus-show-mime)
8016           (> (prefix-numeric-value arg) 0)))
8017   (gnus-summary-select-article t 'force))
8018
8019 (defun gnus-summary-caesar-message (rotnum)
8020   "Caesar rotates all letters of current message by 13/47 places.
8021 With prefix arg, specifies the number of places to rotate each letter forward.
8022 Caesar rotates Japanese letters by 47 places in any case."
8023   (interactive "P")
8024   (gnus-set-global-variables)
8025   (gnus-summary-select-article)
8026   (let ((mail-header-separator "")) ; !!! Is this necessary?
8027     (gnus-overload-functions)
8028     (gnus-eval-in-buffer-window 
8029      gnus-article-buffer
8030      (save-restriction
8031        (widen)
8032        ;; We don't want to jump to the beginning of the message.
8033        ;; `save-excursion' does not do its job.
8034        (move-to-window-line 0)
8035        (let ((last (point)))
8036          (news-caesar-buffer-body rotnum)
8037          (goto-char last)
8038          (recenter 0))))))
8039
8040 (defun gnus-summary-stop-page-breaking ()
8041   "Stop page breaking in the current article."
8042   (interactive)
8043   (gnus-set-global-variables)
8044   (gnus-summary-select-article)
8045   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8046
8047 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8048
8049 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8050   "Move the current article to a different newsgroup.
8051 If N is a positive number, move the N next articles.
8052 If N is a negative number, move the N previous articles.
8053 If N is nil and any articles have been marked with the process mark,
8054 move those articles instead.
8055 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8056 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8057 re-spool using this method.
8058 For this function to work, both the current newsgroup and the
8059 newsgroup that you want to move to have to support the `request-move'
8060 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8061   (interactive "P")
8062   (gnus-set-global-variables)
8063   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8064       (error "The current newsgroup does not support article moving"))
8065   (let ((articles (gnus-summary-work-articles n))
8066         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8067         art-group)
8068     (if (and (not to-newsgroup) (not select-method))
8069         (setq to-newsgroup
8070               (completing-read 
8071                (format "Where do you want to move %s? %s"
8072                        (if (> (length articles) 1)
8073                            (format "these %d articles" (length articles))
8074                          "this article")
8075                        (if gnus-current-move-group
8076                            (format "(%s default) " gnus-current-move-group)
8077                          ""))
8078                gnus-active-hashtb nil nil prefix)))
8079     (if to-newsgroup
8080         (progn
8081           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8082               (setq to-newsgroup (or gnus-current-move-group "")))
8083           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8084               (gnus-activate-newsgroup to-newsgroup)
8085               (error "No such group: %s" to-newsgroup))
8086           (setq gnus-current-move-group to-newsgroup)))
8087     (or (gnus-check-backend-function 'request-accept-article 
8088                                      (or select-method to-newsgroup))
8089         (error "%s does not support article moving" to-newsgroup))
8090     (gnus-message 6 "Moving to %s: %s..." 
8091                   (or select-method to-newsgroup) articles)
8092     (while articles
8093       (if (setq art-group
8094                 (gnus-request-move-article 
8095                  (car articles)                   ; Article to move
8096                  gnus-newsgroup-name              ; From newsgrouo
8097                  (nth 1 (gnus-find-method-for-group 
8098                          gnus-newsgroup-name))    ; Server
8099                  (list 'gnus-request-accept-article 
8100                        (if select-method
8101                            (list 'quote select-method)
8102                          to-newsgroup)
8103                        (not (cdr articles)))     ; Accept form
8104                  (not (cdr articles))))          ; Only save nov last time
8105           (let* ((buffer-read-only nil)
8106                  (entry 
8107                   (or
8108                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8109                    (gnus-gethash 
8110                     (gnus-group-prefixed-name 
8111                      (car art-group) 
8112                      (if select-method (list select-method "")
8113                        (gnus-find-method-for-group to-newsgroup)))
8114                     gnus-newsrc-hashtb)))
8115                  (info (nth 2 entry))
8116                  (article (car articles))
8117                  (marked (nth 3 info)))
8118             (gnus-summary-goto-subject article)
8119             (beginning-of-line)
8120             (delete-region (point)
8121                            (progn (forward-line 1) (point)))
8122             (if (not (memq article gnus-newsgroup-unreads))
8123                 (setcar (cdr (cdr info))
8124                         (gnus-add-to-range (nth 2 info) 
8125                                            (list (cdr art-group)))))
8126             ;; Copy any marks over to the new group.
8127             (let ((marks '((tick . gnus-newsgroup-marked)
8128                            (dormant . gnus-newsgroup-dormant)
8129                            (expire . gnus-newsgroup-expirable)
8130                            (bookmark . gnus-newsgroup-bookmarks)
8131                         ;   (score . gnus-newsgroup-scored)
8132                            (reply . gnus-newsgroup-replied)))
8133                   (to-article (cdr art-group)))
8134               (while marks
8135                 (if (memq article (symbol-value (cdr (car marks))))
8136                     (gnus-add-marked-articles 
8137                      (car info) (car (car marks)) (list to-article) info))
8138                 (setq marks (cdr marks))))
8139             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8140             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8141             (setq gnus-newsgroup-dormant
8142                   (delq article gnus-newsgroup-dormant)))
8143         (gnus-message 1 "Couldn't move article %s" (car articles)))
8144       (gnus-summary-remove-process-mark (car articles))
8145       (setq articles (cdr articles)))))
8146
8147 (defun gnus-summary-respool-article (n &optional respool-method)
8148   "Respool the current article.
8149 The article will be squeezed through the mail spooling process again,
8150 which means that it will be put in some mail newsgroup or other
8151 depending on `nnmail-split-methods'.
8152 If N is a positive number, respool the N next articles.
8153 If N is a negative number, respool the N previous articles.
8154 If N is nil and any articles have been marked with the process mark,
8155 respool those articles instead.
8156
8157 Respooling can be done both from mail groups and \"real\" newsgroups.
8158 In the former case, the articles in question will be moved from the
8159 current group into whatever groups they are destined to.  In the
8160 latter case, they will be copied into the relevant groups."
8161   (interactive "P")
8162   (gnus-set-global-variables)
8163   (let ((respool-methods (gnus-methods-using 'respool))
8164         (methname 
8165          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8166     (or respool-method
8167         (setq respool-method
8168               (completing-read
8169                "What method do you want to use when respooling? "
8170                respool-methods nil t methname)))
8171     (or (string= respool-method "")
8172         (if (assoc (symbol-name
8173                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8174                    respool-methods)
8175             (gnus-summary-move-article n nil (intern respool-method))
8176           (gnus-summary-copy-article n nil (intern respool-method))))))
8177
8178 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8179 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8180   "Move the current article to a different newsgroup.
8181 If N is a positive number, move the N next articles.
8182 If N is a negative number, move the N previous articles.
8183 If N is nil and any articles have been marked with the process mark,
8184 move those articles instead.
8185 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8186 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8187 re-spool using this method.
8188 For this function to work, the newsgroup that you want to move to have
8189 to support the `request-move' and `request-accept'
8190 functions. (Ie. mail newsgroups at present.)"
8191   (interactive "P")
8192   (gnus-set-global-variables)
8193   (let ((articles (gnus-summary-work-articles n))
8194         (copy-buf (get-buffer-create "*copy work*"))
8195         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8196         art-group)
8197     (buffer-disable-undo copy-buf)
8198     (if (and (not to-newsgroup) (not select-method))
8199         (setq to-newsgroup
8200               (completing-read 
8201                (format "Where do you want to copy %s? %s"
8202                        (if (> (length articles) 1)
8203                            (format "these %d articles" (length articles))
8204                          "this article")
8205                        (if gnus-current-move-group
8206                            (format "(%s default) " gnus-current-move-group)
8207                          ""))
8208                gnus-active-hashtb nil nil prefix)))
8209     (if to-newsgroup
8210         (progn
8211           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8212               (setq to-newsgroup (or gnus-current-move-group "")))
8213           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8214               (gnus-activate-newsgroup to-newsgroup)
8215               (error "No such group: %s" to-newsgroup))
8216           (setq gnus-current-move-group to-newsgroup)))
8217     (or (gnus-check-backend-function 'request-accept-article 
8218                                      (or select-method to-newsgroup))
8219         (error "%s does not support article copying" to-newsgroup))
8220     (gnus-message 6 "Copying to %s: %s..." 
8221                   (or select-method to-newsgroup) articles)
8222     (while articles
8223       (if (setq art-group
8224                 (save-excursion
8225                   (set-buffer copy-buf)
8226                   (gnus-request-article-this-buffer
8227                    (car articles) gnus-newsgroup-name)
8228                   (gnus-request-accept-article
8229                    (if select-method (quote select-method) to-newsgroup)
8230                    (not (cdr articles)))))
8231           (let* ((entry 
8232                   (or
8233                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8234                    (gnus-gethash 
8235                     (gnus-group-prefixed-name 
8236                      (car art-group) 
8237                      (if select-method (list select-method "")
8238                        (gnus-find-method-for-group to-newsgroup)))
8239                     gnus-newsrc-hashtb)))
8240                  (info (nth 2 entry))
8241                  (article (car articles))
8242                  (marked (nth 3 info)))
8243             (if (not (memq article gnus-newsgroup-unreads))
8244                 (setcar (cdr (cdr info))
8245                         (gnus-add-to-range (nth 2 info) 
8246                                            (list (cdr art-group)))))
8247             ;; Copy any marks over to the new group.
8248             (let ((marks '((tick . gnus-newsgroup-marked)
8249                            (dormant . gnus-newsgroup-dormant)
8250                            (expire . gnus-newsgroup-expirable)
8251                            (bookmark . gnus-newsgroup-bookmarks)
8252                         ;   (score . gnus-newsgroup-scored)
8253                            (reply . gnus-newsgroup-replied)))
8254                   (to-article (cdr art-group)))
8255               (while marks
8256                 (if (memq article (symbol-value (cdr (car marks))))
8257                     (gnus-add-marked-articles 
8258                      (car info) (car (car marks)) (list to-article) info))
8259                 (setq marks (cdr marks)))))
8260         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8261       (gnus-summary-remove-process-mark (car articles))
8262       (setq articles (cdr articles)))
8263     (kill-buffer copy-buf)))
8264
8265 (defun gnus-summary-import-article (file)
8266   "Import a random file into a mail newsgroup."
8267   (interactive "fImport file: ")
8268   (let ((group gnus-newsgroup-name)
8269         atts)
8270     (or (gnus-check-backend-function 'request-accept-article group)
8271         (error "%s does not support article importing" group))
8272     (or (file-readable-p file)
8273         (not (file-regular-p file))
8274         (error "Can't read %s" file))
8275     (save-excursion
8276       (set-buffer (get-buffer-create " *import file*"))
8277       (buffer-disable-undo (current-buffer))
8278       (erase-buffer)
8279       (insert-file-contents file)
8280       (goto-char (point-min))
8281       (if (nnheader-article-p)
8282           ()
8283         (setq atts (file-attributes file))
8284         (insert "From: " (read-string "From: ") "\n"
8285                 "Subject: " (read-string "Subject: ") "\n"
8286                 "Date: " (current-time-string (nth 5 atts)) "\n"
8287                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8288       (gnus-request-accept-article group t)
8289       (kill-buffer (current-buffer)))))
8290
8291 (defun gnus-summary-expire-articles ()
8292   "Expire all articles that are marked as expirable in the current group."
8293   (interactive)
8294   (if (and gnus-newsgroup-expirable
8295            (gnus-check-backend-function 
8296             'request-expire-articles gnus-newsgroup-name))
8297       (let ((expirable gnus-newsgroup-expirable))
8298         ;; The list of articles that weren't expired is returned.
8299         (setq gnus-newsgroup-expirable 
8300               (gnus-request-expire-articles gnus-newsgroup-expirable
8301                                             gnus-newsgroup-name))
8302         ;; We go through the old list of expirable, and mark all
8303         ;; really expired articles as non-existant.
8304         (while expirable
8305           (or (memq (car expirable) gnus-newsgroup-expirable)
8306               (gnus-summary-mark-as-read (car expirable) "%"))
8307           (setq expirable (cdr expirable))))))
8308
8309 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8310 (defun gnus-summary-delete-article (n)
8311   "Delete the N next (mail) articles.
8312 This command actually deletes articles. This is not a marking
8313 command. The article will disappear forever from you life, never to
8314 return. 
8315 If N is negative, delete backwards.
8316 If N is nil and articles have been marked with the process mark,
8317 delete these instead."
8318   (interactive "P")
8319   (or (gnus-check-backend-function 'request-expire-articles 
8320                                    gnus-newsgroup-name)
8321       (error "The current newsgroup does not support article deletion."))
8322   ;; Compute the list of articles to delete.
8323   (let ((articles (gnus-summary-work-articles n))
8324         not-deleted)
8325     (if (and gnus-novice-user
8326              (not (gnus-y-or-n-p 
8327                    (format "Do you really want to delete %s forever? "
8328                            (if (> (length articles) 1) "these articles"
8329                              "this article")))))
8330         ()
8331       ;; Delete the articles.
8332       (setq not-deleted (gnus-request-expire-articles 
8333                          articles gnus-newsgroup-name 'force))
8334       (while articles
8335         (gnus-summary-remove-process-mark (car articles))       
8336         ;; The backend might not have been able to delete the article
8337         ;; after all.  
8338         (or (memq (car articles) not-deleted)
8339             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8340         (setq articles (cdr articles))))
8341     (gnus-summary-position-cursor)
8342     not-deleted))
8343
8344 (defun gnus-summary-edit-article ()
8345   "Enter into a buffer and edit the current article.
8346 This will have permanent effect only in mail groups."
8347   (interactive)
8348   (or (gnus-check-backend-function 
8349        'request-replace-article gnus-newsgroup-name)
8350       (error "The current newsgroup does not support article editing."))
8351   (gnus-summary-select-article t)
8352   (other-window 1)
8353   (gnus-message 6 "C-c C-c to end edits")
8354   (setq buffer-read-only nil)
8355   (text-mode)
8356   (use-local-map (copy-keymap (current-local-map)))
8357   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8358   (goto-char (point-min))
8359   (search-forward "\n\n" nil t))
8360
8361 (defun gnus-summary-edit-article-done ()
8362   "Make edits to the current article permanent."
8363   (interactive)
8364   (if (not (gnus-request-replace-article 
8365             (cdr gnus-article-current) (car gnus-article-current) 
8366             (current-buffer)))
8367       (error "Couldn't replace article.")
8368     (gnus-article-mode)
8369     (use-local-map gnus-article-mode-map)
8370     (setq buffer-read-only t)
8371     (pop-to-buffer gnus-summary-buffer)))      
8372
8373 (defun gnus-summary-fancy-query ()
8374   "Query where the fancy respool algorithm would put this article."
8375   (interactive)
8376   (gnus-summary-select-article)
8377   (save-excursion
8378     (set-buffer gnus-article-buffer)
8379     (save-restriction
8380       (goto-char (point-min))
8381       (search-forward "\n\n")
8382       (narrow-to-region (point-min) (point))
8383       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8384
8385 ;; Summary score commands.
8386
8387 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8388
8389 (defun gnus-summary-raise-score (n)
8390   "Raise the score of the current article by N."
8391   (interactive "p")
8392   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8393
8394 (defun gnus-summary-lower-score (n)
8395   "Lower the score of the current article by N."
8396   (interactive "p")
8397   (gnus-summary-raise-score (- n)))
8398
8399 (defun gnus-summary-set-score (n)
8400   "Set the score of the current article to N."
8401   (interactive "p")
8402   ;; Skip dummy header line.
8403   (save-excursion
8404     (gnus-summary-show-thread)
8405     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8406         (forward-line 1))
8407     (let ((buffer-read-only nil))
8408       ;; Set score.
8409       (gnus-summary-update-mark
8410        (if (= n (or gnus-summary-default-score 0)) ? 
8411          (if (< n (or gnus-summary-default-score 0)) 
8412              gnus-score-below-mark gnus-score-over-mark)) 'score))
8413     (let* ((article (gnus-summary-article-number))
8414            (score (assq article gnus-newsgroup-scored)))
8415       (if score (setcdr score n)
8416         (setq gnus-newsgroup-scored 
8417               (cons (cons article n) gnus-newsgroup-scored))))
8418     (gnus-summary-update-line)))
8419
8420 (defun gnus-summary-current-score ()
8421   "Return the score of the current article."
8422   (interactive)
8423   (message "%s" (gnus-summary-article-score)))
8424
8425 ;; Summary marking commands.
8426
8427 (defun gnus-summary-raise-same-subject-and-select (score)
8428   "Raise articles which has the same subject with SCORE and select the next."
8429   (interactive "p")
8430   (let ((subject (gnus-summary-subject-string)))
8431     (gnus-summary-raise-score score)
8432     (while (gnus-summary-search-subject nil nil subject)
8433       (gnus-summary-raise-score score))
8434     (gnus-summary-next-article t)))
8435
8436 (defun gnus-summary-raise-same-subject (score)
8437   "Raise articles which has the same subject with SCORE."
8438   (interactive "p")
8439   (let ((subject (gnus-summary-subject-string)))
8440     (gnus-summary-raise-score score)
8441     (while (gnus-summary-search-subject nil nil subject)
8442       (gnus-summary-raise-score score))
8443     (gnus-summary-next-subject 1 t)))
8444
8445 (defun gnus-score-default (level)
8446   (if level (prefix-numeric-value level) 
8447     gnus-score-interactive-default-score))
8448
8449 (defun gnus-summary-raise-thread (score)
8450   "Raise articles under current thread with SCORE."
8451   (interactive "P")
8452   (setq score (1- (gnus-score-default score)))
8453   (let (e)
8454     (save-excursion
8455       (let ((level (gnus-summary-thread-level)))
8456         (gnus-summary-raise-score score)
8457         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8458                     (> (gnus-summary-thread-level) level))
8459           (gnus-summary-raise-score score))
8460         (setq e (point))))
8461     (let ((gnus-summary-check-current t))
8462       (or (zerop (gnus-summary-next-subject 1 t))
8463           (goto-char e))))
8464   (gnus-summary-recenter)
8465   (gnus-summary-position-cursor)
8466   (gnus-set-mode-line 'summary))
8467
8468 (defun gnus-summary-lower-same-subject-and-select (score)
8469   "Raise articles which has the same subject with SCORE and select the next."
8470   (interactive "p")
8471   (gnus-summary-raise-same-subject-and-select (- score)))
8472
8473 (defun gnus-summary-lower-same-subject (score)
8474   "Raise articles which has the same subject with SCORE."
8475   (interactive "p")
8476   (gnus-summary-raise-same-subject (- score)))
8477
8478 (defun gnus-summary-lower-thread (score)
8479   "Raise articles under current thread with SCORE."
8480   (interactive "P")
8481   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8482
8483 (defun gnus-summary-kill-same-subject-and-select (unmark)
8484   "Mark articles which has the same subject as read, and then select the next.
8485 If UNMARK is positive, remove any kind of mark.
8486 If UNMARK is negative, tick articles."
8487   (interactive "P")
8488   (if unmark
8489       (setq unmark (prefix-numeric-value unmark)))
8490   (let ((count
8491          (gnus-summary-mark-same-subject
8492           (gnus-summary-subject-string) unmark)))
8493     ;; Select next unread article. If auto-select-same mode, should
8494     ;; select the first unread article.
8495     (gnus-summary-next-article t (and gnus-auto-select-same
8496                                       (gnus-summary-subject-string)))
8497     (gnus-message 7 "%d articles are marked as %s"
8498                   count (if unmark "unread" "read"))))
8499
8500 (defun gnus-summary-kill-same-subject (unmark)
8501   "Mark articles which has the same subject as read. 
8502 If UNMARK is positive, remove any kind of mark.
8503 If UNMARK is negative, tick articles."
8504   (interactive "P")
8505   (if unmark
8506       (setq unmark (prefix-numeric-value unmark)))
8507   (let ((count
8508          (gnus-summary-mark-same-subject
8509           (gnus-summary-subject-string) unmark)))
8510     ;; If marked as read, go to next unread subject.
8511     (if (null unmark)
8512         ;; Go to next unread subject.
8513         (gnus-summary-next-subject 1 t))
8514     (gnus-message 7 "%d articles are marked as %s"
8515                   count (if unmark "unread" "read"))))
8516
8517 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8518   "Mark articles with same SUBJECT as read, and return marked number.
8519 If optional argument UNMARK is positive, remove any kinds of marks.
8520 If optional argument UNMARK is negative, mark articles as unread instead."
8521   (let ((count 1))
8522     (save-excursion
8523       (cond ((null unmark)
8524              (gnus-summary-mark-as-read nil gnus-killed-mark))
8525             ((> unmark 0)
8526              (gnus-summary-tick-article nil t))
8527             (t
8528              (gnus-summary-tick-article)))
8529       (while (and subject
8530                   (gnus-summary-search-forward nil subject))
8531         (cond ((null unmark)
8532                (gnus-summary-mark-as-read nil gnus-killed-mark))
8533               ((> unmark 0)
8534                (gnus-summary-tick-article nil t))
8535               (t
8536                (gnus-summary-tick-article)))
8537         (setq count (1+ count))))
8538     ;; Hide killed thread subtrees.  Does not work properly always.
8539     ;;(and (null unmark)
8540     ;;     gnus-thread-hide-killed
8541     ;;     (gnus-summary-hide-thread))
8542     ;; Return number of articles marked as read.
8543     count))
8544
8545 (defun gnus-summary-mark-as-processable (n &optional unmark)
8546   "Set the process mark on the next N articles.
8547 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8548 the process mark instead.  The difference between N and the actual
8549 number of articles marked is returned."
8550   (interactive "p")
8551   (let ((backward (< n 0))
8552         (n (abs n)))
8553   (while (and 
8554           (> n 0)
8555           (if unmark
8556               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8557             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8558           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8559     (setq n (1- n)))
8560   (if (/= 0 n) (gnus-message 7 "No more articles"))
8561   (gnus-summary-recenter)
8562   (gnus-summary-position-cursor)
8563   n))
8564
8565 (defun gnus-summary-unmark-as-processable (n)
8566   "Remove the process mark from the next N articles.
8567 If N is negative, mark backward instead.  The difference between N and
8568 the actual number of articles marked is returned."
8569   (interactive "p")
8570   (gnus-summary-mark-as-processable n t))
8571
8572 (defun gnus-summary-unmark-all-processable ()
8573   "Remove the process mark from all articles."
8574   (interactive)
8575   (save-excursion
8576     (while gnus-newsgroup-processable
8577       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8578   (gnus-summary-position-cursor))
8579
8580 (defun gnus-summary-mark-as-expirable (n)
8581   "Mark N articles forward as expirable.
8582 If N is negative, mark backward instead. The difference between N and
8583 the actual number of articles marked is returned."
8584   (interactive "p")
8585   (gnus-summary-mark-forward n gnus-expirable-mark))
8586
8587 (defun gnus-summary-mark-article-as-replied (article)
8588   "Mark ARTICLE replied and update the summary line."
8589   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8590   (let ((buffer-read-only nil))
8591     (if (gnus-summary-goto-subject article)
8592         (progn
8593           (gnus-summary-update-mark gnus-replied-mark 'replied)
8594           t))))
8595
8596 (defun gnus-summary-set-bookmark (article)
8597   "Set a bookmark in current article."
8598   (interactive (list (gnus-summary-article-number)))
8599   (if (or (not (get-buffer gnus-article-buffer))
8600           (not gnus-current-article)
8601           (not gnus-article-current)
8602           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8603       (error "No current article selected"))
8604   ;; Remove old bookmark, if one exists.
8605   (let ((old (assq article gnus-newsgroup-bookmarks)))
8606     (if old (setq gnus-newsgroup-bookmarks 
8607                   (delq old gnus-newsgroup-bookmarks))))
8608   ;; Set the new bookmark, which is on the form 
8609   ;; (article-number . line-number-in-body).
8610   (setq gnus-newsgroup-bookmarks 
8611         (cons 
8612          (cons article 
8613                (save-excursion
8614                  (set-buffer gnus-article-buffer)
8615                  (count-lines
8616                   (min (point)
8617                        (save-excursion
8618                          (goto-char (point-min))
8619                          (search-forward "\n\n" nil t)
8620                          (point)))
8621                   (point))))
8622          gnus-newsgroup-bookmarks))
8623   (gnus-message 6 "A bookmark has been added to the current article."))
8624
8625 (defun gnus-summary-remove-bookmark (article)
8626   "Remove the bookmark from the current article."
8627   (interactive (list (gnus-summary-article-number)))
8628   ;; Remove old bookmark, if one exists.
8629   (let ((old (assq article gnus-newsgroup-bookmarks)))
8630     (if old 
8631         (progn
8632           (setq gnus-newsgroup-bookmarks 
8633                 (delq old gnus-newsgroup-bookmarks))
8634           (gnus-message 6 "Removed bookmark."))
8635       (gnus-message 6 "No bookmark in current article."))))
8636
8637 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8638 (defun gnus-summary-mark-as-dormant (n)
8639   "Mark N articles forward as dormant.
8640 If N is negative, mark backward instead.  The difference between N and
8641 the actual number of articles marked is returned."
8642   (interactive "p")
8643   (gnus-summary-mark-forward n gnus-dormant-mark))
8644
8645 (defun gnus-summary-set-process-mark (article)
8646   "Set the process mark on ARTICLE and update the summary line."
8647   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8648   (let ((buffer-read-only nil))
8649     (if (gnus-summary-goto-subject article)
8650         (progn
8651           (gnus-summary-show-thread)
8652           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8653                (forward-line 1))
8654           (gnus-summary-update-mark gnus-process-mark 'replied)
8655           t))))
8656
8657 (defun gnus-summary-remove-process-mark (article)
8658   "Remove the process mark from ARTICLE and update the summary line."
8659   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8660   (let ((buffer-read-only nil))
8661     (if (gnus-summary-goto-subject article)
8662         (progn
8663           (gnus-summary-show-thread)
8664           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8665                (forward-line 1))
8666           (gnus-summary-update-mark ?  'replied)
8667           (if (memq article gnus-newsgroup-replied) 
8668               (gnus-summary-update-mark gnus-replied-mark 'replied))
8669           t))))
8670
8671 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8672   "Mark N articles as read forwards.
8673 If N is negative, mark backwards instead.
8674 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8675 marked as unread. 
8676 The difference between N and the actual number of articles marked is
8677 returned."
8678   (interactive "p")
8679   (gnus-set-global-variables)
8680   (let ((backward (< n 0))
8681         (n (abs n))
8682         (mark (or mark gnus-del-mark)))
8683   (while (and (> n 0)
8684               (gnus-summary-mark-article nil mark no-expire)
8685               (zerop (gnus-summary-next-subject 
8686                       (if backward -1 1) gnus-summary-goto-unread t)))
8687     (setq n (1- n)))
8688   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8689   (gnus-summary-recenter)
8690   (gnus-summary-position-cursor)
8691   (gnus-set-mode-line 'summary)
8692   n))
8693
8694 (defun gnus-summary-mark-article (&optional article mark no-expire)
8695   "Mark ARTICLE with MARK.
8696 MARK can be any character.
8697 Five MARK strings are reserved: ?  (unread), 
8698 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8699 If MARK is nil, then the default character ?D is used.
8700 If ARTICLE is nil, then the article on the current line will be
8701 marked." 
8702   ;; If no mark is given, then we check auto-expiring.
8703   (and (not no-expire)
8704        gnus-newsgroup-auto-expire 
8705        (or (not mark)
8706            (and (numberp mark) (or (= mark gnus-killed-mark)
8707                                    (= mark gnus-del-mark)
8708                                    (= mark gnus-catchup-mark)
8709                                    (= mark gnus-low-score-mark)
8710                                    (= mark gnus-read-mark))))
8711        (setq mark gnus-expirable-mark))
8712   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8713          (article (or article (gnus-summary-article-number))))
8714     (if (or (= mark gnus-unread-mark) 
8715             (= mark gnus-ticked-mark) 
8716             (= mark gnus-dormant-mark))
8717         (gnus-mark-article-as-unread article mark)
8718       (gnus-mark-article-as-read article mark))
8719
8720     ;; See whether the article is to be put in the cache.
8721     (and gnus-use-cache
8722          (save-excursion
8723            (gnus-cache-possibly-enter-article 
8724             gnus-newsgroup-name article 
8725             (gnus-get-header-by-number article)
8726             (= mark gnus-ticked-mark)
8727             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8728
8729     (if (gnus-summary-goto-subject article)
8730         (let ((buffer-read-only nil))
8731           (gnus-summary-show-thread)
8732           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8733                (forward-line 1))
8734           ;; Fix the mark.
8735           (gnus-summary-update-mark mark 'unread)
8736           t))))
8737
8738 (defun gnus-summary-update-mark (mark type)
8739   (beginning-of-line)
8740   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8741         plist)
8742     (if (not forward)
8743         ()
8744       (forward-char forward)
8745       (setq plist (text-properties-at (point)))
8746       (delete-char 1)
8747       (insert mark)
8748       (and plist (add-text-properties (1- (point)) (point) plist))
8749       (and (eq type 'unread)
8750            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
8751       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8752   
8753 (defun gnus-mark-article-as-read (article &optional mark)
8754   "Enter ARTICLE in the pertinent lists and remove it from others."
8755   ;; Make the article expirable.
8756   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8757     (if (= mark gnus-expirable-mark)
8758         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8759       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8760     ;; Remove from unread and marked lists.
8761     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8762     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8763     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))))
8764
8765 (defun gnus-mark-article-as-unread (article &optional mark)
8766   "Enter ARTICLE in the pertinent lists and remove it from others."
8767   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8768     ;; Add to unread list.
8769     (or (memq article gnus-newsgroup-unreads)
8770         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
8771     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
8772     ;; lists.  Otherwise, it must be added to the list.
8773     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8774     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8775     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8776     (if (= mark gnus-ticked-mark)
8777         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
8778     (if (= mark gnus-dormant-mark)
8779         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
8780
8781 (defalias 'gnus-summary-mark-as-unread-forward 
8782   'gnus-summary-tick-article-forward)
8783 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8784                'gnus-summary-tick-article-forward)
8785 (defun gnus-summary-tick-article-forward (n)
8786   "Tick N articles forwards.
8787 If N is negative, tick backwards instead.
8788 The difference between N and the number of articles ticked is returned."
8789   (interactive "p")
8790   (gnus-summary-mark-forward n gnus-ticked-mark))
8791
8792 (defalias 'gnus-summary-mark-as-unread-backward 
8793   'gnus-summary-tick-article-backward)
8794 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8795                'gnus-summary-tick-article-backward)
8796 (defun gnus-summary-tick-article-backward (n)
8797   "Tick N articles backwards.
8798 The difference between N and the number of articles ticked is returned."
8799   (interactive "p")
8800   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8801
8802 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8803 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8804 (defun gnus-summary-tick-article (&optional article clear-mark)
8805   "Mark current article as unread.
8806 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8807 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8808   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8809                                        gnus-ticked-mark)))
8810
8811 (defun gnus-summary-mark-as-read-forward (n)
8812   "Mark N articles as read forwards.
8813 If N is negative, mark backwards instead.
8814 The difference between N and the actual number of articles marked is
8815 returned."
8816   (interactive "p")
8817   (gnus-summary-mark-forward n gnus-del-mark t))
8818
8819 (defun gnus-summary-mark-as-read-backward (n)
8820   "Mark the N articles as read backwards.
8821 The difference between N and the actual number of articles marked is
8822 returned."
8823   (interactive "p")
8824   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8825
8826 (defun gnus-summary-mark-as-read (&optional article mark)
8827   "Mark current article as read.
8828 ARTICLE specifies the article to be marked as read.
8829 MARK specifies a string to be inserted at the beginning of the line."
8830   (gnus-summary-mark-article article mark))
8831
8832 (defun gnus-summary-clear-mark-forward (n)
8833   "Clear marks from N articles forward.
8834 If N is negative, clear backward instead.
8835 The difference between N and the number of marks cleared is returned."
8836   (interactive "p")
8837   (gnus-summary-mark-forward n gnus-unread-mark))
8838
8839 (defun gnus-summary-clear-mark-backward (n)
8840   "Clear marks from N articles backward.
8841 The difference between N and the number of marks cleared is returned."
8842   (interactive "p")
8843   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8844
8845 (defun gnus-summary-mark-unread-as-read ()
8846   "Intended to be used by `gnus-summary-mark-article-hook'."
8847   (or (memq gnus-current-article gnus-newsgroup-marked)
8848       (memq gnus-current-article gnus-newsgroup-dormant)
8849       (memq gnus-current-article gnus-newsgroup-expirable)
8850       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8851
8852 (defun gnus-summary-mark-region-as-read (point mark all)
8853   "Mark all unread articles between point and mark as read.
8854 If given a prefix, mark all articles between point and mark as read,
8855 even ticked and dormant ones."
8856   (interactive "r\nP")
8857   (save-excursion
8858     (goto-char point)
8859     (beginning-of-line)
8860     (while (and 
8861             (< (point) mark)
8862             (progn
8863               (and
8864                (or all
8865                    (and
8866                     (not (memq (gnus-summary-article-number)
8867                                gnus-newsgroup-marked))
8868                     (not (memq (gnus-summary-article-number)
8869                                gnus-newsgroup-dormant))))
8870                (gnus-summary-mark-article
8871                 (gnus-summary-article-number) gnus-del-mark))
8872               t)
8873             (zerop (forward-line 1))))))
8874
8875 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8876 (defalias 'gnus-summary-delete-marked-as-read 
8877   'gnus-summary-remove-lines-marked-as-read)
8878 (make-obsolete 'gnus-summary-delete-marked-as-read 
8879                'gnus-summary-remove-lines-marked-as-read)
8880 (defun gnus-summary-remove-lines-marked-as-read ()
8881   "Remove lines that are marked as read."
8882   (interactive)
8883   (gnus-summary-remove-lines-marked-with 
8884    (concat (mapconcat
8885             (lambda (char) (char-to-string (symbol-value char)))
8886             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
8887               gnus-killed-mark gnus-kill-file-mark
8888               gnus-low-score-mark gnus-expirable-mark)
8889             ""))))
8890
8891 (defalias 'gnus-summary-delete-marked-with 
8892   'gnus-summary-remove-lines-marked-with)
8893 (make-obsolete 'gnus-summary-delete-marked-with 
8894                'gnus-summary-remove-lines-marked-with)
8895 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8896 (defun gnus-summary-remove-lines-marked-with (marks)
8897   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8898   (interactive "sMarks: ")
8899   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8900   (gnus-set-global-variables)
8901   (let ((buffer-read-only nil)
8902         (marks (concat "^[" marks "]")))
8903     (goto-char (point-min))
8904     (if gnus-newsgroup-adaptive
8905         (gnus-score-remove-lines-adaptive marks)
8906       (while (re-search-forward marks nil t)
8907         (gnus-delete-line)))
8908     ;; If we use dummy roots, we have to do an additional sweep over
8909     ;; the buffer.
8910     (if (not (eq gnus-summary-make-false-root 'dummy))
8911         ()
8912       (goto-char (point-min))
8913       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
8914       (while (re-search-forward marks nil t)
8915         (if (gnus-subject-equal
8916              (gnus-summary-subject-string)
8917              (progn
8918                (forward-line 1)
8919                (gnus-summary-subject-string)))
8920             ()
8921           (forward-line -1)
8922           (gnus-delete-line))))))
8923   (or (zerop (buffer-size))
8924       (if (eobp)
8925           (gnus-summary-prev-subject 1)
8926         (gnus-summary-position-cursor)))
8927
8928 (defun gnus-summary-expunge-below (score)
8929   "Remove articles with score less than SCORE."
8930   (interactive "P")
8931   (gnus-set-global-variables)
8932   (setq score (if score
8933                   (prefix-numeric-value score)
8934                 (or gnus-summary-default-score 0)))
8935   (save-excursion
8936     (set-buffer gnus-summary-buffer)
8937     (goto-char (point-min))
8938     (let ((buffer-read-only nil)
8939           beg)
8940       (while (not (eobp))
8941         (if (< (gnus-summary-article-score) score)
8942             (progn
8943               (setq beg (point))
8944               (forward-line 1)
8945               (delete-region beg (point)))
8946           (forward-line 1)))
8947       ;; Adjust point.
8948       (or (zerop (buffer-size))
8949           (if (eobp)
8950               (gnus-summary-prev-subject 1)
8951             (gnus-summary-position-cursor))))))
8952
8953 (defun gnus-summary-mark-below (score mark)
8954   "Mark articles with score less than SCORE with MARK."
8955   (interactive "P\ncMark: ")
8956   (gnus-set-global-variables)
8957   (setq score (if score
8958                   (prefix-numeric-value score)
8959                 (or gnus-summary-default-score 0)))
8960   (save-excursion
8961     (set-buffer gnus-summary-buffer)
8962     (goto-char (point-min))
8963     (while (not (eobp))
8964       (and (< (gnus-summary-article-score) score)
8965            (gnus-summary-mark-article nil mark))
8966       (forward-line 1))))
8967
8968 (defun gnus-summary-kill-below (score)
8969   "Mark articles with score below SCORE as read."
8970   (interactive "P")
8971   (gnus-summary-mark-below score gnus-killed-mark))
8972
8973 (defun gnus-summary-clear-above (score)
8974   "Clear all marks from articles with score above SCORE."
8975   (interactive "P")
8976   (gnus-summary-mark-above score gnus-unread-mark))
8977
8978 (defun gnus-summary-tick-above (score)
8979   "Tick all articles with score above SCORE."
8980   (interactive "P")
8981   (gnus-summary-mark-above score gnus-ticked-mark))
8982
8983 (defun gnus-summary-mark-above (score mark)
8984   "Mark articles with score over SCORE with MARK."
8985   (interactive "P\ncMark: ")
8986   (setq score (if score
8987                   (prefix-numeric-value score)
8988                 (or gnus-summary-default-score 0)))
8989   (save-excursion
8990     (set-buffer gnus-summary-buffer)
8991     (goto-char (point-min))
8992     (while (not (eobp))
8993       (if (> (gnus-summary-article-score) score)
8994           (progn
8995             (gnus-summary-mark-article nil mark)
8996             (forward-line 1))
8997         (forward-line 1)))))
8998
8999 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9000 (defun gnus-summary-show-all-expunged ()
9001   "Display all the hidden articles that were expunged for low scores."
9002   (interactive)
9003   (let ((buffer-read-only nil))
9004     (let ((scored gnus-newsgroup-scored)
9005           headers h)
9006       (while scored
9007         (or (gnus-summary-goto-subject (car (car scored)))
9008             (and (setq h (gnus-get-header-by-number (car (car scored))))
9009                  (< (cdr (car scored)) gnus-summary-expunge-below)
9010                  (setq headers (cons h headers))))
9011         (setq scored (cdr scored)))
9012       (or headers (error "No expunged articles hidden."))
9013       (goto-char (point-min))
9014       (save-excursion 
9015         (gnus-summary-update-lines 
9016          (point)
9017          (progn
9018            (gnus-summary-prepare-threads (nreverse headers) 0)
9019            (point)))))
9020     (goto-char (point-min))
9021     (gnus-summary-position-cursor)))
9022
9023 (defun gnus-summary-show-all-dormant ()
9024   "Display all the hidden articles that are marked as dormant."
9025   (interactive)
9026   (let ((buffer-read-only nil))
9027     (goto-char (point-min))
9028     (let ((dormant gnus-newsgroup-dormant)
9029           headers h)
9030       (while dormant
9031         (or (gnus-summary-goto-subject (car dormant))
9032             (and (setq h (gnus-get-header-by-number (car dormant)))
9033                  (setq headers (cons h headers))))
9034         (setq dormant (cdr dormant)))
9035       (or headers (error "No dormant articles hidden."))
9036       (save-excursion 
9037         (gnus-summary-update-lines 
9038          (point)
9039          (progn
9040            (gnus-summary-prepare-threads (nreverse headers) 0)
9041            (point)))))
9042     (goto-char (point-min))
9043     (gnus-summary-position-cursor)))
9044
9045 (defun gnus-summary-hide-all-dormant ()
9046   "Hide all dormant articles."
9047   (interactive)
9048   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9049   (gnus-summary-position-cursor))
9050
9051 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9052   "Mark all articles not marked as unread in this newsgroup as read.
9053 If prefix argument ALL is non-nil, all articles are marked as read.
9054 If QUIETLY is non-nil, no questions will be asked.
9055 If TO-HERE is non-nil, it should be a point in the buffer. All
9056 articles before this point will be marked as read.
9057 The number of articles marked as read is returned."
9058   (interactive "P")
9059   (prog1
9060       (if (or quietly
9061               (not gnus-interactive-catchup) ;Without confirmation?
9062               gnus-expert-user
9063               (gnus-y-or-n-p
9064                (if all
9065                    "Mark absolutely all articles as read? "
9066                  "Mark all unread articles as read? ")))
9067           (if (and not-mark 
9068                    (not gnus-newsgroup-adaptive)
9069                    (not gnus-newsgroup-auto-expire))
9070               (progn
9071                 (and all (setq gnus-newsgroup-marked nil
9072                                gnus-newsgroup-dormant nil))
9073                 (setq gnus-newsgroup-unreads gnus-newsgroup-marked))
9074             (let ((unreads (length gnus-newsgroup-unreads)))
9075               (if (gnus-summary-first-subject (not all))
9076                   (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
9077                               (if to-here (< (point) to-here) t)
9078                               (gnus-summary-search-subject nil (not all)))))
9079               (- unreads (length gnus-newsgroup-unreads))
9080               (or to-here
9081                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9082     (gnus-summary-position-cursor)))
9083
9084 (defun gnus-summary-catchup-to-here (&optional all)
9085   "Mark all unticked articles before the current one as read.
9086 If ALL is non-nil, also mark ticked and dormant articles as read."
9087   (interactive)
9088   (beginning-of-line)
9089   (gnus-summary-catchup all t (point))
9090   (gnus-set-mode-line 'summary)
9091   (gnus-summary-position-cursor))
9092
9093 (defun gnus-summary-catchup-all (&optional quietly)
9094   "Mark all articles in this newsgroup as read."
9095   (interactive)
9096   (gnus-summary-catchup t quietly))
9097
9098 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9099   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9100 If prefix argument ALL is non-nil, all articles are marked as read."
9101   (interactive "P")
9102   (gnus-summary-catchup all quietly nil 'fast)
9103   ;; Select next newsgroup or exit.
9104   (if (eq gnus-auto-select-next 'quietly)
9105       (gnus-summary-next-group nil)
9106     (gnus-summary-exit)))
9107
9108 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9109   "Mark all articles in this newsgroup as read, and then exit."
9110   (interactive)
9111   (gnus-summary-catchup-and-exit t quietly))
9112
9113 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9114 (defun gnus-summary-catchup-and-goto-next-group (all)
9115   "Mark all articles in this group as read and select the next group.
9116 If given a prefix, mark all articles, unread as well as ticked, as
9117 read." 
9118   (interactive "P")
9119   (gnus-summary-catchup all)
9120   (gnus-summary-next-group))
9121
9122 ;; Thread-based commands.
9123
9124 (defun gnus-summary-toggle-threads (arg)
9125   "Toggle showing conversation threads.
9126 If ARG is positive number, turn showing conversation threads on."
9127   (interactive "P")
9128   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9129     (setq gnus-show-threads
9130           (if (null arg) (not gnus-show-threads)
9131             (> (prefix-numeric-value arg) 0)))
9132     (gnus-summary-prepare)
9133     (gnus-summary-goto-subject current)
9134     (gnus-summary-position-cursor)))
9135
9136 (defun gnus-summary-show-all-threads ()
9137   "Show all threads."
9138   (interactive)
9139   (save-excursion
9140     (let ((buffer-read-only nil))
9141       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9142   (gnus-summary-position-cursor))
9143
9144 (defun gnus-summary-show-thread ()
9145   "Show thread subtrees.
9146 Returns nil if no thread was there to be shown."
9147   (interactive)
9148   (prog1
9149       (save-excursion
9150         (let ((buffer-read-only nil)
9151               ;; first goto end then to beg, to have point at beg after let
9152               (end (progn (end-of-line) (point)))
9153               (beg (progn (beginning-of-line) (point))))
9154           (prog1
9155               ;; Any hidden lines here?
9156               (search-forward "\r" end t)
9157             (subst-char-in-region beg end ?\^M ?\n t))))
9158     (gnus-summary-position-cursor)))
9159
9160 (defun gnus-summary-hide-all-threads ()
9161   "Hide all thread subtrees."
9162   (interactive)
9163   (save-excursion
9164     (goto-char (point-min))
9165     (gnus-summary-hide-thread)
9166     (while (and (not (eobp)) (zerop (forward-line 1)))
9167       (gnus-summary-hide-thread)))
9168   (gnus-summary-position-cursor))
9169
9170 (defun gnus-summary-hide-thread ()
9171   "Hide thread subtrees.
9172 Returns nil if no threads were there to be hidden."
9173   (interactive)
9174   (let ((buffer-read-only nil)
9175         (start (point))
9176         (level (gnus-summary-thread-level))
9177         (end (point)))
9178     ;; Go forward until either the buffer ends or the subthread
9179     ;; ends. 
9180     (if (eobp)
9181         ()
9182       (while (and (zerop (forward-line 1))
9183                   (> (gnus-summary-thread-level) level))
9184         (setq end (point)))
9185       (prog1
9186           (save-excursion
9187             (goto-char end)
9188             (search-backward "\n" start t))
9189         (subst-char-in-region start end ?\n ?\^M t)
9190         (forward-line -1)))))
9191
9192 (defun gnus-summary-go-to-next-thread (&optional previous)
9193   "Go to the same level (or less) next thread.
9194 If PREVIOUS is non-nil, go to previous thread instead.
9195 Return the article number moved to, or nil if moving was impossible."
9196   (let ((level (gnus-summary-thread-level))
9197         (article (gnus-summary-article-number)))
9198     (if previous 
9199         (while (and (zerop (gnus-summary-prev-subject 1))
9200                     (> (gnus-summary-thread-level) level)))
9201       (while (and (zerop (gnus-summary-next-subject 1))
9202                   (> (gnus-summary-thread-level) level))))
9203     (gnus-summary-recenter)
9204     (gnus-summary-position-cursor)
9205     (let ((oart (gnus-summary-article-number)))
9206       (and (/= oart article) oart))))
9207
9208 (defun gnus-summary-next-thread (n)
9209   "Go to the same level next N'th thread.
9210 If N is negative, search backward instead.
9211 Returns the difference between N and the number of skips actually
9212 done."
9213   (interactive "p")
9214   (let ((backward (< n 0))
9215         (n (abs n)))
9216   (while (and (> n 0)
9217               (gnus-summary-go-to-next-thread backward))
9218     (setq n (1- n)))
9219   (gnus-summary-position-cursor)
9220   (if (/= 0 n) (gnus-message 7 "No more threads"))
9221   n))
9222
9223 (defun gnus-summary-prev-thread (n)
9224   "Go to the same level previous N'th thread.
9225 Returns the difference between N and the number of skips actually
9226 done."
9227   (interactive "p")
9228   (gnus-summary-next-thread (- n)))
9229
9230 (defun gnus-summary-go-down-thread (&optional same)
9231   "Go down one level in the current thread.
9232 If SAME is non-nil, also move to articles of the same level."
9233   (let ((level (gnus-summary-thread-level))
9234         (start (point)))
9235     (if (and (zerop (forward-line 1))
9236              (> (gnus-summary-thread-level) level))
9237         t
9238       (goto-char start)
9239       nil)))
9240
9241 (defun gnus-summary-go-up-thread ()
9242   "Go up one level in the current thread."
9243   (let ((level (gnus-summary-thread-level))
9244         (start (point)))
9245     (while (and (zerop (forward-line -1))
9246                 (>= (gnus-summary-thread-level) level)))
9247     (if (>= (gnus-summary-thread-level) level)
9248         (progn
9249           (goto-char start)
9250           nil)
9251       t)))
9252
9253 (defun gnus-summary-down-thread (n)
9254   "Go down thread N steps.
9255 If N is negative, go up instead.
9256 Returns the difference between N and how many steps down that were
9257 taken."
9258   (interactive "p")
9259   (let ((up (< n 0))
9260         (n (abs n)))
9261   (while (and (> n 0)
9262               (if up (gnus-summary-go-up-thread)
9263                 (gnus-summary-go-down-thread)))
9264     (setq n (1- n)))
9265   (gnus-summary-position-cursor)
9266   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9267   n))
9268
9269 (defun gnus-summary-up-thread (n)
9270   "Go up thread N steps.
9271 If N is negative, go up instead.
9272 Returns the difference between N and how many steps down that were
9273 taken."
9274   (interactive "p")
9275   (gnus-summary-down-thread (- n)))
9276
9277 (defun gnus-summary-kill-thread (unmark)
9278   "Mark articles under current thread as read.
9279 If the prefix argument is positive, remove any kinds of marks.
9280 If the prefix argument is negative, tick articles instead."
9281   (interactive "P")
9282   (if unmark
9283       (setq unmark (prefix-numeric-value unmark)))
9284   (let ((killing t)
9285         (level (gnus-summary-thread-level)))
9286     (save-excursion
9287       (while killing
9288         ;; Mark the article...
9289         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9290               ((> unmark 0) (gnus-summary-tick-article nil t))
9291               (t (gnus-summary-tick-article)))
9292         ;; ...and go forward until either the buffer ends or the subtree
9293         ;; ends. 
9294         (if (not (and (zerop (forward-line 1))
9295                       (> (gnus-summary-thread-level) level)))
9296             (setq killing nil))))
9297     ;; Hide killed subtrees.
9298     (and (null unmark)
9299          gnus-thread-hide-killed
9300          (gnus-summary-hide-thread))
9301     ;; If marked as read, go to next unread subject.
9302     (if (null unmark)
9303         ;; Go to next unread subject.
9304         (gnus-summary-next-subject 1 t)))
9305   (gnus-set-mode-line 'summary))
9306
9307 ;; Summary sorting commands
9308
9309 (defun gnus-summary-sort-by-number (&optional reverse)
9310   "Sort summary buffer by article number.
9311 Argument REVERSE means reverse order."
9312   (interactive "P")
9313   (gnus-summary-sort 
9314    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9315
9316 (defun gnus-summary-sort-by-author (&optional reverse)
9317   "Sort summary buffer by author name alphabetically.
9318 If case-fold-search is non-nil, case of letters is ignored.
9319 Argument REVERSE means reverse order."
9320   (interactive "P")
9321   (gnus-summary-sort
9322    (cons
9323     (lambda ()
9324       (let ((extract (funcall
9325                       gnus-extract-address-components
9326                       (header-from (gnus-get-header-by-number
9327                                     (gnus-summary-article-number))))))
9328         (or (car extract) (cdr extract))))
9329     'gnus-thread-sort-by-author)
9330    reverse))
9331
9332 (defun gnus-summary-sort-by-subject (&optional reverse)
9333   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9334 If case-fold-search is non-nil, case of letters is ignored.
9335 Argument REVERSE means reverse order."
9336   (interactive "P")
9337   (gnus-summary-sort
9338    (cons
9339     (lambda ()
9340       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9341     'gnus-thread-sort-by-subject)
9342    reverse))
9343
9344 (defun gnus-summary-sort-by-date (&optional reverse)
9345   "Sort summary buffer by date.
9346 Argument REVERSE means reverse order."
9347   (interactive "P")
9348   (gnus-summary-sort
9349    (cons
9350     (lambda ()
9351       (gnus-sortable-date
9352        (header-date 
9353         (gnus-get-header-by-number (gnus-summary-article-number)))))
9354     'gnus-thread-sort-by-date)
9355    reverse))
9356
9357 (defun gnus-summary-sort-by-score (&optional reverse)
9358   "Sort summary buffer by score.
9359 Argument REVERSE means reverse order."
9360   (interactive "P")
9361   (gnus-summary-sort 
9362    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9363    (not reverse)))
9364
9365 (defvar gnus-summary-already-sorted nil)
9366 (defun gnus-summary-sort (predicate reverse)
9367   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9368   (if gnus-summary-already-sorted
9369       ()
9370     (let (buffer-read-only)
9371       (if (not gnus-show-threads)
9372           (progn
9373             (goto-char (point-min))
9374             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9375         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9376               (gnus-summary-prepare-hook nil)
9377               (gnus-summary-already-sorted nil))
9378           (gnus-summary-prepare)
9379           (and gnus-show-threads
9380                gnus-thread-hide-subtree
9381                (gnus-summary-hide-all-threads)))))))
9382   
9383 (defun gnus-sortable-date (date)
9384   "Make sortable string by string-lessp from DATE.
9385 Timezone package is used."
9386   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9387          (year   (aref date 0))
9388          (month  (aref date 1))
9389          (day    (aref date 2)))
9390     (timezone-make-sortable-date 
9391      year month day 
9392      (timezone-make-time-string
9393       (aref date 3) (aref date 4) (aref date 5)))))
9394
9395
9396 ;; Summary saving commands.
9397
9398 (defun gnus-summary-save-article (n)
9399   "Save the current article using the default saver function.
9400 If N is a positive number, save the N next articles.
9401 If N is a negative number, save the N previous articles.
9402 If N is nil and any articles have been marked with the process mark,
9403 save those articles instead.
9404 The variable `gnus-default-article-saver' specifies the saver function."
9405   (interactive "P")
9406   (let ((articles (gnus-summary-work-articles n)))
9407     (while articles
9408       (let ((header (gnus-get-header-by-number (car articles))))
9409         (if (vectorp header)
9410             (progn
9411               (gnus-summary-select-article t nil nil (car articles))
9412               (or gnus-save-all-headers
9413                   (gnus-article-hide-headers t))
9414               ;; Remove any X-Gnus lines.
9415               (save-excursion
9416                 (save-restriction
9417                   (set-buffer gnus-article-buffer)
9418                   (let ((buffer-read-only nil))
9419                     (goto-char (point-min))
9420                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9421                                                   (point-max)))
9422                     (while (re-search-forward "^X-Gnus" nil t)
9423                       (beginning-of-line)
9424                       (delete-region (point)
9425                                      (progn (forward-line 1) (point))))
9426                     (widen))))
9427               (save-excursion
9428                 (if gnus-default-article-saver
9429                     (funcall gnus-default-article-saver)
9430                   (error "No default saver is defined."))))
9431           (if (assq 'name header)
9432               (gnus-copy-file (cdr (assq 'name header)))
9433             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9434       (gnus-summary-remove-process-mark (car articles))
9435       (setq articles (cdr articles)))
9436     (gnus-summary-position-cursor)
9437     n))
9438
9439 (defun gnus-summary-pipe-output (arg)
9440   "Pipe the current article to a subprocess.
9441 If N is a positive number, pipe the N next articles.
9442 If N is a negative number, pipe the N previous articles.
9443 If N is nil and any articles have been marked with the process mark,
9444 pipe those articles instead."
9445   (interactive "P")
9446   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9447     (gnus-summary-save-article arg)))
9448
9449 (defun gnus-summary-save-article-mail (arg)
9450   "Append the current article to an mail file.
9451 If N is a positive number, save the N next articles.
9452 If N is a negative number, save the N previous articles.
9453 If N is nil and any articles have been marked with the process mark,
9454 save those articles instead."
9455   (interactive "P")
9456   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9457     (gnus-summary-save-article arg)))
9458
9459 (defun gnus-summary-save-article-rmail (arg)
9460   "Append the current article to an rmail file.
9461 If N is a positive number, save the N next articles.
9462 If N is a negative number, save the N previous articles.
9463 If N is nil and any articles have been marked with the process mark,
9464 save those articles instead."
9465   (interactive "P")
9466   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9467     (gnus-summary-save-article arg)))
9468
9469 (defun gnus-summary-save-article-file (arg)
9470   "Append the current article to a file.
9471 If N is a positive number, save the N next articles.
9472 If N is a negative number, save the N previous articles.
9473 If N is nil and any articles have been marked with the process mark,
9474 save those articles instead."
9475   (interactive "P")
9476   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9477     (gnus-summary-save-article arg)))
9478
9479 (defun gnus-read-save-file-name (prompt default-name)
9480   (let ((methods gnus-split-methods)
9481         split-name)
9482     (if (not gnus-split-methods)
9483         ()
9484       (save-excursion
9485         (set-buffer gnus-article-buffer)
9486         (gnus-narrow-to-headers)
9487         (while methods
9488           (goto-char (point-min))
9489           (and (condition-case () 
9490                    (re-search-forward (car (car methods)) nil t)
9491                  (error nil))
9492                (setq split-name (cons (nth 1 (car methods)) split-name)))
9493           (setq methods (cdr methods)))
9494         (widen)))
9495     (cond ((null split-name)
9496            (read-file-name
9497             (concat prompt " (default "
9498                     (file-name-nondirectory default-name) ") ")
9499             (file-name-directory default-name)
9500             default-name))
9501           ((= 1 (length split-name))
9502            (read-file-name
9503             (concat prompt " (default " (car split-name) ") ")
9504             gnus-article-save-directory
9505             (concat gnus-article-save-directory (car split-name))))
9506           (t
9507            (setq split-name (mapcar (lambda (el) (list el))
9508                                     (nreverse split-name)))
9509            (let ((result (completing-read 
9510                           (concat prompt " ")
9511                           split-name nil nil)))
9512              (concat gnus-article-save-directory
9513                      (if (string= result "")
9514                          (car (car split-name))
9515                        result)))))))
9516
9517 (defun gnus-summary-save-in-rmail (&optional filename)
9518   "Append this article to Rmail file.
9519 Optional argument FILENAME specifies file name.
9520 Directory to save to is default to `gnus-article-save-directory' which
9521 is initialized from the SAVEDIR environment variable."
9522   (interactive)
9523   (let ((default-name
9524           (funcall gnus-rmail-save-name gnus-newsgroup-name
9525                    gnus-current-headers gnus-newsgroup-last-rmail)))
9526     (or filename
9527         (setq filename (gnus-read-save-file-name 
9528                         "Save in rmail file:" default-name)))
9529     (gnus-make-directory (file-name-directory filename))
9530     (gnus-eval-in-buffer-window 
9531      gnus-article-buffer
9532      (save-excursion
9533        (save-restriction
9534          (widen)
9535          (gnus-output-to-rmail filename))))
9536     ;; Remember the directory name to save articles.
9537     (setq gnus-newsgroup-last-rmail filename)))
9538
9539 (defun gnus-summary-save-in-mail (&optional filename)
9540   "Append this article to Unix mail file.
9541 Optional argument FILENAME specifies file name.
9542 Directory to save to is default to `gnus-article-save-directory' which
9543 is initialized from the SAVEDIR environment variable."
9544   (interactive)
9545   (let ((default-name
9546           (funcall gnus-mail-save-name gnus-newsgroup-name
9547                    gnus-current-headers gnus-newsgroup-last-mail)))
9548     (or filename
9549         (setq filename (gnus-read-save-file-name 
9550                         "Save in Unix mail file:" default-name)))
9551     (setq filename
9552           (expand-file-name filename
9553                             (and default-name
9554                                  (file-name-directory default-name))))
9555     (gnus-make-directory (file-name-directory filename))
9556     (gnus-eval-in-buffer-window 
9557      gnus-article-buffer
9558      (save-excursion
9559        (save-restriction
9560          (widen)
9561          (if (and (file-readable-p filename) (rmail-file-p filename))
9562              (gnus-output-to-rmail filename)
9563            (rmail-output filename 1 t t)))))
9564     ;; Remember the directory name to save articles.
9565     (setq gnus-newsgroup-last-mail filename)))
9566
9567 (defun gnus-summary-save-in-file (&optional filename)
9568   "Append this article to file.
9569 Optional argument FILENAME specifies file name.
9570 Directory to save to is default to `gnus-article-save-directory' which
9571 is initialized from the SAVEDIR environment variable."
9572   (interactive)
9573   (let ((default-name
9574           (funcall gnus-file-save-name gnus-newsgroup-name
9575                    gnus-current-headers gnus-newsgroup-last-file)))
9576     (or filename
9577         (setq filename (gnus-read-save-file-name 
9578                         "Save in file:" default-name)))
9579     (gnus-make-directory (file-name-directory filename))
9580     (gnus-eval-in-buffer-window 
9581      gnus-article-buffer
9582      (save-excursion
9583        (save-restriction
9584          (widen)
9585          (gnus-output-to-file filename))))
9586     ;; Remember the directory name to save articles.
9587     (setq gnus-newsgroup-last-file filename)))
9588
9589 (defun gnus-summary-save-in-pipe (&optional command)
9590   "Pipe this article to subprocess."
9591   (interactive)
9592   (let ((command (read-string "Shell command on article: "
9593                               gnus-last-shell-command)))
9594     (if (string-equal command "")
9595         (setq command gnus-last-shell-command))
9596     (gnus-eval-in-buffer-window 
9597      gnus-article-buffer
9598      (save-restriction
9599        (widen)
9600        (shell-command-on-region (point-min) (point-max) command nil)))
9601     (setq gnus-last-shell-command command)))
9602
9603 ;; Summary extract commands
9604
9605 (defun gnus-summary-insert-pseudos (pslist)
9606   (let ((buffer-read-only nil)
9607         (article (gnus-summary-article-number))
9608         b)
9609     (or (gnus-summary-goto-subject article)
9610         (error (format "No such article: %d" article)))
9611     (or gnus-newsgroup-headers-hashtb-by-number
9612         (gnus-make-headers-hashtable-by-number))
9613     (gnus-summary-position-cursor)
9614     ;; If all commands are to be bunched up on one line, we collect
9615     ;; them here.  
9616     (if gnus-view-pseudos-separately
9617         ()
9618       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9619             files action)
9620         (while ps
9621           (setq action (cdr (assq 'action (car ps))))
9622           (setq files (list (cdr (assq 'name (car ps)))))
9623           (while (and ps (cdr ps)
9624                       (string= (or action "1")
9625                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9626             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9627             (setcdr ps (cdr (cdr ps))))
9628           (if (not files)
9629               ()
9630             (if (not (string-match "%s" action))
9631                 (setq files (cons " " files)))
9632             (setq files (cons " " files))
9633             (and (assq 'execute (car ps))
9634                  (setcdr (assq 'execute (car ps))
9635                          (funcall (if (string-match "%s" action)
9636                                       'format 'concat)
9637                                   action 
9638                                   (mapconcat (lambda (f) f) files " ")))))
9639           (setq ps (cdr ps)))))
9640     (if gnus-view-pseudos
9641         (while pslist
9642           (and (assq 'execute (car pslist))
9643                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9644                                      (eq gnus-view-pseudos 'not-confirm)))
9645           (setq pslist (cdr pslist)))
9646       (save-excursion
9647         (while pslist
9648           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9649                                          (gnus-summary-article-number)))
9650           (forward-line 1)
9651           (setq b (point))
9652           (insert "          " (file-name-nondirectory 
9653                                 (cdr (assq 'name (car pslist))))
9654                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9655           (add-text-properties 
9656            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9657                           'gnus-mark gnus-unread-mark 
9658                           'gnus-level 0
9659                           'gnus-pseudo (car pslist)))
9660           (forward-line -1)
9661           (gnus-sethash (int-to-string gnus-reffed-article-number)
9662                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9663           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9664           (setq pslist (cdr pslist)))))))
9665
9666 (defun gnus-pseudos< (p1 p2)
9667   (let ((c1 (cdr (assq 'action p1)))
9668         (c2 (cdr (assq 'action p2))))
9669     (and c1 c2 (string< c1 c2))))
9670
9671 (defun gnus-request-pseudo-article (props)
9672   (cond ((assq 'execute props)
9673          (gnus-execute-command (cdr (assq 'execute props)))))
9674   (let ((gnus-current-article (gnus-summary-article-number)))
9675     (run-hooks 'gnus-mark-article-hook)))
9676
9677 (defun gnus-execute-command (command &optional automatic)
9678   (save-excursion
9679     (gnus-article-setup-buffer)
9680     (set-buffer gnus-article-buffer)
9681     (let ((command (if automatic command (read-string "Command: " command)))
9682           (buffer-read-only nil))
9683       (erase-buffer)
9684       (insert "$ " command "\n\n")
9685       (if gnus-view-pseudo-asynchronously
9686           (start-process "gnus-execute" nil "sh" "-c" command)
9687         (call-process "sh" nil t nil "-c" command)))))
9688
9689 (defun gnus-copy-file (file &optional to)
9690   "Copy FILE to TO."
9691   (interactive
9692    (list (read-file-name "Copy file: " default-directory)
9693          (read-file-name "Copy file to: " default-directory)))
9694   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9695   (and (file-directory-p to) 
9696        (setq to (concat (file-name-as-directory to)
9697                         (file-name-nondirectory file))))
9698   (copy-file file to))
9699
9700 ;; Summary kill commands.
9701
9702 (defun gnus-summary-edit-global-kill (article)
9703   "Edit the \"global\" kill file."
9704   (interactive (list (gnus-summary-article-number)))
9705   (gnus-group-edit-global-kill article))
9706
9707 (defun gnus-summary-edit-local-kill ()
9708   "Edit a local kill file applied to the current newsgroup."
9709   (interactive)
9710   (setq gnus-current-headers 
9711         (gnus-gethash 
9712          (int-to-string (gnus-summary-article-number))
9713          gnus-newsgroup-headers-hashtb-by-number))
9714   (gnus-set-global-variables)
9715   (gnus-group-edit-local-kill 
9716    (gnus-summary-article-number) gnus-newsgroup-name))
9717
9718 \f
9719 ;;;
9720 ;;; Gnus article mode
9721 ;;;
9722
9723 (put 'gnus-article-mode 'mode-class 'special)
9724
9725 (if gnus-article-mode-map
9726     nil
9727   (setq gnus-article-mode-map (make-keymap))
9728   (suppress-keymap gnus-article-mode-map)
9729   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9730   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9731   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9732   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9733   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9734   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9735   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9736   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9737   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9738   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9739   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9740   
9741   ;; Duplicate almost all summary keystrokes in the article mode map.
9742   (let ((commands 
9743          (list "#" "\M-#" "\C-c\M-#" "n" "p"
9744                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9745                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9746                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k""x" "X" 
9747                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9748                "t" "\M-t" "C" "S"
9749                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9750                "V" "\C-c\C-d")))
9751     (while commands
9752       (define-key gnus-article-mode-map (car commands) 
9753         'gnus-article-summary-command)
9754       (setq commands (cdr commands))))
9755
9756   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9757     (while commands
9758       (define-key gnus-article-mode-map (car commands) 
9759         'gnus-article-summary-command-nosave)
9760       (setq commands (cdr commands)))))
9761
9762
9763 (defun gnus-article-mode ()
9764   "Major mode for displaying an article.
9765
9766 All normal editing commands are switched off.
9767
9768 The following commands are available:
9769
9770 \\<gnus-article-mode-map>
9771 \\[gnus-article-next-page]\t Scroll the article one page forwards
9772 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9773 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9774 \\[gnus-article-show-summary]\t Display the summary buffer
9775 \\[gnus-article-mail]\t Send a reply to the address near point
9776 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9777 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9778 \\[gnus-info-find-node]\t Go to the Gnus info node"
9779   (interactive)
9780   (if gnus-visual (gnus-article-make-menu-bar))
9781   (kill-all-local-variables)
9782   (setq mode-line-modified "-- ")
9783   (make-local-variable 'mode-line-format)
9784   (setq mode-line-format (copy-sequence mode-line-format))
9785   (and (equal (nth 3 mode-line-format) "   ")
9786        (setcar (nthcdr 3 mode-line-format) ""))
9787   (setq mode-name "Article")
9788   (setq major-mode 'gnus-article-mode)
9789   (make-local-variable 'minor-mode-alist)
9790   (or (assq 'gnus-show-mime minor-mode-alist)
9791       (setq minor-mode-alist
9792             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9793   (use-local-map gnus-article-mode-map)
9794   (make-local-variable 'page-delimiter)
9795   (setq page-delimiter gnus-page-delimiter)
9796   (buffer-disable-undo (current-buffer))
9797   (setq buffer-read-only t)             ;Disable modification
9798   (run-hooks 'gnus-article-mode-hook))
9799
9800 (defun gnus-article-setup-buffer ()
9801   "Initialize article mode buffer."
9802   (if (get-buffer gnus-article-buffer)
9803       (save-excursion
9804         (set-buffer gnus-article-buffer)
9805         (or (eq major-mode 'gnus-article-mode)
9806             (gnus-article-mode)))
9807     (save-excursion
9808       (set-buffer (get-buffer-create gnus-article-buffer))
9809       (gnus-add-current-to-buffer-list)
9810       (gnus-article-mode))))
9811
9812 ;; Set article window start at LINE, where LINE is the number of lines
9813 ;; from the head of the article.
9814 (defun gnus-article-set-window-start (&optional line)
9815   (set-window-start 
9816    (get-buffer-window gnus-article-buffer)
9817    (save-excursion
9818      (set-buffer gnus-article-buffer)
9819      (goto-char (point-min))
9820      (if (not line)
9821          (point-min)
9822        (gnus-message 6 "Moved to bookmark")
9823        (search-forward "\n\n" nil t)
9824        (forward-line line)
9825        (point)))))
9826
9827 (defun gnus-request-article-this-buffer (article group)
9828   "Get an article and insert it into this buffer."
9829   (setq group (or group gnus-newsgroup-name))
9830
9831   ;; Open server if it has closed.
9832   (gnus-check-news-server (gnus-find-method-for-group group))
9833
9834   ;; Using `gnus-request-article' directly will insert the article into
9835   ;; `nntp-server-buffer' - so we'll save some time by not having to
9836   ;; copy it from the server buffer into the article buffer.
9837
9838   ;; We only request an article by message-id when we do not have the
9839   ;; headers for it, so we'll have to get those.
9840   (and (stringp article) 
9841        (let ((gnus-override-method gnus-refer-article-method))
9842          (gnus-read-header article)))
9843
9844   ;; If the article number is negative, that means that this article
9845   ;; doesn't belong in this newsgroup (possibly), so we find its
9846   ;; message-id and request it by id instead of number.
9847   (if (not (numberp article))
9848       ()
9849     (save-excursion
9850       (set-buffer gnus-summary-buffer)
9851       (let ((header (gnus-get-header-by-number article)))
9852         (if (< article 0)
9853             (if (vectorp header)
9854                 ;; It's a real article.
9855                 (setq article (header-id header))
9856               ;; It is an extracted pseudo-article.
9857               (setq article 'pseudo)
9858               (gnus-request-pseudo-article header)))
9859
9860         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9861           (if (not (eq (car method) 'nneething))
9862               ()
9863             (let ((dir (concat (file-name-as-directory (nth 1 method))
9864                                (header-subject header))))
9865               (if (file-directory-p dir)
9866                   (progn
9867                     (setq article 'nneething)
9868                     (gnus-group-enter-directory dir)))))))))
9869
9870   ;; Check the cache.
9871   (if (and gnus-use-cache
9872            (numberp article)
9873            (gnus-cache-request-article article group))
9874       'article
9875     ;; Get the article and into the article buffer.
9876     (if (or (stringp article) (numberp article))
9877         (progn
9878           (erase-buffer)
9879           (let ((gnus-override-method 
9880                  (and (stringp article) gnus-refer-article-method)))
9881             (and (gnus-request-article article group (current-buffer))
9882                  'article)))
9883       article)))
9884
9885 (defun gnus-read-header (id)
9886   "Read the headers of article ID and enter them into the Gnus system."
9887   (or gnus-newsgroup-headers-hashtb-by-number
9888       (gnus-make-headers-hashtable-by-number))
9889   (let (header)
9890     (if (not (setq header 
9891                    (car (if (let ((gnus-nov-is-evil t))
9892                               (gnus-retrieve-headers 
9893                                (list id) gnus-newsgroup-name))
9894                             (gnus-get-newsgroup-headers)))))
9895         nil
9896       (if (stringp id)
9897           (header-set-number header gnus-reffed-article-number))
9898       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
9899       (gnus-sethash (int-to-string (header-number header)) header
9900                     gnus-newsgroup-headers-hashtb-by-number)
9901       (if (stringp id)
9902           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
9903       (setq gnus-current-headers header)
9904       header)))
9905
9906 (defun gnus-article-prepare (article &optional all-headers header)
9907   "Prepare ARTICLE in article mode buffer.
9908 ARTICLE should either be an article number or a Message-ID.
9909 If ARTICLE is an id, HEADER should be the article headers.
9910 If ALL-HEADERS is non-nil, no headers are hidden."
9911   (save-excursion
9912     ;; Make sure we start in a summary buffer.
9913     (or (eq major-mode 'gnus-summary-mode)
9914         (set-buffer gnus-summary-buffer))
9915     (setq gnus-summary-buffer (current-buffer))
9916     ;; Make sure the connection to the server is alive.
9917     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
9918         (progn
9919           (gnus-check-news-server 
9920            (gnus-find-method-for-group gnus-newsgroup-name))
9921           (gnus-request-group gnus-newsgroup-name t)))
9922     (or gnus-newsgroup-headers-hashtb-by-number
9923         (gnus-make-headers-hashtable-by-number))
9924     (let* ((article (if header (header-number header) article))
9925            (summary-buffer (current-buffer))
9926            (internal-hook gnus-article-internal-prepare-hook)
9927            (group gnus-newsgroup-name)
9928            result)
9929       (save-excursion
9930         (gnus-article-setup-buffer)
9931         (set-buffer gnus-article-buffer)
9932         (if (not (setq result (let ((buffer-read-only nil))
9933                                 (gnus-request-article-this-buffer 
9934                                  article group))))
9935             ;; There is no such article.
9936             (progn
9937               (save-excursion
9938                 (set-buffer gnus-summary-buffer)
9939                 (setq gnus-current-article 0)
9940                 (and (numberp article) 
9941                      (gnus-summary-mark-as-read article gnus-canceled-mark))
9942                 (gnus-message 1 "No such article (may be canceled)")
9943                 (and (numberp article)
9944                      (setq gnus-current-article article))
9945                 (ding))
9946               (and (numberp article)
9947                    (setq gnus-article-current 
9948                          (cons gnus-newsgroup-name article)))
9949               nil)
9950           (if (or (eq result 'pseudo) (eq result 'nneething))
9951               (progn
9952                 (save-excursion
9953                   (set-buffer summary-buffer)
9954                   (setq gnus-last-article gnus-current-article
9955                         gnus-newsgroup-history (cons gnus-current-article
9956                                                      gnus-newsgroup-history)
9957                         gnus-current-article 0
9958                         gnus-current-headers nil
9959                         gnus-article-current nil)
9960                   (if (eq result 'nneething)
9961                       (gnus-configure-windows 'summary)
9962                     (gnus-configure-windows 'article))
9963                   (gnus-set-global-variables))
9964                 (gnus-set-mode-line 'article))
9965             ;; The result from the `request' was an actual article -
9966             ;; or at least some text that is now displayed in the
9967             ;; article buffer.
9968             (if (and (numberp article)
9969                      (not (eq article gnus-current-article)))
9970                 ;; Seems like a new article has been selected.
9971                 ;; `gnus-current-article' must be an article number.
9972                 (save-excursion
9973                   (set-buffer summary-buffer)
9974                   (setq gnus-last-article gnus-current-article
9975                         gnus-newsgroup-history (cons gnus-current-article
9976                                                      gnus-newsgroup-history)
9977                         gnus-current-article article
9978                         gnus-current-headers 
9979                         (gnus-get-header-by-number gnus-current-article)
9980                         gnus-article-current 
9981                         (cons gnus-newsgroup-name gnus-current-article))
9982                   (gnus-summary-show-thread)
9983                   (run-hooks 'gnus-mark-article-hook)
9984                   (gnus-set-mode-line 'summary)
9985                   (and gnus-visual 
9986                        (run-hooks 'gnus-visual-mark-article-hook))
9987                   ;; Set the global newsgroup variables here.
9988                   ;; Suggested by Jim Sisolak
9989                   ;; <sisolak@trans4.neep.wisc.edu>.
9990                   (gnus-set-global-variables)
9991                   (and gnus-use-cache 
9992                        (gnus-cache-possibly-enter-article
9993                         group article
9994                         (gnus-get-header-by-number article)
9995                         (memq article gnus-newsgroup-marked)
9996                         (memq article gnus-newsgroup-dormant)
9997                         (memq article gnus-newsgroup-unreads)))))
9998             ;; gnus-have-all-headers must be either T or NIL.
9999             (setq gnus-have-all-headers
10000                   (not (not (or all-headers gnus-show-all-headers))))
10001             ;; Hooks for getting information from the article.
10002             ;; This hook must be called before being narrowed.
10003             (let (buffer-read-only)
10004               (run-hooks 'internal-hook)
10005               (run-hooks 'gnus-article-prepare-hook)
10006               ;; Decode MIME message.
10007               (if (and gnus-show-mime
10008                        (gnus-fetch-field "Mime-Version"))
10009                   (funcall gnus-show-mime-method))
10010               ;; Perform the article display hooks.
10011               (run-hooks 'gnus-article-display-hook))
10012             ;; Do page break.
10013             (goto-char (point-min))
10014             (and gnus-break-pages (gnus-narrow-to-page))
10015             (gnus-set-mode-line 'article)
10016             (gnus-configure-windows 'article)
10017             (goto-char (point-min))
10018             t))))))
10019
10020 (defun gnus-article-show-all-headers ()
10021   "Show all article headers in article mode buffer."
10022   (save-excursion 
10023     (gnus-article-setup-buffer)
10024     (set-buffer gnus-article-buffer)
10025     (let ((buffer-read-only nil))
10026       (remove-text-properties (point-min) (point-max) 
10027                               gnus-hidden-properties))))
10028
10029 (defun gnus-article-hide-headers-if-wanted ()
10030   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10031 Provided for backwards compatability."
10032   (or gnus-have-all-headers
10033       (gnus-article-hide-headers)))
10034
10035 (defun gnus-article-hide-headers (&optional delete)
10036   "Hide unwanted headers and possibly sort them as well."
10037   (interactive "P")
10038   (save-excursion
10039     (set-buffer gnus-article-buffer)
10040     (save-restriction
10041       (let ((sorted gnus-sorted-header-list)
10042             (buffer-read-only nil)
10043             want want-list beg want-l)
10044         ;; First we narrow to just the headers.
10045         (widen)
10046         (goto-char (point-min))
10047         ;; Hide any "From " lines at the beginning of (mail) articles. 
10048         (while (looking-at "From ")
10049           (forward-line 1))
10050         (if (bobp) 
10051             (add-text-properties (point-min) (point) gnus-hidden-properties))
10052         ;; Then treat the rest of the header lines.
10053         (narrow-to-region 
10054          (point) 
10055          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10056         ;; Then we use the two regular expressions
10057         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10058         ;; select which header lines is to remain visible in the
10059         ;; article buffer.
10060         (goto-char (point-min))
10061         (while (re-search-forward "^[^ \t]*:" nil t)
10062           (beginning-of-line)
10063           ;; We add the headers we want to keep to a list and delete
10064           ;; them from the buffer.
10065           (if (or (and (stringp gnus-visible-headers)
10066                        (looking-at gnus-visible-headers))
10067                   (and (not (stringp gnus-visible-headers))
10068                        (stringp gnus-ignored-headers)
10069                        (not (looking-at gnus-ignored-headers))))
10070               (progn
10071                 (setq beg (point))
10072                 (forward-line 1)
10073                 ;; Be sure to get multi-line headers...
10074                 (re-search-forward "^[^ \t]*:" nil t)
10075                 (beginning-of-line)
10076                 (setq want-list 
10077                       (cons (buffer-substring beg (point)) want-list))
10078                 (delete-region beg (point))
10079                 (goto-char beg))
10080             (forward-line 1)))
10081         ;; Next we perform the sorting by looking at
10082         ;; `gnus-sorted-header-list'. 
10083         (goto-char (point-min))
10084         (while (and sorted want-list)
10085           (setq want-l want-list)
10086           (while (and want-l
10087                       (not (string-match (car sorted) (car want-l))))
10088             (setq want-l (cdr want-l)))
10089           (if want-l 
10090               (progn
10091                 (insert (car want-l))
10092                 (setq want-list (delq (car want-l) want-list))))
10093           (setq sorted (cdr sorted)))
10094         ;; Any headers that were not matched by the sorted list we
10095         ;; just tack on the end of the visible header list.
10096         (while want-list
10097           (insert (car want-list))
10098           (setq want-list (cdr want-list)))
10099         ;; And finally we make the unwanted headers invisible.
10100         (if delete
10101             (delete-region (point) (point-max))
10102           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10103           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10104
10105 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10106 (defun gnus-article-treat-overstrike ()
10107   "Translate overstrikes into bold text."
10108   (interactive)
10109   (save-excursion
10110     (set-buffer gnus-article-buffer)
10111     (let ((buffer-read-only nil))
10112       (while (search-forward "\b" nil t)
10113         (let ((next (following-char))
10114               (previous (char-after (- (point) 2))))
10115           (cond ((eq next previous)
10116                  (delete-region (- (point) 2) (point))
10117                  (put-text-property (point) (1+ (point))
10118                                     'face 'bold))
10119                 ((eq next ?_)
10120                  (delete-region (1- (point)) (1+ (point)))
10121                  (put-text-property (1- (point)) (point)
10122                                     'face 'underline))
10123                 ((eq previous ?_)
10124                  (delete-region (- (point) 2) (point))
10125                  (put-text-property (point) (1+ (point))
10126                                     'face 'underline))))))))
10127
10128 (defun gnus-article-word-wrap ()
10129   "Format too long lines."
10130   (interactive)
10131   (save-excursion
10132     (set-buffer gnus-article-buffer)
10133     (let ((buffer-read-only nil))
10134       (goto-char (point-min))
10135       (search-forward "\n\n" nil t)
10136       (end-of-line 1)
10137       (let ((paragraph-start "^\\W"))
10138         (while (not (eobp))
10139           (and (>= (current-column) (min fill-column (window-width)))
10140                (/= (preceding-char) ?:)
10141                (fill-paragraph nil))
10142           (end-of-line 2))))))
10143
10144 (defun gnus-article-remove-cr ()
10145   "Remove carriage returns from an article."
10146   (interactive)
10147   (save-excursion
10148     (set-buffer gnus-article-buffer)
10149     (let ((buffer-read-only nil))
10150       (goto-char (point-min))
10151       (while (search-forward "\r" nil t)
10152         (replace-match "" t t)))))
10153
10154 (defun gnus-article-display-x-face (&optional force)
10155   "Look for an X-Face header and display it if present."
10156   (interactive (list 'force))
10157   (save-excursion
10158     (set-buffer gnus-article-buffer)
10159     (let ((inhibit-point-motion-hooks t)
10160           (case-fold-search nil))
10161       (save-restriction
10162         (goto-char (point-min))
10163         (search-forward "\n\n")
10164         (narrow-to-region (point-min) (point))
10165         (goto-char (point-min))
10166         (if (or (not gnus-article-x-face-command)
10167                 (and (not force)
10168                      (or (not gnus-article-x-face-too-ugly)
10169                          (string-match gnus-article-x-face-too-ugly
10170                                        (mail-fetch-field "from"))))
10171                 (progn
10172                   (goto-char (point-min))
10173                   (not (re-search-forward "^X-Face: " nil t))))
10174             nil
10175           (let ((beg (point))
10176                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10177             (if (symbolp gnus-article-x-face-command)
10178                 (and (or (fboundp gnus-article-x-face-command)
10179                          (error "%s is not a function"
10180                                 gnus-article-x-face-command))
10181                      (funcall gnus-article-x-face-command beg end))
10182               (call-process-region beg end "sh" nil 0 nil
10183                                    "-c" gnus-article-x-face-command))))))))
10184
10185 (defun gnus-article-de-quoted-unreadable (&optional force)
10186   "Do a naïve translation of a quoted-printable-encoded article.
10187 This is in no way, shape or form meant as a replacement for real MIME
10188 processing, but is simply a stop-gap measure until MIME support is
10189 written.
10190 If FORCE, decode the article whether it is marked as quoted-printable
10191 or not." 
10192   (interactive (list 'force))
10193   (save-excursion
10194     (set-buffer gnus-article-buffer)
10195     (let ((case-fold-search t)
10196           (buffer-read-only nil)
10197           (type (gnus-fetch-field "content-transfer-encoding")))
10198       (if (or force (and type (string-match "quoted-printable" type)))
10199           (progn
10200             (goto-char (point-min))
10201             (search-forward "\n\n" nil 'move)
10202             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10203
10204 (defun gnus-mime-decode-quoted-printable (from to)
10205   ;; Decode quoted-printable from region between FROM and TO.
10206   (save-excursion
10207     (goto-char from)
10208     (while (search-forward "=" to t)
10209       (cond ((eq (following-char) ?\n)
10210              (delete-char -1)
10211              (delete-char 1))
10212             ((looking-at "[0-9A-F][0-9A-F]")
10213              (delete-char -1)
10214              (insert (hexl-hex-string-to-integer
10215                       (buffer-substring (point) (+ 2 (point)))))
10216              (delete-char 2))
10217             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10218
10219 (defun gnus-article-date-ut (&optional type)
10220   "Convert DATE date to universal time in the current article.
10221 If TYPE is `local', convert to local time; if it is `lapsed', output
10222 how much time has lapsed since DATE."
10223   (interactive (list 'ut))
10224   (let ((date (header-date (or gnus-current-headers 
10225                                (gnus-get-header-by-number
10226                                 (gnus-summary-article-number))"")))
10227         (date-regexp "^Date: \\|^X-Sent: "))
10228     (if (not date)
10229         ()
10230       (save-excursion
10231         (set-buffer gnus-article-buffer)
10232         (let ((buffer-read-only nil))
10233           (goto-char (point-min))
10234           (if (and (re-search-forward date-regexp nil t)
10235                    (progn 
10236                      (beginning-of-line)
10237                      (looking-at date-regexp)))
10238               (delete-region (gnus-point-at-bol)
10239                              (progn (end-of-line) (1+ (point))))
10240             (goto-char (point-min))
10241             (goto-char (- (search-forward "\n\n") 2)))
10242           (insert
10243            (cond 
10244             ((eq type 'local)
10245              (concat "Date: " (timezone-make-date-arpa-standard date) "\n"))
10246             ((eq type 'ut)
10247              (concat "Date: " (timezone-make-date-arpa-standard date nil "UT")
10248                      "\n"))
10249             ((eq type 'lapsed)
10250              (let* ((sec (- (gnus-seconds-since-epoch 
10251                              (timezone-make-date-arpa-standard
10252                               (current-time-string) (current-time-zone) "UT"))
10253                             (gnus-seconds-since-epoch 
10254                              (timezone-make-date-arpa-standard date nil "UT"))))
10255                     (units (list (cons 'year (* 365.25 24 60 60))
10256                                  (cons 'week (* 7 24 60 60))
10257                                  (cons 'day (* 24 60 60))
10258                                  (cons 'hour (* 60 60))
10259                                  (cons 'minute 60)
10260                                  (cons 'second 1)))
10261                     num prev)
10262                (concat
10263                 "X-Sent: "
10264                 (mapconcat 
10265                  (lambda (unit)
10266                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10267                        ""
10268                      (setq sec (- sec (* num (cdr unit))))
10269                      (prog1
10270                          (concat (if prev ", " "") (int-to-string (floor num))
10271                                  " " (symbol-name (car unit))
10272                                  (if (> num 1) "s" ""))
10273                        (setq prev t))))
10274                  units "")
10275                 " ago\n")))
10276             (t
10277              (error "Unknown conversion type: %s" type)))))))))
10278
10279 (defun gnus-article-date-local ()
10280   "Convert the current article date to the local timezone."
10281   (interactive)
10282   (gnus-article-date-ut 'local))
10283
10284 (defun gnus-article-date-lapsed ()
10285   "Convert the current article date to time lapsed since it was sent."
10286   (interactive)
10287   (gnus-article-date-ut 'lapsed))
10288
10289 (defun gnus-article-maybe-highlight ()
10290   (if gnus-visual (gnus-article-highlight)))
10291
10292 ;; Article savers.
10293
10294 (defun gnus-output-to-rmail (file-name)
10295   "Append the current article to an Rmail file named FILE-NAME."
10296   (require 'rmail)
10297   ;; Most of these codes are borrowed from rmailout.el.
10298   (setq file-name (expand-file-name file-name))
10299   (setq rmail-default-rmail-file file-name)
10300   (let ((artbuf (current-buffer))
10301         (tmpbuf (get-buffer-create " *Gnus-output*")))
10302     (save-excursion
10303       (or (get-file-buffer file-name)
10304           (file-exists-p file-name)
10305           (if (gnus-yes-or-no-p
10306                (concat "\"" file-name "\" does not exist, create it? "))
10307               (let ((file-buffer (create-file-buffer file-name)))
10308                 (save-excursion
10309                   (set-buffer file-buffer)
10310                   (rmail-insert-rmail-file-header)
10311                   (let ((require-final-newline nil))
10312                     (write-region (point-min) (point-max) file-name t 1)))
10313                 (kill-buffer file-buffer))
10314             (error "Output file does not exist")))
10315       (set-buffer tmpbuf)
10316       (buffer-disable-undo (current-buffer))
10317       (erase-buffer)
10318       (insert-buffer-substring artbuf)
10319       (gnus-convert-article-to-rmail)
10320       ;; Decide whether to append to a file or to an Emacs buffer.
10321       (let ((outbuf (get-file-buffer file-name)))
10322         (if (not outbuf)
10323             (append-to-file (point-min) (point-max) file-name)
10324           ;; File has been visited, in buffer OUTBUF.
10325           (set-buffer outbuf)
10326           (let ((buffer-read-only nil)
10327                 (msg (and (boundp 'rmail-current-message)
10328                           (symbol-value 'rmail-current-message))))
10329             ;; If MSG is non-nil, buffer is in RMAIL mode.
10330             (if msg
10331                 (progn (widen)
10332                        (narrow-to-region (point-max) (point-max))))
10333             (insert-buffer-substring tmpbuf)
10334             (if msg
10335                 (progn
10336                   (goto-char (point-min))
10337                   (widen)
10338                   (search-backward "\^_")
10339                   (narrow-to-region (point) (point-max))
10340                   (goto-char (1+ (point-min)))
10341                   (rmail-count-new-messages t)
10342                   (rmail-show-message msg)))))))
10343     (kill-buffer tmpbuf)))
10344
10345 (defun gnus-output-to-file (file-name)
10346   "Append the current article to a file named FILE-NAME."
10347   (setq file-name (expand-file-name file-name))
10348   (let ((artbuf (current-buffer))
10349         (tmpbuf (get-buffer-create " *Gnus-output*")))
10350     (save-excursion
10351       (set-buffer tmpbuf)
10352       (buffer-disable-undo (current-buffer))
10353       (erase-buffer)
10354       (insert-buffer-substring artbuf)
10355       ;; Append newline at end of the buffer as separator, and then
10356       ;; save it to file.
10357       (goto-char (point-max))
10358       (insert "\n")
10359       (append-to-file (point-min) (point-max) file-name))
10360     (kill-buffer tmpbuf)))
10361
10362 (defun gnus-convert-article-to-rmail ()
10363   "Convert article in current buffer to Rmail message format."
10364   (let ((buffer-read-only nil))
10365     ;; Convert article directly into Babyl format.
10366     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10367     (goto-char (point-min))
10368     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10369     (while (search-forward "\n\^_" nil t) ;single char
10370       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10371     (goto-char (point-max))
10372     (insert "\^_")))
10373
10374 (defun gnus-narrow-to-page (&optional arg)
10375   "Make text outside current page invisible except for page delimiter.
10376 A numeric arg specifies to move forward or backward by that many pages,
10377 thus showing a page other than the one point was originally in."
10378   (interactive "P")
10379   (setq arg (if arg (prefix-numeric-value arg) 0))
10380   (save-excursion
10381     (forward-page -1)                   ;Beginning of current page.
10382     (widen)
10383     (if (> arg 0)
10384         (forward-page arg)
10385       (if (< arg 0)
10386           (forward-page (1- arg))))
10387     ;; Find the end of the page.
10388     (forward-page)
10389     ;; If we stopped due to end of buffer, stay there.
10390     ;; If we stopped after a page delimiter, put end of restriction
10391     ;; at the beginning of that line.
10392     ;; These are commented out.
10393     ;;    (if (save-excursion (beginning-of-line)
10394     ;;                  (looking-at page-delimiter))
10395     ;;  (beginning-of-line))
10396     (narrow-to-region (point)
10397                       (progn
10398                         ;; Find the top of the page.
10399                         (forward-page -1)
10400                         ;; If we found beginning of buffer, stay there.
10401                         ;; If extra text follows page delimiter on same line,
10402                         ;; include it.
10403                         ;; Otherwise, show text starting with following line.
10404                         (if (and (eolp) (not (bobp)))
10405                             (forward-line 1))
10406                         (point)))))
10407
10408 (defun gnus-gmt-to-local ()
10409   "Rewrite Date header described in GMT to local in current buffer.
10410 Intended to be used with gnus-article-prepare-hook."
10411   (save-excursion
10412     (save-restriction
10413       (widen)
10414       (goto-char (point-min))
10415       (narrow-to-region (point-min)
10416                         (progn (search-forward "\n\n" nil 'move) (point)))
10417       (goto-char (point-min))
10418       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10419           (let ((buffer-read-only nil)
10420                 (date (buffer-substring (match-beginning 1) (match-end 1))))
10421             (delete-region (match-beginning 1) (match-end 1))
10422             (insert
10423              (timezone-make-date-arpa-standard 
10424               date nil (current-time-zone))))))))
10425
10426
10427 ;; Article mode commands
10428
10429 (defun gnus-article-next-page (lines)
10430   "Show next page of current article.
10431 If end of article, return non-nil. Otherwise return nil.
10432 Argument LINES specifies lines to be scrolled up."
10433   (interactive "P")
10434   (move-to-window-line -1)
10435   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10436   (if (save-excursion
10437         (end-of-line)
10438         (and (pos-visible-in-window-p)  ;Not continuation line.
10439              (eobp)))
10440       ;; Nothing in this page.
10441       (if (or (not gnus-break-pages)
10442               (save-excursion
10443                 (save-restriction
10444                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10445           t                             ;Nothing more.
10446         (gnus-narrow-to-page 1)         ;Go to next page.
10447         nil)
10448     ;; More in this page.
10449     (condition-case ()
10450         (scroll-up lines)
10451       (end-of-buffer
10452        ;; Long lines may cause an end-of-buffer error.
10453        (goto-char (point-max))))
10454     nil))
10455
10456 (defun gnus-article-prev-page (lines)
10457   "Show previous page of current article.
10458 Argument LINES specifies lines to be scrolled down."
10459   (interactive "P")
10460   (move-to-window-line 0)
10461   (if (and gnus-break-pages
10462            (bobp)
10463            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10464       (progn
10465         (gnus-narrow-to-page -1) ;Go to previous page.
10466         (goto-char (point-max))
10467         (recenter -1))
10468     (scroll-down lines)))
10469
10470 (defun gnus-article-refer-article ()
10471   "Read article specified by message-id around point."
10472   (interactive)
10473   (search-forward ">" nil t)    ;Move point to end of "<....>".
10474   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10475       (let ((message-id
10476              (buffer-substring (match-beginning 1) (match-end 1))))
10477         (set-buffer gnus-summary-buffer)
10478         (gnus-summary-refer-article message-id))
10479     (error "No references around point")))
10480
10481 (defun gnus-article-show-summary ()
10482   "Reconfigure windows to show summary buffer."
10483   (interactive)
10484   (gnus-configure-windows 'article)
10485   (gnus-summary-goto-subject gnus-current-article))
10486
10487 (defun gnus-article-describe-briefly ()
10488   "Describe article mode commands briefly."
10489   (interactive)
10490   (gnus-message 6
10491    (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")))
10492
10493 (defun gnus-article-summary-command ()
10494   "Execute the last keystroke in the summary buffer."
10495   (interactive)
10496   (let ((obuf (current-buffer))
10497         (owin (current-window-configuration))
10498         func)
10499     (switch-to-buffer gnus-summary-buffer 'norecord)
10500     (setq func (lookup-key (current-local-map) (this-command-keys)))
10501     (call-interactively func)
10502     (set-buffer obuf)
10503     (let ((npoint (point)))
10504       (set-window-configuration owin)
10505       (set-window-start (get-buffer-window (current-buffer)) (point)))))
10506
10507 (defun gnus-article-summary-command-nosave ()
10508   "Execute the last keystroke in the summary buffer."
10509   (interactive)
10510   (let ((obuf (current-buffer))
10511         (owin (current-window-configuration))
10512         func)
10513     (switch-to-buffer gnus-summary-buffer 'norecord)
10514     (setq func (lookup-key (current-local-map) (this-command-keys)))
10515     (call-interactively func)))
10516
10517 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10518 ;; Modified by tower@prep Nov 86
10519 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10520
10521 (defun gnus-caesar-region (&optional n)
10522   "Caesar rotation of region by N, default 13, for decrypting netnews.
10523 ROT47 will be performed for Japanese text in any case."
10524   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10525                    (list (prefix-numeric-value current-prefix-arg))
10526                  (list nil)))
10527   (cond ((not (numberp n)) (setq n 13))
10528         (t (setq n (mod n 26))))        ;canonicalize N
10529   (if (not (zerop n))           ; no action needed for a rot of 0
10530       (progn
10531         (if (or (not (boundp 'caesar-translate-table))
10532                 (not caesar-translate-table)
10533                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10534             (let ((i 0) 
10535                   (lower "abcdefghijklmnopqrstuvwxyz")
10536                   upper)
10537               (gnus-message 9 "Building caesar-translate-table...")
10538               (setq caesar-translate-table (make-vector 256 0))
10539               (while (< i 256)
10540                 (aset caesar-translate-table i i)
10541                 (setq i (1+ i)))
10542               (setq lower (concat lower lower)
10543                     upper (upcase lower)
10544                     i 0)
10545               (while (< i 26)
10546                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10547                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10548                 (setq i (1+ i)))
10549               ;; ROT47 for Japanese text.
10550               ;; Thanks to ichikawa@flab.fujitsu.junet.
10551               (setq i 161)
10552               (let ((t1 (logior ?O 128))
10553                     (t2 (logior ?! 128))
10554                     (t3 (logior ?~ 128)))
10555                 (while (< i 256)
10556                   (aset caesar-translate-table i
10557                         (let ((v (aref caesar-translate-table i)))
10558                           (if (<= v t1) (if (< v t2) v (+ v 47))
10559                             (if (<= v t3) (- v 47) v))))
10560                   (setq i (1+ i))))
10561               (gnus-message 9 "Building caesar-translate-table...done")))
10562         (let ((from (region-beginning))
10563               (to (region-end))
10564               (i 0) str len)
10565           (setq str (buffer-substring from to))
10566           (setq len (length str))
10567           (while (< i len)
10568             (aset str i (aref caesar-translate-table (aref str i)))
10569             (setq i (1+ i)))
10570           (goto-char from)
10571           (delete-region from to)
10572           (insert str)))))
10573
10574 \f
10575 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10576
10577 ;;;###autoload
10578 (defalias 'gnus-batch-kill 'gnus-batch-score)
10579 ;;;###autoload
10580 (defun gnus-batch-score ()
10581   "Run batched scoring.
10582 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10583 Newsgroups is a list of strings in Bnews format.  If you want to score
10584 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10585 score the alt hierarchy, you'd say \"!alt.all\"."
10586   (interactive)
10587   (let* ((yes-and-no
10588           (gnus-newsrc-parse-options
10589            (apply (function concat)
10590                   (mapcar (lambda (g) (concat g " "))
10591                           command-line-args-left))))
10592          (gnus-expert-user t)
10593          (nnmail-spool-file nil)
10594          (gnus-use-dribble-file nil)
10595          (yes (car yes-and-no))
10596          (no (cdr yes-and-no))
10597          group subscribed newsrc entry
10598          ;; Disable verbose message.
10599          gnus-novice-user gnus-large-newsgroup)
10600     ;; Eat all arguments.
10601     (setq command-line-args-left nil)
10602     ;; Start Gnus.
10603     (gnus)
10604     ;; Apply kills to specified newsgroups in command line arguments.
10605     (setq newsrc (cdr gnus-newsrc-alist))
10606     (while newsrc
10607       (setq group (car (car newsrc)))
10608       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10609       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10610                (and (car entry)
10611                     (or (eq (car entry) t)
10612                         (not (zerop (car entry)))))
10613                (if yes (string-match yes group) t)
10614                (or (null no) (not (string-match no group))))
10615           (progn
10616             (gnus-summary-read-group group nil t)
10617             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10618                  (gnus-summary-exit))))
10619       (setq newsrc (cdr newsrc)))
10620     ;; Exit Emacs.
10621     (switch-to-buffer gnus-group-buffer)
10622     (gnus-group-save-newsrc)))
10623
10624 (defun gnus-apply-kill-file ()
10625   "Apply a kill file to the current newsgroup.
10626 Returns the number of articles marked as read."
10627   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10628           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10629       (gnus-apply-kill-file-internal)
10630     0))
10631
10632 (defun gnus-kill-save-kill-buffer ()
10633   (save-excursion
10634     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10635       (if (get-file-buffer file)
10636           (progn
10637             (set-buffer (get-file-buffer file))
10638             (and (buffer-modified-p) (save-buffer))
10639             (kill-buffer (current-buffer)))))))
10640
10641 (defvar gnus-kill-file-name "KILL"
10642   "Suffix of the kill files.")
10643
10644 (defun gnus-newsgroup-kill-file (newsgroup)
10645   "Return the name of a kill file name for NEWSGROUP.
10646 If NEWSGROUP is nil, return the global kill file name instead."
10647   (cond ((or (null newsgroup)
10648              (string-equal newsgroup ""))
10649          ;; The global KILL file is placed at top of the directory.
10650          (expand-file-name gnus-kill-file-name
10651                            (or gnus-kill-files-directory "~/News")))
10652         ((gnus-use-long-file-name 'not-kill)
10653          ;; Append ".KILL" to newsgroup name.
10654          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10655                            (or gnus-kill-files-directory "~/News")))
10656         (t
10657          ;; Place "KILL" under the hierarchical directory.
10658          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10659                                    "/" gnus-kill-file-name)
10660                            (or gnus-kill-files-directory "~/News")))))
10661
10662 \f
10663 ;;;
10664 ;;; Dribble file
10665 ;;;
10666
10667 (defvar gnus-dribble-ignore nil)
10668
10669 (defun gnus-dribble-file-name ()
10670   (concat gnus-startup-file "-dribble"))
10671
10672 (defun gnus-dribble-open ()
10673   (save-excursion 
10674     (set-buffer 
10675      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10676     (buffer-disable-undo (current-buffer))
10677     (bury-buffer gnus-dribble-buffer)
10678     (auto-save-mode t)
10679     (goto-char (point-max))))
10680
10681 (defun gnus-dribble-enter (string)
10682   (if (and (not gnus-dribble-ignore)
10683            gnus-dribble-buffer
10684            (buffer-name gnus-dribble-buffer))
10685       (let ((obuf (current-buffer)))
10686         (set-buffer gnus-dribble-buffer)
10687         (insert string "\n")
10688         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10689         (set-buffer obuf))))
10690
10691 (defun gnus-dribble-read-file ()
10692   (let ((dribble-file (gnus-dribble-file-name)))
10693     (save-excursion 
10694       (set-buffer (setq gnus-dribble-buffer 
10695                         (get-buffer-create 
10696                          (file-name-nondirectory dribble-file))))
10697       (gnus-add-current-to-buffer-list)
10698       (erase-buffer)
10699       (set-visited-file-name dribble-file)
10700       (buffer-disable-undo (current-buffer))
10701       (bury-buffer (current-buffer))
10702       (set-buffer-modified-p nil)
10703       (let ((auto (make-auto-save-file-name))
10704             (gnus-dribble-ignore t))
10705         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10706             (progn
10707               (if (file-newer-than-file-p auto dribble-file)
10708                   (setq dribble-file auto))
10709               (insert-file-contents dribble-file)
10710               (if (not (zerop (buffer-size)))
10711                   (set-buffer-modified-p t))
10712               (if (gnus-y-or-n-p 
10713                    "Auto-save file exists. Do you want to read it? ")
10714                   (progn
10715                     (gnus-message 5 "Reading %s..." dribble-file) 
10716                     (eval-current-buffer)
10717                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10718
10719 (defun gnus-dribble-delete-file ()
10720   (if (file-exists-p (gnus-dribble-file-name))
10721       (delete-file (gnus-dribble-file-name)))
10722   (if gnus-dribble-buffer
10723       (save-excursion
10724         (set-buffer gnus-dribble-buffer)
10725         (let ((auto (make-auto-save-file-name)))
10726           (if (file-exists-p auto)
10727               (delete-file auto))
10728           (erase-buffer)
10729           (set-buffer-modified-p nil)))))
10730
10731 (defun gnus-dribble-save ()
10732   (if (and gnus-dribble-buffer
10733            (buffer-name gnus-dribble-buffer))
10734       (save-excursion
10735         (set-buffer gnus-dribble-buffer)
10736         (save-buffer))))
10737
10738 (defun gnus-dribble-clear ()
10739   (save-excursion
10740     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10741         (progn
10742           (set-buffer gnus-dribble-buffer)
10743           (erase-buffer)
10744           (set-buffer-modified-p nil)
10745           (setq buffer-saved-size (buffer-size))))))
10746
10747 ;;;
10748 ;;; Server Communication
10749 ;;;
10750
10751 ;; All the Gnus backends have the same interface, and should return
10752 ;; data in a similar format. Below is an overview of what functions
10753 ;; these packages must supply and what results they should return.
10754 ;;
10755 ;; Variables:
10756 ;;
10757 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10758 ;; buffer. 
10759 ;;
10760 ;; Functions for the imaginary backend `choke':
10761 ;;
10762 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10763 ;; Should return all headers for all ARTICLES, or return NOV lines for
10764 ;; the same.
10765 ;;
10766 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10767 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10768 ;; must be returned.
10769 ;;
10770 ;; `choke-close-group GROUP &optional SERVER'
10771 ;; Close group. Most backends won't have to do anything with this
10772 ;; call, but it is an opportunity to clean up, if that is needed. It
10773 ;; is called when Gnus exits a group.
10774 ;;
10775 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10776 ;; Return ARTICLE, which is either an article number or
10777 ;; message-id. Note that not all backends can return articles based on
10778 ;; message-id. 
10779 ;;
10780 ;; `choke-request-list SERVER'
10781 ;; Return a list of all newsgroups on SERVER.
10782 ;;
10783 ;; `choke-request-list-newsgroups SERVER'
10784 ;; Return a list of descriptions of all newsgroups on SERVER.
10785 ;;
10786 ;; `choke-request-newgroups DATE &optional SERVER'
10787 ;; Return a list of all groups that have arrived after DATE on
10788 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10789 ;; always check whether the groups are old or not. Backends that do
10790 ;; not store date information may just return the entire list of
10791 ;; groups, although this might not be a good idea in general.
10792 ;;
10793 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10794 ;; Should return a buffer that is suitable for "posting". nnspool and
10795 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10796 ;; buffer. This function should fill out the appropriate headers. 
10797 ;;
10798 ;; `choke-request-post &optional SERVER'
10799 ;; Function that will be called from a buffer to be posted. 
10800 ;;
10801 ;; `choke-open-server SERVER &optional ARGUMENT'
10802 ;; Open a connection to SERVER.
10803 ;;
10804 ;; `choke-close-server &optional SERVER'
10805 ;; Close the connection to SERVER.
10806 ;;
10807 ;; `choke-server-opened &optional SERVER'
10808 ;; Whether the conenction to SERVER is opened or not.
10809 ;;
10810 ;; `choke-server-status &optional SERVER'
10811 ;; Should return a status string (not in the nntp buffer, but as the
10812 ;; result of the function).
10813 ;;
10814 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10815 ;; Optional function for retrieving active file info on all groups in
10816 ;; GROUPS.  Two return formats are supported: The normal active file
10817 ;; format, and a list of GROUP lines.  This function should return (as
10818 ;; a function value) either `active' or `group', depending on what
10819 ;; format it returns.
10820 ;;
10821 ;; The following functions are optional and apply only to backends
10822 ;; that are able to control the contents of their groups totally
10823 ;; (ie. mail backends.)  Backends that aren't able to do that
10824 ;; shouldn't define these functions at all. Gnus will check for their
10825 ;; presence before attempting to call them.
10826 ;;
10827 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10828 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10829 ;; backends will not be able to expire articles. Should return a list
10830 ;; of all articles that were not expired.
10831 ;;
10832 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10833 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10834 ;; Removes any information it has added to the article (extra headers,
10835 ;; whatever - make it as clean as possible), and then passes the
10836 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
10837 ;; function described below. If the ACCEPT-FORM returns a non-nil
10838 ;; value, the article should then be deleted. If LAST is nil, that
10839 ;; means that there will be further calls to this function. This might
10840 ;; be taken as an advice not to save buffers/internal variables just
10841 ;; yet, but wait until the last call to speed things up.
10842 ;;
10843 ;; `choke-request-accept-article GROUP &optional LAST' 
10844 ;; The contents of the current buffer will be put into GROUP.  There
10845 ;; should, of course, be an article in the current buffer.  This
10846 ;; function is normally only called by the function described above,
10847 ;; and LAST works the same way as in that function.
10848 ;;
10849 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
10850 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
10851 ;; This provides an easy interface for allowing editing of
10852 ;; articles. Note that even headers may be edited, so the backend has
10853 ;; to update any tables (nov buffers, etc) that it maintains after
10854 ;; replacing the article.
10855 ;;
10856 ;; `choke-request-create-group GROUP &optional SERVER'
10857 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
10858 ;; might be a group that already exists, but hasn't been registered
10859 ;; yet. 
10860 ;;
10861 ;; All these functions must return nil if they couldn't service the
10862 ;; request. If the optional arguments are not supplied, some "current"
10863 ;; or "default" values should be used. In short, one should emulate an
10864 ;; NNTP server, in a way.
10865 ;;
10866 ;; If you want to write a new backend, you just have to supply the
10867 ;; functions listed above. In addition, you must enter the new backend
10868 ;; into the list of valid select methods:
10869 ;; (setq gnus-valid-select-methods 
10870 ;;       (cons '("choke" mail) gnus-valid-select-methods))
10871 ;; The first element in this list is the name of the backend. Other
10872 ;; elemnets may be `mail' (for mail groups),  `post' (for news
10873 ;; groups), `none' (neither), `respool' (for groups that can control
10874 ;; their contents). 
10875
10876 (defun gnus-start-news-server (&optional confirm)
10877   "Open a method for getting news.
10878 If CONFIRM is non-nil, the user will be asked for an NNTP server."
10879   (let (how where)
10880     (if gnus-current-select-method
10881         ;; Stream is already opened.
10882         nil
10883       ;; Open NNTP server.
10884       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
10885       (if confirm
10886           (progn
10887             ;; Read server name with completion.
10888             (setq gnus-nntp-server
10889                   (completing-read "NNTP server: "
10890                                    (mapcar (lambda (server) (list server))
10891                                            (cons (list gnus-nntp-server)
10892                                                  gnus-secondary-servers))
10893                                    nil nil gnus-nntp-server))))
10894
10895       (if (and gnus-nntp-server 
10896                (stringp gnus-nntp-server)
10897                (not (string= gnus-nntp-server "")))
10898           (setq gnus-select-method
10899                 (cond ((or (string= gnus-nntp-server "")
10900                            (string= gnus-nntp-server "::"))
10901                        (list 'nnspool (system-name)))
10902                       ((string-match "^:" gnus-nntp-server)
10903                        (list 'nnmh gnus-nntp-server 
10904                              (list 'nnmh-directory 
10905                                    (file-name-as-directory
10906                                     (expand-file-name
10907                                      (concat "~/" (substring
10908                                                    gnus-nntp-server 1)))))
10909                              (list 'nnmh-get-new-mail nil)))
10910                       (t
10911                        (list 'nntp gnus-nntp-server)))))
10912
10913       (setq how (car gnus-select-method))
10914       (setq where (car (cdr gnus-select-method)))
10915       (cond ((eq how 'nnspool)
10916              (require 'nnspool)
10917              (gnus-message 5 "Looking up local news spool..."))
10918             ((eq how 'nnmh)
10919              (require 'nnmh)
10920              (gnus-message 5 "Looking up mh spool..."))
10921             (t
10922              (require 'nntp)))
10923       (setq gnus-current-select-method gnus-select-method)
10924       (run-hooks 'gnus-open-server-hook)
10925       (or 
10926        ;; gnus-open-server-hook might have opened it
10927        (gnus-server-opened gnus-select-method)  
10928        (gnus-open-server gnus-select-method)
10929        (gnus-y-or-n-p
10930         (format
10931          "%s server on %s can't be opened. Continue? "
10932          (car gnus-select-method) (nth 1 gnus-select-method)))
10933        (progn
10934          (gnus-message 1 "Couldn't open server on %s" 
10935                        (nth 1 gnus-select-method))
10936          (ding)
10937          nil)))))
10938
10939 (defun gnus-check-news-server (&optional method)
10940   "If the news server is down, start it up again."
10941   (let ((method (if method method gnus-select-method)))
10942     (and (stringp method)
10943          (setq method (gnus-server-to-method method)))
10944     (if (gnus-server-opened method)
10945         ;; Stream is already opened.
10946         t
10947       ;; Open server.
10948       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
10949       (run-hooks 'gnus-open-server-hook)
10950       (or (gnus-server-opened method)
10951           (gnus-open-server method))
10952       (message ""))))
10953
10954 (defun gnus-nntp-message (&optional message)
10955   "Check the status of the NNTP server.
10956 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
10957 is returned insted of the status string."
10958   (let ((status (gnus-status-message (gnus-find-method-for-group 
10959                                       gnus-newsgroup-name)))
10960         (message (or message "")))
10961     (if (and (stringp status) (> (length status) 0))
10962         status message)))
10963
10964 (defun gnus-get-function (method function)
10965   (and (stringp method)
10966        (setq method (gnus-server-to-method method)))
10967   (let ((func (intern (format "%s-%s" (car method) function))))
10968     (if (not (fboundp func)) 
10969         (progn
10970           (require (car method))
10971           (if (not (fboundp func)) 
10972               (error "No such function: %s" func))))
10973     func))
10974
10975 ;;; Interface functions to the backends.
10976
10977 (defun gnus-open-server (method)
10978   (funcall (gnus-get-function method 'open-server)
10979            (nth 1 method) (nthcdr 2 method)))
10980
10981 (defun gnus-close-server (method)
10982   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
10983
10984 (defun gnus-request-list (method)
10985   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
10986
10987 (defun gnus-request-list-newsgroups (method)
10988   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
10989
10990 (defun gnus-request-newgroups (date method)
10991   (funcall (gnus-get-function method 'request-newgroups) 
10992            date (nth 1 method)))
10993
10994 (defun gnus-server-opened (method)
10995   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
10996
10997 (defun gnus-status-message (method)
10998   (let ((method (if (stringp method) (gnus-find-method-for-group method)
10999                   method)))
11000     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11001
11002 (defun gnus-request-group (group &optional dont-check)
11003   (let ((method (gnus-find-method-for-group group)))
11004 ;    (and t (message "%s GROUP %s" (car method) group))
11005     (funcall (gnus-get-function method 'request-group) 
11006              (gnus-group-real-name group) (nth 1 method) dont-check)))
11007
11008 (defun gnus-request-asynchronous (group &optional articles)
11009   (let ((method (gnus-find-method-for-group group)))
11010     (funcall (gnus-get-function method 'request-asynchronous) 
11011              (gnus-group-real-name group) (nth 1 method) articles)))
11012
11013 (defun gnus-list-active-group (group)
11014   (let ((method (gnus-find-method-for-group group))
11015         (func 'list-active-group))
11016     (and (gnus-check-backend-function func group)
11017          (funcall (gnus-get-function method func) 
11018                   (gnus-group-real-name group) (nth 1 method)))))
11019
11020 (defun gnus-request-group-description (group)
11021   (let ((method (gnus-find-method-for-group group))
11022         (func 'request-group-description))
11023     (and (gnus-check-backend-function func group)
11024          (funcall (gnus-get-function method func) 
11025                   (gnus-group-real-name group) (nth 1 method)))))
11026
11027 (defun gnus-close-group (group)
11028   (let ((method (gnus-find-method-for-group group)))
11029     (funcall (gnus-get-function method 'close-group) 
11030              (gnus-group-real-name group) (nth 1 method))))
11031
11032 (defun gnus-retrieve-headers (articles group)
11033   (let ((method (gnus-find-method-for-group group)))
11034     (if gnus-use-cache
11035         (gnus-cache-retrieve-headers articles group)
11036       (funcall (gnus-get-function method 'retrieve-headers) 
11037                articles (gnus-group-real-name group) (nth 1 method)))))
11038
11039 (defun gnus-retrieve-groups (groups method)
11040   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11041
11042 (defun gnus-request-article (article group &optional buffer)
11043   (let ((method (gnus-find-method-for-group group)))
11044     (funcall (gnus-get-function method 'request-article) 
11045              article (gnus-group-real-name group) (nth 1 method) buffer)))
11046
11047 (defun gnus-request-head (article group)
11048   (let ((method (gnus-find-method-for-group group)))
11049     (funcall (gnus-get-function method 'request-head) 
11050              article (gnus-group-real-name group) (nth 1 method))))
11051
11052 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11053 (defun gnus-request-post-buffer (post group subject header artbuf
11054                                       info follow-to respect-poster)
11055    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11056                                             group gnus-newsrc-hashtb)))))
11057           (method
11058            (if (and gnus-post-method
11059                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11060                     (memq 'post (assoc
11061                                  (format "%s" (car (gnus-find-method-for-group
11062                                                     gnus-newsgroup-name)))
11063                                         gnus-valid-select-methods)))
11064                gnus-post-method
11065              (gnus-find-method-for-group gnus-newsgroup-name))))
11066      (or (gnus-server-opened method)
11067          (gnus-open-server method)
11068          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11069      (let ((mail-self-blind nil)
11070            (mail-archive-file-name nil))
11071        (funcall (gnus-get-function method 'request-post-buffer) 
11072                 post group subject header artbuf info follow-to
11073                 respect-poster))))
11074
11075 (defun gnus-request-post (method &optional force)
11076   (and (stringp method)
11077        (setq method (gnus-server-to-method method)))
11078   (and (not force) gnus-post-method
11079        (memq 'post (assoc (format "%s" (car method))
11080                           gnus-valid-select-methods))
11081        (setq method gnus-post-method))
11082   (funcall (gnus-get-function method 'request-post) 
11083            (nth 1 method)))
11084
11085 (defun gnus-request-expire-articles (articles group &optional force)
11086   (let ((method (gnus-find-method-for-group group)))
11087     (funcall (gnus-get-function method 'request-expire-articles) 
11088              articles (gnus-group-real-name group) (nth 1 method)
11089              force)))
11090
11091 (defun gnus-request-move-article 
11092   (article group server accept-function &optional last)
11093   (let ((method (gnus-find-method-for-group group)))
11094     (funcall (gnus-get-function method 'request-move-article) 
11095              article (gnus-group-real-name group) 
11096              (nth 1 method) accept-function last)))
11097
11098 (defun gnus-request-accept-article (group &optional last)
11099   (let ((func (if (symbolp group) group
11100                 (car (gnus-find-method-for-group group)))))
11101     (funcall (intern (format "%s-request-accept-article" func))
11102              (if (stringp group) (gnus-group-real-name group) group)
11103              last)))
11104
11105 (defun gnus-request-replace-article (article group buffer)
11106   (let ((func (car (gnus-find-method-for-group group))))
11107     (funcall (intern (format "%s-request-replace-article" func))
11108              article (gnus-group-real-name group) buffer)))
11109
11110 (defun gnus-request-create-group (group)
11111   (let ((method (gnus-find-method-for-group group)))
11112     (funcall (gnus-get-function method 'request-create-group) 
11113              (gnus-group-real-name group) (nth 1 method))))
11114
11115 (defun gnus-member-of-valid (symbol group)
11116   (memq symbol (assoc
11117                 (format "%s" (car (gnus-find-method-for-group group)))
11118                 gnus-valid-select-methods)))
11119
11120 (defun gnus-find-method-for-group (group &optional info)
11121   (or gnus-override-method
11122       (and (not group)
11123            gnus-select-method)
11124       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11125             method)
11126         (if (or (not info)
11127                 (not (setq method (nth 4 info))))
11128             (setq method gnus-select-method)
11129           (setq method
11130                 (cond ((stringp method)
11131                        (gnus-server-to-method method))
11132                       ((stringp (car method))
11133                        (gnus-server-extend-method group method))
11134                       (t
11135                        method))))
11136         (gnus-server-add-address method))))
11137
11138 (defun gnus-check-backend-function (func group)
11139   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11140                  group)))
11141     (fboundp (intern (format "%s-%s" method func)))))
11142
11143 (defun gnus-methods-using (method)
11144   (let ((valids gnus-valid-select-methods)
11145         outs)
11146     (while valids
11147       (if (memq method (car valids)) 
11148           (setq outs (cons (car valids) outs)))
11149       (setq valids (cdr valids)))
11150     outs))
11151
11152 ;;; 
11153 ;;; Active & Newsrc File Handling
11154 ;;;
11155
11156 ;; Newsrc related functions.
11157 ;; Gnus internal format of gnus-newsrc-alist:
11158 ;; (("alt.general" 3 (1 . 1))
11159 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11160 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11161 ;; The first item is the group name; the second is the subscription
11162 ;; level; the third is either a range of a list of ranges of read
11163 ;; articles, the optional fourth element is a list of marked articles,
11164 ;; the optional fifth element is the select method.
11165 ;;
11166 ;; Gnus internal format of gnus-newsrc-hashtb:
11167 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11168 ;; This is the entry for "alt.misc". The first element is the number
11169 ;; of unread articles in "alt.misc". The cdr of this entry is the
11170 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11171 ;; trivial to remove or add new elements into gnus-newsrc-alist
11172 ;; without scanning the entire list. So, to get the actual information
11173 ;; of "alt.misc", you'd say something like 
11174 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11175 ;;
11176 ;; Gnus internal format of gnus-active-hashtb:
11177 ;; ((1 . 1))
11178 ;;  (5 . 10))
11179 ;;  (67 . 99)) ...)
11180 ;; The only element in each entry in this hash table is a range of
11181 ;; (possibly) available articles. (Articles in this range may have
11182 ;; been expired or cancelled.)
11183 ;;
11184 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11185 ;; ("alt.misc" "alt.test" "alt.general" ...)
11186
11187 (defun gnus-setup-news (&optional rawfile level)
11188   "Setup news information.
11189 If RAWFILE is non-nil, the .newsrc file will also be read.
11190 If LEVEL is non-nil, the news will be set up at level LEVEL."
11191   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11192     ;; Clear some variables to re-initialize news information.
11193     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11194
11195     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11196     (if init (gnus-read-newsrc-file rawfile))
11197
11198     ;; Read the active file and create `gnus-active-hashtb'.
11199     ;; If `gnus-read-active-file' is nil, then we just create an empty
11200     ;; hash table. The partial filling out of the hash table will be
11201     ;; done in `gnus-get-unread-articles'.
11202     (if (and gnus-read-active-file 
11203              (not level)
11204              (gnus-server-opened gnus-select-method))
11205         (gnus-read-active-file)
11206       (setq gnus-active-hashtb (make-vector 4095 0)))
11207
11208     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11209
11210     ;; Find the number of unread articles in each non-dead group.
11211     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11212     ;; Find new newsgroups and treat them.
11213     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11214              (gnus-server-opened gnus-select-method))
11215         (gnus-find-new-newsgroups))
11216     (if (and init gnus-check-bogus-newsgroups 
11217              gnus-read-active-file (not level)
11218              (gnus-server-opened gnus-select-method))
11219         (gnus-check-bogus-newsgroups))))
11220
11221 (defun gnus-find-new-newsgroups ()
11222   "Search for new newsgroups and add them.
11223 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11224 The `-n' option line from .newsrc is respected."
11225   (interactive)
11226   (or (gnus-check-first-time-used)
11227       (if (or (consp gnus-check-new-newsgroups)
11228               (eq gnus-check-new-newsgroups 'ask-server))
11229           (gnus-ask-server-for-new-groups)
11230         (let ((groups 0)
11231               group new-newsgroups)
11232           (or gnus-have-read-active-file (gnus-read-active-file))
11233           (setq gnus-newsrc-last-checked-date (current-time-string))
11234           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11235           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11236           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11237           (mapatoms
11238            (lambda (sym)
11239              (setq group (symbol-name sym))
11240              (if (or (gnus-gethash group gnus-killed-hashtb)
11241                      (gnus-gethash group gnus-newsrc-hashtb))
11242                  ()
11243                (let ((do-sub (gnus-matches-options-n group)))
11244                  (cond ((eq do-sub 'subscribe)
11245                         (setq groups (1+ groups))
11246                         (gnus-sethash group group gnus-killed-hashtb)
11247                         (funcall 
11248                          gnus-subscribe-options-newsgroup-method group))
11249                        ((eq do-sub 'ignore)
11250                         nil)
11251                        (t
11252                         (setq groups (1+ groups))
11253                         (gnus-sethash group group gnus-killed-hashtb)
11254                         (if gnus-subscribe-hierarchical-interactive
11255                             (setq new-newsgroups (cons group new-newsgroups))
11256                           (funcall gnus-subscribe-newsgroup-method group)))))))
11257            gnus-active-hashtb)
11258           (if new-newsgroups 
11259               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11260           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11261           (if (> groups 0)
11262               (gnus-message 6 "%d new newsgroup%s arrived." 
11263                             groups (if (> groups 1) "s have" " has")))))))
11264
11265 (defun gnus-matches-options-n (group)
11266   ;; Returns `subscribe' if the group is to be uncoditionally
11267   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11268   ;; no match for the group.
11269
11270   ;; First we check the two user variables.
11271   (cond
11272    ((and gnus-options-subscribe
11273          (string-match gnus-options-not-subscribe group))
11274     'subscribe)
11275    ((and gnus-options-not-subscribe
11276          (string-match gnus-options-subscribe group))
11277     'ignore)
11278    ;; Then we go through the list that was retrieved from the .newsrc
11279    ;; file.  This list has elements on the form 
11280    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11281    ;; is in the reverse order of the options line) is returned.
11282    (t
11283     (let ((regs gnus-newsrc-options-n))
11284       (while (and regs
11285                   (not (string-match (car (car regs)) group)))
11286         (setq regs (cdr regs)))
11287       (and regs (cdr (car regs)))))))
11288
11289 (defun gnus-ask-server-for-new-groups ()
11290   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11291          (methods (cons gnus-select-method 
11292                         (append
11293                          (and (consp gnus-check-new-newsgroups)
11294                               gnus-check-new-newsgroups)
11295                          gnus-secondary-select-methods)))
11296          (groups 0)
11297          (new-date (current-time-string))
11298          hashtb group new-newsgroups got-new)
11299     ;; Go thorugh both primary and secondary select methods and
11300     ;; request new newsgroups.  
11301     (while methods
11302       (if (gnus-request-newgroups date (car methods))
11303           (save-excursion
11304             (setq got-new t)
11305             (or hashtb (setq hashtb (gnus-make-hashtable 
11306                                      (count-lines (point-min) (point-max)))))
11307             (set-buffer nntp-server-buffer)
11308             ;; Enter all the new groups in a hashtable.
11309             (gnus-active-to-gnus-format (car methods) hashtb)))
11310       (setq methods (cdr methods)))
11311     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11312     ;; Now all new groups from all select methods are in `hashtb'.
11313     (mapatoms
11314      (lambda (group-sym)
11315        (setq group (symbol-name group-sym))
11316        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11317                (member group gnus-zombie-list)
11318                (member group gnus-killed-list))
11319            ;; The group is already known.
11320            ()
11321          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
11322          (let ((do-sub (gnus-matches-options-n group)))
11323            (cond ((eq do-sub 'subscribe)
11324                   (setq groups (1+ groups))
11325                   (gnus-sethash group group gnus-killed-hashtb)
11326                   (funcall 
11327                    gnus-subscribe-options-newsgroup-method group))
11328                  ((eq do-sub 'ignore)
11329                   nil)
11330                  (t
11331                   (setq groups (1+ groups))
11332                   (gnus-sethash group group gnus-killed-hashtb)
11333                   (if gnus-subscribe-hierarchical-interactive
11334                       (setq new-newsgroups (cons group new-newsgroups))
11335                     (funcall gnus-subscribe-newsgroup-method group)))))))
11336      hashtb)
11337     (if new-newsgroups 
11338         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11339     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11340     (if (> groups 0)
11341         (gnus-message 6 "%d new newsgroup%s arrived." 
11342                       groups (if (> groups 1) "s have" " has")))
11343     got-new))
11344
11345 (defun gnus-check-first-time-used ()
11346   (if (or (> (length gnus-newsrc-alist) 1)
11347           (file-exists-p gnus-startup-file)
11348           (file-exists-p (concat gnus-startup-file ".el"))
11349           (file-exists-p (concat gnus-startup-file ".eld")))
11350       nil
11351     (gnus-message 6 "First time user; subscribing you to default groups")
11352     (or gnus-have-read-active-file (gnus-read-active-file))
11353     (setq gnus-newsrc-last-checked-date (current-time-string))
11354     (let ((groups gnus-default-subscribed-newsgroups)
11355           group)
11356       (if (eq groups t)
11357           nil
11358         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11359         (mapatoms
11360          (lambda (sym)
11361            (setq group (symbol-name sym))
11362            (let ((do-sub (gnus-matches-options-n group)))
11363              (cond ((eq do-sub 'subscribe)
11364                     (gnus-sethash group group gnus-killed-hashtb)
11365                     (funcall 
11366                      gnus-subscribe-options-newsgroup-method group))
11367                    ((eq do-sub 'ignore)
11368                     nil)
11369                    (t
11370                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11371          gnus-active-hashtb)
11372         (while groups
11373           (if (gnus-gethash (car groups) gnus-active-hashtb)
11374               (gnus-group-change-level 
11375                (car groups) gnus-level-default-subscribed gnus-level-killed))
11376           (setq groups (cdr groups)))
11377         (gnus-group-make-help-group)
11378         (and gnus-novice-user
11379              (gnus-message 7 "`A k' to list killed groups"))))))
11380
11381 (defun gnus-subscribe-group (group previous &optional method)
11382   (gnus-group-change-level 
11383    (if method
11384        (list t group gnus-level-default-subscribed nil nil method)
11385      group) 
11386    gnus-level-default-subscribed gnus-level-killed previous t))
11387
11388 ;; `gnus-group-change-level' is the fundamental function for changing
11389 ;; subscription levels of newsgroups. This might mean just changing
11390 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11391 ;; again, which subscribes/unsubscribes a group, which is equally
11392 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11393 ;; from 8-9 to 1-7 means that you remove the group from the list of
11394 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11395 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11396 ;; which is trivial.
11397 ;; ENTRY can either be a string (newsgroup name) or a list (if
11398 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11399 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11400 ;; entries. 
11401 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11402 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11403 ;; after. 
11404 (defun gnus-group-change-level (entry level &optional oldlevel
11405                                       previous fromkilled)
11406   (let ((pinfo entry)
11407         group info active num)
11408     ;; Glean what info we can from the arguments
11409     (if (consp entry)
11410         (if fromkilled (setq group (nth 1 entry))
11411           (setq group (car (nth 2 entry))))
11412       (setq group entry))
11413     (if (and (stringp entry)
11414              oldlevel 
11415              (< oldlevel gnus-level-zombie))
11416         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11417     (if (and (not oldlevel)
11418              (listp entry))
11419         (setq oldlevel (car (cdr (nth 2 entry)))))
11420     (if (stringp previous)
11421         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11422
11423     (gnus-dribble-enter
11424      (format "(gnus-group-change-level %S %S %S %S %S)" 
11425              group level oldlevel (car (nth 2 previous)) fromkilled))
11426     
11427     ;; Then we remove the newgroup from any old structures, if needed.
11428     ;; If the group was killed, we remove it from the killed or zombie
11429     ;; list. If not, and it is in fact going to be killed, we remove
11430     ;; it from the newsrc hash table and assoc.
11431     (cond ((>= oldlevel gnus-level-zombie)
11432            (if (= oldlevel gnus-level-zombie)
11433                (setq gnus-zombie-list (delete group gnus-zombie-list))
11434              (setq gnus-killed-list (delete group gnus-killed-list))))
11435           (t
11436            (if (>= level gnus-level-zombie)
11437                (progn
11438                  (gnus-sethash (car (nth 2 entry))
11439                                nil gnus-newsrc-hashtb)
11440                  (if (nth 3 entry)
11441                      (setcdr (gnus-gethash (car (nth 3 entry))
11442                                            gnus-newsrc-hashtb)
11443                              (cdr entry)))
11444                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11445
11446     ;; Finally we enter (if needed) the list where it is supposed to
11447     ;; go, and change the subscription level. If it is to be killed,
11448     ;; we enter it into the killed or zombie list.
11449     (cond ((>= level gnus-level-zombie)
11450            ;; Remove from the hash table.
11451            (gnus-sethash group nil gnus-newsrc-hashtb)
11452            (or (gnus-group-foreign-p group)
11453                ;; We do not enter foreign groups into the list of dead
11454                ;; groups.  
11455                (if (= level gnus-level-zombie)
11456                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11457                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11458           (t
11459            ;; If the list is to be entered into the newsrc assoc, and
11460            ;; it was killed, we have to create an entry in the newsrc
11461            ;; hashtb format and fix the pointers in the newsrc assoc.
11462            (if (>= oldlevel gnus-level-zombie)
11463                (progn
11464                  (if (listp entry)
11465                      (progn
11466                        (setq info (cdr entry))
11467                        (setq num (car entry)))
11468                    (setq active (gnus-gethash group gnus-active-hashtb))
11469                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11470                    ;; Check whether the group is foreign. If so, the
11471                    ;; foreign select method has to be entered into the
11472                    ;; info. 
11473                    (let ((method (gnus-group-method-name group)))
11474                      (if (eq method gnus-select-method)
11475                          (setq info (list group level nil))
11476                        (setq info (list group level nil nil method)))))
11477                  (or previous 
11478                      (setq previous 
11479                            (let ((p gnus-newsrc-alist))
11480                              (while (cdr (cdr p))
11481                                (setq p (cdr p)))
11482                              p)))
11483                  (setq entry (cons info (cdr (cdr previous))))
11484                  (if (cdr previous)
11485                      (progn
11486                        (setcdr (cdr previous) entry)
11487                        (gnus-sethash group (cons num (cdr previous)) 
11488                                      gnus-newsrc-hashtb))
11489                    (setcdr previous entry)
11490                    (gnus-sethash group (cons num previous)
11491                                  gnus-newsrc-hashtb))
11492                  (if (cdr entry)
11493                      (setcdr (gnus-gethash (car (car (cdr entry)))
11494                                            gnus-newsrc-hashtb)
11495                              entry)))
11496              ;; It was alive, and it is going to stay alive, so we
11497              ;; just change the level and don't change any pointers or
11498              ;; hash table entries.
11499              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11500
11501 (defun gnus-kill-newsgroup (newsgroup)
11502   "Obsolete function. Kills a newsgroup."
11503   (gnus-group-change-level
11504    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11505
11506 (defun gnus-check-bogus-newsgroups (&optional confirm)
11507   "Remove bogus newsgroups.
11508 If CONFIRM is non-nil, the user has to confirm the deletion of every
11509 newsgroup." 
11510   (let ((newsrc (cdr gnus-newsrc-alist))
11511         bogus group)
11512     (gnus-message 5 "Checking bogus newsgroups...")
11513     (or gnus-have-read-active-file (gnus-read-active-file))
11514     ;; Find all bogus newsgroup that are subscribed.
11515     (while newsrc
11516       (setq group (car (car newsrc)))
11517       (if (or (gnus-gethash group gnus-active-hashtb)
11518               (nth 4 (car newsrc))
11519               (and confirm
11520                    (not (gnus-y-or-n-p
11521                          (format "Remove bogus newsgroup: %s " group)))))
11522           ;; Active newsgroup.
11523           ()
11524         ;; Found a bogus newsgroup.
11525         (setq bogus (cons group bogus)))
11526       (setq newsrc (cdr newsrc)))
11527     ;; Remove all bogus subscribed groups by first killing them, and
11528     ;; then removing them from the list of killed groups.
11529     (while bogus
11530       (gnus-group-change-level 
11531        (gnus-gethash (car bogus) gnus-newsrc-hashtb) gnus-level-killed)
11532       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
11533       (setq bogus (cdr bogus)))
11534     ;; Then we remove all bogus groups from the list of killed and
11535     ;; zombie groups. They are are removed without confirmation.
11536     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11537           killed)
11538       (while dead-lists
11539         (setq killed (symbol-value (car dead-lists)))
11540         (while killed
11541           (setq group (car killed))
11542           (or (gnus-gethash group gnus-active-hashtb)
11543               ;; The group is bogus.
11544               (set (car dead-lists)
11545                    (delete group (symbol-value (car dead-lists)))))
11546           (setq killed (cdr killed)))
11547         (setq dead-lists (cdr dead-lists))))
11548     (gnus-message 5 "Checking bogus newsgroups...done")))
11549
11550 (defun gnus-check-duplicate-killed-groups ()
11551   "Remove duplicates from the list of killed groups."
11552   (interactive)
11553   (let ((killed gnus-killed-list))
11554     (while killed
11555       (gnus-message 9 "%d" (length killed))
11556       (setcdr killed (delete (car killed) (cdr killed)))
11557       (setq killed (cdr killed)))))
11558
11559 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11560 ;; and compute how many unread articles there are in each group.
11561 (defun gnus-get-unread-articles (&optional level)
11562   (let* ((newsrc (cdr gnus-newsrc-alist))
11563          (conditional level)
11564          (level (or level (1+ gnus-level-subscribed)))
11565          info group active virtuals method)
11566     (gnus-message 5 "Checking new news...")
11567     (while newsrc
11568       (setq info (car newsrc))
11569       (setq group (car info))
11570       (setq active (gnus-gethash group gnus-active-hashtb))
11571
11572       ;; Check newsgroups. If the user doesn't want to check them, or
11573       ;; they can't be checked (for instance, if the news server can't
11574       ;; be reached) we just set the number of unread articles in this
11575       ;; newsgroup to t. This means that Gnus thinks that there are
11576       ;; unread articles, but it has no idea how many.
11577       (if (and (setq method (nth 4 info))
11578                (not (gnus-server-equal gnus-select-method
11579                                        (gnus-server-get-method nil method))))
11580           (if (or (and gnus-activate-foreign-newsgroups 
11581                        (not (numberp gnus-activate-foreign-newsgroups)))
11582                   (and (numberp gnus-activate-foreign-newsgroups)
11583                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
11584                        (<= (nth 1 info) level)))
11585               (if (eq (car (if (stringp method) 
11586                                (gnus-server-to-method method)
11587                              (nth 4 info))) 'nnvirtual)
11588                   (setq virtuals (cons info virtuals))
11589                 (setq active (gnus-activate-newsgroup (car info)))))
11590         (if (and (not gnus-read-active-file)
11591                  (<= (nth 1 info) level))
11592             (progn
11593               (setq active (gnus-activate-newsgroup (car info))))))
11594       
11595       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
11596                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
11597       (and active 
11598            (gnus-get-unread-articles-in-group info active)
11599            ;; Close the groups as we look at them!
11600            (gnus-close-group group))
11601       (setq newsrc (cdr newsrc)))
11602
11603     ;; Activate the virtual groups. This has to be done after all the
11604     ;; other groups. 
11605     ;; !!! If one virtual group contains another virtual group, even
11606     ;; doing it this way might cause problems.
11607    (while virtuals
11608       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11609            (gnus-get-unread-articles-in-group (car virtuals) active))
11610       (setq virtuals (cdr virtuals)))
11611
11612     (gnus-message 5 "Checking new news...done")))
11613
11614 ;; Create a hash table out of the newsrc alist. The `car's of the
11615 ;; alist elements are used as keys.
11616 (defun gnus-make-hashtable-from-newsrc-alist ()
11617   (let ((alist gnus-newsrc-alist)
11618         (ohashtb gnus-newsrc-hashtb)
11619         prev)
11620     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11621     (setq alist 
11622           (setq prev (setq gnus-newsrc-alist 
11623                            (if (equal (car (car gnus-newsrc-alist))
11624                                       "dummy.group")
11625                                gnus-newsrc-alist
11626                              (cons (list "dummy.group" 0 nil) alist)))))
11627     (while alist
11628       (gnus-sethash (car (car alist)) 
11629                     (cons (and ohashtb (car (gnus-gethash 
11630                                              (car (car alist)) ohashtb))) 
11631                           prev) gnus-newsrc-hashtb)
11632       (setq prev alist
11633             alist (cdr alist)))))
11634
11635 (defun gnus-make-hashtable-from-killed ()
11636   "Create a hash table from the killed and zombie lists."
11637   (let ((lists '(gnus-killed-list gnus-zombie-list))
11638         list)
11639     (setq gnus-killed-hashtb 
11640           (gnus-make-hashtable 
11641            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11642     (while lists
11643       (setq list (symbol-value (car lists)))
11644       (setq lists (cdr lists))
11645       (while list
11646         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11647         (setq list (cdr list))))))
11648
11649 (defun gnus-get-unread-articles-in-group (info active)
11650   (let* ((range (nth 2 info))
11651          (num 0)
11652          (marked (nth 3 info))
11653          srange lowest group highest)
11654     ;; If a cache is present, we may have to alter the active info.
11655     (and gnus-use-cache
11656          (gnus-cache-possibly-alter-active (car info) active))
11657     ;; Modify the list of read articles according to what articles 
11658     ;; are available; then tally the unread articles and add the
11659     ;; number to the group hash table entry.
11660     (cond ((zerop (cdr active))
11661            (setq num 0))
11662           ((not range)
11663            (setq num (- (1+ (cdr active)) (car active))))
11664           ((not (listp (cdr range)))
11665            ;; Fix a single (num . num) range according to the
11666            ;; active hash table.
11667            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11668            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11669            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11670            ;; Compute number of unread articles.
11671            (setq num (max 0 (- (cdr active) 
11672                                (- (1+ (cdr range)) (car range))))))
11673           (t
11674            ;; The read list is a list of ranges. Fix them according to
11675            ;; the active hash table.
11676            ;; First peel off any elements that are below the lower
11677            ;; active limit. 
11678            (while (and (cdr range) 
11679                        (>= (car active) 
11680                            (or (and (atom (car (cdr range))) (car (cdr range)))
11681                                (car (car (cdr range))))))
11682              (if (numberp (car range))
11683                  (setcar range 
11684                          (cons (car range) 
11685                                (or (and (numberp (car (cdr range)))
11686                                         (car (cdr range))) 
11687                                    (cdr (car (cdr range))))))
11688                (setcdr (car range) 
11689                        (or (and (numberp (nth 1 range)) (nth 1 range))
11690                            (cdr (car (cdr range))))))
11691              (setcdr range (cdr (cdr range))))
11692            ;; Adjust the first element to be the same as the lower limit. 
11693            (if (and (not (atom (car range))) 
11694                     (< (cdr (car range)) (car active)))
11695                (setcdr (car range) (1- (car active))))
11696            ;; Then we want to peel off any elements that are higher
11697            ;; than the upper active limit.  
11698            (let ((srange range))
11699              ;; Go past all legal elements.
11700              (while (and (cdr srange) 
11701                          (<= (or (and (atom (car (cdr srange)))
11702                                       (car (cdr srange)))
11703                                  (car (car (cdr srange)))) (cdr active)))
11704                (setq srange (cdr srange)))
11705              (if (cdr srange)
11706                  ;; Nuke all remaining illegal elements.
11707                  (setcdr srange nil))
11708
11709              ;; Adjust the final element.
11710              (if (and (not (atom (car srange)))
11711                       (> (cdr (car srange)) (cdr active)))
11712                  (setcdr (car srange) (cdr active))))
11713            ;; Compute the number of unread articles.
11714            (while range
11715              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11716                                          (cdr (car range))))
11717                                  (or (and (atom (car range)) (car range))
11718                                      (car (car range))))))
11719              (setq range (cdr range)))
11720            (setq num (max 0 (- (cdr active) num)))))
11721     (and info
11722          (progn
11723            (and (assq 'tick marked)
11724                 (inline (gnus-remove-illegal-marked-articles
11725                          (assq 'tick marked) (nth 2 info))))
11726            (and (assq 'dormant marked)
11727                 (inline (gnus-remove-illegal-marked-articles
11728                          (assq 'dormant marked) (nth 2 info))))
11729            (setcar
11730             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11731             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11732                                 (length (cdr (assq 'dormant marked)))))))))
11733     num))
11734
11735 (defun gnus-remove-illegal-marked-articles (marked ranges)
11736   (let ((m (cdr marked)))
11737     ;; Make sure that all ticked articles are a subset of the unread
11738     ;; articles. 
11739     (while m
11740       (if (gnus-member-of-range (car m) ranges)
11741           (setcdr marked (cdr m))
11742         (setq marked m))
11743       (setq m (cdr m)))))
11744
11745 (defun gnus-activate-newsgroup (group)
11746   (let ((method (gnus-find-method-for-group group))
11747         active)
11748     (and (or (gnus-server-opened method) (gnus-open-server method))
11749          (gnus-request-group group)
11750          (save-excursion
11751            (set-buffer nntp-server-buffer)
11752            (goto-char (point-min))
11753            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11754                 (progn
11755                   (goto-char (match-beginning 1))
11756                   (gnus-sethash 
11757                    group (setq active (cons (read (current-buffer))
11758                                             (read (current-buffer))))
11759                    gnus-active-hashtb))
11760                 active)))))
11761
11762 (defun gnus-update-read-articles 
11763   (group unread unselected ticked &optional domarks replied expirable killed
11764          dormant bookmark score)
11765   "Update the list of read and ticked articles in GROUP using the
11766 UNREAD and TICKED lists.
11767 Note: UNSELECTED has to be sorted over `<'.
11768 Returns whether the updating was successful."
11769   (let* ((active (or gnus-newsgroup-active 
11770                      (gnus-gethash group gnus-active-hashtb)))
11771          (entry (gnus-gethash group gnus-newsrc-hashtb))
11772          (number (car entry))
11773          (info (nth 2 entry))
11774          (marked (nth 3 info))
11775          (prev 1)
11776          (unread (sort (copy-sequence unread) (function <)))
11777          last read)
11778     (if (or (not info) (not active))
11779         ;; There is no info on this group if it was, in fact,
11780         ;; killed. Gnus stores no information on killed groups, so
11781         ;; there's nothing to be done. 
11782         ;; One could store the information somewhere temporarily,
11783         ;; perhaps... Hmmm... 
11784         ()
11785       ;; Remove any negative articles numbers.
11786       (while (and unread (< (car unread) 0))
11787         (setq unread (cdr unread)))
11788       ;; Remove any expired article numbers
11789       (while (and unread (< (car unread) (car active)))
11790         (setq unread (cdr unread)))
11791       (while (and ticked (< (car ticked) (car active)))
11792         (setq ticked (cdr ticked)))
11793       (while (and dormant (< (car dormant) (car active)))
11794         (setq dormant (cdr dormant)))
11795       (setq unread (sort (append unselected unread) '<))
11796       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11797       (setcar entry (max 0 (- (length unread) (length ticked) 
11798                               (length dormant))))
11799       ;; Compute the ranges of read articles by looking at the list of
11800       ;; unread articles.  
11801       (while unread
11802         (if (/= (car unread) prev)
11803             (setq read (cons (if (= prev (1- (car unread))) prev
11804                                (cons prev (1- (car unread)))) read)))
11805         (setq prev (1+ (car unread)))
11806         (setq unread (cdr unread)))
11807       (if (<= prev (cdr active))
11808           (setq read (cons (cons prev (cdr active)) read)))
11809       ;; Enter this list into the group info.
11810       (setcar (cdr (cdr info)) 
11811               (if (> (length read) 1) (nreverse read) read))
11812       ;; Enter the list of ticked articles.
11813       (gnus-set-marked-articles 
11814        info ticked
11815        (if domarks replied (cdr (assq 'reply marked)))
11816        (if domarks expirable (cdr (assq 'expire marked)))
11817        (if domarks killed (cdr (assq 'killed marked)))
11818        (if domarks dormant (cdr (assq 'dormant marked)))
11819        (if domarks bookmark (cdr (assq 'bookmark marked)))
11820        (if domarks score (cdr (assq 'score marked))))
11821       t)))
11822
11823 (defun gnus-make-articles-unread (group articles)
11824   "Mark ARTICLES in GROUP as unread."
11825   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
11826                           (gnus-gethash (gnus-group-real-name group)
11827                                         gnus-newsrc-hashtb))))
11828          (ranges (nth 2 info))
11829          news)
11830     (while articles
11831       (and (gnus-member-of-range (car articles) ranges)
11832            (setq news (cons (car articles) news)))
11833       (setq articles (cdr articles)))
11834     (if (not news)
11835         ()
11836       (setcar (nthcdr 2 info)
11837               (gnus-remove-from-range (nth 2 info) (nreverse news)))
11838       (gnus-group-update-group group t))))
11839
11840 (defun gnus-read-active-file ()
11841   "Get active file from NNTP server."
11842   (gnus-group-set-mode-line)
11843   (let ((methods (cons gnus-select-method gnus-secondary-select-methods))
11844         (not-first nil)
11845         list-type)
11846     (setq gnus-have-read-active-file nil)
11847     (save-excursion
11848       (set-buffer nntp-server-buffer)
11849       (while methods
11850         (let* ((where (nth 1 (car methods)))
11851                (mesg (format "Reading active file%s via %s..."
11852                              (if (and where (not (zerop (length where))))
11853                                  (concat " from " where) "")
11854                              (car (car methods)))))
11855           (gnus-message 5 mesg)
11856           (gnus-check-news-server (car methods))
11857           (cond 
11858            ((and (eq gnus-read-active-file 'some)
11859                  (gnus-check-backend-function
11860                   'retrieve-groups (car (car methods))))
11861             (let ((newsrc (cdr gnus-newsrc-alist))
11862                   groups)
11863               (while newsrc
11864                 (and (gnus-server-equal 
11865                       (gnus-find-method-for-group
11866                        (car (car newsrc)) (car newsrc))
11867                       (gnus-server-get-method nil (car methods)))
11868                      (setq groups (cons (car (car newsrc)) groups)))
11869                 (setq newsrc (cdr newsrc)))
11870               (setq list-type (gnus-retrieve-groups groups (car methods)))
11871               (cond ((not list-type)
11872                      (gnus-message 
11873                       1 "Cannot read partial active file from %s server." 
11874                       (car (car methods)))
11875                      (ding)
11876                      (sit-for 2))
11877                     ((eq list-type 'active)
11878                      (gnus-active-to-gnus-format (and not-first (car methods)))
11879                      (setq not-first t))
11880                     (t
11881                      (gnus-groups-to-gnus-format (and not-first (car methods)))
11882                      (setq not-first t)))))
11883            (t
11884             (if (not (gnus-request-list (car methods)))
11885                 (progn
11886                   (gnus-message 1 "Cannot read active file from %s server." 
11887                                 (car (car methods)))
11888                   (ding))
11889               (gnus-active-to-gnus-format 
11890                (and gnus-have-read-active-file (car methods)))
11891               (setq gnus-have-read-active-file t)
11892               (gnus-message 5 "%s...done" mesg)))))
11893         (setq methods (cdr methods))))))
11894
11895 ;; Read an active file and place the results in `gnus-active-hashtb'.
11896 (defun gnus-active-to-gnus-format (method &optional hashtb)
11897   (let ((cur (current-buffer))
11898         (hashtb (or hashtb 
11899                     (if method
11900                         gnus-active-hashtb
11901                       (setq gnus-active-hashtb
11902                             (gnus-make-hashtable 
11903                              (count-lines (point-min) (point-max))))))))
11904     ;; Delete unnecessary lines.
11905     (goto-char (point-min))
11906     (while (search-forward "\nto." nil t)
11907       (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
11908     (or (string= gnus-ignored-newsgroups "")
11909         (progn
11910           (goto-char (point-min))
11911           (delete-matching-lines gnus-ignored-newsgroups)))
11912     (and method (not (eq method gnus-select-method))
11913          (let ((prefix (gnus-group-prefixed-name "" method)))
11914            (goto-char (point-min))
11915            (while (and (not (eobp))
11916                        (null (insert prefix))
11917                        (zerop (forward-line 1))))))
11918     (goto-char (point-min))
11919     ;; Store active file in hashtable.
11920     (goto-char (point-min))
11921     (if (string-match "%[oO]" gnus-group-line-format)
11922         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
11923         ;; If we want information on moderated groups, we use this
11924         ;; loop...   
11925         (let* ((mod-hashtb (make-vector 7 0))
11926                (m (intern "m" mod-hashtb))
11927                group max mod min)
11928           (while (not (eobp))
11929             (condition-case nil
11930                 (progn
11931                   (narrow-to-region (point) (gnus-point-at-eol))
11932                   (setq group (let ((obarray hashtb)) (read cur)))
11933                   (and (numberp (setq max (read cur)))
11934                        (numberp (setq min (read cur)))
11935                        (set group (cons min max)))
11936                   ;; Enter moderated groups into a list.
11937                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
11938                       (setq gnus-moderated-list 
11939                             (cons (symbol-name group) gnus-moderated-list))))
11940               (error nil))
11941             (widen)
11942             (forward-line 1)))
11943       ;; And if we do not care about moderation, we use this loop,
11944       ;; which is faster.
11945       (let (group max min)
11946         (while (not (eobp))
11947           (condition-case ()
11948               (progn
11949                 (narrow-to-region (point) (gnus-point-at-eol))
11950                 ;; group gets set to a symbol interned in the hash table
11951                 ;; (what a hack!!)
11952                 (setq group (let ((obarray hashtb)) (read cur)))
11953                 (and (numberp (setq max (read cur)))
11954                      (numberp (setq min (read cur)))
11955                      (set group (cons min max))))
11956             (error 
11957              (progn (ding) 
11958                     (gnus-message 3 "Warning - illegal active: %s"
11959                                   (buffer-substring 
11960                                    (gnus-point-at-bol) (gnus-point-at-eol)))
11961                     nil)))
11962           (widen)
11963           (forward-line 1))))))
11964
11965 (defun gnus-groups-to-gnus-format (method &optional hashtb)
11966   ;; Parse a "groups" active file.
11967   (let ((cur (current-buffer))
11968         (hashtb (or hashtb 
11969                     (if method
11970                         gnus-active-hashtb
11971                       (setq gnus-active-hashtb
11972                             (gnus-make-hashtable 
11973                              (count-lines (point-min) (point-max)))))))
11974         (prefix (and method (not (eq method gnus-select-method))
11975                      (gnus-group-prefixed-name "" method))))
11976
11977     (goto-char (point-min))
11978     (condition-case ()
11979         ;; We split this into to separate loops, one with the prefix
11980         ;; and one without to speed the reading up somewhat.
11981         (if prefix
11982             (let (min max opoint)
11983               (while (not (eobp))
11984                 (read cur) (read cur)
11985                 (setq min (read cur)
11986                       max (read cur)
11987                       opoint (point))
11988                 (skip-chars-forward " \t")
11989                 (insert prefix)
11990                 (goto-char opoint)
11991                 (set (let ((obarray hashtb)) (read cur)) 
11992                      (cons min max))
11993                 (forward-line 1)))
11994           (let (min max opoint)
11995             (while (not (eobp))
11996               (if (= (following-char) ?2)
11997                   (progn
11998                     (read cur) (read cur)
11999                     (setq min (read cur)
12000                           max (read cur))
12001                     (set (let ((obarray hashtb)) (read cur)) 
12002                          (cons min max))))
12003               (forward-line 1))))
12004       (error 
12005        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12006
12007 (defun gnus-read-newsrc-file (&optional force)
12008   "Read startup file.
12009 If FORCE is non-nil, the .newsrc file is read."
12010   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12011   ;; Reset variables that might be defined in the .newsrc.eld file.
12012   (let ((variables gnus-variable-list))
12013     (while variables
12014       (set (car variables) nil)
12015       (setq variables (cdr variables))))
12016   (let* ((newsrc-file gnus-current-startup-file)
12017          (quick-file (concat newsrc-file ".el")))
12018     (save-excursion
12019       ;; We always load the .newsrc.eld file. If always contains
12020       ;; much information that can not be gotten from the .newsrc
12021       ;; file (ticked articles, killed groups, foreign methods, etc.)
12022       (gnus-read-newsrc-el-file quick-file)
12023  
12024       (if (or force
12025               (and (file-newer-than-file-p newsrc-file quick-file)
12026                    (file-newer-than-file-p newsrc-file 
12027                                            (concat quick-file "d")))
12028               (not gnus-newsrc-alist))
12029           ;; We read the .newsrc file. Note that if there if a
12030           ;; .newsrc.eld file exists, it has already been read, and
12031           ;; the `gnus-newsrc-hashtb' has been created. While reading
12032           ;; the .newsrc file, Gnus will only use the information it
12033           ;; can find there for changing the data already read -
12034           ;; ie. reading the .newsrc file will not trash the data
12035           ;; already read (except for read articles).
12036           (save-excursion
12037             (gnus-message 5 "Reading %s..." newsrc-file)
12038             (set-buffer (find-file-noselect newsrc-file))
12039             (buffer-disable-undo (current-buffer))
12040             (gnus-newsrc-to-gnus-format)
12041             (kill-buffer (current-buffer))
12042             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12043
12044 (defun gnus-read-newsrc-el-file (file)
12045   (let ((ding-file (concat file "d")))
12046     ;; We always, always read the .eld file.
12047     (gnus-message 5 "Reading %s..." ding-file)
12048     (let (gnus-newsrc-assoc)
12049       (condition-case nil
12050           (load ding-file t t t)
12051         (error nil))
12052       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12053     (let ((inhibit-quit t))
12054       (gnus-uncompress-newsrc-assoc))
12055     (gnus-make-hashtable-from-newsrc-alist)
12056     (if (not (file-newer-than-file-p file ding-file))
12057         ()
12058       ;; Old format quick file
12059       (gnus-message 5 "Reading %s..." file)
12060       ;; The .el file is newer than the .eld file, so we read that one
12061       ;; as well. 
12062       (gnus-read-old-newsrc-el-file file))))
12063
12064 ;; Parse the old-style quick startup file
12065 (defun gnus-read-old-newsrc-el-file (file)
12066   (let (newsrc killed marked group g m len info)
12067     (prog1
12068         (let ((gnus-killed-assoc nil)
12069               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12070           (prog1
12071               (condition-case nil
12072                   (load file t t t)
12073                 (error nil))
12074             (setq newsrc gnus-newsrc-assoc
12075                   killed gnus-killed-assoc
12076                   marked gnus-marked-assoc)))
12077       (setq gnus-newsrc-alist nil)
12078       (while newsrc
12079         (setq group (car newsrc))
12080         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12081           (if info
12082               (progn
12083                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12084                 (setcar (cdr info)
12085                         (if (nth 1 group) gnus-level-default-subscribed 
12086                           gnus-level-default-unsubscribed))
12087                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12088             (setq gnus-newsrc-alist
12089                   (cons 
12090                    (setq info
12091                          (list (car group)
12092                                (if (nth 1 group) gnus-level-default-subscribed
12093                                  gnus-level-default-unsubscribed) 
12094                                (cdr (cdr group))))
12095                    gnus-newsrc-alist)))
12096           (if (setq m (assoc (car group) marked))
12097             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12098         (setq newsrc (cdr newsrc)))
12099       (setq newsrc killed)
12100       (while newsrc
12101         (setcar newsrc (car (car newsrc)))
12102         (setq newsrc (cdr newsrc)))
12103       (setq gnus-killed-list killed))
12104     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12105     (gnus-make-hashtable-from-newsrc-alist)))
12106       
12107 (defun gnus-make-newsrc-file (file)
12108   "Make server dependent file name by catenating FILE and server host name."
12109   (let* ((file (expand-file-name file nil))
12110          (real-file (concat file "-" (nth 1 gnus-select-method))))
12111     (if (file-exists-p real-file)
12112         real-file file)))
12113
12114 (defun gnus-uncompress-newsrc-assoc ()
12115   ;; Uncompress all lists of marked articles in the newsrc assoc.
12116   (let ((newsrc gnus-newsrc-alist)
12117         marked)
12118     (while newsrc
12119       (if (not (setq marked (nth 3 (car newsrc))))
12120           ()
12121         (while marked
12122           (or (eq 'score (car (car marked)))
12123               (eq 'bookmark (car (car marked)))
12124               (eq 'killed (car (car marked)))
12125               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12126           (setq marked (cdr marked))))
12127       (setq newsrc (cdr newsrc)))))
12128
12129 (defun gnus-compress-newsrc-assoc ()
12130   ;; Compress all lists of marked articles in the newsrc assoc.
12131   (let ((newsrc gnus-newsrc-alist)
12132         marked)
12133     (while newsrc
12134       (if (not (setq marked (nth 3 (car newsrc))))
12135           ()
12136         (while marked
12137           (or (eq 'score (car (car marked)))
12138               (eq 'bookmark (car (car marked)))
12139               (eq 'killed (car (car marked)))
12140               (setcdr (car marked) 
12141                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
12142           (setq marked (cdr marked))))
12143       (setq newsrc (cdr newsrc)))))
12144
12145 (defun gnus-newsrc-to-gnus-format ()
12146   (setq gnus-newsrc-options "")
12147   (or gnus-active-hashtb
12148       (setq gnus-active-hashtb (make-vector 4095 0)))
12149   (let ((buf (current-buffer))
12150         (already-read (> (length gnus-newsrc-alist) 1))
12151         group level subscribed info options-symbol newsrc
12152         symbol reads num1)
12153     (goto-char (point-min))
12154     ;; We intern the symbol `options' in the active hashtb so that we
12155     ;; can `eq' against it later.
12156     (setq options-symbol (intern "options" gnus-active-hashtb))
12157   
12158     (while (not (eobp))
12159       ;; We first read the first word on the line by narrowing and
12160       ;; then reading into `gnus-active-hashtb'.  Most groups will
12161       ;; already exist in that hashtb, so this will save some string
12162       ;; space.
12163       (narrow-to-region
12164        (point)
12165        (progn (skip-chars-forward "^ \t!:\n") (point)))
12166       (goto-char (point-min))
12167       (setq symbol 
12168             (and (/= (point-min) (point-max))
12169                  (let ((obarray gnus-active-hashtb)) (read buf))))
12170       (widen)
12171       ;; Now, the symbol we have read is either `options' or a group
12172       ;; name.  If it is an options line, we just add it to a string. 
12173       (cond 
12174        ((eq symbol options-symbol)
12175         (setq gnus-newsrc-options
12176               ;; This concatting is quite inefficient, but since our
12177               ;; thorough studies show that approx 99.37% of all
12178               ;; .newsrc files only contain a single options line, we
12179               ;; don't give a damn, frankly, my dear.
12180               (concat gnus-newsrc-options
12181                       (buffer-substring 
12182                        (gnus-point-at-bol)
12183                        ;; Options may continue on the next line.
12184                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12185                                 (progn (beginning-of-line) (point)))
12186                            (point))))))
12187        (symbol
12188         ;; It was a group name.
12189         (setq subscribed (= (following-char) ?:)
12190               group (symbol-name symbol)
12191               reads nil)
12192         (if (eolp)
12193             ;; If the line ends here, this is clearly a buggy line, so
12194             ;; we put point a the beginning of line and let the cond
12195             ;; below do the error handling.
12196             (beginning-of-line)
12197           ;; We skip to the beginning of the ranges.
12198           (skip-chars-forward "!: \t"))
12199         ;; We are now at the beginning of the list of read articles.
12200         ;; We read them range by range.
12201         (while
12202             (cond 
12203              ((looking-at "[0-9]+")
12204               ;; We narrow and read a number instead of buffer-substring/
12205               ;; string-to-int because it's faster. narrow/widen is
12206               ;; faster than save-restriction/narrow, and save-restriction
12207               ;; produces a garbage object.
12208               (setq num1 (progn
12209                            (narrow-to-region (match-beginning 0) (match-end 0))
12210                            (read buf)))
12211               (widen)
12212               ;; If the next character is a dash, then this is a range.
12213               (if (= (following-char) ?-)
12214                   (progn
12215                     ;; We read the upper bound of the range.
12216                     (forward-char 1)
12217                     (if (not (looking-at "[0-9]+"))
12218                         ;; This is a buggy line, by we pretend that
12219                         ;; it's kinda OK. Perhaps the user should be
12220                         ;; dinged? 
12221                         (setq reads (cons num1 reads))
12222                       (setq reads 
12223                             (cons 
12224                              (cons num1 (progn
12225                                           (narrow-to-region (match-beginning 0) 
12226                                                             (match-end 0))
12227                                           (read buf)))
12228                              reads))
12229                       (widen)))
12230                 ;; It was just a simple number, so we add it to the
12231                 ;; list of ranges.
12232                 (setq reads (cons num1 reads)))
12233               ;; If the next char in ?\n, then we have reached the end
12234               ;; of the line and return nil.
12235               (/= (following-char) ?\n))
12236              ((= (following-char) ?\n)
12237               ;; End of line, so we end.
12238               nil)
12239              (t
12240               ;; Not numbers and not eol, so this might be a buggy
12241               ;; line... 
12242               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12243                   (progn
12244                     ;; The line was buggy.
12245                     (setq group nil)
12246                     (gnus-message 3 "Mangled line: %s" 
12247                                   (buffer-substring (gnus-point-at-bol) 
12248                                                     (gnus-point-at-eol)))
12249                     (ding)
12250                     (sit-for 1)))
12251               nil))
12252           ;; Skip past ", ". Spaces are illegal in these ranges, but
12253           ;; we allow them, because it's a common mistake to put a
12254           ;; space after the comma.
12255           (skip-chars-forward ", "))
12256
12257         ;; We have already read .newsrc.eld, so we gently update the
12258         ;; data in the hash table with the information we have just
12259         ;; read. 
12260         (if (not group)
12261             ()
12262           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12263                 level)
12264             (if info
12265                 ;; There is an entry for this file in the alist.
12266                 (progn
12267                   (setcar (nthcdr 2 info) (nreverse reads))
12268                   ;; We update the level very gently.  In fact, we
12269                   ;; only change it if there's been a status change
12270                   ;; from subscribed to unsubscribed, or vice versa.
12271                   (setq level (nth 1 info))
12272                   (cond ((and (<= level gnus-level-subscribed)
12273                               (not subscribed))
12274                          (setq level (if reads
12275                                          gnus-level-default-unsubscribed 
12276                                        (1+ gnus-level-default-unsubscribed))))
12277                         ((and (> level gnus-level-subscribed) subscribed)
12278                          (setq level gnus-level-default-subscribed)))
12279                   (setcar (cdr info) level))
12280               ;; This is a new group.
12281               (setq info (list group 
12282                                (if subscribed
12283                                    gnus-level-default-subscribed 
12284                                  (if reads
12285                                      (1+ gnus-level-subscribed)
12286                                    gnus-level-default-unsubscribed))
12287                                (nreverse reads))))
12288             (setq newsrc (cons info newsrc))))
12289         (forward-line 1))))
12290     
12291     (setq newsrc (nreverse newsrc))
12292
12293     (if (not already-read)
12294         ()
12295       ;; We now have two newsrc lists - `newsrc', which is what we
12296       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12297       ;; what we've read from .newsrc.eld. We have to merge these
12298       ;; lists. We do this by "attaching" any (foreign) groups in the
12299       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12300       (let ((rc (cdr gnus-newsrc-alist))
12301             (prev gnus-newsrc-alist)
12302             entry mentry)
12303         (while rc
12304           (or (assoc (car (car rc)) newsrc)
12305               (if (setq entry (assoc (car (car prev)) newsrc))
12306                   (setcdr (setq mentry (memq entry newsrc))
12307                           (cons (car rc) (cdr mentry)))
12308                 (setq newsrc (cons (car rc) newsrc))))
12309           (setq prev rc
12310                 rc (cdr rc)))))
12311
12312     (setq gnus-newsrc-alist newsrc)
12313     ;; We make the newsrc hashtb.
12314     (gnus-make-hashtable-from-newsrc-alist)
12315
12316     ;; Finally, if we read some options lines, we parse them.
12317     (or (string= gnus-newsrc-options "")
12318         (gnus-newsrc-parse-options gnus-newsrc-options))
12319     ))
12320
12321 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12322 ;; The return value will be a list on the form
12323 ;; ((regexp1 . ignore)
12324 ;;  (regexp2 . subscribe)...)
12325 ;; When handling new newsgroups, groups that match a `ignore' regexp
12326 ;; will be ignored, and groups that match a `subscribe' regexp will be
12327 ;; subscribed. A line like
12328 ;; options -n !all rec.all
12329 ;; will lead to a list that looks like
12330 ;; (("^rec\\..+" . subscribe) 
12331 ;;  ("^.+" . ignore))
12332 ;; So all "rec.*" groups will be subscribed, while all the other
12333 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12334 ;; different from "options -n rec.all !all". 
12335 (defun gnus-newsrc-parse-options (options)
12336   (let (out eol)
12337     (save-excursion
12338       (gnus-set-work-buffer)
12339       (insert (regexp-quote options))
12340       ;; First we treat all continuation lines.
12341       (goto-char (point-min))
12342       (while (re-search-forward "\n[ \t]+" nil t)
12343         (replace-match " " t t))
12344       ;; Then we transform all "all"s into ".+"s.
12345       (goto-char (point-min))
12346       (while (re-search-forward "\\ball\\b" nil t)
12347         (replace-match ".+" t t))
12348       (goto-char (point-min))
12349       ;; We remove all other options than the "-n" ones.
12350       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12351         (replace-match " ")
12352         (forward-char -1))
12353       (goto-char (point-min))
12354
12355       ;; We are only interested in "options -n" lines - we
12356       ;; ignore the other option lines.
12357       (while (re-search-forward "[ \t]-n" nil t)
12358         (setq eol 
12359               (or (save-excursion
12360                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12361                          (- (point) 2)))
12362                   (gnus-point-at-eol)))
12363         ;; Search for all "words"...
12364         (while (re-search-forward "[^ \t,\n-]+" eol t)
12365           (if (= (char-after (match-beginning 0)) ?!)
12366               ;; If the word begins with a bang (!), this is a "not"
12367               ;; spec. We put this spec (minus the bang) and the
12368               ;; symbol `ignore' into the list.
12369               (setq out (cons (cons (concat 
12370                                      "^" (buffer-substring 
12371                                           (1+ (match-beginning 0))
12372                                           (match-end 0)))
12373                                     'ignore) out))
12374             ;; There was no bang, so this is a "yes" spec.
12375             (setq out (cons (cons (concat 
12376                                    "^" (buffer-substring (match-beginning 0)
12377                                                          (match-end 0)))
12378                                   'subscribe) out)))))
12379     
12380       (setq gnus-newsrc-options-n out))))
12381                
12382
12383 (defun gnus-save-newsrc-file ()
12384   "Save .newsrc file."
12385   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12386   ;; from the variable gnus-newsrc-alist.
12387   (and (or gnus-newsrc-alist gnus-killed-list)
12388        gnus-current-startup-file
12389        (let ((make-backup-files t)
12390              (version-control nil)
12391              (require-final-newline t)) ;Don't ask even if requested.
12392          ;; You can stop or change version control of backup file.
12393          ;; Suggested by jason@violet.berkeley.edu.
12394          (run-hooks 'gnus-save-newsrc-hook)
12395          (save-excursion
12396            (if (or (not gnus-dribble-buffer)
12397                    (not (buffer-name gnus-dribble-buffer))
12398                    (zerop (save-excursion
12399                             (set-buffer gnus-dribble-buffer)
12400                             (buffer-size))))
12401                (gnus-message 4 "(No changes need to be saved)")
12402              (if gnus-save-newsrc-file
12403                  (progn
12404                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12405                    ;; Make backup file of master newsrc.
12406                    (gnus-gnus-to-newsrc-format)
12407                    (gnus-message 5 "Saving %s...done"
12408                                  gnus-current-startup-file)))
12409              ;; Quickly loadable .newsrc.
12410              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12411              (gnus-add-current-to-buffer-list)
12412              (buffer-disable-undo (current-buffer))
12413              (erase-buffer)
12414              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12415              (gnus-gnus-to-quick-newsrc-format)
12416              (write-region 1 (point-max) 
12417                            (concat gnus-current-startup-file ".eld") 
12418                            nil 'nomesg)
12419              (kill-buffer (current-buffer))
12420              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12421              (gnus-dribble-delete-file))))))
12422
12423 (defun gnus-gnus-to-quick-newsrc-format ()
12424   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12425   (insert ";; (ding) Gnus startup file.\n")
12426   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12427   (insert ";; to read .newsrc.\n")
12428   (let ((variables gnus-variable-list)
12429         (inhibit-quit t)
12430         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12431         variable)
12432     ;; insert lisp expressions.
12433     (gnus-compress-newsrc-assoc)
12434     (while variables
12435       (setq variable (car variables))
12436       (and (boundp variable)
12437            (symbol-value variable)
12438            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12439            (insert "(setq " (symbol-name variable) " '"
12440                    (prin1-to-string (symbol-value variable))
12441                    ")\n"))
12442       (setq variables (cdr variables)))
12443     (gnus-uncompress-newsrc-assoc)))
12444
12445
12446 (defun gnus-gnus-to-newsrc-format ()
12447   ;; Generate and save the .newsrc file.
12448   (let ((newsrc (cdr gnus-newsrc-alist))
12449         info ranges range)
12450     (save-excursion
12451       (set-buffer (create-file-buffer gnus-startup-file))
12452       (buffer-disable-undo (current-buffer))
12453       (erase-buffer)
12454       ;; Write options.
12455       (if gnus-newsrc-options (insert gnus-newsrc-options))
12456       ;; Write subscribed and unsubscribed.
12457       (while newsrc
12458         (setq info (car newsrc))
12459         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12460             (progn
12461               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12462                                      "!" ":"))
12463               (if (setq ranges (nth 2 info))
12464                   (progn
12465                     (insert " ")
12466                     (if (not (listp (cdr ranges)))
12467                         (if (= (car ranges) (cdr ranges))
12468                             (insert (int-to-string (car ranges)))
12469                           (insert (int-to-string (car ranges)) "-" 
12470                                   (int-to-string (cdr ranges))))
12471                       (while ranges
12472                         (setq range (car ranges)
12473                               ranges (cdr ranges))
12474                         (if (or (atom range) (= (car range) (cdr range)))
12475                             (insert (int-to-string 
12476                                      (or (and (atom range) range) 
12477                                          (car range))))
12478                           (insert (int-to-string (car range)) "-"
12479                                   (int-to-string (cdr range))))
12480                         (if ranges (insert ","))))))
12481               (insert "\n")))
12482         (setq newsrc (cdr newsrc)))
12483       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12484       (kill-buffer (current-buffer)))))
12485
12486 (defun gnus-read-descriptions-file ()
12487   (gnus-message 5 "Reading descriptions file...")
12488   (cond 
12489    ((not (or (gnus-server-opened gnus-select-method)
12490              (gnus-open-server gnus-select-method)))
12491     (gnus-message 1 "Couldn't open server")
12492     nil)
12493    ((not (gnus-request-list-newsgroups gnus-select-method))
12494     (gnus-message 1 "Couldn't read newsgroups descriptions")
12495     nil)
12496    (t
12497     (let (group)
12498       (setq gnus-description-hashtb 
12499             (gnus-make-hashtable (length gnus-active-hashtb)))
12500       (save-excursion
12501         (save-restriction
12502           (set-buffer nntp-server-buffer)
12503           (goto-char (point-min))
12504           (if (or (search-forward "\n.\n" nil t)
12505                   (goto-char (point-max)))
12506               (progn
12507                 (beginning-of-line)
12508                 (narrow-to-region (point-min) (point))))
12509           (goto-char (point-min))
12510           (while (not (eobp))
12511             ;; If we get an error, we set group to 0, which is not a
12512             ;; symbol... 
12513             (setq group 
12514                   (condition-case ()
12515                       (let ((obarray gnus-description-hashtb))
12516                         ;; Group is set to a symbol interned in this
12517                         ;; hash table.
12518                         (read nntp-server-buffer))
12519                     (error 0)))
12520             (skip-chars-forward " \t")
12521             ;; ... which leads to this line being effectively ignored.
12522             (and (symbolp group)
12523                  (set group (buffer-substring 
12524                              (point) (progn (end-of-line) (point)))))
12525             (forward-line 1))))
12526       (gnus-message 5 "Reading descriptions file...done")
12527       t))))
12528
12529 (defun gnus-group-get-description (group)
12530   ;; Get the description of a group by sending XGTITLE to the server.
12531   (and (gnus-request-group-description group)
12532        (save-excursion
12533          (set-buffer nntp-server-buffer)
12534          (goto-char (point-min))
12535          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12536               (buffer-substring (match-beginning 1) (match-end 1))))))
12537
12538 ;;;
12539 ;;; Server
12540 ;;;
12541
12542 (defvar gnus-server-mode-hook nil
12543   "Hook run in `gnus-server-mode' buffers.")
12544
12545 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12546   "Format of server lines.
12547 It works along the same lines as a normal formatting string,
12548 with some simple extensions.")
12549
12550 (defvar gnus-server-mode-line-format "(ding) List of servers"
12551   "The format specification for the server mode line.")
12552
12553 (defconst gnus-server-line-format-alist
12554   (list (list ?h 'how ?s)
12555         (list ?n 'name ?s)
12556         (list ?w 'where ?s)
12557         ))
12558
12559 (defconst gnus-server-mode-line-format-alist 
12560   (list (list ?S 'news-server ?s)
12561         (list ?M 'news-method ?s)
12562         (list ?u 'user-defined ?s)))
12563
12564 (defvar gnus-server-line-format-spec nil)
12565 (defvar gnus-server-mode-line-format-spec nil)
12566 (defvar gnus-server-killed-servers nil)
12567
12568 (defvar gnus-server-mode-map nil)
12569 (put 'gnus-server-mode 'mode-class 'special)
12570
12571 (if gnus-server-mode-map
12572     nil
12573   (setq gnus-server-mode-map (make-sparse-keymap))
12574   (suppress-keymap gnus-server-mode-map)
12575   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12576   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12577   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12578   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12579   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12580   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12581   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12582   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12583   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12584   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12585
12586 (defun gnus-server-mode ()
12587   "Major mode for listing and editing servers.
12588
12589 All normal editing commands are switched off.
12590 \\<gnus-server-mode-map>
12591
12592 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12593
12594 The following commands are available:
12595
12596 \\{gnus-server-mode-map}"
12597   (interactive)
12598   (if gnus-visual (gnus-server-make-menu-bar))
12599   (kill-all-local-variables)
12600   (setq mode-line-modified "-- ")
12601   (make-local-variable 'mode-line-format)
12602   (setq mode-line-format (copy-sequence mode-line-format))
12603   (and (equal (nth 3 mode-line-format) "   ")
12604        (setcar (nthcdr 3 mode-line-format) ""))
12605   (setq major-mode 'gnus-server-mode)
12606   (setq mode-name "Server")
12607 ;  (gnus-group-set-mode-line)
12608   (setq mode-line-process nil)
12609   (use-local-map gnus-server-mode-map)
12610   (buffer-disable-undo (current-buffer))
12611   (setq truncate-lines t)
12612   (setq buffer-read-only t)
12613   (run-hooks 'gnus-server-mode-hook))
12614
12615 (defun gnus-server-insert-server-line (sformat name method)
12616   (let* ((sformat (or sformat gnus-server-line-format-spec))
12617          (how (car method))
12618          (where (nth 1 method))
12619          b)
12620     (beginning-of-line)
12621     (setq b (point))
12622     ;; Insert the text.
12623     (insert (eval sformat))
12624     (add-text-properties 
12625      b (1+ b) (list 'gnus-server (intern name)))))
12626
12627 (defun gnus-server-setup-buffer ()
12628   (if (get-buffer gnus-server-buffer)
12629       ()
12630     (save-excursion
12631       (set-buffer (get-buffer-create gnus-server-buffer))
12632       (gnus-server-mode)
12633       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12634
12635 (defun gnus-server-prepare ()
12636   (setq gnus-server-mode-line-format-spec 
12637         (gnus-parse-format gnus-server-mode-line-format 
12638                            gnus-server-mode-line-format-alist))
12639   (setq gnus-server-line-format-spec 
12640         (gnus-parse-format gnus-server-line-format 
12641                            gnus-server-line-format-alist))
12642   (let ((alist gnus-server-alist)
12643         (buffer-read-only nil))
12644     (erase-buffer)
12645     (while alist
12646       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12647       (setq alist (cdr alist))))
12648   (goto-char (point-min))
12649   (gnus-server-position-cursor))
12650
12651 (defun gnus-server-server-name ()
12652   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12653     (and server (symbol-name server))))
12654
12655 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12656
12657 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12658
12659 (defun gnus-server-update-server (server)
12660   (save-excursion
12661     (set-buffer gnus-server-buffer)
12662     (let ((buffer-read-only nil)
12663           (info (cdr (assoc server gnus-server-alist))))
12664       (gnus-dribble-enter 
12665        (concat "(gnus-server-set-info \"" server "\" '"
12666                (prin1-to-string info) ")"))
12667       ;; Buffer may be narrowed.
12668       (save-restriction
12669         (widen)
12670         (if (gnus-server-goto-server server)
12671             (delete-region (progn (beginning-of-line) (point))
12672                            (progn (forward-line 1) (point))))
12673         (let ((entry (assoc server gnus-server-alist)))
12674           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12675           (gnus-server-position-cursor))))))
12676
12677 (defun gnus-server-set-info (server info)
12678   ;; Enter a select method into the virtual server alist.
12679   (gnus-dribble-enter 
12680    (concat "(gnus-server-set-info \"" server "\" '"
12681            (prin1-to-string info) ")"))
12682   (let* ((server (nth 1 info))
12683          (entry (assoc server gnus-server-alist)))
12684     (if entry (setcdr entry info)
12685       (setq gnus-server-alist
12686             (nconc gnus-server-alist (list (cons server info)))))))
12687
12688 (defun gnus-server-to-method (server)
12689   ;; Map virtual server names to select methods.
12690   (or (and (equal server "native") gnus-select-method)
12691       (cdr (assoc server gnus-server-alist))))
12692
12693 (defun gnus-server-extend-method (group method)
12694   ;; This function "extends" a virtual server.  If the server is
12695   ;; "hello", and the select method is ("hello" (my-var "something")) 
12696   ;; in the group "alt.alt", this will result in a new virtual server
12697   ;; called "helly+alt.alt".
12698   (let ((entry
12699          (gnus-copy-sequence 
12700           (if (equal (car method) "native") gnus-select-method
12701               (cdr (assoc (car method) gnus-server-alist))))))
12702     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12703     (nconc entry (cdr method))))
12704
12705 (defun gnus-server-get-method (group method)
12706   ;; Input either a server name, and extended server name, or a
12707   ;; select method, and return a select method. 
12708   (cond ((stringp method)
12709          (gnus-server-to-method method))
12710         ((stringp (car method))
12711          (gnus-server-extend-method group method))
12712         (t
12713          (gnus-server-add-address method))))
12714
12715 (defun gnus-server-add-address (method)
12716   (let ((method-name (symbol-name (car method))))
12717     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12718              (not (assq (intern (concat method-name "-address")) method)))
12719         (append method (list (list (intern (concat method-name "-address"))
12720                                    (nth 1 method))))
12721       method)))
12722
12723 (defun gnus-server-equal (s1 s2)
12724   (or (equal s1 s2)
12725       (and (= (length s1) (length s2))
12726            (progn
12727              (while (and s1 (member (car s1) s2))
12728                (setq s1 (cdr s1)))
12729              (null s1)))))
12730
12731 ;;; Interactive server functions.
12732
12733 (defun gnus-server-kill-server (server)
12734   "Kill the server on the current line."
12735   (interactive (list (gnus-server-server-name)))
12736   (or (gnus-server-goto-server server)
12737       (if server (error "No such server: %s" server)
12738         (error "No server on the current line")))
12739   (let ((buffer-read-only nil))
12740     (delete-region (progn (beginning-of-line) (point))
12741                    (progn (forward-line 1) (point))))
12742   (setq gnus-server-killed-servers 
12743         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12744   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12745                                 gnus-server-alist))
12746   (gnus-server-position-cursor))
12747
12748 (defun gnus-server-yank-server ()
12749   "Yank the previously killed server."
12750   (interactive)
12751   (or gnus-server-killed-servers
12752       (error "No killed servers to be yanked"))
12753   (let ((alist gnus-server-alist)
12754         (server (gnus-server-server-name))
12755         (killed (car gnus-server-killed-servers)))
12756     (if (not server) 
12757         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12758       (if (string= server (car (car gnus-server-alist)))
12759           (setq gnus-server-alist (cons killed gnus-server-alist))
12760         (while (and (cdr alist)
12761                     (not (string= server (car (car (cdr alist))))))
12762           (setq alist (cdr alist)))
12763         (setcdr alist (cons killed (cdr alist)))))
12764     (gnus-server-update-server (car killed))
12765     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12766     (gnus-server-position-cursor)))
12767
12768 (defun gnus-server-exit ()
12769   "Return to the group buffer."
12770   (interactive)
12771   (kill-buffer (current-buffer))
12772   (switch-to-buffer gnus-group-buffer))
12773
12774 (defun gnus-server-list-servers ()
12775   "List all available servers."
12776   (interactive)
12777   (let ((cur (gnus-server-server-name)))
12778     (gnus-server-prepare)
12779     (if cur (gnus-server-goto-server cur)
12780       (goto-char (point-max))
12781       (forward-line -1))
12782     (gnus-server-position-cursor)))
12783
12784 (defun gnus-server-copy-server (from to)
12785   (interactive
12786    (list
12787     (or (gnus-server-server-name)
12788         (error "No server on the current line"))
12789     (read-string "Copy to: ")))
12790   (or from (error "No server on current line"))
12791   (or (and to (not (string= to ""))) (error "No name to copy to"))
12792   (and (assoc to gnus-server-alist) (error "%s already exists" to))
12793   (or (assoc from gnus-server-alist) 
12794       (error "%s: no such server" from))
12795   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
12796     (setcar to-entry to)
12797     (setcar (nthcdr 2 to-entry) to)
12798     (setq gnus-server-killed-servers 
12799           (cons to-entry gnus-server-killed-servers))
12800     (gnus-server-yank-server)))
12801
12802 (defun gnus-server-add-server (how where)
12803   (interactive 
12804    (list (intern (completing-read "Server method: "
12805                                   gnus-valid-select-methods nil t))
12806          (read-string "Server name: ")))
12807   (setq gnus-server-killed-servers 
12808         (cons (list where how where) gnus-server-killed-servers))
12809   (gnus-server-yank-server))
12810
12811 (defun gnus-server-goto-server (server)
12812   "Jump to a server line."
12813   (interactive
12814    (list (completing-read "Goto server: " gnus-server-alist nil t)))
12815   (let ((to (text-property-any (point-min) (point-max) 
12816                                'gnus-server (intern server))))
12817     (and to
12818          (progn
12819            (goto-char to) 
12820            (gnus-server-position-cursor)))))
12821
12822 (defun gnus-server-edit-server (server)
12823   "Edit the server on the current line."
12824   (interactive (list (gnus-server-server-name)))
12825   (or server
12826       (error "No server on current line"))
12827   (let ((winconf (current-window-configuration)))
12828     (get-buffer-create gnus-server-edit-buffer)
12829     (gnus-configure-windows 'edit-server)
12830     (gnus-add-current-to-buffer-list)
12831     (emacs-lisp-mode)
12832     (make-local-variable 'gnus-prev-winconf)
12833     (setq gnus-prev-winconf winconf)
12834     (use-local-map (copy-keymap (current-local-map)))
12835     (let ((done-func '(lambda () 
12836                         "Exit editing mode and update the information."
12837                         (interactive)
12838                         (gnus-server-edit-server-done 'group))))
12839       (setcar (cdr (nth 4 done-func)) server)
12840       (local-set-key "\C-c\C-c" done-func))
12841     (erase-buffer)
12842     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
12843     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
12844
12845 (defun gnus-server-edit-server-done (server)
12846   (interactive)
12847   (set-buffer (get-buffer-create gnus-server-edit-buffer))
12848   (goto-char (point-min))
12849   (let ((form (read (current-buffer)))
12850         (winconf gnus-prev-winconf))
12851     (gnus-server-set-info server form)
12852     (kill-buffer (current-buffer))
12853     (and winconf (set-window-configuration winconf))
12854     (set-buffer gnus-server-buffer)
12855     (gnus-server-update-server (gnus-server-server-name))
12856     (gnus-server-position-cursor)))
12857
12858 (defun gnus-server-read-server (server)
12859   "Browse a server."
12860   (interactive (list (gnus-server-server-name)))
12861   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
12862
12863 (defun gnus-mouse-pick-server (e)
12864   (interactive "e")
12865   (mouse-set-point e)
12866   (gnus-server-read-server (gnus-server-server-name)))
12867
12868 ;;;
12869 ;;; entry points into gnus-score.el
12870 ;;;
12871
12872 ;;; Finding score files. 
12873
12874 (defvar gnus-global-score-files nil
12875   "*List of global score files and directories.
12876 Set this variable if you want to use people's score files.  One entry
12877 for each score file or each score file directory.  Gnus will decide
12878 by itself what score files are applicable to which group.
12879
12880 Say you want to use the single score file
12881 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
12882 score files in the \"/ftp.some-where:/pub/score\" directory.
12883
12884  (setq gnus-global-score-files
12885        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
12886          \"/ftp.some-where:/pub/score\"))")
12887
12888 (defun gnus-score-score-files (group)
12889   "Return a list of all possible score files."
12890   ;; Search and set any global score files.
12891   (and gnus-global-score-files 
12892        (or gnus-internal-global-score-files
12893            (gnus-score-search-global-directories gnus-global-score-files)))
12894   ;; Fix the kill-file dir variable.
12895   (setq gnus-kill-files-directory 
12896         (file-name-as-directory
12897          (or gnus-kill-files-directory "~/News/")))
12898   ;; If er can't read it, there's no score files.
12899   (if (not (file-readable-p (expand-file-name gnus-kill-files-directory)))
12900       (setq gnus-score-file-list nil)
12901     (if (gnus-use-long-file-name 'not-score)
12902         ;; We want long file names.
12903         (if (or (not gnus-score-file-list)
12904                 (not (car gnus-score-file-list))
12905                 (gnus-file-newer-than gnus-kill-files-directory
12906                                       (car gnus-score-file-list)))
12907               (setq gnus-score-file-list 
12908                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
12909                           (nreverse 
12910                            (directory-files 
12911                             gnus-kill-files-directory t 
12912                             (gnus-score-file-regexp))))))
12913       ;; We do not use long file names, so we have to do some
12914       ;; directory traversing.  
12915       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
12916             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
12917             dir files suffix)
12918         (while suffixes
12919           (setq dir (expand-file-name
12920                      (concat gnus-kill-files-directory
12921                              (gnus-replace-chars-in-string group ?. ?/))))
12922           (setq suffix (car suffixes)
12923                 suffixes (cdr suffixes))
12924           (if (file-exists-p (concat dir "/" suffix))
12925               (setq files (cons (concat dir "/" suffix) files)))
12926           (while (>= (1+ (length dir)) mdir)
12927             (and (file-exists-p (concat dir "/all/" suffix))
12928                  (setq files (cons (concat dir "/all/" suffix) files)))
12929             (string-match "/[^/]*$" dir)
12930             (setq dir (substring dir 0 (match-beginning 0)))))
12931         (setq gnus-score-file-list 
12932               (cons nil (nreverse files)))))
12933     (cdr gnus-score-file-list)))
12934
12935 (defun gnus-score-file-regexp ()
12936   (concat "\\(" gnus-score-file-suffix 
12937           "\\|" gnus-adaptive-file-suffix "\\)$"))
12938         
12939 (defun gnus-score-find-bnews (group)
12940   "Return a list of score files for GROUP.
12941 The score files are those files in the ~/News directory which matches
12942 GROUP using BNews sys file syntax."
12943   (let* ((sfiles (append (gnus-score-score-files group)
12944                          gnus-internal-global-score-files))
12945          (kill-dir (file-name-as-directory 
12946                     (expand-file-name gnus-kill-files-directory)))
12947          (klen (length kill-dir))
12948          ofiles not-match regexp)
12949     (save-excursion
12950       (set-buffer (get-buffer-create "*gnus score files*"))
12951       (buffer-disable-undo (current-buffer))
12952       ;; Go through all score file names and create regexp with them
12953       ;; as the source.  
12954       (while sfiles
12955         (erase-buffer)
12956         (insert (car sfiles))
12957         (goto-char (point-min))
12958         ;; First remove the suffix itself.
12959         (re-search-forward (concat "." (gnus-score-file-regexp)))
12960         (replace-match "" t t) 
12961         (goto-char (point-min))
12962         (if (looking-at (regexp-quote kill-dir))
12963             ;; If the file name was just "SCORE", `klen' is one character
12964             ;; too much.
12965             (delete-char (min (1- (point-max)) klen))
12966           (goto-char (point-max))
12967           (search-backward "/")
12968           (delete-region (1+ (point)) (point-min)))
12969         ;; If short file names were used, we have to translate slashes.
12970         (goto-char (point-min))
12971         (while (search-forward "/" nil t)
12972           (replace-match "." t t))
12973         ;; Translate "all" to ".*".
12974         (while (search-forward "all" nil t)
12975           (replace-match ".*" t t))
12976         (goto-char (point-min))
12977         ;; Deal with "not."s.
12978         (if (looking-at "not.")
12979             (progn
12980               (setq not-match t)
12981               (setq regexp (buffer-substring 5 (point-max))))
12982           (setq regexp (buffer-substring 1 (point-max)))
12983           (setq not-match nil))
12984         ;; Finally - if this resulting regexp matches the group name,
12985         ;; we add this score file to the list of score files
12986         ;; applicable to this group.
12987         (if (or (and not-match
12988                      (not (string-match regexp group)))
12989                 (and (not not-match)
12990                      (string-match regexp group)))
12991             (setq ofiles (cons (car sfiles) ofiles)))
12992         (setq sfiles (cdr sfiles)))
12993       (kill-buffer (current-buffer))
12994       ;; Slight kludge here - the last score file returned should be
12995       ;; the local score file, whether it exists or not. This is so
12996       ;; that any score commands the user enters will go to the right
12997       ;; file, and not end up in some global score file.
12998       (let ((localscore
12999              (expand-file-name
13000               (if (gnus-use-long-file-name 'not-score)
13001                   (concat gnus-kill-files-directory group "." 
13002                           gnus-score-file-suffix)
13003                 (concat gnus-kill-files-directory
13004                         (gnus-replace-chars-in-string group ?. ?/)
13005                         "/" gnus-score-file-suffix)))))
13006         (and (member localscore ofiles)
13007              (delete localscore ofiles))
13008         (setq ofiles (cons localscore ofiles)))
13009       (nreverse ofiles))))
13010
13011 (defun gnus-score-find-single (group)
13012   "Return list containing the score file for GROUP."
13013   (list (gnus-score-file-name group)))
13014
13015 (defun gnus-score-find-hierarchical (group)
13016   "Return list of score files for GROUP.
13017 This includes the score file for the group and all its parents."
13018   (let ((all (copy-sequence '(nil)))
13019         (start 0))
13020     (while (string-match "\\." group (1+ start))
13021       (setq start (match-beginning 0))
13022       (setq all (cons (substring group 0 start) all)))
13023     (setq all (cons group all))
13024     (mapcar 'gnus-score-file-name (nreverse all))))
13025
13026 (defvar gnus-score-file-alist-cache nil)
13027
13028 (defun gnus-score-find-alist (group)
13029   "Return list of score files for GROUP.
13030 The list is determined from the variable gnus-score-file-alist."
13031   (let ((alist gnus-score-file-multiple-match-alist)
13032         score-files)
13033     ;; if this group has been seen before, return the cached entry
13034     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13035         (cdr score-files)       ; ensures caching of groups with no matches
13036       ;; handle the multiple match alist
13037       (while alist
13038         (and (string-match (car (car alist)) group)
13039              (setq score-files
13040                    (nconc score-files (cdr (car alist)))))
13041         (setq alist (cdr alist)))
13042       (setq alist gnus-score-file-single-match-alist)
13043       ;; handle the single match alist
13044       (catch 'done
13045         (while alist
13046           (and (string-match (car (car alist)) group)
13047                ;; progn used just in case ("regexp") has no files
13048                ;; and score-files is still nil. -sj
13049                ;; this can be construed as a "stop searching here" feature :>
13050                ;; and used to simplify regexps in the single-alist 
13051                (progn
13052                  (setq score-files
13053                        (nconc score-files (cdr (car alist))))
13054                  (throw 'done nil)))
13055           (setq alist (cdr alist))))
13056       ;; cache the score files
13057       (setq gnus-score-file-alist-cache
13058             (cons (cons group score-files) gnus-score-file-alist-cache))
13059       score-files)))
13060
13061
13062 (defun gnus-possibly-score-headers (&optional trace)
13063   (let ((func gnus-score-find-score-files-function)
13064         score-files scores)
13065     (and func (not (listp func))
13066          (setq func (list func)))
13067     ;; Go through all the functions for finding score files (or actual
13068     ;; scores) and add them to a list.
13069     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
13070     (while func
13071       (and (symbolp (car func))
13072            (fboundp (car func))
13073            (setq score-files 
13074                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13075       (setq func (cdr func)))
13076     (if score-files (gnus-score-headers score-files trace))))
13077
13078 (defun gnus-score-file-name (newsgroup &optional suffix)
13079   "Return the name of a score file for NEWSGROUP."
13080   (let ((suffix (or suffix gnus-score-file-suffix)))
13081     (cond  ((or (null newsgroup)
13082                 (string-equal newsgroup ""))
13083             ;; The global score file is placed at top of the directory.
13084             (expand-file-name 
13085              suffix (or gnus-kill-files-directory "~/News")))
13086            ((gnus-use-long-file-name 'not-score)
13087             ;; Append ".SCORE" to newsgroup name.
13088             (expand-file-name (concat newsgroup "." suffix)
13089                               (or gnus-kill-files-directory "~/News")))
13090            (t
13091             ;; Place "SCORE" under the hierarchical directory.
13092             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13093                                       "/" suffix)
13094                               (or gnus-kill-files-directory "~/News"))))))
13095
13096 (defun gnus-score-search-global-directories (files)
13097   "Scan all global score directories for score files."
13098   ;; Set the variable `gnus-internal-global-score-files' to all
13099   ;; available global score files.
13100   (interactive (list gnus-global-score-files))
13101   (let (out)
13102     (while files
13103       (if (string-match "/$" (car files))
13104           (setq out (nconc (directory-files 
13105                             (car files) t
13106                             (concat (gnus-score-file-regexp) "$"))))
13107         (setq out (cons (car files) out)))
13108       (setq files (cdr files)))
13109     (setq gnus-internal-global-score-files out)))
13110
13111 ;; Allow redefinition of Gnus functions.
13112
13113 (gnus-ems-redefine)
13114
13115 (provide 'gnus)
13116
13117 ;;; gnus.el ends here