*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@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 multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread 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-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is nil.")
552
553 (defvar gnus-group-use-permanent-levels nil
554   "*If non-nil, once you set a level, Gnus will use this level.")
555
556 (defvar gnus-show-mime nil
557   "*If non-ni, do mime processing of articles.
558 The articles will simply be fed to the function given by
559 `gnus-show-mime-method'.")
560
561 (defvar gnus-strict-mime t
562   "*If nil, decode MIME header even if there is not Mime-Version field.")
563  
564 (defvar gnus-show-mime-method (function metamail-buffer)
565   "*Function to process a MIME message.
566 The function is called from the article buffer.")
567
568 (defvar gnus-show-threads t
569   "*If non-nil, display threads in summary mode.")
570
571 (defvar gnus-thread-hide-subtree nil
572   "*If non-nil, hide all threads initially.
573 If threads are hidden, you have to run the command
574 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
575 to expose hidden threads.")
576
577 (defvar gnus-thread-hide-killed t
578   "*If non-nil, hide killed threads automatically.")
579
580 (defvar gnus-thread-ignore-subject nil
581   "*If non-nil, ignore subjects and do all threading based on the Reference header.
582 If nil, which is the default, articles that have different subjects
583 from their parents will start separate threads.")
584
585 (defvar gnus-thread-indent-level 4
586   "*Number that says how much each sub-thread should be indented.")
587
588 (defvar gnus-ignored-newsgroups ""
589   "*A regexp to match uninteresting newsgroups in the active file.
590 Any lines in the active file matching this regular expression are
591 removed from the newsgroup list before anything else is done to it,
592 thus making them effectively non-existent.")
593
594 (defvar gnus-ignored-headers
595   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
596   "*All headers that match this regexp will be hidden.
597 Also see `gnus-visible-headers'.")
598
599 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
600   "*All headers that do not match this regexp will be hidden.
601 Also see `gnus-ignored-headers'.")
602
603 (defvar gnus-sorted-header-list
604   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
605     "^Cc:" "^Date:" "^Organization:")
606   "*This variable is a list of regular expressions.
607 If it is non-nil, headers that match the regular expressions will
608 be placed first in the article buffer in the sequence specified by
609 this list.")
610
611 (defvar gnus-show-all-headers nil
612   "*If non-nil, don't hide any headers.")
613
614 (defvar gnus-save-all-headers t
615   "*If non-nil, don't remove any headers before saving.")
616
617 (defvar gnus-inhibit-startup-message nil
618   "*If non-nil, the startup message will not be displayed.")
619
620 (defvar gnus-signature-separator "^-- *$"
621   "Regexp matching signature separator.")
622
623 (defvar gnus-auto-extend-newsgroup t
624   "*If non-nil, extend newsgroup forward and backward when requested.")
625
626 (defvar gnus-auto-select-first t
627   "*If non-nil, select the first unread article when entering a group.
628 If you want to prevent automatic selection of the first unread article
629 in some newsgroups, set the variable to nil in
630 `gnus-select-group-hook'.") 
631
632 (defvar gnus-auto-select-next t
633   "*If non-nil, offer to go to the next group from the end of the previous.
634 If the value is t and the next newsgroup is empty, Gnus will exit
635 summary mode and go back to group mode.  If the value is neither nil
636 nor t, Gnus will select the following unread newsgroup.  In
637 particular, if the value is the symbol `quietly', the next unread
638 newsgroup will be selected without any confirmations.")
639
640 (defvar gnus-auto-select-same nil
641   "*If non-nil, select the next article with the same subject.")
642
643 (defvar gnus-summary-check-current nil
644   "*If non-nil, consider the current article when moving.
645 The \"unread\" movement commands will stay on the same line if the
646 current article is unread.")
647
648 (defvar gnus-auto-center-summary t
649   "*If non-nil, always center the current summary buffer.")
650
651 (defvar gnus-break-pages t
652   "*If non-nil, do page breaking on articles.
653 The page delimiter is specified by the `gnus-page-delimiter'
654 variable.")
655
656 (defvar gnus-page-delimiter "^\^L"
657   "*Regexp describing what to use as article page delimiters.
658 The default value is \"^\^L\", which is a form linefeed at the
659 beginning of a line.")
660
661 (defvar gnus-use-full-window t
662   "*If non-nil, use the entire Emacs screen.")
663
664 (defvar gnus-window-configuration nil
665   "Obsolete variable.  See `gnus-buffer-configuration'.")
666
667 (defvar gnus-buffer-configuration
668   '((group ([group 1.0 point] 
669             (if gnus-carpal [group-carpal 4])))
670     (summary ([summary 1.0 point]
671               (if gnus-carpal [summary-carpal 4])))
672     (article ([summary 0.25 point] 
673               (if gnus-carpal [summary-carpal 4]) 
674               [article 1.0]))
675     (server ([server 1.0 point]
676              (if gnus-carpal [server-carpal 2])))
677     (browse ([browse 1.0 point]
678              (if gnus-carpal [browse-carpal 2])))
679     (group-mail ([mail 1.0 point]))
680     (summary-mail ([mail 1.0 point]))
681     (summary-reply ([article 0.5]
682                     [mail 1.0 point]))
683     (info ([nil 1.0 point]))
684     (summary-faq ([summary 0.25]
685                   [article 1.0 point]))
686     (edit-group ([group 0.5]
687                  [edit-group 1.0 point]))
688     (edit-server ([server 0.5]
689                   [edit-server 1.0 point]))
690     (edit-score ([summary 0.25]
691                  [edit-score 1.0 point]))
692     (post ([post 1.0 point]))
693     (reply ([article 0.5]
694             [mail 1.0 point]))
695     (mail-forward ([mail 1.0 point]))
696     (post-forward ([post 1.0 point]))
697     (reply-yank ([mail 1.0 point]))
698     (followup ([article 0.5]
699                [post 1.0 point]))
700     (followup-yank ([post 1.0 point])))
701   "Window configuration for all possible Gnus buffers.
702 This variable is a list of lists.  Each of these lists has a NAME and
703 a RULE.  The NAMEs are commonsense names like `group', which names a
704 rule used when displaying the group buffer; `summary', which names a
705 rule for what happens when you enter a group and do not display an
706 article buffer; and so on.  See the value of this variable for a
707 complete list of NAMEs.
708
709 Each RULE is a list of vectors.  The first element in this vector is
710 the name of the buffer to be displayed; the second element is the
711 percentage of the screen this buffer is to occupy (a number in the
712 0.0-0.99 range); the optional third element is `point', which should
713 be present to denote which buffer point is to go to after making this
714 buffer configuration.")
715
716 (defvar gnus-window-to-buffer
717   '((group . gnus-group-buffer)
718     (summary . gnus-summary-buffer)
719     (article . gnus-article-buffer)
720     (server . gnus-server-buffer)
721     (browse . "*Gnus Browse Server*")
722     (edit-group . gnus-group-edit-buffer)
723     (edit-server . gnus-server-edit-buffer)
724     (group-carpal . gnus-carpal-group-buffer)
725     (summary-carpal . gnus-carpal-summary-buffer)
726     (server-carpal . gnus-carpal-server-buffer)
727     (browse-carpal . gnus-carpal-browse-buffer)
728     (edit-score . gnus-score-edit-buffer)
729     (mail . gnus-mail-buffer)
730     (post . gnus-post-news-buffer))
731   "Mapping from short symbols to buffer names or buffer variables.")
732
733 (defvar gnus-carpal nil
734   "*If non-nil, display clickable icons.")
735
736 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
737   "*Function called with a group name when new group is detected.
738 A few pre-made functions are supplied: `gnus-subscribe-randomly'
739 inserts new groups at the beginning of the list of groups;
740 `gnus-subscribe-alphabetically' inserts new groups in strict
741 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
742 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
743 for your decision.")
744
745 ;; Suggested by a bug report by Hallvard B Furuseth.
746 ;; <h.b.furuseth@usit.uio.no>. 
747 (defvar gnus-subscribe-options-newsgroup-method
748   (function gnus-subscribe-alphabetically)
749   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
750 If, for instance, you want to subscribe to all newsgroups in the
751 \"no\" and \"alt\" hierarchies, you'd put the following in your
752 .newsrc file:
753
754 options -n no.all alt.all
755
756 Gnus will the subscribe all new newsgroups in these hierarchies with
757 the subscription method in this variable.")
758
759 (defvar gnus-subscribe-hierarchical-interactive nil
760   "*If non-nil, Gnus will offer to subscribe hierarchically.
761 When a new hierarchy appears, Gnus will ask the user:
762
763 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
764
765 If the user pressed `d', Gnus will descend the hierarchy, `y' will
766 subscribe to all newsgroups in the hierarchy and `s' will skip this
767 hierarchy in its entirety.")
768
769 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
770   "*Function used for sorting the group buffer.
771 This function will be called with group info entries as the arguments
772 for the groups to be sorted.  Pre-made functions include
773 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
774 `gnus-sort-by-level'")
775
776 ;; Mark variables suggested by Thomas Michanek
777 ;; <Thomas.Michanek@telelogic.se>. 
778 (defvar gnus-unread-mark ? 
779   "*Mark used for unread articles.")
780 (defvar gnus-ticked-mark ?!
781   "*Mark used for ticked articles.")
782 (defvar gnus-dormant-mark ??
783   "*Mark used for dormant articles.")
784 (defvar gnus-del-mark ?D
785   "*Mark used for del'd articles.")
786 (defvar gnus-read-mark ?d
787   "*Mark used for read articles.")
788 (defvar gnus-expirable-mark ?E
789   "*Mark used for expirable articles.")
790 (defvar gnus-killed-mark ?K
791   "*Mark used for killed articles.")
792 (defvar gnus-kill-file-mark ?X
793   "*Mark used for articles killed by kill files.")
794 (defvar gnus-low-score-mark ?Y
795   "*Mark used for articles with a low score.")
796 (defvar gnus-catchup-mark ?C
797   "*Mark used for articles that are caught up.")
798 (defvar gnus-replied-mark ?R
799   "*Mark used for articles that have been replied to.")
800 (defvar gnus-process-mark ?# 
801   "*Process mark.")
802 (defvar gnus-ancient-mark ?A
803   "*Mark used for ancient articles.")
804 (defvar gnus-canceled-mark ?G
805   "*Mark used for canceled articles.")
806 (defvar gnus-score-over-mark ?+
807   "*Score mark used for articles with high scores.")
808 (defvar gnus-score-below-mark ?-
809   "*Score mark used for articles with low scores.")
810 (defvar gnus-empty-thread-mark ? 
811   "*There is no thread under the article.")
812 (defvar gnus-not-empty-thread-mark ?=
813   "*There is a thread under the article.")
814 (defvar gnus-dummy-mark ?Z
815   "*This is a dummy article.")
816
817 (defvar gnus-view-pseudo-asynchronously nil
818   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
819
820 (defvar gnus-view-pseudos nil
821   "*If `automatic', pseudo-articles will be viewed automatically.
822 If `not-confirm', pseudos will be viewed automatically, and the user
823 will not be asked to confirm the command.")
824
825 (defvar gnus-view-pseudos-separately t
826   "*If non-nil, one pseudo-article will be created for each file to be viewed.
827 If nil, all files that use the same viewing command will be given as a
828 list of parameters to that command.")
829
830 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
831   "*Format of group lines.
832 It works along the same lines as a normal formatting string,
833 with some simple extensions.
834
835 %M    Only marked articles (character, \"*\" or \" \")
836 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
837 %L    Level of subscribedness (integer)
838 %N    Number of unread articles (integer)
839 %I    Number of dormant articles (integer)
840 %i    Number of ticked and dormant (integer)
841 %T    Number of ticked articles (integer)
842 %R    Number of read articles (integer)
843 %t    Total number of articles (integer)
844 %y    Number of unread, unticked articles (integer)
845 %G    Group name (string)
846 %g    Qualified group name (string)
847 %D    Group description (string)
848 %s    Select method (string)
849 %o    Moderated group (char, \"m\")
850 %p    Process mark (char)
851 %O    Moderated group (string, \"(m)\" or \"\")
852 %n    Select from where (string)
853 %z    A string that look like `<%s:%n>' if a foreign select method is used
854 %u    User defined specifier. The next character in the format string should
855       be a letter.  Gnus will call the function gnus-user-format-function-X,
856       where X is the letter following %u. The function will be passed the
857       current header as argument. The function should return a string, which
858       will be inserted into the buffer just like information from any other
859       group specifier.
860
861 Text between %( and %) will be highlighted with `gnus-mouse-face' when
862 the mouse point move inside the area.  There can only be one such area.
863
864 Note that this format specification is not always respected. For
865 reasons of efficiency, when listing killed groups, this specification
866 is ignored altogether. If the spec is changed considerably, your
867 output may end up looking strange when listing both alive and killed
868 groups.
869
870 If you use %o or %O, reading the active file will be slower and quite
871 a bit of extra memory will be used. %D will also worsen performance.
872 Also note that if you change the format specification to include any
873 of these specs, you must probably re-start Gnus to see them go into
874 effect.") 
875
876 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
877   "*The format specification of the lines in the summary buffer.
878
879 It works along the same lines as a normal formatting string,
880 with some simple extensions.
881
882 %N   Article number, left padded with spaces (string)
883 %S   Subject (string)
884 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
885 %n   Name of the poster (string)
886 %A   Address of the poster (string)
887 %F   Contents of the From: header (string)
888 %x   Contents of the Xref: header (string)
889 %D   Date of the article (string)
890 %d   Date of the article (string) in DD-MMM format
891 %M   Message-id of the article (string)
892 %r   References of the article (string)
893 %c   Number of characters in the article (integer)
894 %L   Number of lines in the article (integer)
895 %I   Indentation based on thread level (a string of spaces)
896 %T   A string with two possible values: 80 spaces if the article
897      is on thread level two or larger and 0 spaces on level one
898 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
899 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
900 %[   Opening bracket (character, \"[\" or \"<\")
901 %]   Closing bracket (character, \"]\" or \">\")
902 %>   Spaces of length thread-level (string)
903 %<   Spaces of length (- 20 thread-level) (string)
904 %i   Article score (number)
905 %z   Article zcore (character)
906 %t   Number of articles under the current thread (number).
907 %e   Whether the thread is empty or not (character).
908 %u   User defined specifier. The next character in the format string should
909      be a letter.  Gnus will call the function gnus-user-format-function-X,
910      where X is the letter following %u. The function will be passed the
911      current header as argument. The function should return a string, which
912      will be inserted into the summary just like information from any other
913      summary specifier.
914
915 Text between %( and %) will be highlighted with `gnus-mouse-face'
916 when the mouse point is placed inside the area.  There can only be one
917 such area.
918
919 The %U (status), %R (replied) and %z (zcore) specs have to be handled
920 with care. For reasons of efficiency, Gnus will compute what column
921 these characters will end up in, and \"hard-code\" that. This means that
922 it is illegal to have these specs after a variable-length spec. Well,
923 you might not be arrested, but your summary buffer will look strange,
924 which is bad enough.
925
926 The smart choice is to have these specs as for to the left as
927 possible. 
928
929 This restriction may disappear in later versions of Gnus.")
930
931 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
932   "*The format specification for the dummy roots in the summary buffer.
933 It works along the same lines as a normal formatting string,
934 with some simple extensions.
935
936 %S  The subject")
937
938 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
939   "*The format specification for the summary mode line.")
940
941 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
942   "*The format specification for the article mode line.")
943
944 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
945   "*The format specification for the group mode line.")
946
947 (defvar gnus-valid-select-methods
948   '(("nntp" post address prompt-address)
949     ("nnspool" post)
950     ("nnvirtual" none virtual prompt-address) 
951     ("nnmbox" mail respool) 
952     ("nnml" mail respool)
953     ("nnmh" mail respool) 
954     ("nndir" none prompt-address address)
955     ("nneething" none prompt-address)
956     ("nndigest" none) 
957     ("nndoc" none prompt-address) 
958     ("nnbabyl" mail respool) 
959     ("nnkiboze" post virtual) 
960     ("nnsoup" post)
961     ("nnfolder" mail respool))
962   "An alist of valid select methods.
963 The first element of each list lists should be a string with the name
964 of the select method. The other elements may be be the category of
965 this method (ie. `post', `mail', `none' or whatever) or other
966 properties that this method has (like being respoolable).
967 If you implement a new select method, all you should have to change is
968 this variable. I think.")
969
970 (defvar gnus-updated-mode-lines '(group article summary)
971   "*List of buffers that should update their mode lines.
972 The list may contain the symbols `group', `article' and `summary'. If
973 the corresponding symbol is present, Gnus will keep that mode line
974 updated with information that may be pertinent. 
975 If this variable is nil, screen refresh may be quicker.")
976
977 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
978 (defvar gnus-mode-non-string-length 21
979   "*Max length of mode-line non-string contents.
980 If this is nil, Gnus will take space as is needed, leaving the rest
981 of the modeline intact.")
982
983 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
984 (defvar gnus-mouse-face 'highlight
985   "*Face used for mouse highlighting in Gnus.
986 No mouse highlights will be done if `gnus-visual' is nil.")
987
988 (defvar gnus-summary-mark-below nil
989   "*Mark all articles with a score below this variable as read.
990 This variable is local to each summary buffer and usually set by the
991 score file.")  
992
993 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
994   "*List of functions used for sorting threads in the summary buffer.
995 By default, threads are sorted by article number.
996
997 Each function takes two threads and return non-nil if the first thread
998 should be sorted before the other.  If you use more than one function,
999 the primary sort function should be the last.
1000
1001 Ready-mady functions include `gnus-thread-sort-by-number',
1002 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1003 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1004 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1005
1006 (defvar gnus-thread-score-function '+
1007   "*Function used for calculating the total score of a thread.
1008
1009 The function is called with the scores of the article and each
1010 subthread and should then return the score of the thread.
1011
1012 Some functions you can use are `+', `max', or `min'.")
1013
1014 (defvar gnus-options-subscribe nil
1015   "*All new groups matching this regexp will be subscribed unconditionally.
1016 Note that this variable deals only with new newsgroups.  This variable
1017 does not affect old newsgroups.")
1018
1019 (defvar gnus-options-not-subscribe nil
1020   "*All new groups matching this regexp will be ignored.
1021 Note that this variable deals only with new newsgroups.  This variable
1022 does not affect old (already subscribed) newsgroups.")
1023
1024 (defvar gnus-auto-expirable-newsgroups nil
1025   "*Groups in which to automatically mark read articles as expirable.
1026 If non-nil, this should be a regexp that should match all groups in
1027 which to perform auto-expiry.  This only makes sense for mail groups.")
1028
1029 (defvar gnus-hidden-properties '(invisible t intangible t)
1030   "Property list to use for hiding text.")
1031
1032 ;; Hooks.
1033
1034 (defvar gnus-group-mode-hook nil
1035   "*A hook for Gnus group mode.")
1036
1037 (defvar gnus-summary-mode-hook nil
1038   "*A hook for Gnus summary mode.
1039 This hook is run before any variables are set in the summary buffer.")
1040
1041 (defvar gnus-article-mode-hook nil
1042   "*A hook for Gnus article mode.")
1043
1044 (defvar gnus-open-server-hook nil
1045   "*A hook called just before opening connection to the news server.")
1046
1047 (defvar gnus-startup-hook nil
1048   "*A hook called at startup.
1049 This hook is called after Gnus is connected to the NNTP server.")
1050
1051 (defvar gnus-get-new-news-hook nil
1052   "*A hook run just before Gnus checks for new news.")
1053
1054 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1055   "*A function that is called to generate the group buffer.
1056 The function is called with three arguments: The first is a number;
1057 all group with a level less or equal to that number should be listed,
1058 if the second is non-nil, empty groups should also be displayed. If
1059 the third is non-nil, it is a number. No groups with a level lower
1060 than this number should be displayed.
1061
1062 The only current function implemented is `gnus-group-prepare-flat'.")
1063
1064 (defvar gnus-group-prepare-hook nil
1065   "*A hook called after the group buffer has been generated.
1066 If you want to modify the group buffer, you can use this hook.")
1067
1068 (defvar gnus-summary-prepare-hook nil
1069   "*A hook called after the summary buffer has been generated.
1070 If you want to modify the summary buffer, you can use this hook.")
1071
1072 (defvar gnus-article-prepare-hook nil
1073   "*A hook called after an article has been prepared in the article buffer.
1074 If you want to run a special decoding program like nkf, use this hook.")
1075
1076 (defvar gnus-article-display-hook nil
1077   "*A hook called after the article is displayed in the article buffer.
1078 The hook is designed to change the contents of the article
1079 buffer. Typical functions that this hook may contain are
1080 `gnus-article-hide-headers' (hide selected headers),
1081 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1082 `gnus-article-hide-signature' (hide signature) and
1083 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1084 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1085 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1086
1087 (defvar gnus-article-x-face-command
1088   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1089   "String or function to be executed to display an X-Face header.
1090 If it is a string, the command will be executed in a sub-shell
1091 asynchronously. The compressed face will be piped to this command.") 
1092
1093 (defvar gnus-article-x-face-too-ugly nil
1094   "Regexp matching posters whose face shouldn't be shown automatically.")
1095
1096 (defvar gnus-select-group-hook nil
1097   "*A hook called when a newsgroup is selected.
1098
1099 If you'd like to simplify subjects like the
1100 `gnus-summary-next-same-subject' command does, you can use the
1101 following hook:
1102
1103  (setq gnus-select-group-hook
1104       (list
1105         (lambda ()
1106           (mapcar (lambda (header)
1107                      (header-set-subject
1108                       header
1109                       (gnus-simplify-subject
1110                        (header-subject header) 're-only)))
1111                   gnus-newsgroup-headers))))")
1112
1113 (defvar gnus-select-article-hook
1114   '(gnus-summary-show-thread)
1115   "*A hook called when an article is selected.
1116 The default hook shows conversation thread subtrees of the selected
1117 article automatically using `gnus-summary-show-thread'.")
1118
1119 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1120   "*A hook called to apply kill files to a group.
1121 This hook is intended to apply a kill file to the selected newsgroup.
1122 The function `gnus-apply-kill-file' is called by default.
1123
1124 Since a general kill file is too heavy to use only for a few
1125 newsgroups, I recommend you to use a lighter hook function. For
1126 example, if you'd like to apply a kill file to articles which contains
1127 a string `rmgroup' in subject in newsgroup `control', you can use the
1128 following hook:
1129
1130 \(setq gnus-apply-kill-hook
1131       (list
1132         (lambda ()
1133           (cond ((string-match \"control\" gnus-newsgroup-name)
1134                  (gnus-kill \"Subject\" \"rmgroup\")
1135                  (gnus-expunge \"X\"))))))")
1136
1137 (defvar gnus-visual-mark-article-hook 
1138   (list 'gnus-highlight-selected-summary)
1139   "*Hook run after selecting an article in the summary buffer.
1140 It is meant to be used for highlighting the article in some way.  It
1141 is not run if `gnus-visual' is nil.")
1142
1143 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1144   "*A hook called after preparing body, but before preparing header headers.
1145 The default hook (`gnus-inews-insert-signature') inserts a signature
1146 file specified by the variable `gnus-signature-file'.")
1147
1148 (defvar gnus-exit-group-hook nil
1149   "*A hook called when exiting (not quitting) summary mode.")
1150
1151 (defvar gnus-suspend-gnus-hook nil
1152   "*A hook called when suspending (not exiting) Gnus.")
1153
1154 (defvar gnus-exit-gnus-hook nil
1155   "*A hook called when exiting Gnus.")
1156
1157 (defvar gnus-save-newsrc-hook nil
1158   "*A hook called when saving the newsrc file.")
1159
1160 (defvar gnus-summary-update-hook 
1161   (list 'gnus-summary-highlight-line)
1162   "*A hook called when a summary line is changed.
1163 The hook will not be called if `gnus-visual' is nil.
1164
1165 The default function `gnus-summary-highlight-line' will
1166 highlight the line according to the `gnus-summary-highlight'
1167 variable.")
1168
1169 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1170   "*A hook called when an article is selected for the first time.
1171 The hook is intended to mark an article as read (or unread)
1172 automatically when it is selected.")
1173
1174 \f
1175 ;; Internal variables
1176
1177 ;; Avoid highlighting in kill files.
1178 (defvar gnus-summary-inhibit-highlight nil)
1179 (defvar gnus-newsgroup-selected-overlay nil)
1180
1181 (defvar gnus-article-mode-map nil)
1182 (defvar caesar-translate-table nil)
1183 (defvar gnus-dribble-buffer nil)
1184 (defvar gnus-headers-retrieved-by nil)
1185 (defvar gnus-article-reply nil)
1186 (defvar gnus-override-method nil)
1187 (defvar gnus-article-check-size nil)
1188
1189 (defvar gnus-current-score-file nil)
1190 (defvar gnus-internal-global-score-files nil)
1191 (defvar gnus-score-file-list nil)
1192
1193
1194 (defvar gnus-current-move-group nil)
1195
1196 (defvar gnus-newsgroup-dependencies nil)
1197 (defvar gnus-newsgroup-threads nil)
1198 (defvar gnus-newsgroup-async nil)
1199 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1200
1201 (defvar gnus-newsgroup-adaptive nil)
1202
1203 (defvar gnus-summary-display-table nil)
1204
1205 (defconst gnus-group-line-format-alist
1206   (list (list ?M 'marked ?c)
1207         (list ?S 'subscribed ?c)
1208         (list ?L 'level ?d)
1209         (list ?N 'number ?s)
1210         (list ?I 'number-of-dormant ?d)
1211         (list ?T 'number-of-ticked ?d)
1212         (list ?R 'number-of-read ?s)
1213         (list ?t 'number-total ?d)
1214         (list ?y 'number-of-unread-unticked ?s)
1215         (list ?i 'number-of-ticked-and-dormant ?d)
1216         (list ?g 'group ?s)
1217         (list ?G 'qualified-group ?s)
1218         (list ?D 'newsgroup-description ?s)
1219         (list ?o 'moderated ?c)
1220         (list ?O 'moderated-string ?s)
1221         (list ?p 'process-marked ?c)
1222         (list ?s 'news-server ?s)
1223         (list ?n 'news-method ?s)
1224         (list ?z 'news-method-string ?s)
1225         (list ?u 'user-defined ?s)))
1226
1227 (defconst gnus-summary-line-format-alist 
1228   (list (list ?N 'number ?d)
1229         (list ?S 'subject ?s)
1230         (list ?s 'subject-or-nil ?s)
1231         (list ?n 'name ?s)
1232         (list ?A 'address ?s)
1233         (list ?F 'from ?s)
1234         (list ?x (macroexpand '(header-xref header)) ?s)
1235         (list ?D (macroexpand '(header-date header)) ?s)
1236         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1237         (list ?M (macroexpand '(header-id header)) ?s)
1238         (list ?r (macroexpand '(header-references header)) ?s)
1239         (list ?c '(or (header-chars header) 0) ?d)
1240         (list ?L 'lines ?d)
1241         (list ?I 'indentation ?s)
1242         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1243         (list ?R 'replied ?c)
1244         (list ?\[ 'opening-bracket ?c)
1245         (list ?\] 'closing-bracket ?c)
1246         (list ?\> '(make-string level ? ) ?s)
1247         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1248         (list ?i 'score ?d)
1249         (list ?z 'score-char ?c)
1250         (list ?U 'unread ?c)
1251         (list ?t '(gnus-summary-number-of-articles-in-thread 
1252                    (or (prog1 gnus-tmp-adopt-thread 
1253                          (setq gnus-tmp-adopt-thread nil))
1254                        (if (boundp 'thread) (symbol-value 'thread)
1255                          thread nil)))
1256                    ?d)
1257         (list ?e '(gnus-summary-number-of-articles-in-thread 
1258                    (or gnus-tmp-adopt-thread 
1259                        (if (boundp 'thread) (symbol-value 'thread)
1260                          thread nil)) t)
1261                    ?c)
1262         (list ?u 'user-defined ?s))
1263   "An alist of format specifications that can appear in summary lines,
1264 and what variables they correspond with, along with the type of the
1265 variable (string, integer, character, etc).")
1266
1267 (defconst gnus-summary-dummy-line-format-alist
1268   (list (list ?S 'subject ?s)
1269         (list ?N 'number ?d)
1270         (list ?u 'user-defined ?s)))
1271
1272 (defconst gnus-summary-mode-line-format-alist 
1273   (list (list ?G 'group-name ?s)
1274         (list ?g '(gnus-short-group-name group-name) ?s)
1275         (list ?A 'article-number ?d)
1276         (list ?Z 'unread-and-unselected ?s)
1277         (list ?V 'gnus-version ?s)
1278         (list ?U 'unread ?d)
1279         (list ?S 'subject ?s)
1280         (list ?e 'unselected ?d)
1281         (list ?u 'user-defined ?s)
1282         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1283
1284 (defconst gnus-group-mode-line-format-alist 
1285   (list (list ?S 'news-server ?s)
1286         (list ?M 'news-method ?s)
1287         (list ?u 'user-defined ?s)))
1288
1289 (defvar gnus-have-read-active-file nil)
1290
1291 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls & Boys)"
1292   "The mail address of the Gnus maintainers.")
1293
1294 (defconst gnus-version "(ding) Gnus v0.88"
1295   "Version number for this version of Gnus.")
1296
1297 (defvar gnus-info-nodes
1298   '((gnus-group-mode            "(gnus)The Group Buffer")
1299     (gnus-summary-mode          "(gnus)The Summary Buffer")
1300     (gnus-article-mode          "(gnus)The Article Buffer"))
1301   "Assoc list of major modes and related Info nodes.")
1302
1303 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1304   "The location of the (ding) Gnus documentation group.")
1305
1306 (defvar gnus-group-buffer "*Group*")
1307 (defvar gnus-summary-buffer "*Summary*")
1308 (defvar gnus-article-buffer "*Article*")
1309 (defvar gnus-server-buffer "*Server*")
1310
1311 (defvar gnus-work-buffer " *gnus work*")
1312
1313 (defvar gnus-buffer-list nil
1314   "Gnus buffers that should be killed on exit.")
1315
1316 (defvar gnus-server-alist nil
1317   "List of available servers.")
1318
1319 (defvar gnus-variable-list
1320   '(gnus-newsrc-options gnus-newsrc-options-n
1321     gnus-newsrc-last-checked-date 
1322     gnus-newsrc-alist gnus-server-alist
1323     gnus-killed-list gnus-zombie-list)
1324   "Gnus variables saved in the quick startup file.")
1325
1326 (defvar gnus-overload-functions
1327   '((news-inews gnus-inews-news "rnewspost")
1328     (caesar-region gnus-caesar-region "rnews"))
1329   "Functions overloaded by gnus.
1330 It is a list of `(original overload &optional file)'.")
1331
1332 (defvar gnus-newsrc-options nil
1333   "Options line in the .newsrc file.")
1334
1335 (defvar gnus-newsrc-options-n nil
1336   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1337
1338 (defvar gnus-newsrc-last-checked-date nil
1339   "Date Gnus last asked server for new newsgroups.")
1340
1341 (defvar gnus-newsrc-alist nil
1342   "Assoc list of read articles.
1343 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1344
1345 (defvar gnus-newsrc-hashtb nil
1346   "Hashtable of gnus-newsrc-alist.")
1347
1348 (defvar gnus-killed-list nil
1349   "List of killed newsgroups.")
1350
1351 (defvar gnus-killed-hashtb nil
1352   "Hash table equivalent of gnus-killed-list.")
1353
1354 (defvar gnus-zombie-list nil
1355   "List of almost dead newsgroups.")
1356
1357 (defvar gnus-description-hashtb nil
1358   "Descriptions of newsgroups.")
1359
1360 (defvar gnus-list-of-killed-groups nil
1361   "List of newsgroups that have recently been killed by the user.")
1362
1363 (defvar gnus-active-hashtb nil
1364   "Hashtable of active articles.")
1365
1366 (defvar gnus-moderated-list nil
1367   "List of moderated newsgroups.")
1368
1369 (defvar gnus-group-marked nil)
1370
1371 (defvar gnus-current-startup-file nil
1372   "Startup file for the current host.")
1373
1374 (defvar gnus-last-search-regexp nil
1375   "Default regexp for article search command.")
1376
1377 (defvar gnus-last-shell-command nil
1378   "Default shell command on article.")
1379
1380 (defvar gnus-current-select-method nil
1381   "The current method for selecting a newsgroup.")
1382
1383 (defvar gnus-have-all-newsgroups nil)
1384
1385 (defvar gnus-article-internal-prepare-hook nil)
1386
1387 (defvar gnus-newsgroup-name nil)
1388 (defvar gnus-newsgroup-begin nil)
1389 (defvar gnus-newsgroup-end nil)
1390 (defvar gnus-newsgroup-last-rmail nil)
1391 (defvar gnus-newsgroup-last-mail nil)
1392 (defvar gnus-newsgroup-last-folder nil)
1393 (defvar gnus-newsgroup-last-file nil)
1394 (defvar gnus-newsgroup-auto-expire nil)
1395 (defvar gnus-newsgroup-active nil)
1396
1397 (defvar gnus-newsgroup-unreads nil
1398   "List of unread articles in the current newsgroup.")
1399
1400 (defvar gnus-newsgroup-unselected nil
1401   "List of unselected unread articles in the current newsgroup.")
1402
1403 (defvar gnus-newsgroup-marked nil
1404   "List of ticked articles in the current newsgroup (a subset of unread art).")
1405
1406 (defvar gnus-newsgroup-killed nil
1407   "List of ranges of articles that have been through the scoring process.")
1408
1409 (defvar gnus-newsgroup-kill-headers nil)
1410
1411 (defvar gnus-newsgroup-replied nil
1412   "List of articles that have been replied to in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-expirable nil
1415   "List of articles in the current newsgroup that can be expired.")
1416
1417 (defvar gnus-newsgroup-processable nil
1418   "List of articles in the current newsgroup that can be processed.")
1419
1420 (defvar gnus-newsgroup-bookmarks nil
1421   "List of articles in the current newsgroup that have bookmarks.")
1422
1423 (defvar gnus-newsgroup-dormant nil
1424   "List of dormant articles in the current newsgroup.")
1425
1426 (defvar gnus-newsgroup-scored nil
1427   "List of scored articles in the current newsgroup.")
1428
1429 (defvar gnus-newsgroup-headers nil
1430   "List of article headers in the current newsgroup.")
1431 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1432
1433 (defvar gnus-newsgroup-ancient nil
1434   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1435
1436 (defvar gnus-current-article nil)
1437 (defvar gnus-article-current nil)
1438 (defvar gnus-current-headers nil)
1439 (defvar gnus-have-all-headers nil)
1440 (defvar gnus-last-article nil)
1441 (defvar gnus-newsgroup-history nil)
1442 (defvar gnus-current-kill-article nil)
1443
1444 ;; Save window configuration.
1445 (defvar gnus-prev-winconf nil)
1446
1447 ;; Format specs
1448 (defvar gnus-summary-line-format-spec nil)
1449 (defvar gnus-summary-dummy-line-format-spec nil)
1450 (defvar gnus-group-line-format-spec nil)
1451 (defvar gnus-summary-mode-line-format-spec nil)
1452 (defvar gnus-article-mode-line-format-spec nil)
1453 (defvar gnus-group-mode-line-format-spec nil)
1454 (defvar gnus-summary-mark-positions nil)
1455
1456 (defvar gnus-summary-expunge-below nil)
1457 (defvar gnus-reffed-article-number nil)
1458
1459 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1460 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1461
1462 (defconst gnus-summary-local-variables 
1463   '(gnus-newsgroup-name 
1464     gnus-newsgroup-begin gnus-newsgroup-end 
1465     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1466     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1467     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1468     gnus-newsgroup-unselected gnus-newsgroup-marked
1469     gnus-newsgroup-replied gnus-newsgroup-expirable
1470     gnus-newsgroup-processable gnus-newsgroup-killed
1471     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1472     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1473     gnus-current-article gnus-current-headers gnus-have-all-headers
1474     gnus-last-article gnus-article-internal-prepare-hook
1475     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1476     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1477     gnus-newsgroup-threads gnus-newsgroup-async
1478     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1479     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1480     gnus-newsgroup-history gnus-newsgroup-ancient
1481     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring))
1482   "Variables that are buffer-local to the summary buffers.")
1483
1484 (defconst gnus-bug-message
1485   "Sending a bug report to the Gnus Towers.
1486 ========================================
1487
1488 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1489 be sent to the Gnus Bug Exterminators. 
1490
1491 At the bottom of the buffer you'll see lots of variable settings.
1492 Please do not delete those.  They will tell the Bug People what your
1493 environment is, so that it will be easier to locate the bugs.
1494
1495 If you have found a bug that makes Emacs go \"beep\", set
1496 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1497 and include the backtrace in your bug report.
1498
1499 Please describe the bug in annoying, painstaking detail.
1500
1501 Thank you for your help in stamping out bugs.
1502 ")
1503
1504 ;;; End of variables.
1505
1506 ;; Define some autoload functions Gnus might use.
1507 (eval-and-compile
1508
1509   ;; Various 
1510   (autoload 'metamail-buffer "metamail")
1511   (autoload 'Info-goto-node "info")
1512   (autoload 'hexl-hex-string-to-integer "hexl")
1513   (autoload 'pp "pp")
1514   (autoload 'pp-to-string "pp")
1515   (autoload 'pp-eval-expression "pp")
1516   (autoload 'mail-extract-address-components "mail-extr")
1517
1518   (autoload 'nnmail-split-fancy "nnmail")
1519   (autoload 'nnvirtual-catchup-group "nnvirtual")
1520
1521   ;; timezone
1522   (autoload 'timezone-make-date-arpa-standard "timezone")
1523   (autoload 'timezone-fix-time "timezone")
1524   (autoload 'timezone-make-sortable-date "timezone")
1525   (autoload 'timezone-make-time-string "timezone")
1526
1527   ;; rmail & friends
1528   (autoload 'mail-position-on-field "sendmail")
1529   (autoload 'mail-setup "sendmail")
1530   (autoload 'rmail-output "rmailout")
1531   (autoload 'news-mail-other-window "rnewspost")
1532   (autoload 'news-reply-yank-original "rnewspost")
1533   (autoload 'news-caesar-buffer-body "rnewspost")
1534   (autoload 'rmail-insert-rmail-file-header "rmail")
1535   (autoload 'rmail-count-new-messages "rmail")
1536   (autoload 'rmail-show-message "rmail")
1537
1538   ;; gnus-soup
1539   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1540   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1541   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1542   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1543   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1544   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1545   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1546
1547   ;; gnus-mh
1548   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1549   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1550   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1551   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1552   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1553   (autoload 'gnus-Folder-save-name "gnus-mh")
1554   (autoload 'gnus-folder-save-name "gnus-mh")
1555
1556   ;; gnus-vis misc
1557   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1558   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1559   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1560   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1561   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1562   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1563   (autoload 'gnus-summary-highlight-line "gnus-vis")
1564   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1565
1566   ;; gnus-vis article
1567   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1568   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1569   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1570   (autoload 'gnus-article-hide "gnus-vis" nil t)
1571   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1572   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1573   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1574   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1575   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1576   (autoload 'gnus-article-add-button "gnus-vis")
1577
1578   ;; gnus-cite
1579   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1580   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1581   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1582
1583   ;; gnus-kill
1584   (autoload 'gnus-kill "gnus-kill")
1585   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1586   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1587   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1588   (autoload 'gnus-execute "gnus-kill")
1589   (autoload 'gnus-expunge "gnus-kill")
1590
1591   ;; gnus-cache
1592   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1593   (autoload 'gnus-cache-save-buffers "gnus-cache")
1594   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1595   (autoload 'gnus-cache-request-article "gnus-cache")
1596   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1597   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1598   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1599   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1600
1601   ;; gnus-score
1602   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1603   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1604   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1605   (autoload 'gnus-score-save "gnus-score")
1606   (autoload 'gnus-score-headers "gnus-score")
1607   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1608   (autoload 'gnus-score-adaptive "gnus-score")
1609   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1610   (autoload 'gnus-score-find-trace "gnus-score")
1611
1612   ;; gnus-edit
1613   (autoload 'gnus-score-customize "gnus-edit" nil t)
1614
1615   ;; gnus-uu
1616   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1617   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1618   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1619   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1620   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1621   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1622   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1623   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1624   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1625   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1626   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1627   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1628   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1629   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1630   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1631   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1632   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1633   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1634   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1635   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1636   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1637   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1638
1639   ;; gnus-msg
1640   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1641   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1642   (autoload 'gnus-group-mail "gnus-msg" nil t)
1643   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1644   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1645   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1646   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1647   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1648   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1649   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1650   (autoload 'gnus-post-news "gnus-msg" nil t)
1651   (autoload 'gnus-inews-news "gnus-msg" nil t)
1652   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1653   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1654   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1655   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1656   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1657   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1658   (autoload 'gnus-mail-yank-original "gnus-msg")
1659   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1660   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1661   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1662   (autoload 'gnus-article-mail-with-original "gnus-msg")
1663   (autoload 'gnus-article-mail "gnus-msg")
1664
1665   ;; gnus-vm
1666   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1667   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1668   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1669   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1670   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1671   (autoload 'gnus-yank-article "gnus-vm" nil t)
1672
1673   )
1674
1675 \f
1676
1677 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1678 ;; If you want the cursor to go somewhere else, set these two
1679 ;; functions in some startup hook to whatever you want.
1680 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1681 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1682
1683 ;;; Various macros and substs.
1684
1685 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1686   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1687   (` (let ((GnusStartBufferWindow (selected-window)))
1688        (unwind-protect
1689            (progn
1690              (pop-to-buffer (, buffer))
1691              (,@ forms))
1692          (select-window GnusStartBufferWindow)))))
1693
1694 (defmacro gnus-gethash (string hashtable)
1695   "Get hash value of STRING in HASHTABLE."
1696   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1697   ;;(` (abbrev-expansion (, string) (, hashtable)))
1698   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1699
1700 (defmacro gnus-sethash (string value hashtable)
1701   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1702   ;; We cannot use define-abbrev since it only accepts string as value.
1703   ;; (set (intern string hashtable) value))
1704   (` (set (intern (, string) (, hashtable)) (, value))))
1705
1706 (defsubst gnus-buffer-substring (beg end)
1707   (buffer-substring (match-beginning beg) (match-end end)))
1708
1709 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1710 ;;   function `substring' might cut on a middle of multi-octet
1711 ;;   character.
1712
1713 (defun gnus-truncate-string (str width)
1714   (substring str width))
1715
1716 (defsubst gnus-simplify-subject-re (subject)
1717   "Remove \"Re:\" from subject lines."
1718   (let ((case-fold-search t))
1719     (if (string-match "^re: *" subject)
1720         (substring subject (match-end 0))
1721       subject)))
1722
1723 (defsubst gnus-goto-char (point)
1724   (and point (goto-char point)))
1725
1726 (defmacro gnus-buffer-exists-p (buffer)
1727   (` (and (, buffer)
1728           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1729                    (, buffer)))))
1730
1731 (defmacro gnus-kill-buffer (buffer)
1732   (` (if (gnus-buffer-exists-p (, buffer))
1733          (kill-buffer (, buffer)))))
1734
1735 (defsubst gnus-point-at-bol ()
1736   "Return point at the beginning of line."
1737   (let ((p (point)))
1738     (beginning-of-line)
1739     (prog1
1740         (point)
1741       (goto-char p))))
1742
1743 (defsubst gnus-point-at-eol ()
1744   "Return point at the beginning of line."
1745   (let ((p (point)))
1746     (end-of-line)
1747     (prog1
1748         (point)
1749       (goto-char p))))
1750
1751 ;; Delete the current line (and the next N lines.);
1752 (defmacro gnus-delete-line (&optional n)
1753   (` (delete-region (progn (beginning-of-line) (point))
1754                     (progn (forward-line (, (or n 1))) (point)))))
1755
1756 ;;; Load the compatability functions. 
1757
1758 (require 'gnus-ems)
1759
1760 \f
1761 ;;;
1762 ;;; Gnus Utility Functions
1763 ;;;
1764
1765 (defun gnus-extract-address-components (from)
1766   (let (name address)
1767     ;; First find the address - the thing with the @ in it.  This may
1768     ;; not be accurate in mail addresses, but does the trick most of
1769     ;; the time in news messages.
1770     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1771         (setq address (substring from (match-beginning 0) (match-end 0))))
1772     ;; Then we check whether the "name <address>" format is used.
1773     (and address
1774          (string-match (concat "<" (regexp-quote address) ">") from)
1775          (and (setq name (substring from 0 (1- (match-beginning 0))))
1776               ;; Strip any quotes from the name.
1777               (string-match "\".*\"" name)
1778               (setq name (substring name 1 (1- (match-end 0))))))
1779     ;; If not, then "address (name)" is used.
1780     (or name
1781         (and (string-match "(.+)" from)
1782              (setq name (substring from (1+ (match-beginning 0)) 
1783                                    (1- (match-end 0)))))
1784         (and (string-match "()" from)
1785              (setq name address))
1786         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1787         ;; XOVER might not support folded From headers.
1788         (and (string-match "(.*" from)
1789              (setq name (substring from (1+ (match-beginning 0)) 
1790                                    (match-end 0)))))
1791     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1792     (list (or name from) (or address from))))
1793
1794 (defun gnus-fetch-field (field)
1795   "Return the value of the header FIELD of current article."
1796   (save-excursion
1797     (save-restriction
1798       (let ((case-fold-search t))
1799         (gnus-narrow-to-headers)
1800         (mail-fetch-field field)))))
1801
1802 (defun gnus-goto-colon ()
1803   (beginning-of-line)
1804   (search-forward ":" (gnus-point-at-eol) t))
1805
1806 (defun gnus-narrow-to-headers ()
1807   (widen)
1808   (save-excursion
1809     (narrow-to-region
1810      (goto-char (point-min))
1811      (if (search-forward "\n\n" nil t)
1812          (1- (point))
1813        (point-max)))))
1814
1815 (defun gnus-update-format-specifications ()
1816   (gnus-make-thread-indent-array)
1817   (setq gnus-summary-line-format-spec 
1818         (gnus-parse-format
1819          gnus-summary-line-format gnus-summary-line-format-alist))
1820   (gnus-update-summary-mark-positions)
1821   (setq gnus-summary-dummy-line-format-spec 
1822         (gnus-parse-format gnus-summary-dummy-line-format 
1823                            gnus-summary-dummy-line-format-alist))
1824   (setq gnus-group-line-format-spec
1825         (gnus-parse-format 
1826          gnus-group-line-format 
1827          gnus-group-line-format-alist))
1828   (if (and (string-match "%D" gnus-group-line-format)
1829            (not gnus-description-hashtb)
1830            gnus-read-active-file)
1831       (gnus-read-all-descriptions-files))
1832   (setq gnus-summary-mode-line-format-spec 
1833         (gnus-parse-format gnus-summary-mode-line-format 
1834                            gnus-summary-mode-line-format-alist))
1835   (setq gnus-article-mode-line-format-spec 
1836         (gnus-parse-format gnus-article-mode-line-format 
1837                            gnus-summary-mode-line-format-alist))
1838   (setq gnus-group-mode-line-format-spec 
1839         (gnus-parse-format gnus-group-mode-line-format 
1840                            gnus-group-mode-line-format-alist)))
1841
1842 (defun gnus-update-summary-mark-positions ()
1843   (save-excursion
1844     (let ((gnus-replied-mark 129)
1845           (gnus-score-below-mark 130)
1846           (gnus-score-over-mark 130)
1847           (thread nil)
1848           pos)
1849       (gnus-set-work-buffer)
1850       (gnus-summary-insert-line 
1851        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1852       (goto-char (point-min))
1853       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1854                                          (- (point) 2)))))
1855       (goto-char (point-min))
1856       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1857                                           (- (point) 2))) pos))
1858       (goto-char (point-min))
1859       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1860                                         (- (point) 2))) pos))
1861       (setq gnus-summary-mark-positions pos))))
1862
1863 (defun gnus-format-max-width (form length)
1864   (let* ((val (eval form))
1865          (valstr (if (numberp val) (int-to-string val) val)))
1866     (if (> (length valstr) length)
1867         (substring valstr 0 length)
1868       valstr)))
1869
1870 (defun gnus-set-mouse-face (string)
1871   ;; Set mouse face property on STRING.
1872   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1873   string)
1874
1875 (defun gnus-parse-format (format spec-alist)
1876   ;; This function parses the FORMAT string with the help of the
1877   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1878   ;; string.  If the FORMAT string contains the specifiers %( and %)
1879   ;; the text between them will have the mouse-face text property.
1880   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1881       (if (and gnus-visual gnus-mouse-face)
1882           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1883                 (button (substring format (match-beginning 2) (match-end 2)))
1884                 (post (substring format (match-beginning 3) (match-end 3))))
1885             (list 'concat
1886                   (gnus-parse-simple-format pre spec-alist)
1887                   (list 'gnus-set-mouse-face
1888                         (gnus-parse-simple-format button spec-alist))
1889                   (gnus-parse-simple-format post spec-alist)))
1890         (gnus-parse-simple-format
1891          (concat (substring format (match-beginning 1) (match-end 1))
1892                  (substring format (match-beginning 2) (match-end 2))
1893                  (substring format (match-beginning 3) (match-end 3)))
1894          spec-alist))
1895     (gnus-parse-simple-format format spec-alist)))
1896
1897 (defun gnus-parse-simple-format (format spec-alist)
1898   ;; This function parses the FORMAT string with the help of the
1899   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1900   ;; string. The list will consist of the symbol `format', a format
1901   ;; specification string, and a list of forms depending on the
1902   ;; SPEC-ALIST.
1903   (let ((max-width 0)
1904         spec flist fstring b newspec max-width elem beg)
1905     (save-excursion
1906       (gnus-set-work-buffer)
1907       (insert format)
1908       (goto-char (point-min))
1909       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1910         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1911                                                      (match-end 2))))
1912         ;; First check if there are any specs that look anything like
1913         ;; "%12,12A", ie. with a "max width specification". These have
1914         ;; to be treated specially.
1915         (if (setq beg (match-beginning 1))
1916             (setq max-width 
1917                   (string-to-int 
1918                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1919           (setq max-width 0)
1920           (setq beg (match-beginning 2)))
1921         ;; Find the specification from `spec-alist'.
1922         (if (not (setq elem (cdr (assq spec spec-alist))))
1923             (setq elem '("*" ?s)))
1924         ;; Treat user defined format specifiers specially
1925         (and (eq (car elem) 'user-defined)
1926              (setq elem
1927                    (list 
1928                     (list (intern (concat "gnus-user-format-function-"
1929                                           (buffer-substring
1930                                            (match-beginning 3)
1931                                            (match-end 3))))
1932                           'header)
1933                     ?s))
1934              (delete-region (match-beginning 3) (match-end 3)))
1935         (if (not (zerop max-width))
1936             (let ((el (car elem)))
1937               (cond ((= (car (cdr elem)) ?c) 
1938                      (setq el (list 'char-to-string el)))
1939                     ((= (car (cdr elem)) ?d)
1940                      (numberp el) (setq el (list 'int-to-string el))))
1941               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1942                                 flist))
1943               (setq newspec ?s))
1944           (setq flist (cons (car elem) flist))
1945           (setq newspec (car (cdr elem))))
1946         ;; Remove the old specification (and possibly a ",12" string).
1947         (delete-region beg (match-end 2))
1948         ;; Insert the new specification.
1949         (goto-char beg)
1950         (insert newspec))
1951       (setq fstring (buffer-substring 1 (point-max))))
1952     (cons 'format (cons fstring (nreverse flist)))))
1953
1954 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1955 (defun gnus-read-init-file ()
1956   (and gnus-init-file
1957        (or (and (file-exists-p gnus-init-file) 
1958                 ;; Don't try to load a directory.
1959                 (not (file-directory-p gnus-init-file)))
1960            (file-exists-p (concat gnus-init-file ".el"))
1961            (file-exists-p (concat gnus-init-file ".elc")))
1962        (load gnus-init-file nil t)))
1963
1964 (defun gnus-set-work-buffer ()
1965   (if (get-buffer gnus-work-buffer)
1966       (progn
1967         (set-buffer gnus-work-buffer)
1968         (erase-buffer))
1969     (set-buffer (get-buffer-create gnus-work-buffer))
1970     (kill-all-local-variables)
1971     (buffer-disable-undo (current-buffer))
1972     (gnus-add-current-to-buffer-list)))
1973
1974 ;; Article file names when saving.
1975
1976 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1977   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1978 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1979 Otherwise, it is like ~/News/news/group/num."
1980   (let ((default
1981           (expand-file-name
1982            (concat (if (gnus-use-long-file-name 'not-save)
1983                        (gnus-capitalize-newsgroup newsgroup)
1984                      (gnus-newsgroup-directory-form newsgroup))
1985                    "/" (int-to-string (header-number headers)))
1986            (or gnus-article-save-directory "~/News"))))
1987     (if (and last-file
1988              (string-equal (file-name-directory default)
1989                            (file-name-directory last-file))
1990              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1991         default
1992       (or last-file default))))
1993
1994 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1995   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1996 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1997 Otherwise, it is like ~/News/news/group/num."
1998   (let ((default
1999           (expand-file-name
2000            (concat (if (gnus-use-long-file-name 'not-save)
2001                        newsgroup
2002                      (gnus-newsgroup-directory-form newsgroup))
2003                    "/" (int-to-string (header-number headers)))
2004            (or gnus-article-save-directory "~/News"))))
2005     (if (and last-file
2006              (string-equal (file-name-directory default)
2007                            (file-name-directory last-file))
2008              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2009         default
2010       (or last-file default))))
2011
2012 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2013   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2014 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2015 Otherwise, it is like ~/News/news/group/news."
2016   (or last-file
2017       (expand-file-name
2018        (if (gnus-use-long-file-name 'not-save)
2019            (gnus-capitalize-newsgroup newsgroup)
2020          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2021        (or gnus-article-save-directory "~/News"))))
2022
2023 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2024   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2025 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2026 Otherwise, it is like ~/News/news/group/news."
2027   (or last-file
2028       (expand-file-name
2029        (if (gnus-use-long-file-name 'not-save)
2030            newsgroup
2031          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2032        (or gnus-article-save-directory "~/News"))))
2033
2034 ;; For subscribing new newsgroup
2035
2036 (defun gnus-subscribe-hierarchical-interactive (groups)
2037   (let ((groups (sort groups 'string<))
2038         prefixes prefix start rest ans group starts)
2039     (while groups
2040       (setq prefixes (list "^"))
2041       (while (and groups prefixes)
2042         (while (not (string-match (car prefixes) (car groups)))
2043           (setq prefixes (cdr prefixes)))
2044         (setq prefix (car prefixes))
2045         (setq start (1- (length prefix)))
2046         (if (and (string-match "[^\\.]\\." (car groups) start)
2047                  (cdr groups)
2048                  (setq prefix 
2049                        (concat "^" (substring (car groups) 0 (match-end 0))))
2050                  (string-match prefix (car (cdr groups))))
2051             (progn
2052               (setq prefixes (cons prefix prefixes))
2053               (message "Descend hierarchy %s? ([y]nsq): " 
2054                        (substring prefix 1 (1- (length prefix))))
2055               (setq ans (read-char))
2056               (cond ((= ans ?n)
2057                      (while (and groups 
2058                                  (string-match prefix 
2059                                                (setq group (car groups))))
2060                        (setq gnus-killed-list 
2061                              (cons group gnus-killed-list))
2062                        (gnus-sethash group group gnus-killed-hashtb)
2063                        (setq groups (cdr groups)))
2064                      (setq starts (cdr starts)))
2065                     ((= ans ?s)
2066                      (while (and groups 
2067                                  (string-match prefix 
2068                                                (setq group (car groups))))
2069                        (gnus-sethash group group gnus-killed-hashtb)
2070                        (gnus-subscribe-alphabetically (car groups))
2071                        (setq groups (cdr groups)))
2072                      (setq starts (cdr starts)))
2073                     ((= ans ?q)
2074                      (while groups
2075                        (setq group (car groups))
2076                        (setq gnus-killed-list (cons group gnus-killed-list))
2077                        (gnus-sethash group group gnus-killed-hashtb)
2078                        (setq groups (cdr groups))))
2079                     (t nil)))
2080           (message "Subscribe %s? ([n]yq)" (car groups))
2081           (setq ans (read-char))
2082           (setq group (car groups))
2083           (cond ((= ans ?y)
2084                  (gnus-subscribe-alphabetically (car groups))
2085                  (gnus-sethash group group gnus-killed-hashtb))
2086                 ((= ans ?q)
2087                  (while groups
2088                    (setq group (car groups))
2089                    (setq gnus-killed-list (cons group gnus-killed-list))
2090                    (gnus-sethash group group gnus-killed-hashtb)
2091                    (setq groups (cdr groups))))
2092                 (t 
2093                  (setq gnus-killed-list (cons group gnus-killed-list))
2094                  (gnus-sethash group group gnus-killed-hashtb)))
2095           (setq groups (cdr groups)))))))
2096
2097 (defun gnus-subscribe-randomly (newsgroup)
2098   "Subscribe new NEWSGROUP by making it the first newsgroup."
2099   (gnus-subscribe-newsgroup newsgroup))
2100
2101 (defun gnus-subscribe-alphabetically (newgroup)
2102   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2103   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2104   (let ((groups (cdr gnus-newsrc-alist))
2105         before)
2106     (while (and (not before) groups)
2107       (if (string< newgroup (car (car groups)))
2108           (setq before (car (car groups)))
2109         (setq groups (cdr groups))))
2110     (gnus-subscribe-newsgroup newgroup before)))
2111
2112 (defun gnus-subscribe-hierarchically (newgroup)
2113   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2114   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2115   (save-excursion
2116     (set-buffer (find-file-noselect gnus-current-startup-file))
2117     (let ((groupkey newgroup)
2118           before)
2119       (while (and (not before) groupkey)
2120         (goto-char (point-min))
2121         (let ((groupkey-re
2122                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2123           (while (and (re-search-forward groupkey-re nil t)
2124                       (progn
2125                         (setq before (buffer-substring
2126                                       (match-beginning 1) (match-end 1)))
2127                         (string< before newgroup)))))
2128         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2129         (setq groupkey
2130               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2131                   (substring groupkey (match-beginning 1) (match-end 1)))))
2132       (gnus-subscribe-newsgroup newgroup before))))
2133
2134 (defun gnus-subscribe-interactively (newsgroup)
2135   "Subscribe new NEWSGROUP interactively.
2136 It is inserted in hierarchical newsgroup order if subscribed. If not,
2137 it is killed."
2138   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2139       (gnus-subscribe-hierarchically newsgroup)
2140     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2141
2142 (defun gnus-subscribe-zombies (newsgroup)
2143   "Make new NEWSGROUP a zombie group."
2144   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2145
2146 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2147   "Subscribe new NEWSGROUP.
2148 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2149 the first newsgroup."
2150   ;; We subscribe the group by changing its level to `subscribed'.
2151   (gnus-group-change-level 
2152    newsgroup gnus-level-default-subscribed
2153    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2154   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2155
2156 ;; For directories
2157
2158 (defun gnus-newsgroup-directory-form (newsgroup)
2159   "Make hierarchical directory name from NEWSGROUP name."
2160   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2161         (len (length newsgroup))
2162         idx)
2163     ;; If this is a foreign group, we don't want to translate the
2164     ;; entire name.  
2165     (if (setq idx (string-match ":" newsgroup))
2166         (aset newsgroup idx ?/)
2167       (setq idx 0))
2168     ;; Replace all occurrences of `.' with `/'.
2169     (while (< idx len)
2170       (if (= (aref newsgroup idx) ?.)
2171           (aset newsgroup idx ?/))
2172       (setq idx (1+ idx)))
2173     newsgroup))
2174
2175 (defun gnus-make-directory (dir)
2176   "Make DIRECTORY recursively."
2177   (let* ((dir (expand-file-name dir default-directory))
2178          dirs)
2179     (if (string-match "/$" dir)
2180         (setq dir (substring dir 0 (match-beginning 0))))
2181     (while (not (file-exists-p dir))
2182       (setq dirs (cons dir dirs))
2183       (string-match "/[^/]+$" dir)
2184       (setq dir (substring dir 0 (match-beginning 0))))
2185     (while dirs
2186       (make-directory (car dirs))
2187       (setq dirs (cdr dirs)))))
2188
2189 (defun gnus-capitalize-newsgroup (newsgroup)
2190   "Capitalize NEWSGROUP name."
2191   (and (not (zerop (length newsgroup)))
2192        (concat (char-to-string (upcase (aref newsgroup 0)))
2193                (substring newsgroup 1))))
2194
2195 ;; Var
2196
2197 (defun gnus-simplify-subject (subject &optional re-only)
2198   "Remove `Re:' and words in parentheses.
2199 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2200   (let ((case-fold-search t))           ;Ignore case.
2201     ;; Remove `Re:' and `Re^N:'.
2202     (if (string-match "^re:[ \t]*" subject)
2203         (setq subject (substring subject (match-end 0))))
2204     ;; Remove words in parentheses from end.
2205     (or re-only
2206         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2207           (setq subject (substring subject 0 (match-beginning 0)))))
2208     ;; Return subject string.
2209     subject))
2210
2211 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2212 ;; all whitespace.
2213 (defun gnus-simplify-subject-fuzzy (subject)
2214   (let ((case-fold-search t))
2215     (save-excursion
2216       (gnus-set-work-buffer)
2217       (insert subject)
2218       (inline (gnus-simplify-buffer-fuzzy))
2219       (buffer-string))))
2220
2221 (defun gnus-simplify-buffer-fuzzy ()
2222   (goto-char (point-min))
2223   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2224   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2225                             nil t)
2226     (replace-match "" t t))
2227   (goto-char (point-min))
2228   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2229     (replace-match "" t t))
2230   (goto-char (point-min))
2231   (while (re-search-forward "[ \t]+" nil t)
2232     (replace-match " " t t))
2233   (goto-char (point-min))
2234   (while (re-search-forward "[ \t]+$" nil t)
2235     (replace-match "" t t))
2236   (goto-char (point-min))
2237   (while (re-search-forward "^[ \t]+" nil t)
2238     (replace-match "" t t))
2239   (if gnus-simplify-subject-fuzzy-regexp
2240       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2241         (replace-match "" t t)))
2242     )
2243
2244 ;; Add the current buffer to the list of buffers to be killed on exit. 
2245 (defun gnus-add-current-to-buffer-list ()
2246   (or (memq (current-buffer) gnus-buffer-list)
2247       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2248
2249 (defun gnus-string> (s1 s2)
2250   (not (or (string< s1 s2)
2251            (string= s1 s2))))
2252
2253 ;; Functions accessing headers.
2254 ;; Functions are more convenient than macros in some cases.
2255
2256 (defun gnus-header-number (header)
2257   (header-number header))
2258
2259 (defun gnus-header-subject (header)
2260   (header-subject header))
2261
2262 (defun gnus-header-from (header)
2263   (header-from header))
2264
2265 (defun gnus-header-xref (header)
2266   (header-xref header))
2267
2268 (defun gnus-header-lines (header)
2269   (header-lines header))
2270
2271 (defun gnus-header-date (header)
2272   (header-date header))
2273
2274 (defun gnus-header-id (header)
2275   (header-id header))
2276
2277 (defun gnus-header-references (header)
2278   (header-references header))
2279
2280 ;;; General various misc type functions.
2281
2282 (defun gnus-clear-system ()
2283   "Clear all variables and buffers."
2284   ;; Clear Gnus variables.
2285   (let ((variables gnus-variable-list))
2286     (while variables
2287       (set (car variables) nil)
2288       (setq variables (cdr variables))))
2289   ;; Clear other internal variables.
2290   (setq gnus-list-of-killed-groups nil
2291         gnus-have-read-active-file nil
2292         gnus-newsrc-alist nil
2293         gnus-newsrc-hashtb nil
2294         gnus-killed-list nil
2295         gnus-zombie-list nil
2296         gnus-killed-hashtb nil
2297         gnus-active-hashtb nil
2298         gnus-moderated-list nil
2299         gnus-description-hashtb nil
2300         gnus-newsgroup-headers nil
2301         gnus-newsgroup-headers-hashtb-by-number nil
2302         gnus-newsgroup-name nil
2303         gnus-server-alist nil
2304         gnus-current-select-method nil)
2305   ;; Reset any score variables.
2306   (and (boundp 'gnus-score-cache)
2307        (set 'gnus-score-cache nil))
2308   (and (boundp 'gnus-internal-global-score-files)
2309        (set 'gnus-internal-global-score-files nil))
2310   ;; Kill the startup file.
2311   (and gnus-current-startup-file
2312        (get-file-buffer gnus-current-startup-file)
2313        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2314   ;; Save any cache buffers.
2315   (and gnus-use-cache (gnus-cache-save-buffers))
2316   ;; Clear the dribble buffer.
2317   (gnus-dribble-clear)
2318   ;; Kill global KILL file buffer.
2319   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2320       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2321   (gnus-kill-buffer nntp-server-buffer)
2322   ;; Kill Gnus buffers.
2323   (while gnus-buffer-list
2324     (gnus-kill-buffer (car gnus-buffer-list))
2325     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2326
2327 (defun gnus-windows-old-to-new (setting)
2328   (if (symbolp setting)
2329       (setq setting 
2330             (cond ((eq setting 'SelectArticle)
2331                    'article)
2332                   ((eq setting 'SelectSubject)
2333                    'summary)
2334                   ((eq setting 'SelectNewsgroup)
2335                    'group)
2336                   (t setting))))
2337   (if (or (listp setting)
2338           (not (and gnus-window-configuration
2339                     (memq setting '(group summary article)))))
2340       setting
2341     (let* ((setting (if (eq setting 'group) 
2342                         (if (assq 'newsgroup gnus-window-configuration)
2343                             'newsgroup
2344                           'newsgroups) setting))
2345            (elem (car (cdr (assq setting gnus-window-configuration))))
2346            (total (apply '+ elem))
2347            (types '(group summary article))
2348            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2349            (i 0)
2350            perc
2351            out)
2352       (while (< i 3)
2353         (or (zerop (nth i elem))
2354             (progn
2355               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2356               (setq out (cons (if (eq pbuf (nth i types))
2357                                   (vector (nth i types) perc 'point)
2358                                 (vector (nth i types) perc))
2359                               out))))
2360         (setq i (1+ i)))
2361       (list (nreverse out)))))
2362            
2363 (defun gnus-add-configuration (conf)
2364   (setq gnus-buffer-configuration 
2365         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2366                          gnus-buffer-configuration))))
2367
2368 (defun gnus-configure-windows (setting)
2369   (setq setting (gnus-windows-old-to-new setting))
2370   (let ((r (if (symbolp setting)
2371                   (cdr (assq setting gnus-buffer-configuration))
2372                 setting))
2373         (in-buf (current-buffer))
2374         rule val window w height hor ohor heights sub jump-buffer
2375         rel total to-buf)
2376     (or r (error "No such setting: %s" setting))
2377
2378     ;; Either remove all windows or just remove all Gnus windows.
2379     (if gnus-use-full-window
2380         (delete-other-windows)
2381       (gnus-remove-some-windows)
2382       (switch-to-buffer nntp-server-buffer))
2383
2384     (while r
2385       (setq hor (car r)
2386             ohor nil)
2387
2388       ;; We have to do the (possible) horizontal splitting before the
2389       ;; vertical. 
2390       (if (and (listp (car hor)) 
2391                (eq (car (car hor)) 'horizontal))
2392           (progn
2393             (split-window 
2394              nil
2395              (if (integerp (nth 1 (car hor)))
2396                  (nth 1 (car hor))
2397                (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2398              t)
2399             (setq hor (cdr hor))))
2400
2401       ;; Go through the rules and eval the elements that are to be
2402       ;; evaled.  
2403       (while hor
2404         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2405             (progn
2406               ;; Expand short buffer name.
2407               (setq w (aref val 0))
2408               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2409                    (progn
2410                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2411                      (aset val 0 w)))
2412               (setq ohor (cons val ohor))))
2413         (setq hor (cdr hor)))
2414       (setq rule (cons (nreverse ohor) rule))
2415       (setq r (cdr r)))
2416     (setq rule (nreverse rule))
2417
2418     ;; We tally the window sizes.
2419     (setq total (window-height))
2420     (while rule
2421       (setq hor (car rule))
2422       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2423           (setq hor (cdr hor)))
2424       (setq sub 0)
2425       (while hor
2426         (setq rel (aref (car hor) 1)
2427               heights (cons
2428                        (cond ((and (floatp rel) (= 1.0 rel))
2429                               'x)
2430                              ((integerp rel)
2431                               rel)
2432                              (t
2433                               (max (floor (* total rel)) 4)))
2434                        heights)
2435               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2436               hor (cdr hor)))
2437       (setq heights (nreverse heights)
2438             hor (car rule))
2439
2440       ;; We then go through these heighs and create windows for them.
2441       (while heights
2442         (setq height (car heights)
2443               heights (cdr heights))
2444         (and (eq height 'x)
2445              (setq height (- total sub)))
2446         (and heights
2447              (split-window nil height))
2448         (setq to-buf (aref (car hor) 0))
2449         (switch-to-buffer 
2450          (cond ((not to-buf)
2451                 in-buf)
2452                ((symbolp to-buf)
2453                 (symbol-value (aref (car hor) 0)))
2454                (t
2455                 (aref (car hor) 0))))
2456         (and (> (length (car hor)) 2)
2457              (eq (aref (car hor) 2) 'point)
2458              (setq jump-buffer (current-buffer)))
2459         (other-window 1)
2460         (setq hor (cdr hor)))
2461       
2462       (setq rule (cdr rule)))
2463
2464     ;; Finally, we pop to the buffer that's supposed to have point. 
2465     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2466
2467     (select-window (get-buffer-window jump-buffer))
2468     (set-buffer jump-buffer)))
2469       
2470 (defun gnus-remove-some-windows ()
2471   (let ((buffers gnus-window-to-buffer)
2472         (first t)
2473         buf)
2474     (while buffers
2475       (setq buf (cdr (car buffers)))
2476       (if (symbolp buf)
2477           (setq buf (and (boundp buf) (symbol-value buf))))
2478       (and buf 
2479            (get-buffer-window buf)
2480            (progn
2481              (set-buffer buf)
2482              (if first
2483                  (progn
2484                    (switch-to-buffer nntp-server-buffer)
2485                    (setq first nil))
2486                (delete-window (get-buffer-window buf)))))
2487       (setq buffers (cdr buffers)))
2488     (set-buffer nntp-server-buffer)))
2489
2490 (defun gnus-version ()
2491   "Version numbers of this version of Gnus."
2492   (interactive)
2493   (let ((methods gnus-valid-select-methods)
2494         (mess gnus-version)
2495         meth)
2496     ;; Go through all the legal select methods and add their version
2497     ;; numbers to the total version string. Only the backends that are
2498     ;; currently in use will have their message numbers taken into
2499     ;; consideration. 
2500     (while methods
2501       (setq meth (intern (concat (car (car methods)) "-version")))
2502       (and (boundp meth)
2503            (stringp (symbol-value meth))
2504            (setq mess (concat mess "; " (symbol-value meth))))
2505       (setq methods (cdr methods)))
2506     (gnus-message 2 mess)))
2507
2508 (defun gnus-info-find-node ()
2509   "Find Info documentation of Gnus."
2510   (interactive)
2511   ;; Enlarge info window if needed.
2512   (let ((mode major-mode))
2513     (gnus-configure-windows 'info)
2514     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2515
2516 (defun gnus-bug ()
2517   "Send a bug report to the Gnus maintainers."
2518   (interactive)
2519   (let ((winconf (current-window-configuration)))
2520     (delete-other-windows)
2521     (switch-to-buffer "*Gnus Bug Help*")
2522     (erase-buffer)
2523     (insert gnus-bug-message)
2524     (goto-char (point-min))
2525     (pop-to-buffer "*Gnus Bug*")
2526     (erase-buffer)
2527     (mail-mode)
2528     (mail-setup gnus-maintainer nil nil nil nil nil)
2529     (make-local-variable 'gnus-prev-winconf)
2530     (setq gnus-prev-winconf winconf)
2531     (use-local-map (copy-keymap mail-mode-map))
2532     (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
2533     (goto-char (point-min))
2534     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2535     (forward-line 1)
2536     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
2537     (let ((b (point)))
2538       (gnus-debug)
2539       (goto-char (- b 3)))
2540     (message "")))
2541
2542 (defun gnus-debug ()
2543   "Attemps to go through the Gnus source file and report what variables have been changed.
2544 The source file has to be in the Emacs load path."
2545   (interactive)
2546   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el"))
2547         file dirs expr olist)
2548     (save-excursion
2549       (set-buffer (get-buffer-create " *gnus bug info*"))
2550       (buffer-disable-undo (current-buffer))
2551       (message "Please wait while we snoop your variables...")
2552       (sit-for 0)
2553       (while files
2554         (erase-buffer)
2555         (setq dirs load-path)
2556         (while dirs
2557           (if (or (not (car dirs))
2558                   (not (stringp (car dirs)))
2559                   (not (file-exists-p 
2560                         (setq file (concat (file-name-as-directory 
2561                                             (car dirs)) (car files))))))
2562               (setq dirs (cdr dirs))
2563             (setq dirs nil)
2564             (insert-file-contents file)
2565             (goto-char (point-min))
2566             (or (re-search-forward "^;;* Internal variables" nil t)
2567                 (error "Malformed sources in file %s" file))
2568             (narrow-to-region (point-min) (point))
2569             (goto-char (point-min))
2570             (while (setq expr (condition-case () 
2571                                   (read (current-buffer)) (error nil)))
2572               (and (eq (car expr) 'defvar)
2573                    (stringp (nth 3 expr))
2574                    (or (not (boundp (nth 1 expr)))
2575                        (not (equal (eval (nth 2 expr))
2576                                    (symbol-value (nth 1 expr)))))
2577                    (setq olist (cons (nth 1 expr) olist))))))
2578         (setq files (cdr files)))
2579       (kill-buffer (current-buffer)))
2580     (insert "------------------- Environment follows -------------------\n\n")
2581     (while olist
2582       (if (boundp (car olist))
2583           (insert "(setq " (symbol-name (car olist)) " '" 
2584                   (prin1-to-string (symbol-value (car olist))) ")\n")
2585         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2586       (setq olist (cdr olist)))
2587     (insert "\n\n")))
2588
2589 (defun gnus-overload-functions (&optional overloads)
2590   "Overload functions specified by optional argument OVERLOADS.
2591 If nothing is specified, use the variable gnus-overload-functions."
2592   (let ((defs nil)
2593         (overloads (or overloads gnus-overload-functions)))
2594     (while overloads
2595       (setq defs (car overloads))
2596       (setq overloads (cdr overloads))
2597       ;; Load file before overloading function if necessary.  Make
2598       ;; sure we cannot use `require' always.
2599       (and (not (fboundp (car defs)))
2600            (car (cdr (cdr defs)))
2601            (load (car (cdr (cdr defs))) nil 'nomessage))
2602       (fset (car defs) (car (cdr defs))))))
2603
2604 (defun gnus-replace-chars-in-string (string from to)
2605   "Replace characters in STRING from FROM to TO."
2606   (let ((string (substring string 0))   ;Copy string.
2607         (len (length string))
2608         (idx 0))
2609     ;; Replace all occurrences of FROM with TO.
2610     (while (< idx len)
2611       (if (= (aref string idx) from)
2612           (aset string idx to))
2613       (setq idx (1+ idx)))
2614     string))
2615
2616 (defun gnus-days-between (date1 date2)
2617   ;; Return the number of days between date1 and date2.
2618   (- (gnus-day-number date1) (gnus-day-number date2)))
2619
2620 (defun gnus-day-number (date)
2621   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2622                      (timezone-parse-date date))))
2623     (timezone-absolute-from-gregorian 
2624      (nth 1 dat) (nth 2 dat) (car dat))))
2625
2626 ;; Returns a floating point number that says how many seconds have
2627 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2628 (defun gnus-seconds-since-epoch (date)
2629   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2630                         (timezone-parse-date date)))
2631          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2632                         (timezone-parse-time
2633                          (aref (timezone-parse-date date) 3))))
2634          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2635                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2636          (tday (- (timezone-absolute-from-gregorian 
2637                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2638                   (timezone-absolute-from-gregorian 
2639                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2640     (+ (nth 2 ttime)
2641        (* (nth 1 ttime) 60)
2642        (* 1.0 (nth 0 ttime) 60 60)
2643        (* 1.0 tday 60 60 24))))
2644
2645 (defun gnus-file-newer-than (file date)
2646   (let ((fdate (nth 5 (file-attributes file))))
2647     (or (> (car fdate) (car date))
2648         (and (= (car fdate) (car date))
2649              (> (nth 1 fdate) (nth 1 date))))))
2650
2651 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2652 ;; the echo area.
2653 (defun gnus-y-or-n-p (prompt)
2654   (prog1
2655       (y-or-n-p prompt)
2656     (message "")))
2657
2658 (defun gnus-yes-or-no-p (prompt)
2659   (prog1
2660       (yes-or-no-p prompt)
2661     (message "")))
2662
2663 ;; Check whether to use long file names.
2664 (defun gnus-use-long-file-name (symbol)
2665   ;; The variable has to be set...
2666   (and gnus-use-long-file-name
2667        ;; If it isn't a list, then we return t.
2668        (or (not (listp gnus-use-long-file-name))
2669            ;; If it is a list, and the list contains `symbol', we
2670            ;; return nil.  
2671            (not (memq symbol gnus-use-long-file-name)))))
2672
2673 ;; I suspect there's a better way, but I haven't taken the time to do
2674 ;; it yet. -erik selberg@cs.washington.edu
2675 (defun gnus-dd-mmm (messy-date)
2676   "Return a string like DD-MMM from a big messy string"
2677   (let ((datevec (timezone-parse-date messy-date)))
2678     (format "%2s-%s"
2679             (or (aref datevec 2) "??")
2680             (capitalize
2681              (or (car 
2682                   (nth (1- (string-to-number (aref datevec 1)))
2683                        timezone-months-assoc))
2684                  "???")))))
2685
2686 ;; Make a hash table (default and minimum size is 255).
2687 ;; Optional argument HASHSIZE specifies the table size.
2688 (defun gnus-make-hashtable (&optional hashsize)
2689   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2690
2691 ;; Make a number that is suitable for hashing; bigger than MIN and one
2692 ;; less than 2^x.
2693 (defun gnus-create-hash-size (min)
2694   (let ((i 1))
2695     (while (< i min)
2696       (setq i (* 2 i)))
2697     (1- i)))
2698
2699 ;; Show message if message has a lower level than `gnus-verbose'. 
2700 ;; Guide-line for numbers:
2701 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2702 ;; for things that take a long time, 7 - not very important messages
2703 ;; on stuff, 9 - messages inside loops.
2704 (defun gnus-message (level &rest args)
2705   (if (<= level gnus-verbose)
2706       (apply 'message args)
2707     ;; We have to do this format thingie here even if the result isn't
2708     ;; shown - the return value has to be the same as the return value
2709     ;; from `message'.
2710     (apply 'format args)))
2711
2712 ;; Generate a unique new group name.
2713 (defun gnus-generate-new-group-name (leaf)
2714   (let ((name leaf)
2715         (num 0))
2716     (while (gnus-gethash name gnus-newsrc-hashtb)
2717       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2718     name))
2719
2720 (defun gnus-find-file-noselect (file &optional force)
2721   "Does vaguely the same as find-file-noselect. No hooks are run."
2722   (let (buf insert)
2723     (if (setq buf (get-file-buffer file))
2724         (setq insert force)
2725       (setq buf (create-file-buffer file))
2726       (setq insert t))
2727     (if (not insert)
2728         buf
2729       (save-excursion
2730         (set-buffer buf)
2731         (erase-buffer)
2732         (and (file-readable-p file)
2733              (insert-file-contents file))
2734         (set-visited-file-name file)
2735         (set-buffer-modified-p nil)
2736         (current-buffer)))))
2737
2738 ;;; List and range functions
2739
2740 (defun gnus-last-element (list)
2741   "Return last element of LIST."
2742   (while (cdr list)
2743     (setq list (cdr list)))
2744   (car list))
2745
2746 (defun gnus-copy-sequence (list)
2747   "Do a complete, total copy of a list."
2748   (if (and (consp list) (not (consp (cdr list))))
2749       (cons (car list) (cdr list))
2750     (mapcar (lambda (elem) (if (consp elem) 
2751                                (if (consp (cdr elem))
2752                                    (gnus-copy-sequence elem)
2753                                  (cons (car elem) (cdr elem)))
2754                              elem))
2755             list)))
2756
2757 (defun gnus-set-difference (list1 list2)
2758   "Return a list of elements of LIST1 that do not appear in LIST2."
2759   (let ((list1 (copy-sequence list1)))
2760     (while list2
2761       (setq list1 (delq (car list2) list1))
2762       (setq list2 (cdr list2)))
2763     list1))
2764
2765 (defun gnus-sorted-complement (list1 list2)
2766   "Return a list of elements of LIST1 that do not appear in LIST2.
2767 Both lists have to be sorted over <."
2768   (let (out)
2769     (if (or (null list1) (null list2))
2770         (or list1 list2)
2771       (while (and list1 list2)
2772         (cond ((= (car list1) (car list2))
2773                (setq list1 (cdr list1)
2774                      list2 (cdr list2)))
2775               ((< (car list1) (car list2))
2776                (setq out (cons (car list1) out))
2777                (setq list1 (cdr list1)))
2778               (t
2779                (setq out (cons (car list2) out))
2780                (setq list2 (cdr list2)))))
2781       (nconc (nreverse out) (or list1 list2)))))
2782
2783 (defun gnus-intersection (list1 list2)      
2784   (let ((result nil))
2785     (while list2
2786       (if (memq (car list2) list1)
2787           (setq result (cons (car list2) result)))
2788       (setq list2 (cdr list2)))
2789     result))
2790
2791 (defun gnus-sorted-intersection (list1 list2)
2792   ;; LIST1 and LIST2 have to be sorted over <.
2793   (let (out)
2794     (while (and list1 list2)
2795       (cond ((= (car list1) (car list2))
2796              (setq out (cons (car list1) out)
2797                    list1 (cdr list1)
2798                    list2 (cdr list2)))
2799             ((< (car list1) (car list2))
2800              (setq list1 (cdr list1)))
2801             (t
2802              (setq list2 (cdr list2)))))
2803     (nreverse out)))
2804
2805 (defun gnus-set-sorted-intersection (list1 list2)
2806   ;; LIST1 and LIST2 have to be sorted over <.
2807   ;; This function modifies LIST1.
2808   (let* ((top (cons nil list1))
2809          (prev top))
2810   (while (and list1 list2)
2811     (cond ((= (car list1) (car list2))
2812            (setq prev list1
2813                  list1 (cdr list1)
2814                  list2 (cdr list2)))
2815           ((< (car list1) (car list2))
2816            (setcdr prev (cdr list1))
2817            (setq list1 (cdr list1)))
2818           (t
2819            (setq list2 (cdr list2)))))
2820   (setcdr prev nil)
2821   (cdr top)))
2822
2823 (defun gnus-compress-sequence (numbers &optional always-list)
2824   "Convert list of numbers to a list of ranges or a single range.
2825 If ALWAYS-LIST is non-nil, this function will always release a list of
2826 ranges."
2827   (let* ((first (car numbers))
2828          (last (car numbers))
2829          result)
2830     (if (null numbers)
2831         nil
2832       (if (not (listp (cdr numbers)))
2833           numbers
2834         (while numbers
2835           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2836                 ((= (1+ last) (car numbers)) ;Still in sequence
2837                  (setq last (car numbers)))
2838                 (t                      ;End of one sequence
2839                  (setq result 
2840                        (cons (if (= first last) first
2841                                (cons first last)) result))
2842                  (setq first (car numbers))
2843                  (setq last  (car numbers))))
2844           (setq numbers (cdr numbers)))
2845         (if (and (not always-list) (null result))
2846             (if (= first last) (list first) (cons first last))
2847           (nreverse (cons (if (= first last) first (cons first last))
2848                           result)))))))
2849
2850 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2851 (defun gnus-uncompress-range (ranges)
2852   "Expand a list of ranges into a list of numbers.
2853 RANGES is either a single range on the form `(num . num)' or a list of
2854 these ranges."
2855   (let (first last result)
2856     (cond 
2857      ((null ranges)
2858       nil)
2859      ((not (listp (cdr ranges)))
2860       (setq first (car ranges))
2861       (setq last (cdr ranges))
2862       (while (<= first last)
2863         (setq result (cons first result))
2864         (setq first (1+ first)))
2865       (nreverse result))
2866      (t
2867       (while ranges
2868         (if (atom (car ranges))
2869             (if (numberp (car ranges))
2870                 (setq result (cons (car ranges) result)))
2871           (setq first (car (car ranges)))
2872           (setq last  (cdr (car ranges)))
2873           (while (<= first last)
2874             (setq result (cons first result))
2875             (setq first (1+ first))))
2876         (setq ranges (cdr ranges)))
2877       (nreverse result)))))
2878
2879 (defun gnus-add-to-range (ranges list)
2880   "Return a list of ranges that has all articles from both RANGES and LIST.
2881 Note: LIST has to be sorted over `<'."
2882   (if (not ranges)
2883       (gnus-compress-sequence list t)
2884     (setq list (copy-sequence list))
2885     (or (listp (cdr ranges))
2886         (setq ranges (list ranges)))
2887     (let ((out ranges)
2888           ilist lowest highest temp)
2889       (while (and ranges list)
2890         (setq ilist list)
2891         (setq lowest (or (and (atom (car ranges)) (car ranges))
2892                          (car (car ranges))))
2893         (while (and list (cdr list) (< (car (cdr list)) lowest))
2894           (setq list (cdr list)))
2895         (if (< (car ilist) lowest)
2896             (progn
2897               (setq temp list)
2898               (setq list (cdr list))
2899               (setcdr temp nil)
2900               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2901         (setq highest (or (and (atom (car ranges)) (car ranges))
2902                           (cdr (car ranges))))
2903         (while (and list (<= (car list) highest))
2904           (setq list (cdr list)))
2905         (setq ranges (cdr ranges)))
2906       (if list
2907           (setq out (nconc (gnus-compress-sequence list t) out)))
2908       (setq out (sort out (lambda (r1 r2) 
2909                             (< (or (and (atom r1) r1) (car r1))
2910                                (or (and (atom r2) r2) (car r2))))))
2911       (setq ranges out)
2912       (while ranges
2913         (if (atom (car ranges))
2914             (if (cdr ranges)
2915                 (if (atom (car (cdr ranges)))
2916                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2917                         (progn
2918                           (setcar ranges (cons (car ranges) 
2919                                                (car (cdr ranges))))
2920                           (setcdr ranges (cdr (cdr ranges)))))
2921                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2922                       (progn
2923                         (setcar (car (cdr ranges)) (car ranges))
2924                         (setcar ranges (car (cdr ranges)))
2925                         (setcdr ranges (cdr (cdr ranges)))))))
2926           (if (cdr ranges)
2927               (if (atom (car (cdr ranges)))
2928                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2929                       (progn
2930                         (setcdr (car ranges) (car (cdr ranges)))
2931                         (setcdr ranges (cdr (cdr ranges)))))
2932                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2933                     (progn
2934                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2935                       (setcdr ranges (cdr (cdr ranges))))))))
2936         (setq ranges (cdr ranges)))
2937       out)))
2938
2939 (defun gnus-remove-from-range (ranges list)
2940   "Return a list of ranges that has all articles from LIST removed from RANGES.
2941 Note: LIST has to be sorted over `<'."
2942   ;; !!! This function shouldn't look like this, but I've got a headache.
2943   (gnus-compress-sequence 
2944    (gnus-sorted-complement
2945     (gnus-uncompress-range ranges) list)))
2946
2947 (defun gnus-member-of-range (number ranges)
2948   (if (not (listp (cdr ranges)))
2949       (and (>= number (car ranges)) 
2950            (<= number (cdr ranges)))
2951     (let ((not-stop t))
2952       (while (and ranges 
2953                   (if (numberp (car ranges))
2954                       (>= number (car ranges))
2955                     (>= number (car (car ranges))))
2956                   not-stop)
2957         (if (if (numberp (car ranges))
2958                 (= number (car ranges))
2959               (and (>= number (car (car ranges)))
2960                    (<= number (cdr (car ranges)))))
2961             (setq not-stop nil))
2962         (setq ranges (cdr ranges)))
2963       (not not-stop))))
2964
2965 \f
2966 ;;;
2967 ;;; Gnus group mode
2968 ;;;
2969
2970 (defvar gnus-group-mode-map nil)
2971 (defvar gnus-group-group-map nil)
2972 (defvar gnus-group-mark-map nil)
2973 (defvar gnus-group-list-map nil)
2974 (defvar gnus-group-sub-map nil)
2975 (put 'gnus-group-mode 'mode-class 'special)
2976
2977 (if gnus-group-mode-map
2978     nil
2979   (setq gnus-group-mode-map (make-keymap))
2980   (suppress-keymap gnus-group-mode-map)
2981   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2982   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2983   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2984   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2985   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2986   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2987   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2988   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2989   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2990   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2991   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2992   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2993   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2994   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2995   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2996   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2997   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2998   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2999   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3000   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3001   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3002   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3003   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3004   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3005   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3006   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3007   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3008   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3009   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3010   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3011   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3012   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3013   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3014   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3015   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3016   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3017   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3018   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3019   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3020   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3021   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3022   (define-key gnus-group-mode-map "V" 'gnus-version)
3023   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3024   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3025   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3026   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3027   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3028   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3029   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3030   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3031   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3032   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3033   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3034   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3035   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3036   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3037   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3038
3039   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3040   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3041   (define-prefix-command 'gnus-group-mark-map)
3042   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3043   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3044   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3045   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3046
3047   (define-prefix-command 'gnus-group-group-map)
3048   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3049   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3050   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3051   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3052   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3053   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3054   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3055   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3056   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3057   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3058   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3059   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3060   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3061   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3062   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3063   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3064   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3065   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3066
3067   (define-prefix-command 'gnus-group-list-map)
3068   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3069   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3070   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3071   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3072   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3073   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3074   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3075   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3076   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3077
3078   (define-prefix-command 'gnus-group-sub-map)
3079   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3080   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3081   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3082   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3083   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3084   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3085   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3086   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3087
3088 (defun gnus-group-mode ()
3089   "Major mode for reading news.
3090
3091 All normal editing commands are switched off.
3092 \\<gnus-group-mode-map>
3093 The group buffer lists (some of) the groups available.  For instance,
3094 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3095 lists all zombie groups. 
3096
3097 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3098 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3099
3100 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3101
3102 The following commands are available:
3103
3104 \\{gnus-group-mode-map}"
3105   (interactive)
3106   (if gnus-visual (gnus-group-make-menu-bar))
3107   (kill-all-local-variables)
3108   (setq mode-line-modified "-- ")
3109   (make-local-variable 'mode-line-format)
3110   (setq mode-line-format (copy-sequence mode-line-format))
3111   (and (equal (nth 3 mode-line-format) "   ")
3112        (setcar (nthcdr 3 mode-line-format) ""))
3113   (setq major-mode 'gnus-group-mode)
3114   (setq mode-name "Group")
3115   (gnus-group-set-mode-line)
3116   (setq mode-line-process nil)
3117   (use-local-map gnus-group-mode-map)
3118   (buffer-disable-undo (current-buffer))
3119   (setq truncate-lines t)
3120   (setq buffer-read-only t)
3121   (run-hooks 'gnus-group-mode-hook))
3122
3123 (defun gnus-mouse-pick-group (e)
3124   (interactive "e")
3125   (mouse-set-point e)
3126   (gnus-group-read-group nil))
3127
3128 ;;;###autoload
3129 (defun gnus-no-server (&optional arg)
3130   "Read network news.
3131 If ARG is a positive number, Gnus will use that as the
3132 startup level. If ARG is nil, Gnus will be started at level 2. 
3133 If ARG is non-nil and not a positive number, Gnus will
3134 prompt the user for the name of an NNTP server to use.
3135 As opposed to `gnus', this command will not connect to the local server."
3136   (interactive "P")
3137   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3138
3139 (defalias '\(ding\) 'gnus)
3140
3141 ;;;###autoload
3142 (defun gnus (&optional arg dont-connect)
3143   "Read network news.
3144 If ARG is non-nil and a positive number, Gnus will use that as the
3145 startup level. If ARG is non-nil and not a positive number, Gnus will
3146 prompt the user for the name of an NNTP server to use."
3147   (interactive "P")
3148   (if (get-buffer gnus-group-buffer)
3149       (progn
3150         (switch-to-buffer gnus-group-buffer)
3151         (gnus-group-get-new-news))
3152     (gnus-clear-system)
3153     (nnheader-init-server-buffer)
3154     (gnus-read-init-file)
3155     (let ((level (and arg (numberp arg) (> arg 0) arg))
3156           did-connect)
3157       (unwind-protect
3158           (progn
3159             (gnus-group-setup-buffer)
3160             (or dont-connect 
3161                 (setq did-connect
3162                       (gnus-start-news-server (and arg (not level))))))
3163         (if (and (not dont-connect) 
3164                  (not did-connect))
3165             (gnus-group-quit)
3166           (run-hooks 'gnus-startup-hook)
3167           ;; NNTP server is successfully open. 
3168           (gnus-update-format-specifications)
3169           (gnus-summary-make-display-table)
3170           (let ((buffer-read-only nil))
3171             (erase-buffer)
3172             (if (not gnus-inhibit-startup-message)
3173                 (progn
3174                   (gnus-group-startup-message)
3175                   (sit-for 0))))
3176           (gnus-setup-news nil level)
3177           (and gnus-use-dribble-file (gnus-dribble-open))
3178           (gnus-group-list-groups level)
3179           (gnus-configure-windows 'group))))))
3180
3181 (defun gnus-group-startup-message (&optional x y)
3182   "Insert startup message in current buffer."
3183   ;; Insert the message.
3184   (erase-buffer)
3185   (insert
3186    (format "
3187     %s
3188            A newsreader 
3189       for GNU Emacs
3190
3191         Based on GNUS 
3192              written by 
3193      Masanobu UMEDA
3194
3195     Lars Magne 
3196          Ingebrigtsen 
3197       larsi@ifi.uio.no
3198
3199            gnus-version))
3200   ;; And then hack it.
3201   ;; 18 is the longest line.
3202   (indent-rigidly (point-min) (point-max) 
3203                   (/ (max (- (window-width) (or x 28)) 0) 2))
3204   (goto-char (point-min))
3205   ;; +4 is fuzzy factor.
3206   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3207
3208 (defun gnus-group-setup-buffer ()
3209   (or (get-buffer gnus-group-buffer)
3210       (progn
3211         (switch-to-buffer gnus-group-buffer)
3212         (gnus-add-current-to-buffer-list)
3213         (gnus-group-mode)
3214         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3215
3216 (defun gnus-group-list-groups (level &optional unread)
3217   "List newsgroups with level LEVEL or lower that have unread articles.
3218 Default is all subscribed groups.
3219 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3220   (interactive (list (and current-prefix-arg
3221                           (prefix-numeric-value current-prefix-arg))))
3222   (if gnus-group-use-permanent-levels
3223       (progn
3224         (setq gnus-group-default-list-level 
3225               (or level gnus-group-default-list-level))
3226         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3227     (setq level (or level gnus-group-default-list-level 
3228                     gnus-level-subscribed)))
3229   (gnus-group-setup-buffer)     ;May call from out of group buffer
3230   (let ((case-fold-search nil)
3231         (group (gnus-group-group-name)))
3232     (funcall gnus-group-prepare-function level unread nil)
3233     (if (zerop (buffer-size))
3234         (gnus-message 5 gnus-no-groups-message)
3235       (goto-char (point-min))
3236       (if (not group)
3237           ;; Go to the first group with unread articles.
3238           (gnus-group-search-forward nil nil nil t)
3239         ;; Find the right group to put point on. If the current group
3240         ;; has disapeared in the new listing, try to find the next
3241         ;; one. If no next one can be found, just leave point at the
3242         ;; first newsgroup in the buffer.
3243         (if (not (gnus-goto-char
3244                   (text-property-any (point-min) (point-max) 
3245                                      'gnus-group (intern group))))
3246             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3247               (while (and newsrc
3248                           (not (gnus-goto-char 
3249                                 (text-property-any 
3250                                  (point-min) (point-max) 'gnus-group 
3251                                  (intern (car (car newsrc)))))))
3252                 (setq newsrc (cdr newsrc)))
3253               (or newsrc (progn (goto-char (point-max))
3254                                 (forward-line -1))))))
3255       ;; Adjust cursor point.
3256       (gnus-group-position-cursor))))
3257
3258 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3259   "List all newsgroups with unread articles of level LEVEL or lower.
3260 If ALL is non-nil, list groups that have no unread articles.
3261 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3262 If REGEXP, only list groups matching REGEXP."
3263   (set-buffer gnus-group-buffer)
3264   (let ((buffer-read-only nil)
3265         (newsrc (cdr gnus-newsrc-alist))
3266         (lowest (or lowest 1))
3267         info clevel unread group)
3268     (erase-buffer)
3269     (if (< lowest gnus-level-zombie)
3270         ;; List living groups.
3271         (while newsrc
3272           (setq info (car newsrc)
3273                 group (car info)
3274                 newsrc (cdr newsrc)
3275                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3276           (and unread ; This group might be bogus
3277                (or (not regexp)
3278                    (string-match regexp group))
3279                (<= (setq clevel (car (cdr info))) level) 
3280                (>= clevel lowest)
3281                (or all            ; We list all groups?
3282                    (eq unread t)  ; We list unactivated groups
3283                    (> unread 0)   ; We list groups with unread articles
3284                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3285                (gnus-group-insert-group-line 
3286                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3287
3288     ;; List dead groups.
3289     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3290          (gnus-group-prepare-flat-list-dead 
3291           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3292           gnus-level-zombie ?Z
3293           regexp))
3294     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3295          (gnus-group-prepare-flat-list-dead 
3296           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3297           gnus-level-killed ?K regexp))
3298
3299     (gnus-group-set-mode-line)
3300     (setq gnus-have-all-newsgroups all)
3301     (run-hooks 'gnus-group-prepare-hook)))
3302
3303 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3304   ;; List zombies and killed lists somehwat faster, which was
3305   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3306   ;; this by ignoring the group format specification altogether.
3307   (let (group beg)
3308     (while groups
3309       (setq group (car groups)
3310             groups (cdr groups))
3311       (if (or (not regexp)
3312               (string-match regexp group))
3313           (progn
3314             (setq beg (point))
3315             (insert (format " %c     *: %s\n" mark group))
3316             (add-text-properties 
3317              beg (1+ beg) 
3318              (list 'gnus-group (intern group)
3319                    'gnus-unread t
3320                    'gnus-level level)))))))
3321
3322 (defun gnus-group-real-name (group)
3323   "Find the real name of a foreign newsgroup."
3324   (if (string-match ":[^:]+$" group)
3325       (substring group (1+ (match-beginning 0)))
3326     group))
3327
3328 (defun gnus-group-prefixed-name (group method)
3329   "Return the whole name from GROUP and METHOD."
3330   (and (stringp method) (setq method (gnus-server-to-method method)))
3331   (concat (format "%s" (car method))
3332           (if (and 
3333                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3334                (not (string= (nth 1 method) "")))
3335               (concat "+" (nth 1 method)))
3336           ":" group))
3337
3338 (defun gnus-group-real-prefix (group)
3339   "Return the prefix of the current group name."
3340   (if (string-match "^[^:]+:" group)
3341       (substring group 0 (match-end 0))
3342     ""))
3343
3344 (defun gnus-group-method-name (group)
3345   "Return the method used for selecting GROUP."
3346   (let ((prefix (gnus-group-real-prefix group)))
3347     (if (equal prefix "")
3348         gnus-select-method
3349       (if (string-match "^[^\\+]+\\+" prefix)
3350           (list (intern (substring prefix 0 (1- (match-end 0))))
3351                 (substring prefix (match-end 0) (1- (length prefix))))
3352         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3353
3354 (defun gnus-group-foreign-p (group)
3355   "Return nil if GROUP is native, non-nil if it is foreign."
3356   (string-match ":" group))
3357
3358 (defun gnus-group-set-info (info &optional method-only-group part)
3359   (let* ((entry (gnus-gethash
3360                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3361          (part-info info)
3362          (info (if method-only-group (nth 2 entry) info)))
3363     (if (not method-only-group)
3364         ()
3365       (or entry
3366           (error "Trying to change non-existent group %s" method-only-group))
3367       ;; We have recevied parts of the actual group info - either the
3368       ;; select method or the group parameters.  We first check
3369       ;; whether we have to extend the info, and if so, do that.
3370       (let ((len (length info))
3371             (total (if (eq part 'method) 5 6)))
3372         (and (< len total)
3373              (setcdr (nthcdr (1- len) info)
3374                      (make-list (- total len) nil)))
3375         ;; Then we enter the new info.
3376         (setcar (nthcdr (1- total) info) part-info)))
3377     ;; We uncompress some lists of marked articles.
3378     (let (marked)
3379       (if (not (setq marked (nth 3 info)))
3380           ()
3381         (while marked
3382           (or (eq 'score (car (car marked)))
3383               (eq 'bookmark (car (car marked)))
3384               (eq 'killed (car (car marked)))
3385               (setcdr (car marked) 
3386                       (gnus-uncompress-range (cdr (car marked)))))
3387           (setq marked (cdr marked)))))
3388     (if entry
3389         ()
3390       ;; This is a new group, so we just create it.
3391       (save-excursion
3392         (set-buffer gnus-group-buffer)
3393         (if (nth 4 info)
3394             ;; It's a foreign group...
3395             (gnus-group-make-group 
3396              (gnus-group-real-name (car info))
3397              (prin1-to-string (car (nth 4 info)))
3398              (nth 1 (nth 4 info)))
3399           ;; It's a native group.
3400           (gnus-group-make-group
3401            (car info)
3402            (prin1-to-string (car gnus-select-method))
3403            (nth 1 gnus-select-method)))
3404         (gnus-message 6 "Note: New group created")
3405         (setq entry 
3406               (gnus-gethash (gnus-group-prefixed-name 
3407                              (gnus-group-real-name (car info))
3408                              (or (nth 4 info) gnus-select-method))
3409                             gnus-newsrc-hashtb))))
3410     ;; Whether it was a new group or not, we now have the entry, so we
3411     ;; can do the update.
3412     (if entry
3413         (progn
3414           (setcar (nthcdr 2 entry) info)
3415           (if (and (not (eq (car entry) t)) 
3416                    (gnus-gethash (car info) gnus-active-hashtb))
3417               (let ((marked (nth 3 info)))
3418                 (setcar entry 
3419                         (max 0 (- (length (gnus-list-of-unread-articles 
3420                                            (car info)))
3421                                   (length (cdr (assq 'tick marked)))
3422                                   (length (cdr (assq 'dormant marked)))))))))
3423       (error "No such group: %s" (car info)))))
3424
3425 (defun gnus-group-set-method-info (group select-method)
3426   (gnus-group-set-info select-method group 'method))
3427
3428 (defun gnus-group-set-params-info (group params)
3429   (gnus-group-set-info params group 'params))
3430
3431 (defun gnus-group-update-group-line ()
3432   "This function updates the current line in the newsgroup buffer and
3433 moves the point to the colon."
3434   (let* ((buffer-read-only nil)
3435          (group (gnus-group-group-name))
3436          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3437     (if entry
3438         (gnus-dribble-enter 
3439          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3440                  ")")))
3441     (beginning-of-line)
3442     (delete-region (point) (progn (forward-line 1) (point)))
3443     (gnus-group-insert-group-line-info group)
3444     (forward-line -1)
3445     (gnus-group-position-cursor)))
3446
3447 (defun gnus-group-insert-group-line-info (group)
3448   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3449         active info)
3450     (if entry
3451         (progn
3452           (setq info (nth 2 entry))
3453           (gnus-group-insert-group-line 
3454            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3455       (setq active (gnus-gethash group gnus-active-hashtb))
3456       (gnus-group-insert-group-line 
3457        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3458                    gnus-level-killed)
3459        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3460
3461 (defun gnus-group-insert-group-line (gformat group level marked number method)
3462   (let* ((gformat (or gformat gnus-group-line-format-spec))
3463          (active (gnus-gethash group gnus-active-hashtb))
3464          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3465          (number-of-dormant (length (cdr (assq 'dormant marked))))
3466          (number-of-ticked (length (cdr (assq 'tick marked))))
3467          (number-of-ticked-and-dormant
3468           (+ number-of-ticked number-of-dormant))
3469          (number-of-unread-unticked 
3470           (if (numberp number) (int-to-string (max 0 number))
3471             "*"))
3472          (number-of-read
3473           (if (numberp number)
3474               (max 0 (- number-total number))
3475             "*"))
3476          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3477                            ((<= level gnus-level-unsubscribed) ?U)
3478                            ((= level gnus-level-zombie) ?Z)
3479                            (t ?K)))
3480          (qualified-group (gnus-group-real-name group))
3481          (newsgroup-description 
3482           (if gnus-description-hashtb
3483               (or (gnus-gethash group gnus-description-hashtb) "")
3484             ""))
3485          (moderated (if (member group gnus-moderated-list) ?m ? ))
3486          (moderated-string (if (eq moderated ?m) "(m)" ""))
3487          (method (gnus-server-get-method group method))
3488          (news-server (or (car (cdr method)) ""))
3489          (news-method (or (car method) ""))
3490          (news-method-string 
3491           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3492          (marked (if (and 
3493                       (numberp number) 
3494                       (zerop number)
3495                       (> number-of-ticked 0))
3496                      ?* ? ))
3497          (number (if (eq number t) "*" (+ number number-of-dormant 
3498                                           number-of-ticked)))
3499          (process-marked (if (member qualified-group gnus-group-marked)
3500                              gnus-process-mark ? ))
3501          (buffer-read-only nil)
3502          b)
3503     (beginning-of-line)
3504     (setq b (point))
3505     ;; Insert the text.
3506     (insert (eval gformat))
3507
3508     (add-text-properties 
3509      b (1+ b) (list 'gnus-group (intern group)
3510                     'gnus-unread (if (numberp number)
3511                                      (string-to-int number-of-unread-unticked)
3512                                    t)
3513                     'gnus-marked marked
3514                     'gnus-level level))))
3515
3516 (defun gnus-group-update-group (group &optional visible-only)
3517   "Update newsgroup info of GROUP.
3518 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3519   (save-excursion
3520     (set-buffer gnus-group-buffer)
3521     (let ((buffer-read-only nil)
3522           visible)
3523       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3524         (if entry
3525             (gnus-dribble-enter 
3526              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3527                      ")"))))
3528       ;; Buffer may be narrowed.
3529       (save-restriction
3530         (widen)
3531         ;; Search a line to modify.  If the buffer is large, the search
3532         ;; takes long time.  In most cases, current point is on the line
3533         ;; we are looking for.  So, first of all, check current line. 
3534         (if (or (progn
3535                   (beginning-of-line)
3536                   (eq (get-text-property (point) 'gnus-group)
3537                       (intern group)))
3538                 (progn
3539                   (gnus-goto-char 
3540                    (text-property-any 
3541                     (point-min) (point-max) 'gnus-group (intern group)))))
3542             ;; GROUP is listed in current buffer. So, delete old line.
3543             (progn
3544               (setq visible t)
3545               (beginning-of-line)
3546               (delete-region (point) (progn (forward-line 1) (point))))
3547           ;; No such line in the buffer, find out where it's supposed to
3548           ;; go, and insert it there (or at the end of the buffer).
3549           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3550           (or visible-only
3551               (let ((entry 
3552                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3553                 (while (and entry
3554                             (car entry)
3555                             (not
3556                              (gnus-goto-char
3557                               (text-property-any
3558                                (point-min) (point-max) 
3559                                'gnus-group (intern (car (car entry)))))))
3560                   (setq entry (cdr entry)))
3561                 (or entry (goto-char (point-max)))))))
3562       (if (or visible (not visible-only))
3563           (gnus-group-insert-group-line-info group))
3564       (gnus-group-set-mode-line))))
3565
3566 (defun gnus-group-set-mode-line ()
3567   (if (memq 'group gnus-updated-mode-lines)
3568       (let* ((gformat (or gnus-group-mode-line-format-spec
3569                           (setq gnus-group-mode-line-format-spec
3570                                 (gnus-parse-format 
3571                                  gnus-group-mode-line-format 
3572                                  gnus-group-mode-line-format-alist))))
3573              (news-server (car (cdr gnus-select-method)))
3574              (news-method (car gnus-select-method))
3575              (max-len 60)
3576              (mode-string (eval gformat)))
3577         (setq mode-string (eval gformat))
3578         (if (> (length mode-string) max-len) 
3579             (setq mode-string (substring mode-string 0 (- max-len 4))))
3580         (setq mode-line-buffer-identification mode-string)
3581         (set-buffer-modified-p t))))
3582
3583 (defun gnus-group-group-name ()
3584   "Get the name of the newsgroup on the current line."
3585   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3586     (and group (symbol-name group))))
3587
3588 (defun gnus-group-group-level ()
3589   "Get the level of the newsgroup on the current line."
3590   (get-text-property (gnus-point-at-bol) 'gnus-level))
3591
3592 (defun gnus-group-search-forward (&optional backward all level first-too)
3593   "Find the next newsgroup with unread articles.
3594 If BACKWARD is non-nil, find the previous newsgroup instead.
3595 If ALL is non-nil, just find any newsgroup.
3596 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3597 group exists.
3598 If FIRST-TOO, the current line is also eligible as a target."
3599   (let ((way (if backward -1 1))
3600         (low 10)
3601         (beg (point))
3602         pos found)
3603     (if (and backward (progn (beginning-of-line)) (bobp))
3604         nil
3605       (or first-too (forward-line way))
3606       (while (and 
3607               (not (eobp))
3608               (not (setq 
3609                     found 
3610                     (and (or all
3611                              (and
3612                               (let ((unread 
3613                                      (get-text-property (point) 'gnus-unread)))
3614                                 (or (eq unread t) (and unread (> unread 0))))
3615                               (let ((lev (get-text-property
3616                                           (point) 'gnus-level)))
3617                                 (and lev (<= (get-text-property 
3618                                               (point) 'gnus-level)
3619                                              gnus-level-subscribed)))))
3620                          (or (not level)
3621                              (let ((lev (get-text-property (point) 'gnus-level)))
3622                                (if (and lev (<= lev level))
3623                                    t
3624                                  (if (< lev low)
3625                                      (progn
3626                                        (setq low lev)
3627                                        (setq pos (point))))
3628                                  nil))))))
3629               (zerop (forward-line way)))))
3630     (if found 
3631         (progn (gnus-group-position-cursor) t)
3632       (if pos (goto-char pos) (goto-char beg))
3633       nil)))
3634
3635 ;;; Gnus group mode commands
3636
3637 ;; Group marking.
3638
3639 (defun gnus-group-mark-group (n &optional unmark)
3640   "Mark the current group."
3641   (interactive "p")
3642   (let ((buffer-read-only nil)
3643         group)
3644     (while 
3645         (and (> n 0) 
3646              (setq group (gnus-group-group-name))
3647              (progn
3648                (beginning-of-line)
3649                (forward-char 2)
3650                (delete-char 1)
3651                (if unmark
3652                    (progn
3653                      (insert " ")
3654                      (setq gnus-group-marked (delete group gnus-group-marked)))
3655                  (insert "#")
3656                  (setq gnus-group-marked
3657                        (cons group (delete group gnus-group-marked))))
3658                t)
3659              (zerop (gnus-group-next-group 1)))
3660       (setq n (1- n)))
3661     (gnus-summary-position-cursor)
3662     n))
3663
3664 (defun gnus-group-unmark-group (n)
3665   "Remove the mark from the current group."
3666   (interactive "p")
3667   (gnus-group-mark-group n 'unmark))
3668
3669 (defun gnus-group-mark-region (unmark beg end)
3670   "Mark all groups between point and mark.
3671 If UNMARK, remove the mark instead."
3672   (interactive "P\nr")
3673   (let ((num (count-lines beg end)))
3674     (save-excursion
3675       (goto-char beg)
3676       (- num (gnus-group-mark-group num unmark)))))
3677
3678 (defun gnus-group-remove-mark (group)
3679   (and (gnus-group-goto-group group)
3680        (save-excursion
3681          (gnus-group-mark-group 1 'unmark))))
3682
3683 ;; Return a list of groups to work on.  Take into consideration N (the
3684 ;; prefix) and the list of marked groups.
3685 (defun gnus-group-process-prefix (n)
3686   (cond (n
3687          (setq n (prefix-numeric-value n))
3688          ;; There is a prefix, so we return a list of the N next
3689          ;; groups. 
3690          (let ((way (if (< n 0) -1 1))
3691                (n (abs n))
3692                group groups)
3693            (save-excursion
3694              (while (and (> n 0)
3695                          (setq group (gnus-group-group-name)))
3696                (setq groups (cons group groups))
3697                (setq n (1- n))
3698                (forward-line way)))
3699            (nreverse groups)))
3700         (gnus-group-marked
3701          ;; No prefix, but a list of marked articles.
3702          (reverse gnus-group-marked))
3703         (t
3704          ;; Neither marked articles or a prefix, so we return the
3705          ;; current group.
3706          (let ((group (gnus-group-group-name)))
3707            (and group (list group))))))
3708
3709 ;; Selecting groups.
3710
3711 (defun gnus-group-read-group (all &optional no-article group)
3712   "Read news in this newsgroup.
3713 If argument ALL is non-nil, already read articles become readable.
3714 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3715   (interactive "P")
3716   (let ((group (or group (gnus-group-group-name)))
3717         number active marked entry)
3718     (or group (error "No group on current line"))
3719     (setq marked 
3720           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3721     ;; This group might be a dead group. In that case we have to get
3722     ;; the number of unread articles from `gnus-active-hashtb'.
3723     (if entry
3724         (setq number (car entry))
3725       (if (setq active (gnus-gethash group gnus-active-hashtb))
3726           (setq number (- (1+ (cdr active)) (car active)))))
3727     (gnus-summary-read-group 
3728      group (or all (and (numberp number) 
3729                         (zerop (+ number (length (cdr (assq 'tick marked)))
3730                                   (length (cdr (assq 'dormant marked)))))))
3731      no-article)))
3732
3733 (defun gnus-group-select-group (all)
3734   "Select this newsgroup.
3735 No article is selected automatically.
3736 If argument ALL is non-nil, already read articles become readable."
3737   (interactive "P")
3738   (gnus-group-read-group all t))
3739
3740 ;; Enter a group that is not in the group buffer. Non-nil is returned
3741 ;; if selection was successful.
3742 (defun gnus-group-read-ephemeral-group 
3743   (group method &optional activate quit-config)
3744   (let ((group (if (gnus-group-foreign-p group) group
3745                  (gnus-group-prefixed-name group method))))
3746     (gnus-sethash 
3747      group
3748      (list t nil (list group gnus-level-default-subscribed nil nil 
3749                        (append method
3750                                (list
3751                                 (list 'quit-config 
3752                                       (if quit-config quit-config
3753                                         (cons (current-buffer) 'summary)))))))
3754      gnus-newsrc-hashtb)
3755     (set-buffer gnus-group-buffer)
3756     (or (gnus-server-opened method)
3757         (gnus-open-server method)
3758         (error "Unable to contact server: %s" (gnus-status-message method)))
3759     (if activate (gnus-request-group group))
3760     (condition-case ()
3761         (gnus-group-read-group t t group)
3762       (error nil)
3763       (quit nil))
3764     (not (equal major-mode 'gnus-group-mode))))
3765   
3766 (defun gnus-group-jump-to-group (group)
3767   "Jump to newsgroup GROUP."
3768   (interactive 
3769    (list (completing-read 
3770           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3771
3772   (if (equal group "")
3773       (error "Empty group name"))
3774
3775   (let ((b (text-property-any 
3776             (point-min) (point-max) 'gnus-group (intern group))))
3777     (if b
3778         ;; Either go to the line in the group buffer...
3779         (goto-char b)
3780       ;; ... or insert the line.
3781       (or
3782        (gnus-gethash group gnus-active-hashtb)
3783        (gnus-activate-newsgroup group)
3784        (error "%s error: %s" group (gnus-status-message group)))
3785
3786       (gnus-group-update-group group)
3787       (goto-char (text-property-any 
3788                   (point-min) (point-max) 'gnus-group (intern group)))))
3789   ;; Adjust cursor point.
3790   (gnus-group-position-cursor))
3791
3792 (defun gnus-group-goto-group (group)
3793   "Goto to newsgroup GROUP."
3794   (let ((b (text-property-any (point-min) (point-max) 
3795                               'gnus-group (intern group))))
3796     (and b (goto-char b))))
3797
3798 (defun gnus-group-next-group (n)
3799   "Go to next N'th newsgroup.
3800 If N is negative, search backward instead.
3801 Returns the difference between N and the number of skips actually
3802 done."
3803   (interactive "p")
3804   (gnus-group-next-unread-group n t))
3805
3806 (defun gnus-group-next-unread-group (n &optional all level)
3807   "Go to next N'th unread newsgroup.
3808 If N is negative, search backward instead.
3809 If ALL is non-nil, choose any newsgroup, unread or not.
3810 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3811 such group can be found, the next group with a level higher than
3812 LEVEL.
3813 Returns the difference between N and the number of skips actually
3814 made."
3815   (interactive "p")
3816   (let ((backward (< n 0))
3817         (n (abs n)))
3818     (while (and (> n 0)
3819                 (gnus-group-search-forward 
3820                  backward (or (not gnus-group-goto-unread) all) level))
3821       (setq n (1- n)))
3822     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3823                                (if level " on this level or higher" "")))
3824     n))
3825
3826 (defun gnus-group-prev-group (n)
3827   "Go to previous N'th newsgroup.
3828 Returns the difference between N and the number of skips actually
3829 done."
3830   (interactive "p")
3831   (gnus-group-next-unread-group (- n) t))
3832
3833 (defun gnus-group-prev-unread-group (n)
3834   "Go to previous N'th unread newsgroup.
3835 Returns the difference between N and the number of skips actually
3836 done."  
3837   (interactive "p")
3838   (gnus-group-next-unread-group (- n)))
3839
3840 (defun gnus-group-next-unread-group-same-level (n)
3841   "Go to next N'th unread newsgroup on the same level.
3842 If N is negative, search backward instead.
3843 Returns the difference between N and the number of skips actually
3844 done."
3845   (interactive "p")
3846   (gnus-group-next-unread-group n t (gnus-group-group-level))
3847   (gnus-group-position-cursor))
3848
3849 (defun gnus-group-prev-unread-group-same-level (n)
3850   "Go to next N'th unread newsgroup on the same level.
3851 Returns the difference between N and the number of skips actually
3852 done."
3853   (interactive "p")
3854   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3855   (gnus-group-position-cursor))
3856
3857 (defun gnus-group-best-unread-group (&optional exclude-group)
3858   "Go to the group with the highest level.
3859 If EXCLUDE-GROUP, do not go to that group."
3860   (interactive)
3861   (goto-char (point-min))
3862   (let ((best 100000)
3863         unread best-point)
3864     (while (setq unread (get-text-property (point) 'gnus-unread))
3865       (if (and (numberp unread) (> unread 0))
3866           (progn
3867             (or best-point (setq best-point (point)))
3868             (if (and (< (get-text-property (point) 'gnus-level) best)
3869                      (or (not exclude-group)
3870                          (not (equal exclude-group (gnus-group-group-name)))))
3871                 (progn 
3872                   (setq best (get-text-property (point) 'gnus-level))
3873                   (setq best-point (point))))))
3874       (forward-line 1))
3875     (if best-point (goto-char best-point))
3876     (gnus-summary-position-cursor)
3877     (and best-point (gnus-group-group-name))))
3878
3879 (defun gnus-group-first-unread-group ()
3880   "Go to the first group with unread articles."
3881   (interactive)
3882   (goto-char (point-min))
3883   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3884       (gnus-group-next-unread-group 1))
3885   (gnus-group-position-cursor))
3886
3887 (defun gnus-group-enter-server-mode ()
3888   "Jump to the server buffer."
3889   (interactive)
3890   (gnus-server-setup-buffer)
3891   (gnus-configure-windows 'server)
3892   (gnus-server-prepare))
3893
3894 (defun gnus-group-make-group (name method &optional address)
3895   "Add a new newsgroup.
3896 The user will be prompted for a NAME, for a select METHOD, and an
3897 ADDRESS."
3898   (interactive
3899    (cons 
3900     (read-string "Group name: ")
3901     (let ((method
3902            (completing-read 
3903             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3904             nil t)))
3905       (if (assoc method gnus-valid-select-methods)
3906           (list method
3907                 (if (memq 'prompt-address
3908                           (assoc method gnus-valid-select-methods))
3909                     (read-string "Address: ")
3910                   ""))
3911         (list method nil)))))
3912   
3913   (let* ((meth (if address (list (intern method) address) method))
3914          (nname (gnus-group-prefixed-name name meth))
3915          info)
3916     (and (gnus-gethash nname gnus-newsrc-hashtb)
3917          (error "Group %s already exists" nname))
3918     (gnus-group-change-level 
3919      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3920      gnus-level-default-subscribed gnus-level-killed 
3921      (and (gnus-group-group-name)
3922           (gnus-gethash (gnus-group-group-name)
3923                         gnus-newsrc-hashtb))
3924      t)
3925     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3926     (gnus-dribble-enter 
3927      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3928     (gnus-group-insert-group-line-info nname)
3929
3930     (and (gnus-check-backend-function 'request-create-group nname)
3931          (gnus-request-create-group nname))))
3932
3933 (defun gnus-group-edit-group (group &optional part)
3934   "Edit the group on the current line."
3935   (interactive (list (gnus-group-group-name)))
3936   (let ((done-func '(lambda () 
3937                       "Exit editing mode and update the information."
3938                       (interactive)
3939                       (gnus-group-edit-group-done 'part 'group)))
3940         (part (or part 'info))
3941         (winconf (current-window-configuration))
3942         info)
3943     (or group (error "No group on current line"))
3944     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3945         (error "Killed group; can't be edited"))
3946     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3947     (gnus-configure-windows 'edit-group)
3948     (gnus-add-current-to-buffer-list)
3949     (emacs-lisp-mode)
3950     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3951     (use-local-map (copy-keymap emacs-lisp-mode-map))
3952     (local-set-key "\C-c\C-c" done-func)
3953     (make-local-variable 'gnus-prev-winconf)
3954     (setq gnus-prev-winconf winconf)
3955     ;; We modify the func to let it know what part it is editing.
3956     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3957     (setcar (cdr (cdr (nth 4 done-func))) group)
3958     (erase-buffer)
3959     (insert
3960      (cond 
3961       ((eq part 'method)
3962        ";; Type `C-c C-c' after editing the select method.\n\n")
3963       ((eq part 'params)
3964        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3965       ((eq part 'info)
3966        ";; Type `C-c C-c' after editing the group info.\n\n")))
3967     (let ((cinfo (gnus-copy-sequence info))
3968           marked)
3969       (if (not (setq marked (nth 3 cinfo)))
3970           ()
3971         (while marked
3972           (or (eq 'score (car (car marked)))
3973               (eq 'bookmark (car (car marked)))
3974               (eq 'killed (car (car marked)))
3975               (not (numberp (car (cdr (car marked)))))
3976               (setcdr (car marked) 
3977                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3978           (setq marked (cdr marked))))
3979       (insert 
3980        (pp-to-string
3981         (cond ((eq part 'method)
3982                (or (nth 4 info) "native"))
3983               ((eq part 'params)
3984                (nth 5 info))
3985               (t
3986                cinfo)))
3987        "\n"))))
3988
3989 (defun gnus-group-edit-group-method (group)
3990   "Edit the select method of GROUP."
3991   (interactive (list (gnus-group-group-name)))
3992   (gnus-group-edit-group group 'method))
3993
3994 (defun gnus-group-edit-group-parameters (group)
3995   "Edit the group parameters of GROUP."
3996   (interactive (list (gnus-group-group-name)))
3997   (gnus-group-edit-group group 'params))
3998
3999 (defun gnus-group-edit-group-done (part group)
4000   "Get info from buffer, update variables and jump to the group buffer."
4001   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4002   (goto-char (point-min))
4003   (let ((form (read (current-buffer)))
4004         (winconf gnus-prev-winconf))
4005     (if (eq part 'info) 
4006         (gnus-group-set-info form)
4007       (gnus-group-set-info form group part))
4008     (kill-buffer (current-buffer))
4009     (and winconf (set-window-configuration winconf))
4010     (set-buffer gnus-group-buffer)
4011     (gnus-group-update-group (gnus-group-group-name))
4012     (gnus-group-position-cursor)))
4013
4014 (defun gnus-group-make-help-group ()
4015   "Create the (ding) Gnus documentation group."
4016   (interactive)
4017   (let ((path load-path))
4018     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4019                                    "gnus-help" '(nndoc "gnus-help")))
4020                        gnus-newsrc-hashtb)
4021          (error "Documentation group already exists"))
4022     (while (and path
4023                 (not (file-exists-p (concat (file-name-as-directory (car path))
4024                                             "doc.txt"))))
4025       (setq path (cdr path)))
4026     (or path (error "Couldn't find doc group"))
4027     (gnus-group-make-group 
4028      (gnus-group-real-name name)
4029      (list 'nndoc name
4030            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4031            (list 'nndoc-article-type 'mbox))))
4032   (gnus-group-position-cursor))
4033
4034 (defun gnus-group-make-doc-group (file type)
4035   "Create a group that uses a single file as the source."
4036   (interactive 
4037    (list (read-file-name "File name: ") 
4038          (let ((err "")
4039                found char)
4040            (while (not found)
4041              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4042              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4043                                ((= char ?b) 'babyl)
4044                                ((= char ?d) 'digest)
4045                                (t (setq mess "%c unknown. " char)
4046                                   nil))))
4047            found)))
4048   (let* ((file (expand-file-name file))
4049          (name (gnus-generate-new-group-name
4050                 (gnus-group-prefixed-name
4051                  (file-name-nondirectory file) '(nndoc "")))))
4052     (gnus-group-make-group 
4053      (gnus-group-real-name name)
4054      (list 'nndoc name
4055            (list 'nndoc-address file)
4056            (list 'nndoc-article-type type)))))
4057
4058 (defun gnus-group-make-archive-group ()
4059   "Create the (ding) Gnus archive group."
4060   (interactive)
4061   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4062                      gnus-newsrc-hashtb)
4063        (error "Archive group already exists"))
4064   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4065   (gnus-group-position-cursor))
4066
4067 (defun gnus-group-make-directory-group (dir)
4068   "Create an nndir group.
4069 The user will be prompted for a directory. The contents of this
4070 directory will be used as a newsgroup. The directory should contain
4071 mail messages or news articles in files that have numeric names."
4072   (interactive
4073    (list (read-file-name "Create group from directory: ")))
4074   (or (file-exists-p dir) (error "No such directory"))
4075   (or (file-directory-p dir) (error "Not a directory"))
4076   (gnus-group-make-group dir "nndir" dir)
4077   (gnus-group-position-cursor))
4078
4079 (defun gnus-group-make-kiboze-group (group address scores)
4080   "Create an nnkiboze group.
4081 The user will be prompted for a name, a regexp to match groups, and
4082 score file entries for articles to include in the group."
4083   (interactive
4084    (list
4085     (read-string "nnkiboze group name: ")
4086     (read-string "Source groups (regexp): ")
4087     (let ((headers (mapcar (lambda (group) (list group))
4088                            '("subject" "from" "number" "date" "message-id"
4089                              "references" "chars" "lines" "xref")))
4090           scores header regexp regexps)
4091       (while (not (equal "" (setq header (completing-read 
4092                                           "Match on header: " headers nil t))))
4093         (setq regexps nil)
4094         (while (not (equal "" (setq regexp (read-string 
4095                                             (format "Match on %s (string): "
4096                                                     header)))))
4097           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4098         (setq scores (cons (cons header regexps) scores)))
4099       scores)))
4100   (gnus-group-make-group group "nnkiboze" address)
4101   (save-excursion
4102     (gnus-set-work-buffer)
4103     (let (emacs-lisp-mode-hook)
4104       (pp scores (current-buffer)))
4105     (write-region (point-min) (point-max) 
4106                   (concat (or gnus-kill-files-directory "~/News")
4107                           "nnkiboze:" group "." gnus-score-file-suffix)))
4108   (gnus-group-position-cursor))
4109
4110 (defun gnus-group-add-to-virtual (n vgroup)
4111   "Add the current group to a virtual group."
4112   (interactive
4113    (list current-prefix-arg
4114          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4115                           "nnvirtual:")))
4116   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4117       (error "%s is not an nnvirtual group" vgroup))
4118   (let* ((groups (gnus-group-process-prefix n))
4119          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4120     (setcar (cdr method)
4121             (concat 
4122              (nth 1 method) "\\|"
4123              (mapconcat 
4124               (lambda (s) 
4125                 (gnus-group-remove-mark s)
4126                 (concat "\\(^" (regexp-quote s) "$\\)"))
4127               groups "\\|"))))
4128   (gnus-group-position-cursor))
4129
4130 (defun gnus-group-make-empty-virtual (group)
4131   "Create a new, fresh, empty virtual group."
4132   (interactive "sCreate new, empty virtual group: ")
4133   (let* ((method (list 'nnvirtual "^$"))
4134          (pgroup (gnus-group-prefixed-name group method)))
4135     ;; Check whether it exists already.
4136     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4137          (error "Group %s already exists." pgroup))
4138     ;; Subscribe the new group after the group on the current line.
4139     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4140     (gnus-group-update-group pgroup)
4141     (forward-line -1)
4142     (gnus-group-position-cursor)))
4143
4144 (defun gnus-group-enter-directory (dir)
4145   "Enter an ephemeral nneething group."
4146   (interactive "DDirectory to read: ")
4147   (let* ((method (list 'nneething dir))
4148          (leaf (gnus-group-prefixed-name
4149                 (file-name-nondirectory (directory-file-name dir))
4150                 method))
4151          (name (gnus-generate-new-group-name leaf))
4152          (num 0))
4153     (let ((nneething-read-only t))
4154       (or (gnus-group-read-ephemeral-group 
4155            name method t
4156            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4157                                       'summary 'group)))
4158           (error "Couldn't enter %s" dir)))))
4159
4160 ;; Group sorting commands
4161 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4162
4163 (defun gnus-group-sort-groups ()
4164   "Sort the group buffer using `gnus-group-sort-function'."
4165   (interactive)
4166   (setq gnus-newsrc-alist 
4167         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4168   (gnus-make-hashtable-from-newsrc-alist)
4169   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4170
4171 (defun gnus-group-sort-by-alphabet (info1 info2)
4172   (string< (car info1) (car info2)))
4173
4174 (defun gnus-group-sort-by-unread (info1 info2)
4175   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4176         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4177     (< (or (and (numberp n1) n1) 0)
4178        (or (and (numberp n2) n2) 0))))
4179
4180 (defun gnus-group-sort-by-level (info1 info2)
4181   (< (nth 1 info1) (nth 1 info2)))
4182
4183 ;; Group catching up.
4184
4185 (defun gnus-group-catchup-current (n &optional all)
4186   "Mark all articles not marked as unread in current newsgroup as read.
4187 If prefix argument N is numeric, the ARG next newsgroups will be
4188 caught up. If ALL is non-nil, marked articles will also be marked as
4189 read. Cross references (Xref: header) of articles are ignored.
4190 The difference between N and actual number of newsgroups that were
4191 caught up is returned."
4192   (interactive "P")
4193   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4194                gnus-expert-user
4195                (gnus-y-or-n-p
4196                 (if all
4197                     "Do you really want to mark all articles as read? "
4198                   "Mark all unread articles as read? "))))
4199       n
4200     (let ((groups (gnus-group-process-prefix n))
4201           (ret 0))
4202       (while groups
4203         ;; Virtual groups have to be given special treatment. 
4204         (let ((method (gnus-find-method-for-group (car groups))))
4205           (if (eq 'nnvirtual (car method))
4206               (nnvirtual-catchup-group
4207                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4208         (gnus-group-remove-mark (car groups))
4209         (if (prog1
4210                 (gnus-group-goto-group (car groups))
4211               (gnus-group-catchup (car groups) all))
4212             (gnus-group-update-group-line)
4213           (setq ret (1+ ret)))
4214         (setq groups (cdr groups)))
4215       (gnus-group-next-unread-group 1)
4216       ret)))
4217
4218 (defun gnus-group-catchup-current-all (n)
4219   "Mark all articles in current newsgroup as read.
4220 Cross references (Xref: header) of articles are ignored."
4221   (interactive "P")
4222   (gnus-group-catchup-current n 'all))
4223
4224 (defun gnus-group-catchup (group &optional all)
4225   "Mark all articles in GROUP as read.
4226 If ALL is non-nil, all articles are marked as read.
4227 The return value is the number of articles that were marked as read,
4228 or nil if no action could be taken."
4229   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4230          (num (car entry))
4231          (marked (nth 3 (nth 2 entry)))
4232          ticked)
4233     (if (not (numberp (car entry)))
4234         (gnus-message 1 "Can't catch up; non-active group")
4235       ;; Do the updating only if the newsgroup isn't killed.
4236       (if (not entry)
4237           ()
4238         (gnus-update-read-articles 
4239          group (and (not all) (append (cdr (assq 'tick marked))
4240                                       (cdr (assq 'dormant marked))))
4241          nil (and (not all) (cdr (assq 'tick marked))))
4242         (and all marked
4243              (setcar (nthcdr 3 (nth 2 entry)) 
4244                      (delq (assq 'dormant marked) 
4245                            (nth 3 (nth 2 entry)))))))
4246     num))
4247
4248 (defun gnus-group-expire-articles (n)
4249   "Expire all expirable articles in the current newsgroup."
4250   (interactive "P")
4251   (let ((groups (gnus-group-process-prefix n))
4252         group)
4253     (or groups (error "No groups to expire"))
4254     (while groups
4255       (setq group (car groups)
4256             groups (cdr groups))
4257       (gnus-group-remove-mark group)
4258       (if (not (gnus-check-backend-function 'request-expire-articles group))
4259           ()
4260         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4261                (expirable (if (memq 'total-expire (nth 5 info))
4262                               (cons nil (gnus-list-of-read-articles group))
4263                             (assq 'expire (nth 3 info)))))
4264           (and expirable 
4265                (setcdr expirable
4266                        (gnus-request-expire-articles 
4267                         (cdr expirable) group))))))))
4268
4269 (defun gnus-group-expire-all-groups ()
4270   "Expire all expirable articles in all newsgroups."
4271   (interactive)
4272   (gnus-message 5 "Expiring...")
4273   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4274                                    (cdr gnus-newsrc-alist))))
4275     (gnus-group-expire-articles nil))
4276   (gnus-message 5 "Expiring...done"))
4277
4278 (defun gnus-group-set-current-level (n level)
4279   "Set the level of the next N groups to LEVEL."
4280   (interactive "P\nnLevel: ")
4281   (or (and (>= level 1) (<= level gnus-level-killed))
4282       (error "Illegal level: %d" level))
4283   (let ((groups (gnus-group-process-prefix n))
4284         group)
4285     (while groups
4286       (setq group (car groups)
4287             groups (cdr groups))
4288       (gnus-group-remove-mark group)
4289       (gnus-message 6 "Changed level of %s from %d to %d" 
4290                     group (gnus-group-group-level) level)
4291       (gnus-group-change-level group level
4292                                (gnus-group-group-level))
4293       (gnus-group-update-group-line)))
4294   (gnus-group-position-cursor))
4295
4296 (defun gnus-group-unsubscribe-current-group (arg)
4297   "Toggle subscribe from/to unsubscribe current group."
4298   (interactive "P")
4299   (let ((group (gnus-group-group-name)))
4300     (or group (error "No newsgroup on current line"))
4301     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
4302                           gnus-level-default-unsubscribed
4303                         gnus-level-default-subscribed)))
4304     (gnus-group-unsubscribe-group group arg)
4305     (gnus-group-next-group 1)))
4306
4307 (defun gnus-group-unsubscribe-group (group &optional level)
4308   "Toggle subscribe from/to unsubscribe GROUP.
4309 New newsgroup is added to .newsrc automatically."
4310   (interactive
4311    (list (completing-read "Group: " gnus-active-hashtb nil 
4312                           gnus-have-read-active-file)))
4313   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4314     (cond (newsrc
4315            ;; Toggle subscription flag.
4316            (gnus-group-change-level 
4317             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4318                                            gnus-level-subscribed) 
4319                                        (1+ gnus-level-subscribed)
4320                                      gnus-level-default-subscribed)))
4321            (gnus-group-update-group group))
4322           ((and (stringp group)
4323                 (or (not gnus-have-read-active-file)
4324                     (gnus-gethash group gnus-active-hashtb)))
4325            ;; Add new newsgroup.
4326            (gnus-group-change-level 
4327             group 
4328             (if level level gnus-level-default-subscribed) 
4329             (or (and (member group gnus-zombie-list) 
4330                      gnus-level-zombie) 
4331                 gnus-level-killed)
4332             (and (gnus-group-group-name)
4333                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4334            (gnus-group-update-group group))
4335           (t (error "No such newsgroup: %s" group)))
4336     (gnus-group-position-cursor)))
4337
4338 (defun gnus-group-transpose-groups (n)
4339   "Move the current newsgroup up N places.
4340 If given a negative prefix, move down instead. The difference between
4341 N and the number of steps taken is returned." 
4342   (interactive "p")
4343   (or (gnus-group-group-name)
4344       (error "No group on current line"))
4345   (gnus-group-kill-group 1)
4346   (prog1
4347       (forward-line (- n))
4348     (gnus-group-yank-group)
4349     (gnus-group-position-cursor)))
4350
4351 (defun gnus-group-kill-all-zombies ()
4352   "Kill all zombie newsgroups."
4353   (interactive)
4354   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4355   (setq gnus-zombie-list nil)
4356   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4357   (goto-char (point-min))
4358   (gnus-group-position-cursor))
4359
4360 (defun gnus-group-kill-region (begin end)
4361   "Kill newsgroups in current region (excluding current point).
4362 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4363   (interactive "r")
4364   (let ((lines
4365          ;; Count lines.
4366          (save-excursion
4367            (count-lines
4368             (progn
4369               (goto-char begin)
4370               (beginning-of-line)
4371               (point))
4372             (progn
4373               (goto-char end)
4374               (beginning-of-line)
4375               (point))))))
4376     (goto-char begin)
4377     (beginning-of-line)                 ;Important when LINES < 1
4378     (gnus-group-kill-group lines)))
4379
4380 (defun gnus-group-kill-group (n)
4381   "The the next N groups.
4382 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4383 However, only groups that were alive can be yanked; already killed 
4384 groups or zombie groups can't be yanked.
4385 The return value is the name of the (last) group that was killed."
4386   (interactive "P")
4387   (let ((buffer-read-only nil)
4388         (groups (gnus-group-process-prefix n))
4389         group entry level)
4390     (while groups
4391       (setq group (car groups)
4392             groups (cdr groups))
4393       (gnus-group-remove-mark group)
4394       (setq level (gnus-group-group-level))
4395       (gnus-delete-line)
4396       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4397           (setq gnus-list-of-killed-groups 
4398                 (cons (cons (car entry) (nth 2 entry)) 
4399                       gnus-list-of-killed-groups)))
4400       (gnus-group-change-level 
4401        (if entry entry group) gnus-level-killed (if entry nil level)))
4402     (gnus-group-position-cursor)
4403     group))
4404
4405 (defun gnus-group-yank-group (&optional arg)
4406   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4407 inserting it before the current newsgroup.  The numeric ARG specifies
4408 how many newsgroups are to be yanked.  The name of the (last)
4409 newsgroup yanked is returned."
4410   (interactive "p")
4411   (if (not arg) (setq arg 1))
4412   (let (info group prev)
4413     (while (>= (setq arg (1- arg)) 0)
4414       (if (not (setq info (car gnus-list-of-killed-groups)))
4415           (error "No more newsgroups to yank"))
4416       (setq group (nth 2 info))
4417       ;; Find which newsgroup to insert this one before - search
4418       ;; backward until something suitable is found. If there are no
4419       ;; other newsgroups in this buffer, just make this newsgroup the
4420       ;; first newsgroup.
4421       (setq prev (gnus-group-group-name))
4422       (gnus-group-change-level 
4423        info (nth 2 info) gnus-level-killed 
4424        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4425        t)
4426       (gnus-group-insert-group-line-info (nth 1 info))
4427       (setq gnus-list-of-killed-groups 
4428             (cdr gnus-list-of-killed-groups)))
4429     (forward-line -1)
4430     (gnus-group-position-cursor)
4431     group))
4432       
4433 (defun gnus-group-list-all-groups (arg)
4434   "List all newsgroups with level ARG or lower.
4435 Default is gnus-level-unsubscribed, which lists all subscribed and most
4436 unsubscribed groups."
4437   (interactive "P")
4438   (setq arg (or arg gnus-level-unsubscribed))
4439   (gnus-group-list-groups arg t))
4440
4441 (defun gnus-group-list-killed ()
4442   "List all killed newsgroups in the group buffer."
4443   (interactive)
4444   (if (not gnus-killed-list)
4445       (gnus-message 6 "No killed groups")
4446     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4447     (goto-char (point-min)))
4448   (gnus-group-position-cursor))
4449
4450 (defun gnus-group-list-zombies ()
4451   "List all zombie newsgroups in the group buffer."
4452   (interactive)
4453   (if (not gnus-zombie-list)
4454       (gnus-message 6 "No zombie groups")
4455     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4456     (goto-char (point-min)))
4457   (gnus-group-position-cursor))
4458
4459 (defun gnus-group-get-new-news (&optional arg)
4460   "Get newly arrived articles.
4461 If ARG is non-nil, it should be a number between one and nine to
4462 specify which levels you are interested in re-scanning."
4463   (interactive "P")
4464   (run-hooks 'gnus-get-new-news-hook)
4465   (let ((level arg))
4466     (if gnus-group-use-permanent-levels
4467         (if level
4468             (setq gnus-group-default-list-level level)
4469           (setq level (or gnus-group-default-list-level 
4470                           gnus-level-subscribed))))
4471     (if (and gnus-read-active-file (not level))
4472         (progn
4473           (gnus-read-active-file)
4474           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4475       (let ((gnus-read-active-file nil)
4476             (gnus-have-read-active-file nil))
4477         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4478     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4479                                 gnus-group-default-list-level
4480                                 gnus-level-subscribed)
4481                             gnus-have-all-newsgroups)))
4482
4483 (defun gnus-group-get-new-news-this-group (n)
4484   "Check for newly arrived news in the current group (and the N-1 next groups).
4485 The difference between N and the number of newsgroup checked is returned.
4486 If N is negative, this group and the N-1 previous groups will be checked."
4487   (interactive "P")
4488   (let* ((groups (gnus-group-process-prefix n))
4489          (ret (if (numberp n) (- n (length groups)) 0))
4490          (w-p (window-start))
4491          group)
4492     (while groups
4493       (setq group (car groups)
4494             groups (cdr groups))
4495       (gnus-group-remove-mark group)
4496       (or (gnus-get-new-news-in-group group)
4497           (progn 
4498             (ding) 
4499             (message "%s error: %s" group (gnus-status-message group))
4500             (sit-for 2))))
4501     ;; !!! I don't know why the buffer scrolls forward when updating
4502     ;; the first line in the group buffer, but it does. So we set the
4503     ;; window start forcibly.
4504 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4505     (gnus-group-next-unread-group 1 t)
4506     (gnus-summary-position-cursor)
4507     ret))
4508
4509 (defun gnus-get-new-news-in-group (group)
4510   (and group 
4511        (gnus-activate-newsgroup group)
4512        (progn
4513          (gnus-get-unread-articles-in-group 
4514           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4515           (gnus-gethash group gnus-active-hashtb))
4516          (gnus-group-update-group-line)
4517          t)))
4518
4519 (defun gnus-group-fetch-faq (group)
4520   "Fetch the FAQ for the current group."
4521   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4522   (or group (error "No group name given"))
4523   (let ((file (concat gnus-group-faq-directory group))) 
4524     (if (not (file-exists-p file))
4525         (error "No such file: %s" file)
4526       (find-file file))))
4527   
4528 (defun gnus-group-describe-group (force &optional group)
4529   "Display a description of the current newsgroup."
4530   (interactive (list current-prefix-arg (gnus-group-group-name)))
4531   (and force (setq gnus-description-hashtb nil))
4532   (let ((method (gnus-find-method-for-group group))
4533         desc)
4534     (or group (error "No group name given"))
4535     (and (or (and gnus-description-hashtb
4536                   ;; We check whether this group's method has been
4537                   ;; queried for a description file.  
4538                   (gnus-gethash 
4539                    (gnus-group-prefixed-name "" method) 
4540                    gnus-description-hashtb))
4541              (setq desc (gnus-group-get-description group))
4542              (gnus-read-descriptions-file method))
4543          (message
4544           (or desc (gnus-gethash group gnus-description-hashtb)
4545               "No description available")))))
4546
4547 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4548 (defun gnus-group-describe-all-groups (force)
4549   "Pop up a buffer with descriptions of all newsgroups."
4550   (interactive "P")
4551   (and force (setq gnus-description-hashtb nil))
4552   (if (not (or gnus-description-hashtb
4553                (gnus-read-all-descriptions-files)))
4554       (error "Couldn't request descriptions file"))
4555   (let ((buffer-read-only nil)
4556         b)
4557     (erase-buffer)
4558     (mapatoms
4559      (lambda (group)
4560        (setq b (point))
4561        (insert (format "      *: %-20s %s\n" (symbol-name group)
4562                        (symbol-value group)))
4563        (add-text-properties 
4564         b (1+ b) (list 'gnus-group group
4565                        'gnus-unread t 'gnus-marked nil
4566                        'gnus-level (1+ gnus-level-subscribed))))
4567      gnus-description-hashtb)
4568     (goto-char (point-min))
4569     (gnus-group-position-cursor)))
4570
4571 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4572 (defun gnus-group-apropos (regexp &optional search-description)
4573   "List all newsgroups that have names that match a regexp."
4574   (interactive "sGnus apropos (regexp): ")
4575   (let ((prev "")
4576         (obuf (current-buffer))
4577         groups des prev)
4578     ;; Go through all newsgroups that are known to Gnus.
4579     (mapatoms 
4580      (lambda (group)
4581        (and (string-match regexp (symbol-name group))
4582             (setq groups (cons (symbol-name group) groups))))
4583      gnus-active-hashtb)
4584     ;; Go through all descriptions that are known to Gnus. 
4585     (if search-description
4586         (mapatoms 
4587          (lambda (group)
4588            (and (string-match regexp (symbol-value group))
4589                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4590                 (setq groups (cons (symbol-name group) groups))))
4591          gnus-description-hashtb))
4592     (if (not groups)
4593         (gnus-message 3 "No groups matched \"%s\"." regexp)
4594       ;; Print out all the groups.
4595       (save-excursion
4596         (pop-to-buffer "*Gnus Help*")
4597         (buffer-disable-undo (current-buffer))
4598         (erase-buffer)
4599         (setq groups (sort groups 'string<))
4600         (while groups
4601           ;; Groups may be entered twice into the list of groups.
4602           (if (not (string= (car groups) prev))
4603               (progn
4604                 (insert (setq prev (car groups)) "\n")
4605                 (if (and gnus-description-hashtb
4606                          (setq des (gnus-gethash (car groups) 
4607                                                  gnus-description-hashtb)))
4608                     (insert "  " des "\n"))))
4609           (setq groups (cdr groups)))
4610         (goto-char (point-min))))
4611     (pop-to-buffer obuf)))
4612
4613 (defun gnus-group-description-apropos (regexp)
4614   "List all newsgroups that have names or descriptions that match a regexp."
4615   (interactive "sGnus description apropos (regexp): ")
4616   (if (not (or gnus-description-hashtb
4617                (gnus-read-all-descriptions-files)))
4618       (error "Couldn't request descriptions file"))
4619   (gnus-group-apropos regexp t))
4620
4621 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4622 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4623   "List all groups with unread articles that match REGEXP.
4624 If the prefix LEVEL is non-nil, it should be a number that says which
4625 level to cut off listing groups. 
4626 If ALL, also list groups with no unread articles.
4627 If LOWEST, don't list groups with level lower than LOWEST."
4628   (interactive "P\nsList newsgroups matching: ")
4629   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4630                            all (or lowest 1) regexp)
4631   (goto-char (point-min))
4632   (gnus-group-position-cursor))
4633
4634 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4635   "List all groups that match REGEXP.
4636 If the prefix LEVEL is non-nil, it should be a number that says which
4637 level to cut off listing groups. 
4638 If LOWEST, don't list groups with level lower than LOWEST."
4639   (interactive "P\nsList newsgroups matching: ")
4640   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4641
4642 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4643 (defun gnus-group-save-newsrc ()
4644   "Save the Gnus startup files."
4645   (interactive)
4646   (gnus-save-newsrc-file))
4647
4648 (defun gnus-group-restart (&optional arg)
4649   "Force Gnus to read the .newsrc file."
4650   (interactive "P")
4651   (gnus-save-newsrc-file)
4652   (gnus-setup-news 'force)
4653   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4654
4655 (defun gnus-group-read-init-file ()
4656   "Read the Gnus elisp init file."
4657   (interactive)
4658   (gnus-read-init-file))
4659
4660 (defun gnus-group-check-bogus-groups (silent)
4661   "Check bogus newsgroups.
4662 If given a prefix, don't ask for confirmation before removing a bogus
4663 group."
4664   (interactive "P")
4665   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4666   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4667
4668 (defun gnus-group-edit-global-kill (article &optional group)
4669   "Edit the global kill file.
4670 If GROUP, edit that local kill file instead."
4671   (interactive "P")
4672   (setq gnus-current-kill-article article)
4673   (gnus-kill-file-edit-file group)
4674   (gnus-message 6
4675    (substitute-command-keys
4676     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4677
4678 (defun gnus-group-edit-local-kill (article group)
4679   "Edit a local kill file."
4680   (interactive (list nil (gnus-group-group-name)))
4681   (gnus-group-edit-global-kill article group))
4682
4683 (defun gnus-group-force-update ()
4684   "Update `.newsrc' file."
4685   (interactive)
4686   (gnus-save-newsrc-file))
4687
4688 (defun gnus-group-suspend ()
4689   "Suspend the current Gnus session.
4690 In fact, cleanup buffers except for group mode buffer.
4691 The hook gnus-suspend-gnus-hook is called before actually suspending."
4692   (interactive)
4693   (run-hooks 'gnus-suspend-gnus-hook)
4694   ;; Kill Gnus buffers except for group mode buffer.
4695   (let ((group-buf (get-buffer gnus-group-buffer)))
4696     ;; Do this on a separate list in case the user does a ^G before we finish
4697     (let ((gnus-buffer-list
4698            (delq group-buf (delq gnus-dribble-buffer
4699                                  (append gnus-buffer-list nil)))))
4700       (while gnus-buffer-list
4701         (gnus-kill-buffer (car gnus-buffer-list))
4702         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4703     (if group-buf
4704         (progn
4705           (setq gnus-buffer-list (list group-buf))
4706           (bury-buffer group-buf)
4707           (delete-windows-on group-buf t)))))
4708
4709 (defun gnus-group-clear-dribble ()
4710   "Clear all information from the dribble buffer."
4711   (interactive)
4712   (gnus-dribble-clear))
4713
4714 (defun gnus-group-exit ()
4715   "Quit reading news after updating .newsrc.eld and .newsrc.
4716 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4717   (interactive)
4718   (if (or noninteractive                ;For gnus-batch-kill
4719           (zerop (buffer-size))         ;No news is good news.
4720           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4721           (not gnus-interactive-exit)   ;Without confirmation
4722           gnus-expert-user
4723           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4724       (progn
4725         (if gnus-use-full-window
4726             (delete-other-windows)
4727           (gnus-remove-some-windows))
4728         (run-hooks 'gnus-exit-gnus-hook)
4729         (gnus-offer-save-summaries)
4730         (gnus-save-newsrc-file)
4731         (gnus-close-backends)
4732         (gnus-clear-system))))
4733
4734 (defun gnus-close-backends ()
4735   ;; Send a close request to all backends that support such a request. 
4736   (let ((methods gnus-valid-select-methods)
4737         func)
4738     (while methods
4739       (if (fboundp (setq func (intern (concat (car (car methods))
4740                                               "-request-close"))))
4741           (funcall func))
4742       (setq methods (cdr methods)))))
4743
4744 (defun gnus-group-quit ()
4745   "Quit reading news without updating .newsrc.eld or .newsrc.
4746 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4747   (interactive)
4748   (if (or noninteractive                ;For gnus-batch-kill
4749           (zerop (buffer-size))
4750           (not (gnus-server-opened gnus-select-method))
4751           gnus-expert-user
4752           (not gnus-current-startup-file)
4753           (gnus-yes-or-no-p
4754            (format "Quit reading news without saving %s? "
4755                    (file-name-nondirectory gnus-current-startup-file))))
4756       (progn
4757         (run-hooks 'gnus-exit-gnus-hook)
4758         (if gnus-use-full-window
4759             (delete-other-windows)
4760           (gnus-remove-some-windows))
4761         (gnus-dribble-save)
4762         (gnus-close-backends)
4763         (gnus-clear-system))))
4764
4765 (defun gnus-offer-save-summaries ()
4766   (let ((buffers (buffer-list)))
4767     (save-excursion
4768       (while buffers
4769         (and 
4770          ;; We look for buffers with "Summary" in the name.
4771          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4772          (progn
4773            (set-buffer (car buffers))
4774            ;; We check that this is, indeed, a summary buffer.
4775            (eq major-mode 'gnus-summary-mode)) 
4776          ;; We ask the user whether she wants to save the info.
4777          (gnus-y-or-n-p
4778                (format "Update summary buffer %s? " (buffer-name)))
4779          ;; We do it by simply exiting.
4780          (gnus-summary-exit))
4781         (setq buffers (cdr buffers))))))
4782
4783 (defun gnus-group-describe-briefly ()
4784   "Give a one line description of the group mode commands."
4785   (interactive)
4786   (gnus-message 6
4787    (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")))
4788
4789 (defun gnus-group-browse-foreign-server (method)
4790   "Browse a foreign news server.
4791 If called interactively, this function will ask for a select method
4792  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4793 If not, METHOD should be a list where the first element is the method
4794 and the second element is the address."
4795   (interactive
4796    (list (let ((how (completing-read 
4797                      "Which backend: "
4798                      (append gnus-valid-select-methods gnus-server-alist)
4799                      nil t "nntp")))
4800            ;; We either got a backend name or a virtual server name.
4801            ;; If the first, we also need an address.
4802            (if (assoc how gnus-valid-select-methods)
4803                (list (intern how)
4804                      ;; Suggested by mapjph@bath.ac.uk.
4805                      (completing-read 
4806                       "Address: " 
4807                       (mapcar (lambda (server) (list server))
4808                               gnus-secondary-servers)))
4809              ;; We got a server name, so we find the method.
4810              (gnus-server-to-method how)))))
4811   (gnus-browse-foreign-server method))
4812
4813 \f
4814 ;;;
4815 ;;; Browse Server Mode
4816 ;;;
4817
4818 (defvar gnus-browse-mode-hook nil)
4819 (defvar gnus-browse-mode-map nil)
4820 (put 'gnus-browse-mode 'mode-class 'special)
4821
4822 (if gnus-browse-mode-map
4823     nil
4824   (setq gnus-browse-mode-map (make-keymap))
4825   (suppress-keymap gnus-browse-mode-map)
4826   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4827   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4828   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4829   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4830   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4831   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4832   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4833   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4834   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4835   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4836   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4837   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4838   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4839   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4840   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4841   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4842   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4843   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4844   )
4845
4846 (defvar gnus-browse-current-method nil)
4847 (defvar gnus-browse-return-buffer nil)
4848
4849 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4850
4851 (defun gnus-browse-foreign-server (method &optional return-buffer)
4852   (setq gnus-browse-current-method method)
4853   (setq gnus-browse-return-buffer return-buffer)
4854   (let ((gnus-select-method method)
4855         groups group)
4856     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4857     (or (gnus-server-opened method)
4858         (gnus-open-server method)
4859         (error "Unable to contact server: %s" (gnus-status-message method)))
4860     (or (gnus-request-list method)
4861         (error "Couldn't request list: %s" (gnus-status-message method)))
4862     (get-buffer-create gnus-browse-buffer)
4863     (gnus-add-current-to-buffer-list)
4864     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4865     (gnus-configure-windows 'browse)
4866     (buffer-disable-undo (current-buffer))
4867     (let ((buffer-read-only nil))
4868       (erase-buffer))
4869     (gnus-browse-mode)
4870     (setq mode-line-buffer-identification
4871           (format
4872            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4873     (save-excursion
4874       (set-buffer nntp-server-buffer)
4875       (let ((cur (current-buffer)))
4876         (goto-char (point-min))
4877         (or (string= gnus-ignored-newsgroups "")
4878             (delete-matching-lines gnus-ignored-newsgroups))
4879         (while (re-search-forward 
4880                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4881           (goto-char (match-end 1))
4882           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4883                                                      (match-end 1))
4884                                    (max 0 (- (1+ (read cur)) (read cur))))
4885                              groups)))))
4886     (setq groups (sort groups 
4887                        (lambda (l1 l2)
4888                          (string< (car l1) (car l2)))))
4889     (let ((buffer-read-only nil))
4890       (while groups
4891         (setq group (car groups))
4892         (insert 
4893          (format "K%7d: %s\n" (cdr group) (car group)))
4894         (setq groups (cdr groups))))
4895     (switch-to-buffer (current-buffer))
4896     (goto-char (point-min))
4897     (gnus-group-position-cursor)))
4898
4899 (defun gnus-browse-mode ()
4900   "Major mode for browsing a foreign server.
4901
4902 All normal editing commands are switched off.
4903
4904 \\<gnus-browse-mode-map>
4905 The only things you can do in this buffer is
4906
4907 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4908 The group will be inserted into the group buffer upon exit from this
4909 buffer.  
4910
4911 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4912
4913 3) `\\[gnus-browse-exit]' to return to the group buffer."
4914   (interactive)
4915   (kill-all-local-variables)
4916   (if gnus-visual (gnus-browse-make-menu-bar))
4917   (setq mode-line-modified "-- ")
4918   (make-local-variable 'mode-line-format)
4919   (setq mode-line-format (copy-sequence mode-line-format))
4920   (and (equal (nth 3 mode-line-format) "   ")
4921        (setcar (nthcdr 3 mode-line-format) ""))
4922   (setq major-mode 'gnus-browse-mode)
4923   (setq mode-name "Browse Server")
4924   (setq mode-line-process nil)
4925   (use-local-map gnus-browse-mode-map)
4926   (buffer-disable-undo (current-buffer))
4927   (setq truncate-lines t)
4928   (setq buffer-read-only t)
4929   (run-hooks 'gnus-browse-mode-hook))
4930
4931 (defun gnus-browse-read-group (&optional no-article)
4932   "Enter the group at the current line."
4933   (interactive)
4934   (let ((group (gnus-browse-group-name)))
4935     (or (gnus-group-read-ephemeral-group 
4936          group gnus-browse-current-method nil
4937          (cons (current-buffer) 'browse))
4938         (error "Couldn't enter %s" group))))
4939
4940 (defun gnus-browse-select-group ()
4941   "Select the current group."
4942   (interactive)
4943   (gnus-browse-read-group 'no))
4944
4945 (defun gnus-browse-next-group (n)
4946   "Go to the next group."
4947   (interactive "p")
4948   (prog1
4949       (forward-line n)
4950     (gnus-group-position-cursor)))
4951
4952 (defun gnus-browse-prev-group (n)
4953   "Go to the next group."
4954   (interactive "p")
4955   (gnus-browse-next-group (- n)))
4956
4957 (defun gnus-browse-unsubscribe-current-group (arg)
4958   "(Un)subscribe to the next ARG groups."
4959   (interactive "p")
4960   (and (eobp)
4961        (error "No group at current line."))
4962   (let ((ward (if (< arg 0) -1 1))
4963         (arg (abs arg)))
4964     (while (and (> arg 0)
4965                 (not (eobp))
4966                 (gnus-browse-unsubscribe-group)
4967                 (zerop (gnus-browse-next-group ward)))
4968       (setq arg (1- arg)))
4969     (gnus-group-position-cursor)
4970     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4971     arg))
4972
4973 (defun gnus-browse-group-name ()
4974   (save-excursion
4975     (beginning-of-line)
4976     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4977         ()
4978       (gnus-group-prefixed-name 
4979        (buffer-substring (match-beginning 1) (match-end 1))
4980        gnus-browse-current-method))))
4981   
4982 (defun gnus-browse-unsubscribe-group ()
4983   (let ((sub nil)
4984         (buffer-read-only nil)
4985         group)
4986     (save-excursion
4987       (beginning-of-line)
4988       (if (= (following-char) ?K) (setq sub t))
4989       (setq group (gnus-browse-group-name))
4990       (beginning-of-line)
4991       (delete-char 1)
4992       (if sub
4993           (progn
4994             (gnus-group-change-level 
4995              (list t group gnus-level-default-subscribed
4996                    nil nil gnus-browse-current-method) 
4997              gnus-level-default-subscribed gnus-level-killed
4998              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4999              t)
5000             (insert ? ))
5001         (gnus-group-change-level 
5002          group gnus-level-killed gnus-level-default-subscribed)
5003         (insert ?K)))
5004     t))
5005
5006 (defun gnus-browse-exit ()
5007   "Quit browsing and return to the group buffer."
5008   (interactive)
5009   (if (eq major-mode 'gnus-browse-mode)
5010       (kill-buffer (current-buffer)))
5011   (if gnus-browse-return-buffer
5012       (gnus-configure-windows 'server)
5013     (gnus-configure-windows 'group)))
5014
5015 (defun gnus-browse-describe-briefly ()
5016   "Give a one line description of the group mode commands."
5017   (interactive)
5018   (gnus-message 6
5019    (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")))
5020       
5021 \f
5022 ;;;
5023 ;;; Gnus summary mode
5024 ;;;
5025
5026 (defvar gnus-summary-mode-map nil)
5027 (defvar gnus-summary-mark-map nil)
5028 (defvar gnus-summary-mscore-map nil)
5029 (defvar gnus-summary-article-map nil)
5030 (defvar gnus-summary-thread-map nil)
5031 (defvar gnus-summary-goto-map nil)
5032 (defvar gnus-summary-exit-map nil)
5033 (defvar gnus-summary-various-map nil)
5034 (defvar gnus-summary-interest-map nil)
5035 (defvar gnus-summary-sort-map nil)
5036 (defvar gnus-summary-backend-map nil)
5037 (defvar gnus-summary-save-map nil)
5038 (defvar gnus-summary-wash-map nil)
5039 (defvar gnus-summary-help-map nil)
5040
5041 (put 'gnus-summary-mode 'mode-class 'special)
5042
5043 (if gnus-summary-mode-map
5044     nil
5045   (setq gnus-summary-mode-map (make-keymap))
5046   (suppress-keymap gnus-summary-mode-map)
5047
5048   ;; Non-orthogonal keys
5049
5050   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5051   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5052   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5053   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5054   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5055   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5056   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5057   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5058   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5059   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5060   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5061   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5062   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5063   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5064   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5065   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5066   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5067   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5068   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5069   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5070   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5071   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5072   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5073   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5074   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5075   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5076   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5077   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5078   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5079   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5080   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5081   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5082   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5083   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5084   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5085   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5086   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5087   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5088   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5089   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5090   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5091   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5092   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5093   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5094   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5095   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5096   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5097   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5098   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5099   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5100   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5101   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5102   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5103   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5104   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5105   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5106   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5107   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5108   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5109   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5110   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5111   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5112   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5113   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5114   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5115   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5116   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5117   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5118   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5119   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5120   (define-key gnus-summary-mode-map "V" 'gnus-version)
5121   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5122   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5123   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5124   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5125   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5126   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5127   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5128   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5129 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5130   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5131   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5132   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5133 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5134   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5135   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5136   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5137   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5138   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5139
5140
5141   ;; Sort of orthogonal keymap
5142   (define-prefix-command 'gnus-summary-mark-map)
5143   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5144   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5145   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5146   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5147   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5148   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5149   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5150   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5151   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5152   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5153   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5154   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5155   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5156   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5157   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5158   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5159   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5160   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5161   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5162   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5163   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5164   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5165   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5166   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5167
5168   (define-prefix-command 'gnus-summary-mscore-map)
5169   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5170   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5171   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5172   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5173   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5174
5175   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5176   
5177   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5178   
5179   (define-prefix-command 'gnus-summary-goto-map)
5180   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5181   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5182   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5183   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5184   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5185   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5186   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5187   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5188   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5189   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5190   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5191   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5192   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5193   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5194
5195
5196   (define-prefix-command 'gnus-summary-thread-map)
5197   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5198   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5199   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5200   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5201   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5202   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5203   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5204   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5205   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5206   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5207   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5208   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5209   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5210   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5211
5212   
5213   (define-prefix-command 'gnus-summary-exit-map)
5214   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5215   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5216   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5217   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5218   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5219   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5220   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5221   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5222   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5223   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5224   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5225
5226
5227   (define-prefix-command 'gnus-summary-article-map)
5228   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5229   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5230   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5231   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5232   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5233   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5234   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5235   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5236   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5237   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5238   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5239   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5240   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5241   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5242   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5243   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5244   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5245   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5246
5247
5248   (define-prefix-command 'gnus-summary-wash-map)
5249   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5250   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5251   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5252   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5253   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5254   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5255   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5256   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5257   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5258   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5259   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5260   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5261
5262   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5263   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5264   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5265   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5266   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5267   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5268
5269
5270   (define-prefix-command 'gnus-summary-help-map)
5271   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5272   (define-key gnus-summary-help-map "v" 'gnus-version)
5273   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5274   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5275   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5276   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5277
5278
5279   (define-prefix-command 'gnus-summary-backend-map)
5280   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5281   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5282   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5283   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5284   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5285   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5286   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5287   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5288   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5289
5290
5291   (define-prefix-command 'gnus-summary-save-map)
5292   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5293   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5294   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5295   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5296   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5297   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5298   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5299   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5300 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5301
5302   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5303   
5304   (define-prefix-command 'gnus-summary-various-map)
5305   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5306   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5307   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5308   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5309   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5310   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5311   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5312   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5313   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5314   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5315   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5316
5317   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5318
5319   (define-prefix-command 'gnus-summary-sort-map)
5320   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5321   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5322   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5323   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5324   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5325   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5326
5327   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5328   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5329   )
5330
5331
5332 \f
5333
5334 (defun gnus-summary-mode ()
5335   "Major mode for reading articles.
5336
5337 All normal editing commands are switched off.
5338 \\<gnus-summary-mode-map>
5339 Each line in this buffer represents one article.  To read an
5340 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5341 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5342 respectively.
5343
5344 You can also post articles and send mail from this buffer.  To 
5345 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5346 of an article, type `\\[gnus-summary-reply]'.
5347
5348 There are approx. one gazillion commands you can execute in this 
5349 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5350
5351 The following commands are available:
5352
5353 \\{gnus-summary-mode-map}"
5354   (interactive)
5355   (if gnus-visual (gnus-summary-make-menu-bar))
5356   (kill-all-local-variables)
5357   (let ((locals gnus-summary-local-variables))
5358     (while locals
5359       (if (consp (car locals))
5360           (progn
5361             (make-local-variable (car (car locals)))
5362             (set (car (car locals)) (eval (cdr (car locals)))))
5363         (make-local-variable (car locals))
5364         (set (car locals) nil))
5365       (setq locals (cdr locals))))
5366   (gnus-make-thread-indent-array)
5367   (gnus-update-format-specifications)
5368   (setq mode-line-modified "-- ")
5369   (make-local-variable 'mode-line-format)
5370   (setq mode-line-format (copy-sequence mode-line-format))
5371   (and (equal (nth 3 mode-line-format) "   ")
5372        (setcar (nthcdr 3 mode-line-format) ""))
5373   (setq major-mode 'gnus-summary-mode)
5374   (setq mode-name "Summary")
5375   (make-local-variable 'minor-mode-alist)
5376   (use-local-map gnus-summary-mode-map)
5377   (buffer-disable-undo (current-buffer))
5378   (setq buffer-read-only t)             ;Disable modification
5379   (setq truncate-lines t)
5380   (setq selective-display t)
5381   (setq selective-display-ellipses t)   ;Display `...'
5382   (setq buffer-display-table gnus-summary-display-table)
5383   (run-hooks 'gnus-summary-mode-hook))
5384
5385 (defun gnus-summary-make-display-table ()
5386   ;; Change the display table.  Odd characters have a tendency to mess
5387   ;; up nicely formatted displays - we make all possible glyphs
5388   ;; display only a single character.
5389
5390   ;; We start from the standard display table, if any.
5391   (setq gnus-summary-display-table 
5392         (or (copy-sequence standard-display-table)
5393             (make-display-table)))
5394   ;; Nix out all the control chars...
5395   (let ((i 32))
5396     (while (>= (setq i (1- i)) 0)
5397       (aset gnus-summary-display-table i [??])))
5398   ;; ... but not newline and cr, of course. (cr is necessary for the
5399   ;; selective display).  
5400   (aset gnus-summary-display-table ?\n nil)
5401   (aset gnus-summary-display-table ?\r nil)
5402   ;; We nix out any glyphs over 126 that are not set already.  
5403   (let ((i 256))
5404     (while (>= (setq i (1- i)) 127)
5405       ;; Only modify if the entry is nil.
5406       (or (aref gnus-summary-display-table i) 
5407           (aset gnus-summary-display-table i [??])))))
5408
5409 (defun gnus-summary-clear-local-variables ()
5410   (let ((locals gnus-summary-local-variables))
5411     (while locals
5412       (if (consp (car locals))
5413           (and (vectorp (car (car locals)))
5414                (set (car (car locals)) nil))
5415         (and (vectorp (car locals))
5416              (set (car locals) nil)))
5417       (setq locals (cdr locals)))))
5418
5419 (defun gnus-mouse-pick-article (e)
5420   (interactive "e")
5421   (mouse-set-point e)
5422   (gnus-summary-next-page nil t))
5423
5424 (defun gnus-summary-setup-buffer (group)
5425   "Initialize summary buffer."
5426   (let ((buffer (concat "*Summary " group "*")))
5427     (if (get-buffer buffer)
5428         (progn
5429           (set-buffer buffer)
5430           (not gnus-newsgroup-begin))
5431       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5432       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5433       (gnus-add-current-to-buffer-list)
5434       (gnus-summary-mode)
5435       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5436       (setq gnus-newsgroup-name group)
5437       t)))
5438
5439 (defun gnus-set-global-variables ()
5440   ;; Set the global equivalents of the summary buffer-local variables
5441   ;; to the latest values they had. These reflect the summary buffer
5442   ;; that was in action when the last article was fetched.
5443   (if (eq major-mode 'gnus-summary-mode) 
5444       (progn
5445         (setq gnus-summary-buffer (current-buffer))
5446         (let ((name gnus-newsgroup-name)
5447               (marked gnus-newsgroup-marked)
5448               (unread gnus-newsgroup-unreads)
5449               (headers gnus-current-headers)
5450               (score-file gnus-current-score-file))
5451           (save-excursion
5452             (set-buffer gnus-group-buffer)
5453             (setq gnus-newsgroup-name name)
5454             (setq gnus-newsgroup-marked marked)
5455             (setq gnus-newsgroup-unreads unread)
5456             (setq gnus-current-headers headers)
5457             (setq gnus-current-score-file score-file))))))
5458
5459 (defun gnus-summary-insert-dummy-line (sformat subject number)
5460   (if (not sformat) 
5461       (setq sformat gnus-summary-dummy-line-format-spec))
5462   (let (b)
5463     (beginning-of-line)
5464     (setq b (point))
5465     (insert (eval sformat))
5466     (add-text-properties
5467      b (1+ b)
5468      (list 'gnus-number number 
5469            'gnus-mark gnus-dummy-mark
5470            'gnus-level 0))))
5471
5472 (defvar gnus-thread-indent-array nil)
5473 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5474 (defun gnus-make-thread-indent-array ()
5475   (let ((n 200))
5476     (if (and gnus-thread-indent-array
5477              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5478         nil
5479       (setq gnus-thread-indent-array (make-vector 201 "")
5480             gnus-thread-indent-array-level gnus-thread-indent-level)
5481       (while (>= n 0)
5482         (aset gnus-thread-indent-array n
5483               (make-string (* n gnus-thread-indent-level) ? ))
5484         (setq n (1- n))))))
5485
5486 (defun gnus-summary-insert-line 
5487   (sformat header level current unread replied expirable subject-or-nil
5488            &optional dummy score)
5489   (or sformat (setq sformat gnus-summary-line-format-spec))
5490   (let* ((indentation (aref gnus-thread-indent-array level))
5491          (lines (header-lines header))
5492          (score (or score gnus-summary-default-score 0))
5493          (score-char
5494           (if (or (null gnus-summary-default-score)
5495                   (<= (abs (- score gnus-summary-default-score))
5496                       gnus-summary-zcore-fuzz)) ? 
5497             (if (< score gnus-summary-default-score)
5498                 gnus-score-below-mark gnus-score-over-mark)))
5499          (replied (if replied gnus-replied-mark ? ))
5500          (from (header-from header))
5501          (name-address (funcall gnus-extract-address-components from))
5502          (address (car (cdr name-address)))
5503          (name (or (car name-address) (car (cdr name-address))))
5504          (subject (header-subject header))
5505          (number (header-number header))
5506          (opening-bracket (if dummy ?\< ?\[))
5507          (closing-bracket (if dummy ?\> ?\]))
5508          (buffer-read-only nil)
5509          (b (progn (beginning-of-line) (point))))
5510     (or (numberp lines) (setq lines 0))
5511     (insert (eval sformat))
5512     (add-text-properties
5513      b (1+ b) (list 'gnus-number number 
5514                     'gnus-mark (or unread gnus-unread-mark)
5515                     'gnus-level level))))
5516
5517 (defun gnus-summary-update-line (&optional dont-update)
5518   ;; Update summary line after change.
5519   (or (not gnus-summary-default-score)
5520       gnus-summary-inhibit-highlight
5521       (let ((gnus-summary-inhibit-highlight t)
5522             (article (gnus-summary-article-number)))
5523         (progn
5524           (or dont-update
5525               (if (and gnus-summary-mark-below
5526                        (< (gnus-summary-article-score)
5527                           gnus-summary-mark-below))
5528                   (and (not (memq article gnus-newsgroup-marked))
5529                        (not (memq article gnus-newsgroup-dormant))
5530                        (memq article gnus-newsgroup-unreads)
5531                        (gnus-summary-mark-article nil gnus-low-score-mark))
5532                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5533                      (gnus-summary-mark-article nil gnus-unread-mark))))
5534           (and gnus-visual
5535                (run-hooks 'gnus-summary-update-hook))))))
5536
5537 (defun gnus-summary-update-lines (&optional beg end)
5538   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5539   (let ((beg (or beg (point-min)))
5540         (end (or end (point-max))))
5541     (save-excursion
5542       (set-buffer gnus-summary-buffer)
5543       (goto-char beg)
5544       (while (and (not (eobp)) (< (point) end))
5545         (gnus-summary-update-line)
5546         (forward-line 1)))))
5547
5548 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5549   ;; Sum up all elements (and sub-elements) in a list.
5550   (let ((number 
5551          (if (listp thread) 
5552              (apply 
5553               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5554            1)))
5555     (if char 
5556         (if (> number 1) gnus-not-empty-thread-mark
5557           gnus-empty-thread-mark)
5558       number)))
5559
5560 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5561   "Start reading news in newsgroup GROUP.
5562 If SHOW-ALL is non-nil, already read articles are also listed.
5563 If NO-ARTICLE is non-nil, no article is selected initially."
5564   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5565   (let* ((new-group (gnus-summary-setup-buffer group))
5566          (did-select (and new-group (gnus-select-newsgroup group show-all)))
5567          (method (car (gnus-find-method-for-group group))))
5568     (cond 
5569      ((not new-group)
5570       (gnus-set-global-variables)
5571       (gnus-kill-buffer kill-buffer)
5572       (gnus-configure-windows 'summary)
5573       (gnus-set-mode-line 'summary)
5574       (gnus-summary-position-cursor)
5575       (message "")
5576       t)
5577      ((null did-select) 
5578       (and (eq major-mode 'gnus-summary-mode)
5579            (not (equal (current-buffer) kill-buffer))
5580            (progn
5581              (kill-buffer (current-buffer))
5582              (set-buffer gnus-group-buffer)
5583              (gnus-group-next-unread-group 1)))
5584       (message "Can't select group")
5585       nil)
5586      ((eq did-select 'quit)
5587       (and (eq major-mode 'gnus-summary-mode)
5588            (not (equal (current-buffer) kill-buffer))
5589            (kill-buffer (current-buffer)))
5590       (gnus-kill-buffer kill-buffer)
5591       (gnus-configure-windows 'group)
5592       (gnus-group-next-unread-group 1)
5593       (signal 'quit nil))
5594      (t
5595       (gnus-set-global-variables)
5596       ;; Save the active value in effect when the group was entered.
5597       (setq gnus-newsgroup-active 
5598             (gnus-copy-sequence
5599              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5600       ;; You can change the subjects in this hook.
5601       (run-hooks 'gnus-select-group-hook)
5602       ;; Do score processing.
5603       (and gnus-use-scoring (gnus-possibly-score-headers))
5604       ;; Update the format specifiers.
5605       (gnus-update-format-specifications)
5606       ;; Generate the summary buffer.
5607       (gnus-summary-prepare)
5608       (if (zerop (buffer-size))
5609           (cond (gnus-newsgroup-dormant
5610                  (gnus-summary-show-all-dormant))
5611                 ((and gnus-newsgroup-scored show-all)
5612                  (gnus-summary-show-all-expunged))))
5613       ;; Function `gnus-apply-kill-file' must be called in this hook.
5614       (run-hooks 'gnus-apply-kill-hook)
5615       (if (zerop (buffer-size))
5616           (progn
5617             ;; This newsgroup is empty.
5618             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5619             (gnus-message 6 "No unread news")
5620             (gnus-kill-buffer kill-buffer)
5621             nil)
5622         ;;(save-excursion
5623         ;;  (if kill-buffer
5624         ;;      (let ((gnus-summary-buffer kill-buffer))
5625         ;;      (gnus-configure-windows 'group))))
5626         ;; Hide conversation thread subtrees.  We cannot do this in
5627         ;; gnus-summary-prepare-hook since kill processing may not
5628         ;; work with hidden articles.
5629         (and gnus-show-threads
5630              gnus-thread-hide-subtree
5631              (gnus-summary-hide-all-threads))
5632         ;; Show first unread article if requested.
5633         (goto-char (point-min))
5634         (if (and (not no-article)
5635                  gnus-auto-select-first
5636                  (gnus-summary-first-unread-article))
5637             ()
5638           (gnus-configure-windows 'summary))
5639         (gnus-set-mode-line 'summary)
5640         (gnus-summary-position-cursor)
5641         ;; If in async mode, we send some info to the backend.
5642         (and gnus-newsgroup-async
5643              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5644              (gnus-request-asynchronous 
5645               gnus-newsgroup-name
5646               (if (and gnus-asynchronous-article-function
5647                        (fboundp gnus-asynchronous-article-function))
5648                   (funcall gnus-asynchronous-article-function
5649                            gnus-newsgroup-threads)
5650                 gnus-newsgroup-threads)))
5651         (gnus-kill-buffer kill-buffer)
5652         (if (not (get-buffer-window gnus-group-buffer))
5653             ()
5654           ;; gotta use windows, because recenter does wierd stuff if
5655           ;; the current buffer ain't the displayed window.
5656           (let ((owin (selected-window))) 
5657             (select-window (get-buffer-window gnus-group-buffer))
5658             (and (gnus-group-goto-group group)
5659                  (recenter))
5660             (select-window owin))))
5661       t))))
5662
5663 (defun gnus-summary-prepare ()
5664   ;; Generate the summary buffer.
5665   (let ((buffer-read-only nil))
5666     (erase-buffer)
5667     (gnus-summary-prepare-threads 
5668      (if gnus-show-threads
5669          (gnus-gather-threads 
5670           (gnus-sort-threads 
5671            (if (and gnus-summary-expunge-below
5672                     (not gnus-fetch-old-headers))
5673                (gnus-make-threads-and-expunge)
5674              (gnus-make-threads))))
5675        gnus-newsgroup-headers)
5676      0 nil nil t)
5677     ;; Erase header retrieval message.
5678     (gnus-summary-update-lines)
5679     (message "")
5680     ;; Remove the final newline.
5681     ;;(goto-char (point-max))
5682     ;;(delete-char -1)
5683     ;; Call hooks for modifying summary buffer.
5684     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5685     (goto-char (point-min))
5686     (run-hooks 'gnus-summary-prepare-hook)))
5687
5688 (defun gnus-subject-equal (s1 s2)
5689   (cond 
5690    ((numberp gnus-summary-gather-subject-limit)
5691     (string= (if (> (length s1) gnus-summary-gather-subject-limit)
5692                  (substring s1 0 gnus-summary-gather-subject-limit)
5693                s1)
5694              (if (> (length s2) gnus-summary-gather-subject-limit)
5695                  (substring s2 0 gnus-summary-gather-subject-limit)
5696                s2)))
5697    ((eq 'fuzzy gnus-summary-gather-subject-limit)
5698     (string= (gnus-simplify-subject-fuzzy s1)
5699              (gnus-simplify-subject-fuzzy s2)))
5700    (t
5701     (string= s1 s2))))
5702
5703 (defun gnus-gather-threads (threads)
5704   "Gather threads that have lost their roots."
5705   (if (not gnus-summary-make-false-root)
5706       threads 
5707     (let ((hashtb (gnus-make-hashtable 1023))
5708           (prev threads)
5709           (result threads)
5710           thread subject hthread whole-subject)
5711       (while threads
5712         (setq whole-subject 
5713               (setq subject (header-subject (car (car threads)))))
5714         (if gnus-summary-gather-subject-limit
5715             (or (and (numberp gnus-summary-gather-subject-limit)
5716                      (> (length subject) gnus-summary-gather-subject-limit)
5717                      (setq subject
5718                            (substring subject 0 
5719                                       gnus-summary-gather-subject-limit)))
5720                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5721                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5722           (setq subject (gnus-simplify-subject-re subject)))
5723         (if (setq hthread 
5724                   (gnus-gethash subject hashtb))
5725             (progn
5726               (or (stringp (car (car hthread)))
5727                   (setcar hthread (list whole-subject (car hthread))))
5728               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5729                                            (list (car threads))))
5730               (setcdr prev (cdr threads))
5731               (setq threads prev))
5732           (gnus-sethash subject threads hashtb))
5733         (setq prev threads)
5734         (setq threads (cdr threads)))
5735       result)))
5736
5737 (defun gnus-make-threads ()
5738   ;; This function takes the dependencies already made by 
5739   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5740   ;; through the dependecies in the hash table and finds all the
5741   ;; roots. Roots do not refer back to any valid articles.
5742   (let (roots)
5743     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5744          (gnus-build-old-threads))
5745     (mapatoms
5746      (lambda (refs)
5747        (if (not (car (symbol-value refs)))
5748            (setq roots (append (cdr (symbol-value refs)) roots))
5749          ;; Ok, these refer back to valid articles, but if
5750          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5751          ;; the root has the same subject as its children. The children
5752          ;; that do not are made into roots and removed from the list
5753          ;; of children. 
5754          (or gnus-thread-ignore-subject
5755              (let* ((prev (symbol-value refs))
5756                     (subject (gnus-simplify-subject-re 
5757                               (header-subject (car prev))))
5758                     (headers (cdr prev)))
5759                (while headers
5760                  (if (not (string= subject
5761                                    (gnus-simplify-subject-re 
5762                                     (header-subject (car headers)))))
5763                      (progn
5764                        (setq roots (cons (car headers) roots))
5765                        (setcdr prev (cdr headers)))
5766                    (setq prev headers))
5767                  (setq headers (cdr headers)))))))
5768      gnus-newsgroup-dependencies)
5769     
5770     (mapcar 'gnus-trim-thread
5771             (apply 'append
5772                    (mapcar 'gnus-cut-thread
5773                            (mapcar 'gnus-make-sub-thread roots))))))
5774   
5775 (defun gnus-make-threads-and-expunge ()
5776   ;; This function takes the dependencies already made by 
5777   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5778   ;; through the dependecies in the hash table and finds all the
5779   ;; roots. Roots do not refer back to any valid articles.
5780   (let ((default (or gnus-summary-default-score 0))
5781         (below gnus-summary-expunge-below)
5782         roots article)
5783     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5784          (gnus-build-old-threads))
5785     (mapatoms
5786      (lambda (refs)
5787        (if (not (car (symbol-value refs)))
5788            ;; These articles do not refer back to any other articles -
5789            ;; they are roots.
5790            (let ((headers (cdr (symbol-value refs))))
5791              ;; We weed out the low-scored articles.
5792              (while headers
5793                (if (not (< (or (cdr (assq (header-number (car headers))
5794                                           gnus-newsgroup-scored)) default)
5795                            below))
5796                    ;; It is over.
5797                    (setq roots (cons (car headers) roots))
5798                  ;; It is below, so we mark it as read.
5799                  (setq gnus-newsgroup-unreads
5800                        (delq (header-number (car headers))
5801                              gnus-newsgroup-unreads)))
5802                (setq headers (cdr headers))))
5803          ;; Ok, these refer back to valid articles, but if
5804          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5805          ;; the root has the same subject as its children. The children
5806          ;; that do not are made into roots and removed from the list
5807          ;; of children. 
5808          (or gnus-thread-ignore-subject
5809              (let* ((prev (symbol-value refs))
5810                     (subject (gnus-simplify-subject-re 
5811                               (header-subject (car prev))))
5812                     (headers (cdr prev)))
5813                (while headers
5814                  (if (not (string= subject
5815                                    (gnus-simplify-subject-re 
5816                                     (header-subject (car headers)))))
5817                      (progn
5818                        (if (not (< (or (cdr (assq (header-number (car headers))
5819                                                   gnus-newsgroup-scored))
5820                                        default) below))
5821                            (setq roots (cons (car headers) roots))
5822                          (setq gnus-newsgroup-unreads
5823                                (delq (header-number (car headers))
5824                                      gnus-newsgroup-unreads)))
5825                        (setcdr prev (cdr headers)))
5826                    (setq prev headers))
5827                  (setq headers (cdr headers)))))
5828          ;; If this article is expunged, some of the children might be
5829          ;; roots.  
5830          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5831                                gnus-newsgroup-scored)) default)
5832                 below)
5833              (let* ((prev (symbol-value refs))
5834                     (headers (cdr prev)))
5835                (while headers
5836                  (setq article (header-number (car headers)))
5837                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5838                                  default) below))
5839                      (progn (setq roots (cons (car headers) roots))
5840                             (setq prev headers))
5841                    (setq gnus-newsgroup-unreads 
5842                          (delq article gnus-newsgroup-unreads))
5843                    (setcdr prev (cdr headers)))
5844                  (setq headers (cdr headers))))
5845            ;; It was not expunged, but we look at expunged children.
5846            (let* ((prev (symbol-value refs))
5847                   (headers (cdr prev))
5848                   article id)
5849              (while headers
5850                (setq article (header-number (car headers)))
5851                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5852                                default) below))
5853                    (setq prev headers)
5854                  (setq gnus-newsgroup-unreads 
5855                        (delq article gnus-newsgroup-unreads))
5856                  (setcdr prev (cdr headers)))
5857                (setq headers (cdr headers)))))))
5858      gnus-newsgroup-dependencies)
5859
5860     (mapcar 'gnus-trim-thread
5861             (apply 'append
5862                    (mapcar 'gnus-cut-thread
5863                            (mapcar 'gnus-make-sub-thread roots))))))
5864   
5865 (defun gnus-cut-thread (thread)
5866   ;; Remove leaf dormant or ancient articles from THREAD.
5867   (let ((head (car thread))
5868         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5869     (if (and (null tail)
5870              (let ((number (header-number head)))
5871                (or (memq number gnus-newsgroup-ancient)
5872                    (memq number gnus-newsgroup-dormant)
5873                    (and gnus-summary-expunge-below
5874                         (eq gnus-fetch-old-headers 'some)
5875                         (< (or (cdr (assq number gnus-newsgroup-scored))
5876                                gnus-summary-default-score 0)
5877                            gnus-summary-expunge-below)
5878                         (progn
5879                           (setq gnus-newsgroup-unreads
5880                                 (delq number gnus-newsgroup-unreads))
5881                           t)))))
5882         nil
5883       (list (cons head tail)))))
5884
5885 (defun gnus-trim-thread (thread)
5886   ;; Remove root ancient articles with only one child from THREAD.
5887   (if (and (eq gnus-fetch-old-headers 'some)
5888            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5889            (= (length thread) 2))
5890       (gnus-trim-thread (nth 1 thread))
5891     thread))
5892
5893 (defun gnus-make-sub-thread (root)
5894   ;; This function makes a sub-tree for a node in the tree.
5895   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5896                                               gnus-newsgroup-dependencies)))))
5897     (cons root (mapcar 'gnus-make-sub-thread children))))
5898
5899 (defun gnus-build-old-threads ()
5900   ;; Look at all the articles that refer back to old articles, and
5901   ;; fetch the headers for the articles that aren't there. This will
5902   ;; build complete threads - if the roots haven't been expired by the
5903   ;; server, that is.
5904   (let (id heads)
5905     (mapatoms
5906      (lambda (refs)
5907        (if (not (car (symbol-value refs)))
5908            (progn
5909              (setq heads (cdr (symbol-value refs)))
5910              (while heads
5911                (if (not (memq (header-number (car heads))
5912                               gnus-newsgroup-dormant))
5913                    (progn
5914                      (setq id (symbol-name refs))
5915                      (while (and (setq id (gnus-build-get-header id))
5916                                  (not (car (gnus-gethash 
5917                                             id gnus-newsgroup-dependencies)))))
5918                      (setq heads nil))
5919                  (setq heads (cdr heads)))))))
5920      gnus-newsgroup-dependencies)))
5921
5922 (defun gnus-build-get-header (id)
5923   ;; Look through the buffer of NOV lines and find the header to
5924   ;; ID. Enter this line into the dependencies hash table, and return
5925   ;; the id of the parent article (if any).
5926   (let ((deps gnus-newsgroup-dependencies)
5927         found header)
5928     (prog1
5929         (save-excursion
5930           (set-buffer nntp-server-buffer)
5931           (goto-char (point-min))
5932           (while (and (not found) (search-forward id nil t))
5933             (beginning-of-line)
5934             (setq found (looking-at 
5935                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5936                                  (regexp-quote id))))
5937             (or found (beginning-of-line 2)))
5938           (if found
5939               (let (ref)
5940                 (beginning-of-line)
5941                 (and
5942                  (setq header (gnus-nov-parse-line 
5943                                (read (current-buffer)) deps))
5944                  (setq ref (header-references header))
5945                  (string-match "\\(<[^>]+>\\) *$" ref)
5946                  (substring ref (match-beginning 1) (match-end 1))))))
5947       (and header
5948            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5949                  gnus-newsgroup-ancient (cons (header-number header)
5950                                               gnus-newsgroup-ancient))))))
5951
5952 ;; Re-build the thread containing ID.
5953 (defun gnus-rebuild-thread (id)
5954   (let ((dep gnus-newsgroup-dependencies)
5955         (buffer-read-only nil)
5956         parent headers refs thread art)
5957     (while (and id (setq headers
5958                          (car (setq art (gnus-gethash (downcase id) dep)))))
5959       (setq parent art)
5960       (setq id (and (setq refs (header-references headers))
5961                     (string-match "\\(<[^>]+>\\) *$" refs)
5962                     (substring refs (match-beginning 1) (match-end 1)))))
5963     (setq thread (gnus-make-sub-thread (car parent)))
5964     (gnus-rebuild-remove-articles thread)
5965     (let ((beg (point)))
5966       (gnus-summary-prepare-threads (list thread) 0)
5967       (gnus-summary-update-lines beg (point)))))
5968
5969 ;; Delete all lines in the summary buffer that correspond to articles
5970 ;; in this thread.
5971 (defun gnus-rebuild-remove-articles (thread)
5972   (and (gnus-summary-goto-subject (header-number (car thread)))
5973        (gnus-delete-line))
5974   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5975
5976 (defun gnus-sort-threads (threads)
5977   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5978   (let ((fun gnus-thread-sort-functions))
5979     (while fun
5980       (setq threads (sort threads (car fun))
5981             fun (cdr fun))))
5982   threads)
5983
5984 (defun gnus-thread-header (thread)
5985   ;; Return header of first article in THREAD.
5986   (if (consp thread)
5987       (if (stringp (car thread))
5988           (car (car (cdr thread)))
5989         (car thread))
5990     thread))
5991
5992 (defun gnus-thread-sort-by-number (h1 h2)
5993   "Sort threads by root article number."
5994   (let ((h1 (gnus-thread-header h1))
5995         (h2 (gnus-thread-header h2)))
5996     (< (header-number h1) (header-number h2))))
5997
5998 (defun gnus-thread-sort-by-author (h1 h2)
5999   "Sort threads by root author."
6000   (let ((h1 (gnus-thread-header h1))
6001         (h2 (gnus-thread-header h2)))
6002     (string-lessp
6003      (let ((extract (funcall 
6004                      gnus-extract-address-components (header-from h1))))
6005        (or (car extract) (cdr extract)))
6006      (let ((extract (funcall
6007                      gnus-extract-address-components (header-from h2))))
6008        (or (car extract) (cdr extract))))))
6009
6010 (defun gnus-thread-sort-by-subject (h1 h2)
6011   "Sort threads by root subject."
6012   (let ((h1 (gnus-thread-header h1))
6013         (h2 (gnus-thread-header h2)))
6014     (string-lessp
6015      (downcase (gnus-simplify-subject (header-subject h1)))
6016      (downcase (gnus-simplify-subject (header-subject h2))))))
6017
6018 (defun gnus-thread-sort-by-date (h1 h2)
6019   "Sort threads by root article date."
6020   (let ((h1 (gnus-thread-header h1))
6021         (h2 (gnus-thread-header h2)))
6022     (string-lessp
6023      (gnus-sortable-date (header-date h1))
6024      (gnus-sortable-date (header-date h2)))))
6025
6026 (defun gnus-thread-sort-by-score (h1 h2)
6027   "Sort threads by root article score.
6028 Unscored articles will be counted as having a score of zero."
6029   (let ((h1 (gnus-thread-header h1))
6030         (h2 (gnus-thread-header h2)))
6031     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
6032           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
6033       (> (or (cdr s1) gnus-summary-default-score 0)
6034          (or (cdr s2) gnus-summary-default-score 0)))))
6035
6036 (defun gnus-thread-sort-by-total-score (h1 h2)
6037   "Sort threads by the sum of all scores in the thread.
6038 Unscored articles will be counted as having a score of zero."
6039   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6040
6041 (defun gnus-thread-total-score (thread)
6042   ;;  This function find the total score of THREAD.
6043   (if (consp thread)
6044       (if (stringp (car thread))
6045           (apply gnus-thread-score-function 0
6046                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6047         (gnus-thread-total-score-1 thread))
6048     (gnus-thread-total-score-1 (list thread))))
6049
6050 (defun gnus-thread-total-score-1 (root)
6051   ;; This function find the total score of the thread below ROOT.
6052   (setq root (car root))
6053   (apply gnus-thread-score-function
6054          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6055              gnus-summary-default-score 0)
6056          (mapcar 'gnus-thread-total-score
6057                  (cdr (gnus-gethash (downcase (header-id root))
6058                                     gnus-newsgroup-dependencies)))))
6059
6060 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6061 (defvar gnus-tmp-prev-subject "")
6062 (defvar gnus-tmp-adopt-thread nil)
6063
6064 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
6065 (defun gnus-summary-prepare-threads 
6066   (threads level &optional not-child no-subject cull)
6067   "Prepare summary buffer from THREADS and indentation LEVEL.  
6068 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6069 or a straight list of headers."
6070   (let (thread header number subject clevel)
6071     (while threads
6072       (setq thread (car threads)
6073             threads (cdr threads))
6074       ;; If `thread' is a cons, hierarchical threads are used.  If not,
6075       ;; `thread' is the header.
6076       (if (consp thread)
6077           (setq header (car thread))
6078         (setq header thread)
6079         (and cull
6080              (or (memq (setq number (header-number header))
6081                        gnus-newsgroup-dormant)
6082                  (and gnus-summary-expunge-below
6083                       (< (or (cdr (assq number gnus-newsgroup-scored))
6084                              gnus-summary-default-score 0)
6085                          gnus-summary-expunge-below)))
6086              (progn
6087                (setq header nil)
6088                (setq gnus-newsgroup-unreads 
6089                      (delq number gnus-newsgroup-unreads)))))
6090       (cond 
6091        ((stringp header)
6092         ;; The header is a dummy root.
6093         (cond ((eq gnus-summary-make-false-root 'adopt)
6094                ;; We let the first article adopt the rest.
6095                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6096                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6097                (setq thread (cdr (cdr thread)))
6098                (while thread
6099                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
6100                  (setq thread (cdr thread))))
6101               ((eq gnus-summary-make-false-root 'dummy)
6102                ;; We output a dummy root.
6103                (gnus-summary-insert-dummy-line 
6104                 nil header (header-number (car (car (cdr thread)))))
6105                (setq clevel 1))
6106               ((eq gnus-summary-make-false-root 'empty)
6107                ;; We print the articles with empty subject fields. 
6108                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6109                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6110                (setq thread (cdr (cdr thread)))
6111                (while thread
6112                  (gnus-summary-prepare-threads 
6113                   (list (car thread)) 0 nil
6114                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6115                             (not (string=  
6116                                   (gnus-simplify-subject-re 
6117                                    (header-subject (car (car thread))))
6118                                   (gnus-simplify-subject-re header))))))
6119                  (setq thread (cdr thread))))
6120               (t
6121                ;; We do not make a root for the gathered
6122                ;; sub-threads at all.  
6123                (setq clevel 0)))
6124         ;; Print the sub-threads.
6125         (and (consp thread) (cdr thread)
6126              (gnus-summary-prepare-threads (cdr thread) clevel)))
6127        ;; The header is a real article.
6128        (header
6129         (setq number (header-number header)
6130               subject (header-subject header))
6131         (and gnus-newsgroup-async
6132              (setq gnus-newsgroup-threads
6133                    (cons (cons (header-number header)
6134                                (header-lines header)) gnus-newsgroup-threads)))
6135         (gnus-summary-insert-line
6136          nil header level nil 
6137          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6138                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6139                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6140                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6141                (t gnus-ancient-mark))
6142          (memq number gnus-newsgroup-replied)
6143          (memq number gnus-newsgroup-expirable)
6144          (if no-subject 
6145              gnus-summary-same-subject
6146            (if (or (zerop level)
6147                    (and gnus-thread-ignore-subject
6148                         (not (string= 
6149                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6150                               (gnus-simplify-subject-re subject)))))
6151                subject
6152              gnus-summary-same-subject))
6153          not-child
6154          (cdr (assq number gnus-newsgroup-scored)))
6155         (setq gnus-tmp-prev-subject subject)
6156         ;; Recursively print subthreads.
6157         (and (consp thread) (cdr thread)
6158              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6159
6160 (defun gnus-select-newsgroup (group &optional read-all)
6161   "Select newsgroup GROUP.
6162 If READ-ALL is non-nil, all articles in the group are selected."
6163   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6164          (info (nth 2 entry))
6165          articles header-marks)
6166     (gnus-check-news-server
6167      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6168
6169     (or (gnus-server-opened gnus-current-select-method)
6170         (gnus-open-server gnus-current-select-method)
6171         (error "Couldn't open server"))
6172     
6173     (or (and (eq (car entry) t)
6174              (gnus-activate-newsgroup (car info)))
6175         (gnus-request-group group t)
6176         (progn
6177           (kill-buffer (current-buffer))
6178           (error "Couldn't request group %s: %s" 
6179                  group (gnus-status-message group))))
6180
6181     (setq gnus-newsgroup-name group)
6182     (setq gnus-newsgroup-unselected nil)
6183     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6184
6185     (and gnus-asynchronous
6186          (gnus-check-backend-function 
6187           'request-asynchronous gnus-newsgroup-name)
6188          (setq gnus-newsgroup-async
6189                (gnus-request-asynchronous gnus-newsgroup-name)))
6190
6191     (setq articles (gnus-articles-to-read group read-all))
6192
6193     (cond 
6194      ((null articles) 
6195       (gnus-message 3 "Couldn't select newsgroup")
6196       'quit)
6197      ((eq articles 0) nil)
6198      (t
6199       ;; Init the dependencies hash table.
6200       (setq gnus-newsgroup-dependencies 
6201             (gnus-make-hashtable (length articles)))
6202       ;; Retrieve the headers and read them in.
6203       (setq gnus-newsgroup-headers 
6204             (if (eq 'nov (setq gnus-headers-retrieved-by
6205                                (gnus-retrieve-headers 
6206                                 (if (and gnus-fetch-old-headers 
6207                                          (not (eq 1 (car articles))))
6208                                     (cons 1 articles)
6209                                   articles)
6210                                 gnus-newsgroup-name)))
6211                 (progn
6212                   (gnus-get-newsgroup-headers-xover articles))
6213               ;; If we were to fetch old headers, but the backend didn't
6214               ;; support XOVER, then it is possible we fetched one article
6215               ;; that we shouldn't have. If that's the case, we pop it off the
6216               ;; list of headers.
6217               (if (not gnus-fetch-old-headers)
6218                   ()
6219                 (save-excursion
6220                   (set-buffer nntp-server-buffer)
6221                   (goto-char (point-min))
6222                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6223                        (delete-region 
6224                         (point) 
6225                         (search-forward "\n.\n" nil t)))))
6226               (gnus-get-newsgroup-headers)))
6227       ;; Remove canceled articles from the list of unread articles.
6228       (setq gnus-newsgroup-unreads
6229             (gnus-set-sorted-intersection 
6230              gnus-newsgroup-unreads
6231              (mapcar (lambda (headers) (header-number headers))
6232                      gnus-newsgroup-headers)))
6233       ;; Adjust and set lists of article marks.
6234       (and info
6235            (let (marked)
6236              (gnus-adjust-marked-articles info)
6237              (setq gnus-newsgroup-marked 
6238                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6239              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6240              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6241              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6242              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6243              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6244              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6245              (setq gnus-newsgroup-processable nil)))
6246       ;; Check whether auto-expire is to be done in this group.
6247       (setq gnus-newsgroup-auto-expire
6248             (or (and (stringp gnus-auto-expirable-newsgroups)
6249                      (string-match gnus-auto-expirable-newsgroups group))
6250                 (memq 'auto-expire (nth 5 info))))
6251       ;; First and last article in this newsgroup.
6252       (and gnus-newsgroup-headers
6253            (setq gnus-newsgroup-begin 
6254                  (header-number (car gnus-newsgroup-headers)))
6255            (setq gnus-newsgroup-end
6256                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6257       (setq gnus-reffed-article-number -1)
6258       ;; GROUP is successfully selected.
6259       (or gnus-newsgroup-headers t)))))
6260
6261 (defun gnus-articles-to-read (group read-all)
6262   ;; Find out what articles the user wants to read.
6263   (let* ((articles
6264           ;; Select all articles if `read-all' is non-nil, or if all the
6265           ;; unread articles are dormant articles.
6266           (if (or (and read-all (not (numberp read-all)))
6267                   (= (length gnus-newsgroup-unreads) 
6268                      (length gnus-newsgroup-dormant)))
6269               (gnus-uncompress-range 
6270                (gnus-gethash group gnus-active-hashtb))
6271             gnus-newsgroup-unreads))
6272          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6273          (scored (length scored-list))
6274          (number (length articles))
6275          (marked (+ (length gnus-newsgroup-marked)
6276                     (length gnus-newsgroup-dormant)))
6277          (select
6278           (cond 
6279            ((numberp read-all)
6280             read-all)
6281            (t
6282             (condition-case ()
6283                 (cond ((and (or (<= scored marked)
6284                                 (= scored number))
6285                             (numberp gnus-large-newsgroup)
6286                             (> number gnus-large-newsgroup))
6287                        (let ((input
6288                               (read-string
6289                                (format
6290                                 "How many articles from %s (default %d): "
6291                                 gnus-newsgroup-name number))))
6292                          (if (string-equal input "")
6293                              number input)))
6294                       ((and (> scored marked) (< scored number))
6295                        (let ((input
6296                               (read-string
6297                                (format 
6298                                 "%s %s (%d scored, %d total): "
6299                                 "How many articles from"
6300                                 group scored number))))
6301                          (if (string-equal input "")
6302                              number input)))
6303                       (t number))
6304               (quit nil)))))
6305          total-articles)
6306     (setq select (if (stringp select) (string-to-number select) select))
6307     (if (or (null select) (zerop select))
6308         select
6309       (if (and (not (zerop scored)) (<= (abs select) scored))
6310           (progn
6311             (setq articles (sort scored-list '<))
6312             (setq number (length articles)))
6313         (setq articles (copy-sequence articles)))
6314
6315       (setq total-articles articles)
6316       
6317       (if (< (abs select) number)
6318           (if (< select 0) 
6319               ;; Select the N oldest articles.
6320               (setcdr (nthcdr (1- (abs select)) articles) nil)
6321             ;; Select the N most recent articles.
6322             (setq articles (nthcdr (- number select) articles))))
6323       (setq gnus-newsgroup-unselected
6324             (gnus-sorted-intersection
6325              gnus-newsgroup-unreads
6326              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6327       articles)))
6328
6329 (defun gnus-killed-articles (killed articles)
6330   (let (out)
6331     (while articles
6332       (if (inline (gnus-member-of-range (car articles) killed))
6333           (setq out (cons (car articles) out)))
6334       (setq articles (cdr articles)))
6335     out))
6336
6337 (defun gnus-adjust-marked-articles (info &optional active)
6338   "Remove all marked articles that are no longer legal."
6339   (let ((marked-lists (nth 3 info))
6340         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6341         marked m prev)
6342     ;; There are many types of marked articles.
6343     (while marked-lists
6344       (setq m (cdr (setq prev (car marked-lists))))
6345       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6346              ;; Make sure that all ticked articles are a subset of the
6347              ;; unread/unselected articles.
6348              (while m
6349                (if (or (memq (car m) gnus-newsgroup-unreads)
6350                        (memq (car m) gnus-newsgroup-unselected))
6351                    (setq prev m)
6352                  (setcdr prev (cdr m)))
6353                (setq m (cdr m))))
6354             ((eq 'score (car prev))
6355              ;; Scored articles should be a subset of
6356              ;; unread/unselected articles. 
6357              (while m
6358                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6359                        (memq (car (car m)) gnus-newsgroup-unreads))
6360                    (setq prev m)
6361                  (setcdr prev (cdr m)))
6362                (setq m (cdr m))))
6363             ((eq 'bookmark (car prev))
6364              ;; Bookmarks should be a subset of active articles.
6365              (while m
6366                (if (< (car (car m)) (car active))
6367                    (setcdr prev (cdr m))
6368                  (setq prev m))
6369                (setq m (cdr m))))
6370             ((eq 'killed (car prev))
6371              ;; Articles that have been through the kill process are
6372              ;; to be a subset of active articles.
6373              (while (and m (< (or (and (numberp (car m)) (car m))
6374                                   (cdr (car m)))
6375                               (car active)))
6376                (setcdr prev (cdr m))
6377                (setq m (cdr m)))
6378              (if (and m (< (or (and (numberp (car m)) (car m))
6379                                (car (car m)))
6380                            (car active))) 
6381                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6382             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6383              ;; The replied and expirable articles have to be articles
6384              ;; that are active. 
6385              (while m
6386                (if (< (car m) (car active))
6387                    (setcdr prev (cdr m))
6388                  (setq prev m))
6389                (setq m (cdr m)))))
6390       (setq marked-lists (cdr marked-lists)))
6391     ;; Remove all lists that are empty.
6392     (setq marked-lists (nth 3 info))
6393     (if marked-lists
6394         (progn
6395           (while (= 1 (length (car marked-lists)))
6396             (setq marked-lists (cdr marked-lists)))
6397           (setq m (cdr (setq prev marked-lists)))
6398           (while m
6399             (if (= 1 (length (car m)))
6400                 (setcdr prev (cdr m))
6401               (setq prev m))
6402             (setq m (cdr m)))
6403           (setcar (nthcdr 3 info) marked-lists)))
6404     ;; Finally, if there are no marked lists at all left, and if there
6405     ;; are no elements after the lists in the info list, we just chop
6406     ;; the info list off before the marked lists.
6407     (and (null marked-lists) 
6408          (not (nthcdr 4 info))
6409          (setcdr (nthcdr 2 info) nil)))
6410   info)
6411
6412 (defun gnus-set-marked-articles 
6413   (info ticked replied expirable killed dormant bookmark score) 
6414   "Enter the various lists of marked articles into the newsgroup info list."
6415   (let (newmarked)
6416     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6417     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6418     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6419                                          newmarked)))
6420     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6421     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6422     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6423                                         newmarked)))
6424     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6425     (if (nthcdr 3 info)
6426         (progn
6427           (setcar (nthcdr 3 info) newmarked)
6428           (and (not newmarked)
6429                (not (nthcdr 4 info))
6430                (setcdr (nthcdr 2 info) nil)))
6431       (if newmarked
6432           (setcdr (nthcdr 2 info) (list newmarked))))))
6433
6434 (defun gnus-add-marked-articles (group type articles &optional info force)
6435   ;; Add ARTICLES of TYPE to the info of GROUP.
6436   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6437   ;; add, but replace marked articles of TYPE with ARTICLES.
6438   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6439         marked m)
6440     (or (not info)
6441         (and (not (setq marked (nthcdr 3 info)))
6442              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6443         (and (not (setq m (assq type (car marked))))
6444              (setcar marked (cons (cons type articles) (car marked))))
6445         (if force
6446             (setcdr m articles)
6447           (nconc m articles)))))
6448          
6449 (defun gnus-set-mode-line (where)
6450   "This function sets the mode line of the article or summary buffers.
6451 If WHERE is `summary', the summary mode line format will be used."
6452   (if (memq where gnus-updated-mode-lines)
6453       (let (mode-string)
6454         (save-excursion
6455           (set-buffer gnus-summary-buffer)
6456           (let* ((mformat (if (eq where 'article) 
6457                               gnus-article-mode-line-format-spec
6458                             gnus-summary-mode-line-format-spec))
6459                  (group-name gnus-newsgroup-name)
6460                  (article-number (or gnus-current-article 0))
6461                  (unread (- (length gnus-newsgroup-unreads)
6462                             (length gnus-newsgroup-dormant)))
6463                  (unread-and-unticked 
6464                   (- unread (length gnus-newsgroup-marked)))
6465                  (unselected (length gnus-newsgroup-unselected))
6466                  (unread-and-unselected
6467                   (cond ((and (zerop unread-and-unticked)
6468                               (zerop unselected)) "")
6469                         ((zerop unselected) 
6470                          (format "{%d more}" unread-and-unticked))
6471                         (t (format "{%d(+%d) more}"
6472                                    unread-and-unticked unselected))))
6473                  (subject
6474                   (if gnus-current-headers
6475                       (header-subject gnus-current-headers) ""))
6476                  (max-len (and gnus-mode-non-string-length
6477                                (- (frame-width) gnus-mode-non-string-length)))
6478                  header) ;; passed as argument to any user-format-funcs
6479             (setq mode-string (eval mformat))
6480             (or (numberp max-len)
6481                 (setq max-len (length mode-string)))
6482             (if (< max-len 4) (setq max-len 4))
6483             (if (> (length mode-string) max-len)
6484                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6485                 ;;  function `substring' might cut on a middle
6486                 ;;  of multi-octet character.
6487                 (setq mode-string 
6488                       (concat (gnus-truncate-string mode-string (- max-len 3))
6489                               "...")))
6490             (setq mode-string (format (format "%%-%ds" max-len)
6491                                       mode-string))))
6492         (setq mode-line-buffer-identification mode-string)
6493         (set-buffer-modified-p t))))
6494
6495 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6496   "Go through the HEADERS list and add all Xrefs to a hash table.
6497 The resulting hash table is returned, or nil if no Xrefs were found."
6498   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6499          (prefix (if (and 
6500                       (gnus-group-foreign-p from-newsgroup)
6501                       (not (memq 'virtual 
6502                                  (assoc (symbol-name (car from-method))
6503                                         gnus-valid-select-methods))))
6504                      (gnus-group-real-prefix from-newsgroup)))
6505          (xref-hashtb (make-vector 63 0))
6506          start group entry number xrefs header)
6507     (while headers
6508       (setq header (car headers))
6509       (if (and (setq xrefs (header-xref header))
6510                (not (memq (header-number header) unreads)))
6511           (progn
6512             (setq start 0)
6513             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6514               (setq start (match-end 0))
6515               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6516                                                     (match-end 1))))
6517               (setq number 
6518                     (string-to-int (substring xrefs (match-beginning 2) 
6519                                               (match-end 2))))
6520               (if (setq entry (gnus-gethash group xref-hashtb))
6521                   (setcdr entry (cons number (cdr entry)))
6522                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6523       (setq headers (cdr headers)))
6524     (if start xref-hashtb nil)))
6525
6526 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6527   "Look through all the headers and mark the Xrefs as read."
6528   (let ((virtual (memq 'virtual 
6529                        (assoc (symbol-name (car (gnus-find-method-for-group 
6530                                                  from-newsgroup)))
6531                               gnus-valid-select-methods)))
6532         name entry read info xref-hashtb idlist active num range exps method
6533         nth4)
6534     (save-excursion
6535       (set-buffer gnus-group-buffer)
6536       (if (setq xref-hashtb 
6537                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6538           (mapatoms 
6539            (lambda (group)
6540              (if (string= from-newsgroup (setq name (symbol-name group)))
6541                  ()
6542                (setq idlist (symbol-value group))
6543                ;; Dead groups are not updated.
6544                (if (and (prog1 
6545                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6546                                   info (nth 2 entry))
6547                           (if (stringp (setq nth4 (nth 4 info)))
6548                               (setq nth4 (gnus-server-to-method nth4))))
6549                         ;; Only do the xrefs if the group has the same
6550                         ;; select method as the group we have just read.
6551                         (or (gnus-methods-equal-p 
6552                              nth4 (gnus-find-method-for-group from-newsgroup))
6553                             virtual
6554                             (equal nth4 
6555                                    (setq method (gnus-find-method-for-group 
6556                                                  from-newsgroup)))
6557                             (and (equal (car nth4) (car method))
6558                                  (equal (nth 1 nth4) (nth 1 method))))
6559                         gnus-use-cross-reference
6560                         (or (not (eq gnus-use-cross-reference t))
6561                             virtual
6562                             ;; Only do cross-references on subscribed
6563                             ;; groups, if that is what is wanted.  
6564                             (<= (nth 1 info) gnus-level-subscribed)))
6565                    (progn
6566                      (setq num 0)
6567                      ;; Set the new list of read articles in this group.
6568                      (setq active (gnus-gethash name gnus-active-hashtb))
6569                      ;; First peel off all illegal article numbers.
6570                      (if active
6571                          (let ((ids idlist)
6572                                (ticked (cdr (assq 'tick (nth 3 info))))
6573                                (dormant (cdr (assq 'dormant (nth 3 info))))
6574                                id)
6575                            (setq exps nil)
6576                            (while ids
6577                              (setq id (car ids))
6578                              (if (or (> id (cdr active))
6579                                      (< id (car active))
6580                                      (memq id ticked)
6581                                      (memq id dormant))
6582                                  (setq idlist (delq id idlist)))
6583                              (and (memq id expirable)
6584                                   (setq exps (cons id exps)))
6585                              (setq ids (cdr ids)))))
6586                      ;; Update expirable articles.
6587                      (gnus-add-marked-articles nil 'expirable exps info)
6588                      (and active
6589                           (null (nth 2 info))
6590                           (> (car active) 1)
6591                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6592                      (setcar (nthcdr 2 info)
6593                              (setq range
6594                                    (gnus-add-to-range 
6595                                     (nth 2 info) 
6596                                     (setq idlist (sort idlist '<)))))
6597                      ;; Then we have to re-compute how many unread
6598                      ;; articles there are in this group.
6599                      (if active
6600                          (progn
6601                            (cond 
6602                             ((not range)
6603                              (setq num (- (1+ (cdr active)) (car active))))
6604                             ((not (listp (cdr range)))
6605                              (setq num (- (cdr active) (- (1+ (cdr range)) 
6606                                                           (car range)))))
6607                             (t
6608                              (while range
6609                                (if (numberp (car range))
6610                                    (setq num (1+ num))
6611                                  (setq num (+ num (- (1+ (cdr (car range)))
6612                                                      (car (car range))))))
6613                                (setq range (cdr range)))
6614                              (setq num (- (cdr active) num))))
6615                            ;; Update the number of unread articles.
6616                            (setcar 
6617                             entry 
6618                             (max 0 (- num 
6619                                       (length (cdr (assq 'tick (nth 3 info))))
6620                                       (length 
6621                                        (cdr (assq 'dormant (nth 3 info)))))))
6622                            ;; Update the group buffer.
6623                            (gnus-group-update-group name t)))))))
6624            xref-hashtb)))))
6625
6626 (defun gnus-methods-equal-p (m1 m2)
6627   (let ((m1 (or m1 gnus-select-method))
6628         (m2 (or m2 gnus-select-method)))
6629     (or (equal m1 m2)
6630         (and (eq (car m1) (car m2))
6631              (or (not (memq 'address (assoc (symbol-name (car m1))
6632                                             gnus-valid-select-methods)))
6633                  (equal (nth 1 m1) (nth 1 m2)))))))
6634
6635 (defsubst gnus-header-value ()
6636   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6637
6638 (defvar gnus-newsgroup-none-id 0)
6639
6640 (defun gnus-get-newsgroup-headers ()
6641   (setq gnus-article-internal-prepare-hook nil)
6642   (let ((cur nntp-server-buffer)
6643         (dependencies gnus-newsgroup-dependencies)
6644         headers char article id dep end)
6645     (save-excursion
6646       (set-buffer nntp-server-buffer)
6647       (goto-char (point-min))
6648       ;; Search to the beginning of the next header. Error messages
6649       ;; do not begin with 2 or 3.
6650       (while (re-search-forward "^[23][0-9]+ " nil t)
6651         (let ((header (make-vector 9 nil))
6652               (c (following-char))
6653               (case-fold-search t)
6654               (p (point))
6655               from subject in-reply-to references ref)
6656           (setq id nil
6657                 ref nil
6658                 references nil
6659                 subject nil
6660                 from nil)
6661           (header-set-number header (setq article (read cur)))
6662           ;; This implementation of this function, with nine
6663           ;; search-forwards instead of the one re-search-forward and
6664           ;; a case (which basically was the old function) is actually
6665           ;; about twice as fast, even though it looks messier. You
6666           ;; can't have everything, I guess. Speed and elegance
6667           ;; doesn't always come hand in hand.
6668           (save-restriction
6669             (narrow-to-region (point) (or (save-excursion 
6670                                             (search-forward "\n.\n" nil t))
6671                                           (point)))
6672             (if (search-forward "\nfrom: " nil t)
6673                 (header-set-from header (gnus-header-value))
6674               (header-set-from header "(nobody)"))
6675             (goto-char p)
6676             (if (search-forward "\nsubject: " nil t)
6677                 (header-set-subject header (gnus-header-value))
6678               (header-set-subject header "(none)"))
6679             (goto-char p)
6680             (and (search-forward "\nxref: " nil t)
6681                  (header-set-xref header (gnus-header-value)))
6682             (goto-char p)
6683             (or (numberp (and (search-forward "\nlines: " nil t)
6684                               (header-set-lines header (read cur))))
6685                 (header-set-lines header 0))
6686             (goto-char p)
6687             (and (search-forward "\ndate: " nil t)
6688                  (header-set-date header (gnus-header-value)))
6689             (goto-char p)
6690             (if (search-forward "\nmessage-id: " nil t)
6691                 (header-set-id header (setq id (gnus-header-value)))
6692               ;; If there was no message-id, we just fake one to make
6693               ;; subsequent routines simpler.
6694               (header-set-id 
6695                header 
6696                (setq id (concat "none+" 
6697                                 (int-to-string 
6698                                  (setq gnus-newsgroup-none-id 
6699                                        (1+ gnus-newsgroup-none-id)))))))
6700             (goto-char p)
6701             (if (search-forward "\nreferences: " nil t)
6702                 (progn
6703                   (header-set-references header (gnus-header-value))
6704                   (setq end (match-end 0))
6705                   (save-excursion
6706                     (setq ref 
6707                           (downcase
6708                            (buffer-substring
6709                             (progn 
6710                               (end-of-line)
6711                               (search-backward ">" end t)
6712                               (1+ (point)))
6713                             (progn
6714                               (search-backward "<" end t)
6715                               (point)))))))
6716               ;; Get the references from the in-reply-to header if there
6717               ;; ware no references and the in-reply-to header looks
6718               ;; promising. 
6719               (if (and (search-forward "\nin-reply-to: " nil t)
6720                        (setq in-reply-to (gnus-header-value))
6721                        (string-match "<[^>]+>" in-reply-to))
6722                   (progn
6723                     (header-set-references 
6724                      header 
6725                      (setq ref (substring in-reply-to (match-beginning 0)
6726                                           (match-end 0))))
6727                     (setq ref (downcase ref)))
6728                 (setq ref "none")))
6729             ;; We do some threading while we read the headers. The
6730             ;; message-id and the last reference are both entered into
6731             ;; the same hash table. Some tippy-toeing around has to be
6732             ;; done in case an article has arrived before the article
6733             ;; which it refers to.
6734             (if (boundp (setq dep (intern (downcase id) dependencies)))
6735                 (if (car (symbol-value dep))
6736                     ;; An article with this Message-ID has already
6737                     ;; been seen, so we ignore this one, except we add
6738                     ;; any additional Xrefs (in case the two articles
6739                     ;; came from different servers.
6740                     (progn
6741                       (header-set-xref 
6742                        (car (symbol-value dep))
6743                        (concat (or (header-xref (car (symbol-value dep))) "")
6744                                (or (header-xref header) "")))
6745                       (setq header nil))
6746                   (setcar (symbol-value dep) header))
6747               (set dep (list header)))
6748             (if header
6749                 (progn
6750                   (if (boundp (setq dep (intern ref dependencies)))
6751                       (setcdr (symbol-value dep) 
6752                               (cons header (cdr (symbol-value dep))))
6753                     (set dep (list nil header)))
6754                   (setq headers (cons header headers))))
6755             (goto-char (point-max))))))
6756     (nreverse headers)))
6757
6758 ;; The following macros and functions were written by Felix Lee
6759 ;; <flee@cse.psu.edu>. 
6760
6761 (defmacro gnus-nov-read-integer ()
6762   '(prog1
6763        (if (= (following-char) ?\t)
6764            0
6765          (let ((num (condition-case nil (read buffer) (error nil))))
6766            (if (numberp num) num 0)))
6767      (or (eobp) (forward-char 1))))
6768
6769 (defmacro gnus-nov-skip-field ()
6770   '(search-forward "\t" eol 'move))
6771
6772 (defmacro gnus-nov-field ()
6773   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6774
6775 ;; Goes through the xover lines and returns a list of vectors
6776 (defun gnus-get-newsgroup-headers-xover (sequence)
6777   "Parse the news overview data in the server buffer, and return a
6778 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6779   ;; Get the Xref when the users reads the articles since most/some
6780   ;; NNTP servers do not include Xrefs when using XOVER.
6781   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6782   (let ((cur nntp-server-buffer)
6783         (dependencies gnus-newsgroup-dependencies)
6784         (none 0)
6785         number headers header)
6786     (save-excursion
6787       (set-buffer nntp-server-buffer)
6788       (goto-char (point-min))
6789       (while (and sequence (not (eobp)))
6790         (setq number (read cur))
6791         (while (and sequence (< (car sequence) number))
6792           (setq sequence (cdr sequence)))
6793         (and sequence 
6794              (eq number (car sequence))
6795              (progn
6796                (setq sequence (cdr sequence))
6797                (if (setq header 
6798                          (inline (gnus-nov-parse-line number dependencies)))
6799                    (setq headers (cons header headers)))))
6800         (forward-line 1))
6801       (setq headers (nreverse headers)))
6802     headers))
6803
6804 ;; This function has to be called with point after the article number
6805 ;; on the beginning of the line.
6806 (defun gnus-nov-parse-line (number dependencies)
6807   (let ((none 0)
6808         (eol (gnus-point-at-eol)) 
6809         (buffer (current-buffer))
6810         header ref id dep)
6811
6812     ;; overview: [num subject from date id refs chars lines misc]
6813     (narrow-to-region (point) eol)
6814     (forward-char)
6815
6816     (condition-case nil
6817         (setq header
6818               (vector 
6819                number                   ; number
6820                (gnus-nov-field)         ; subject
6821                (gnus-nov-field)         ; from
6822                (gnus-nov-field)         ; date
6823                (setq id (or (gnus-nov-field)
6824                             (concat "none+"
6825                                     (int-to-string 
6826                                      (setq none (1+ none)))))) ; id
6827                (progn
6828                  (save-excursion
6829                    (let ((beg (point)))
6830                      (search-forward "\t" eol)
6831                      (if (search-backward ">" beg t)
6832                          (setq ref 
6833                                (downcase 
6834                                 (buffer-substring 
6835                                  (1+ (point))
6836                                  (progn
6837                                    (search-backward "<" beg t)
6838                                    (point)))))
6839                        (setq ref nil))))
6840                  (gnus-nov-field))      ; refs
6841                (gnus-nov-read-integer)  ; chars
6842                (gnus-nov-read-integer)  ; lines
6843                (if (= (following-char) ?\n)
6844                    nil
6845                  (gnus-nov-field))      ; misc
6846                ))
6847       (error (progn 
6848                (ding)
6849                (message "Strange nov line.")
6850                (setq header nil)
6851                (goto-char eol))))
6852
6853     (widen)
6854
6855     ;; We build the thread tree.
6856     (and header
6857          (if (boundp (setq dep (intern (downcase id) dependencies)))
6858              (if (car (symbol-value dep))
6859                  ;; An article with this Message-ID has already been seen,
6860                  ;; so we ignore this one, except we add any additional
6861                  ;; Xrefs (in case the two articles came from different
6862                  ;; servers.
6863                  (progn
6864                    (header-set-xref 
6865                     (car (symbol-value dep))
6866                     (concat (or (header-xref (car (symbol-value dep))) "")
6867                             (or (header-xref header) "")))
6868                    (setq header nil))
6869                (setcar (symbol-value dep) header))
6870            (set dep (list header))))
6871     (if header
6872         (progn
6873           (if (boundp (setq dep (intern (or ref "none") 
6874                                         dependencies)))
6875               (setcdr (symbol-value dep) 
6876                       (cons header (cdr (symbol-value dep))))
6877             (set dep (list nil header)))))
6878     header))
6879
6880 (defun gnus-article-get-xrefs ()
6881   "Fill in the Xref value in `gnus-current-headers', if necessary.
6882 This is meant to be called in `gnus-article-internal-prepare-hook'."
6883   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6884                                  gnus-current-headers)))
6885     (or (not gnus-use-cross-reference)
6886         (not headers)
6887         (and (header-xref headers)
6888              (not (string= (header-xref headers) "")))
6889         (let ((case-fold-search t)
6890               xref)
6891           (save-restriction
6892             (gnus-narrow-to-headers)
6893             (goto-char (point-min))
6894             (if (or (and (eq (downcase (following-char)) ?x)
6895                          (looking-at "Xref:"))
6896                     (search-forward "\nXref:" nil t))
6897                 (progn
6898                   (goto-char (1+ (match-end 0)))
6899                   (setq xref (buffer-substring (point) 
6900                                                (progn (end-of-line) (point))))
6901                   (header-set-xref headers xref))))))))
6902
6903 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6904 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6905
6906 ;; Return a header specified by a NUMBER.
6907 (defun gnus-get-header-by-number (number)
6908   (save-excursion
6909     (set-buffer gnus-summary-buffer)
6910     (or gnus-newsgroup-headers-hashtb-by-number
6911         (gnus-make-headers-hashtable-by-number))
6912     (gnus-gethash (int-to-string number)
6913                   gnus-newsgroup-headers-hashtb-by-number)))
6914
6915 (defun gnus-make-headers-hashtable-by-number ()
6916   "Make hashtable for the variable gnus-newsgroup-headers by number."
6917   (save-excursion
6918     (set-buffer gnus-summary-buffer)
6919     (let ((headers gnus-newsgroup-headers)
6920           header)
6921       (setq gnus-newsgroup-headers-hashtb-by-number
6922             (gnus-make-hashtable (length headers)))
6923       (while headers
6924         (setq header (car headers))
6925         (gnus-sethash (int-to-string (header-number header))
6926                       header gnus-newsgroup-headers-hashtb-by-number)
6927         (setq headers (cdr headers))))))
6928
6929 (defun gnus-more-header-backward ()
6930   "Find new header backward."
6931   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6932         (artnum gnus-newsgroup-begin)
6933         (header nil))
6934     (while (and (not header)
6935                 (> artnum first))
6936       (setq artnum (1- artnum))
6937       (setq header (gnus-read-header artnum)))
6938     header))
6939
6940 (defun gnus-more-header-forward (&optional backward)
6941   "Find new header forward.
6942 If BACKWARD, find new header backward instead."
6943   (if backward
6944       (gnus-more-header-backward)
6945     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6946           (artnum gnus-newsgroup-end)
6947           (header nil))
6948       (while (and (not header)
6949                   (< artnum last))
6950         (setq artnum (1+ artnum))
6951         (setq header (gnus-read-header artnum)))
6952       header)))
6953
6954 (defun gnus-extend-newsgroup (header &optional backward)
6955   "Extend newsgroup selection with HEADER.
6956 Optional argument BACKWARD means extend toward backward."
6957   (if header
6958       (let ((artnum (header-number header)))
6959         (setq gnus-newsgroup-headers
6960               (if backward
6961                   (cons header gnus-newsgroup-headers)
6962                 (nconc gnus-newsgroup-headers (list header))))
6963         (setq gnus-newsgroup-unselected
6964               (delq artnum gnus-newsgroup-unselected))
6965         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6966         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6967
6968 (defun gnus-summary-work-articles (n)
6969   "Return a list of articles to be worked upon. The prefix argument,
6970 the list of process marked articles, and the current article will be
6971 taken into consideration."
6972   (let (articles)
6973     (if (and n (numberp n))
6974         (let ((backward (< n 0))
6975               (n (abs n)))
6976           (save-excursion
6977             (while (and (> n 0)
6978                         (setq articles (cons (gnus-summary-article-number) 
6979                                              articles))
6980                         (gnus-summary-search-forward nil nil backward))
6981               (setq n (1- n))))
6982           (sort articles (function <)))
6983       (or (reverse gnus-newsgroup-processable)
6984           (list (gnus-summary-article-number))))))
6985
6986 (defun gnus-summary-search-group (&optional backward use-level)
6987   "Search for next unread newsgroup.
6988 If optional argument BACKWARD is non-nil, search backward instead."
6989   (save-excursion
6990     (set-buffer gnus-group-buffer)
6991     (if (gnus-group-search-forward 
6992          backward nil (if use-level (gnus-group-group-level) nil))
6993         (gnus-group-group-name))))
6994
6995 (defun gnus-summary-best-group (&optional exclude-group)
6996   "Find the name of the best unread group.
6997 If EXCLUDE-GROUP, do not go to this group."
6998   (save-excursion
6999     (set-buffer gnus-group-buffer)
7000     (save-excursion
7001       (gnus-group-best-unread-group exclude-group))))
7002
7003 (defun gnus-summary-search-subject (&optional backward unread subject)
7004   "Search for article forward.
7005 If BACKWARD is non-nil, search backward.
7006 If UNREAD is non-nil, only unread articles are selected.
7007 If SUBJECT is non-nil, the article which has the same subject will be
7008 searched for." 
7009   (let ((func (if backward 'previous-single-property-change
7010                 'next-single-property-change))
7011         (beg (point))
7012         (did t)
7013         pos psubject)
7014     (beginning-of-line)
7015     (and gnus-summary-check-current unread
7016          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7017          (setq did nil))
7018     (if (not did)
7019         ()
7020       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7021       (while
7022           (and 
7023            (setq pos (funcall func (point) 'gnus-number))
7024            (goto-char (if backward (1- pos) pos))
7025            (setq did
7026                  (not (and
7027                        (or (not unread)
7028                            (eq (get-text-property (point) 'gnus-mark)
7029                                gnus-unread-mark))
7030                        (or (not subject)
7031                            (and (setq psubject (gnus-summary-subject-string))
7032                                 (gnus-subject-eq subject psubject))))))
7033            (if backward (if (bobp) nil (forward-char -1) t)
7034              (if (eobp) nil (forward-char 1) t)))))
7035     (if did
7036         (progn (goto-char beg) nil)
7037       (prog1
7038           (get-text-property (point) 'gnus-number)
7039         (gnus-summary-position-cursor)))))
7040
7041 (defun gnus-subject-eq (s1 s2)
7042   (cond
7043    ((null gnus-summary-gather-subject-limit)
7044     (equal (gnus-simplify-subject-re s1)
7045            (gnus-simplify-subject-re s2)))
7046    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7047     (equal (gnus-simplify-subject-fuzzy s1)
7048            (gnus-simplify-subject-fuzzy s2)))
7049    ((numberp gnus-summary-gather-subject-limit)
7050     (equal (substring s1 gnus-summary-gather-subject-limit)
7051            (substring s2 gnus-summary-gather-subject-limit)))
7052    (t
7053     (equal s1 s2))))
7054     
7055 (defun gnus-summary-search-forward (&optional unread subject backward)
7056   "Search for article forward.
7057 If UNREAD is non-nil, only unread articles are selected.
7058 If SUBJECT is non-nil, the article which has the same subject will be
7059 searched for. 
7060 If BACKWARD is non-nil, the search will be performed backwards instead."
7061   (gnus-summary-search-subject backward unread subject))
7062
7063 (defun gnus-summary-search-backward (&optional unread subject)
7064   "Search for article backward.
7065 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7066 If 2nd optional argument SUBJECT is non-nil, the article which has
7067 the same subject will be searched for."
7068   (gnus-summary-search-forward unread subject t))
7069
7070 (defun gnus-summary-article-number (&optional number-or-nil)
7071   "The article number of the article on the current line.
7072 If there isn's an article number here, then we return the current
7073 article number."
7074   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7075     (if number-or-nil number (or number gnus-current-article))))
7076
7077 (defun gnus-summary-thread-level ()
7078   "The thread level of the article on the current line."
7079   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7080       0))
7081
7082 (defun gnus-summary-pseudo-article ()
7083   "The thread level of the article on the current line."
7084   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7085
7086 (defun gnus-summary-article-mark ()
7087   "The mark on the current line."
7088   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7089
7090 (defun gnus-summary-subject-string ()
7091   "Return current subject string or nil if nothing."
7092   (let ((article (gnus-summary-article-number))
7093         header)
7094     (and article 
7095          (setq header (gnus-get-header-by-number article))
7096          (vectorp header)
7097          (header-subject header))))
7098
7099 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7100 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7101 (defun gnus-summary-article-score ()
7102   "Return current article score."
7103   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7104       gnus-summary-default-score 0))
7105
7106 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
7107
7108 (defun gnus-summary-recenter ()
7109   "Center point in the summary window.
7110 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7111 displayed, no centering will be performed." 
7112   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7113   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7114   (let* ((top (cond ((< (window-height) 4) 0)
7115                     ((< (window-height) 7) 1)
7116                     (t 2)))
7117          (height (1- (window-height)))
7118          (bottom (save-excursion (goto-char (point-max))
7119                                  (forward-line (- height))
7120                                  (point)))
7121          (window (get-buffer-window (current-buffer))))
7122     (and 
7123      ;; The user has to want it,
7124      gnus-auto-center-summary 
7125      ;; the article buffer must be displayed,
7126      (get-buffer-window gnus-article-buffer)
7127      ;; Set the window start to either `bottom', which is the biggest
7128      ;; possible valid number, or the second line from the top,
7129      ;; whichever is the least.
7130      (set-window-start
7131       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7132
7133 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7134 (defun gnus-short-group-name (group &optional levels)
7135   "Collapse GROUP name LEVELS."
7136   (let* ((name "") (foreign "") (depth -1) (skip 1)
7137          (levels (or levels
7138                      (progn
7139                        (while (string-match "\\." group skip)
7140                          (setq skip (match-end 0)
7141                                depth (+ depth 1)))
7142                        depth))))
7143     (if (string-match ":" group)
7144         (setq foreign (substring group 0 (match-end 0))
7145               group (substring group (match-end 0))))
7146     (while group
7147       (if (and (string-match "\\." group) (> levels 0))
7148           (setq name (concat name (substring group 0 1))
7149                 group (substring group (match-end 0))
7150                 levels (- levels 1)
7151                 name (concat name "."))
7152         (setq name (concat foreign name group)
7153               group nil)))
7154     name))
7155
7156 (defun gnus-summary-jump-to-group (newsgroup)
7157   "Move point to NEWSGROUP in group mode buffer."
7158   ;; Keep update point of group mode buffer if visible.
7159   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7160       (save-window-excursion
7161         ;; Take care of tree window mode.
7162         (if (get-buffer-window gnus-group-buffer)
7163             (pop-to-buffer gnus-group-buffer))
7164         (gnus-group-jump-to-group newsgroup))
7165     (save-excursion
7166       ;; Take care of tree window mode.
7167       (if (get-buffer-window gnus-group-buffer)
7168           (pop-to-buffer gnus-group-buffer)
7169         (set-buffer gnus-group-buffer))
7170       (gnus-group-jump-to-group newsgroup))))
7171
7172 ;; This function returns a list of article numbers based on the
7173 ;; difference between the ranges of read articles in this group and
7174 ;; the range of active articles.
7175 (defun gnus-list-of-unread-articles (group)
7176   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7177          (active (gnus-gethash group gnus-active-hashtb))
7178          (last (cdr active))
7179          unread first nlast unread)
7180     ;; If none are read, then all are unread. 
7181     (if (not read)
7182         (setq first (car active))
7183       ;; If the range of read articles is a single range, then the
7184       ;; first unread article is the article after the last read
7185       ;; article. Sounds logical, doesn't it?
7186       (if (not (listp (cdr read)))
7187           (setq first (1+ (cdr read)))
7188         ;; `read' is a list of ranges.
7189         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7190                                 (car (car read)))) 1)
7191             (setq first 1))
7192         (while read
7193           (if first 
7194               (while (< first nlast)
7195                 (setq unread (cons first unread))
7196                 (setq first (1+ first))))
7197           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7198           (setq nlast (if (atom (car (cdr read))) 
7199                           (car (cdr read))
7200                         (car (car (cdr read)))))
7201           (setq read (cdr read)))))
7202     ;; And add the last unread articles.
7203     (while (<= first last)
7204       (setq unread (cons first unread))
7205       (setq first (1+ first)))
7206     ;; Return the list of unread articles.
7207     (nreverse unread)))
7208
7209 (defun gnus-list-of-read-articles (group)
7210   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7211         (active (gnus-gethash group gnus-active-hashtb)))
7212     (and info active
7213          (gnus-sorted-complement 
7214           (gnus-uncompress-range active) 
7215           (gnus-list-of-unread-articles group)))))
7216
7217 ;; Various summary commands
7218
7219 (defun gnus-summary-universal-argument ()
7220   "Perform any operation on all articles marked with the process mark."
7221   (interactive)
7222   (gnus-set-global-variables)
7223   (let ((articles (reverse gnus-newsgroup-processable))
7224         key func)
7225     (or articles (error "No articles marked"))
7226     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7227         (error "Undefined key"))
7228     (while articles
7229       (gnus-summary-goto-subject (car articles))
7230       (command-execute func)
7231       (gnus-summary-remove-process-mark (car articles))
7232       (setq articles (cdr articles)))))
7233
7234 (defun gnus-summary-toggle-truncation (arg)
7235   "Toggle truncation of summary lines.
7236 With arg, turn line truncation on iff arg is positive."
7237   (interactive "P")
7238   (setq truncate-lines
7239         (if (null arg) (not truncate-lines)
7240           (> (prefix-numeric-value arg) 0)))
7241   (redraw-display))
7242
7243 (defun gnus-summary-reselect-current-group (all)
7244   "Once exit and then reselect the current newsgroup.
7245 The prefix argument ALL means to select all articles."
7246   (interactive "P")
7247   (gnus-set-global-variables)
7248   (let ((current-subject (gnus-summary-article-number))
7249         (group gnus-newsgroup-name))
7250     (setq gnus-newsgroup-begin nil)
7251     (gnus-summary-exit t)
7252     ;; We have to adjust the point of group mode buffer because the
7253     ;; current point was moved to the next unread newsgroup by
7254     ;; exiting.
7255     (gnus-summary-jump-to-group group)
7256     (gnus-group-read-group all t)
7257     (gnus-summary-goto-subject current-subject)))
7258
7259 (defun gnus-summary-rescan-group (all)
7260   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7261   (interactive "P")
7262   (gnus-set-global-variables)
7263   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7264   (let ((group gnus-newsgroup-name))
7265     (gnus-summary-exit)
7266     (gnus-summary-jump-to-group group)
7267     (save-excursion
7268       (set-buffer gnus-group-buffer)
7269       (gnus-group-get-new-news-this-group 1))
7270     (gnus-summary-jump-to-group group)
7271     (gnus-group-read-group all)))
7272
7273 (defun gnus-summary-update-info ()
7274   (let* ((group gnus-newsgroup-name)
7275          (method (car (gnus-find-method-for-group group))))
7276     (if gnus-newsgroup-kill-headers
7277         (setq gnus-newsgroup-killed
7278               (gnus-compress-sequence
7279                (nconc
7280                 (gnus-set-sorted-intersection
7281                  (gnus-uncompress-range gnus-newsgroup-killed)
7282                  (setq gnus-newsgroup-unselected
7283                        (sort gnus-newsgroup-unselected '<)))
7284                 (setq gnus-newsgroup-unreads
7285                       (sort gnus-newsgroup-unreads '<))) t)))
7286     (or (listp (cdr gnus-newsgroup-killed))
7287         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7288     (let ((updated nil)
7289           (headers gnus-newsgroup-headers))
7290       (gnus-close-group group)
7291       (run-hooks 'gnus-exit-group-hook)
7292       (gnus-update-read-articles 
7293        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7294        gnus-newsgroup-marked
7295        t gnus-newsgroup-replied gnus-newsgroup-expirable
7296        gnus-newsgroup-killed gnus-newsgroup-dormant
7297        gnus-newsgroup-bookmarks 
7298        (and gnus-save-score gnus-newsgroup-scored))
7299       (and gnus-use-cross-reference
7300            (gnus-mark-xrefs-as-read 
7301             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7302       ;; Do adaptive scoring, and possibly save score files.
7303       (and gnus-newsgroup-adaptive
7304            (gnus-score-adaptive))
7305       (and gnus-use-scoring 
7306            (fboundp 'gnus-score-save)
7307            (funcall 'gnus-score-save))
7308       ;; Do not switch windows but change the buffer to work.
7309       (set-buffer gnus-group-buffer)
7310       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7311           (gnus-group-update-group group)))))
7312   
7313 (defun gnus-summary-exit (&optional temporary)
7314   "Exit reading current newsgroup, and then return to group selection mode.
7315 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7316   (interactive)
7317   (gnus-set-global-variables)
7318   (gnus-kill-save-kill-buffer)
7319   (let* ((group gnus-newsgroup-name)
7320          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7321                                                   gnus-newsgroup-name))))
7322          (mode major-mode)
7323          (method (car (gnus-find-method-for-group group)))
7324          (buf (current-buffer)))
7325     (gnus-summary-update-info) ; Make all changes in this group permanent.
7326     ;; Make sure where I was, and go to next newsgroup.
7327     (or quit-config
7328         (progn
7329           (gnus-group-jump-to-group group)
7330           (gnus-group-next-unread-group 1)))
7331     (if temporary
7332         nil                             ;Nothing to do.
7333       (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7334       ;; We set all buffer-local variables to nil. It is unclear why
7335       ;; this is needed, but if we don't, buffer-local variables are
7336       ;; not garbage-collected, it seems. This would the lead to en
7337       ;; ever-growing Emacs.
7338       (set-buffer buf)
7339       (gnus-summary-clear-local-variables)
7340       ;; We clear the global counterparts of the buffer-local
7341       ;; variables as well, just to be on the safe side.
7342       (gnus-configure-windows 'group)
7343       (gnus-summary-clear-local-variables)
7344       ;; Return to group mode buffer. 
7345       (if (eq mode 'gnus-summary-mode)
7346           (gnus-kill-buffer buf))
7347       (if (get-buffer gnus-article-buffer)
7348           (bury-buffer gnus-article-buffer))
7349       (setq gnus-current-select-method gnus-select-method)
7350       (pop-to-buffer gnus-group-buffer)
7351       (if (not quit-config)
7352           (progn
7353             (gnus-group-jump-to-group group)
7354             (gnus-group-next-unread-group 1))
7355         (if (not (buffer-name (car quit-config)))
7356             (gnus-configure-windows 'group)
7357           (set-buffer (car quit-config))
7358           (and (eq major-mode 'gnus-summary-mode)
7359                (gnus-set-global-variables))
7360           (gnus-configure-windows (cdr quit-config)))))))
7361
7362 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7363 (defun gnus-summary-exit-no-update (&optional no-questions)
7364   "Quit reading current newsgroup without updating read article info."
7365   (interactive)
7366   (let* ((group gnus-newsgroup-name)
7367          (quit-config (nth 1 (assoc 'quit-config 
7368                                     (gnus-find-method-for-group group)))))
7369     (if (or no-questions
7370             gnus-expert-user
7371             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7372         (progn
7373           (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7374           (gnus-close-group group)
7375           (gnus-summary-clear-local-variables)
7376           (set-buffer gnus-group-buffer)
7377           (gnus-summary-clear-local-variables)
7378           ;; Return to group selection mode.
7379           (gnus-configure-windows 'group)
7380           (if (get-buffer gnus-summary-buffer)
7381               (kill-buffer gnus-summary-buffer))
7382           (if (get-buffer gnus-article-buffer)
7383               (bury-buffer gnus-article-buffer))
7384           (if (equal (gnus-group-group-name) group)
7385               (gnus-group-next-unread-group 1))
7386           (if quit-config
7387               (progn
7388                 (if (not (buffer-name (car quit-config)))
7389                     (gnus-configure-windows 'group)
7390                   (set-buffer (car quit-config))
7391                   (and (eq major-mode 'gnus-summary-mode)
7392                        (gnus-set-global-variables))
7393                   (gnus-configure-windows (cdr quit-config)))))))))
7394
7395 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7396 (defun gnus-summary-fetch-faq (group)
7397   "Fetch the FAQ for the current group."
7398   (interactive (list gnus-newsgroup-name))
7399   (gnus-configure-windows 'summary-faq)
7400   (find-file (concat gnus-group-faq-directory group)))
7401
7402 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7403 (defun gnus-summary-describe-group (force)
7404   "Describe the current newsgroup."
7405   (interactive "P")
7406   (gnus-group-describe-group force gnus-newsgroup-name))
7407
7408 (defun gnus-summary-describe-briefly ()
7409   "Describe summary mode commands briefly."
7410   (interactive)
7411   (gnus-message 6
7412     (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")))
7413
7414 ;; Walking around group mode buffer from summary mode.
7415
7416 (defun gnus-summary-next-group (&optional no-article group backward)
7417   "Exit current newsgroup and then select next unread newsgroup.
7418 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7419 If BACKWARD, go to previous group instead."
7420   (interactive "P")
7421   (gnus-set-global-variables)
7422   (let ((ingroup gnus-newsgroup-name)
7423         (sumbuf (current-buffer))
7424         num)
7425     (set-buffer gnus-group-buffer)
7426     (if (and group
7427              (or (and (numberp (setq num (car (gnus-gethash
7428                                                group gnus-newsrc-hashtb))))
7429                       (< num 1))
7430                  (null num)))
7431         (progn
7432           (gnus-group-jump-to-group group)
7433           (setq group nil))
7434       (gnus-group-jump-to-group ingroup))
7435     (gnus-summary-search-group backward)
7436     (let ((group (or group (gnus-summary-search-group backward)))
7437           (buf gnus-summary-buffer))
7438       (set-buffer sumbuf)
7439       (gnus-summary-exit t)             ;Update all information.
7440       (if (null group)
7441           (gnus-summary-exit-no-update t)
7442         (gnus-message 5 "Selecting %s..." group)
7443         (set-buffer gnus-group-buffer)
7444         ;; We are now in group mode buffer.
7445         ;; Make sure group mode buffer point is on GROUP.
7446         (gnus-group-jump-to-group group)
7447         (if (not (eq gnus-auto-select-next 'quietly))
7448             (progn
7449               (gnus-summary-read-group group nil no-article buf)
7450               (and (string= gnus-newsgroup-name ingroup)
7451                    (bufferp sumbuf) (buffer-name sumbuf)
7452                    (progn
7453                      (set-buffer (setq gnus-summary-buffer sumbuf))
7454                      (gnus-summary-exit-no-update t))))
7455           (let ((prevgroup group))
7456             (gnus-summary-read-group group nil no-article buf)
7457             (while (and (string= gnus-newsgroup-name ingroup)
7458                         (bufferp sumbuf) 
7459                         (buffer-name sumbuf)
7460                         (not (string= prevgroup (gnus-group-group-name))))
7461               (set-buffer gnus-group-buffer)
7462               (gnus-summary-read-group 
7463                (setq prevgroup (gnus-group-group-name)) 
7464                nil no-article buf))
7465             (and (string= prevgroup (gnus-group-group-name))
7466                  ;; We have reached the final group in the group
7467                  ;; buffer.
7468                  (progn
7469                    (if (buffer-name sumbuf)
7470                        (progn
7471                          (set-buffer sumbuf)
7472                          (gnus-summary-exit)))))))))))
7473
7474 (defun gnus-summary-prev-group (no-article)
7475   "Exit current newsgroup and then select previous unread newsgroup.
7476 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7477   (interactive "P")
7478   (gnus-summary-next-group no-article nil t))
7479
7480 ;; Walking around summary lines.
7481
7482 (defun gnus-summary-first-subject (unread)
7483   "Go to the first unread subject.
7484 If UNREAD is non-nil, go to the first unread article.
7485 Returns nil if there are no unread articles."
7486   (interactive "P")
7487   (prog1
7488       (cond ((not unread)
7489              (goto-char (point-min)))
7490             ((gnus-goto-char 
7491               (text-property-any 
7492                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7493              t)
7494             (t 
7495              ;; There are no unread articles.
7496              (gnus-message 3 "No more unread articles")
7497              nil))
7498     (gnus-summary-position-cursor)))
7499
7500 (defun gnus-summary-next-subject (n &optional unread dont-display)
7501   "Go to next N'th summary line.
7502 If N is negative, go to the previous N'th subject line.
7503 If UNREAD is non-nil, only unread articles are selected.
7504 The difference between N and the actual number of steps taken is
7505 returned."
7506   (interactive "p")
7507   (let ((backward (< n 0))
7508         (n (abs n)))
7509     (while (and (> n 0)
7510                 (gnus-summary-search-forward unread nil backward))
7511       (setq n (1- n)))
7512     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7513                                (if unread " unread" "")))
7514     (or dont-display
7515         (progn
7516           (gnus-summary-recenter)
7517           (gnus-summary-position-cursor)))
7518   n))
7519
7520 (defun gnus-summary-next-unread-subject (n)
7521   "Go to next N'th unread summary line."
7522   (interactive "p")
7523   (gnus-summary-next-subject n t))
7524
7525 (defun gnus-summary-prev-subject (n &optional unread)
7526   "Go to previous N'th summary line.
7527 If optional argument UNREAD is non-nil, only unread article is selected."
7528   (interactive "p")
7529   (gnus-summary-next-subject (- n) unread))
7530
7531 (defun gnus-summary-prev-unread-subject (n)
7532   "Go to previous N'th unread summary line."
7533   (interactive "p")
7534   (gnus-summary-next-subject (- n) t))
7535
7536 (defun gnus-summary-goto-subject (article)
7537   "Go the subject line of ARTICLE."
7538   (interactive
7539    (list
7540     (string-to-int
7541      (completing-read "Article number: "
7542                       (mapcar
7543                        (lambda (headers)
7544                          (list
7545                           (int-to-string (header-number headers))))
7546                        gnus-newsgroup-headers)
7547                       nil 'require-match))))
7548   (or article (error "No article number"))
7549   (let ((b (point)))
7550     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7551                                                 'gnus-number article)))
7552         ()
7553       (gnus-summary-show-thread)
7554       ;; Skip dummy articles. 
7555       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7556           (forward-line 1))
7557       (prog1
7558           (if (not (eobp))
7559               article
7560             (goto-char b)
7561             nil)
7562         (gnus-summary-position-cursor)))))
7563
7564 ;; Walking around summary lines with displaying articles.
7565
7566 (defun gnus-summary-expand-window ()
7567   "Make the summary buffer take up the entire Emacs frame."
7568   (interactive)
7569   (gnus-set-global-variables)
7570   (gnus-configure-windows 'summary))
7571
7572 (defun gnus-summary-display-article (article &optional all-header)
7573   "Display ARTICLE in article buffer."
7574   (gnus-set-global-variables)
7575   (if (null article)
7576       nil
7577     (gnus-article-prepare article all-header)
7578     (gnus-summary-show-thread)
7579     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7580         (progn
7581           (forward-line 1)
7582           (gnus-summary-position-cursor)))
7583     (run-hooks 'gnus-select-article-hook)
7584     (gnus-summary-recenter)
7585 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
7586 ;    (sit-for 0)
7587     (gnus-summary-goto-subject article)
7588     ;; Successfully display article.
7589     (gnus-summary-update-line)
7590     (gnus-article-set-window-start 
7591      (cdr (assq article gnus-newsgroup-bookmarks)))
7592     t))
7593
7594 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7595   "Select the current article.
7596 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7597 non-nil, the article will be re-fetched even if it already present in
7598 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7599 be displayed."
7600   (and (not pseudo) (gnus-summary-pseudo-article)
7601        (error "This is a pseudo-article."))
7602   (let ((article (or article (gnus-summary-article-number)))
7603         (all-headers (not (not all-headers))) ;Must be T or NIL.
7604         did) 
7605     (prog1
7606         (save-excursion
7607           (set-buffer gnus-summary-buffer)
7608           (if (or (null gnus-current-article)
7609                   (null gnus-article-current)
7610                   (not (eq article (cdr gnus-article-current)))
7611                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7612                   force)
7613               ;; The requested article is different from the current article.
7614               (progn
7615                 (gnus-summary-display-article article all-headers)
7616                 (setq did article))
7617             (if all-headers (gnus-article-show-all-headers))
7618             nil))
7619       (if did 
7620           (gnus-article-set-window-start 
7621            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7622
7623 (defun gnus-summary-set-current-mark (&optional current-mark)
7624   "Obsolete function."
7625   nil)
7626
7627 (defun gnus-summary-next-article (unread &optional subject backward)
7628   "Select the next article.
7629 If UNREAD, only unread articles are selected.
7630 If SUBJECT, only articles with SUBJECT are selected.
7631 If BACKWARD, the previous article is selected instead of the next."
7632   (interactive "P")
7633   (gnus-set-global-variables)
7634   (let ((opoint (point))
7635         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
7636         header)
7637     (cond
7638      ;; Is there such an article?
7639      ((gnus-summary-display-article 
7640        (gnus-summary-search-forward unread subject backward))
7641       (gnus-summary-position-cursor))
7642      ;; If not, we try the first unread, if that is wanted.
7643      ((and subject
7644            gnus-auto-select-same
7645            (gnus-summary-first-unread-article))
7646       (gnus-message 6 "Wrapped"))
7647      ;; Try to get next/previous article not displayed in this group.
7648      ((and gnus-auto-extend-newsgroup
7649            (not unread) (not subject)
7650            (setq header (gnus-more-header-forward backward)))
7651       (gnus-extend-newsgroup header backward)
7652       (let ((buffer-read-only nil))
7653         (goto-char (if backward (point-min) (point-max)))
7654         (gnus-summary-prepare-threads (list header) 0))
7655       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7656                                    gnus-newsgroup-end)))
7657      ;; Go to next/previous group.
7658      (t
7659       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7660           (gnus-summary-jump-to-group gnus-newsgroup-name))
7661       (let ((cmd (aref (this-command-keys) 0))
7662             (group 
7663              (if (eq gnus-keep-same-level 'best) 
7664                  (gnus-summary-best-group gnus-newsgroup-name)
7665                (gnus-summary-search-group backward gnus-keep-same-level))))
7666         ;; For some reason, the group window gets selected. We change
7667         ;; it back.  
7668         (select-window (get-buffer-window (current-buffer)))
7669         ;; Keep just the event type of CMD.
7670         (and (listp cmd) (setq cmd (car cmd)))
7671         ;; Select next unread newsgroup automagically.
7672         (cond 
7673          ((not gnus-auto-select-next)
7674           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7675          ((eq gnus-auto-select-next 'quietly)
7676           ;; Select quietly.
7677           (if (assoc 'quit-config (gnus-find-method-for-group 
7678                                    gnus-newsgroup-name))
7679               (gnus-summary-exit)
7680             (gnus-message 7 "No more%s articles (%s)..."
7681                           (if unread " unread" "") 
7682                           (if group (concat "selecting " group)
7683                             "exiting"))
7684             (gnus-summary-next-group nil group backward)))
7685          (t
7686           (let ((keystrokes '(?\C-n ?\C-p))
7687                 key)
7688             (while (or (null key) (memq key keystrokes))
7689               (gnus-message 
7690                7 "No more%s articles%s" (if unread " unread" "")
7691                (if (and group (not (assoc 'quit-config
7692                                           (gnus-find-method-for-group 
7693                                            gnus-newsgroup-name))))
7694                    (format " (Type %s for %s [%s])"
7695                            (single-key-description cmd) group
7696                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7697                  (format " (Type %s to exit %s)"
7698                          (single-key-description cmd)
7699                          gnus-newsgroup-name)))
7700               ;; Confirm auto selection.
7701               (let* ((event (read-event)))
7702                 (setq key (if (listp event) (car event) event))
7703                 (if (memq key keystrokes)
7704                     (let ((obuf (current-buffer)))
7705                       (switch-to-buffer gnus-group-buffer)
7706                       (gnus-group-jump-to-group group)
7707                       (execute-kbd-macro (char-to-string key))
7708                       (setq group (gnus-group-group-name))
7709                       (switch-to-buffer obuf)))))
7710             (if (equal key cmd)
7711                 (if (or (not group) (assoc 'quit-config
7712                                            (gnus-find-method-for-group
7713                                             gnus-newsgroup-name)))
7714                     (gnus-summary-exit)
7715                   (gnus-summary-next-group nil group backward))
7716               (setq unread-command-events (list key)))))))))))
7717
7718 (defun gnus-summary-next-unread-article ()
7719   "Select unread article after current one."
7720   (interactive)
7721   (gnus-summary-next-article t (and gnus-auto-select-same
7722                                     (gnus-summary-subject-string))))
7723
7724 (defun gnus-summary-prev-article (unread &optional subject)
7725   "Select the article after the current one.
7726 If UNREAD is non-nil, only unread articles are selected."
7727   (interactive "P")
7728   (gnus-summary-next-article unread subject t))
7729
7730 (defun gnus-summary-prev-unread-article ()
7731   "Select unred article before current one."
7732   (interactive)
7733   (gnus-summary-prev-article t (and gnus-auto-select-same
7734                                     (gnus-summary-subject-string))))
7735
7736 (defun gnus-summary-next-page (lines &optional circular)
7737   "Show next page of selected article.
7738 If end of article, select next article.
7739 Argument LINES specifies lines to be scrolled up.
7740 If CIRCULAR is non-nil, go to the start of the article instead of 
7741 instead of selecting the next article when reaching the end of the
7742 current article." 
7743   (interactive "P")
7744   (setq gnus-summary-buffer (current-buffer))
7745   (gnus-set-global-variables)
7746   (let ((article (gnus-summary-article-number))
7747         (endp nil))
7748     (gnus-configure-windows 'article)
7749     (if (or (null gnus-current-article)
7750             (null gnus-article-current)
7751             (/= article (cdr gnus-article-current))
7752             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7753         ;; Selected subject is different from current article's.
7754         (gnus-summary-display-article article)
7755       (gnus-eval-in-buffer-window
7756        gnus-article-buffer
7757        (setq endp (gnus-article-next-page lines)))
7758       (if endp
7759           (cond (circular
7760                  (gnus-summary-beginning-of-article))
7761                 (lines
7762                  (gnus-message 3 "End of message"))
7763                 ((null lines)
7764                  (gnus-summary-next-unread-article)))))
7765     (gnus-summary-recenter)
7766     (gnus-summary-position-cursor)))
7767
7768 (defun gnus-summary-prev-page (lines)
7769   "Show previous page of selected article.
7770 Argument LINES specifies lines to be scrolled down."
7771   (interactive "P")
7772   (gnus-set-global-variables)
7773   (let ((article (gnus-summary-article-number)))
7774     (gnus-configure-windows 'article)
7775     (if (or (null gnus-current-article)
7776             (null gnus-article-current)
7777             (/= article (cdr gnus-article-current))
7778             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7779         ;; Selected subject is different from current article's.
7780         (gnus-summary-display-article article)
7781       (gnus-summary-recenter)
7782       (gnus-eval-in-buffer-window gnus-article-buffer
7783         (gnus-article-prev-page lines))))
7784   (gnus-summary-position-cursor))
7785
7786 (defun gnus-summary-scroll-up (lines)
7787   "Scroll up (or down) one line current article.
7788 Argument LINES specifies lines to be scrolled up (or down if negative)."
7789   (interactive "p")
7790   (gnus-set-global-variables)
7791   (gnus-configure-windows 'article)
7792   (or (gnus-summary-select-article nil nil 'pseudo)
7793       (gnus-eval-in-buffer-window 
7794        gnus-article-buffer
7795        (cond ((> lines 0)
7796               (if (gnus-article-next-page lines)
7797                   (gnus-message 3 "End of message")))
7798              ((< lines 0)
7799               (gnus-article-prev-page (- lines))))))
7800   (gnus-summary-recenter)
7801   (gnus-summary-position-cursor))
7802
7803 (defun gnus-summary-next-same-subject ()
7804   "Select next article which has the same subject as current one."
7805   (interactive)
7806   (gnus-set-global-variables)
7807   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7808
7809 (defun gnus-summary-prev-same-subject ()
7810   "Select previous article which has the same subject as current one."
7811   (interactive)
7812   (gnus-set-global-variables)
7813   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7814
7815 (defun gnus-summary-next-unread-same-subject ()
7816   "Select next unread article which has the same subject as current one."
7817   (interactive)
7818   (gnus-set-global-variables)
7819   (gnus-summary-next-article t (gnus-summary-subject-string)))
7820
7821 (defun gnus-summary-prev-unread-same-subject ()
7822   "Select previous unread article which has the same subject as current one."
7823   (interactive)
7824   (gnus-set-global-variables)
7825   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7826
7827 (defun gnus-summary-first-unread-article ()
7828   "Select the first unread article. 
7829 Return nil if there are no unread articles."
7830   (interactive)
7831   (gnus-set-global-variables)
7832   (prog1
7833       (if (gnus-summary-first-subject t)
7834           (gnus-summary-display-article (gnus-summary-article-number)))
7835     (gnus-summary-position-cursor)))
7836
7837 (defun gnus-summary-best-unread-article ()
7838   "Select the unread article with the highest score."
7839   (interactive)
7840   (gnus-set-global-variables)
7841   (let ((scored gnus-newsgroup-scored)
7842         (best -1000000)
7843         article art)
7844     (while scored
7845       (or (> best (cdr (car scored)))
7846           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7847                (not (memq art gnus-newsgroup-marked))
7848                (not (memq art gnus-newsgroup-dormant))
7849                (if (= best (cdr (car scored)))
7850                    (setq article (min art article))
7851                  (setq article art)
7852                  (setq best (cdr (car scored))))))
7853       (setq scored (cdr scored)))
7854     (if article 
7855         (gnus-summary-goto-article article)
7856       (gnus-summary-first-unread-article))
7857     (gnus-summary-position-cursor)))
7858
7859 (defun gnus-summary-goto-article (article &optional all-headers)
7860   "Fetch ARTICLE and display it if it exists.
7861 If ALL-HEADERS is non-nil, no header lines are hidden."
7862   (interactive
7863    (list
7864     (string-to-int
7865      (completing-read 
7866       "Article number: "
7867       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7868               gnus-newsgroup-headers) 
7869       nil 'require-match))))
7870   (prog1
7871       (and (gnus-summary-goto-subject article)
7872            (gnus-summary-display-article article all-headers))
7873     (gnus-summary-position-cursor)))
7874
7875 (defun gnus-summary-goto-last-article ()
7876   "Go to the last article."
7877   (interactive)
7878   (prog1
7879       (and gnus-last-article
7880            (gnus-summary-goto-article gnus-last-article))
7881     (gnus-summary-position-cursor)))
7882
7883 (defun gnus-summary-pop-article (number)
7884   "Pop one article off the history and go to the previous.
7885 NUMBER articles will be popped off."
7886   (interactive "p")
7887   (let (to)
7888     (setq gnus-newsgroup-history
7889           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7890     (if to
7891         (gnus-summary-goto-article (car to))
7892       (error "Article history empty")))
7893   (gnus-summary-position-cursor))
7894
7895 ;; Summary article oriented commands
7896
7897 (defun gnus-summary-refer-parent-article (n)
7898   "Refer parent article N times.
7899 The difference between N and the number of articles fetched is returned."
7900   (interactive "p")
7901   (gnus-set-global-variables)
7902   (while 
7903       (and 
7904        (> n 0)
7905        (let ((ref (header-references (gnus-get-header-by-number
7906                                       (gnus-summary-article-number)))))
7907          (if (and ref (not (equal ref ""))
7908                   (string-match "<[^<>]*>[ \t]*$" ref))
7909              (gnus-summary-refer-article 
7910               (substring ref (match-beginning 0) (match-end 0)))
7911            (gnus-message 1 "No references in article %d"
7912                          (gnus-summary-article-number))
7913            nil)))
7914     (setq n (1- n)))
7915   (gnus-summary-position-cursor)
7916   n)
7917     
7918 (defun gnus-summary-refer-article (message-id)
7919   "Refer article specified by MESSAGE-ID.
7920 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7921   (interactive "sMessage-ID: ")
7922   (if (or (not (stringp message-id))
7923           (zerop (length message-id)))
7924       ()
7925     ;; Construct the correct Message-ID if necessary.
7926     ;; Suggested by tale@pawl.rpi.edu.
7927     (or (string-match "^<" message-id)
7928         (setq message-id (concat "<" message-id)))
7929     (or (string-match ">$" message-id)
7930         (setq message-id (concat message-id ">")))
7931     (let ((header (car (gnus-gethash (downcase message-id)
7932                                      gnus-newsgroup-dependencies))))
7933       (if header
7934           (or (gnus-summary-goto-article (header-number header))
7935               ;; The header has been read, but the article had been
7936               ;; expunged, so we insert it again.
7937               (progn
7938                 (gnus-summary-insert-line
7939                  nil header 0 nil gnus-read-mark nil nil
7940                  (header-subject header))
7941                 (forward-line -1)
7942                 (header-number header)))
7943         (let ((gnus-override-method gnus-refer-article-method)
7944               (gnus-ancient-mark gnus-read-mark)
7945               (tmp-buf (get-buffer-create " *gnus refer"))
7946               (tmp-point (window-start
7947                           (get-buffer-window gnus-article-buffer)))
7948               number)
7949           (and gnus-refer-article-method
7950                (or (gnus-server-opened gnus-refer-article-method)
7951                    (gnus-open-server gnus-refer-article-method)))
7952           ;; Save the old article buffer.
7953           (save-excursion
7954             (set-buffer tmp-buf)
7955             (buffer-disable-undo (current-buffer))
7956             (insert-buffer-substring gnus-article-buffer))
7957           (prog1
7958               (if (gnus-article-prepare 
7959                    message-id nil (gnus-read-header message-id))
7960                   (progn
7961                     (setq number (header-number gnus-current-headers))
7962                     (gnus-rebuild-thread message-id)
7963                     (gnus-summary-goto-subject number)
7964                     (gnus-summary-recenter)
7965                     (gnus-article-set-window-start 
7966                      (cdr (assq number gnus-newsgroup-bookmarks)))
7967                     message-id)
7968                 ;; We restore the old article buffer.
7969                 (save-excursion
7970                   (set-buffer gnus-article-buffer)
7971                   (let ((buffer-read-only nil))
7972                     (insert-buffer-substring tmp-buf)
7973                     (and tmp-point
7974                          (set-window-start (get-buffer-window (current-buffer))
7975                                            tmp-point))))
7976                 nil)
7977             (kill-buffer tmp-buf)))))))
7978
7979 (defun gnus-summary-enter-digest-group ()
7980   "Enter a digest group based on the current article."
7981   (interactive)
7982   (gnus-set-global-variables)
7983   (gnus-summary-select-article)
7984   ;; We do not want a narrowed article.
7985   (gnus-summary-stop-page-breaking)
7986   (let ((name (format "%s-%d" 
7987                       (gnus-group-prefixed-name 
7988                        gnus-newsgroup-name (list 'nndoc "")) 
7989                       gnus-current-article))
7990         (ogroup gnus-newsgroup-name)
7991         (buf (current-buffer)))
7992     (if (gnus-group-read-ephemeral-group 
7993          name (list 'nndoc name
7994                     (list 'nndoc-address (get-buffer gnus-article-buffer))
7995                     '(nndoc-article-type digest))
7996          t)
7997         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
7998                 (list (list (cons 'to-group ogroup))))
7999       (switch-to-buffer buf)
8000       (gnus-set-global-variables)
8001       (gnus-configure-windows 'summary)
8002       (gnus-message 3 "Article not a digest?"))))
8003
8004 (defun gnus-summary-isearch-article ()
8005   "Do incremental search forward on current article."
8006   (interactive)
8007   (gnus-set-global-variables)
8008   (gnus-summary-select-article)
8009   (gnus-eval-in-buffer-window 
8010    gnus-article-buffer (isearch-forward)))
8011
8012 (defun gnus-summary-search-article-forward (regexp &optional backward)
8013   "Search for an article containing REGEXP forward.
8014 If BACKWARD, search backward instead."
8015   (interactive
8016    (list (read-string
8017           (format "Search article %s (regexp%s): "
8018                   (if current-prefix-arg "backward" "forward")
8019                   (if gnus-last-search-regexp
8020                       (concat ", default " gnus-last-search-regexp)
8021                     "")))
8022          current-prefix-arg))
8023   (gnus-set-global-variables)
8024   (if (string-equal regexp "")
8025       (setq regexp (or gnus-last-search-regexp ""))
8026     (setq gnus-last-search-regexp regexp))
8027   (if (gnus-summary-search-article regexp backward)
8028       (gnus-article-set-window-start 
8029        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8030     (error "Search failed: \"%s\"" regexp)))
8031
8032 (defun gnus-summary-search-article-backward (regexp)
8033   "Search for an article containing REGEXP backward."
8034   (interactive
8035    (list (read-string
8036           (format "Search article backward (regexp%s): "
8037                   (if gnus-last-search-regexp
8038                       (concat ", default " gnus-last-search-regexp)
8039                     "")))))
8040   (gnus-summary-search-article-forward regexp 'backward))
8041
8042 (defun gnus-summary-search-article (regexp &optional backward)
8043   "Search for an article containing REGEXP.
8044 Optional argument BACKWARD means do search for backward.
8045 gnus-select-article-hook is not called during the search."
8046   (let ((gnus-select-article-hook nil)  ;Disable hook.
8047         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8048         (re-search
8049          (if backward
8050              (function re-search-backward) (function re-search-forward)))
8051         (found nil)
8052         (last nil))
8053     ;; Hidden thread subtrees must be searched for ,too.
8054     (gnus-summary-show-all-threads)
8055     (if (eobp) (forward-line -1))
8056     ;; First of all, search current article.
8057     ;; We don't want to read article again from NNTP server nor reset
8058     ;; current point.
8059     (gnus-summary-select-article)
8060     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8061     (setq last gnus-current-article)
8062     (gnus-eval-in-buffer-window gnus-article-buffer
8063       (save-restriction
8064         (widen)
8065         ;; Begin search from current point.
8066         (setq found (funcall re-search regexp nil t))))
8067     ;; Then search next articles.
8068     (while (and (not found)
8069                 (gnus-summary-display-article 
8070                  (gnus-summary-search-subject backward nil nil)))
8071       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8072       (gnus-eval-in-buffer-window gnus-article-buffer
8073         (save-restriction
8074           (widen)
8075           (goto-char (if backward (point-max) (point-min)))
8076           (setq found (funcall re-search regexp nil t)))))
8077     (message "")
8078     ;; Adjust article pointer.
8079     (or (eq last gnus-current-article)
8080         (setq gnus-last-article last))
8081     ;; Return T if found such article.
8082     found))
8083
8084 (defun gnus-summary-execute-command (header regexp command &optional backward)
8085   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8086 If HEADER is an empty string (or nil), the match is done on the entire
8087 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8088   (interactive
8089    (list (let ((completion-ignore-case t))
8090            (completing-read 
8091             "Header name: "
8092             (mapcar (lambda (string) (list string))
8093                     '("Number" "Subject" "From" "Lines" "Date"
8094                       "Message-ID" "Xref" "References"))
8095             nil 'require-match))
8096          (read-string "Regexp: ")
8097          (read-key-sequence "Command: ")
8098          current-prefix-arg))
8099   (gnus-set-global-variables)
8100   ;; Hidden thread subtrees must be searched as well.
8101   (gnus-summary-show-all-threads)
8102   ;; We don't want to change current point nor window configuration.
8103   (save-excursion
8104     (save-window-excursion
8105       (gnus-message 6 "Executing %s..." (key-description command))
8106       ;; We'd like to execute COMMAND interactively so as to give arguments.
8107       (gnus-execute header regexp
8108                     (` (lambda ()
8109                          (call-interactively '(, (key-binding command)))))
8110                     backward)
8111       (gnus-message 6 "Executing %s...done" (key-description command)))))
8112
8113 (defun gnus-summary-beginning-of-article ()
8114   "Scroll the article back to the beginning."
8115   (interactive)
8116   (gnus-set-global-variables)
8117   (gnus-summary-select-article)
8118   (gnus-eval-in-buffer-window
8119    gnus-article-buffer
8120    (widen)
8121    (goto-char (point-min))
8122    (and gnus-break-pages (gnus-narrow-to-page))))
8123
8124 (defun gnus-summary-end-of-article ()
8125   "Scroll to the end of the article."
8126   (interactive)
8127   (gnus-set-global-variables)
8128   (gnus-summary-select-article)
8129   (gnus-eval-in-buffer-window 
8130    gnus-article-buffer
8131    (widen)
8132    (goto-char (point-max))
8133    (and gnus-break-pages (gnus-narrow-to-page))))
8134
8135 (defun gnus-summary-show-article ()
8136   "Force re-fetching of the current article."
8137   (interactive)
8138   (gnus-set-global-variables)
8139   (gnus-summary-select-article gnus-have-all-headers t))
8140
8141 (defun gnus-summary-verbose-header (arg)
8142   "Toggle permanent full header display.
8143 If ARG is a positive number, turn header display on.
8144 If ARG is a negative number, turn header display off."
8145   (interactive "P")
8146   (gnus-set-global-variables)
8147   (gnus-summary-toggle-header arg)
8148   (setq gnus-have-all-headers
8149         (cond ((or (not (numberp arg))
8150                    (zerop arg))
8151                (not gnus-have-all-headers))
8152               ((natnump arg)
8153                t))))
8154
8155 (defun gnus-summary-toggle-header (arg)
8156   "Show the headers if they are hidden, or hide them if they are shown.
8157 If ARG is a positive number, show the entire header.
8158 If ARG is a negative number, hide the unwanted header lines."
8159   (interactive "P")
8160   (gnus-set-global-variables)
8161   (save-excursion
8162     (set-buffer gnus-article-buffer)
8163     (let ((buffer-read-only nil))
8164       (if (numberp arg) 
8165           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8166                                                 gnus-hidden-properties)
8167             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8168         (if (text-property-any (point-min) (point-max) 'invisible t)
8169             (remove-text-properties (point-min) (point-max)
8170                                     gnus-hidden-properties)
8171           (let ((gnus-have-all-headers nil))
8172             (run-hooks 'gnus-article-display-hook))))
8173       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8174
8175 (defun gnus-summary-show-all-headers ()
8176   "Make all header lines visible."
8177   (interactive)
8178   (gnus-set-global-variables)
8179   (gnus-article-show-all-headers))
8180
8181 (defun gnus-summary-toggle-mime (arg)
8182   "Toggle MIME processing.
8183 If ARG is a positive number, turn MIME processing on."
8184   (interactive "P")
8185   (gnus-set-global-variables)
8186   (setq gnus-show-mime
8187         (if (null arg) (not gnus-show-mime)
8188           (> (prefix-numeric-value arg) 0)))
8189   (gnus-summary-select-article t 'force))
8190
8191 (defun gnus-summary-caesar-message (rotnum)
8192   "Caesar rotates all letters of current message by 13/47 places.
8193 With prefix arg, specifies the number of places to rotate each letter forward.
8194 Caesar rotates Japanese letters by 47 places in any case."
8195   (interactive "P")
8196   (gnus-set-global-variables)
8197   (gnus-summary-select-article)
8198   (let ((mail-header-separator "")) ; !!! Is this necessary?
8199     (gnus-overload-functions)
8200     (gnus-eval-in-buffer-window 
8201      gnus-article-buffer
8202      (save-restriction
8203        (widen)
8204        ;; We don't want to jump to the beginning of the message.
8205        ;; `save-excursion' does not do its job.
8206        (move-to-window-line 0)
8207        (let ((last (point)))
8208          (news-caesar-buffer-body rotnum)
8209          (goto-char last)
8210          (recenter 0))))))
8211
8212 (defun gnus-summary-stop-page-breaking ()
8213   "Stop page breaking in the current article."
8214   (interactive)
8215   (gnus-set-global-variables)
8216   (gnus-summary-select-article)
8217   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8218
8219 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8220
8221 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8222   "Move the current article to a different newsgroup.
8223 If N is a positive number, move the N next articles.
8224 If N is a negative number, move the N previous articles.
8225 If N is nil and any articles have been marked with the process mark,
8226 move those articles instead.
8227 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8228 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8229 re-spool using this method.
8230 For this function to work, both the current newsgroup and the
8231 newsgroup that you want to move to have to support the `request-move'
8232 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8233   (interactive "P")
8234   (gnus-set-global-variables)
8235   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8236       (error "The current newsgroup does not support article moving"))
8237   (let ((articles (gnus-summary-work-articles n))
8238         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8239         art-group)
8240     (if (and (not to-newsgroup) (not select-method))
8241         (setq to-newsgroup
8242               (completing-read 
8243                (format "Where do you want to move %s? %s"
8244                        (if (> (length articles) 1)
8245                            (format "these %d articles" (length articles))
8246                          "this article")
8247                        (if gnus-current-move-group
8248                            (format "(%s default) " gnus-current-move-group)
8249                          ""))
8250                gnus-active-hashtb nil nil prefix)))
8251     (if to-newsgroup
8252         (progn
8253           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8254               (setq to-newsgroup (or gnus-current-move-group "")))
8255           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8256               (gnus-activate-newsgroup to-newsgroup)
8257               (error "No such group: %s" to-newsgroup))
8258           (setq gnus-current-move-group to-newsgroup)))
8259     (or (gnus-check-backend-function 'request-accept-article 
8260                                      (or select-method to-newsgroup))
8261         (error "%s does not support article moving" to-newsgroup))
8262     (gnus-message 6 "Moving to %s: %s..." 
8263                   (or select-method to-newsgroup) articles)
8264     (while articles
8265       (if (setq art-group
8266                 (gnus-request-move-article 
8267                  (car articles)                   ; Article to move
8268                  gnus-newsgroup-name              ; From newsgrouo
8269                  (nth 1 (gnus-find-method-for-group 
8270                          gnus-newsgroup-name))    ; Server
8271                  (list 'gnus-request-accept-article 
8272                        (if select-method
8273                            (list 'quote select-method)
8274                          to-newsgroup)
8275                        (not (cdr articles)))     ; Accept form
8276                  (not (cdr articles))))          ; Only save nov last time
8277           (let* ((buffer-read-only nil)
8278                  (entry 
8279                   (or
8280                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8281                    (gnus-gethash 
8282                     (gnus-group-prefixed-name 
8283                      (car art-group) 
8284                      (if select-method (list select-method "")
8285                        (gnus-find-method-for-group to-newsgroup)))
8286                     gnus-newsrc-hashtb)))
8287                  (info (nth 2 entry))
8288                  (article (car articles))
8289                  (marked (nth 3 info)))
8290             (gnus-summary-goto-subject article)
8291             (beginning-of-line)
8292             (delete-region (point)
8293                            (progn (forward-line 1) (point)))
8294             (if (not (memq article gnus-newsgroup-unreads))
8295                 (setcar (cdr (cdr info))
8296                         (gnus-add-to-range (nth 2 info) 
8297                                            (list (cdr art-group)))))
8298             ;; Copy any marks over to the new group.
8299             (let ((marks '((tick . gnus-newsgroup-marked)
8300                            (dormant . gnus-newsgroup-dormant)
8301                            (expire . gnus-newsgroup-expirable)
8302                            (bookmark . gnus-newsgroup-bookmarks)
8303                         ;   (score . gnus-newsgroup-scored)
8304                            (reply . gnus-newsgroup-replied)))
8305                   (to-article (cdr art-group)))
8306               (while marks
8307                 (if (memq article (symbol-value (cdr (car marks))))
8308                     (gnus-add-marked-articles 
8309                      (car info) (car (car marks)) (list to-article) info))
8310                 (setq marks (cdr marks))))
8311             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8312             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8313             (setq gnus-newsgroup-dormant
8314                   (delq article gnus-newsgroup-dormant)))
8315         (gnus-message 1 "Couldn't move article %s" (car articles)))
8316       (gnus-summary-remove-process-mark (car articles))
8317       (setq articles (cdr articles)))))
8318
8319 (defun gnus-summary-respool-article (n &optional respool-method)
8320   "Respool the current article.
8321 The article will be squeezed through the mail spooling process again,
8322 which means that it will be put in some mail newsgroup or other
8323 depending on `nnmail-split-methods'.
8324 If N is a positive number, respool the N next articles.
8325 If N is a negative number, respool the N previous articles.
8326 If N is nil and any articles have been marked with the process mark,
8327 respool those articles instead.
8328
8329 Respooling can be done both from mail groups and \"real\" newsgroups.
8330 In the former case, the articles in question will be moved from the
8331 current group into whatever groups they are destined to.  In the
8332 latter case, they will be copied into the relevant groups."
8333   (interactive "P")
8334   (gnus-set-global-variables)
8335   (let ((respool-methods (gnus-methods-using 'respool))
8336         (methname 
8337          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8338     (or respool-method
8339         (setq respool-method
8340               (completing-read
8341                "What method do you want to use when respooling? "
8342                respool-methods nil t methname)))
8343     (or (string= respool-method "")
8344         (if (assoc (symbol-name
8345                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8346                    respool-methods)
8347             (gnus-summary-move-article n nil (intern respool-method))
8348           (gnus-summary-copy-article n nil (intern respool-method))))))
8349
8350 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8351 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8352   "Move the current article to a different newsgroup.
8353 If N is a positive number, move the N next articles.
8354 If N is a negative number, move the N previous articles.
8355 If N is nil and any articles have been marked with the process mark,
8356 move those articles instead.
8357 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8358 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8359 re-spool using this method.
8360 For this function to work, the newsgroup that you want to move to have
8361 to support the `request-move' and `request-accept'
8362 functions. (Ie. mail newsgroups at present.)"
8363   (interactive "P")
8364   (gnus-set-global-variables)
8365   (let ((articles (gnus-summary-work-articles n))
8366         (copy-buf (get-buffer-create "*copy work*"))
8367         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8368         art-group)
8369     (buffer-disable-undo copy-buf)
8370     (if (and (not to-newsgroup) (not select-method))
8371         (setq to-newsgroup
8372               (completing-read 
8373                (format "Where do you want to copy %s? %s"
8374                        (if (> (length articles) 1)
8375                            (format "these %d articles" (length articles))
8376                          "this article")
8377                        (if gnus-current-move-group
8378                            (format "(%s default) " gnus-current-move-group)
8379                          ""))
8380                gnus-active-hashtb nil nil prefix)))
8381     (if to-newsgroup
8382         (progn
8383           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8384               (setq to-newsgroup (or gnus-current-move-group "")))
8385           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8386               (gnus-activate-newsgroup to-newsgroup)
8387               (error "No such group: %s" to-newsgroup))
8388           (setq gnus-current-move-group to-newsgroup)))
8389     (or (gnus-check-backend-function 'request-accept-article 
8390                                      (or select-method to-newsgroup))
8391         (error "%s does not support article copying" to-newsgroup))
8392     (gnus-message 6 "Copying to %s: %s..." 
8393                   (or select-method to-newsgroup) articles)
8394     (while articles
8395       (if (setq art-group
8396                 (save-excursion
8397                   (set-buffer copy-buf)
8398                   (gnus-request-article-this-buffer
8399                    (car articles) gnus-newsgroup-name)
8400                   (gnus-request-accept-article
8401                    (if select-method (quote select-method) to-newsgroup)
8402                    (not (cdr articles)))))
8403           (let* ((entry 
8404                   (or
8405                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8406                    (gnus-gethash 
8407                     (gnus-group-prefixed-name 
8408                      (car art-group) 
8409                      (if select-method (list select-method "")
8410                        (gnus-find-method-for-group to-newsgroup)))
8411                     gnus-newsrc-hashtb)))
8412                  (info (nth 2 entry))
8413                  (article (car articles))
8414                  (marked (nth 3 info)))
8415             (if (not (memq article gnus-newsgroup-unreads))
8416                 (setcar (cdr (cdr info))
8417                         (gnus-add-to-range (nth 2 info) 
8418                                            (list (cdr art-group)))))
8419             ;; Copy any marks over to the new group.
8420             (let ((marks '((tick . gnus-newsgroup-marked)
8421                            (dormant . gnus-newsgroup-dormant)
8422                            (expire . gnus-newsgroup-expirable)
8423                            (bookmark . gnus-newsgroup-bookmarks)
8424                         ;   (score . gnus-newsgroup-scored)
8425                            (reply . gnus-newsgroup-replied)))
8426                   (to-article (cdr art-group)))
8427               (while marks
8428                 (if (memq article (symbol-value (cdr (car marks))))
8429                     (gnus-add-marked-articles 
8430                      (car info) (car (car marks)) (list to-article) info))
8431                 (setq marks (cdr marks)))))
8432         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8433       (gnus-summary-remove-process-mark (car articles))
8434       (setq articles (cdr articles)))
8435     (kill-buffer copy-buf)))
8436
8437 (defun gnus-summary-import-article (file)
8438   "Import a random file into a mail newsgroup."
8439   (interactive "fImport file: ")
8440   (let ((group gnus-newsgroup-name)
8441         atts)
8442     (or (gnus-check-backend-function 'request-accept-article group)
8443         (error "%s does not support article importing" group))
8444     (or (file-readable-p file)
8445         (not (file-regular-p file))
8446         (error "Can't read %s" file))
8447     (save-excursion
8448       (set-buffer (get-buffer-create " *import file*"))
8449       (buffer-disable-undo (current-buffer))
8450       (erase-buffer)
8451       (insert-file-contents file)
8452       (goto-char (point-min))
8453       (if (nnheader-article-p)
8454           ()
8455         (setq atts (file-attributes file))
8456         (insert "From: " (read-string "From: ") "\n"
8457                 "Subject: " (read-string "Subject: ") "\n"
8458                 "Date: " (current-time-string (nth 5 atts)) "\n"
8459                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8460       (gnus-request-accept-article group t)
8461       (kill-buffer (current-buffer)))))
8462
8463 (defun gnus-summary-expire-articles ()
8464   "Expire all articles that are marked as expirable in the current group."
8465   (interactive)
8466   (if (and gnus-newsgroup-expirable
8467            (gnus-check-backend-function 
8468             'request-expire-articles gnus-newsgroup-name))
8469       (let ((expirable gnus-newsgroup-expirable))
8470         ;; The list of articles that weren't expired is returned.
8471         (setq gnus-newsgroup-expirable 
8472               (gnus-request-expire-articles gnus-newsgroup-expirable
8473                                             gnus-newsgroup-name))
8474         ;; We go through the old list of expirable, and mark all
8475         ;; really expired articles as non-existent.
8476         (while expirable
8477           (or (memq (car expirable) gnus-newsgroup-expirable)
8478               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8479           (setq expirable (cdr expirable))))))
8480
8481 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8482 (defun gnus-summary-delete-article (n)
8483   "Delete the N next (mail) articles.
8484 This command actually deletes articles. This is not a marking
8485 command. The article will disappear forever from you life, never to
8486 return. 
8487 If N is negative, delete backwards.
8488 If N is nil and articles have been marked with the process mark,
8489 delete these instead."
8490   (interactive "P")
8491   (or (gnus-check-backend-function 'request-expire-articles 
8492                                    gnus-newsgroup-name)
8493       (error "The current newsgroup does not support article deletion."))
8494   ;; Compute the list of articles to delete.
8495   (let ((articles (gnus-summary-work-articles n))
8496         not-deleted)
8497     (if (and gnus-novice-user
8498              (not (gnus-y-or-n-p 
8499                    (format "Do you really want to delete %s forever? "
8500                            (if (> (length articles) 1) "these articles"
8501                              "this article")))))
8502         ()
8503       ;; Delete the articles.
8504       (setq not-deleted (gnus-request-expire-articles 
8505                          articles gnus-newsgroup-name 'force))
8506       (while articles
8507         (gnus-summary-remove-process-mark (car articles))       
8508         ;; The backend might not have been able to delete the article
8509         ;; after all.  
8510         (or (memq (car articles) not-deleted)
8511             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8512         (setq articles (cdr articles))))
8513     (gnus-summary-position-cursor)
8514     not-deleted))
8515
8516 (defun gnus-summary-edit-article ()
8517   "Enter into a buffer and edit the current article.
8518 This will have permanent effect only in mail groups."
8519   (interactive)
8520   (or (gnus-check-backend-function 
8521        'request-replace-article gnus-newsgroup-name)
8522       (error "The current newsgroup does not support article editing."))
8523   (gnus-summary-select-article t)
8524   (other-window 1)
8525   (gnus-message 6 "C-c C-c to end edits")
8526   (setq buffer-read-only nil)
8527   (text-mode)
8528   (use-local-map (copy-keymap (current-local-map)))
8529   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8530   (goto-char (point-min))
8531   (search-forward "\n\n" nil t))
8532
8533 (defun gnus-summary-edit-article-done ()
8534   "Make edits to the current article permanent."
8535   (interactive)
8536   (if (not (gnus-request-replace-article 
8537             (cdr gnus-article-current) (car gnus-article-current) 
8538             (current-buffer)))
8539       (error "Couldn't replace article.")
8540     (gnus-article-mode)
8541     (use-local-map gnus-article-mode-map)
8542     (setq buffer-read-only t)
8543     (pop-to-buffer gnus-summary-buffer)))      
8544
8545 (defun gnus-summary-fancy-query ()
8546   "Query where the fancy respool algorithm would put this article."
8547   (interactive)
8548   (gnus-summary-select-article)
8549   (save-excursion
8550     (set-buffer gnus-article-buffer)
8551     (save-restriction
8552       (goto-char (point-min))
8553       (search-forward "\n\n")
8554       (narrow-to-region (point-min) (point))
8555       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8556
8557 ;; Summary score commands.
8558
8559 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8560
8561 (defun gnus-summary-raise-score (n)
8562   "Raise the score of the current article by N."
8563   (interactive "p")
8564   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8565
8566 (defun gnus-summary-lower-score (n)
8567   "Lower the score of the current article by N."
8568   (interactive "p")
8569   (gnus-summary-raise-score (- n)))
8570
8571 (defun gnus-summary-set-score (n)
8572   "Set the score of the current article to N."
8573   (interactive "p")
8574   ;; Skip dummy header line.
8575   (save-excursion
8576     (gnus-summary-show-thread)
8577     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8578         (forward-line 1))
8579     (let ((buffer-read-only nil))
8580       ;; Set score.
8581       (gnus-summary-update-mark
8582        (if (= n (or gnus-summary-default-score 0)) ? 
8583          (if (< n (or gnus-summary-default-score 0)) 
8584              gnus-score-below-mark gnus-score-over-mark)) 'score))
8585     (let* ((article (gnus-summary-article-number))
8586            (score (assq article gnus-newsgroup-scored)))
8587       (if score (setcdr score n)
8588         (setq gnus-newsgroup-scored 
8589               (cons (cons article n) gnus-newsgroup-scored))))
8590     (gnus-summary-update-line)))
8591
8592 (defun gnus-summary-current-score ()
8593   "Return the score of the current article."
8594   (interactive)
8595   (message "%s" (gnus-summary-article-score)))
8596
8597 ;; Summary marking commands.
8598
8599 (defun gnus-summary-raise-same-subject-and-select (score)
8600   "Raise articles which has the same subject with SCORE and select the next."
8601   (interactive "p")
8602   (let ((subject (gnus-summary-subject-string)))
8603     (gnus-summary-raise-score score)
8604     (while (gnus-summary-search-subject nil nil subject)
8605       (gnus-summary-raise-score score))
8606     (gnus-summary-next-article t)))
8607
8608 (defun gnus-summary-raise-same-subject (score)
8609   "Raise articles which has the same subject with SCORE."
8610   (interactive "p")
8611   (let ((subject (gnus-summary-subject-string)))
8612     (gnus-summary-raise-score score)
8613     (while (gnus-summary-search-subject nil nil subject)
8614       (gnus-summary-raise-score score))
8615     (gnus-summary-next-subject 1 t)))
8616
8617 (defun gnus-score-default (level)
8618   (if level (prefix-numeric-value level) 
8619     gnus-score-interactive-default-score))
8620
8621 (defun gnus-summary-raise-thread (score)
8622   "Raise the score of the articles in the current thread with SCORE."
8623   (interactive "P")
8624   (setq score (1- (gnus-score-default score)))
8625   (let (e)
8626     (save-excursion
8627       (let ((level (gnus-summary-thread-level)))
8628         (gnus-summary-raise-score score)
8629         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8630                     (> (gnus-summary-thread-level) level))
8631           (gnus-summary-raise-score score))
8632         (setq e (point))))
8633     (let ((gnus-summary-check-current t))
8634       (or (zerop (gnus-summary-next-subject 1 t))
8635           (goto-char e))))
8636   (gnus-summary-recenter)
8637   (gnus-summary-position-cursor)
8638   (gnus-set-mode-line 'summary))
8639
8640 (defun gnus-summary-lower-same-subject-and-select (score)
8641   "Raise articles which has the same subject with SCORE and select the next."
8642   (interactive "p")
8643   (gnus-summary-raise-same-subject-and-select (- score)))
8644
8645 (defun gnus-summary-lower-same-subject (score)
8646   "Raise articles which has the same subject with SCORE."
8647   (interactive "p")
8648   (gnus-summary-raise-same-subject (- score)))
8649
8650 (defun gnus-summary-lower-thread (score)
8651   "Lower score of articles in the current thread with SCORE."
8652   (interactive "P")
8653   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8654
8655 (defun gnus-summary-kill-same-subject-and-select (unmark)
8656   "Mark articles which has the same subject as read, and then select the next.
8657 If UNMARK is positive, remove any kind of mark.
8658 If UNMARK is negative, tick articles."
8659   (interactive "P")
8660   (if unmark
8661       (setq unmark (prefix-numeric-value unmark)))
8662   (let ((count
8663          (gnus-summary-mark-same-subject
8664           (gnus-summary-subject-string) unmark)))
8665     ;; Select next unread article. If auto-select-same mode, should
8666     ;; select the first unread article.
8667     (gnus-summary-next-article t (and gnus-auto-select-same
8668                                       (gnus-summary-subject-string)))
8669     (gnus-message 7 "%d articles are marked as %s"
8670                   count (if unmark "unread" "read"))))
8671
8672 (defun gnus-summary-kill-same-subject (unmark)
8673   "Mark articles which has the same subject as read. 
8674 If UNMARK is positive, remove any kind of mark.
8675 If UNMARK is negative, tick articles."
8676   (interactive "P")
8677   (if unmark
8678       (setq unmark (prefix-numeric-value unmark)))
8679   (let ((count
8680          (gnus-summary-mark-same-subject
8681           (gnus-summary-subject-string) unmark)))
8682     ;; If marked as read, go to next unread subject.
8683     (if (null unmark)
8684         ;; Go to next unread subject.
8685         (gnus-summary-next-subject 1 t))
8686     (gnus-message 7 "%d articles are marked as %s"
8687                   count (if unmark "unread" "read"))))
8688
8689 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8690   "Mark articles with same SUBJECT as read, and return marked number.
8691 If optional argument UNMARK is positive, remove any kinds of marks.
8692 If optional argument UNMARK is negative, mark articles as unread instead."
8693   (let ((count 1))
8694     (save-excursion
8695       (cond ((null unmark)
8696              (gnus-summary-mark-as-read nil gnus-killed-mark))
8697             ((> unmark 0)
8698              (gnus-summary-tick-article nil t))
8699             (t
8700              (gnus-summary-tick-article)))
8701       (while (and subject
8702                   (gnus-summary-search-forward nil subject))
8703         (cond ((null unmark)
8704                (gnus-summary-mark-as-read nil gnus-killed-mark))
8705               ((> unmark 0)
8706                (gnus-summary-tick-article nil t))
8707               (t
8708                (gnus-summary-tick-article)))
8709         (setq count (1+ count))))
8710     ;; Hide killed thread subtrees.  Does not work properly always.
8711     ;;(and (null unmark)
8712     ;;     gnus-thread-hide-killed
8713     ;;     (gnus-summary-hide-thread))
8714     ;; Return number of articles marked as read.
8715     count))
8716
8717 (defun gnus-summary-mark-as-processable (n &optional unmark)
8718   "Set the process mark on the next N articles.
8719 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8720 the process mark instead.  The difference between N and the actual
8721 number of articles marked is returned."
8722   (interactive "p")
8723   (let ((backward (< n 0))
8724         (n (abs n)))
8725   (while (and 
8726           (> n 0)
8727           (if unmark
8728               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8729             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8730           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8731     (setq n (1- n)))
8732   (if (/= 0 n) (gnus-message 7 "No more articles"))
8733   (gnus-summary-recenter)
8734   (gnus-summary-position-cursor)
8735   n))
8736
8737 (defun gnus-summary-unmark-as-processable (n)
8738   "Remove the process mark from the next N articles.
8739 If N is negative, mark backward instead.  The difference between N and
8740 the actual number of articles marked is returned."
8741   (interactive "p")
8742   (gnus-summary-mark-as-processable n t))
8743
8744 (defun gnus-summary-unmark-all-processable ()
8745   "Remove the process mark from all articles."
8746   (interactive)
8747   (save-excursion
8748     (while gnus-newsgroup-processable
8749       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8750   (gnus-summary-position-cursor))
8751
8752 (defun gnus-summary-mark-as-expirable (n)
8753   "Mark N articles forward as expirable.
8754 If N is negative, mark backward instead. The difference between N and
8755 the actual number of articles marked is returned."
8756   (interactive "p")
8757   (gnus-summary-mark-forward n gnus-expirable-mark))
8758
8759 (defun gnus-summary-mark-article-as-replied (article)
8760   "Mark ARTICLE replied and update the summary line."
8761   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8762   (let ((buffer-read-only nil))
8763     (if (gnus-summary-goto-subject article)
8764         (progn
8765           (gnus-summary-update-mark gnus-replied-mark 'replied)
8766           t))))
8767
8768 (defun gnus-summary-set-bookmark (article)
8769   "Set a bookmark in current article."
8770   (interactive (list (gnus-summary-article-number)))
8771   (if (or (not (get-buffer gnus-article-buffer))
8772           (not gnus-current-article)
8773           (not gnus-article-current)
8774           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8775       (error "No current article selected"))
8776   ;; Remove old bookmark, if one exists.
8777   (let ((old (assq article gnus-newsgroup-bookmarks)))
8778     (if old (setq gnus-newsgroup-bookmarks 
8779                   (delq old gnus-newsgroup-bookmarks))))
8780   ;; Set the new bookmark, which is on the form 
8781   ;; (article-number . line-number-in-body).
8782   (setq gnus-newsgroup-bookmarks 
8783         (cons 
8784          (cons article 
8785                (save-excursion
8786                  (set-buffer gnus-article-buffer)
8787                  (count-lines
8788                   (min (point)
8789                        (save-excursion
8790                          (goto-char (point-min))
8791                          (search-forward "\n\n" nil t)
8792                          (point)))
8793                   (point))))
8794          gnus-newsgroup-bookmarks))
8795   (gnus-message 6 "A bookmark has been added to the current article."))
8796
8797 (defun gnus-summary-remove-bookmark (article)
8798   "Remove the bookmark from the current article."
8799   (interactive (list (gnus-summary-article-number)))
8800   ;; Remove old bookmark, if one exists.
8801   (let ((old (assq article gnus-newsgroup-bookmarks)))
8802     (if old 
8803         (progn
8804           (setq gnus-newsgroup-bookmarks 
8805                 (delq old gnus-newsgroup-bookmarks))
8806           (gnus-message 6 "Removed bookmark."))
8807       (gnus-message 6 "No bookmark in current article."))))
8808
8809 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8810 (defun gnus-summary-mark-as-dormant (n)
8811   "Mark N articles forward as dormant.
8812 If N is negative, mark backward instead.  The difference between N and
8813 the actual number of articles marked is returned."
8814   (interactive "p")
8815   (gnus-summary-mark-forward n gnus-dormant-mark))
8816
8817 (defun gnus-summary-set-process-mark (article)
8818   "Set the process mark on ARTICLE and update the summary line."
8819   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8820   (let ((buffer-read-only nil))
8821     (if (gnus-summary-goto-subject article)
8822         (progn
8823           (gnus-summary-show-thread)
8824           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8825                (forward-line 1))
8826           (gnus-summary-update-mark gnus-process-mark 'replied)
8827           t))))
8828
8829 (defun gnus-summary-remove-process-mark (article)
8830   "Remove the process mark from ARTICLE and update the summary line."
8831   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8832   (let ((buffer-read-only nil))
8833     (if (gnus-summary-goto-subject article)
8834         (progn
8835           (gnus-summary-show-thread)
8836           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8837                (forward-line 1))
8838           (gnus-summary-update-mark ?  'replied)
8839           (if (memq article gnus-newsgroup-replied) 
8840               (gnus-summary-update-mark gnus-replied-mark 'replied))
8841           t))))
8842
8843 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8844   "Mark N articles as read forwards.
8845 If N is negative, mark backwards instead.
8846 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8847 marked as unread. 
8848 The difference between N and the actual number of articles marked is
8849 returned."
8850   (interactive "p")
8851   (gnus-set-global-variables)
8852   (let ((backward (< n 0))
8853         (n (abs n))
8854         (mark (or mark gnus-del-mark)))
8855   (while (and (> n 0)
8856               (gnus-summary-mark-article nil mark no-expire)
8857               (zerop (gnus-summary-next-subject 
8858                       (if backward -1 1) gnus-summary-goto-unread t)))
8859     (setq n (1- n)))
8860   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8861   (gnus-summary-recenter)
8862   (gnus-summary-position-cursor)
8863   (gnus-set-mode-line 'summary)
8864   n))
8865
8866 (defun gnus-summary-mark-article (&optional article mark no-expire)
8867   "Mark ARTICLE with MARK.
8868 MARK can be any character.
8869 Five MARK strings are reserved: ?  (unread), 
8870 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8871 If MARK is nil, then the default character ?D is used.
8872 If ARTICLE is nil, then the article on the current line will be
8873 marked." 
8874   ;; If no mark is given, then we check auto-expiring.
8875   (and (not no-expire)
8876        gnus-newsgroup-auto-expire 
8877        (or (not mark)
8878            (and (numberp mark) 
8879                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
8880                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
8881                     (= mark gnus-read-mark) (= mark gnus-ancient-mark))))
8882        (setq mark gnus-expirable-mark))
8883   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8884          (article (or article (gnus-summary-article-number))))
8885     (or article (error "No article on current line"))
8886     (if (or (= mark gnus-unread-mark) 
8887             (= mark gnus-ticked-mark) 
8888             (= mark gnus-dormant-mark))
8889         (gnus-mark-article-as-unread article mark)
8890       (gnus-mark-article-as-read article mark))
8891
8892     ;; See whether the article is to be put in the cache.
8893     (and gnus-use-cache
8894          (save-excursion
8895            (gnus-cache-possibly-enter-article 
8896             gnus-newsgroup-name article 
8897             (gnus-get-header-by-number article)
8898             (= mark gnus-ticked-mark)
8899             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8900
8901     (if (gnus-summary-goto-subject article)
8902         (let ((buffer-read-only nil))
8903           (gnus-summary-show-thread)
8904           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8905                (forward-line 1))
8906           ;; Fix the mark.
8907           (gnus-summary-update-mark mark 'unread)
8908           t))))
8909
8910 (defun gnus-summary-update-mark (mark type)
8911   (beginning-of-line)
8912   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8913         plist)
8914     (if (not forward)
8915         ()
8916       (forward-char forward)
8917       (setq plist (text-properties-at (point)))
8918       (delete-char 1)
8919       (insert mark)
8920       (and plist (add-text-properties (1- (point)) (point) plist))
8921       (and (eq type 'unread)
8922            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
8923       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8924   
8925 (defun gnus-mark-article-as-read (article &optional mark)
8926   "Enter ARTICLE in the pertinent lists and remove it from others."
8927   ;; Make the article expirable.
8928   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8929     (if (= mark gnus-expirable-mark)
8930         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8931       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8932     ;; Remove from unread and marked lists.
8933     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8934     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8935     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8936     ;; Possibly remove from cache, if that is used. 
8937     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
8938
8939 (defun gnus-mark-article-as-unread (article &optional mark)
8940   "Enter ARTICLE in the pertinent lists and remove it from others."
8941   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8942     ;; Add to unread list.
8943     (or (memq article gnus-newsgroup-unreads)
8944         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
8945     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
8946     ;; lists.  Otherwise, it must be added to the list.
8947     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8948     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8949     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8950     (if (= mark gnus-ticked-mark)
8951         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
8952     (if (= mark gnus-dormant-mark)
8953         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
8954
8955 (defalias 'gnus-summary-mark-as-unread-forward 
8956   'gnus-summary-tick-article-forward)
8957 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8958                'gnus-summary-tick-article-forward)
8959 (defun gnus-summary-tick-article-forward (n)
8960   "Tick N articles forwards.
8961 If N is negative, tick backwards instead.
8962 The difference between N and the number of articles ticked is returned."
8963   (interactive "p")
8964   (gnus-summary-mark-forward n gnus-ticked-mark))
8965
8966 (defalias 'gnus-summary-mark-as-unread-backward 
8967   'gnus-summary-tick-article-backward)
8968 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8969                'gnus-summary-tick-article-backward)
8970 (defun gnus-summary-tick-article-backward (n)
8971   "Tick N articles backwards.
8972 The difference between N and the number of articles ticked is returned."
8973   (interactive "p")
8974   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8975
8976 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8977 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8978 (defun gnus-summary-tick-article (&optional article clear-mark)
8979   "Mark current article as unread.
8980 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8981 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8982   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8983                                        gnus-ticked-mark)))
8984
8985 (defun gnus-summary-mark-as-read-forward (n)
8986   "Mark N articles as read forwards.
8987 If N is negative, mark backwards instead.
8988 The difference between N and the actual number of articles marked is
8989 returned."
8990   (interactive "p")
8991   (gnus-summary-mark-forward n gnus-del-mark t))
8992
8993 (defun gnus-summary-mark-as-read-backward (n)
8994   "Mark the N articles as read backwards.
8995 The difference between N and the actual number of articles marked is
8996 returned."
8997   (interactive "p")
8998   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8999
9000 (defun gnus-summary-mark-as-read (&optional article mark)
9001   "Mark current article as read.
9002 ARTICLE specifies the article to be marked as read.
9003 MARK specifies a string to be inserted at the beginning of the line."
9004   (gnus-summary-mark-article article mark))
9005
9006 (defun gnus-summary-clear-mark-forward (n)
9007   "Clear marks from N articles forward.
9008 If N is negative, clear backward instead.
9009 The difference between N and the number of marks cleared is returned."
9010   (interactive "p")
9011   (gnus-summary-mark-forward n gnus-unread-mark))
9012
9013 (defun gnus-summary-clear-mark-backward (n)
9014   "Clear marks from N articles backward.
9015 The difference between N and the number of marks cleared is returned."
9016   (interactive "p")
9017   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9018
9019 (defun gnus-summary-mark-unread-as-read ()
9020   "Intended to be used by `gnus-summary-mark-article-hook'."
9021   (or (memq gnus-current-article gnus-newsgroup-marked)
9022       (memq gnus-current-article gnus-newsgroup-dormant)
9023       (memq gnus-current-article gnus-newsgroup-expirable)
9024       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
9025
9026 (defun gnus-summary-mark-region-as-read (point mark all)
9027   "Mark all unread articles between point and mark as read.
9028 If given a prefix, mark all articles between point and mark as read,
9029 even ticked and dormant ones."
9030   (interactive "r\nP")
9031   (save-excursion
9032     (goto-char point)
9033     (beginning-of-line)
9034     (while (and 
9035             (< (point) mark)
9036             (progn
9037               (and
9038                (or all
9039                    (and
9040                     (not (memq (gnus-summary-article-number)
9041                                gnus-newsgroup-marked))
9042                     (not (memq (gnus-summary-article-number)
9043                                gnus-newsgroup-dormant))))
9044                (gnus-summary-mark-article
9045                 (gnus-summary-article-number) gnus-del-mark))
9046               t)
9047             (zerop (forward-line 1))))))
9048
9049 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9050 (defalias 'gnus-summary-delete-marked-as-read 
9051   'gnus-summary-remove-lines-marked-as-read)
9052 (make-obsolete 'gnus-summary-delete-marked-as-read 
9053                'gnus-summary-remove-lines-marked-as-read)
9054 (defun gnus-summary-remove-lines-marked-as-read ()
9055   "Remove lines that are marked as read."
9056   (interactive)
9057   (gnus-summary-remove-lines-marked-with 
9058    (concat (mapconcat
9059             (lambda (char) (char-to-string (symbol-value char)))
9060             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9061               gnus-killed-mark gnus-kill-file-mark
9062               gnus-low-score-mark gnus-expirable-mark)
9063             ""))))
9064
9065 (defalias 'gnus-summary-delete-marked-with 
9066   'gnus-summary-remove-lines-marked-with)
9067 (make-obsolete 'gnus-summary-delete-marked-with 
9068                'gnus-summary-remove-lines-marked-with)
9069 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9070 (defun gnus-summary-remove-lines-marked-with (marks)
9071   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9072   (interactive "sMarks: ")
9073   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9074   (gnus-set-global-variables)
9075   (let ((buffer-read-only nil)
9076         (marks (concat "^[" marks "]")))
9077     (goto-char (point-min))
9078     (if gnus-newsgroup-adaptive
9079         (gnus-score-remove-lines-adaptive marks)
9080       (while (re-search-forward marks nil t)
9081         (gnus-delete-line)))
9082     ;; If we use dummy roots, we have to do an additional sweep over
9083     ;; the buffer.
9084     (if (not (eq gnus-summary-make-false-root 'dummy))
9085         ()
9086       (goto-char (point-min))
9087       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9088       (while (re-search-forward marks nil t)
9089         (if (gnus-subject-equal
9090              (gnus-summary-subject-string)
9091              (progn
9092                (forward-line 1)
9093                (gnus-summary-subject-string)))
9094             ()
9095           (forward-line -1)
9096           (gnus-delete-line)))))
9097   (or (zerop (buffer-size))
9098       (if (eobp)
9099           (gnus-summary-prev-subject 1)
9100         (gnus-summary-position-cursor))))
9101
9102 (defun gnus-summary-expunge-below (score)
9103   "Remove articles with score less than SCORE."
9104   (interactive "P")
9105   (gnus-set-global-variables)
9106   (setq score (if score
9107                   (prefix-numeric-value score)
9108                 (or gnus-summary-default-score 0)))
9109   (save-excursion
9110     (set-buffer gnus-summary-buffer)
9111     (goto-char (point-min))
9112     (let ((buffer-read-only nil)
9113           beg)
9114       (while (not (eobp))
9115         (if (< (gnus-summary-article-score) score)
9116             (progn
9117               (setq beg (point))
9118               (forward-line 1)
9119               (delete-region beg (point)))
9120           (forward-line 1)))
9121       ;; Adjust point.
9122       (or (zerop (buffer-size))
9123           (if (eobp)
9124               (gnus-summary-prev-subject 1)
9125             (gnus-summary-position-cursor))))))
9126
9127 (defun gnus-summary-mark-below (score mark)
9128   "Mark articles with score less than SCORE with MARK."
9129   (interactive "P\ncMark: ")
9130   (gnus-set-global-variables)
9131   (setq score (if score
9132                   (prefix-numeric-value score)
9133                 (or gnus-summary-default-score 0)))
9134   (save-excursion
9135     (set-buffer gnus-summary-buffer)
9136     (goto-char (point-min))
9137     (while (not (eobp))
9138       (and (< (gnus-summary-article-score) score)
9139            (gnus-summary-mark-article nil mark))
9140       (forward-line 1))))
9141
9142 (defun gnus-summary-kill-below (score)
9143   "Mark articles with score below SCORE as read."
9144   (interactive "P")
9145   (gnus-summary-mark-below score gnus-killed-mark))
9146
9147 (defun gnus-summary-clear-above (score)
9148   "Clear all marks from articles with score above SCORE."
9149   (interactive "P")
9150   (gnus-summary-mark-above score gnus-unread-mark))
9151
9152 (defun gnus-summary-tick-above (score)
9153   "Tick all articles with score above SCORE."
9154   (interactive "P")
9155   (gnus-summary-mark-above score gnus-ticked-mark))
9156
9157 (defun gnus-summary-mark-above (score mark)
9158   "Mark articles with score over SCORE with MARK."
9159   (interactive "P\ncMark: ")
9160   (setq score (if score
9161                   (prefix-numeric-value score)
9162                 (or gnus-summary-default-score 0)))
9163   (save-excursion
9164     (set-buffer gnus-summary-buffer)
9165     (goto-char (point-min))
9166     (while (not (eobp))
9167       (if (> (gnus-summary-article-score) score)
9168           (progn
9169             (gnus-summary-mark-article nil mark)
9170             (forward-line 1))
9171         (forward-line 1)))))
9172
9173 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9174 (defun gnus-summary-show-all-expunged ()
9175   "Display all the hidden articles that were expunged for low scores."
9176   (interactive)
9177   (let ((buffer-read-only nil))
9178     (let ((scored gnus-newsgroup-scored)
9179           headers h)
9180       (while scored
9181         (or (gnus-summary-goto-subject (car (car scored)))
9182             (and (setq h (gnus-get-header-by-number (car (car scored))))
9183                  (< (cdr (car scored)) gnus-summary-expunge-below)
9184                  (setq headers (cons h headers))))
9185         (setq scored (cdr scored)))
9186       (or headers (error "No expunged articles hidden."))
9187       (goto-char (point-min))
9188       (save-excursion 
9189         (gnus-summary-update-lines 
9190          (point)
9191          (progn
9192            (gnus-summary-prepare-threads (nreverse headers) 0)
9193            (point)))))
9194     (goto-char (point-min))
9195     (gnus-summary-position-cursor)))
9196
9197 (defun gnus-summary-show-all-dormant ()
9198   "Display all the hidden articles that are marked as dormant."
9199   (interactive)
9200   (let ((buffer-read-only nil))
9201     (goto-char (point-min))
9202     (let ((dormant gnus-newsgroup-dormant)
9203           headers h)
9204       (while dormant
9205         (or (gnus-summary-goto-subject (car dormant))
9206             (and (setq h (gnus-get-header-by-number (car dormant)))
9207                  (setq headers (cons h headers))))
9208         (setq dormant (cdr dormant)))
9209       (or headers (error "No dormant articles hidden."))
9210       (save-excursion 
9211         (gnus-summary-update-lines 
9212          (point)
9213          (progn
9214            (gnus-summary-prepare-threads (nreverse headers) 0)
9215            (point)))))
9216     (goto-char (point-min))
9217     (gnus-summary-position-cursor)))
9218
9219 (defun gnus-summary-hide-all-dormant ()
9220   "Hide all dormant articles."
9221   (interactive)
9222   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9223   (gnus-summary-position-cursor))
9224
9225 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9226   "Mark all articles not marked as unread in this newsgroup as read.
9227 If prefix argument ALL is non-nil, all articles are marked as read.
9228 If QUIETLY is non-nil, no questions will be asked.
9229 If TO-HERE is non-nil, it should be a point in the buffer. All
9230 articles before this point will be marked as read.
9231 The number of articles marked as read is returned."
9232   (interactive "P")
9233   (prog1
9234       (if (or quietly
9235               (not gnus-interactive-catchup) ;Without confirmation?
9236               gnus-expert-user
9237               (gnus-y-or-n-p
9238                (if all
9239                    "Mark absolutely all articles as read? "
9240                  "Mark all unread articles as read? ")))
9241           (if (and not-mark 
9242                    (not gnus-newsgroup-adaptive)
9243                    (not gnus-newsgroup-auto-expire))
9244               (progn
9245                 (and all (setq gnus-newsgroup-marked nil
9246                                gnus-newsgroup-dormant nil))
9247                 (setq gnus-newsgroup-unreads 
9248                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9249             ;; We actually mark all articles as canceled, which we
9250             ;; have to do when using auto-expiry or adaptive scoring. 
9251             (let ((unreads (length gnus-newsgroup-unreads)))
9252               (if (gnus-summary-first-subject (not all))
9253                   (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
9254                               (if to-here (< (point) to-here) t)
9255                               (gnus-summary-search-subject nil (not all)))))
9256               (- unreads (length gnus-newsgroup-unreads))
9257               (or to-here
9258                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9259     (gnus-summary-position-cursor)))
9260
9261 (defun gnus-summary-catchup-to-here (&optional all)
9262   "Mark all unticked articles before the current one as read.
9263 If ALL is non-nil, also mark ticked and dormant articles as read."
9264   (interactive)
9265   (beginning-of-line)
9266   (gnus-summary-catchup all t (point))
9267   (gnus-set-mode-line 'summary)
9268   (gnus-summary-position-cursor))
9269
9270 (defun gnus-summary-catchup-all (&optional quietly)
9271   "Mark all articles in this newsgroup as read."
9272   (interactive)
9273   (gnus-summary-catchup t quietly))
9274
9275 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9276   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9277 If prefix argument ALL is non-nil, all articles are marked as read."
9278   (interactive "P")
9279   (gnus-summary-catchup all quietly nil 'fast)
9280   ;; Select next newsgroup or exit.
9281   (if (eq gnus-auto-select-next 'quietly)
9282       (gnus-summary-next-group nil)
9283     (gnus-summary-exit)))
9284
9285 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9286   "Mark all articles in this newsgroup as read, and then exit."
9287   (interactive)
9288   (gnus-summary-catchup-and-exit t quietly))
9289
9290 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9291 (defun gnus-summary-catchup-and-goto-next-group (all)
9292   "Mark all articles in this group as read and select the next group.
9293 If given a prefix, mark all articles, unread as well as ticked, as
9294 read." 
9295   (interactive "P")
9296   (gnus-summary-catchup all)
9297   (gnus-summary-next-group))
9298
9299 ;; Thread-based commands.
9300
9301 (defun gnus-summary-toggle-threads (arg)
9302   "Toggle showing conversation threads.
9303 If ARG is positive number, turn showing conversation threads on."
9304   (interactive "P")
9305   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9306     (setq gnus-show-threads
9307           (if (null arg) (not gnus-show-threads)
9308             (> (prefix-numeric-value arg) 0)))
9309     (gnus-summary-prepare)
9310     (gnus-summary-goto-subject current)
9311     (gnus-summary-position-cursor)))
9312
9313 (defun gnus-summary-show-all-threads ()
9314   "Show all threads."
9315   (interactive)
9316   (save-excursion
9317     (let ((buffer-read-only nil))
9318       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9319   (gnus-summary-position-cursor))
9320
9321 (defun gnus-summary-show-thread ()
9322   "Show thread subtrees.
9323 Returns nil if no thread was there to be shown."
9324   (interactive)
9325   (prog1
9326       (save-excursion
9327         (let ((buffer-read-only nil)
9328               ;; first goto end then to beg, to have point at beg after let
9329               (end (progn (end-of-line) (point)))
9330               (beg (progn (beginning-of-line) (point))))
9331           (prog1
9332               ;; Any hidden lines here?
9333               (search-forward "\r" end t)
9334             (subst-char-in-region beg end ?\^M ?\n t))))
9335     (gnus-summary-position-cursor)))
9336
9337 (defun gnus-summary-hide-all-threads ()
9338   "Hide all thread subtrees."
9339   (interactive)
9340   (save-excursion
9341     (goto-char (point-min))
9342     (gnus-summary-hide-thread)
9343     (while (and (not (eobp)) (zerop (forward-line 1)))
9344       (gnus-summary-hide-thread)))
9345   (gnus-summary-position-cursor))
9346
9347 (defun gnus-summary-hide-thread ()
9348   "Hide thread subtrees.
9349 Returns nil if no threads were there to be hidden."
9350   (interactive)
9351   (let ((buffer-read-only nil)
9352         (start (point))
9353         (level (gnus-summary-thread-level))
9354         (end (point)))
9355     ;; Go forward until either the buffer ends or the subthread
9356     ;; ends. 
9357     (if (eobp)
9358         ()
9359       (while (and (zerop (forward-line 1))
9360                   (> (gnus-summary-thread-level) level))
9361         (setq end (point)))
9362       (prog1
9363           (save-excursion
9364             (goto-char end)
9365             (search-backward "\n" start t))
9366         (subst-char-in-region start end ?\n ?\^M t)
9367         (forward-line -1)))))
9368
9369 (defun gnus-summary-go-to-next-thread (&optional previous)
9370   "Go to the same level (or less) next thread.
9371 If PREVIOUS is non-nil, go to previous thread instead.
9372 Return the article number moved to, or nil if moving was impossible."
9373   (let ((level (gnus-summary-thread-level))
9374         (article (gnus-summary-article-number)))
9375     (if previous 
9376         (while (and (zerop (gnus-summary-prev-subject 1))
9377                     (> (gnus-summary-thread-level) level)))
9378       (while (and (zerop (gnus-summary-next-subject 1))
9379                   (> (gnus-summary-thread-level) level))))
9380     (gnus-summary-recenter)
9381     (gnus-summary-position-cursor)
9382     (let ((oart (gnus-summary-article-number)))
9383       (and (/= oart article) oart))))
9384
9385 (defun gnus-summary-next-thread (n)
9386   "Go to the same level next N'th thread.
9387 If N is negative, search backward instead.
9388 Returns the difference between N and the number of skips actually
9389 done."
9390   (interactive "p")
9391   (let ((backward (< n 0))
9392         (n (abs n)))
9393   (while (and (> n 0)
9394               (gnus-summary-go-to-next-thread backward))
9395     (setq n (1- n)))
9396   (gnus-summary-position-cursor)
9397   (if (/= 0 n) (gnus-message 7 "No more threads"))
9398   n))
9399
9400 (defun gnus-summary-prev-thread (n)
9401   "Go to the same level previous N'th thread.
9402 Returns the difference between N and the number of skips actually
9403 done."
9404   (interactive "p")
9405   (gnus-summary-next-thread (- n)))
9406
9407 (defun gnus-summary-go-down-thread (&optional same)
9408   "Go down one level in the current thread.
9409 If SAME is non-nil, also move to articles of the same level."
9410   (let ((level (gnus-summary-thread-level))
9411         (start (point)))
9412     (if (and (zerop (forward-line 1))
9413              (> (gnus-summary-thread-level) level))
9414         t
9415       (goto-char start)
9416       nil)))
9417
9418 (defun gnus-summary-go-up-thread ()
9419   "Go up one level in the current thread."
9420   (let ((level (gnus-summary-thread-level))
9421         (start (point)))
9422     (while (and (zerop (forward-line -1))
9423                 (>= (gnus-summary-thread-level) level)))
9424     (if (>= (gnus-summary-thread-level) level)
9425         (progn
9426           (goto-char start)
9427           nil)
9428       t)))
9429
9430 (defun gnus-summary-down-thread (n)
9431   "Go down thread N steps.
9432 If N is negative, go up instead.
9433 Returns the difference between N and how many steps down that were
9434 taken."
9435   (interactive "p")
9436   (let ((up (< n 0))
9437         (n (abs n)))
9438   (while (and (> n 0)
9439               (if up (gnus-summary-go-up-thread)
9440                 (gnus-summary-go-down-thread)))
9441     (setq n (1- n)))
9442   (gnus-summary-position-cursor)
9443   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9444   n))
9445
9446 (defun gnus-summary-up-thread (n)
9447   "Go up thread N steps.
9448 If N is negative, go up instead.
9449 Returns the difference between N and how many steps down that were
9450 taken."
9451   (interactive "p")
9452   (gnus-summary-down-thread (- n)))
9453
9454 (defun gnus-summary-kill-thread (unmark)
9455   "Mark articles under current thread as read.
9456 If the prefix argument is positive, remove any kinds of marks.
9457 If the prefix argument is negative, tick articles instead."
9458   (interactive "P")
9459   (if unmark
9460       (setq unmark (prefix-numeric-value unmark)))
9461   (let ((killing t)
9462         (level (gnus-summary-thread-level)))
9463     (save-excursion
9464       (while killing
9465         ;; Mark the article...
9466         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9467               ((> unmark 0) (gnus-summary-tick-article nil t))
9468               (t (gnus-summary-tick-article)))
9469         ;; ...and go forward until either the buffer ends or the subtree
9470         ;; ends. 
9471         (if (not (and (zerop (forward-line 1))
9472                       (> (gnus-summary-thread-level) level)))
9473             (setq killing nil))))
9474     ;; Hide killed subtrees.
9475     (and (null unmark)
9476          gnus-thread-hide-killed
9477          (gnus-summary-hide-thread))
9478     ;; If marked as read, go to next unread subject.
9479     (if (null unmark)
9480         ;; Go to next unread subject.
9481         (gnus-summary-next-subject 1 t)))
9482   (gnus-set-mode-line 'summary))
9483
9484 ;; Summary sorting commands
9485
9486 (defun gnus-summary-sort-by-number (&optional reverse)
9487   "Sort summary buffer by article number.
9488 Argument REVERSE means reverse order."
9489   (interactive "P")
9490   (gnus-summary-sort 
9491    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9492
9493 (defun gnus-summary-sort-by-author (&optional reverse)
9494   "Sort summary buffer by author name alphabetically.
9495 If case-fold-search is non-nil, case of letters is ignored.
9496 Argument REVERSE means reverse order."
9497   (interactive "P")
9498   (gnus-summary-sort
9499    (cons
9500     (lambda ()
9501       (let ((extract (funcall
9502                       gnus-extract-address-components
9503                       (header-from (gnus-get-header-by-number
9504                                     (gnus-summary-article-number))))))
9505         (or (car extract) (cdr extract))))
9506     'gnus-thread-sort-by-author)
9507    reverse))
9508
9509 (defun gnus-summary-sort-by-subject (&optional reverse)
9510   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9511 If case-fold-search is non-nil, case of letters is ignored.
9512 Argument REVERSE means reverse order."
9513   (interactive "P")
9514   (gnus-summary-sort
9515    (cons
9516     (lambda ()
9517       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9518     'gnus-thread-sort-by-subject)
9519    reverse))
9520
9521 (defun gnus-summary-sort-by-date (&optional reverse)
9522   "Sort summary buffer by date.
9523 Argument REVERSE means reverse order."
9524   (interactive "P")
9525   (gnus-summary-sort
9526    (cons
9527     (lambda ()
9528       (gnus-sortable-date
9529        (header-date 
9530         (gnus-get-header-by-number (gnus-summary-article-number)))))
9531     'gnus-thread-sort-by-date)
9532    reverse))
9533
9534 (defun gnus-summary-sort-by-score (&optional reverse)
9535   "Sort summary buffer by score.
9536 Argument REVERSE means reverse order."
9537   (interactive "P")
9538   (gnus-summary-sort 
9539    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9540    (not reverse)))
9541
9542 (defvar gnus-summary-already-sorted nil)
9543 (defun gnus-summary-sort (predicate reverse)
9544   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9545   (if gnus-summary-already-sorted
9546       ()
9547     (let (buffer-read-only)
9548       (if (not gnus-show-threads)
9549           (progn
9550             (goto-char (point-min))
9551             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9552         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9553               (gnus-summary-prepare-hook nil)
9554               (gnus-summary-already-sorted nil))
9555           (gnus-summary-prepare)
9556           (and gnus-show-threads
9557                gnus-thread-hide-subtree
9558                (gnus-summary-hide-all-threads))
9559           ;; If in async mode, we send some info to the backend.
9560           (and gnus-newsgroup-async
9561                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9562                (gnus-request-asynchronous 
9563                 gnus-newsgroup-name
9564                 (if (and gnus-asynchronous-article-function
9565                          (fboundp gnus-asynchronous-article-function))
9566                     (funcall gnus-asynchronous-article-function
9567                              gnus-newsgroup-threads)))))))))
9568
9569   
9570 (defun gnus-sortable-date (date)
9571   "Make sortable string by string-lessp from DATE.
9572 Timezone package is used."
9573   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9574          (year   (aref date 0))
9575          (month  (aref date 1))
9576          (day    (aref date 2)))
9577     (timezone-make-sortable-date 
9578      year month day 
9579      (timezone-make-time-string
9580       (aref date 3) (aref date 4) (aref date 5)))))
9581
9582
9583 ;; Summary saving commands.
9584
9585 (defun gnus-summary-save-article (n)
9586   "Save the current article using the default saver function.
9587 If N is a positive number, save the N next articles.
9588 If N is a negative number, save the N previous articles.
9589 If N is nil and any articles have been marked with the process mark,
9590 save those articles instead.
9591 The variable `gnus-default-article-saver' specifies the saver function."
9592   (interactive "P")
9593   (let ((articles (gnus-summary-work-articles n)))
9594     (while articles
9595       (let ((header (gnus-get-header-by-number (car articles))))
9596         (if (vectorp header)
9597             (progn
9598               (gnus-summary-select-article t nil nil (car articles))
9599               (or gnus-save-all-headers
9600                   (gnus-article-hide-headers t))
9601               ;; Remove any X-Gnus lines.
9602               (save-excursion
9603                 (save-restriction
9604                   (set-buffer gnus-article-buffer)
9605                   (let ((buffer-read-only nil))
9606                     (goto-char (point-min))
9607                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9608                                                   (point-max)))
9609                     (while (re-search-forward "^X-Gnus" nil t)
9610                       (beginning-of-line)
9611                       (delete-region (point)
9612                                      (progn (forward-line 1) (point))))
9613                     (widen))))
9614               (save-excursion
9615                 (if gnus-default-article-saver
9616                     (funcall gnus-default-article-saver)
9617                   (error "No default saver is defined."))))
9618           (if (assq 'name header)
9619               (gnus-copy-file (cdr (assq 'name header)))
9620             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9621       (gnus-summary-remove-process-mark (car articles))
9622       (setq articles (cdr articles)))
9623     (gnus-summary-position-cursor)
9624     n))
9625
9626 (defun gnus-summary-pipe-output (arg)
9627   "Pipe the current article to a subprocess.
9628 If N is a positive number, pipe the N next articles.
9629 If N is a negative number, pipe the N previous articles.
9630 If N is nil and any articles have been marked with the process mark,
9631 pipe those articles instead."
9632   (interactive "P")
9633   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9634     (gnus-summary-save-article arg)))
9635
9636 (defun gnus-summary-save-article-mail (arg)
9637   "Append the current article to an mail file.
9638 If N is a positive number, save the N next articles.
9639 If N is a negative number, save the N previous articles.
9640 If N is nil and any articles have been marked with the process mark,
9641 save those articles instead."
9642   (interactive "P")
9643   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9644     (gnus-summary-save-article arg)))
9645
9646 (defun gnus-summary-save-article-rmail (arg)
9647   "Append the current article to an rmail file.
9648 If N is a positive number, save the N next articles.
9649 If N is a negative number, save the N previous articles.
9650 If N is nil and any articles have been marked with the process mark,
9651 save those articles instead."
9652   (interactive "P")
9653   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9654     (gnus-summary-save-article arg)))
9655
9656 (defun gnus-summary-save-article-file (arg)
9657   "Append the current article to a file.
9658 If N is a positive number, save the N next articles.
9659 If N is a negative number, save the N previous articles.
9660 If N is nil and any articles have been marked with the process mark,
9661 save those articles instead."
9662   (interactive "P")
9663   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9664     (gnus-summary-save-article arg)))
9665
9666 (defun gnus-read-save-file-name (prompt default-name)
9667   (let ((methods gnus-split-methods)
9668         split-name)
9669     (if (not gnus-split-methods)
9670         ()
9671       (save-excursion
9672         (set-buffer gnus-article-buffer)
9673         (gnus-narrow-to-headers)
9674         (while methods
9675           (goto-char (point-min))
9676           (and (condition-case () 
9677                    (re-search-forward (car (car methods)) nil t)
9678                  (error nil))
9679                (setq split-name (cons (nth 1 (car methods)) split-name)))
9680           (setq methods (cdr methods)))
9681         (widen)))
9682     (cond ((null split-name)
9683            (read-file-name
9684             (concat prompt " (default "
9685                     (file-name-nondirectory default-name) ") ")
9686             (file-name-directory default-name)
9687             default-name))
9688           ((= 1 (length split-name))
9689            (read-file-name
9690             (concat prompt " (default " (car split-name) ") ")
9691             gnus-article-save-directory
9692             (concat gnus-article-save-directory (car split-name))))
9693           (t
9694            (setq split-name (mapcar (lambda (el) (list el))
9695                                     (nreverse split-name)))
9696            (let ((result (completing-read 
9697                           (concat prompt " ")
9698                           split-name nil nil)))
9699              (concat gnus-article-save-directory
9700                      (if (string= result "")
9701                          (car (car split-name))
9702                        result)))))))
9703
9704 (defun gnus-summary-save-in-rmail (&optional filename)
9705   "Append this article to Rmail file.
9706 Optional argument FILENAME specifies file name.
9707 Directory to save to is default to `gnus-article-save-directory' which
9708 is initialized from the SAVEDIR environment variable."
9709   (interactive)
9710   (let ((default-name
9711           (funcall gnus-rmail-save-name gnus-newsgroup-name
9712                    gnus-current-headers gnus-newsgroup-last-rmail)))
9713     (or filename
9714         (setq filename (gnus-read-save-file-name 
9715                         "Save in rmail file:" default-name)))
9716     (gnus-make-directory (file-name-directory filename))
9717     (gnus-eval-in-buffer-window 
9718      gnus-article-buffer
9719      (save-excursion
9720        (save-restriction
9721          (widen)
9722          (gnus-output-to-rmail filename))))
9723     ;; Remember the directory name to save articles.
9724     (setq gnus-newsgroup-last-rmail filename)))
9725
9726 (defun gnus-summary-save-in-mail (&optional filename)
9727   "Append this article to Unix mail file.
9728 Optional argument FILENAME specifies file name.
9729 Directory to save to is default to `gnus-article-save-directory' which
9730 is initialized from the SAVEDIR environment variable."
9731   (interactive)
9732   (let ((default-name
9733           (funcall gnus-mail-save-name gnus-newsgroup-name
9734                    gnus-current-headers gnus-newsgroup-last-mail)))
9735     (or filename
9736         (setq filename (gnus-read-save-file-name 
9737                         "Save in Unix mail file:" default-name)))
9738     (setq filename
9739           (expand-file-name filename
9740                             (and default-name
9741                                  (file-name-directory default-name))))
9742     (gnus-make-directory (file-name-directory filename))
9743     (gnus-eval-in-buffer-window 
9744      gnus-article-buffer
9745      (save-excursion
9746        (save-restriction
9747          (widen)
9748          (if (and (file-readable-p filename) (rmail-file-p filename))
9749              (gnus-output-to-rmail filename)
9750            (rmail-output filename 1 t t)))))
9751     ;; Remember the directory name to save articles.
9752     (setq gnus-newsgroup-last-mail filename)))
9753
9754 (defun gnus-summary-save-in-file (&optional filename)
9755   "Append this article to file.
9756 Optional argument FILENAME specifies file name.
9757 Directory to save to is default to `gnus-article-save-directory' which
9758 is initialized from the SAVEDIR environment variable."
9759   (interactive)
9760   (let ((default-name
9761           (funcall gnus-file-save-name gnus-newsgroup-name
9762                    gnus-current-headers gnus-newsgroup-last-file)))
9763     (or filename
9764         (setq filename (gnus-read-save-file-name 
9765                         "Save in file:" default-name)))
9766     (gnus-make-directory (file-name-directory filename))
9767     (gnus-eval-in-buffer-window 
9768      gnus-article-buffer
9769      (save-excursion
9770        (save-restriction
9771          (widen)
9772          (gnus-output-to-file filename))))
9773     ;; Remember the directory name to save articles.
9774     (setq gnus-newsgroup-last-file filename)))
9775
9776 (defun gnus-summary-save-in-pipe (&optional command)
9777   "Pipe this article to subprocess."
9778   (interactive)
9779   (let ((command (read-string "Shell command on article: "
9780                               gnus-last-shell-command)))
9781     (if (string-equal command "")
9782         (setq command gnus-last-shell-command))
9783     (gnus-eval-in-buffer-window 
9784      gnus-article-buffer
9785      (save-restriction
9786        (widen)
9787        (shell-command-on-region (point-min) (point-max) command nil)))
9788     (setq gnus-last-shell-command command)))
9789
9790 ;; Summary extract commands
9791
9792 (defun gnus-summary-insert-pseudos (pslist)
9793   (let ((buffer-read-only nil)
9794         (article (gnus-summary-article-number))
9795         b)
9796     (or (gnus-summary-goto-subject article)
9797         (error (format "No such article: %d" article)))
9798     (or gnus-newsgroup-headers-hashtb-by-number
9799         (gnus-make-headers-hashtable-by-number))
9800     (gnus-summary-position-cursor)
9801     ;; If all commands are to be bunched up on one line, we collect
9802     ;; them here.  
9803     (if gnus-view-pseudos-separately
9804         ()
9805       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9806             files action)
9807         (while ps
9808           (setq action (cdr (assq 'action (car ps))))
9809           (setq files (list (cdr (assq 'name (car ps)))))
9810           (while (and ps (cdr ps)
9811                       (string= (or action "1")
9812                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9813             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9814             (setcdr ps (cdr (cdr ps))))
9815           (if (not files)
9816               ()
9817             (if (not (string-match "%s" action))
9818                 (setq files (cons " " files)))
9819             (setq files (cons " " files))
9820             (and (assq 'execute (car ps))
9821                  (setcdr (assq 'execute (car ps))
9822                          (funcall (if (string-match "%s" action)
9823                                       'format 'concat)
9824                                   action 
9825                                   (mapconcat (lambda (f) f) files " ")))))
9826           (setq ps (cdr ps)))))
9827     (if gnus-view-pseudos
9828         (while pslist
9829           (and (assq 'execute (car pslist))
9830                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9831                                      (eq gnus-view-pseudos 'not-confirm)))
9832           (setq pslist (cdr pslist)))
9833       (save-excursion
9834         (while pslist
9835           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9836                                          (gnus-summary-article-number)))
9837           (forward-line 1)
9838           (setq b (point))
9839           (insert "          " (file-name-nondirectory 
9840                                 (cdr (assq 'name (car pslist))))
9841                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9842           (add-text-properties 
9843            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9844                           'gnus-mark gnus-unread-mark 
9845                           'gnus-level 0
9846                           'gnus-pseudo (car pslist)))
9847           (forward-line -1)
9848           (gnus-sethash (int-to-string gnus-reffed-article-number)
9849                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9850           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9851           (setq pslist (cdr pslist)))))))
9852
9853 (defun gnus-pseudos< (p1 p2)
9854   (let ((c1 (cdr (assq 'action p1)))
9855         (c2 (cdr (assq 'action p2))))
9856     (and c1 c2 (string< c1 c2))))
9857
9858 (defun gnus-request-pseudo-article (props)
9859   (cond ((assq 'execute props)
9860          (gnus-execute-command (cdr (assq 'execute props)))))
9861   (let ((gnus-current-article (gnus-summary-article-number)))
9862     (run-hooks 'gnus-mark-article-hook)))
9863
9864 (defun gnus-execute-command (command &optional automatic)
9865   (save-excursion
9866     (gnus-article-setup-buffer)
9867     (set-buffer gnus-article-buffer)
9868     (let ((command (if automatic command (read-string "Command: " command)))
9869           (buffer-read-only nil))
9870       (erase-buffer)
9871       (insert "$ " command "\n\n")
9872       (if gnus-view-pseudo-asynchronously
9873           (start-process "gnus-execute" nil "sh" "-c" command)
9874         (call-process "sh" nil t nil "-c" command)))))
9875
9876 (defun gnus-copy-file (file &optional to)
9877   "Copy FILE to TO."
9878   (interactive
9879    (list (read-file-name "Copy file: " default-directory)
9880          (read-file-name "Copy file to: " default-directory)))
9881   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9882   (and (file-directory-p to) 
9883        (setq to (concat (file-name-as-directory to)
9884                         (file-name-nondirectory file))))
9885   (copy-file file to))
9886
9887 ;; Summary kill commands.
9888
9889 (defun gnus-summary-edit-global-kill (article)
9890   "Edit the \"global\" kill file."
9891   (interactive (list (gnus-summary-article-number)))
9892   (gnus-group-edit-global-kill article))
9893
9894 (defun gnus-summary-edit-local-kill ()
9895   "Edit a local kill file applied to the current newsgroup."
9896   (interactive)
9897   (setq gnus-current-headers 
9898         (gnus-gethash 
9899          (int-to-string (gnus-summary-article-number))
9900          gnus-newsgroup-headers-hashtb-by-number))
9901   (gnus-set-global-variables)
9902   (gnus-group-edit-local-kill 
9903    (gnus-summary-article-number) gnus-newsgroup-name))
9904
9905 \f
9906 ;;;
9907 ;;; Gnus article mode
9908 ;;;
9909
9910 (put 'gnus-article-mode 'mode-class 'special)
9911
9912 (if gnus-article-mode-map
9913     nil
9914   (setq gnus-article-mode-map (make-keymap))
9915   (suppress-keymap gnus-article-mode-map)
9916   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9917   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9918   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9919   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9920   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9921   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9922   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9923   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9924   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9925   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9926   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9927   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
9928   
9929   ;; Duplicate almost all summary keystrokes in the article mode map.
9930   (let ((commands 
9931          (list "#" "\M-#" "\C-c\M-#" "n" "p"
9932                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9933                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9934                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k""x" "X" 
9935                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9936                "t" "\M-t" "C" "S"
9937                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9938                "V" "\C-c\C-d")))
9939     (while commands
9940       (define-key gnus-article-mode-map (car commands) 
9941         'gnus-article-summary-command)
9942       (setq commands (cdr commands))))
9943
9944   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9945     (while commands
9946       (define-key gnus-article-mode-map (car commands) 
9947         'gnus-article-summary-command-nosave)
9948       (setq commands (cdr commands)))))
9949
9950
9951 (defun gnus-article-mode ()
9952   "Major mode for displaying an article.
9953
9954 All normal editing commands are switched off.
9955
9956 The following commands are available:
9957
9958 \\<gnus-article-mode-map>
9959 \\[gnus-article-next-page]\t Scroll the article one page forwards
9960 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9961 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9962 \\[gnus-article-show-summary]\t Display the summary buffer
9963 \\[gnus-article-mail]\t Send a reply to the address near point
9964 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9965 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9966 \\[gnus-info-find-node]\t Go to the Gnus info node"
9967   (interactive)
9968   (if gnus-visual (gnus-article-make-menu-bar))
9969   (kill-all-local-variables)
9970   (setq mode-line-modified "-- ")
9971   (make-local-variable 'mode-line-format)
9972   (setq mode-line-format (copy-sequence mode-line-format))
9973   (and (equal (nth 3 mode-line-format) "   ")
9974        (setcar (nthcdr 3 mode-line-format) ""))
9975   (setq mode-name "Article")
9976   (setq major-mode 'gnus-article-mode)
9977   (make-local-variable 'minor-mode-alist)
9978   (or (assq 'gnus-show-mime minor-mode-alist)
9979       (setq minor-mode-alist
9980             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9981   (use-local-map gnus-article-mode-map)
9982   (make-local-variable 'page-delimiter)
9983   (setq page-delimiter gnus-page-delimiter)
9984   (buffer-disable-undo (current-buffer))
9985   (setq buffer-read-only t)             ;Disable modification
9986   (run-hooks 'gnus-article-mode-hook))
9987
9988 (defun gnus-article-setup-buffer ()
9989   "Initialize article mode buffer."
9990   (if (get-buffer gnus-article-buffer)
9991       (save-excursion
9992         (set-buffer gnus-article-buffer)
9993         (gnus-add-current-to-buffer-list)
9994         (or (eq major-mode 'gnus-article-mode)
9995             (gnus-article-mode)))
9996     (save-excursion
9997       (set-buffer (get-buffer-create gnus-article-buffer))
9998       (gnus-add-current-to-buffer-list)
9999       (gnus-article-mode))))
10000
10001 ;; Set article window start at LINE, where LINE is the number of lines
10002 ;; from the head of the article.
10003 (defun gnus-article-set-window-start (&optional line)
10004   (set-window-start 
10005    (get-buffer-window gnus-article-buffer)
10006    (save-excursion
10007      (set-buffer gnus-article-buffer)
10008      (goto-char (point-min))
10009      (if (not line)
10010          (point-min)
10011        (gnus-message 6 "Moved to bookmark")
10012        (search-forward "\n\n" nil t)
10013        (forward-line line)
10014        (point)))))
10015
10016 (defun gnus-request-article-this-buffer (article group)
10017   "Get an article and insert it into this buffer."
10018   (setq group (or group gnus-newsgroup-name))
10019
10020   ;; Open server if it has closed.
10021   (gnus-check-news-server (gnus-find-method-for-group group))
10022
10023   ;; Using `gnus-request-article' directly will insert the article into
10024   ;; `nntp-server-buffer' - so we'll save some time by not having to
10025   ;; copy it from the server buffer into the article buffer.
10026
10027   ;; We only request an article by message-id when we do not have the
10028   ;; headers for it, so we'll have to get those.
10029   (and (stringp article) 
10030        (let ((gnus-override-method gnus-refer-article-method))
10031          (gnus-read-header article)))
10032
10033   ;; If the article number is negative, that means that this article
10034   ;; doesn't belong in this newsgroup (possibly), so we find its
10035   ;; message-id and request it by id instead of number.
10036   (if (not (numberp article))
10037       ()
10038     (save-excursion
10039       (set-buffer gnus-summary-buffer)
10040       (let ((header (gnus-get-header-by-number article)))
10041         (if (< article 0)
10042             (if (vectorp header)
10043                 ;; It's a real article.
10044                 (setq article (header-id header))
10045               ;; It is an extracted pseudo-article.
10046               (setq article 'pseudo)
10047               (gnus-request-pseudo-article header)))
10048
10049         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10050           (if (not (eq (car method) 'nneething))
10051               ()
10052             (let ((dir (concat (file-name-as-directory (nth 1 method))
10053                                (header-subject header))))
10054               (if (file-directory-p dir)
10055                   (progn
10056                     (setq article 'nneething)
10057                     (gnus-group-enter-directory dir)))))))))
10058
10059   ;; Check the cache.
10060   (if (and gnus-use-cache
10061            (numberp article)
10062            (gnus-cache-request-article article group))
10063       'article
10064     ;; Get the article and into the article buffer.
10065     (if (or (stringp article) (numberp article))
10066         (progn
10067           (erase-buffer)
10068           (let ((gnus-override-method 
10069                  (and (stringp article) gnus-refer-article-method)))
10070             (and (gnus-request-article article group (current-buffer))
10071                  'article)))
10072       article)))
10073
10074 (defun gnus-read-header (id)
10075   "Read the headers of article ID and enter them into the Gnus system."
10076   (or gnus-newsgroup-headers-hashtb-by-number
10077       (gnus-make-headers-hashtable-by-number))
10078   (let (header)
10079     (if (not (setq header 
10080                    (car (if (let ((gnus-nov-is-evil t))
10081                               (gnus-retrieve-headers 
10082                                (list id) gnus-newsgroup-name))
10083                             (gnus-get-newsgroup-headers)))))
10084         nil
10085       (if (stringp id)
10086           (header-set-number header gnus-reffed-article-number))
10087       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10088       (gnus-sethash (int-to-string (header-number header)) header
10089                     gnus-newsgroup-headers-hashtb-by-number)
10090       (if (stringp id)
10091           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10092       (setq gnus-current-headers header)
10093       header)))
10094
10095 (defun gnus-article-prepare (article &optional all-headers header)
10096   "Prepare ARTICLE in article mode buffer.
10097 ARTICLE should either be an article number or a Message-ID.
10098 If ARTICLE is an id, HEADER should be the article headers.
10099 If ALL-HEADERS is non-nil, no headers are hidden."
10100   (save-excursion
10101     ;; Make sure we start in a summary buffer.
10102     (or (eq major-mode 'gnus-summary-mode)
10103         (set-buffer gnus-summary-buffer))
10104     (setq gnus-summary-buffer (current-buffer))
10105     ;; Make sure the connection to the server is alive.
10106     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10107         (progn
10108           (gnus-check-news-server 
10109            (gnus-find-method-for-group gnus-newsgroup-name))
10110           (gnus-request-group gnus-newsgroup-name t)))
10111     (or gnus-newsgroup-headers-hashtb-by-number
10112         (gnus-make-headers-hashtable-by-number))
10113     (let* ((article (if header (header-number header) article))
10114            (summary-buffer (current-buffer))
10115            (internal-hook gnus-article-internal-prepare-hook)
10116            (group gnus-newsgroup-name)
10117            result)
10118       (save-excursion
10119         (gnus-article-setup-buffer)
10120         (set-buffer gnus-article-buffer)
10121         (if (not (setq result (let ((buffer-read-only nil))
10122                                 (gnus-request-article-this-buffer 
10123                                  article group))))
10124             ;; There is no such article.
10125             (save-excursion
10126               (if (not (numberp article))
10127                   ()
10128                 (setq gnus-article-current 
10129                       (cons gnus-newsgroup-name article))
10130                 (set-buffer gnus-summary-buffer)
10131                 (setq gnus-current-article article)
10132                 (gnus-summary-mark-as-read article gnus-canceled-mark))
10133               (gnus-message 1 "No such article (may be canceled)")
10134               (ding)
10135               nil)
10136           (if (or (eq result 'pseudo) (eq result 'nneething))
10137               (progn
10138                 (save-excursion
10139                   (set-buffer summary-buffer)
10140                   (setq gnus-last-article gnus-current-article
10141                         gnus-newsgroup-history (cons gnus-current-article
10142                                                      gnus-newsgroup-history)
10143                         gnus-current-article 0
10144                         gnus-current-headers nil
10145                         gnus-article-current nil)
10146                   (if (eq result 'nneething)
10147                       (gnus-configure-windows 'summary)
10148                     (gnus-configure-windows 'article))
10149                   (gnus-set-global-variables))
10150                 (gnus-set-mode-line 'article))
10151             ;; The result from the `request' was an actual article -
10152             ;; or at least some text that is now displayed in the
10153             ;; article buffer.
10154             (if (and (numberp article)
10155                      (not (eq article gnus-current-article)))
10156                 ;; Seems like a new article has been selected.
10157                 ;; `gnus-current-article' must be an article number.
10158                 (save-excursion
10159                   (set-buffer summary-buffer)
10160                   (setq gnus-last-article gnus-current-article
10161                         gnus-newsgroup-history (cons gnus-current-article
10162                                                      gnus-newsgroup-history)
10163                         gnus-current-article article
10164                         gnus-current-headers 
10165                         (gnus-get-header-by-number gnus-current-article)
10166                         gnus-article-current 
10167                         (cons gnus-newsgroup-name gnus-current-article))
10168                   (gnus-summary-show-thread)
10169                   (run-hooks 'gnus-mark-article-hook)
10170                   (gnus-set-mode-line 'summary)
10171                   (and gnus-visual 
10172                        (run-hooks 'gnus-visual-mark-article-hook))
10173                   ;; Set the global newsgroup variables here.
10174                   ;; Suggested by Jim Sisolak
10175                   ;; <sisolak@trans4.neep.wisc.edu>.
10176                   (gnus-set-global-variables)
10177                   (and gnus-use-cache 
10178                        (gnus-cache-possibly-enter-article
10179                         group article
10180                         (gnus-get-header-by-number article)
10181                         (memq article gnus-newsgroup-marked)
10182                         (memq article gnus-newsgroup-dormant)
10183                         (memq article gnus-newsgroup-unreads)))))
10184             ;; gnus-have-all-headers must be either T or NIL.
10185             (setq gnus-have-all-headers
10186                   (not (not (or all-headers gnus-show-all-headers))))
10187             ;; Hooks for getting information from the article.
10188             ;; This hook must be called before being narrowed.
10189             (let (buffer-read-only)
10190               (run-hooks 'internal-hook)
10191               (run-hooks 'gnus-article-prepare-hook)
10192               ;; Decode MIME message.
10193               (if (and gnus-show-mime
10194                        (or (not gnus-strict-mime)
10195                            (gnus-fetch-field "Mime-Version")))
10196                   (funcall gnus-show-mime-method))
10197               ;; Perform the article display hooks.
10198               (run-hooks 'gnus-article-display-hook))
10199             ;; Do page break.
10200             (goto-char (point-min))
10201             (and gnus-break-pages (gnus-narrow-to-page))
10202             (gnus-set-mode-line 'article)
10203             (gnus-configure-windows 'article)
10204             (goto-char (point-min))
10205             t))))))
10206
10207 (defun gnus-article-show-all-headers ()
10208   "Show all article headers in article mode buffer."
10209   (save-excursion 
10210     (gnus-article-setup-buffer)
10211     (set-buffer gnus-article-buffer)
10212     (let ((buffer-read-only nil))
10213       (remove-text-properties (point-min) (point-max) 
10214                               gnus-hidden-properties))))
10215
10216 (defun gnus-article-hide-headers-if-wanted ()
10217   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10218 Provided for backwards compatability."
10219   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10220       (gnus-article-hide-headers)))
10221
10222 (defun gnus-article-hide-headers (&optional delete)
10223   "Hide unwanted headers and possibly sort them as well."
10224   (interactive "P")
10225   (save-excursion
10226     (set-buffer gnus-article-buffer)
10227     (save-restriction
10228       (let ((sorted gnus-sorted-header-list)
10229             (buffer-read-only nil)
10230             want want-list beg want-l)
10231         ;; First we narrow to just the headers.
10232         (widen)
10233         (goto-char (point-min))
10234         ;; Hide any "From " lines at the beginning of (mail) articles. 
10235         (while (looking-at "From ")
10236           (forward-line 1))
10237         (if (bobp) 
10238             (add-text-properties (point-min) (point) gnus-hidden-properties))
10239         ;; Then treat the rest of the header lines.
10240         (narrow-to-region 
10241          (point) 
10242          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10243         ;; Then we use the two regular expressions
10244         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10245         ;; select which header lines is to remain visible in the
10246         ;; article buffer.
10247         (goto-char (point-min))
10248         (while (re-search-forward "^[^ \t]*:" nil t)
10249           (beginning-of-line)
10250           ;; We add the headers we want to keep to a list and delete
10251           ;; them from the buffer.
10252           (if (or (and (stringp gnus-visible-headers)
10253                        (looking-at gnus-visible-headers))
10254                   (and (not (stringp gnus-visible-headers))
10255                        (stringp gnus-ignored-headers)
10256                        (not (looking-at gnus-ignored-headers))))
10257               (progn
10258                 (setq beg (point))
10259                 (forward-line 1)
10260                 ;; Be sure to get multi-line headers...
10261                 (re-search-forward "^[^ \t]*:" nil t)
10262                 (beginning-of-line)
10263                 (setq want-list 
10264                       (cons (buffer-substring beg (point)) want-list))
10265                 (delete-region beg (point))
10266                 (goto-char beg))
10267             (forward-line 1)))
10268         ;; Next we perform the sorting by looking at
10269         ;; `gnus-sorted-header-list'. 
10270         (goto-char (point-min))
10271         (while (and sorted want-list)
10272           (setq want-l want-list)
10273           (while (and want-l
10274                       (not (string-match (car sorted) (car want-l))))
10275             (setq want-l (cdr want-l)))
10276           (if want-l 
10277               (progn
10278                 (insert (car want-l))
10279                 (setq want-list (delq (car want-l) want-list))))
10280           (setq sorted (cdr sorted)))
10281         ;; Any headers that were not matched by the sorted list we
10282         ;; just tack on the end of the visible header list.
10283         (while want-list
10284           (insert (car want-list))
10285           (setq want-list (cdr want-list)))
10286         ;; And finally we make the unwanted headers invisible.
10287         (if delete
10288             (delete-region (point) (point-max))
10289           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10290           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10291
10292 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10293 (defun gnus-article-treat-overstrike ()
10294   "Translate overstrikes into bold text."
10295   (interactive)
10296   (save-excursion
10297     (set-buffer gnus-article-buffer)
10298     (let ((buffer-read-only nil))
10299       (while (search-forward "\b" nil t)
10300         (let ((next (following-char))
10301               (previous (char-after (- (point) 2))))
10302           (cond ((eq next previous)
10303                  (delete-region (- (point) 2) (point))
10304                  (put-text-property (point) (1+ (point))
10305                                     'face 'bold))
10306                 ((eq next ?_)
10307                  (delete-region (1- (point)) (1+ (point)))
10308                  (put-text-property (1- (point)) (point)
10309                                     'face 'underline))
10310                 ((eq previous ?_)
10311                  (delete-region (- (point) 2) (point))
10312                  (put-text-property (point) (1+ (point))
10313                                     'face 'underline))))))))
10314
10315 (defun gnus-article-word-wrap ()
10316   "Format too long lines."
10317   (interactive)
10318   (save-excursion
10319     (set-buffer gnus-article-buffer)
10320     (let ((buffer-read-only nil))
10321       (goto-char (point-min))
10322       (search-forward "\n\n" nil t)
10323       (end-of-line 1)
10324       (let ((paragraph-start "^\\W")
10325             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10326             (adaptive-fill-mode t))
10327         (while (not (eobp))
10328           (and (>= (current-column) (min fill-column (window-width)))
10329                (/= (preceding-char) ?:)
10330                (fill-paragraph nil))
10331           (end-of-line 2))))))
10332
10333 (defun gnus-article-remove-cr ()
10334   "Remove carriage returns from an article."
10335   (interactive)
10336   (save-excursion
10337     (set-buffer gnus-article-buffer)
10338     (let ((buffer-read-only nil))
10339       (goto-char (point-min))
10340       (while (search-forward "\r" nil t)
10341         (replace-match "" t t)))))
10342
10343 (defun gnus-article-display-x-face (&optional force)
10344   "Look for an X-Face header and display it if present."
10345   (interactive (list 'force))
10346   (save-excursion
10347     (set-buffer gnus-article-buffer)
10348     (let ((inhibit-point-motion-hooks t)
10349           (case-fold-search nil))
10350       (save-restriction
10351         (goto-char (point-min))
10352         (search-forward "\n\n")
10353         (narrow-to-region (point-min) (point))
10354         (goto-char (point-min))
10355         (if (or (not gnus-article-x-face-command)
10356                 (and (not force)
10357                      (or (not gnus-article-x-face-too-ugly)
10358                          (string-match gnus-article-x-face-too-ugly
10359                                        (mail-fetch-field "from"))))
10360                 (progn
10361                   (goto-char (point-min))
10362                   (not (re-search-forward "^X-Face: " nil t))))
10363             nil
10364           (let ((beg (point))
10365                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10366             (if (symbolp gnus-article-x-face-command)
10367                 (and (or (fboundp gnus-article-x-face-command)
10368                          (error "%s is not a function"
10369                                 gnus-article-x-face-command))
10370                      (funcall gnus-article-x-face-command beg end))
10371               (call-process-region beg end "sh" nil 0 nil
10372                                    "-c" gnus-article-x-face-command))))))))
10373
10374 (defun gnus-article-de-quoted-unreadable (&optional force)
10375   "Do a naïve translation of a quoted-printable-encoded article.
10376 This is in no way, shape or form meant as a replacement for real MIME
10377 processing, but is simply a stop-gap measure until MIME support is
10378 written.
10379 If FORCE, decode the article whether it is marked as quoted-printable
10380 or not." 
10381   (interactive (list 'force))
10382   (save-excursion
10383     (set-buffer gnus-article-buffer)
10384     (let ((case-fold-search t)
10385           (buffer-read-only nil)
10386           (type (gnus-fetch-field "content-transfer-encoding")))
10387       (if (or force (and type (string-match "quoted-printable" type)))
10388           (progn
10389             (goto-char (point-min))
10390             (search-forward "\n\n" nil 'move)
10391             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10392
10393 (defun gnus-mime-decode-quoted-printable (from to)
10394   ;; Decode quoted-printable from region between FROM and TO.
10395   (save-excursion
10396     (goto-char from)
10397     (while (search-forward "=" to t)
10398       (cond ((eq (following-char) ?\n)
10399              (delete-char -1)
10400              (delete-char 1))
10401             ((looking-at "[0-9A-F][0-9A-F]")
10402              (delete-char -1)
10403              (insert (hexl-hex-string-to-integer
10404                       (buffer-substring (point) (+ 2 (point)))))
10405              (delete-char 2))
10406             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10407
10408 (defvar gnus-article-time-units
10409   (list (cons 'year (* 365.25 24 60 60))
10410         (cons 'week (* 7 24 60 60))
10411         (cons 'day (* 24 60 60))
10412         (cons 'hour (* 60 60))
10413         (cons 'minute 60)
10414         (cons 'second 1)))
10415
10416 (defun gnus-article-date-ut (&optional type)
10417   "Convert DATE date to universal time in the current article.
10418 If TYPE is `local', convert to local time; if it is `lapsed', output
10419 how much time has lapsed since DATE."
10420   (interactive (list 'ut))
10421   (let ((date (header-date (or gnus-current-headers 
10422                                (gnus-get-header-by-number
10423                                 (gnus-summary-article-number))"")))
10424         (date-regexp "^Date: \\|^X-Sent: "))
10425     (if (or (not date)
10426             (string= date ""))
10427         ()
10428       (save-excursion
10429         (set-buffer gnus-article-buffer)
10430         (let ((buffer-read-only nil))
10431           (goto-char (point-min))
10432           (if (and (re-search-forward date-regexp nil t)
10433                    (progn 
10434                      (beginning-of-line)
10435                      (looking-at date-regexp)))
10436               (delete-region (gnus-point-at-bol)
10437                              (progn (end-of-line) (1+ (point))))
10438             (goto-char (point-min))
10439             (goto-char (- (search-forward "\n\n") 2)))
10440           (insert
10441            (cond 
10442             ((eq type 'local)
10443              (concat "Date: " (condition-case ()
10444                                   (timezone-make-date-arpa-standard date)
10445                                 (error date))
10446                      "\n"))
10447             ((eq type 'ut)
10448              (concat "Date: "
10449                      (condition-case ()
10450                          (timezone-make-date-arpa-standard date nil "UT")
10451                        (error date))
10452                      "\n"))
10453             ((eq type 'lapsed)
10454              ;; If the date is seriously mangled, the timezone
10455              ;; functions are liable to bug out, so we condition-case
10456              ;; the entire thing.  
10457              (let* ((sec (condition-case ()
10458                              (max (- (gnus-seconds-since-epoch 
10459                                       (timezone-make-date-arpa-standard
10460                                        (current-time-string) 
10461                                        (current-time-zone) "UT"))
10462                                      (gnus-seconds-since-epoch 
10463                                       (timezone-make-date-arpa-standard 
10464                                        date nil "UT")))
10465                                   0)
10466                            (error 0)))
10467                     num prev)
10468                (concat
10469                 "X-Sent: "
10470                 (mapconcat 
10471                  (lambda (unit)
10472                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10473                        ""
10474                      (setq sec (- sec (* num (cdr unit))))
10475                      (prog1
10476                          (concat (if prev ", " "") (int-to-string (floor num))
10477                                  " " (symbol-name (car unit))
10478                                  (if (> num 1) "s" ""))
10479                        (setq prev t))))
10480                  gnus-article-time-units "")
10481                 " ago\n")))
10482             (t
10483              (error "Unknown conversion type: %s" type)))))))))
10484
10485 (defun gnus-article-date-local ()
10486   "Convert the current article date to the local timezone."
10487   (interactive)
10488   (gnus-article-date-ut 'local))
10489
10490 (defun gnus-article-date-lapsed ()
10491   "Convert the current article date to time lapsed since it was sent."
10492   (interactive)
10493   (gnus-article-date-ut 'lapsed))
10494
10495 (defun gnus-article-maybe-highlight ()
10496   (if gnus-visual (gnus-article-highlight)))
10497
10498 ;; Article savers.
10499
10500 (defun gnus-output-to-rmail (file-name)
10501   "Append the current article to an Rmail file named FILE-NAME."
10502   (require 'rmail)
10503   ;; Most of these codes are borrowed from rmailout.el.
10504   (setq file-name (expand-file-name file-name))
10505   (setq rmail-default-rmail-file file-name)
10506   (let ((artbuf (current-buffer))
10507         (tmpbuf (get-buffer-create " *Gnus-output*")))
10508     (save-excursion
10509       (or (get-file-buffer file-name)
10510           (file-exists-p file-name)
10511           (if (gnus-yes-or-no-p
10512                (concat "\"" file-name "\" does not exist, create it? "))
10513               (let ((file-buffer (create-file-buffer file-name)))
10514                 (save-excursion
10515                   (set-buffer file-buffer)
10516                   (rmail-insert-rmail-file-header)
10517                   (let ((require-final-newline nil))
10518                     (write-region (point-min) (point-max) file-name t 1)))
10519                 (kill-buffer file-buffer))
10520             (error "Output file does not exist")))
10521       (set-buffer tmpbuf)
10522       (buffer-disable-undo (current-buffer))
10523       (erase-buffer)
10524       (insert-buffer-substring artbuf)
10525       (gnus-convert-article-to-rmail)
10526       ;; Decide whether to append to a file or to an Emacs buffer.
10527       (let ((outbuf (get-file-buffer file-name)))
10528         (if (not outbuf)
10529             (append-to-file (point-min) (point-max) file-name)
10530           ;; File has been visited, in buffer OUTBUF.
10531           (set-buffer outbuf)
10532           (let ((buffer-read-only nil)
10533                 (msg (and (boundp 'rmail-current-message)
10534                           (symbol-value 'rmail-current-message))))
10535             ;; If MSG is non-nil, buffer is in RMAIL mode.
10536             (if msg
10537                 (progn (widen)
10538                        (narrow-to-region (point-max) (point-max))))
10539             (insert-buffer-substring tmpbuf)
10540             (if msg
10541                 (progn
10542                   (goto-char (point-min))
10543                   (widen)
10544                   (search-backward "\^_")
10545                   (narrow-to-region (point) (point-max))
10546                   (goto-char (1+ (point-min)))
10547                   (rmail-count-new-messages t)
10548                   (rmail-show-message msg)))))))
10549     (kill-buffer tmpbuf)))
10550
10551 (defun gnus-output-to-file (file-name)
10552   "Append the current article to a file named FILE-NAME."
10553   (setq file-name (expand-file-name file-name))
10554   (let ((artbuf (current-buffer))
10555         (tmpbuf (get-buffer-create " *Gnus-output*")))
10556     (save-excursion
10557       (set-buffer tmpbuf)
10558       (buffer-disable-undo (current-buffer))
10559       (erase-buffer)
10560       (insert-buffer-substring artbuf)
10561       ;; Append newline at end of the buffer as separator, and then
10562       ;; save it to file.
10563       (goto-char (point-max))
10564       (insert "\n")
10565       (append-to-file (point-min) (point-max) file-name))
10566     (kill-buffer tmpbuf)))
10567
10568 (defun gnus-convert-article-to-rmail ()
10569   "Convert article in current buffer to Rmail message format."
10570   (let ((buffer-read-only nil))
10571     ;; Convert article directly into Babyl format.
10572     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10573     (goto-char (point-min))
10574     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10575     (while (search-forward "\n\^_" nil t) ;single char
10576       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10577     (goto-char (point-max))
10578     (insert "\^_")))
10579
10580 (defun gnus-narrow-to-page (&optional arg)
10581   "Make text outside current page invisible except for page delimiter.
10582 A numeric arg specifies to move forward or backward by that many pages,
10583 thus showing a page other than the one point was originally in."
10584   (interactive "P")
10585   (setq arg (if arg (prefix-numeric-value arg) 0))
10586   (save-excursion
10587     (forward-page -1)                   ;Beginning of current page.
10588     (widen)
10589     (if (> arg 0)
10590         (forward-page arg)
10591       (if (< arg 0)
10592           (forward-page (1- arg))))
10593     ;; Find the end of the page.
10594     (forward-page)
10595     ;; If we stopped due to end of buffer, stay there.
10596     ;; If we stopped after a page delimiter, put end of restriction
10597     ;; at the beginning of that line.
10598     ;; These are commented out.
10599     ;;    (if (save-excursion (beginning-of-line)
10600     ;;                  (looking-at page-delimiter))
10601     ;;  (beginning-of-line))
10602     (narrow-to-region (point)
10603                       (progn
10604                         ;; Find the top of the page.
10605                         (forward-page -1)
10606                         ;; If we found beginning of buffer, stay there.
10607                         ;; If extra text follows page delimiter on same line,
10608                         ;; include it.
10609                         ;; Otherwise, show text starting with following line.
10610                         (if (and (eolp) (not (bobp)))
10611                             (forward-line 1))
10612                         (point)))))
10613
10614 (defun gnus-gmt-to-local ()
10615   "Rewrite Date header described in GMT to local in current buffer.
10616 Intended to be used with gnus-article-prepare-hook."
10617   (save-excursion
10618     (save-restriction
10619       (widen)
10620       (goto-char (point-min))
10621       (narrow-to-region (point-min)
10622                         (progn (search-forward "\n\n" nil 'move) (point)))
10623       (goto-char (point-min))
10624       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10625           (let ((buffer-read-only nil)
10626                 (date (buffer-substring-no-properties
10627                        (match-beginning 1) (match-end 1))))
10628             (delete-region (match-beginning 1) (match-end 1))
10629             (insert
10630              (timezone-make-date-arpa-standard 
10631               date nil (current-time-zone))))))))
10632
10633
10634 ;; Article mode commands
10635
10636 (defun gnus-article-next-page (lines)
10637   "Show next page of current article.
10638 If end of article, return non-nil. Otherwise return nil.
10639 Argument LINES specifies lines to be scrolled up."
10640   (interactive "P")
10641   (move-to-window-line -1)
10642   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10643   (if (save-excursion
10644         (end-of-line)
10645         (and (pos-visible-in-window-p)  ;Not continuation line.
10646              (eobp)))
10647       ;; Nothing in this page.
10648       (if (or (not gnus-break-pages)
10649               (save-excursion
10650                 (save-restriction
10651                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10652           t                             ;Nothing more.
10653         (gnus-narrow-to-page 1)         ;Go to next page.
10654         nil)
10655     ;; More in this page.
10656     (condition-case ()
10657         (scroll-up lines)
10658       (end-of-buffer
10659        ;; Long lines may cause an end-of-buffer error.
10660        (goto-char (point-max))))
10661     nil))
10662
10663 (defun gnus-article-prev-page (lines)
10664   "Show previous page of current article.
10665 Argument LINES specifies lines to be scrolled down."
10666   (interactive "P")
10667   (move-to-window-line 0)
10668   (if (and gnus-break-pages
10669            (bobp)
10670            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10671       (progn
10672         (gnus-narrow-to-page -1) ;Go to previous page.
10673         (goto-char (point-max))
10674         (recenter -1))
10675     (scroll-down lines)))
10676
10677 (defun gnus-article-refer-article ()
10678   "Read article specified by message-id around point."
10679   (interactive)
10680   (search-forward ">" nil t)    ;Move point to end of "<....>".
10681   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10682       (let ((message-id
10683              (buffer-substring (match-beginning 1) (match-end 1))))
10684         (set-buffer gnus-summary-buffer)
10685         (gnus-summary-refer-article message-id))
10686     (error "No references around point")))
10687
10688 (defun gnus-article-show-summary ()
10689   "Reconfigure windows to show summary buffer."
10690   (interactive)
10691   (gnus-configure-windows 'article)
10692   (gnus-summary-goto-subject gnus-current-article))
10693
10694 (defun gnus-article-describe-briefly ()
10695   "Describe article mode commands briefly."
10696   (interactive)
10697   (gnus-message 6
10698    (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")))
10699
10700 (defun gnus-article-summary-command ()
10701   "Execute the last keystroke in the summary buffer."
10702   (interactive)
10703   (let ((obuf (current-buffer))
10704         (owin (current-window-configuration))
10705         func)
10706     (switch-to-buffer gnus-summary-buffer 'norecord)
10707     (setq func (lookup-key (current-local-map) (this-command-keys)))
10708     (call-interactively func)
10709     (set-buffer obuf)
10710     (let ((npoint (point)))
10711       (set-window-configuration owin)
10712       (set-window-start (get-buffer-window (current-buffer)) (point)))))
10713
10714 (defun gnus-article-summary-command-nosave ()
10715   "Execute the last keystroke in the summary buffer."
10716   (interactive)
10717   (let ((obuf (current-buffer))
10718         (owin (current-window-configuration))
10719         func)
10720     (switch-to-buffer gnus-summary-buffer 'norecord)
10721     (setq func (lookup-key (current-local-map) (this-command-keys)))
10722     (call-interactively func)))
10723
10724 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10725 ;; Modified by tower@prep Nov 86
10726 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10727
10728 (defun gnus-caesar-region (&optional n)
10729   "Caesar rotation of region by N, default 13, for decrypting netnews.
10730 ROT47 will be performed for Japanese text in any case."
10731   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10732                    (list (prefix-numeric-value current-prefix-arg))
10733                  (list nil)))
10734   (cond ((not (numberp n)) (setq n 13))
10735         (t (setq n (mod n 26))))        ;canonicalize N
10736   (if (not (zerop n))           ; no action needed for a rot of 0
10737       (progn
10738         (if (or (not (boundp 'caesar-translate-table))
10739                 (not caesar-translate-table)
10740                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10741             (let ((i 0) 
10742                   (lower "abcdefghijklmnopqrstuvwxyz")
10743                   upper)
10744               (gnus-message 9 "Building caesar-translate-table...")
10745               (setq caesar-translate-table (make-vector 256 0))
10746               (while (< i 256)
10747                 (aset caesar-translate-table i i)
10748                 (setq i (1+ i)))
10749               (setq lower (concat lower lower)
10750                     upper (upcase lower)
10751                     i 0)
10752               (while (< i 26)
10753                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10754                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10755                 (setq i (1+ i)))
10756               ;; ROT47 for Japanese text.
10757               ;; Thanks to ichikawa@flab.fujitsu.junet.
10758               (setq i 161)
10759               (let ((t1 (logior ?O 128))
10760                     (t2 (logior ?! 128))
10761                     (t3 (logior ?~ 128)))
10762                 (while (< i 256)
10763                   (aset caesar-translate-table i
10764                         (let ((v (aref caesar-translate-table i)))
10765                           (if (<= v t1) (if (< v t2) v (+ v 47))
10766                             (if (<= v t3) (- v 47) v))))
10767                   (setq i (1+ i))))
10768               (gnus-message 9 "Building caesar-translate-table...done")))
10769         (let ((from (region-beginning))
10770               (to (region-end))
10771               (i 0) str len)
10772           (setq str (buffer-substring from to))
10773           (setq len (length str))
10774           (while (< i len)
10775             (aset str i (aref caesar-translate-table (aref str i)))
10776             (setq i (1+ i)))
10777           (goto-char from)
10778           (delete-region from to)
10779           (insert str)))))
10780
10781 \f
10782 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10783
10784 ;;;###autoload
10785 (defalias 'gnus-batch-kill 'gnus-batch-score)
10786 ;;;###autoload
10787 (defun gnus-batch-score ()
10788   "Run batched scoring.
10789 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10790 Newsgroups is a list of strings in Bnews format.  If you want to score
10791 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10792 score the alt hierarchy, you'd say \"!alt.all\"."
10793   (interactive)
10794   (let* ((yes-and-no
10795           (gnus-newsrc-parse-options
10796            (apply (function concat)
10797                   (mapcar (lambda (g) (concat g " "))
10798                           command-line-args-left))))
10799          (gnus-expert-user t)
10800          (nnmail-spool-file nil)
10801          (gnus-use-dribble-file nil)
10802          (yes (car yes-and-no))
10803          (no (cdr yes-and-no))
10804          group subscribed newsrc entry
10805          ;; Disable verbose message.
10806          gnus-novice-user gnus-large-newsgroup)
10807     ;; Eat all arguments.
10808     (setq command-line-args-left nil)
10809     ;; Start Gnus.
10810     (gnus)
10811     ;; Apply kills to specified newsgroups in command line arguments.
10812     (setq newsrc (cdr gnus-newsrc-alist))
10813     (while newsrc
10814       (setq group (car (car newsrc)))
10815       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10816       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10817                (and (car entry)
10818                     (or (eq (car entry) t)
10819                         (not (zerop (car entry)))))
10820                (if yes (string-match yes group) t)
10821                (or (null no) (not (string-match no group))))
10822           (progn
10823             (gnus-summary-read-group group nil t)
10824             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10825                  (gnus-summary-exit))))
10826       (setq newsrc (cdr newsrc)))
10827     ;; Exit Emacs.
10828     (switch-to-buffer gnus-group-buffer)
10829     (gnus-group-save-newsrc)))
10830
10831 (defun gnus-apply-kill-file ()
10832   "Apply a kill file to the current newsgroup.
10833 Returns the number of articles marked as read."
10834   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10835           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10836       (gnus-apply-kill-file-internal)
10837     0))
10838
10839 (defun gnus-kill-save-kill-buffer ()
10840   (save-excursion
10841     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10842       (if (get-file-buffer file)
10843           (progn
10844             (set-buffer (get-file-buffer file))
10845             (and (buffer-modified-p) (save-buffer))
10846             (kill-buffer (current-buffer)))))))
10847
10848 (defvar gnus-kill-file-name "KILL"
10849   "Suffix of the kill files.")
10850
10851 (defun gnus-newsgroup-kill-file (newsgroup)
10852   "Return the name of a kill file name for NEWSGROUP.
10853 If NEWSGROUP is nil, return the global kill file name instead."
10854   (cond ((or (null newsgroup)
10855              (string-equal newsgroup ""))
10856          ;; The global KILL file is placed at top of the directory.
10857          (expand-file-name gnus-kill-file-name
10858                            (or gnus-kill-files-directory "~/News")))
10859         ((gnus-use-long-file-name 'not-kill)
10860          ;; Append ".KILL" to newsgroup name.
10861          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10862                            (or gnus-kill-files-directory "~/News")))
10863         (t
10864          ;; Place "KILL" under the hierarchical directory.
10865          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10866                                    "/" gnus-kill-file-name)
10867                            (or gnus-kill-files-directory "~/News")))))
10868
10869 \f
10870 ;;;
10871 ;;; Dribble file
10872 ;;;
10873
10874 (defvar gnus-dribble-ignore nil)
10875
10876 (defun gnus-dribble-file-name ()
10877   (concat gnus-startup-file "-dribble"))
10878
10879 (defun gnus-dribble-open ()
10880   (save-excursion 
10881     (set-buffer 
10882      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10883     (buffer-disable-undo (current-buffer))
10884     (bury-buffer gnus-dribble-buffer)
10885     (auto-save-mode t)
10886     (goto-char (point-max))))
10887
10888 (defun gnus-dribble-enter (string)
10889   (if (and (not gnus-dribble-ignore)
10890            gnus-dribble-buffer
10891            (buffer-name gnus-dribble-buffer))
10892       (let ((obuf (current-buffer)))
10893         (set-buffer gnus-dribble-buffer)
10894         (insert string "\n")
10895         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10896         (set-buffer obuf))))
10897
10898 (defun gnus-dribble-read-file ()
10899   (let ((dribble-file (gnus-dribble-file-name)))
10900     (save-excursion 
10901       (set-buffer (setq gnus-dribble-buffer 
10902                         (get-buffer-create 
10903                          (file-name-nondirectory dribble-file))))
10904       (gnus-add-current-to-buffer-list)
10905       (erase-buffer)
10906       (set-visited-file-name dribble-file)
10907       (buffer-disable-undo (current-buffer))
10908       (bury-buffer (current-buffer))
10909       (set-buffer-modified-p nil)
10910       (let ((auto (make-auto-save-file-name))
10911             (gnus-dribble-ignore t))
10912         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10913             (progn
10914               (if (file-newer-than-file-p auto dribble-file)
10915                   (setq dribble-file auto))
10916               (insert-file-contents dribble-file)
10917               (if (not (zerop (buffer-size)))
10918                   (set-buffer-modified-p t))
10919               (if (gnus-y-or-n-p 
10920                    "Auto-save file exists. Do you want to read it? ")
10921                   (progn
10922                     (gnus-message 5 "Reading %s..." dribble-file) 
10923                     (eval-current-buffer)
10924                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10925
10926 (defun gnus-dribble-delete-file ()
10927   (if (file-exists-p (gnus-dribble-file-name))
10928       (delete-file (gnus-dribble-file-name)))
10929   (if gnus-dribble-buffer
10930       (save-excursion
10931         (set-buffer gnus-dribble-buffer)
10932         (let ((auto (make-auto-save-file-name)))
10933           (if (file-exists-p auto)
10934               (delete-file auto))
10935           (erase-buffer)
10936           (set-buffer-modified-p nil)))))
10937
10938 (defun gnus-dribble-save ()
10939   (if (and gnus-dribble-buffer
10940            (buffer-name gnus-dribble-buffer))
10941       (save-excursion
10942         (set-buffer gnus-dribble-buffer)
10943         (save-buffer))))
10944
10945 (defun gnus-dribble-clear ()
10946   (save-excursion
10947     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10948         (progn
10949           (set-buffer gnus-dribble-buffer)
10950           (erase-buffer)
10951           (set-buffer-modified-p nil)
10952           (setq buffer-saved-size (buffer-size))))))
10953
10954 ;;;
10955 ;;; Server Communication
10956 ;;;
10957
10958 ;; All the Gnus backends have the same interface, and should return
10959 ;; data in a similar format. Below is an overview of what functions
10960 ;; these packages must supply and what results they should return.
10961 ;;
10962 ;; Variables:
10963 ;;
10964 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10965 ;; buffer. 
10966 ;;
10967 ;; Functions for the imaginary backend `choke':
10968 ;;
10969 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10970 ;; Should return all headers for all ARTICLES, or return NOV lines for
10971 ;; the same.
10972 ;;
10973 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10974 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10975 ;; must be returned.
10976 ;;
10977 ;; `choke-close-group GROUP &optional SERVER'
10978 ;; Close group. Most backends won't have to do anything with this
10979 ;; call, but it is an opportunity to clean up, if that is needed. It
10980 ;; is called when Gnus exits a group.
10981 ;;
10982 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10983 ;; Return ARTICLE, which is either an article number or
10984 ;; message-id. Note that not all backends can return articles based on
10985 ;; message-id. 
10986 ;;
10987 ;; `choke-request-list SERVER'
10988 ;; Return a list of all newsgroups on SERVER.
10989 ;;
10990 ;; `choke-request-list-newsgroups SERVER'
10991 ;; Return a list of descriptions of all newsgroups on SERVER.
10992 ;;
10993 ;; `choke-request-newgroups DATE &optional SERVER'
10994 ;; Return a list of all groups that have arrived after DATE on
10995 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10996 ;; always check whether the groups are old or not. Backends that do
10997 ;; not store date information may just return the entire list of
10998 ;; groups, although this might not be a good idea in general.
10999 ;;
11000 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11001 ;; Should return a buffer that is suitable for "posting". nnspool and
11002 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11003 ;; buffer. This function should fill out the appropriate headers. 
11004 ;;
11005 ;; `choke-request-post &optional SERVER'
11006 ;; Function that will be called from a buffer to be posted. 
11007 ;;
11008 ;; `choke-open-server SERVER &optional ARGUMENT'
11009 ;; Open a connection to SERVER.
11010 ;;
11011 ;; `choke-close-server &optional SERVER'
11012 ;; Close the connection to SERVER.
11013 ;;
11014 ;; `choke-server-opened &optional SERVER'
11015 ;; Whether the conenction to SERVER is opened or not.
11016 ;;
11017 ;; `choke-server-status &optional SERVER'
11018 ;; Should return a status string (not in the nntp buffer, but as the
11019 ;; result of the function).
11020 ;;
11021 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11022 ;; Optional function for retrieving active file info on all groups in
11023 ;; GROUPS.  Two return formats are supported: The normal active file
11024 ;; format, and a list of GROUP lines.  This function should return (as
11025 ;; a function value) either `active' or `group', depending on what
11026 ;; format it returns.
11027 ;;
11028 ;; The following functions are optional and apply only to backends
11029 ;; that are able to control the contents of their groups totally
11030 ;; (ie. mail backends.)  Backends that aren't able to do that
11031 ;; shouldn't define these functions at all. Gnus will check for their
11032 ;; presence before attempting to call them.
11033 ;;
11034 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11035 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11036 ;; backends will not be able to expire articles. Should return a list
11037 ;; of all articles that were not expired.
11038 ;;
11039 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11040 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11041 ;; Removes any information it has added to the article (extra headers,
11042 ;; whatever - make it as clean as possible), and then passes the
11043 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11044 ;; function described below. If the ACCEPT-FORM returns a non-nil
11045 ;; value, the article should then be deleted. If LAST is nil, that
11046 ;; means that there will be further calls to this function. This might
11047 ;; be taken as an advice not to save buffers/internal variables just
11048 ;; yet, but wait until the last call to speed things up.
11049 ;;
11050 ;; `choke-request-accept-article GROUP &optional LAST' 
11051 ;; The contents of the current buffer will be put into GROUP.  There
11052 ;; should, of course, be an article in the current buffer.  This
11053 ;; function is normally only called by the function described above,
11054 ;; and LAST works the same way as in that function.
11055 ;;
11056 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11057 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11058 ;; This provides an easy interface for allowing editing of
11059 ;; articles. Note that even headers may be edited, so the backend has
11060 ;; to update any tables (nov buffers, etc) that it maintains after
11061 ;; replacing the article.
11062 ;;
11063 ;; `choke-request-create-group GROUP &optional SERVER'
11064 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11065 ;; might be a group that already exists, but hasn't been registered
11066 ;; yet. 
11067 ;;
11068 ;; All these functions must return nil if they couldn't service the
11069 ;; request. If the optional arguments are not supplied, some "current"
11070 ;; or "default" values should be used. In short, one should emulate an
11071 ;; NNTP server, in a way.
11072 ;;
11073 ;; If you want to write a new backend, you just have to supply the
11074 ;; functions listed above. In addition, you must enter the new backend
11075 ;; into the list of valid select methods:
11076 ;; (setq gnus-valid-select-methods 
11077 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11078 ;; The first element in this list is the name of the backend. Other
11079 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11080 ;; groups), `none' (neither), `respool' (for groups that can control
11081 ;; their contents). 
11082
11083 (defun gnus-start-news-server (&optional confirm)
11084   "Open a method for getting news.
11085 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11086   (let (how where)
11087     (if gnus-current-select-method
11088         ;; Stream is already opened.
11089         nil
11090       ;; Open NNTP server.
11091       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11092       (if confirm
11093           (progn
11094             ;; Read server name with completion.
11095             (setq gnus-nntp-server
11096                   (completing-read "NNTP server: "
11097                                    (mapcar (lambda (server) (list server))
11098                                            (cons (list gnus-nntp-server)
11099                                                  gnus-secondary-servers))
11100                                    nil nil gnus-nntp-server))))
11101
11102       (if (and gnus-nntp-server 
11103                (stringp gnus-nntp-server)
11104                (not (string= gnus-nntp-server "")))
11105           (setq gnus-select-method
11106                 (cond ((or (string= gnus-nntp-server "")
11107                            (string= gnus-nntp-server "::"))
11108                        (list 'nnspool (system-name)))
11109                       ((string-match "^:" gnus-nntp-server)
11110                        (list 'nnmh gnus-nntp-server 
11111                              (list 'nnmh-directory 
11112                                    (file-name-as-directory
11113                                     (expand-file-name
11114                                      (concat "~/" (substring
11115                                                    gnus-nntp-server 1)))))
11116                              (list 'nnmh-get-new-mail nil)))
11117                       (t
11118                        (list 'nntp gnus-nntp-server)))))
11119
11120       (setq how (car gnus-select-method))
11121       (setq where (car (cdr gnus-select-method)))
11122       (cond ((eq how 'nnspool)
11123              (require 'nnspool)
11124              (gnus-message 5 "Looking up local news spool..."))
11125             ((eq how 'nnmh)
11126              (require 'nnmh)
11127              (gnus-message 5 "Looking up mh spool..."))
11128             (t
11129              (require 'nntp)))
11130       (setq gnus-current-select-method gnus-select-method)
11131       (run-hooks 'gnus-open-server-hook)
11132       (or 
11133        ;; gnus-open-server-hook might have opened it
11134        (gnus-server-opened gnus-select-method)  
11135        (gnus-open-server gnus-select-method)
11136        (gnus-y-or-n-p
11137         (format
11138          "%s server on %s can't be opened. Continue? "
11139          (car gnus-select-method) (nth 1 gnus-select-method)))
11140        (progn
11141          (gnus-message 1 "Couldn't open server on %s" 
11142                        (nth 1 gnus-select-method))
11143          (ding)
11144          nil)))))
11145
11146 (defun gnus-check-news-server (&optional method)
11147   "If the news server is down, start it up again."
11148   (let ((method (if method method gnus-select-method)))
11149     (and (stringp method)
11150          (setq method (gnus-server-to-method method)))
11151     (if (gnus-server-opened method)
11152         ;; Stream is already opened.
11153         t
11154       ;; Open server.
11155       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11156       (run-hooks 'gnus-open-server-hook)
11157       (or (gnus-server-opened method)
11158           (gnus-open-server method))
11159       (message ""))))
11160
11161 (defun gnus-nntp-message (&optional message)
11162   "Check the status of the NNTP server.
11163 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11164 is returned insted of the status string."
11165   (let ((status (gnus-status-message (gnus-find-method-for-group 
11166                                       gnus-newsgroup-name)))
11167         (message (or message "")))
11168     (if (and (stringp status) (> (length status) 0))
11169         status message)))
11170
11171 (defun gnus-get-function (method function)
11172   (and (stringp method)
11173        (setq method (gnus-server-to-method method)))
11174   (let ((func (intern (format "%s-%s" (car method) function))))
11175     (if (not (fboundp func)) 
11176         (progn
11177           (require (car method))
11178           (if (not (fboundp func)) 
11179               (error "No such function: %s" func))))
11180     func))
11181
11182 ;;; Interface functions to the backends.
11183
11184 (defun gnus-open-server (method)
11185   (funcall (gnus-get-function method 'open-server)
11186            (nth 1 method) (nthcdr 2 method)))
11187
11188 (defun gnus-close-server (method)
11189   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11190
11191 (defun gnus-request-list (method)
11192   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11193
11194 (defun gnus-request-list-newsgroups (method)
11195   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11196
11197 (defun gnus-request-newgroups (date method)
11198   (funcall (gnus-get-function method 'request-newgroups) 
11199            date (nth 1 method)))
11200
11201 (defun gnus-server-opened (method)
11202   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11203
11204 (defun gnus-status-message (method)
11205   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11206                   method)))
11207     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11208
11209 (defun gnus-request-group (group &optional dont-check)
11210   (let ((method (gnus-find-method-for-group group)))
11211 ;    (and t (message "%s GROUP %s" (car method) group))
11212     (funcall (gnus-get-function method 'request-group) 
11213              (gnus-group-real-name group) (nth 1 method) dont-check)))
11214
11215 (defun gnus-request-asynchronous (group &optional articles)
11216   (let ((method (gnus-find-method-for-group group)))
11217     (funcall (gnus-get-function method 'request-asynchronous) 
11218              (gnus-group-real-name group) (nth 1 method) articles)))
11219
11220 (defun gnus-list-active-group (group)
11221   (let ((method (gnus-find-method-for-group group))
11222         (func 'list-active-group))
11223     (and (gnus-check-backend-function func group)
11224          (funcall (gnus-get-function method func) 
11225                   (gnus-group-real-name group) (nth 1 method)))))
11226
11227 (defun gnus-request-group-description (group)
11228   (let ((method (gnus-find-method-for-group group))
11229         (func 'request-group-description))
11230     (and (gnus-check-backend-function func group)
11231          (funcall (gnus-get-function method func) 
11232                   (gnus-group-real-name group) (nth 1 method)))))
11233
11234 (defun gnus-close-group (group)
11235   (let ((method (gnus-find-method-for-group group)))
11236     (funcall (gnus-get-function method 'close-group) 
11237              (gnus-group-real-name group) (nth 1 method))))
11238
11239 (defun gnus-retrieve-headers (articles group)
11240   (let ((method (gnus-find-method-for-group group)))
11241     (if (and gnus-use-cache (numberp (car articles)))
11242         (gnus-cache-retrieve-headers articles group)
11243       (funcall (gnus-get-function method 'retrieve-headers) 
11244                articles (gnus-group-real-name group) (nth 1 method)))))
11245
11246 (defun gnus-retrieve-groups (groups method)
11247   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11248
11249 (defun gnus-request-article (article group &optional buffer)
11250   (let ((method (gnus-find-method-for-group group)))
11251     (funcall (gnus-get-function method 'request-article) 
11252              article (gnus-group-real-name group) (nth 1 method) buffer)))
11253
11254 (defun gnus-request-head (article group)
11255   (let ((method (gnus-find-method-for-group group)))
11256     (funcall (gnus-get-function method 'request-head) 
11257              article (gnus-group-real-name group) (nth 1 method))))
11258
11259 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11260 (defun gnus-request-post-buffer (post group subject header artbuf
11261                                       info follow-to respect-poster)
11262    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11263                                             group gnus-newsrc-hashtb)))))
11264           (method
11265            (if (and gnus-post-method
11266                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11267                     (memq 'post (assoc
11268                                  (format "%s" (car (gnus-find-method-for-group
11269                                                     gnus-newsgroup-name)))
11270                                         gnus-valid-select-methods)))
11271                gnus-post-method
11272              (gnus-find-method-for-group gnus-newsgroup-name))))
11273      (or (gnus-server-opened method)
11274          (gnus-open-server method)
11275          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11276      (let ((mail-self-blind nil)
11277            (mail-archive-file-name nil))
11278        (funcall (gnus-get-function method 'request-post-buffer) 
11279                 post group subject header artbuf info follow-to
11280                 respect-poster))))
11281
11282 (defun gnus-request-post (method &optional force)
11283   (and (stringp method)
11284        (setq method (gnus-server-to-method method)))
11285   (and (not force) gnus-post-method
11286        (memq 'post (assoc (format "%s" (car method))
11287                           gnus-valid-select-methods))
11288        (setq method gnus-post-method))
11289   (funcall (gnus-get-function method 'request-post) 
11290            (nth 1 method)))
11291
11292 (defun gnus-request-expire-articles (articles group &optional force)
11293   (let ((method (gnus-find-method-for-group group)))
11294     (funcall (gnus-get-function method 'request-expire-articles) 
11295              articles (gnus-group-real-name group) (nth 1 method)
11296              force)))
11297
11298 (defun gnus-request-move-article 
11299   (article group server accept-function &optional last)
11300   (let ((method (gnus-find-method-for-group group)))
11301     (funcall (gnus-get-function method 'request-move-article) 
11302              article (gnus-group-real-name group) 
11303              (nth 1 method) accept-function last)))
11304
11305 (defun gnus-request-accept-article (group &optional last)
11306   (let ((func (if (symbolp group) group
11307                 (car (gnus-find-method-for-group group)))))
11308     (funcall (intern (format "%s-request-accept-article" func))
11309              (if (stringp group) (gnus-group-real-name group) group)
11310              last)))
11311
11312 (defun gnus-request-replace-article (article group buffer)
11313   (let ((func (car (gnus-find-method-for-group group))))
11314     (funcall (intern (format "%s-request-replace-article" func))
11315              article (gnus-group-real-name group) buffer)))
11316
11317 (defun gnus-request-create-group (group)
11318   (let ((method (gnus-find-method-for-group group)))
11319     (funcall (gnus-get-function method 'request-create-group) 
11320              (gnus-group-real-name group) (nth 1 method))))
11321
11322 (defun gnus-member-of-valid (symbol group)
11323   (memq symbol (assoc
11324                 (format "%s" (car (gnus-find-method-for-group group)))
11325                 gnus-valid-select-methods)))
11326
11327 (defsubst gnus-secondary-method-p (method)
11328   (member method gnus-secondary-select-methods))
11329
11330 (defun gnus-find-method-for-group (group &optional info)
11331   (or gnus-override-method
11332       (and (not group)
11333            gnus-select-method)
11334       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11335             method)
11336         (if (or (not info)
11337                 (not (setq method (nth 4 info))))
11338             (setq method gnus-select-method)
11339           (setq method
11340                 (cond ((stringp method)
11341                        (gnus-server-to-method method))
11342                       ((stringp (car method))
11343                        (gnus-server-extend-method group method))
11344                       (t
11345                        method))))
11346         (gnus-server-add-address method))))
11347
11348 (defun gnus-check-backend-function (func group)
11349   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11350                  group)))
11351     (fboundp (intern (format "%s-%s" method func)))))
11352
11353 (defun gnus-methods-using (method)
11354   (let ((valids gnus-valid-select-methods)
11355         outs)
11356     (while valids
11357       (if (memq method (car valids)) 
11358           (setq outs (cons (car valids) outs)))
11359       (setq valids (cdr valids)))
11360     outs))
11361
11362 ;;; 
11363 ;;; Active & Newsrc File Handling
11364 ;;;
11365
11366 ;; Newsrc related functions.
11367 ;; Gnus internal format of gnus-newsrc-alist:
11368 ;; (("alt.general" 3 (1 . 1))
11369 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11370 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11371 ;; The first item is the group name; the second is the subscription
11372 ;; level; the third is either a range of a list of ranges of read
11373 ;; articles, the optional fourth element is a list of marked articles,
11374 ;; the optional fifth element is the select method.
11375 ;;
11376 ;; Gnus internal format of gnus-newsrc-hashtb:
11377 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11378 ;; This is the entry for "alt.misc". The first element is the number
11379 ;; of unread articles in "alt.misc". The cdr of this entry is the
11380 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11381 ;; trivial to remove or add new elements into gnus-newsrc-alist
11382 ;; without scanning the entire list. So, to get the actual information
11383 ;; of "alt.misc", you'd say something like 
11384 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11385 ;;
11386 ;; Gnus internal format of gnus-active-hashtb:
11387 ;; ((1 . 1))
11388 ;;  (5 . 10))
11389 ;;  (67 . 99)) ...)
11390 ;; The only element in each entry in this hash table is a range of
11391 ;; (possibly) available articles. (Articles in this range may have
11392 ;; been expired or canceled.)
11393 ;;
11394 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11395 ;; ("alt.misc" "alt.test" "alt.general" ...)
11396
11397 (defun gnus-setup-news (&optional rawfile level)
11398   "Setup news information.
11399 If RAWFILE is non-nil, the .newsrc file will also be read.
11400 If LEVEL is non-nil, the news will be set up at level LEVEL."
11401   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11402     ;; Clear some variables to re-initialize news information.
11403     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11404
11405     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11406     (if init (gnus-read-newsrc-file rawfile))
11407
11408     ;; Read the active file and create `gnus-active-hashtb'.
11409     ;; If `gnus-read-active-file' is nil, then we just create an empty
11410     ;; hash table. The partial filling out of the hash table will be
11411     ;; done in `gnus-get-unread-articles'.
11412     (if (and gnus-read-active-file 
11413              (not level))
11414         (gnus-read-active-file)
11415       (setq gnus-active-hashtb (make-vector 4095 0)))
11416
11417     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11418
11419     ;; Find the number of unread articles in each non-dead group.
11420     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11421     ;; Find new newsgroups and treat them.
11422     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11423              (gnus-server-opened gnus-select-method))
11424         (gnus-find-new-newsgroups))
11425     (if (and init gnus-check-bogus-newsgroups 
11426              gnus-read-active-file (not level)
11427              (gnus-server-opened gnus-select-method))
11428         (gnus-check-bogus-newsgroups))))
11429
11430 (defun gnus-find-new-newsgroups ()
11431   "Search for new newsgroups and add them.
11432 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11433 The `-n' option line from .newsrc is respected."
11434   (interactive)
11435   (or (gnus-check-first-time-used)
11436       (if (or (consp gnus-check-new-newsgroups)
11437               (eq gnus-check-new-newsgroups 'ask-server))
11438           (gnus-ask-server-for-new-groups)
11439         (let ((groups 0)
11440               group new-newsgroups)
11441           (or gnus-have-read-active-file (gnus-read-active-file))
11442           (setq gnus-newsrc-last-checked-date (current-time-string))
11443           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11444           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11445           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11446           (mapatoms
11447            (lambda (sym)
11448              (setq group (symbol-name sym))
11449              (if (or (gnus-gethash group gnus-killed-hashtb)
11450                      (gnus-gethash group gnus-newsrc-hashtb))
11451                  ()
11452                (let ((do-sub (gnus-matches-options-n group)))
11453                  (cond ((eq do-sub 'subscribe)
11454                         (setq groups (1+ groups))
11455                         (gnus-sethash group group gnus-killed-hashtb)
11456                         (funcall 
11457                          gnus-subscribe-options-newsgroup-method group))
11458                        ((eq do-sub 'ignore)
11459                         nil)
11460                        (t
11461                         (setq groups (1+ groups))
11462                         (gnus-sethash group group gnus-killed-hashtb)
11463                         (if gnus-subscribe-hierarchical-interactive
11464                             (setq new-newsgroups (cons group new-newsgroups))
11465                           (funcall gnus-subscribe-newsgroup-method group)))))))
11466            gnus-active-hashtb)
11467           (if new-newsgroups 
11468               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11469           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11470           (if (> groups 0)
11471               (gnus-message 6 "%d new newsgroup%s arrived." 
11472                             groups (if (> groups 1) "s have" " has")))))))
11473
11474 (defun gnus-matches-options-n (group)
11475   ;; Returns `subscribe' if the group is to be uncoditionally
11476   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11477   ;; no match for the group.
11478
11479   ;; First we check the two user variables.
11480   (cond
11481    ((and gnus-options-subscribe
11482          (string-match gnus-options-subscribe group))
11483     'subscribe)
11484    ((and gnus-options-not-subscribe
11485          (string-match gnus-options-not-subscribe group))
11486     'ignore)
11487    ;; Then we go through the list that was retrieved from the .newsrc
11488    ;; file.  This list has elements on the form 
11489    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11490    ;; is in the reverse order of the options line) is returned.
11491    (t
11492     (let ((regs gnus-newsrc-options-n))
11493       (while (and regs
11494                   (not (string-match (car (car regs)) group)))
11495         (setq regs (cdr regs)))
11496       (and regs (cdr (car regs)))))))
11497
11498 (defun gnus-ask-server-for-new-groups ()
11499   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11500          (methods (cons gnus-select-method 
11501                         (append
11502                          (and (consp gnus-check-new-newsgroups)
11503                               gnus-check-new-newsgroups)
11504                          gnus-secondary-select-methods)))
11505          (groups 0)
11506          (new-date (current-time-string))
11507          hashtb group new-newsgroups got-new)
11508     ;; Go thorugh both primary and secondary select methods and
11509     ;; request new newsgroups.  
11510     (while methods
11511       (and (or (gnus-server-opened (car methods))
11512                (gnus-open-server (car methods)))
11513            (gnus-request-newgroups date (car methods))
11514            (save-excursion
11515              (setq got-new t)
11516              (set-buffer nntp-server-buffer)
11517              (or hashtb (setq hashtb (gnus-make-hashtable 
11518                                       (count-lines (point-min) (point-max)))))
11519              ;; Enter all the new groups in a hashtable.
11520              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11521       (setq methods (cdr methods)))
11522     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11523     ;; Now all new groups from all select methods are in `hashtb'.
11524     (mapatoms
11525      (lambda (group-sym)
11526        (setq group (symbol-name group-sym))
11527        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11528                (member group gnus-zombie-list)
11529                (member group gnus-killed-list))
11530            ;; The group is already known.
11531            ()
11532          (and (symbol-value group-sym)
11533               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11534          (let ((do-sub (gnus-matches-options-n group)))
11535            (cond ((eq do-sub 'subscribe)
11536                   (setq groups (1+ groups))
11537                   (gnus-sethash group group gnus-killed-hashtb)
11538                   (funcall 
11539                    gnus-subscribe-options-newsgroup-method group))
11540                  ((eq do-sub 'ignore)
11541                   nil)
11542                  (t
11543                   (setq groups (1+ groups))
11544                   (gnus-sethash group group gnus-killed-hashtb)
11545                   (if gnus-subscribe-hierarchical-interactive
11546                       (setq new-newsgroups (cons group new-newsgroups))
11547                     (funcall gnus-subscribe-newsgroup-method group)))))))
11548      hashtb)
11549     (if new-newsgroups 
11550         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11551     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11552     (if (> groups 0)
11553         (gnus-message 6 "%d new newsgroup%s arrived." 
11554                       groups (if (> groups 1) "s have" " has")))
11555     got-new))
11556
11557 (defun gnus-check-first-time-used ()
11558   (if (or (> (length gnus-newsrc-alist) 1)
11559           (file-exists-p gnus-startup-file)
11560           (file-exists-p (concat gnus-startup-file ".el"))
11561           (file-exists-p (concat gnus-startup-file ".eld")))
11562       nil
11563     (gnus-message 6 "First time user; subscribing you to default groups")
11564     (or gnus-have-read-active-file (gnus-read-active-file))
11565     (setq gnus-newsrc-last-checked-date (current-time-string))
11566     (let ((groups gnus-default-subscribed-newsgroups)
11567           group)
11568       (if (eq groups t)
11569           nil
11570         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11571         (mapatoms
11572          (lambda (sym)
11573            (setq group (symbol-name sym))
11574            (let ((do-sub (gnus-matches-options-n group)))
11575              (cond ((eq do-sub 'subscribe)
11576                     (gnus-sethash group group gnus-killed-hashtb)
11577                     (funcall 
11578                      gnus-subscribe-options-newsgroup-method group))
11579                    ((eq do-sub 'ignore)
11580                     nil)
11581                    (t
11582                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11583          gnus-active-hashtb)
11584         (while groups
11585           (if (gnus-gethash (car groups) gnus-active-hashtb)
11586               (gnus-group-change-level 
11587                (car groups) gnus-level-default-subscribed gnus-level-killed))
11588           (setq groups (cdr groups)))
11589         (gnus-group-make-help-group)
11590         (and gnus-novice-user
11591              (gnus-message 7 "`A k' to list killed groups"))))))
11592
11593 (defun gnus-subscribe-group (group previous &optional method)
11594   (gnus-group-change-level 
11595    (if method
11596        (list t group gnus-level-default-subscribed nil nil method)
11597      group) 
11598    gnus-level-default-subscribed gnus-level-killed previous t))
11599
11600 ;; `gnus-group-change-level' is the fundamental function for changing
11601 ;; subscription levels of newsgroups. This might mean just changing
11602 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11603 ;; again, which subscribes/unsubscribes a group, which is equally
11604 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11605 ;; from 8-9 to 1-7 means that you remove the group from the list of
11606 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11607 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11608 ;; which is trivial.
11609 ;; ENTRY can either be a string (newsgroup name) or a list (if
11610 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11611 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11612 ;; entries. 
11613 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11614 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11615 ;; after. 
11616 (defun gnus-group-change-level (entry level &optional oldlevel
11617                                       previous fromkilled)
11618   (let ((pinfo entry)
11619         group info active num)
11620     ;; Glean what info we can from the arguments
11621     (if (consp entry)
11622         (if fromkilled (setq group (nth 1 entry))
11623           (setq group (car (nth 2 entry))))
11624       (setq group entry))
11625     (if (and (stringp entry)
11626              oldlevel 
11627              (< oldlevel gnus-level-zombie))
11628         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11629     (if (and (not oldlevel)
11630              (consp entry))
11631         (setq oldlevel (car (cdr (nth 2 entry)))))
11632     (if (stringp previous)
11633         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11634
11635     (gnus-dribble-enter
11636      (format "(gnus-group-change-level %S %S %S %S %S)" 
11637              group level oldlevel (car (nth 2 previous)) fromkilled))
11638     
11639     ;; Then we remove the newgroup from any old structures, if needed.
11640     ;; If the group was killed, we remove it from the killed or zombie
11641     ;; list. If not, and it is in fact going to be killed, we remove
11642     ;; it from the newsrc hash table and assoc.
11643     (cond ((>= oldlevel gnus-level-zombie)
11644            (if (= oldlevel gnus-level-zombie)
11645                (setq gnus-zombie-list (delete group gnus-zombie-list))
11646              (setq gnus-killed-list (delete group gnus-killed-list))))
11647           (t
11648            (if (>= level gnus-level-zombie)
11649                (progn
11650                  (gnus-sethash (car (nth 2 entry))
11651                                nil gnus-newsrc-hashtb)
11652                  (if (nth 3 entry)
11653                      (setcdr (gnus-gethash (car (nth 3 entry))
11654                                            gnus-newsrc-hashtb)
11655                              (cdr entry)))
11656                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11657
11658     ;; Finally we enter (if needed) the list where it is supposed to
11659     ;; go, and change the subscription level. If it is to be killed,
11660     ;; we enter it into the killed or zombie list.
11661     (cond ((>= level gnus-level-zombie)
11662            ;; Remove from the hash table.
11663            (gnus-sethash group nil gnus-newsrc-hashtb)
11664            (or (gnus-group-foreign-p group)
11665                ;; We do not enter foreign groups into the list of dead
11666                ;; groups.  
11667                (if (= level gnus-level-zombie)
11668                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11669                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11670           (t
11671            ;; If the list is to be entered into the newsrc assoc, and
11672            ;; it was killed, we have to create an entry in the newsrc
11673            ;; hashtb format and fix the pointers in the newsrc assoc.
11674            (if (>= oldlevel gnus-level-zombie)
11675                (progn
11676                  (if (listp entry)
11677                      (progn
11678                        (setq info (cdr entry))
11679                        (setq num (car entry)))
11680                    (setq active (gnus-gethash group gnus-active-hashtb))
11681                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11682                    ;; Check whether the group is foreign. If so, the
11683                    ;; foreign select method has to be entered into the
11684                    ;; info. 
11685                    (let ((method (gnus-group-method-name group)))
11686                      (if (eq method gnus-select-method)
11687                          (setq info (list group level nil))
11688                        (setq info (list group level nil nil method)))))
11689                  (or previous 
11690                      (setq previous 
11691                            (let ((p gnus-newsrc-alist))
11692                              (while (cdr (cdr p))
11693                                (setq p (cdr p)))
11694                              p)))
11695                  (setq entry (cons info (cdr (cdr previous))))
11696                  (if (cdr previous)
11697                      (progn
11698                        (setcdr (cdr previous) entry)
11699                        (gnus-sethash group (cons num (cdr previous)) 
11700                                      gnus-newsrc-hashtb))
11701                    (setcdr previous entry)
11702                    (gnus-sethash group (cons num previous)
11703                                  gnus-newsrc-hashtb))
11704                  (if (cdr entry)
11705                      (setcdr (gnus-gethash (car (car (cdr entry)))
11706                                            gnus-newsrc-hashtb)
11707                              entry)))
11708              ;; It was alive, and it is going to stay alive, so we
11709              ;; just change the level and don't change any pointers or
11710              ;; hash table entries.
11711              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11712
11713 (defun gnus-kill-newsgroup (newsgroup)
11714   "Obsolete function. Kills a newsgroup."
11715   (gnus-group-change-level
11716    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11717
11718 (defun gnus-check-bogus-newsgroups (&optional confirm)
11719   "Remove bogus newsgroups.
11720 If CONFIRM is non-nil, the user has to confirm the deletion of every
11721 newsgroup." 
11722   (let ((newsrc (cdr gnus-newsrc-alist))
11723         bogus group entry)
11724     (gnus-message 5 "Checking bogus newsgroups...")
11725     (or gnus-have-read-active-file (gnus-read-active-file))
11726     ;; Find all bogus newsgroup that are subscribed.
11727     (while newsrc
11728       (setq group (car (car newsrc)))
11729       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11730               (nth 4 (car newsrc))      ; Foreign
11731               (and confirm
11732                    (not (gnus-y-or-n-p
11733                          (format "Remove bogus newsgroup: %s " group)))))
11734           ;; Don't remove.
11735           ()
11736         ;; Found a bogus newsgroup.
11737         (setq bogus (cons group bogus)))
11738       (setq newsrc (cdr newsrc)))
11739     ;; Remove all bogus subscribed groups by first killing them, and
11740     ;; then removing them from the list of killed groups.
11741     (while bogus
11742       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11743            (progn
11744              (gnus-group-change-level entry gnus-level-killed)
11745              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11746       (setq bogus (cdr bogus)))
11747     ;; Then we remove all bogus groups from the list of killed and
11748     ;; zombie groups. They are are removed without confirmation.
11749     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11750           killed)
11751       (while dead-lists
11752         (setq killed (symbol-value (car dead-lists)))
11753         (while killed
11754           (setq group (car killed))
11755           (or (gnus-gethash group gnus-active-hashtb)
11756               ;; The group is bogus.
11757               (set (car dead-lists)
11758                    (delete group (symbol-value (car dead-lists)))))
11759           (setq killed (cdr killed)))
11760         (setq dead-lists (cdr dead-lists))))
11761     (gnus-message 5 "Checking bogus newsgroups...done")))
11762
11763 (defun gnus-check-duplicate-killed-groups ()
11764   "Remove duplicates from the list of killed groups."
11765   (interactive)
11766   (let ((killed gnus-killed-list))
11767     (while killed
11768       (gnus-message 9 "%d" (length killed))
11769       (setcdr killed (delete (car killed) (cdr killed)))
11770       (setq killed (cdr killed)))))
11771
11772 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11773 ;; and compute how many unread articles there are in each group.
11774 (defun gnus-get-unread-articles (&optional level) 
11775   (let* ((newsrc (cdr gnus-newsrc-alist))
11776          (conditional level)
11777          (level (or level (1+ gnus-level-subscribed)))
11778          (foreign-level
11779           (min 
11780            (cond ((and gnus-activate-foreign-newsgroups 
11781                        (not (numberp gnus-activate-foreign-newsgroups)))
11782                   (1+ gnus-level-subscribed))
11783                  ((numberp gnus-activate-foreign-newsgroups)
11784                   gnus-activate-foreign-newsgroups)
11785                  (t 0))
11786            level))
11787          info group active virtuals method)
11788     (gnus-message 5 "Checking new news...")
11789
11790     (while newsrc
11791       (setq info (car newsrc)
11792             group (car info)
11793             active (gnus-gethash group gnus-active-hashtb))
11794
11795       ;; Check newsgroups. If the user doesn't want to check them, or
11796       ;; they can't be checked (for instance, if the news server can't
11797       ;; be reached) we just set the number of unread articles in this
11798       ;; newsgroup to t. This means that Gnus thinks that there are
11799       ;; unread articles, but it has no idea how many.
11800       (if (and (setq method (nth 4 info))
11801                (not (gnus-server-equal gnus-select-method
11802                                        (gnus-server-get-method nil method)))
11803                (not (gnus-secondary-method-p method)))
11804           ;; These groups are foreign. Check the level.
11805           (if (<= (nth 1 info) foreign-level)
11806               (if (eq (car (if (stringp method) 
11807                                (gnus-server-to-method method)
11808                              (nth 4 info))) 'nnvirtual)
11809                   ;; We have to activate the virtual groups after all
11810                   ;; the others, so we just pop them on a list for
11811                   ;; now. 
11812                   (setq virtuals (cons info virtuals))
11813                 (and (setq active (gnus-activate-newsgroup (car info)))
11814                      ;; Close the groups as we look at them!
11815                      (gnus-close-group group))))
11816                 
11817         ;; These groups are native or secondary. 
11818         (if (and (not gnus-have-read-active-file)
11819                  (<= (nth 1 info) level))
11820             (setq active (gnus-activate-newsgroup (car info)))))
11821       
11822       (if active
11823           (gnus-get-unread-articles-in-group info active)
11824         ;; The group couldn't be reached, so we nix out the number of
11825         ;; unread articles and stuff.
11826         (gnus-sethash group nil gnus-active-hashtb)
11827         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
11828
11829       (setq newsrc (cdr newsrc)))
11830
11831     ;; Activate the virtual groups. This has to be done after all the
11832     ;; other groups. 
11833     ;; !!! If one virtual group contains another virtual group, even
11834     ;; doing it this way might cause problems.
11835    (while virtuals
11836       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11837            (gnus-get-unread-articles-in-group (car virtuals) active))
11838       (setq virtuals (cdr virtuals)))
11839
11840     (gnus-message 5 "Checking new news...done")))
11841
11842 ;; Create a hash table out of the newsrc alist. The `car's of the
11843 ;; alist elements are used as keys.
11844 (defun gnus-make-hashtable-from-newsrc-alist ()
11845   (let ((alist gnus-newsrc-alist)
11846         (ohashtb gnus-newsrc-hashtb)
11847         prev)
11848     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11849     (setq alist 
11850           (setq prev (setq gnus-newsrc-alist 
11851                            (if (equal (car (car gnus-newsrc-alist))
11852                                       "dummy.group")
11853                                gnus-newsrc-alist
11854                              (cons (list "dummy.group" 0 nil) alist)))))
11855     (while alist
11856       (gnus-sethash (car (car alist)) 
11857                     (cons (and ohashtb (car (gnus-gethash 
11858                                              (car (car alist)) ohashtb))) 
11859                           prev) gnus-newsrc-hashtb)
11860       (setq prev alist
11861             alist (cdr alist)))))
11862
11863 (defun gnus-make-hashtable-from-killed ()
11864   "Create a hash table from the killed and zombie lists."
11865   (let ((lists '(gnus-killed-list gnus-zombie-list))
11866         list)
11867     (setq gnus-killed-hashtb 
11868           (gnus-make-hashtable 
11869            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11870     (while lists
11871       (setq list (symbol-value (car lists)))
11872       (setq lists (cdr lists))
11873       (while list
11874         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11875         (setq list (cdr list))))))
11876
11877 (defun gnus-get-unread-articles-in-group (info active)
11878   (let* ((range (nth 2 info))
11879          (num 0)
11880          (marked (nth 3 info))
11881          srange lowest group highest)
11882     ;; If a cache is present, we may have to alter the active info.
11883     (and gnus-use-cache
11884          (gnus-cache-possibly-alter-active (car info) active))
11885     ;; Modify the list of read articles according to what articles 
11886     ;; are available; then tally the unread articles and add the
11887     ;; number to the group hash table entry.
11888     (cond ((zerop (cdr active))
11889            (setq num 0))
11890           ((not range)
11891            (setq num (- (1+ (cdr active)) (car active))))
11892           ((not (listp (cdr range)))
11893            ;; Fix a single (num . num) range according to the
11894            ;; active hash table.
11895            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11896            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11897            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11898            ;; Compute number of unread articles.
11899            (setq num (max 0 (- (cdr active) 
11900                                (- (1+ (cdr range)) (car range))))))
11901           (t
11902            ;; The read list is a list of ranges. Fix them according to
11903            ;; the active hash table.
11904            ;; First peel off any elements that are below the lower
11905            ;; active limit. 
11906            (while (and (cdr range) 
11907                        (>= (car active) 
11908                            (or (and (atom (car (cdr range))) (car (cdr range)))
11909                                (car (car (cdr range))))))
11910              (if (numberp (car range))
11911                  (setcar range 
11912                          (cons (car range) 
11913                                (or (and (numberp (car (cdr range)))
11914                                         (car (cdr range))) 
11915                                    (cdr (car (cdr range))))))
11916                (setcdr (car range) 
11917                        (or (and (numberp (nth 1 range)) (nth 1 range))
11918                            (cdr (car (cdr range))))))
11919              (setcdr range (cdr (cdr range))))
11920            ;; Adjust the first element to be the same as the lower limit. 
11921            (if (and (not (atom (car range))) 
11922                     (< (cdr (car range)) (car active)))
11923                (setcdr (car range) (1- (car active))))
11924            ;; Then we want to peel off any elements that are higher
11925            ;; than the upper active limit.  
11926            (let ((srange range))
11927              ;; Go past all legal elements.
11928              (while (and (cdr srange) 
11929                          (<= (or (and (atom (car (cdr srange)))
11930                                       (car (cdr srange)))
11931                                  (car (car (cdr srange)))) (cdr active)))
11932                (setq srange (cdr srange)))
11933              (if (cdr srange)
11934                  ;; Nuke all remaining illegal elements.
11935                  (setcdr srange nil))
11936
11937              ;; Adjust the final element.
11938              (if (and (not (atom (car srange)))
11939                       (> (cdr (car srange)) (cdr active)))
11940                  (setcdr (car srange) (cdr active))))
11941            ;; Compute the number of unread articles.
11942            (while range
11943              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11944                                          (cdr (car range))))
11945                                  (or (and (atom (car range)) (car range))
11946                                      (car (car range))))))
11947              (setq range (cdr range)))
11948            (setq num (max 0 (- (cdr active) num)))))
11949     (and info
11950          (progn
11951            (and (assq 'tick marked)
11952                 (inline (gnus-remove-illegal-marked-articles
11953                          (assq 'tick marked) (nth 2 info))))
11954            (and (assq 'dormant marked)
11955                 (inline (gnus-remove-illegal-marked-articles
11956                          (assq 'dormant marked) (nth 2 info))))
11957            (setcar
11958             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11959             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11960                                 (length (cdr (assq 'dormant marked)))))))))
11961     num))
11962
11963 (defun gnus-remove-illegal-marked-articles (marked ranges)
11964   (let ((m (cdr marked)))
11965     ;; Make sure that all ticked articles are a subset of the unread
11966     ;; articles. 
11967     (while m
11968       (if (gnus-member-of-range (car m) ranges)
11969           (setcdr marked (cdr m))
11970         (setq marked m))
11971       (setq m (cdr m)))))
11972
11973 (defun gnus-activate-newsgroup (group)
11974   (let ((method (gnus-find-method-for-group group))
11975         active)
11976     (and (or (gnus-server-opened method) (gnus-open-server method))
11977          (gnus-request-group group)
11978          (save-excursion
11979            (set-buffer nntp-server-buffer)
11980            (goto-char (point-min))
11981            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11982                 (progn
11983                   (goto-char (match-beginning 1))
11984                   (gnus-sethash 
11985                    group (setq active (cons (read (current-buffer))
11986                                             (read (current-buffer))))
11987                    gnus-active-hashtb))
11988                 active)))))
11989
11990 (defun gnus-update-read-articles 
11991   (group unread unselected ticked &optional domarks replied expirable killed
11992          dormant bookmark score)
11993   "Update the list of read and ticked articles in GROUP using the
11994 UNREAD and TICKED lists.
11995 Note: UNSELECTED has to be sorted over `<'.
11996 Returns whether the updating was successful."
11997   (let* ((active (or gnus-newsgroup-active 
11998                      (gnus-gethash group gnus-active-hashtb)))
11999          (entry (gnus-gethash group gnus-newsrc-hashtb))
12000          (number (car entry))
12001          (info (nth 2 entry))
12002          (marked (nth 3 info))
12003          (prev 1)
12004          (unread (sort (copy-sequence unread) (function <)))
12005          last read)
12006     (if (or (not info) (not active))
12007         ;; There is no info on this group if it was, in fact,
12008         ;; killed. Gnus stores no information on killed groups, so
12009         ;; there's nothing to be done. 
12010         ;; One could store the information somewhere temporarily,
12011         ;; perhaps... Hmmm... 
12012         ()
12013       ;; Remove any negative articles numbers.
12014       (while (and unread (< (car unread) 0))
12015         (setq unread (cdr unread)))
12016       ;; Remove any expired article numbers
12017       (while (and unread (< (car unread) (car active)))
12018         (setq unread (cdr unread)))
12019       (while (and ticked (< (car ticked) (car active)))
12020         (setq ticked (cdr ticked)))
12021       (while (and dormant (< (car dormant) (car active)))
12022         (setq dormant (cdr dormant)))
12023       (setq unread (sort (append unselected unread) '<))
12024       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12025       (setcar entry (max 0 (- (length unread) (length ticked) 
12026                               (length dormant))))
12027       ;; Compute the ranges of read articles by looking at the list of
12028       ;; unread articles.  
12029       (while unread
12030         (if (/= (car unread) prev)
12031             (setq read (cons (if (= prev (1- (car unread))) prev
12032                                (cons prev (1- (car unread)))) read)))
12033         (setq prev (1+ (car unread)))
12034         (setq unread (cdr unread)))
12035       (if (<= prev (cdr active))
12036           (setq read (cons (cons prev (cdr active)) read)))
12037       ;; Enter this list into the group info.
12038       (setcar (cdr (cdr info)) 
12039               (if (> (length read) 1) (nreverse read) read))
12040       ;; Enter the list of ticked articles.
12041       (gnus-set-marked-articles 
12042        info ticked
12043        (if domarks replied (cdr (assq 'reply marked)))
12044        (if domarks expirable (cdr (assq 'expire marked)))
12045        (if domarks killed (cdr (assq 'killed marked)))
12046        (if domarks dormant (cdr (assq 'dormant marked)))
12047        (if domarks bookmark (cdr (assq 'bookmark marked)))
12048        (if domarks score (cdr (assq 'score marked))))
12049       t)))
12050
12051 (defun gnus-make-articles-unread (group articles)
12052   "Mark ARTICLES in GROUP as unread."
12053   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12054                           (gnus-gethash (gnus-group-real-name group)
12055                                         gnus-newsrc-hashtb))))
12056          (ranges (nth 2 info))
12057          news)
12058     (while articles
12059       (and (gnus-member-of-range (car articles) ranges)
12060            (setq news (cons (car articles) news)))
12061       (setq articles (cdr articles)))
12062     (if (not news)
12063         ()
12064       (setcar (nthcdr 2 info)
12065               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12066       (gnus-group-update-group group t))))
12067
12068 ;; Get the active file(s) from the backend(s).
12069 (defun gnus-read-active-file ()
12070   (gnus-group-set-mode-line)
12071   (let ((methods (if (gnus-server-opened gnus-select-method)
12072                      ;; The native server is available.
12073                      (cons gnus-select-method gnus-secondary-select-methods)
12074                    ;; The native server is down, so we just do the
12075                    ;; secondary ones.   
12076                    gnus-secondary-select-methods))
12077         list-type)
12078     (setq gnus-have-read-active-file nil)
12079     (save-excursion
12080       (set-buffer nntp-server-buffer)
12081       (while methods
12082         (let* ((where (nth 1 (car methods)))
12083                (mesg (format "Reading active file%s via %s..."
12084                              (if (and where (not (zerop (length where))))
12085                                  (concat " from " where) "")
12086                              (car (car methods)))))
12087           (gnus-message 5 mesg)
12088           (gnus-check-news-server (car methods))
12089           (cond 
12090            ((and (eq gnus-read-active-file 'some)
12091                  (gnus-check-backend-function
12092                   'retrieve-groups (car (car methods))))
12093             (let ((newsrc (cdr gnus-newsrc-alist))
12094                   groups)
12095               (while newsrc
12096                 (and (gnus-server-equal 
12097                       (gnus-find-method-for-group
12098                        (car (car newsrc)) (car newsrc))
12099                       (gnus-server-get-method nil (car methods)))
12100                      (setq groups (cons (car (car newsrc)) groups)))
12101                 (setq newsrc (cdr newsrc)))
12102               (setq list-type (gnus-retrieve-groups groups (car methods)))
12103               (cond ((not list-type)
12104                      (gnus-message 
12105                       1 "Cannot read partial active file from %s server." 
12106                       (car (car methods)))
12107                      (ding)
12108                      (sit-for 2))
12109                     ((eq list-type 'active)
12110                      (gnus-active-to-gnus-format (car methods)))
12111                     (t
12112                      (gnus-groups-to-gnus-format (car methods))))))
12113            (t
12114             (if (not (gnus-request-list (car methods)))
12115                 (progn
12116                   (gnus-message 1 "Cannot read active file from %s server." 
12117                                 (car (car methods)))
12118                   (ding))
12119               (gnus-active-to-gnus-format (car methods))
12120               (setq gnus-have-read-active-file t)
12121               (gnus-message 5 "%sdone" mesg)))))
12122         (setq methods (cdr methods))))))
12123
12124 ;; Read an active file and place the results in `gnus-active-hashtb'.
12125 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12126   (let ((cur (current-buffer))
12127         (hashtb (or hashtb 
12128                     (if (and gnus-active-hashtb 
12129                              (not (equal method gnus-select-method)))
12130                         gnus-active-hashtb
12131                       (setq gnus-active-hashtb
12132                             (if (equal method gnus-select-method)
12133                                 (gnus-make-hashtable 
12134                                  (count-lines (point-min) (point-max)))
12135                               (gnus-make-hashtable 4096)))))))
12136     ;; Delete unnecessary lines.
12137     (goto-char (point-min))
12138     (while (search-forward "\nto." nil t)
12139       (delete-region (1+ (match-beginning 0)) 
12140                      (progn (forward-line 1) (point))))
12141     (or (string= gnus-ignored-newsgroups "")
12142         (progn
12143           (goto-char (point-min))
12144           (delete-matching-lines gnus-ignored-newsgroups)))
12145     ;; If these are groups from a foreign select method, we insert the
12146     ;; group prefix in front of the group names. 
12147     (and method (not (eq method gnus-select-method))
12148          (let ((prefix (gnus-group-prefixed-name "" method)))
12149            (goto-char (point-min))
12150            (while (and (not (eobp))
12151                        (progn (insert prefix)
12152                               (zerop (forward-line 1)))))))
12153     (goto-char (point-min))
12154     ;; Store active file in hashtable.
12155     (goto-char (point-min))
12156     (if (string-match "%[oO]" gnus-group-line-format)
12157         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12158         ;; If we want information on moderated groups, we use this
12159         ;; loop...   
12160         (let* ((mod-hashtb (make-vector 7 0))
12161                (m (intern "m" mod-hashtb))
12162                group max mod min)
12163           (while (not (eobp))
12164             (condition-case nil
12165                 (progn
12166                   (narrow-to-region (point) (gnus-point-at-eol))
12167                   (setq group (let ((obarray hashtb)) (read cur)))
12168                   (if (and (numberp (setq max (read cur)))
12169                            (numberp (setq min (read cur))))
12170                       (set group (cons min max))
12171                     (set group nil))
12172                   ;; Enter moderated groups into a list.
12173                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12174                       (setq gnus-moderated-list 
12175                             (cons (symbol-name group) gnus-moderated-list))))
12176               (error nil))
12177             (widen)
12178             (forward-line 1)))
12179       ;; And if we do not care about moderation, we use this loop,
12180       ;; which is faster.
12181       (let (group max min)
12182         (while (not (eobp))
12183           (condition-case ()
12184               (progn
12185                 (narrow-to-region (point) (gnus-point-at-eol))
12186                 ;; group gets set to a symbol interned in the hash table
12187                 ;; (what a hack!!)
12188                 (setq group (let ((obarray hashtb)) (read cur)))
12189                 (if (and (numberp (setq max (read cur)))
12190                          (numberp (setq min (read cur))))
12191                     (set group (cons min max))
12192                   (set group nil)))
12193             (error 
12194              (progn 
12195                (if ignore-errors
12196                    (set group nil)
12197                  (ding) 
12198                  (gnus-message 3 "Warning - illegal active: %s"
12199                                (buffer-substring 
12200                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12201                  nil))))
12202           (widen)
12203           (forward-line 1))))))
12204
12205 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12206   ;; Parse a "groups" active file.
12207   (let ((cur (current-buffer))
12208         (hashtb (or hashtb 
12209                     (if method
12210                         gnus-active-hashtb
12211                       (setq gnus-active-hashtb
12212                             (gnus-make-hashtable 
12213                              (count-lines (point-min) (point-max)))))))
12214         (prefix (and method (not (eq method gnus-select-method))
12215                      (gnus-group-prefixed-name "" method))))
12216
12217     (goto-char (point-min))
12218     (condition-case ()
12219         ;; We split this into to separate loops, one with the prefix
12220         ;; and one without to speed the reading up somewhat.
12221         (if prefix
12222             (let (min max opoint)
12223               (while (not (eobp))
12224                 (read cur) (read cur)
12225                 (setq min (read cur)
12226                       max (read cur)
12227                       opoint (point))
12228                 (skip-chars-forward " \t")
12229                 (insert prefix)
12230                 (goto-char opoint)
12231                 (set (let ((obarray hashtb)) (read cur)) 
12232                      (cons min max))
12233                 (forward-line 1)))
12234           (let (min max opoint)
12235             (while (not (eobp))
12236               (if (= (following-char) ?2)
12237                   (progn
12238                     (read cur) (read cur)
12239                     (setq min (read cur)
12240                           max (read cur))
12241                     (set (let ((obarray hashtb)) (read cur)) 
12242                          (cons min max))))
12243               (forward-line 1))))
12244       (error 
12245        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12246
12247 (defun gnus-read-newsrc-file (&optional force)
12248   "Read startup file.
12249 If FORCE is non-nil, the .newsrc file is read."
12250   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12251   ;; Reset variables that might be defined in the .newsrc.eld file.
12252   (let ((variables gnus-variable-list))
12253     (while variables
12254       (set (car variables) nil)
12255       (setq variables (cdr variables))))
12256   (let* ((newsrc-file gnus-current-startup-file)
12257          (quick-file (concat newsrc-file ".el")))
12258     (save-excursion
12259       ;; We always load the .newsrc.eld file. If always contains
12260       ;; much information that can not be gotten from the .newsrc
12261       ;; file (ticked articles, killed groups, foreign methods, etc.)
12262       (gnus-read-newsrc-el-file quick-file)
12263  
12264       (if (or force
12265               (and (file-newer-than-file-p newsrc-file quick-file)
12266                    (file-newer-than-file-p newsrc-file 
12267                                            (concat quick-file "d")))
12268               (not gnus-newsrc-alist))
12269           ;; We read the .newsrc file. Note that if there if a
12270           ;; .newsrc.eld file exists, it has already been read, and
12271           ;; the `gnus-newsrc-hashtb' has been created. While reading
12272           ;; the .newsrc file, Gnus will only use the information it
12273           ;; can find there for changing the data already read -
12274           ;; ie. reading the .newsrc file will not trash the data
12275           ;; already read (except for read articles).
12276           (save-excursion
12277             (gnus-message 5 "Reading %s..." newsrc-file)
12278             (set-buffer (find-file-noselect newsrc-file))
12279             (buffer-disable-undo (current-buffer))
12280             (gnus-newsrc-to-gnus-format)
12281             (kill-buffer (current-buffer))
12282             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12283
12284 (defun gnus-read-newsrc-el-file (file)
12285   (let ((ding-file (concat file "d")))
12286     ;; We always, always read the .eld file.
12287     (gnus-message 5 "Reading %s..." ding-file)
12288     (let (gnus-newsrc-assoc)
12289       (condition-case nil
12290           (load ding-file t t t)
12291         (error nil))
12292       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12293     (let ((inhibit-quit t))
12294       (gnus-uncompress-newsrc-alist))
12295     (gnus-make-hashtable-from-newsrc-alist)
12296     (if (not (file-newer-than-file-p file ding-file))
12297         ()
12298       ;; Old format quick file
12299       (gnus-message 5 "Reading %s..." file)
12300       ;; The .el file is newer than the .eld file, so we read that one
12301       ;; as well. 
12302       (gnus-read-old-newsrc-el-file file))))
12303
12304 ;; Parse the old-style quick startup file
12305 (defun gnus-read-old-newsrc-el-file (file)
12306   (let (newsrc killed marked group g m len info)
12307     (prog1
12308         (let ((gnus-killed-assoc nil)
12309               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12310           (prog1
12311               (condition-case nil
12312                   (load file t t t)
12313                 (error nil))
12314             (setq newsrc gnus-newsrc-assoc
12315                   killed gnus-killed-assoc
12316                   marked gnus-marked-assoc)))
12317       (setq gnus-newsrc-alist nil)
12318       (while newsrc
12319         (setq group (car newsrc))
12320         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12321           (if info
12322               (progn
12323                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12324                 (setcar (cdr info)
12325                         (if (nth 1 group) gnus-level-default-subscribed 
12326                           gnus-level-default-unsubscribed))
12327                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12328             (setq gnus-newsrc-alist
12329                   (cons 
12330                    (setq info
12331                          (list (car group)
12332                                (if (nth 1 group) gnus-level-default-subscribed
12333                                  gnus-level-default-unsubscribed) 
12334                                (cdr (cdr group))))
12335                    gnus-newsrc-alist)))
12336           (if (setq m (assoc (car group) marked))
12337             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12338         (setq newsrc (cdr newsrc)))
12339       (setq newsrc killed)
12340       (while newsrc
12341         (setcar newsrc (car (car newsrc)))
12342         (setq newsrc (cdr newsrc)))
12343       (setq gnus-killed-list killed))
12344     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12345     (gnus-make-hashtable-from-newsrc-alist)))
12346       
12347 (defun gnus-make-newsrc-file (file)
12348   "Make server dependent file name by catenating FILE and server host name."
12349   (let* ((file (expand-file-name file nil))
12350          (real-file (concat file "-" (nth 1 gnus-select-method))))
12351     (if (file-exists-p real-file)
12352         real-file file)))
12353
12354 (defun gnus-uncompress-newsrc-alist ()
12355   ;; Uncompress all lists of marked articles in the newsrc assoc.
12356   (let ((newsrc gnus-newsrc-alist)
12357         marked)
12358     (while newsrc
12359       (if (not (setq marked (nth 3 (car newsrc))))
12360           ()
12361         (while marked
12362           (or (eq 'score (car (car marked)))
12363               (eq 'bookmark (car (car marked)))
12364               (eq 'killed (car (car marked)))
12365               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12366           (setq marked (cdr marked))))
12367       (setq newsrc (cdr newsrc)))))
12368
12369 (defun gnus-compress-newsrc-alist ()
12370   ;; Compress all lists of marked articles in the newsrc assoc.
12371   (let ((newsrc gnus-newsrc-alist)
12372         marked)
12373     (while newsrc
12374       (if (not (setq marked (nth 3 (car newsrc))))
12375           ()
12376         (while marked
12377           (or (eq 'score (car (car marked)))
12378               (eq 'bookmark (car (car marked)))
12379               (eq 'killed (car (car marked)))
12380               (setcdr (car marked) 
12381                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
12382           (setq marked (cdr marked))))
12383       (setq newsrc (cdr newsrc)))))
12384
12385 (defun gnus-newsrc-to-gnus-format ()
12386   (setq gnus-newsrc-options "")
12387   (setq gnus-newsrc-options-n nil)
12388
12389   (or gnus-active-hashtb
12390       (setq gnus-active-hashtb (make-vector 4095 0)))
12391   (let ((buf (current-buffer))
12392         (already-read (> (length gnus-newsrc-alist) 1))
12393         group level subscribed info options-symbol newsrc
12394         symbol reads num1)
12395     (goto-char (point-min))
12396     ;; We intern the symbol `options' in the active hashtb so that we
12397     ;; can `eq' against it later.
12398     (setq options-symbol (intern "options" gnus-active-hashtb))
12399   
12400     (while (not (eobp))
12401       ;; We first read the first word on the line by narrowing and
12402       ;; then reading into `gnus-active-hashtb'.  Most groups will
12403       ;; already exist in that hashtb, so this will save some string
12404       ;; space.
12405       (narrow-to-region
12406        (point)
12407        (progn (skip-chars-forward "^ \t!:\n") (point)))
12408       (goto-char (point-min))
12409       (setq symbol 
12410             (and (/= (point-min) (point-max))
12411                  (let ((obarray gnus-active-hashtb)) (read buf))))
12412       (widen)
12413       ;; Now, the symbol we have read is either `options' or a group
12414       ;; name.  If it is an options line, we just add it to a string. 
12415       (cond 
12416        ((eq symbol options-symbol)
12417         (setq gnus-newsrc-options
12418               ;; This concatting is quite inefficient, but since our
12419               ;; thorough studies show that approx 99.37% of all
12420               ;; .newsrc files only contain a single options line, we
12421               ;; don't give a damn, frankly, my dear.
12422               (concat gnus-newsrc-options
12423                       (buffer-substring 
12424                        (gnus-point-at-bol)
12425                        ;; Options may continue on the next line.
12426                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12427                                 (progn (beginning-of-line) (point)))
12428                            (point))))))
12429        (symbol
12430         ;; It was a group name.
12431         (setq subscribed (= (following-char) ?:)
12432               group (symbol-name symbol)
12433               reads nil)
12434         (if (eolp)
12435             ;; If the line ends here, this is clearly a buggy line, so
12436             ;; we put point a the beginning of line and let the cond
12437             ;; below do the error handling.
12438             (beginning-of-line)
12439           ;; We skip to the beginning of the ranges.
12440           (skip-chars-forward "!: \t"))
12441         ;; We are now at the beginning of the list of read articles.
12442         ;; We read them range by range.
12443         (while
12444             (cond 
12445              ((looking-at "[0-9]+")
12446               ;; We narrow and read a number instead of buffer-substring/
12447               ;; string-to-int because it's faster. narrow/widen is
12448               ;; faster than save-restriction/narrow, and save-restriction
12449               ;; produces a garbage object.
12450               (setq num1 (progn
12451                            (narrow-to-region (match-beginning 0) (match-end 0))
12452                            (read buf)))
12453               (widen)
12454               ;; If the next character is a dash, then this is a range.
12455               (if (= (following-char) ?-)
12456                   (progn
12457                     ;; We read the upper bound of the range.
12458                     (forward-char 1)
12459                     (if (not (looking-at "[0-9]+"))
12460                         ;; This is a buggy line, by we pretend that
12461                         ;; it's kinda OK. Perhaps the user should be
12462                         ;; dinged? 
12463                         (setq reads (cons num1 reads))
12464                       (setq reads 
12465                             (cons 
12466                              (cons num1 (progn
12467                                           (narrow-to-region (match-beginning 0) 
12468                                                             (match-end 0))
12469                                           (read buf)))
12470                              reads))
12471                       (widen)))
12472                 ;; It was just a simple number, so we add it to the
12473                 ;; list of ranges.
12474                 (setq reads (cons num1 reads)))
12475               ;; If the next char in ?\n, then we have reached the end
12476               ;; of the line and return nil.
12477               (/= (following-char) ?\n))
12478              ((= (following-char) ?\n)
12479               ;; End of line, so we end.
12480               nil)
12481              (t
12482               ;; Not numbers and not eol, so this might be a buggy
12483               ;; line... 
12484               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12485                   (progn
12486                     ;; The line was buggy.
12487                     (setq group nil)
12488                     (gnus-message 3 "Mangled line: %s" 
12489                                   (buffer-substring (gnus-point-at-bol) 
12490                                                     (gnus-point-at-eol)))
12491                     (ding)
12492                     (sit-for 1)))
12493               nil))
12494           ;; Skip past ", ". Spaces are illegal in these ranges, but
12495           ;; we allow them, because it's a common mistake to put a
12496           ;; space after the comma.
12497           (skip-chars-forward ", "))
12498
12499         ;; We have already read .newsrc.eld, so we gently update the
12500         ;; data in the hash table with the information we have just
12501         ;; read. 
12502         (if (not group)
12503             ()
12504           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12505                 level)
12506             (if info
12507                 ;; There is an entry for this file in the alist.
12508                 (progn
12509                   (setcar (nthcdr 2 info) (nreverse reads))
12510                   ;; We update the level very gently.  In fact, we
12511                   ;; only change it if there's been a status change
12512                   ;; from subscribed to unsubscribed, or vice versa.
12513                   (setq level (nth 1 info))
12514                   (cond ((and (<= level gnus-level-subscribed)
12515                               (not subscribed))
12516                          (setq level (if reads
12517                                          gnus-level-default-unsubscribed 
12518                                        (1+ gnus-level-default-unsubscribed))))
12519                         ((and (> level gnus-level-subscribed) subscribed)
12520                          (setq level gnus-level-default-subscribed)))
12521                   (setcar (cdr info) level))
12522               ;; This is a new group.
12523               (setq info (list group 
12524                                (if subscribed
12525                                    gnus-level-default-subscribed 
12526                                  (if reads
12527                                      (1+ gnus-level-subscribed)
12528                                    gnus-level-default-unsubscribed))
12529                                (nreverse reads))))
12530             (setq newsrc (cons info newsrc))))))
12531       (forward-line 1))
12532     
12533     (setq newsrc (nreverse newsrc))
12534
12535     (if (not already-read)
12536         ()
12537       ;; We now have two newsrc lists - `newsrc', which is what we
12538       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12539       ;; what we've read from .newsrc.eld. We have to merge these
12540       ;; lists. We do this by "attaching" any (foreign) groups in the
12541       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12542       (let ((rc (cdr gnus-newsrc-alist))
12543             (prev gnus-newsrc-alist)
12544             entry mentry)
12545         (while rc
12546           (or (null (nth 4 (car rc))) ; It's a native group.
12547               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12548               (if (setq entry (assoc (car (car prev)) newsrc))
12549                   (setcdr (setq mentry (memq entry newsrc))
12550                           (cons (car rc) (cdr mentry)))
12551                 (setq newsrc (cons (car rc) newsrc))))
12552           (setq prev rc
12553                 rc (cdr rc)))))
12554
12555     (setq gnus-newsrc-alist newsrc)
12556     ;; We make the newsrc hashtb.
12557     (gnus-make-hashtable-from-newsrc-alist)
12558
12559     ;; Finally, if we read some options lines, we parse them.
12560     (or (string= gnus-newsrc-options "")
12561         (gnus-newsrc-parse-options gnus-newsrc-options))))
12562
12563 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12564 ;; The return value will be a list on the form
12565 ;; ((regexp1 . ignore)
12566 ;;  (regexp2 . subscribe)...)
12567 ;; When handling new newsgroups, groups that match a `ignore' regexp
12568 ;; will be ignored, and groups that match a `subscribe' regexp will be
12569 ;; subscribed. A line like
12570 ;; options -n !all rec.all
12571 ;; will lead to a list that looks like
12572 ;; (("^rec\\..+" . subscribe) 
12573 ;;  ("^.+" . ignore))
12574 ;; So all "rec.*" groups will be subscribed, while all the other
12575 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12576 ;; different from "options -n rec.all !all". 
12577 (defun gnus-newsrc-parse-options (options)
12578   (let (out eol)
12579     (save-excursion
12580       (gnus-set-work-buffer)
12581       (insert (regexp-quote options))
12582       ;; First we treat all continuation lines.
12583       (goto-char (point-min))
12584       (while (re-search-forward "\n[ \t]+" nil t)
12585         (replace-match " " t t))
12586       ;; Then we transform all "all"s into ".+"s.
12587       (goto-char (point-min))
12588       (while (re-search-forward "\\ball\\b" nil t)
12589         (replace-match ".+" t t))
12590       (goto-char (point-min))
12591       ;; We remove all other options than the "-n" ones.
12592       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12593         (replace-match " ")
12594         (forward-char -1))
12595       (goto-char (point-min))
12596
12597       ;; We are only interested in "options -n" lines - we
12598       ;; ignore the other option lines.
12599       (while (re-search-forward "[ \t]-n" nil t)
12600         (setq eol 
12601               (or (save-excursion
12602                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12603                          (- (point) 2)))
12604                   (gnus-point-at-eol)))
12605         ;; Search for all "words"...
12606         (while (re-search-forward "[^ \t,\n-]+" eol t)
12607           (if (= (char-after (match-beginning 0)) ?!)
12608               ;; If the word begins with a bang (!), this is a "not"
12609               ;; spec. We put this spec (minus the bang) and the
12610               ;; symbol `ignore' into the list.
12611               (setq out (cons (cons (concat 
12612                                      "^" (buffer-substring 
12613                                           (1+ (match-beginning 0))
12614                                           (match-end 0)))
12615                                     'ignore) out))
12616             ;; There was no bang, so this is a "yes" spec.
12617             (setq out (cons (cons (concat 
12618                                    "^" (buffer-substring (match-beginning 0)
12619                                                          (match-end 0)))
12620                                   'subscribe) out)))))
12621     
12622       (setq gnus-newsrc-options-n out))))
12623                
12624
12625 (defun gnus-save-newsrc-file ()
12626   "Save .newsrc file."
12627   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12628   ;; from the variable gnus-newsrc-alist.
12629   (and (or gnus-newsrc-alist gnus-killed-list)
12630        gnus-current-startup-file
12631        (let ((make-backup-files t)
12632              (version-control nil)
12633              (require-final-newline t)) ;Don't ask even if requested.
12634          ;; You can stop or change version control of backup file.
12635          ;; Suggested by jason@violet.berkeley.edu.
12636          (run-hooks 'gnus-save-newsrc-hook)
12637          (save-excursion
12638            (if (or (not gnus-dribble-buffer)
12639                    (not (buffer-name gnus-dribble-buffer))
12640                    (zerop (save-excursion
12641                             (set-buffer gnus-dribble-buffer)
12642                             (buffer-size))))
12643                (gnus-message 4 "(No changes need to be saved)")
12644              (if gnus-save-newsrc-file
12645                  (progn
12646                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12647                    ;; Make backup file of master newsrc.
12648                    (gnus-gnus-to-newsrc-format)
12649                    (gnus-message 5 "Saving %s...done"
12650                                  gnus-current-startup-file)))
12651              ;; Quickly loadable .newsrc.
12652              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12653              (gnus-add-current-to-buffer-list)
12654              (buffer-disable-undo (current-buffer))
12655              (erase-buffer)
12656              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12657              (gnus-gnus-to-quick-newsrc-format)
12658              (write-region 1 (point-max) 
12659                            (concat gnus-current-startup-file ".eld") 
12660                            nil 'nomesg)
12661              (kill-buffer (current-buffer))
12662              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12663              (gnus-dribble-delete-file))))))
12664
12665 (defun gnus-gnus-to-quick-newsrc-format ()
12666   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12667   (insert ";; (ding) Gnus startup file.\n")
12668   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12669   (insert ";; to read .newsrc.\n")
12670   (let ((variables gnus-variable-list)
12671         (inhibit-quit t)
12672         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12673         variable)
12674     ;; insert lisp expressions.
12675     (gnus-compress-newsrc-alist)
12676     (while variables
12677       (setq variable (car variables))
12678       (and (boundp variable)
12679            (symbol-value variable)
12680            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12681            (insert "(setq " (symbol-name variable) " '"
12682                    (prin1-to-string (symbol-value variable))
12683                    ")\n"))
12684       (setq variables (cdr variables)))
12685     (gnus-uncompress-newsrc-alist)))
12686
12687
12688 (defun gnus-gnus-to-newsrc-format ()
12689   ;; Generate and save the .newsrc file.
12690   (let ((newsrc (cdr gnus-newsrc-alist))
12691         info ranges range)
12692     (save-excursion
12693       (set-buffer (create-file-buffer gnus-startup-file))
12694       (buffer-disable-undo (current-buffer))
12695       (erase-buffer)
12696       ;; Write options.
12697       (if gnus-newsrc-options (insert gnus-newsrc-options))
12698       ;; Write subscribed and unsubscribed.
12699       (while newsrc
12700         (setq info (car newsrc))
12701         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12702             (progn
12703               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12704                                      "!" ":"))
12705               (if (setq ranges (nth 2 info))
12706                   (progn
12707                     (insert " ")
12708                     (if (not (listp (cdr ranges)))
12709                         (if (= (car ranges) (cdr ranges))
12710                             (insert (int-to-string (car ranges)))
12711                           (insert (int-to-string (car ranges)) "-" 
12712                                   (int-to-string (cdr ranges))))
12713                       (while ranges
12714                         (setq range (car ranges)
12715                               ranges (cdr ranges))
12716                         (if (or (atom range) (= (car range) (cdr range)))
12717                             (insert (int-to-string 
12718                                      (or (and (atom range) range) 
12719                                          (car range))))
12720                           (insert (int-to-string (car range)) "-"
12721                                   (int-to-string (cdr range))))
12722                         (if ranges (insert ","))))))
12723               (insert "\n")))
12724         (setq newsrc (cdr newsrc)))
12725       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12726       (kill-buffer (current-buffer)))))
12727
12728 (defun gnus-read-all-descriptions-files ()
12729   (let ((methods (nconc (list gnus-select-method) 
12730                         gnus-secondary-select-methods)))
12731     (while methods
12732       (gnus-read-descriptions-file (car methods))
12733       (setq methods (cdr methods)))
12734     t))
12735
12736 (defun gnus-read-descriptions-file (&optional method)
12737   (let ((method (or method gnus-select-method)))
12738     ;; We create the hashtable whether we manage to read the desc file
12739     ;; to avoid trying to re-read after a failed read.
12740     (or gnus-description-hashtb
12741         (setq gnus-description-hashtb 
12742               (gnus-make-hashtable (length gnus-active-hashtb))))
12743     ;; Mark this method's desc file as read.
12744     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12745                   gnus-description-hashtb)
12746
12747     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12748     (cond 
12749      ((not (or (gnus-server-opened method)
12750                (gnus-open-server method)))
12751       (gnus-message 1 "Couldn't open server")
12752       nil)
12753      ((not (gnus-request-list-newsgroups method))
12754       (gnus-message 1 "Couldn't read newsgroups descriptions")
12755       nil)
12756      (t
12757       (let (group)
12758         (save-excursion
12759           (save-restriction
12760             (set-buffer nntp-server-buffer)
12761             (goto-char (point-min))
12762             (if (or (search-forward "\n.\n" nil t)
12763                     (goto-char (point-max)))
12764                 (progn
12765                   (beginning-of-line)
12766                   (narrow-to-region (point-min) (point))))
12767             (goto-char (point-min))
12768             (while (not (eobp))
12769               ;; If we get an error, we set group to 0, which is not a
12770               ;; symbol... 
12771               (setq group 
12772                     (condition-case ()
12773                         (let ((obarray gnus-description-hashtb))
12774                           ;; Group is set to a symbol interned in this
12775                           ;; hash table.
12776                           (read nntp-server-buffer))
12777                       (error 0)))
12778               (skip-chars-forward " \t")
12779               ;; ... which leads to this line being effectively ignored.
12780               (and (symbolp group)
12781                    (set group (buffer-substring 
12782                                (point) (progn (end-of-line) (point)))))
12783               (forward-line 1))))
12784         (gnus-message 5 "Reading descriptions file...done")
12785         t)))))
12786
12787 (defun gnus-group-get-description (group)
12788   ;; Get the description of a group by sending XGTITLE to the server.
12789   (and (gnus-request-group-description group)
12790        (save-excursion
12791          (set-buffer nntp-server-buffer)
12792          (goto-char (point-min))
12793          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12794               (buffer-substring (match-beginning 1) (match-end 1))))))
12795
12796 ;;;
12797 ;;; Server
12798 ;;;
12799
12800 (defvar gnus-server-mode-hook nil
12801   "Hook run in `gnus-server-mode' buffers.")
12802
12803 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12804   "Format of server lines.
12805 It works along the same lines as a normal formatting string,
12806 with some simple extensions.")
12807
12808 (defvar gnus-server-mode-line-format "(ding) List of servers"
12809   "The format specification for the server mode line.")
12810
12811 (defconst gnus-server-line-format-alist
12812   (list (list ?h 'how ?s)
12813         (list ?n 'name ?s)
12814         (list ?w 'where ?s)
12815         ))
12816
12817 (defconst gnus-server-mode-line-format-alist 
12818   (list (list ?S 'news-server ?s)
12819         (list ?M 'news-method ?s)
12820         (list ?u 'user-defined ?s)))
12821
12822 (defvar gnus-server-line-format-spec nil)
12823 (defvar gnus-server-mode-line-format-spec nil)
12824 (defvar gnus-server-killed-servers nil)
12825
12826 (defvar gnus-server-mode-map nil)
12827 (put 'gnus-server-mode 'mode-class 'special)
12828
12829 (if gnus-server-mode-map
12830     nil
12831   (setq gnus-server-mode-map (make-sparse-keymap))
12832   (suppress-keymap gnus-server-mode-map)
12833   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12834   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12835   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12836   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12837   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12838   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12839   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12840   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12841   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12842   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12843
12844 (defun gnus-server-mode ()
12845   "Major mode for listing and editing servers.
12846
12847 All normal editing commands are switched off.
12848 \\<gnus-server-mode-map>
12849
12850 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12851
12852 The following commands are available:
12853
12854 \\{gnus-server-mode-map}"
12855   (interactive)
12856   (if gnus-visual (gnus-server-make-menu-bar))
12857   (kill-all-local-variables)
12858   (setq mode-line-modified "-- ")
12859   (make-local-variable 'mode-line-format)
12860   (setq mode-line-format (copy-sequence mode-line-format))
12861   (and (equal (nth 3 mode-line-format) "   ")
12862        (setcar (nthcdr 3 mode-line-format) ""))
12863   (setq major-mode 'gnus-server-mode)
12864   (setq mode-name "Server")
12865 ;  (gnus-group-set-mode-line)
12866   (setq mode-line-process nil)
12867   (use-local-map gnus-server-mode-map)
12868   (buffer-disable-undo (current-buffer))
12869   (setq truncate-lines t)
12870   (setq buffer-read-only t)
12871   (run-hooks 'gnus-server-mode-hook))
12872
12873 (defun gnus-server-insert-server-line (sformat name method)
12874   (let* ((sformat (or sformat gnus-server-line-format-spec))
12875          (how (car method))
12876          (where (nth 1 method))
12877          b)
12878     (beginning-of-line)
12879     (setq b (point))
12880     ;; Insert the text.
12881     (insert (eval sformat))
12882     (add-text-properties 
12883      b (1+ b) (list 'gnus-server (intern name)))))
12884
12885 (defun gnus-server-setup-buffer ()
12886   (if (get-buffer gnus-server-buffer)
12887       ()
12888     (save-excursion
12889       (set-buffer (get-buffer-create gnus-server-buffer))
12890       (gnus-server-mode)
12891       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12892
12893 (defun gnus-server-prepare ()
12894   (setq gnus-server-mode-line-format-spec 
12895         (gnus-parse-format gnus-server-mode-line-format 
12896                            gnus-server-mode-line-format-alist))
12897   (setq gnus-server-line-format-spec 
12898         (gnus-parse-format gnus-server-line-format 
12899                            gnus-server-line-format-alist))
12900   (let ((alist gnus-server-alist)
12901         (buffer-read-only nil))
12902     (erase-buffer)
12903     (while alist
12904       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12905       (setq alist (cdr alist))))
12906   (goto-char (point-min))
12907   (gnus-server-position-cursor))
12908
12909 (defun gnus-server-server-name ()
12910   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12911     (and server (symbol-name server))))
12912
12913 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12914
12915 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12916
12917 (defun gnus-server-update-server (server)
12918   (save-excursion
12919     (set-buffer gnus-server-buffer)
12920     (let ((buffer-read-only nil)
12921           (info (cdr (assoc server gnus-server-alist))))
12922       (gnus-dribble-enter 
12923        (concat "(gnus-server-set-info \"" server "\" '"
12924                (prin1-to-string info) ")"))
12925       ;; Buffer may be narrowed.
12926       (save-restriction
12927         (widen)
12928         (if (gnus-server-goto-server server)
12929             (delete-region (progn (beginning-of-line) (point))
12930                            (progn (forward-line 1) (point))))
12931         (let ((entry (assoc server gnus-server-alist)))
12932           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12933           (gnus-server-position-cursor))))))
12934
12935 (defun gnus-server-set-info (server info)
12936   ;; Enter a select method into the virtual server alist.
12937   (gnus-dribble-enter 
12938    (concat "(gnus-server-set-info \"" server "\" '"
12939            (prin1-to-string info) ")"))
12940   (let* ((server (nth 1 info))
12941          (entry (assoc server gnus-server-alist)))
12942     (if entry (setcdr entry info)
12943       (setq gnus-server-alist
12944             (nconc gnus-server-alist (list (cons server info)))))))
12945
12946 (defun gnus-server-to-method (server)
12947   ;; Map virtual server names to select methods.
12948   (or (and (equal server "native") gnus-select-method)
12949       (cdr (assoc server gnus-server-alist))))
12950
12951 (defun gnus-server-extend-method (group method)
12952   ;; This function "extends" a virtual server.  If the server is
12953   ;; "hello", and the select method is ("hello" (my-var "something")) 
12954   ;; in the group "alt.alt", this will result in a new virtual server
12955   ;; called "helly+alt.alt".
12956   (let ((entry
12957          (gnus-copy-sequence 
12958           (if (equal (car method) "native") gnus-select-method
12959               (cdr (assoc (car method) gnus-server-alist))))))
12960     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12961     (nconc entry (cdr method))))
12962
12963 (defun gnus-server-get-method (group method)
12964   ;; Input either a server name, and extended server name, or a
12965   ;; select method, and return a select method. 
12966   (cond ((stringp method)
12967          (gnus-server-to-method method))
12968         ((stringp (car method))
12969          (gnus-server-extend-method group method))
12970         (t
12971          (gnus-server-add-address method))))
12972
12973 (defun gnus-server-add-address (method)
12974   (let ((method-name (symbol-name (car method))))
12975     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12976              (not (assq (intern (concat method-name "-address")) method)))
12977         (append method (list (list (intern (concat method-name "-address"))
12978                                    (nth 1 method))))
12979       method)))
12980
12981 (defun gnus-server-equal (s1 s2)
12982   (or (equal s1 s2)
12983       (and (= (length s1) (length s2))
12984            (progn
12985              (while (and s1 (member (car s1) s2))
12986                (setq s1 (cdr s1)))
12987              (null s1)))))
12988
12989 ;;; Interactive server functions.
12990
12991 (defun gnus-server-kill-server (server)
12992   "Kill the server on the current line."
12993   (interactive (list (gnus-server-server-name)))
12994   (or (gnus-server-goto-server server)
12995       (if server (error "No such server: %s" server)
12996         (error "No server on the current line")))
12997   (let ((buffer-read-only nil))
12998     (delete-region (progn (beginning-of-line) (point))
12999                    (progn (forward-line 1) (point))))
13000   (setq gnus-server-killed-servers 
13001         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13002   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13003                                 gnus-server-alist))
13004   (gnus-server-position-cursor))
13005
13006 (defun gnus-server-yank-server ()
13007   "Yank the previously killed server."
13008   (interactive)
13009   (or gnus-server-killed-servers
13010       (error "No killed servers to be yanked"))
13011   (let ((alist gnus-server-alist)
13012         (server (gnus-server-server-name))
13013         (killed (car gnus-server-killed-servers)))
13014     (if (not server) 
13015         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13016       (if (string= server (car (car gnus-server-alist)))
13017           (setq gnus-server-alist (cons killed gnus-server-alist))
13018         (while (and (cdr alist)
13019                     (not (string= server (car (car (cdr alist))))))
13020           (setq alist (cdr alist)))
13021         (setcdr alist (cons killed (cdr alist)))))
13022     (gnus-server-update-server (car killed))
13023     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13024     (gnus-server-position-cursor)))
13025
13026 (defun gnus-server-exit ()
13027   "Return to the group buffer."
13028   (interactive)
13029   (kill-buffer (current-buffer))
13030   (switch-to-buffer gnus-group-buffer))
13031
13032 (defun gnus-server-list-servers ()
13033   "List all available servers."
13034   (interactive)
13035   (let ((cur (gnus-server-server-name)))
13036     (gnus-server-prepare)
13037     (if cur (gnus-server-goto-server cur)
13038       (goto-char (point-max))
13039       (forward-line -1))
13040     (gnus-server-position-cursor)))
13041
13042 (defun gnus-server-copy-server (from to)
13043   (interactive
13044    (list
13045     (or (gnus-server-server-name)
13046         (error "No server on the current line"))
13047     (read-string "Copy to: ")))
13048   (or from (error "No server on current line"))
13049   (or (and to (not (string= to ""))) (error "No name to copy to"))
13050   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13051   (or (assoc from gnus-server-alist) 
13052       (error "%s: no such server" from))
13053   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13054     (setcar to-entry to)
13055     (setcar (nthcdr 2 to-entry) to)
13056     (setq gnus-server-killed-servers 
13057           (cons to-entry gnus-server-killed-servers))
13058     (gnus-server-yank-server)))
13059
13060 (defun gnus-server-add-server (how where)
13061   (interactive 
13062    (list (intern (completing-read "Server method: "
13063                                   gnus-valid-select-methods nil t))
13064          (read-string "Server name: ")))
13065   (setq gnus-server-killed-servers 
13066         (cons (list where how where) gnus-server-killed-servers))
13067   (gnus-server-yank-server))
13068
13069 (defun gnus-server-goto-server (server)
13070   "Jump to a server line."
13071   (interactive
13072    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13073   (let ((to (text-property-any (point-min) (point-max) 
13074                                'gnus-server (intern server))))
13075     (and to
13076          (progn
13077            (goto-char to) 
13078            (gnus-server-position-cursor)))))
13079
13080 (defun gnus-server-edit-server (server)
13081   "Edit the server on the current line."
13082   (interactive (list (gnus-server-server-name)))
13083   (or server
13084       (error "No server on current line"))
13085   (let ((winconf (current-window-configuration)))
13086     (get-buffer-create gnus-server-edit-buffer)
13087     (gnus-configure-windows 'edit-server)
13088     (gnus-add-current-to-buffer-list)
13089     (emacs-lisp-mode)
13090     (make-local-variable 'gnus-prev-winconf)
13091     (setq gnus-prev-winconf winconf)
13092     (use-local-map (copy-keymap (current-local-map)))
13093     (let ((done-func '(lambda () 
13094                         "Exit editing mode and update the information."
13095                         (interactive)
13096                         (gnus-server-edit-server-done 'group))))
13097       (setcar (cdr (nth 4 done-func)) server)
13098       (local-set-key "\C-c\C-c" done-func))
13099     (erase-buffer)
13100     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13101     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13102
13103 (defun gnus-server-edit-server-done (server)
13104   (interactive)
13105   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13106   (goto-char (point-min))
13107   (let ((form (read (current-buffer)))
13108         (winconf gnus-prev-winconf))
13109     (gnus-server-set-info server form)
13110     (kill-buffer (current-buffer))
13111     (and winconf (set-window-configuration winconf))
13112     (set-buffer gnus-server-buffer)
13113     (gnus-server-update-server (gnus-server-server-name))
13114     (gnus-server-position-cursor)))
13115
13116 (defun gnus-server-read-server (server)
13117   "Browse a server."
13118   (interactive (list (gnus-server-server-name)))
13119   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13120
13121 (defun gnus-mouse-pick-server (e)
13122   (interactive "e")
13123   (mouse-set-point e)
13124   (gnus-server-read-server (gnus-server-server-name)))
13125
13126 ;;;
13127 ;;; entry points into gnus-score.el
13128 ;;;
13129
13130 ;;; Finding score files. 
13131
13132 (defvar gnus-global-score-files nil
13133   "*List of global score files and directories.
13134 Set this variable if you want to use people's score files.  One entry
13135 for each score file or each score file directory.  Gnus will decide
13136 by itself what score files are applicable to which group.
13137
13138 Say you want to use the single score file
13139 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13140 score files in the \"/ftp.some-where:/pub/score\" directory.
13141
13142  (setq gnus-global-score-files
13143        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13144          \"/ftp.some-where:/pub/score\"))")
13145
13146 (defun gnus-score-score-files (group)
13147   "Return a list of all possible score files."
13148   ;; Search and set any global score files.
13149   (and gnus-global-score-files 
13150        (or gnus-internal-global-score-files
13151            (gnus-score-search-global-directories gnus-global-score-files)))
13152   ;; Fix the kill-file dir variable.
13153   (setq gnus-kill-files-directory 
13154         (file-name-as-directory
13155          (or gnus-kill-files-directory "~/News/")))
13156   ;; If we can't read it, there are no score files.
13157   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13158       (setq gnus-score-file-list nil)
13159     (if (gnus-use-long-file-name 'not-score)
13160         ;; We want long file names.
13161         (if (or (not gnus-score-file-list)
13162                 (not (car gnus-score-file-list))
13163                 (gnus-file-newer-than gnus-kill-files-directory
13164                                       (car gnus-score-file-list)))
13165               (setq gnus-score-file-list 
13166                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13167                           (nreverse 
13168                            (directory-files 
13169                             gnus-kill-files-directory t 
13170                             (gnus-score-file-regexp))))))
13171       ;; We do not use long file names, so we have to do some
13172       ;; directory traversing.  
13173       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13174             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13175             dir files suffix)
13176         (while suffixes
13177           (setq dir (expand-file-name
13178                      (concat gnus-kill-files-directory
13179                              (gnus-replace-chars-in-string group ?. ?/))))
13180           (setq suffix (car suffixes)
13181                 suffixes (cdr suffixes))
13182           (if (file-exists-p (concat dir "/" suffix))
13183               (setq files (cons (concat dir "/" suffix) files)))
13184           (while (>= (1+ (length dir)) mdir)
13185             (and (file-exists-p (concat dir "/all/" suffix))
13186                  (setq files (cons (concat dir "/all/" suffix) files)))
13187             (string-match "/[^/]*$" dir)
13188             (setq dir (substring dir 0 (match-beginning 0)))))
13189         (setq gnus-score-file-list 
13190               (cons nil (nreverse files)))))
13191     (cdr gnus-score-file-list)))
13192
13193 (defun gnus-score-file-regexp ()
13194   (concat "\\(" gnus-score-file-suffix 
13195           "\\|" gnus-adaptive-file-suffix "\\)$"))
13196         
13197 (defun gnus-score-find-bnews (group)
13198   "Return a list of score files for GROUP.
13199 The score files are those files in the ~/News directory which matches
13200 GROUP using BNews sys file syntax."
13201   (let* ((sfiles (append (gnus-score-score-files group)
13202                          gnus-internal-global-score-files))
13203          (kill-dir (file-name-as-directory 
13204                     (expand-file-name gnus-kill-files-directory)))
13205          (klen (length kill-dir))
13206          ofiles not-match regexp)
13207     (save-excursion
13208       (set-buffer (get-buffer-create "*gnus score files*"))
13209       (buffer-disable-undo (current-buffer))
13210       ;; Go through all score file names and create regexp with them
13211       ;; as the source.  
13212       (while sfiles
13213         (erase-buffer)
13214         (insert (car sfiles))
13215         (goto-char (point-min))
13216         ;; First remove the suffix itself.
13217         (re-search-forward (concat "." (gnus-score-file-regexp)))
13218         (replace-match "" t t) 
13219         (goto-char (point-min))
13220         (if (looking-at (regexp-quote kill-dir))
13221             ;; If the file name was just "SCORE", `klen' is one character
13222             ;; too much.
13223             (delete-char (min (1- (point-max)) klen))
13224           (goto-char (point-max))
13225           (search-backward "/")
13226           (delete-region (1+ (point)) (point-min)))
13227         ;; If short file names were used, we have to translate slashes.
13228         (goto-char (point-min))
13229         (while (search-forward "/" nil t)
13230           (replace-match "." t t))
13231         ;; Translate "all" to ".*".
13232         (while (search-forward "all" nil t)
13233           (replace-match ".*" t t))
13234         (goto-char (point-min))
13235         ;; Deal with "not."s.
13236         (if (looking-at "not.")
13237             (progn
13238               (setq not-match t)
13239               (setq regexp (buffer-substring 5 (point-max))))
13240           (setq regexp (buffer-substring 1 (point-max)))
13241           (setq not-match nil))
13242         ;; Finally - if this resulting regexp matches the group name,
13243         ;; we add this score file to the list of score files
13244         ;; applicable to this group.
13245         (if (or (and not-match
13246                      (not (string-match regexp group)))
13247                 (and (not not-match)
13248                      (string-match regexp group)))
13249             (setq ofiles (cons (car sfiles) ofiles)))
13250         (setq sfiles (cdr sfiles)))
13251       (kill-buffer (current-buffer))
13252       ;; Slight kludge here - the last score file returned should be
13253       ;; the local score file, whether it exists or not. This is so
13254       ;; that any score commands the user enters will go to the right
13255       ;; file, and not end up in some global score file.
13256       (let ((localscore
13257              (expand-file-name
13258               (if (gnus-use-long-file-name 'not-score)
13259                   (concat gnus-kill-files-directory group "." 
13260                           gnus-score-file-suffix)
13261                 (concat gnus-kill-files-directory
13262                         (gnus-replace-chars-in-string group ?. ?/)
13263                         "/" gnus-score-file-suffix)))))
13264         (and (member localscore ofiles)
13265              (delete localscore ofiles))
13266         (setq ofiles (cons localscore ofiles)))
13267       (nreverse ofiles))))
13268
13269 (defun gnus-score-find-single (group)
13270   "Return list containing the score file for GROUP."
13271   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13272         (gnus-score-file-name group)))
13273
13274 (defun gnus-score-find-hierarchical (group)
13275   "Return list of score files for GROUP.
13276 This includes the score file for the group and all its parents."
13277   (let ((all (copy-sequence '(nil)))
13278         (start 0))
13279     (while (string-match "\\." group (1+ start))
13280       (setq start (match-beginning 0))
13281       (setq all (cons (substring group 0 start) all)))
13282     (setq all (cons group all))
13283     (nconc
13284      (mapcar (lambda (newsgroup)
13285                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13286              (setq all (nreverse all)))
13287      (mapcar 'gnus-score-file-name all))))
13288
13289 (defvar gnus-score-file-alist-cache nil)
13290
13291 (defun gnus-score-find-alist (group)
13292   "Return list of score files for GROUP.
13293 The list is determined from the variable gnus-score-file-alist."
13294   (let ((alist gnus-score-file-multiple-match-alist)
13295         score-files)
13296     ;; if this group has been seen before, return the cached entry
13297     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13298         (cdr score-files)       ; ensures caching of groups with no matches
13299       ;; handle the multiple match alist
13300       (while alist
13301         (and (string-match (car (car alist)) group)
13302              (setq score-files
13303                    (nconc score-files (cdr (car alist)))))
13304         (setq alist (cdr alist)))
13305       (setq alist gnus-score-file-single-match-alist)
13306       ;; handle the single match alist
13307       (catch 'done
13308         (while alist
13309           (and (string-match (car (car alist)) group)
13310                ;; progn used just in case ("regexp") has no files
13311                ;; and score-files is still nil. -sj
13312                ;; this can be construed as a "stop searching here" feature :>
13313                ;; and used to simplify regexps in the single-alist 
13314                (progn
13315                  (setq score-files
13316                        (append score-files (cdr (car alist))))
13317                  (throw 'done nil)))
13318           (setq alist (cdr alist))))
13319       ;; cache the score files
13320       (setq gnus-score-file-alist-cache
13321             (cons (cons group score-files) gnus-score-file-alist-cache))
13322       score-files)))
13323
13324
13325 (defun gnus-possibly-score-headers (&optional trace)
13326   (let ((func gnus-score-find-score-files-function)
13327         score-files scores)
13328     (and func (not (listp func))
13329          (setq func (list func)))
13330     ;; Go through all the functions for finding score files (or actual
13331     ;; scores) and add them to a list.
13332     (setq score-files (copy-sequence
13333                        (gnus-score-find-alist gnus-newsgroup-name)))
13334     (while func
13335       (and (symbolp (car func))
13336            (fboundp (car func))
13337            (setq score-files 
13338                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13339       (setq func (cdr func)))
13340     (if score-files (gnus-score-headers score-files trace))))
13341
13342 (defun gnus-score-file-name (newsgroup &optional suffix)
13343   "Return the name of a score file for NEWSGROUP."
13344   (let ((suffix (or suffix gnus-score-file-suffix)))
13345     (cond  ((or (null newsgroup)
13346                 (string-equal newsgroup ""))
13347             ;; The global score file is placed at top of the directory.
13348             (expand-file-name 
13349              suffix (or gnus-kill-files-directory "~/News")))
13350            ((gnus-use-long-file-name 'not-score)
13351             ;; Append ".SCORE" to newsgroup name.
13352             (expand-file-name (concat newsgroup "." suffix)
13353                               (or gnus-kill-files-directory "~/News")))
13354            (t
13355             ;; Place "SCORE" under the hierarchical directory.
13356             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13357                                       "/" suffix)
13358                               (or gnus-kill-files-directory "~/News"))))))
13359
13360 (defun gnus-score-search-global-directories (files)
13361   "Scan all global score directories for score files."
13362   ;; Set the variable `gnus-internal-global-score-files' to all
13363   ;; available global score files.
13364   (interactive (list gnus-global-score-files))
13365   (let (out)
13366     (while files
13367       (if (string-match "/$" (car files))
13368           (setq out (nconc (directory-files 
13369                             (car files) t
13370                             (concat (gnus-score-file-regexp) "$"))))
13371         (setq out (cons (car files) out)))
13372       (setq files (cdr files)))
13373     (setq gnus-internal-global-score-files out)))
13374
13375 ;; Allow redefinition of Gnus functions.
13376
13377 (gnus-ems-redefine)
13378
13379 (provide 'gnus)
13380
13381 ;;; gnus.el ends here