*** 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.91"
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   (autoload 'gnus-bug "gnus-msg" nil t)
1665
1666   ;; gnus-vm
1667   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1668   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1669   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1670   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1671   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1672   (autoload 'gnus-yank-article "gnus-vm" nil t)
1673
1674   )
1675
1676 \f
1677
1678 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1679 ;; If you want the cursor to go somewhere else, set these two
1680 ;; functions in some startup hook to whatever you want.
1681 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1682 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1683
1684 ;;; Various macros and substs.
1685
1686 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1687   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1688   (` (let ((GnusStartBufferWindow (selected-window)))
1689        (unwind-protect
1690            (progn
1691              (pop-to-buffer (, buffer))
1692              (,@ forms))
1693          (select-window GnusStartBufferWindow)))))
1694
1695 (defmacro gnus-gethash (string hashtable)
1696   "Get hash value of STRING in HASHTABLE."
1697   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1698   ;;(` (abbrev-expansion (, string) (, hashtable)))
1699   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1700
1701 (defmacro gnus-sethash (string value hashtable)
1702   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1703   ;; We cannot use define-abbrev since it only accepts string as value.
1704   ;; (set (intern string hashtable) value))
1705   (` (set (intern (, string) (, hashtable)) (, value))))
1706
1707 (defsubst gnus-buffer-substring (beg end)
1708   (buffer-substring (match-beginning beg) (match-end end)))
1709
1710 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1711 ;;   function `substring' might cut on a middle of multi-octet
1712 ;;   character.
1713
1714 (defun gnus-truncate-string (str width)
1715   (substring str 0 width))
1716
1717 (defsubst gnus-simplify-subject-re (subject)
1718   "Remove \"Re:\" from subject lines."
1719   (let ((case-fold-search t))
1720     (if (string-match "^re: *" subject)
1721         (substring subject (match-end 0))
1722       subject)))
1723
1724 (defsubst gnus-goto-char (point)
1725   (and point (goto-char point)))
1726
1727 (defmacro gnus-buffer-exists-p (buffer)
1728   (` (and (, buffer)
1729           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1730                    (, buffer)))))
1731
1732 (defmacro gnus-kill-buffer (buffer)
1733   (` (if (gnus-buffer-exists-p (, buffer))
1734          (kill-buffer (, buffer)))))
1735
1736 (defsubst gnus-point-at-bol ()
1737   "Return point at the beginning of line."
1738   (let ((p (point)))
1739     (beginning-of-line)
1740     (prog1
1741         (point)
1742       (goto-char p))))
1743
1744 (defsubst gnus-point-at-eol ()
1745   "Return point at the beginning of line."
1746   (let ((p (point)))
1747     (end-of-line)
1748     (prog1
1749         (point)
1750       (goto-char p))))
1751
1752 ;; Delete the current line (and the next N lines.);
1753 (defmacro gnus-delete-line (&optional n)
1754   (` (delete-region (progn (beginning-of-line) (point))
1755                     (progn (forward-line (, (or n 1))) (point)))))
1756
1757 ;;; Load the compatability functions. 
1758
1759 (require 'gnus-ems)
1760
1761 \f
1762 ;;;
1763 ;;; Gnus Utility Functions
1764 ;;;
1765
1766 (defun gnus-extract-address-components (from)
1767   (let (name address)
1768     ;; First find the address - the thing with the @ in it.  This may
1769     ;; not be accurate in mail addresses, but does the trick most of
1770     ;; the time in news messages.
1771     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1772         (setq address (substring from (match-beginning 0) (match-end 0))))
1773     ;; Then we check whether the "name <address>" format is used.
1774     (and address
1775          (string-match (concat "<" (regexp-quote address) ">") from)
1776          (and (setq name (substring from 0 (1- (match-beginning 0))))
1777               ;; Strip any quotes from the name.
1778               (string-match "\".*\"" name)
1779               (setq name (substring name 1 (1- (match-end 0))))))
1780     ;; If not, then "address (name)" is used.
1781     (or name
1782         (and (string-match "(.+)" from)
1783              (setq name (substring from (1+ (match-beginning 0)) 
1784                                    (1- (match-end 0)))))
1785         (and (string-match "()" from)
1786              (setq name address))
1787         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1788         ;; XOVER might not support folded From headers.
1789         (and (string-match "(.*" from)
1790              (setq name (substring from (1+ (match-beginning 0)) 
1791                                    (match-end 0)))))
1792     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1793     (list (or name from) (or address from))))
1794
1795 (defun gnus-fetch-field (field)
1796   "Return the value of the header FIELD of current article."
1797   (save-excursion
1798     (save-restriction
1799       (let ((case-fold-search t))
1800         (gnus-narrow-to-headers)
1801         (mail-fetch-field field)))))
1802
1803 (defun gnus-goto-colon ()
1804   (beginning-of-line)
1805   (search-forward ":" (gnus-point-at-eol) t))
1806
1807 (defun gnus-narrow-to-headers ()
1808   (widen)
1809   (save-excursion
1810     (narrow-to-region
1811      (goto-char (point-min))
1812      (if (search-forward "\n\n" nil t)
1813          (1- (point))
1814        (point-max)))))
1815
1816 (defun gnus-update-format-specifications ()
1817   (gnus-make-thread-indent-array)
1818   (setq gnus-summary-line-format-spec 
1819         (gnus-parse-format
1820          gnus-summary-line-format gnus-summary-line-format-alist))
1821   (gnus-update-summary-mark-positions)
1822   (setq gnus-summary-dummy-line-format-spec 
1823         (gnus-parse-format gnus-summary-dummy-line-format 
1824                            gnus-summary-dummy-line-format-alist))
1825   (setq gnus-group-line-format-spec
1826         (gnus-parse-format 
1827          gnus-group-line-format 
1828          gnus-group-line-format-alist))
1829   (if (and (string-match "%D" gnus-group-line-format)
1830            (not gnus-description-hashtb)
1831            gnus-read-active-file)
1832       (gnus-read-all-descriptions-files))
1833   (setq gnus-summary-mode-line-format-spec 
1834         (gnus-parse-format gnus-summary-mode-line-format 
1835                            gnus-summary-mode-line-format-alist))
1836   (setq gnus-article-mode-line-format-spec 
1837         (gnus-parse-format gnus-article-mode-line-format 
1838                            gnus-summary-mode-line-format-alist))
1839   (setq gnus-group-mode-line-format-spec 
1840         (gnus-parse-format gnus-group-mode-line-format 
1841                            gnus-group-mode-line-format-alist)))
1842
1843 (defun gnus-update-summary-mark-positions ()
1844   (save-excursion
1845     (let ((gnus-replied-mark 129)
1846           (gnus-score-below-mark 130)
1847           (gnus-score-over-mark 130)
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 newspec 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 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 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     (save-excursion
2475       ;; Remove windows on all known Gnus buffers.
2476       (while buffers
2477         (setq buf (cdr (car buffers)))
2478         (if (symbolp buf)
2479             (setq buf (and (boundp buf) (symbol-value buf))))
2480         (and buf 
2481              (get-buffer-window buf)
2482              (progn
2483                (if first
2484                    (progn
2485                      (pop-to-buffer buf)
2486                      (switch-to-buffer nntp-server-buffer)
2487                      (setq first nil))
2488                  (delete-window (get-buffer-window buf)))))
2489         (setq buffers (cdr buffers)))
2490       ;; Remove windows on *all* summary buffers.
2491       (let ((buffers (buffer-list)))
2492         (while buffers
2493           (if (and (string-match 
2494                     "^\\*Summary" (or (buffer-name (car buffers)) ""))
2495                    (get-buffer-window (car buffers)))
2496               (delete-window (get-buffer-window (car buffers))))
2497           (setq buffers (cdr buffers)))))))
2498                           
2499 (defun gnus-version ()
2500   "Version numbers of this version of Gnus."
2501   (interactive)
2502   (let ((methods gnus-valid-select-methods)
2503         (mess gnus-version)
2504         meth)
2505     ;; Go through all the legal select methods and add their version
2506     ;; numbers to the total version string. Only the backends that are
2507     ;; currently in use will have their message numbers taken into
2508     ;; consideration. 
2509     (while methods
2510       (setq meth (intern (concat (car (car methods)) "-version")))
2511       (and (boundp meth)
2512            (stringp (symbol-value meth))
2513            (setq mess (concat mess "; " (symbol-value meth))))
2514       (setq methods (cdr methods)))
2515     (gnus-message 2 mess)))
2516
2517 (defun gnus-info-find-node ()
2518   "Find Info documentation of Gnus."
2519   (interactive)
2520   ;; Enlarge info window if needed.
2521   (let ((mode major-mode))
2522     (gnus-configure-windows 'info)
2523     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2524
2525 (defun gnus-overload-functions (&optional overloads)
2526   "Overload functions specified by optional argument OVERLOADS.
2527 If nothing is specified, use the variable gnus-overload-functions."
2528   (let ((defs nil)
2529         (overloads (or overloads gnus-overload-functions)))
2530     (while overloads
2531       (setq defs (car overloads))
2532       (setq overloads (cdr overloads))
2533       ;; Load file before overloading function if necessary.  Make
2534       ;; sure we cannot use `require' always.
2535       (and (not (fboundp (car defs)))
2536            (car (cdr (cdr defs)))
2537            (load (car (cdr (cdr defs))) nil 'nomessage))
2538       (fset (car defs) (car (cdr defs))))))
2539
2540 (defun gnus-replace-chars-in-string (string from to)
2541   "Replace characters in STRING from FROM to TO."
2542   (let ((string (substring string 0))   ;Copy string.
2543         (len (length string))
2544         (idx 0))
2545     ;; Replace all occurrences of FROM with TO.
2546     (while (< idx len)
2547       (if (= (aref string idx) from)
2548           (aset string idx to))
2549       (setq idx (1+ idx)))
2550     string))
2551
2552 (defun gnus-days-between (date1 date2)
2553   ;; Return the number of days between date1 and date2.
2554   (- (gnus-day-number date1) (gnus-day-number date2)))
2555
2556 (defun gnus-day-number (date)
2557   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2558                      (timezone-parse-date date))))
2559     (timezone-absolute-from-gregorian 
2560      (nth 1 dat) (nth 2 dat) (car dat))))
2561
2562 ;; Returns a floating point number that says how many seconds have
2563 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2564 (defun gnus-seconds-since-epoch (date)
2565   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2566                         (timezone-parse-date date)))
2567          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2568                         (timezone-parse-time
2569                          (aref (timezone-parse-date date) 3))))
2570          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2571                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2572          (tday (- (timezone-absolute-from-gregorian 
2573                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2574                   (timezone-absolute-from-gregorian 
2575                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2576     (+ (nth 2 ttime)
2577        (* (nth 1 ttime) 60)
2578        (* 1.0 (nth 0 ttime) 60 60)
2579        (* 1.0 tday 60 60 24))))
2580
2581 (defun gnus-file-newer-than (file date)
2582   (let ((fdate (nth 5 (file-attributes file))))
2583     (or (> (car fdate) (car date))
2584         (and (= (car fdate) (car date))
2585              (> (nth 1 fdate) (nth 1 date))))))
2586
2587 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2588 ;; the echo area.
2589 (defun gnus-y-or-n-p (prompt)
2590   (prog1
2591       (y-or-n-p prompt)
2592     (message "")))
2593
2594 (defun gnus-yes-or-no-p (prompt)
2595   (prog1
2596       (yes-or-no-p prompt)
2597     (message "")))
2598
2599 ;; Check whether to use long file names.
2600 (defun gnus-use-long-file-name (symbol)
2601   ;; The variable has to be set...
2602   (and gnus-use-long-file-name
2603        ;; If it isn't a list, then we return t.
2604        (or (not (listp gnus-use-long-file-name))
2605            ;; If it is a list, and the list contains `symbol', we
2606            ;; return nil.  
2607            (not (memq symbol gnus-use-long-file-name)))))
2608
2609 ;; I suspect there's a better way, but I haven't taken the time to do
2610 ;; it yet. -erik selberg@cs.washington.edu
2611 (defun gnus-dd-mmm (messy-date)
2612   "Return a string like DD-MMM from a big messy string"
2613   (let ((datevec (timezone-parse-date messy-date)))
2614     (format "%2s-%s"
2615             (or (aref datevec 2) "??")
2616             (capitalize
2617              (or (car 
2618                   (nth (1- (string-to-number (aref datevec 1)))
2619                        timezone-months-assoc))
2620                  "???")))))
2621
2622 ;; Make a hash table (default and minimum size is 255).
2623 ;; Optional argument HASHSIZE specifies the table size.
2624 (defun gnus-make-hashtable (&optional hashsize)
2625   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2626
2627 ;; Make a number that is suitable for hashing; bigger than MIN and one
2628 ;; less than 2^x.
2629 (defun gnus-create-hash-size (min)
2630   (let ((i 1))
2631     (while (< i min)
2632       (setq i (* 2 i)))
2633     (1- i)))
2634
2635 ;; Show message if message has a lower level than `gnus-verbose'. 
2636 ;; Guide-line for numbers:
2637 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2638 ;; for things that take a long time, 7 - not very important messages
2639 ;; on stuff, 9 - messages inside loops.
2640 (defun gnus-message (level &rest args)
2641   (if (<= level gnus-verbose)
2642       (apply 'message args)
2643     ;; We have to do this format thingie here even if the result isn't
2644     ;; shown - the return value has to be the same as the return value
2645     ;; from `message'.
2646     (apply 'format args)))
2647
2648 ;; Generate a unique new group name.
2649 (defun gnus-generate-new-group-name (leaf)
2650   (let ((name leaf)
2651         (num 0))
2652     (while (gnus-gethash name gnus-newsrc-hashtb)
2653       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2654     name))
2655
2656 (defun gnus-find-file-noselect (file &optional force)
2657   "Does vaguely the same as find-file-noselect. No hooks are run."
2658   (let (buf insert)
2659     (if (setq buf (get-file-buffer file))
2660         (setq insert force)
2661       (setq buf (create-file-buffer file))
2662       (setq insert t))
2663     (if (not insert)
2664         buf
2665       (save-excursion
2666         (set-buffer buf)
2667         (erase-buffer)
2668         (and (file-readable-p file)
2669              (insert-file-contents file))
2670         (set-visited-file-name file)
2671         (set-buffer-modified-p nil)
2672         (current-buffer)))))
2673
2674 ;;; List and range functions
2675
2676 (defun gnus-last-element (list)
2677   "Return last element of LIST."
2678   (while (cdr list)
2679     (setq list (cdr list)))
2680   (car list))
2681
2682 (defun gnus-copy-sequence (list)
2683   "Do a complete, total copy of a list."
2684   (if (and (consp list) (not (consp (cdr list))))
2685       (cons (car list) (cdr list))
2686     (mapcar (lambda (elem) (if (consp elem) 
2687                                (if (consp (cdr elem))
2688                                    (gnus-copy-sequence elem)
2689                                  (cons (car elem) (cdr elem)))
2690                              elem))
2691             list)))
2692
2693 (defun gnus-set-difference (list1 list2)
2694   "Return a list of elements of LIST1 that do not appear in LIST2."
2695   (let ((list1 (copy-sequence list1)))
2696     (while list2
2697       (setq list1 (delq (car list2) list1))
2698       (setq list2 (cdr list2)))
2699     list1))
2700
2701 (defun gnus-sorted-complement (list1 list2)
2702   "Return a list of elements of LIST1 that do not appear in LIST2.
2703 Both lists have to be sorted over <."
2704   (let (out)
2705     (if (or (null list1) (null list2))
2706         (or list1 list2)
2707       (while (and list1 list2)
2708         (cond ((= (car list1) (car list2))
2709                (setq list1 (cdr list1)
2710                      list2 (cdr list2)))
2711               ((< (car list1) (car list2))
2712                (setq out (cons (car list1) out))
2713                (setq list1 (cdr list1)))
2714               (t
2715                (setq out (cons (car list2) out))
2716                (setq list2 (cdr list2)))))
2717       (nconc (nreverse out) (or list1 list2)))))
2718
2719 (defun gnus-intersection (list1 list2)      
2720   (let ((result nil))
2721     (while list2
2722       (if (memq (car list2) list1)
2723           (setq result (cons (car list2) result)))
2724       (setq list2 (cdr list2)))
2725     result))
2726
2727 (defun gnus-sorted-intersection (list1 list2)
2728   ;; LIST1 and LIST2 have to be sorted over <.
2729   (let (out)
2730     (while (and list1 list2)
2731       (cond ((= (car list1) (car list2))
2732              (setq out (cons (car list1) out)
2733                    list1 (cdr list1)
2734                    list2 (cdr list2)))
2735             ((< (car list1) (car list2))
2736              (setq list1 (cdr list1)))
2737             (t
2738              (setq list2 (cdr list2)))))
2739     (nreverse out)))
2740
2741 (defun gnus-set-sorted-intersection (list1 list2)
2742   ;; LIST1 and LIST2 have to be sorted over <.
2743   ;; This function modifies LIST1.
2744   (let* ((top (cons nil list1))
2745          (prev top))
2746   (while (and list1 list2)
2747     (cond ((= (car list1) (car list2))
2748            (setq prev list1
2749                  list1 (cdr list1)
2750                  list2 (cdr list2)))
2751           ((< (car list1) (car list2))
2752            (setcdr prev (cdr list1))
2753            (setq list1 (cdr list1)))
2754           (t
2755            (setq list2 (cdr list2)))))
2756   (setcdr prev nil)
2757   (cdr top)))
2758
2759 (defun gnus-compress-sequence (numbers &optional always-list)
2760   "Convert list of numbers to a list of ranges or a single range.
2761 If ALWAYS-LIST is non-nil, this function will always release a list of
2762 ranges."
2763   (let* ((first (car numbers))
2764          (last (car numbers))
2765          result)
2766     (if (null numbers)
2767         nil
2768       (if (not (listp (cdr numbers)))
2769           numbers
2770         (while numbers
2771           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2772                 ((= (1+ last) (car numbers)) ;Still in sequence
2773                  (setq last (car numbers)))
2774                 (t                      ;End of one sequence
2775                  (setq result 
2776                        (cons (if (= first last) first
2777                                (cons first last)) result))
2778                  (setq first (car numbers))
2779                  (setq last  (car numbers))))
2780           (setq numbers (cdr numbers)))
2781         (if (and (not always-list) (null result))
2782             (if (= first last) (list first) (cons first last))
2783           (nreverse (cons (if (= first last) first (cons first last))
2784                           result)))))))
2785
2786 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2787 (defun gnus-uncompress-range (ranges)
2788   "Expand a list of ranges into a list of numbers.
2789 RANGES is either a single range on the form `(num . num)' or a list of
2790 these ranges."
2791   (let (first last result)
2792     (cond 
2793      ((null ranges)
2794       nil)
2795      ((not (listp (cdr ranges)))
2796       (setq first (car ranges))
2797       (setq last (cdr ranges))
2798       (while (<= first last)
2799         (setq result (cons first result))
2800         (setq first (1+ first)))
2801       (nreverse result))
2802      (t
2803       (while ranges
2804         (if (atom (car ranges))
2805             (if (numberp (car ranges))
2806                 (setq result (cons (car ranges) result)))
2807           (setq first (car (car ranges)))
2808           (setq last  (cdr (car ranges)))
2809           (while (<= first last)
2810             (setq result (cons first result))
2811             (setq first (1+ first))))
2812         (setq ranges (cdr ranges)))
2813       (nreverse result)))))
2814
2815 (defun gnus-add-to-range (ranges list)
2816   "Return a list of ranges that has all articles from both RANGES and LIST.
2817 Note: LIST has to be sorted over `<'."
2818   (if (not ranges)
2819       (gnus-compress-sequence list t)
2820     (setq list (copy-sequence list))
2821     (or (listp (cdr ranges))
2822         (setq ranges (list ranges)))
2823     (let ((out ranges)
2824           ilist lowest highest temp)
2825       (while (and ranges list)
2826         (setq ilist list)
2827         (setq lowest (or (and (atom (car ranges)) (car ranges))
2828                          (car (car ranges))))
2829         (while (and list (cdr list) (< (car (cdr list)) lowest))
2830           (setq list (cdr list)))
2831         (if (< (car ilist) lowest)
2832             (progn
2833               (setq temp list)
2834               (setq list (cdr list))
2835               (setcdr temp nil)
2836               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2837         (setq highest (or (and (atom (car ranges)) (car ranges))
2838                           (cdr (car ranges))))
2839         (while (and list (<= (car list) highest))
2840           (setq list (cdr list)))
2841         (setq ranges (cdr ranges)))
2842       (if list
2843           (setq out (nconc (gnus-compress-sequence list t) out)))
2844       (setq out (sort out (lambda (r1 r2) 
2845                             (< (or (and (atom r1) r1) (car r1))
2846                                (or (and (atom r2) r2) (car r2))))))
2847       (setq ranges out)
2848       (while ranges
2849         (if (atom (car ranges))
2850             (if (cdr ranges)
2851                 (if (atom (car (cdr ranges)))
2852                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2853                         (progn
2854                           (setcar ranges (cons (car ranges) 
2855                                                (car (cdr ranges))))
2856                           (setcdr ranges (cdr (cdr ranges)))))
2857                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2858                       (progn
2859                         (setcar (car (cdr ranges)) (car ranges))
2860                         (setcar ranges (car (cdr ranges)))
2861                         (setcdr ranges (cdr (cdr ranges)))))))
2862           (if (cdr ranges)
2863               (if (atom (car (cdr ranges)))
2864                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2865                       (progn
2866                         (setcdr (car ranges) (car (cdr ranges)))
2867                         (setcdr ranges (cdr (cdr ranges)))))
2868                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2869                     (progn
2870                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2871                       (setcdr ranges (cdr (cdr ranges))))))))
2872         (setq ranges (cdr ranges)))
2873       out)))
2874
2875 (defun gnus-remove-from-range (ranges list)
2876   "Return a list of ranges that has all articles from LIST removed from RANGES.
2877 Note: LIST has to be sorted over `<'."
2878   ;; !!! This function shouldn't look like this, but I've got a headache.
2879   (gnus-compress-sequence 
2880    (gnus-sorted-complement
2881     (gnus-uncompress-range ranges) list)))
2882
2883 (defun gnus-member-of-range (number ranges)
2884   (if (not (listp (cdr ranges)))
2885       (and (>= number (car ranges)) 
2886            (<= number (cdr ranges)))
2887     (let ((not-stop t))
2888       (while (and ranges 
2889                   (if (numberp (car ranges))
2890                       (>= number (car ranges))
2891                     (>= number (car (car ranges))))
2892                   not-stop)
2893         (if (if (numberp (car ranges))
2894                 (= number (car ranges))
2895               (and (>= number (car (car ranges)))
2896                    (<= number (cdr (car ranges)))))
2897             (setq not-stop nil))
2898         (setq ranges (cdr ranges)))
2899       (not not-stop))))
2900
2901 \f
2902 ;;;
2903 ;;; Gnus group mode
2904 ;;;
2905
2906 (defvar gnus-group-mode-map nil)
2907 (defvar gnus-group-group-map nil)
2908 (defvar gnus-group-mark-map nil)
2909 (defvar gnus-group-list-map nil)
2910 (defvar gnus-group-sub-map nil)
2911 (put 'gnus-group-mode 'mode-class 'special)
2912
2913 (if gnus-group-mode-map
2914     nil
2915   (setq gnus-group-mode-map (make-keymap))
2916   (suppress-keymap gnus-group-mode-map)
2917   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2918   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2919   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2920   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2921   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2922   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2923   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2924   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2925   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2926   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2927   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2928   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2929   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2930   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2931   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2932   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2933   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2934   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2935   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2936   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2937   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2938   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2939   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2940   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2941   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2942   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2943   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2944   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2945   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2946   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2947   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2948   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2949   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2950   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2951   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2952   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2953   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2954   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2955   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2956   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2957   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2958   (define-key gnus-group-mode-map "V" 'gnus-version)
2959   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2960   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2961   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2962   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2963   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2964   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2965   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2966   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2967   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2968   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2969   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
2970   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2971   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2972   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
2973   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
2974
2975   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
2976   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
2977   (define-prefix-command 'gnus-group-mark-map)
2978   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
2979   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
2980   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
2981   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
2982
2983   (define-prefix-command 'gnus-group-group-map)
2984   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
2985   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
2986   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
2987   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
2988   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
2989   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
2990   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
2991   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
2992   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
2993   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
2994   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
2995   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
2996   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
2997   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
2998   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
2999   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3000   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3001   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3002
3003   (define-prefix-command 'gnus-group-list-map)
3004   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3005   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3006   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3007   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3008   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3009   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3010   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3011   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3012   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3013
3014   (define-prefix-command 'gnus-group-sub-map)
3015   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3016   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3017   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3018   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3019   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3020   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3021   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3022   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3023
3024 (defun gnus-group-mode ()
3025   "Major mode for reading news.
3026
3027 All normal editing commands are switched off.
3028 \\<gnus-group-mode-map>
3029 The group buffer lists (some of) the groups available.  For instance,
3030 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3031 lists all zombie groups. 
3032
3033 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3034 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3035
3036 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3037
3038 The following commands are available:
3039
3040 \\{gnus-group-mode-map}"
3041   (interactive)
3042   (if gnus-visual (gnus-group-make-menu-bar))
3043   (kill-all-local-variables)
3044   (setq mode-line-modified "-- ")
3045   (make-local-variable 'mode-line-format)
3046   (setq mode-line-format (copy-sequence mode-line-format))
3047   (and (equal (nth 3 mode-line-format) "   ")
3048        (setcar (nthcdr 3 mode-line-format) ""))
3049   (setq major-mode 'gnus-group-mode)
3050   (setq mode-name "Group")
3051   (gnus-group-set-mode-line)
3052   (setq mode-line-process nil)
3053   (use-local-map gnus-group-mode-map)
3054   (buffer-disable-undo (current-buffer))
3055   (setq truncate-lines t)
3056   (setq buffer-read-only t)
3057   (run-hooks 'gnus-group-mode-hook))
3058
3059 (defun gnus-mouse-pick-group (e)
3060   (interactive "e")
3061   (mouse-set-point e)
3062   (gnus-group-read-group nil))
3063
3064 ;;;###autoload
3065 (defun gnus-no-server (&optional arg)
3066   "Read network news.
3067 If ARG is a positive number, Gnus will use that as the
3068 startup level. If ARG is nil, Gnus will be started at level 2. 
3069 If ARG is non-nil and not a positive number, Gnus will
3070 prompt the user for the name of an NNTP server to use.
3071 As opposed to `gnus', this command will not connect to the local server."
3072   (interactive "P")
3073   (setq gnus-group-use-permanent-levels t)
3074   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3075
3076 (defalias '\(ding\) 'gnus)
3077
3078 ;;;###autoload
3079 (defun gnus (&optional arg dont-connect)
3080   "Read network news.
3081 If ARG is non-nil and a positive number, Gnus will use that as the
3082 startup level. If ARG is non-nil and not a positive number, Gnus will
3083 prompt the user for the name of an NNTP server to use."
3084   (interactive "P")
3085   (if (get-buffer gnus-group-buffer)
3086       (progn
3087         (switch-to-buffer gnus-group-buffer)
3088         (gnus-group-get-new-news))
3089     (gnus-clear-system)
3090     (nnheader-init-server-buffer)
3091     (gnus-read-init-file)
3092     (let ((level (and arg (numberp arg) (> arg 0) arg))
3093           did-connect)
3094       (unwind-protect
3095           (progn
3096             (gnus-group-setup-buffer)
3097             (or dont-connect 
3098                 (setq did-connect
3099                       (gnus-start-news-server (and arg (not level))))))
3100         (if (and (not dont-connect) 
3101                  (not did-connect))
3102             (gnus-group-quit)
3103           (run-hooks 'gnus-startup-hook)
3104           ;; NNTP server is successfully open. 
3105           (gnus-update-format-specifications)
3106           (gnus-summary-make-display-table)
3107           (let ((buffer-read-only nil))
3108             (erase-buffer)
3109             (if (not gnus-inhibit-startup-message)
3110                 (progn
3111                   (gnus-group-startup-message)
3112                   (sit-for 0))))
3113           (gnus-setup-news nil level)
3114           (and gnus-use-dribble-file (gnus-dribble-open))
3115           (gnus-group-list-groups level)
3116           (gnus-configure-windows 'group))))))
3117
3118 (defun gnus-group-startup-message (&optional x y)
3119   "Insert startup message in current buffer."
3120   ;; Insert the message.
3121   (erase-buffer)
3122   (insert
3123    (format "
3124     %s
3125            A newsreader 
3126       for GNU Emacs
3127
3128         Based on GNUS 
3129              written by 
3130      Masanobu UMEDA
3131
3132     Lars Magne 
3133          Ingebrigtsen 
3134       larsi@ifi.uio.no
3135
3136            gnus-version))
3137   ;; And then hack it.
3138   ;; 18 is the longest line.
3139   (indent-rigidly (point-min) (point-max) 
3140                   (/ (max (- (window-width) (or x 28)) 0) 2))
3141   (goto-char (point-min))
3142   ;; +4 is fuzzy factor.
3143   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3144
3145 (defun gnus-group-setup-buffer ()
3146   (or (get-buffer gnus-group-buffer)
3147       (progn
3148         (switch-to-buffer gnus-group-buffer)
3149         (gnus-add-current-to-buffer-list)
3150         (gnus-group-mode)
3151         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3152
3153 (defun gnus-group-list-groups (level &optional unread)
3154   "List newsgroups with level LEVEL or lower that have unread articles.
3155 Default is all subscribed groups.
3156 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3157   (interactive (list (and current-prefix-arg
3158                           (prefix-numeric-value current-prefix-arg))))
3159   (if gnus-group-use-permanent-levels
3160       (progn
3161         (setq gnus-group-default-list-level 
3162               (or level gnus-group-default-list-level))
3163         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3164     (setq level (or level gnus-group-default-list-level 
3165                     gnus-level-subscribed)))
3166   (gnus-group-setup-buffer)     ;May call from out of group buffer
3167   (let ((case-fold-search nil)
3168         (group (gnus-group-group-name)))
3169     (funcall gnus-group-prepare-function level unread nil)
3170     (if (zerop (buffer-size))
3171         (gnus-message 5 gnus-no-groups-message)
3172       (goto-char (point-min))
3173       (if (not group)
3174           ;; Go to the first group with unread articles.
3175           (gnus-group-search-forward nil nil nil t)
3176         ;; Find the right group to put point on. If the current group
3177         ;; has disapeared in the new listing, try to find the next
3178         ;; one. If no next one can be found, just leave point at the
3179         ;; first newsgroup in the buffer.
3180         (if (not (gnus-goto-char
3181                   (text-property-any (point-min) (point-max) 
3182                                      'gnus-group (intern group))))
3183             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3184               (while (and newsrc
3185                           (not (gnus-goto-char 
3186                                 (text-property-any 
3187                                  (point-min) (point-max) 'gnus-group 
3188                                  (intern (car (car newsrc)))))))
3189                 (setq newsrc (cdr newsrc)))
3190               (or newsrc (progn (goto-char (point-max))
3191                                 (forward-line -1))))))
3192       ;; Adjust cursor point.
3193       (gnus-group-position-cursor))))
3194
3195 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3196   "List all newsgroups with unread articles of level LEVEL or lower.
3197 If ALL is non-nil, list groups that have no unread articles.
3198 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3199 If REGEXP, only list groups matching REGEXP."
3200   (set-buffer gnus-group-buffer)
3201   (let ((buffer-read-only nil)
3202         (newsrc (cdr gnus-newsrc-alist))
3203         (lowest (or lowest 1))
3204         info clevel unread group)
3205     (erase-buffer)
3206     (if (< lowest gnus-level-zombie)
3207         ;; List living groups.
3208         (while newsrc
3209           (setq info (car newsrc)
3210                 group (car info)
3211                 newsrc (cdr newsrc)
3212                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3213           (and unread ; This group might be bogus
3214                (or (not regexp)
3215                    (string-match regexp group))
3216                (<= (setq clevel (car (cdr info))) level) 
3217                (>= clevel lowest)
3218                (or all            ; We list all groups?
3219                    (eq unread t)  ; We list unactivated groups
3220                    (> unread 0)   ; We list groups with unread articles
3221                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3222                (gnus-group-insert-group-line 
3223                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3224
3225     ;; List dead groups.
3226     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3227          (gnus-group-prepare-flat-list-dead 
3228           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3229           gnus-level-zombie ?Z
3230           regexp))
3231     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3232          (gnus-group-prepare-flat-list-dead 
3233           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3234           gnus-level-killed ?K regexp))
3235
3236     (gnus-group-set-mode-line)
3237     (setq gnus-have-all-newsgroups all)
3238     (run-hooks 'gnus-group-prepare-hook)))
3239
3240 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3241   ;; List zombies and killed lists somehwat faster, which was
3242   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3243   ;; this by ignoring the group format specification altogether.
3244   (let (group beg)
3245     (while groups
3246       (setq group (car groups)
3247             groups (cdr groups))
3248       (if (or (not regexp)
3249               (string-match regexp group))
3250           (progn
3251             (setq beg (point))
3252             (insert (format " %c     *: %s\n" mark group))
3253             (add-text-properties 
3254              beg (1+ beg) 
3255              (list 'gnus-group (intern group)
3256                    'gnus-unread t
3257                    'gnus-level level)))))))
3258
3259 (defun gnus-group-real-name (group)
3260   "Find the real name of a foreign newsgroup."
3261   (if (string-match ":[^:]+$" group)
3262       (substring group (1+ (match-beginning 0)))
3263     group))
3264
3265 (defun gnus-group-prefixed-name (group method)
3266   "Return the whole name from GROUP and METHOD."
3267   (and (stringp method) (setq method (gnus-server-to-method method)))
3268   (concat (format "%s" (car method))
3269           (if (and 
3270                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3271                (not (string= (nth 1 method) "")))
3272               (concat "+" (nth 1 method)))
3273           ":" group))
3274
3275 (defun gnus-group-real-prefix (group)
3276   "Return the prefix of the current group name."
3277   (if (string-match "^[^:]+:" group)
3278       (substring group 0 (match-end 0))
3279     ""))
3280
3281 (defun gnus-group-method-name (group)
3282   "Return the method used for selecting GROUP."
3283   (let ((prefix (gnus-group-real-prefix group)))
3284     (if (equal prefix "")
3285         gnus-select-method
3286       (if (string-match "^[^\\+]+\\+" prefix)
3287           (list (intern (substring prefix 0 (1- (match-end 0))))
3288                 (substring prefix (match-end 0) (1- (length prefix))))
3289         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3290
3291 (defun gnus-group-foreign-p (group)
3292   "Return nil if GROUP is native, non-nil if it is foreign."
3293   (string-match ":" group))
3294
3295 (defun gnus-group-set-info (info &optional method-only-group part)
3296   (let* ((entry (gnus-gethash
3297                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3298          (part-info info)
3299          (info (if method-only-group (nth 2 entry) info)))
3300     (if (not method-only-group)
3301         ()
3302       (or entry
3303           (error "Trying to change non-existent group %s" method-only-group))
3304       ;; We have recevied parts of the actual group info - either the
3305       ;; select method or the group parameters.  We first check
3306       ;; whether we have to extend the info, and if so, do that.
3307       (let ((len (length info))
3308             (total (if (eq part 'method) 5 6)))
3309         (and (< len total)
3310              (setcdr (nthcdr (1- len) info)
3311                      (make-list (- total len) nil)))
3312         ;; Then we enter the new info.
3313         (setcar (nthcdr (1- total) info) part-info)))
3314     ;; We uncompress some lists of marked articles.
3315     (let (marked)
3316       (if (not (setq marked (nth 3 info)))
3317           ()
3318         (while marked
3319           (or (eq 'score (car (car marked)))
3320               (eq 'bookmark (car (car marked)))
3321               (eq 'killed (car (car marked)))
3322               (setcdr (car marked) 
3323                       (gnus-uncompress-range (cdr (car marked)))))
3324           (setq marked (cdr marked)))))
3325     (if entry
3326         ()
3327       ;; This is a new group, so we just create it.
3328       (save-excursion
3329         (set-buffer gnus-group-buffer)
3330         (if (nth 4 info)
3331             ;; It's a foreign group...
3332             (gnus-group-make-group 
3333              (gnus-group-real-name (car info))
3334              (prin1-to-string (car (nth 4 info)))
3335              (nth 1 (nth 4 info)))
3336           ;; It's a native group.
3337           (gnus-group-make-group
3338            (car info)
3339            (prin1-to-string (car gnus-select-method))
3340            (nth 1 gnus-select-method)))
3341         (gnus-message 6 "Note: New group created")
3342         (setq entry 
3343               (gnus-gethash (gnus-group-prefixed-name 
3344                              (gnus-group-real-name (car info))
3345                              (or (nth 4 info) gnus-select-method))
3346                             gnus-newsrc-hashtb))))
3347     ;; Whether it was a new group or not, we now have the entry, so we
3348     ;; can do the update.
3349     (if entry
3350         (progn
3351           (setcar (nthcdr 2 entry) info)
3352           (if (and (not (eq (car entry) t)) 
3353                    (gnus-gethash (car info) gnus-active-hashtb))
3354               (let ((marked (nth 3 info)))
3355                 (setcar entry 
3356                         (max 0 (- (length (gnus-list-of-unread-articles 
3357                                            (car info)))
3358                                   (length (cdr (assq 'tick marked)))
3359                                   (length (cdr (assq 'dormant marked)))))))))
3360       (error "No such group: %s" (car info)))))
3361
3362 (defun gnus-group-set-method-info (group select-method)
3363   (gnus-group-set-info select-method group 'method))
3364
3365 (defun gnus-group-set-params-info (group params)
3366   (gnus-group-set-info params group 'params))
3367
3368 (defun gnus-group-update-group-line ()
3369   "This function updates the current line in the newsgroup buffer and
3370 moves the point to the colon."
3371   (let* ((buffer-read-only nil)
3372          (group (gnus-group-group-name))
3373          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3374     (if entry
3375         (gnus-dribble-enter 
3376          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3377                  ")")))
3378     (beginning-of-line)
3379     (delete-region (point) (progn (forward-line 1) (point)))
3380     (gnus-group-insert-group-line-info group)
3381     (forward-line -1)
3382     (gnus-group-position-cursor)))
3383
3384 (defun gnus-group-insert-group-line-info (group)
3385   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3386         active info)
3387     (if entry
3388         (progn
3389           (setq info (nth 2 entry))
3390           (gnus-group-insert-group-line 
3391            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3392       (setq active (gnus-gethash group gnus-active-hashtb))
3393       (gnus-group-insert-group-line 
3394        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3395                    gnus-level-killed)
3396        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3397
3398 (defun gnus-group-insert-group-line (gformat group level marked number method)
3399   (let* ((gformat (or gformat gnus-group-line-format-spec))
3400          (active (gnus-gethash group gnus-active-hashtb))
3401          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3402          (number-of-dormant (length (cdr (assq 'dormant marked))))
3403          (number-of-ticked (length (cdr (assq 'tick marked))))
3404          (number-of-ticked-and-dormant
3405           (+ number-of-ticked number-of-dormant))
3406          (number-of-unread-unticked 
3407           (if (numberp number) (int-to-string (max 0 number))
3408             "*"))
3409          (number-of-read
3410           (if (numberp number)
3411               (max 0 (- number-total number))
3412             "*"))
3413          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3414                            ((<= level gnus-level-unsubscribed) ?U)
3415                            ((= level gnus-level-zombie) ?Z)
3416                            (t ?K)))
3417          (qualified-group (gnus-group-real-name group))
3418          (newsgroup-description 
3419           (if gnus-description-hashtb
3420               (or (gnus-gethash group gnus-description-hashtb) "")
3421             ""))
3422          (moderated (if (member group gnus-moderated-list) ?m ? ))
3423          (moderated-string (if (eq moderated ?m) "(m)" ""))
3424          (method (gnus-server-get-method group method))
3425          (news-server (or (car (cdr method)) ""))
3426          (news-method (or (car method) ""))
3427          (news-method-string 
3428           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3429          (marked (if (and 
3430                       (numberp number) 
3431                       (zerop number)
3432                       (> number-of-ticked 0))
3433                      ?* ? ))
3434          (number (if (eq number t) "*" (+ number number-of-dormant 
3435                                           number-of-ticked)))
3436          (process-marked (if (member qualified-group gnus-group-marked)
3437                              gnus-process-mark ? ))
3438          (buffer-read-only nil)
3439          b)
3440     (beginning-of-line)
3441     (setq b (point))
3442     ;; Insert the text.
3443     (insert (eval gformat))
3444
3445     (add-text-properties 
3446      b (1+ b) (list 'gnus-group (intern group)
3447                     'gnus-unread (if (numberp number)
3448                                      (string-to-int number-of-unread-unticked)
3449                                    t)
3450                     'gnus-marked marked
3451                     'gnus-level level))))
3452
3453 (defun gnus-group-update-group (group &optional visible-only)
3454   "Update newsgroup info of GROUP.
3455 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3456   (save-excursion
3457     (set-buffer gnus-group-buffer)
3458     (let ((buffer-read-only nil)
3459           visible)
3460       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3461         (if entry
3462             (gnus-dribble-enter 
3463              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3464                      ")"))))
3465       ;; Buffer may be narrowed.
3466       (save-restriction
3467         (widen)
3468         ;; Search a line to modify.  If the buffer is large, the search
3469         ;; takes long time.  In most cases, current point is on the line
3470         ;; we are looking for.  So, first of all, check current line. 
3471         (if (or (progn
3472                   (beginning-of-line)
3473                   (eq (get-text-property (point) 'gnus-group)
3474                       (intern group)))
3475                 (progn
3476                   (gnus-goto-char 
3477                    (text-property-any 
3478                     (point-min) (point-max) 'gnus-group (intern group)))))
3479             ;; GROUP is listed in current buffer. So, delete old line.
3480             (progn
3481               (setq visible t)
3482               (beginning-of-line)
3483               (delete-region (point) (progn (forward-line 1) (point))))
3484           ;; No such line in the buffer, find out where it's supposed to
3485           ;; go, and insert it there (or at the end of the buffer).
3486           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3487           (or visible-only
3488               (let ((entry 
3489                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3490                 (while (and entry
3491                             (car entry)
3492                             (not
3493                              (gnus-goto-char
3494                               (text-property-any
3495                                (point-min) (point-max) 
3496                                'gnus-group (intern (car (car entry)))))))
3497                   (setq entry (cdr entry)))
3498                 (or entry (goto-char (point-max)))))))
3499       (if (or visible (not visible-only))
3500           (gnus-group-insert-group-line-info group))
3501       (gnus-group-set-mode-line))))
3502
3503 (defun gnus-group-set-mode-line ()
3504   (if (memq 'group gnus-updated-mode-lines)
3505       (let* ((gformat (or gnus-group-mode-line-format-spec
3506                           (setq gnus-group-mode-line-format-spec
3507                                 (gnus-parse-format 
3508                                  gnus-group-mode-line-format 
3509                                  gnus-group-mode-line-format-alist))))
3510              (news-server (car (cdr gnus-select-method)))
3511              (news-method (car gnus-select-method))
3512              (max-len 60)
3513              (mode-string (eval gformat)))
3514         (setq mode-string (eval gformat))
3515         (if (> (length mode-string) max-len) 
3516             (setq mode-string (substring mode-string 0 (- max-len 4))))
3517         (setq mode-line-buffer-identification mode-string)
3518         (set-buffer-modified-p t))))
3519
3520 (defun gnus-group-group-name ()
3521   "Get the name of the newsgroup on the current line."
3522   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3523     (and group (symbol-name group))))
3524
3525 (defun gnus-group-group-level ()
3526   "Get the level of the newsgroup on the current line."
3527   (get-text-property (gnus-point-at-bol) 'gnus-level))
3528
3529 (defun gnus-group-search-forward (&optional backward all level first-too)
3530   "Find the next newsgroup with unread articles.
3531 If BACKWARD is non-nil, find the previous newsgroup instead.
3532 If ALL is non-nil, just find any newsgroup.
3533 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3534 group exists.
3535 If FIRST-TOO, the current line is also eligible as a target."
3536   (let ((way (if backward -1 1))
3537         (low 10)
3538         (beg (point))
3539         pos found)
3540     (if (and backward (progn (beginning-of-line)) (bobp))
3541         nil
3542       (or first-too (forward-line way))
3543       (while (and 
3544               (not (eobp))
3545               (not (setq 
3546                     found 
3547                     (and (or all
3548                              (and
3549                               (let ((unread 
3550                                      (get-text-property (point) 'gnus-unread)))
3551                                 (or (eq unread t) (and unread (> unread 0))))
3552                               (let ((lev (get-text-property
3553                                           (point) 'gnus-level)))
3554                                 (and lev (<= (get-text-property 
3555                                               (point) 'gnus-level)
3556                                              gnus-level-subscribed)))))
3557                          (or (not level)
3558                              (let ((lev (get-text-property (point) 'gnus-level)))
3559                                (if (and lev (<= lev level))
3560                                    t
3561                                  (if (< lev low)
3562                                      (progn
3563                                        (setq low lev)
3564                                        (setq pos (point))))
3565                                  nil))))))
3566               (zerop (forward-line way)))))
3567     (if found 
3568         (progn (gnus-group-position-cursor) t)
3569       (if pos (goto-char pos) (goto-char beg))
3570       nil)))
3571
3572 ;;; Gnus group mode commands
3573
3574 ;; Group marking.
3575
3576 (defun gnus-group-mark-group (n &optional unmark)
3577   "Mark the current group."
3578   (interactive "p")
3579   (let ((buffer-read-only nil)
3580         group)
3581     (while 
3582         (and (> n 0) 
3583              (setq group (gnus-group-group-name))
3584              (progn
3585                (beginning-of-line)
3586                (forward-char 2)
3587                (delete-char 1)
3588                (if unmark
3589                    (progn
3590                      (insert " ")
3591                      (setq gnus-group-marked (delete group gnus-group-marked)))
3592                  (insert "#")
3593                  (setq gnus-group-marked
3594                        (cons group (delete group gnus-group-marked))))
3595                t)
3596              (zerop (gnus-group-next-group 1)))
3597       (setq n (1- n)))
3598     (gnus-summary-position-cursor)
3599     n))
3600
3601 (defun gnus-group-unmark-group (n)
3602   "Remove the mark from the current group."
3603   (interactive "p")
3604   (gnus-group-mark-group n 'unmark))
3605
3606 (defun gnus-group-mark-region (unmark beg end)
3607   "Mark all groups between point and mark.
3608 If UNMARK, remove the mark instead."
3609   (interactive "P\nr")
3610   (let ((num (count-lines beg end)))
3611     (save-excursion
3612       (goto-char beg)
3613       (- num (gnus-group-mark-group num unmark)))))
3614
3615 (defun gnus-group-remove-mark (group)
3616   (and (gnus-group-goto-group group)
3617        (save-excursion
3618          (gnus-group-mark-group 1 'unmark))))
3619
3620 ;; Return a list of groups to work on.  Take into consideration N (the
3621 ;; prefix) and the list of marked groups.
3622 (defun gnus-group-process-prefix (n)
3623   (cond (n
3624          (setq n (prefix-numeric-value n))
3625          ;; There is a prefix, so we return a list of the N next
3626          ;; groups. 
3627          (let ((way (if (< n 0) -1 1))
3628                (n (abs n))
3629                group groups)
3630            (save-excursion
3631              (while (and (> n 0)
3632                          (setq group (gnus-group-group-name)))
3633                (setq groups (cons group groups))
3634                (setq n (1- n))
3635                (forward-line way)))
3636            (nreverse groups)))
3637         (gnus-group-marked
3638          ;; No prefix, but a list of marked articles.
3639          (reverse gnus-group-marked))
3640         (t
3641          ;; Neither marked articles or a prefix, so we return the
3642          ;; current group.
3643          (let ((group (gnus-group-group-name)))
3644            (and group (list group))))))
3645
3646 ;; Selecting groups.
3647
3648 (defun gnus-group-read-group (all &optional no-article group)
3649   "Read news in this newsgroup.
3650 If argument ALL is non-nil, already read articles become readable.
3651 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3652   (interactive "P")
3653   (let ((group (or group (gnus-group-group-name)))
3654         number active marked entry)
3655     (or group (error "No group on current line"))
3656     (setq marked 
3657           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3658     ;; This group might be a dead group. In that case we have to get
3659     ;; the number of unread articles from `gnus-active-hashtb'.
3660     (if entry
3661         (setq number (car entry))
3662       (if (setq active (gnus-gethash group gnus-active-hashtb))
3663           (setq number (- (1+ (cdr active)) (car active)))))
3664     (gnus-summary-read-group 
3665      group (or all (and (numberp number) 
3666                         (zerop (+ number (length (cdr (assq 'tick marked)))
3667                                   (length (cdr (assq 'dormant marked)))))))
3668      no-article)))
3669
3670 (defun gnus-group-select-group (all)
3671   "Select this newsgroup.
3672 No article is selected automatically.
3673 If argument ALL is non-nil, already read articles become readable."
3674   (interactive "P")
3675   (gnus-group-read-group all t))
3676
3677 ;; Enter a group that is not in the group buffer. Non-nil is returned
3678 ;; if selection was successful.
3679 (defun gnus-group-read-ephemeral-group 
3680   (group method &optional activate quit-config)
3681   (let ((group (if (gnus-group-foreign-p group) group
3682                  (gnus-group-prefixed-name group method))))
3683     (gnus-sethash 
3684      group
3685      (list t nil (list group gnus-level-default-subscribed nil nil 
3686                        (append method
3687                                (list
3688                                 (list 'quit-config 
3689                                       (if quit-config quit-config
3690                                         (cons (current-buffer) 'summary)))))))
3691      gnus-newsrc-hashtb)
3692     (set-buffer gnus-group-buffer)
3693     (or (gnus-server-opened method)
3694         (gnus-open-server method)
3695         (error "Unable to contact server: %s" (gnus-status-message method)))
3696     (if activate (gnus-request-group group))
3697     (condition-case ()
3698         (gnus-group-read-group t t group)
3699       (error nil)
3700       (quit nil))
3701     (not (equal major-mode 'gnus-group-mode))))
3702   
3703 (defun gnus-group-jump-to-group (group)
3704   "Jump to newsgroup GROUP."
3705   (interactive 
3706    (list (completing-read 
3707           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3708
3709   (if (equal group "")
3710       (error "Empty group name"))
3711
3712   (let ((b (text-property-any 
3713             (point-min) (point-max) 'gnus-group (intern group))))
3714     (if b
3715         ;; Either go to the line in the group buffer...
3716         (goto-char b)
3717       ;; ... or insert the line.
3718       (or
3719        (gnus-gethash group gnus-active-hashtb)
3720        (gnus-activate-newsgroup group)
3721        (error "%s error: %s" group (gnus-status-message group)))
3722
3723       (gnus-group-update-group group)
3724       (goto-char (text-property-any 
3725                   (point-min) (point-max) 'gnus-group (intern group)))))
3726   ;; Adjust cursor point.
3727   (gnus-group-position-cursor))
3728
3729 (defun gnus-group-goto-group (group)
3730   "Goto to newsgroup GROUP."
3731   (let ((b (text-property-any (point-min) (point-max) 
3732                               'gnus-group (intern group))))
3733     (and b (goto-char b))))
3734
3735 (defun gnus-group-next-group (n)
3736   "Go to next N'th newsgroup.
3737 If N is negative, search backward instead.
3738 Returns the difference between N and the number of skips actually
3739 done."
3740   (interactive "p")
3741   (gnus-group-next-unread-group n t))
3742
3743 (defun gnus-group-next-unread-group (n &optional all level)
3744   "Go to next N'th unread newsgroup.
3745 If N is negative, search backward instead.
3746 If ALL is non-nil, choose any newsgroup, unread or not.
3747 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3748 such group can be found, the next group with a level higher than
3749 LEVEL.
3750 Returns the difference between N and the number of skips actually
3751 made."
3752   (interactive "p")
3753   (let ((backward (< n 0))
3754         (n (abs n)))
3755     (while (and (> n 0)
3756                 (gnus-group-search-forward 
3757                  backward (or (not gnus-group-goto-unread) all) level))
3758       (setq n (1- n)))
3759     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3760                                (if level " on this level or higher" "")))
3761     n))
3762
3763 (defun gnus-group-prev-group (n)
3764   "Go to previous N'th newsgroup.
3765 Returns the difference between N and the number of skips actually
3766 done."
3767   (interactive "p")
3768   (gnus-group-next-unread-group (- n) t))
3769
3770 (defun gnus-group-prev-unread-group (n)
3771   "Go to previous N'th unread newsgroup.
3772 Returns the difference between N and the number of skips actually
3773 done."  
3774   (interactive "p")
3775   (gnus-group-next-unread-group (- n)))
3776
3777 (defun gnus-group-next-unread-group-same-level (n)
3778   "Go to next N'th unread newsgroup on the same level.
3779 If N is negative, search backward instead.
3780 Returns the difference between N and the number of skips actually
3781 done."
3782   (interactive "p")
3783   (gnus-group-next-unread-group n t (gnus-group-group-level))
3784   (gnus-group-position-cursor))
3785
3786 (defun gnus-group-prev-unread-group-same-level (n)
3787   "Go to next N'th unread newsgroup on the same level.
3788 Returns the difference between N and the number of skips actually
3789 done."
3790   (interactive "p")
3791   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3792   (gnus-group-position-cursor))
3793
3794 (defun gnus-group-best-unread-group (&optional exclude-group)
3795   "Go to the group with the highest level.
3796 If EXCLUDE-GROUP, do not go to that group."
3797   (interactive)
3798   (goto-char (point-min))
3799   (let ((best 100000)
3800         unread best-point)
3801     (while (setq unread (get-text-property (point) 'gnus-unread))
3802       (if (and (numberp unread) (> unread 0))
3803           (progn
3804             (if (and (< (get-text-property (point) 'gnus-level) best)
3805                      (or (not exclude-group)
3806                          (not (equal exclude-group (gnus-group-group-name)))))
3807                 (progn 
3808                   (setq best (get-text-property (point) 'gnus-level))
3809                   (setq best-point (point))))))
3810       (forward-line 1))
3811     (if best-point (goto-char best-point))
3812     (gnus-summary-position-cursor)
3813     (and best-point (gnus-group-group-name))))
3814
3815 (defun gnus-group-first-unread-group ()
3816   "Go to the first group with unread articles."
3817   (interactive)
3818   (goto-char (point-min))
3819   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3820       (gnus-group-next-unread-group 1))
3821   (gnus-group-position-cursor))
3822
3823 (defun gnus-group-enter-server-mode ()
3824   "Jump to the server buffer."
3825   (interactive)
3826   (gnus-server-setup-buffer)
3827   (gnus-configure-windows 'server)
3828   (gnus-server-prepare))
3829
3830 (defun gnus-group-make-group (name method &optional address)
3831   "Add a new newsgroup.
3832 The user will be prompted for a NAME, for a select METHOD, and an
3833 ADDRESS."
3834   (interactive
3835    (cons 
3836     (read-string "Group name: ")
3837     (let ((method
3838            (completing-read 
3839             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3840             nil t)))
3841       (if (assoc method gnus-valid-select-methods)
3842           (list method
3843                 (if (memq 'prompt-address
3844                           (assoc method gnus-valid-select-methods))
3845                     (read-string "Address: ")
3846                   ""))
3847         (list method nil)))))
3848   
3849   (let* ((meth (if address (list (intern method) address) method))
3850          (nname (gnus-group-prefixed-name name meth))
3851          info)
3852     (and (gnus-gethash nname gnus-newsrc-hashtb)
3853          (error "Group %s already exists" nname))
3854     (gnus-group-change-level 
3855      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3856      gnus-level-default-subscribed gnus-level-killed 
3857      (and (gnus-group-group-name)
3858           (gnus-gethash (gnus-group-group-name)
3859                         gnus-newsrc-hashtb))
3860      t)
3861     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3862     (gnus-dribble-enter 
3863      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3864     (gnus-group-insert-group-line-info nname)
3865
3866     (and (gnus-check-backend-function 'request-create-group nname)
3867          (gnus-request-create-group nname))))
3868
3869 (defun gnus-group-edit-group (group &optional part)
3870   "Edit the group on the current line."
3871   (interactive (list (gnus-group-group-name)))
3872   (let ((done-func '(lambda () 
3873                       "Exit editing mode and update the information."
3874                       (interactive)
3875                       (gnus-group-edit-group-done 'part 'group)))
3876         (part (or part 'info))
3877         (winconf (current-window-configuration))
3878         info)
3879     (or group (error "No group on current line"))
3880     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3881         (error "Killed group; can't be edited"))
3882     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3883     (gnus-configure-windows 'edit-group)
3884     (gnus-add-current-to-buffer-list)
3885     (emacs-lisp-mode)
3886     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3887     (use-local-map (copy-keymap emacs-lisp-mode-map))
3888     (local-set-key "\C-c\C-c" done-func)
3889     (make-local-variable 'gnus-prev-winconf)
3890     (setq gnus-prev-winconf winconf)
3891     ;; We modify the func to let it know what part it is editing.
3892     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3893     (setcar (cdr (cdr (nth 4 done-func))) group)
3894     (erase-buffer)
3895     (insert
3896      (cond 
3897       ((eq part 'method)
3898        ";; Type `C-c C-c' after editing the select method.\n\n")
3899       ((eq part 'params)
3900        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3901       ((eq part 'info)
3902        ";; Type `C-c C-c' after editing the group info.\n\n")))
3903     (let ((cinfo (gnus-copy-sequence info))
3904           marked)
3905       (if (not (setq marked (nth 3 cinfo)))
3906           ()
3907         (while marked
3908           (or (eq 'score (car (car marked)))
3909               (eq 'bookmark (car (car marked)))
3910               (eq 'killed (car (car marked)))
3911               (not (numberp (car (cdr (car marked)))))
3912               (setcdr (car marked) 
3913                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3914           (setq marked (cdr marked))))
3915       (insert 
3916        (pp-to-string
3917         (cond ((eq part 'method)
3918                (or (nth 4 info) "native"))
3919               ((eq part 'params)
3920                (nth 5 info))
3921               (t
3922                cinfo)))
3923        "\n"))))
3924
3925 (defun gnus-group-edit-group-method (group)
3926   "Edit the select method of GROUP."
3927   (interactive (list (gnus-group-group-name)))
3928   (gnus-group-edit-group group 'method))
3929
3930 (defun gnus-group-edit-group-parameters (group)
3931   "Edit the group parameters of GROUP."
3932   (interactive (list (gnus-group-group-name)))
3933   (gnus-group-edit-group group 'params))
3934
3935 (defun gnus-group-edit-group-done (part group)
3936   "Get info from buffer, update variables and jump to the group buffer."
3937   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3938   (goto-char (point-min))
3939   (let ((form (read (current-buffer)))
3940         (winconf gnus-prev-winconf))
3941     (if (eq part 'info) 
3942         (gnus-group-set-info form)
3943       (gnus-group-set-info form group part))
3944     (kill-buffer (current-buffer))
3945     (and winconf (set-window-configuration winconf))
3946     (set-buffer gnus-group-buffer)
3947     (gnus-group-update-group (gnus-group-group-name))
3948     (gnus-group-position-cursor)))
3949
3950 (defun gnus-group-make-help-group ()
3951   "Create the (ding) Gnus documentation group."
3952   (interactive)
3953   (let ((path load-path)
3954         name)
3955     (and (gnus-gethash (setq name (gnus-group-prefixed-name
3956                                    "gnus-help" '(nndoc "gnus-help")))
3957                        gnus-newsrc-hashtb)
3958          (error "Documentation group already exists"))
3959     (while (and path
3960                 (not (file-exists-p (concat (file-name-as-directory (car path))
3961                                             "doc.txt"))))
3962       (setq path (cdr path)))
3963     (or path (error "Couldn't find doc group"))
3964     (gnus-group-make-group 
3965      (gnus-group-real-name name)
3966      (list 'nndoc name
3967            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
3968            (list 'nndoc-article-type 'mbox))))
3969   (gnus-group-position-cursor))
3970
3971 (defun gnus-group-make-doc-group (file type)
3972   "Create a group that uses a single file as the source."
3973   (interactive 
3974    (list (read-file-name "File name: ") 
3975          (let ((err "")
3976                found char)
3977            (while (not found)
3978              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
3979              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
3980                                ((= char ?b) 'babyl)
3981                                ((= char ?d) 'digest)
3982                                (t (setq err "%c unknown. " char)
3983                                   nil))))
3984            found)))
3985   (let* ((file (expand-file-name file))
3986          (name (gnus-generate-new-group-name
3987                 (gnus-group-prefixed-name
3988                  (file-name-nondirectory file) '(nndoc "")))))
3989     (gnus-group-make-group 
3990      (gnus-group-real-name name)
3991      (list 'nndoc name
3992            (list 'nndoc-address file)
3993            (list 'nndoc-article-type type)))))
3994
3995 (defun gnus-group-make-archive-group ()
3996   "Create the (ding) Gnus archive group."
3997   (interactive)
3998   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3999                      gnus-newsrc-hashtb)
4000        (error "Archive group already exists"))
4001   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4002   (gnus-group-position-cursor))
4003
4004 (defun gnus-group-make-directory-group (dir)
4005   "Create an nndir group.
4006 The user will be prompted for a directory. The contents of this
4007 directory will be used as a newsgroup. The directory should contain
4008 mail messages or news articles in files that have numeric names."
4009   (interactive
4010    (list (read-file-name "Create group from directory: ")))
4011   (or (file-exists-p dir) (error "No such directory"))
4012   (or (file-directory-p dir) (error "Not a directory"))
4013   (gnus-group-make-group dir "nndir" dir)
4014   (gnus-group-position-cursor))
4015
4016 (defun gnus-group-make-kiboze-group (group address scores)
4017   "Create an nnkiboze group.
4018 The user will be prompted for a name, a regexp to match groups, and
4019 score file entries for articles to include in the group."
4020   (interactive
4021    (list
4022     (read-string "nnkiboze group name: ")
4023     (read-string "Source groups (regexp): ")
4024     (let ((headers (mapcar (lambda (group) (list group))
4025                            '("subject" "from" "number" "date" "message-id"
4026                              "references" "chars" "lines" "xref")))
4027           scores header regexp regexps)
4028       (while (not (equal "" (setq header (completing-read 
4029                                           "Match on header: " headers nil t))))
4030         (setq regexps nil)
4031         (while (not (equal "" (setq regexp (read-string 
4032                                             (format "Match on %s (string): "
4033                                                     header)))))
4034           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4035         (setq scores (cons (cons header regexps) scores)))
4036       scores)))
4037   (gnus-group-make-group group "nnkiboze" address)
4038   (save-excursion
4039     (gnus-set-work-buffer)
4040     (let (emacs-lisp-mode-hook)
4041       (pp scores (current-buffer)))
4042     (write-region (point-min) (point-max) 
4043                   (concat (or gnus-kill-files-directory "~/News")
4044                           "nnkiboze:" group "." gnus-score-file-suffix)))
4045   (gnus-group-position-cursor))
4046
4047 (defun gnus-group-add-to-virtual (n vgroup)
4048   "Add the current group to a virtual group."
4049   (interactive
4050    (list current-prefix-arg
4051          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4052                           "nnvirtual:")))
4053   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4054       (error "%s is not an nnvirtual group" vgroup))
4055   (let* ((groups (gnus-group-process-prefix n))
4056          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4057     (setcar (cdr method)
4058             (concat 
4059              (nth 1 method) "\\|"
4060              (mapconcat 
4061               (lambda (s) 
4062                 (gnus-group-remove-mark s)
4063                 (concat "\\(^" (regexp-quote s) "$\\)"))
4064               groups "\\|"))))
4065   (gnus-group-position-cursor))
4066
4067 (defun gnus-group-make-empty-virtual (group)
4068   "Create a new, fresh, empty virtual group."
4069   (interactive "sCreate new, empty virtual group: ")
4070   (let* ((method (list 'nnvirtual "^$"))
4071          (pgroup (gnus-group-prefixed-name group method)))
4072     ;; Check whether it exists already.
4073     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4074          (error "Group %s already exists." pgroup))
4075     ;; Subscribe the new group after the group on the current line.
4076     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4077     (gnus-group-update-group pgroup)
4078     (forward-line -1)
4079     (gnus-group-position-cursor)))
4080
4081 (defun gnus-group-enter-directory (dir)
4082   "Enter an ephemeral nneething group."
4083   (interactive "DDirectory to read: ")
4084   (let* ((method (list 'nneething dir))
4085          (leaf (gnus-group-prefixed-name
4086                 (file-name-nondirectory (directory-file-name dir))
4087                 method))
4088          (name (gnus-generate-new-group-name leaf)))
4089     (let ((nneething-read-only t))
4090       (or (gnus-group-read-ephemeral-group 
4091            name method t
4092            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4093                                       'summary 'group)))
4094           (error "Couldn't enter %s" dir)))))
4095
4096 ;; Group sorting commands
4097 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4098
4099 (defun gnus-group-sort-groups ()
4100   "Sort the group buffer using `gnus-group-sort-function'."
4101   (interactive)
4102   (setq gnus-newsrc-alist 
4103         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4104   (gnus-make-hashtable-from-newsrc-alist)
4105   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4106
4107 (defun gnus-group-sort-by-alphabet (info1 info2)
4108   (string< (car info1) (car info2)))
4109
4110 (defun gnus-group-sort-by-unread (info1 info2)
4111   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4112         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4113     (< (or (and (numberp n1) n1) 0)
4114        (or (and (numberp n2) n2) 0))))
4115
4116 (defun gnus-group-sort-by-level (info1 info2)
4117   (< (nth 1 info1) (nth 1 info2)))
4118
4119 ;; Group catching up.
4120
4121 (defun gnus-group-catchup-current (n &optional all)
4122   "Mark all articles not marked as unread in current newsgroup as read.
4123 If prefix argument N is numeric, the ARG next newsgroups will be
4124 caught up. If ALL is non-nil, marked articles will also be marked as
4125 read. Cross references (Xref: header) of articles are ignored.
4126 The difference between N and actual number of newsgroups that were
4127 caught up is returned."
4128   (interactive "P")
4129   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4130                gnus-expert-user
4131                (gnus-y-or-n-p
4132                 (if all
4133                     "Do you really want to mark all articles as read? "
4134                   "Mark all unread articles as read? "))))
4135       n
4136     (let ((groups (gnus-group-process-prefix n))
4137           (ret 0))
4138       (while groups
4139         ;; Virtual groups have to be given special treatment. 
4140         (let ((method (gnus-find-method-for-group (car groups))))
4141           (if (eq 'nnvirtual (car method))
4142               (nnvirtual-catchup-group
4143                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4144         (gnus-group-remove-mark (car groups))
4145         (if (prog1
4146                 (gnus-group-goto-group (car groups))
4147               (gnus-group-catchup (car groups) all))
4148             (gnus-group-update-group-line)
4149           (setq ret (1+ ret)))
4150         (setq groups (cdr groups)))
4151       (gnus-group-next-unread-group 1)
4152       ret)))
4153
4154 (defun gnus-group-catchup-current-all (n)
4155   "Mark all articles in current newsgroup as read.
4156 Cross references (Xref: header) of articles are ignored."
4157   (interactive "P")
4158   (gnus-group-catchup-current n 'all))
4159
4160 (defun gnus-group-catchup (group &optional all)
4161   "Mark all articles in GROUP as read.
4162 If ALL is non-nil, all articles are marked as read.
4163 The return value is the number of articles that were marked as read,
4164 or nil if no action could be taken."
4165   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4166          (num (car entry))
4167          (marked (nth 3 (nth 2 entry))))
4168     (if (not (numberp (car entry)))
4169         (gnus-message 1 "Can't catch up; non-active group")
4170       ;; Do the updating only if the newsgroup isn't killed.
4171       (if (not entry)
4172           ()
4173         (gnus-update-read-articles 
4174          group (and (not all) (append (cdr (assq 'tick marked))
4175                                       (cdr (assq 'dormant marked))))
4176          nil (and (not all) (cdr (assq 'tick marked))))
4177         (and all marked
4178              (setcar (nthcdr 3 (nth 2 entry)) 
4179                      (delq (assq 'dormant marked) 
4180                            (nth 3 (nth 2 entry)))))))
4181     num))
4182
4183 (defun gnus-group-expire-articles (n)
4184   "Expire all expirable articles in the current newsgroup."
4185   (interactive "P")
4186   (let ((groups (gnus-group-process-prefix n))
4187         group)
4188     (or groups (error "No groups to expire"))
4189     (while groups
4190       (setq group (car groups)
4191             groups (cdr groups))
4192       (gnus-group-remove-mark group)
4193       (if (not (gnus-check-backend-function 'request-expire-articles group))
4194           ()
4195         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4196                (expirable (if (memq 'total-expire (nth 5 info))
4197                               (cons nil (gnus-list-of-read-articles group))
4198                             (assq 'expire (nth 3 info)))))
4199           (and expirable 
4200                (setcdr expirable
4201                        (gnus-request-expire-articles 
4202                         (cdr expirable) group))))))))
4203
4204 (defun gnus-group-expire-all-groups ()
4205   "Expire all expirable articles in all newsgroups."
4206   (interactive)
4207   (gnus-message 5 "Expiring...")
4208   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4209                                    (cdr gnus-newsrc-alist))))
4210     (gnus-group-expire-articles nil))
4211   (gnus-message 5 "Expiring...done"))
4212
4213 (defun gnus-group-set-current-level (n level)
4214   "Set the level of the next N groups to LEVEL."
4215   (interactive "P\nnLevel: ")
4216   (or (and (>= level 1) (<= level gnus-level-killed))
4217       (error "Illegal level: %d" level))
4218   (let ((groups (gnus-group-process-prefix n))
4219         group)
4220     (while groups
4221       (setq group (car groups)
4222             groups (cdr groups))
4223       (gnus-group-remove-mark group)
4224       (gnus-message 6 "Changed level of %s from %d to %d" 
4225                     group (gnus-group-group-level) level)
4226       (gnus-group-change-level group level
4227                                (gnus-group-group-level))
4228       (gnus-group-update-group-line)))
4229   (gnus-group-position-cursor))
4230
4231 (defun gnus-group-unsubscribe-current-group (n)
4232   "Toggle subscription of the current group.
4233 If given numerical prefix, toggle the N next groups."
4234   (interactive "P")
4235   (let ((groups (gnus-group-process-prefix n))
4236         group)
4237     (while groups
4238       (setq group (car groups)
4239             groups (cdr groups))
4240       (gnus-group-remove-mark group)
4241       (gnus-group-unsubscribe-group
4242        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4243                  gnus-level-default-unsubscribed
4244                gnus-level-default-subscribed))
4245       (gnus-group-update-group-line))
4246     (gnus-group-next-group 1)))
4247
4248 (defun gnus-group-unsubscribe-group (group &optional level)
4249   "Toggle subscribe from/to unsubscribe GROUP.
4250 New newsgroup is added to .newsrc automatically."
4251   (interactive
4252    (list (completing-read "Group: " gnus-active-hashtb nil 
4253                           gnus-have-read-active-file)))
4254   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4255     (cond (newsrc
4256            ;; Toggle subscription flag.
4257            (gnus-group-change-level 
4258             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4259                                            gnus-level-subscribed) 
4260                                        (1+ gnus-level-subscribed)
4261                                      gnus-level-default-subscribed)))
4262            (gnus-group-update-group group))
4263           ((and (stringp group)
4264                 (or (not gnus-have-read-active-file)
4265                     (gnus-gethash group gnus-active-hashtb)))
4266            ;; Add new newsgroup.
4267            (gnus-group-change-level 
4268             group 
4269             (if level level gnus-level-default-subscribed) 
4270             (or (and (member group gnus-zombie-list) 
4271                      gnus-level-zombie) 
4272                 gnus-level-killed)
4273             (and (gnus-group-group-name)
4274                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4275            (gnus-group-update-group group))
4276           (t (error "No such newsgroup: %s" group)))
4277     (gnus-group-position-cursor)))
4278
4279 (defun gnus-group-transpose-groups (n)
4280   "Move the current newsgroup up N places.
4281 If given a negative prefix, move down instead. The difference between
4282 N and the number of steps taken is returned." 
4283   (interactive "p")
4284   (or (gnus-group-group-name)
4285       (error "No group on current line"))
4286   (gnus-group-kill-group 1)
4287   (prog1
4288       (forward-line (- n))
4289     (gnus-group-yank-group)
4290     (gnus-group-position-cursor)))
4291
4292 (defun gnus-group-kill-all-zombies ()
4293   "Kill all zombie newsgroups."
4294   (interactive)
4295   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4296   (setq gnus-zombie-list nil)
4297   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4298   (goto-char (point-min))
4299   (gnus-group-position-cursor))
4300
4301 (defun gnus-group-kill-region (begin end)
4302   "Kill newsgroups in current region (excluding current point).
4303 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4304   (interactive "r")
4305   (let ((lines
4306          ;; Count lines.
4307          (save-excursion
4308            (count-lines
4309             (progn
4310               (goto-char begin)
4311               (beginning-of-line)
4312               (point))
4313             (progn
4314               (goto-char end)
4315               (beginning-of-line)
4316               (point))))))
4317     (goto-char begin)
4318     (beginning-of-line)                 ;Important when LINES < 1
4319     (gnus-group-kill-group lines)))
4320
4321 (defun gnus-group-kill-group (n)
4322   "The the next N groups.
4323 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4324 However, only groups that were alive can be yanked; already killed 
4325 groups or zombie groups can't be yanked.
4326 The return value is the name of the (last) group that was killed."
4327   (interactive "P")
4328   (let ((buffer-read-only nil)
4329         (groups (gnus-group-process-prefix n))
4330         group entry level)
4331     (while groups
4332       (setq group (car groups)
4333             groups (cdr groups))
4334       (gnus-group-remove-mark group)
4335       (setq level (gnus-group-group-level))
4336       (gnus-delete-line)
4337       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4338           (setq gnus-list-of-killed-groups 
4339                 (cons (cons (car entry) (nth 2 entry)) 
4340                       gnus-list-of-killed-groups)))
4341       (gnus-group-change-level 
4342        (if entry entry group) gnus-level-killed (if entry nil level)))
4343     (gnus-group-position-cursor)
4344     group))
4345
4346 (defun gnus-group-yank-group (&optional arg)
4347   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4348 inserting it before the current newsgroup.  The numeric ARG specifies
4349 how many newsgroups are to be yanked.  The name of the (last)
4350 newsgroup yanked is returned."
4351   (interactive "p")
4352   (if (not arg) (setq arg 1))
4353   (let (info group prev)
4354     (while (>= (setq arg (1- arg)) 0)
4355       (if (not (setq info (car gnus-list-of-killed-groups)))
4356           (error "No more newsgroups to yank"))
4357       (setq group (nth 2 info))
4358       ;; Find which newsgroup to insert this one before - search
4359       ;; backward until something suitable is found. If there are no
4360       ;; other newsgroups in this buffer, just make this newsgroup the
4361       ;; first newsgroup.
4362       (setq prev (gnus-group-group-name))
4363       (gnus-group-change-level 
4364        info (nth 2 info) gnus-level-killed 
4365        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4366        t)
4367       (gnus-group-insert-group-line-info (nth 1 info))
4368       (setq gnus-list-of-killed-groups 
4369             (cdr gnus-list-of-killed-groups)))
4370     (forward-line -1)
4371     (gnus-group-position-cursor)
4372     group))
4373       
4374 (defun gnus-group-list-all-groups (arg)
4375   "List all newsgroups with level ARG or lower.
4376 Default is gnus-level-unsubscribed, which lists all subscribed and most
4377 unsubscribed groups."
4378   (interactive "P")
4379   (setq arg (or arg gnus-level-unsubscribed))
4380   (gnus-group-list-groups arg t))
4381
4382 (defun gnus-group-list-killed ()
4383   "List all killed newsgroups in the group buffer."
4384   (interactive)
4385   (if (not gnus-killed-list)
4386       (gnus-message 6 "No killed groups")
4387     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4388     (goto-char (point-min)))
4389   (gnus-group-position-cursor))
4390
4391 (defun gnus-group-list-zombies ()
4392   "List all zombie newsgroups in the group buffer."
4393   (interactive)
4394   (if (not gnus-zombie-list)
4395       (gnus-message 6 "No zombie groups")
4396     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4397     (goto-char (point-min)))
4398   (gnus-group-position-cursor))
4399
4400 (defun gnus-group-get-new-news (&optional arg)
4401   "Get newly arrived articles.
4402 If ARG is non-nil, it should be a number between one and nine to
4403 specify which levels you are interested in re-scanning."
4404   (interactive "P")
4405   (run-hooks 'gnus-get-new-news-hook)
4406   (let ((level arg))
4407     (if gnus-group-use-permanent-levels
4408         (if level
4409             (setq gnus-group-default-list-level level)
4410           (setq level (or gnus-group-default-list-level 
4411                           gnus-level-subscribed))))
4412     (if (and gnus-read-active-file (not level))
4413         (progn
4414           (gnus-read-active-file)
4415           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4416       (let ((gnus-read-active-file nil)
4417             (gnus-have-read-active-file (not arg)))
4418         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4419     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4420                                 gnus-group-default-list-level
4421                                 gnus-level-subscribed)
4422                             gnus-have-all-newsgroups)))
4423
4424 (defun gnus-group-get-new-news-this-group (n)
4425   "Check for newly arrived news in the current group (and the N-1 next groups).
4426 The difference between N and the number of newsgroup checked is returned.
4427 If N is negative, this group and the N-1 previous groups will be checked."
4428   (interactive "P")
4429   (let* ((groups (gnus-group-process-prefix n))
4430          (ret (if (numberp n) (- n (length groups)) 0))
4431          group)
4432     (while groups
4433       (setq group (car groups)
4434             groups (cdr groups))
4435       (gnus-group-remove-mark group)
4436       (or (gnus-get-new-news-in-group group)
4437           (progn 
4438             (ding) 
4439             (message "%s error: %s" group (gnus-status-message group))
4440             (sit-for 2))))
4441     ;; !!! I don't know why the buffer scrolls forward when updating
4442     ;; the first line in the group buffer, but it does. So we set the
4443     ;; window start forcibly.
4444 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4445     (gnus-group-next-unread-group 1 t)
4446     (gnus-summary-position-cursor)
4447     ret))
4448
4449 (defun gnus-get-new-news-in-group (group)
4450   (and group 
4451        (gnus-activate-newsgroup group)
4452        (progn
4453          (gnus-get-unread-articles-in-group 
4454           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4455           (gnus-gethash group gnus-active-hashtb))
4456          (gnus-group-update-group-line)
4457          t)))
4458
4459 (defun gnus-group-fetch-faq (group)
4460   "Fetch the FAQ for the current group."
4461   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4462   (or group (error "No group name given"))
4463   (let ((file (concat gnus-group-faq-directory group))) 
4464     (if (not (file-exists-p file))
4465         (error "No such file: %s" file)
4466       (find-file file))))
4467   
4468 (defun gnus-group-describe-group (force &optional group)
4469   "Display a description of the current newsgroup."
4470   (interactive (list current-prefix-arg (gnus-group-group-name)))
4471   (and force (setq gnus-description-hashtb nil))
4472   (let ((method (gnus-find-method-for-group group))
4473         desc)
4474     (or group (error "No group name given"))
4475     (and (or (and gnus-description-hashtb
4476                   ;; We check whether this group's method has been
4477                   ;; queried for a description file.  
4478                   (gnus-gethash 
4479                    (gnus-group-prefixed-name "" method) 
4480                    gnus-description-hashtb))
4481              (setq desc (gnus-group-get-description group))
4482              (gnus-read-descriptions-file method))
4483          (message
4484           (or desc (gnus-gethash group gnus-description-hashtb)
4485               "No description available")))))
4486
4487 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4488 (defun gnus-group-describe-all-groups (force)
4489   "Pop up a buffer with descriptions of all newsgroups."
4490   (interactive "P")
4491   (and force (setq gnus-description-hashtb nil))
4492   (if (not (or gnus-description-hashtb
4493                (gnus-read-all-descriptions-files)))
4494       (error "Couldn't request descriptions file"))
4495   (let ((buffer-read-only nil)
4496         b)
4497     (erase-buffer)
4498     (mapatoms
4499      (lambda (group)
4500        (setq b (point))
4501        (insert (format "      *: %-20s %s\n" (symbol-name group)
4502                        (symbol-value group)))
4503        (add-text-properties 
4504         b (1+ b) (list 'gnus-group group
4505                        'gnus-unread t 'gnus-marked nil
4506                        'gnus-level (1+ gnus-level-subscribed))))
4507      gnus-description-hashtb)
4508     (goto-char (point-min))
4509     (gnus-group-position-cursor)))
4510
4511 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4512 (defun gnus-group-apropos (regexp &optional search-description)
4513   "List all newsgroups that have names that match a regexp."
4514   (interactive "sGnus apropos (regexp): ")
4515   (let ((prev "")
4516         (obuf (current-buffer))
4517         groups des)
4518     ;; Go through all newsgroups that are known to Gnus.
4519     (mapatoms 
4520      (lambda (group)
4521        (and (string-match regexp (symbol-name group))
4522             (setq groups (cons (symbol-name group) groups))))
4523      gnus-active-hashtb)
4524     ;; Go through all descriptions that are known to Gnus. 
4525     (if search-description
4526         (mapatoms 
4527          (lambda (group)
4528            (and (string-match regexp (symbol-value group))
4529                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4530                 (setq groups (cons (symbol-name group) groups))))
4531          gnus-description-hashtb))
4532     (if (not groups)
4533         (gnus-message 3 "No groups matched \"%s\"." regexp)
4534       ;; Print out all the groups.
4535       (save-excursion
4536         (pop-to-buffer "*Gnus Help*")
4537         (buffer-disable-undo (current-buffer))
4538         (erase-buffer)
4539         (setq groups (sort groups 'string<))
4540         (while groups
4541           ;; Groups may be entered twice into the list of groups.
4542           (if (not (string= (car groups) prev))
4543               (progn
4544                 (insert (setq prev (car groups)) "\n")
4545                 (if (and gnus-description-hashtb
4546                          (setq des (gnus-gethash (car groups) 
4547                                                  gnus-description-hashtb)))
4548                     (insert "  " des "\n"))))
4549           (setq groups (cdr groups)))
4550         (goto-char (point-min))))
4551     (pop-to-buffer obuf)))
4552
4553 (defun gnus-group-description-apropos (regexp)
4554   "List all newsgroups that have names or descriptions that match a regexp."
4555   (interactive "sGnus description apropos (regexp): ")
4556   (if (not (or gnus-description-hashtb
4557                (gnus-read-all-descriptions-files)))
4558       (error "Couldn't request descriptions file"))
4559   (gnus-group-apropos regexp t))
4560
4561 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4562 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4563   "List all groups with unread articles that match REGEXP.
4564 If the prefix LEVEL is non-nil, it should be a number that says which
4565 level to cut off listing groups. 
4566 If ALL, also list groups with no unread articles.
4567 If LOWEST, don't list groups with level lower than LOWEST."
4568   (interactive "P\nsList newsgroups matching: ")
4569   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4570                            all (or lowest 1) regexp)
4571   (goto-char (point-min))
4572   (gnus-group-position-cursor))
4573
4574 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4575   "List all groups that match REGEXP.
4576 If the prefix LEVEL is non-nil, it should be a number that says which
4577 level to cut off listing groups. 
4578 If LOWEST, don't list groups with level lower than LOWEST."
4579   (interactive "P\nsList newsgroups matching: ")
4580   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4581
4582 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4583 (defun gnus-group-save-newsrc ()
4584   "Save the Gnus startup files."
4585   (interactive)
4586   (gnus-save-newsrc-file))
4587
4588 (defun gnus-group-restart (&optional arg)
4589   "Force Gnus to read the .newsrc file."
4590   (interactive "P")
4591   (gnus-save-newsrc-file)
4592   (gnus-setup-news 'force)
4593   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4594
4595 (defun gnus-group-read-init-file ()
4596   "Read the Gnus elisp init file."
4597   (interactive)
4598   (gnus-read-init-file))
4599
4600 (defun gnus-group-check-bogus-groups (silent)
4601   "Check bogus newsgroups.
4602 If given a prefix, don't ask for confirmation before removing a bogus
4603 group."
4604   (interactive "P")
4605   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4606   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4607
4608 (defun gnus-group-edit-global-kill (article &optional group)
4609   "Edit the global kill file.
4610 If GROUP, edit that local kill file instead."
4611   (interactive "P")
4612   (setq gnus-current-kill-article article)
4613   (gnus-kill-file-edit-file group)
4614   (gnus-message 6
4615    (substitute-command-keys
4616     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4617
4618 (defun gnus-group-edit-local-kill (article group)
4619   "Edit a local kill file."
4620   (interactive (list nil (gnus-group-group-name)))
4621   (gnus-group-edit-global-kill article group))
4622
4623 (defun gnus-group-force-update ()
4624   "Update `.newsrc' file."
4625   (interactive)
4626   (gnus-save-newsrc-file))
4627
4628 (defun gnus-group-suspend ()
4629   "Suspend the current Gnus session.
4630 In fact, cleanup buffers except for group mode buffer.
4631 The hook gnus-suspend-gnus-hook is called before actually suspending."
4632   (interactive)
4633   (run-hooks 'gnus-suspend-gnus-hook)
4634   ;; Kill Gnus buffers except for group mode buffer.
4635   (let ((group-buf (get-buffer gnus-group-buffer)))
4636     ;; Do this on a separate list in case the user does a ^G before we finish
4637     (let ((gnus-buffer-list
4638            (delq group-buf (delq gnus-dribble-buffer
4639                                  (append gnus-buffer-list nil)))))
4640       (while gnus-buffer-list
4641         (gnus-kill-buffer (car gnus-buffer-list))
4642         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4643     (if group-buf
4644         (progn
4645           (setq gnus-buffer-list (list group-buf))
4646           (bury-buffer group-buf)
4647           (delete-windows-on group-buf t)))))
4648
4649 (defun gnus-group-clear-dribble ()
4650   "Clear all information from the dribble buffer."
4651   (interactive)
4652   (gnus-dribble-clear))
4653
4654 (defun gnus-group-exit ()
4655   "Quit reading news after updating .newsrc.eld and .newsrc.
4656 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4657   (interactive)
4658   (if (or noninteractive                ;For gnus-batch-kill
4659           (zerop (buffer-size))         ;No news is good news.
4660           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4661           (not gnus-interactive-exit)   ;Without confirmation
4662           gnus-expert-user
4663           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4664       (progn
4665         (if gnus-use-full-window
4666             (delete-other-windows)
4667           (gnus-remove-some-windows))
4668         (run-hooks 'gnus-exit-gnus-hook)
4669         (gnus-offer-save-summaries)
4670         (gnus-save-newsrc-file)
4671         (gnus-close-backends)
4672         (gnus-clear-system))))
4673
4674 (defun gnus-close-backends ()
4675   ;; Send a close request to all backends that support such a request. 
4676   (let ((methods gnus-valid-select-methods)
4677         func)
4678     (while methods
4679       (if (fboundp (setq func (intern (concat (car (car methods))
4680                                               "-request-close"))))
4681           (funcall func))
4682       (setq methods (cdr methods)))))
4683
4684 (defun gnus-group-quit ()
4685   "Quit reading news without updating .newsrc.eld or .newsrc.
4686 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4687   (interactive)
4688   (if (or noninteractive                ;For gnus-batch-kill
4689           (zerop (buffer-size))
4690           (not (gnus-server-opened gnus-select-method))
4691           gnus-expert-user
4692           (not gnus-current-startup-file)
4693           (gnus-yes-or-no-p
4694            (format "Quit reading news without saving %s? "
4695                    (file-name-nondirectory gnus-current-startup-file))))
4696       (progn
4697         (run-hooks 'gnus-exit-gnus-hook)
4698         (if gnus-use-full-window
4699             (delete-other-windows)
4700           (gnus-remove-some-windows))
4701         (gnus-dribble-save)
4702         (gnus-close-backends)
4703         (gnus-clear-system))))
4704
4705 (defun gnus-offer-save-summaries ()
4706   (let ((buffers (buffer-list)))
4707     (save-excursion
4708       (while buffers
4709         (and 
4710          ;; We look for buffers with "Summary" in the name.
4711          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4712          (progn
4713            (set-buffer (car buffers))
4714            ;; We check that this is, indeed, a summary buffer.
4715            (eq major-mode 'gnus-summary-mode)) 
4716          ;; We ask the user whether she wants to save the info.
4717          (gnus-y-or-n-p
4718                (format "Update summary buffer %s? " (buffer-name)))
4719          ;; We do it by simply exiting.
4720          (gnus-summary-exit))
4721         (setq buffers (cdr buffers))))))
4722
4723 (defun gnus-group-describe-briefly ()
4724   "Give a one line description of the group mode commands."
4725   (interactive)
4726   (gnus-message 6
4727    (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")))
4728
4729 (defun gnus-group-browse-foreign-server (method)
4730   "Browse a foreign news server.
4731 If called interactively, this function will ask for a select method
4732  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4733 If not, METHOD should be a list where the first element is the method
4734 and the second element is the address."
4735   (interactive
4736    (list (let ((how (completing-read 
4737                      "Which backend: "
4738                      (append gnus-valid-select-methods gnus-server-alist)
4739                      nil t "nntp")))
4740            ;; We either got a backend name or a virtual server name.
4741            ;; If the first, we also need an address.
4742            (if (assoc how gnus-valid-select-methods)
4743                (list (intern how)
4744                      ;; Suggested by mapjph@bath.ac.uk.
4745                      (completing-read 
4746                       "Address: " 
4747                       (mapcar (lambda (server) (list server))
4748                               gnus-secondary-servers)))
4749              ;; We got a server name, so we find the method.
4750              (gnus-server-to-method how)))))
4751   (gnus-browse-foreign-server method))
4752
4753 \f
4754 ;;;
4755 ;;; Browse Server Mode
4756 ;;;
4757
4758 (defvar gnus-browse-mode-hook nil)
4759 (defvar gnus-browse-mode-map nil)
4760 (put 'gnus-browse-mode 'mode-class 'special)
4761
4762 (if gnus-browse-mode-map
4763     nil
4764   (setq gnus-browse-mode-map (make-keymap))
4765   (suppress-keymap gnus-browse-mode-map)
4766   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4767   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4768   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4769   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4770   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4771   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4772   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4773   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4774   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4775   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4776   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4777   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4778   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4779   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4780   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4781   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4782   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4783   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4784   )
4785
4786 (defvar gnus-browse-current-method nil)
4787 (defvar gnus-browse-return-buffer nil)
4788
4789 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4790
4791 (defun gnus-browse-foreign-server (method &optional return-buffer)
4792   (setq gnus-browse-current-method method)
4793   (setq gnus-browse-return-buffer return-buffer)
4794   (let ((gnus-select-method method)
4795         groups group)
4796     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4797     (or (gnus-server-opened method)
4798         (gnus-open-server method)
4799         (error "Unable to contact server: %s" (gnus-status-message method)))
4800     (or (gnus-request-list method)
4801         (error "Couldn't request list: %s" (gnus-status-message method)))
4802     (get-buffer-create gnus-browse-buffer)
4803     (gnus-add-current-to-buffer-list)
4804     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4805     (gnus-configure-windows 'browse)
4806     (buffer-disable-undo (current-buffer))
4807     (let ((buffer-read-only nil))
4808       (erase-buffer))
4809     (gnus-browse-mode)
4810     (setq mode-line-buffer-identification
4811           (format
4812            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4813     (save-excursion
4814       (set-buffer nntp-server-buffer)
4815       (let ((cur (current-buffer)))
4816         (goto-char (point-min))
4817         (or (string= gnus-ignored-newsgroups "")
4818             (delete-matching-lines gnus-ignored-newsgroups))
4819         (while (re-search-forward 
4820                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4821           (goto-char (match-end 1))
4822           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4823                                                      (match-end 1))
4824                                    (max 0 (- (1+ (read cur)) (read cur))))
4825                              groups)))))
4826     (setq groups (sort groups 
4827                        (lambda (l1 l2)
4828                          (string< (car l1) (car l2)))))
4829     (let ((buffer-read-only nil))
4830       (while groups
4831         (setq group (car groups))
4832         (insert 
4833          (format "K%7d: %s\n" (cdr group) (car group)))
4834         (setq groups (cdr groups))))
4835     (switch-to-buffer (current-buffer))
4836     (goto-char (point-min))
4837     (gnus-group-position-cursor)))
4838
4839 (defun gnus-browse-mode ()
4840   "Major mode for browsing a foreign server.
4841
4842 All normal editing commands are switched off.
4843
4844 \\<gnus-browse-mode-map>
4845 The only things you can do in this buffer is
4846
4847 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4848 The group will be inserted into the group buffer upon exit from this
4849 buffer.  
4850
4851 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4852
4853 3) `\\[gnus-browse-exit]' to return to the group buffer."
4854   (interactive)
4855   (kill-all-local-variables)
4856   (if gnus-visual (gnus-browse-make-menu-bar))
4857   (setq mode-line-modified "-- ")
4858   (make-local-variable 'mode-line-format)
4859   (setq mode-line-format (copy-sequence mode-line-format))
4860   (and (equal (nth 3 mode-line-format) "   ")
4861        (setcar (nthcdr 3 mode-line-format) ""))
4862   (setq major-mode 'gnus-browse-mode)
4863   (setq mode-name "Browse Server")
4864   (setq mode-line-process nil)
4865   (use-local-map gnus-browse-mode-map)
4866   (buffer-disable-undo (current-buffer))
4867   (setq truncate-lines t)
4868   (setq buffer-read-only t)
4869   (run-hooks 'gnus-browse-mode-hook))
4870
4871 (defun gnus-browse-read-group (&optional no-article)
4872   "Enter the group at the current line."
4873   (interactive)
4874   (let ((group (gnus-browse-group-name)))
4875     (or (gnus-group-read-ephemeral-group 
4876          group gnus-browse-current-method nil
4877          (cons (current-buffer) 'browse))
4878         (error "Couldn't enter %s" group))))
4879
4880 (defun gnus-browse-select-group ()
4881   "Select the current group."
4882   (interactive)
4883   (gnus-browse-read-group 'no))
4884
4885 (defun gnus-browse-next-group (n)
4886   "Go to the next group."
4887   (interactive "p")
4888   (prog1
4889       (forward-line n)
4890     (gnus-group-position-cursor)))
4891
4892 (defun gnus-browse-prev-group (n)
4893   "Go to the next group."
4894   (interactive "p")
4895   (gnus-browse-next-group (- n)))
4896
4897 (defun gnus-browse-unsubscribe-current-group (arg)
4898   "(Un)subscribe to the next ARG groups."
4899   (interactive "p")
4900   (and (eobp)
4901        (error "No group at current line."))
4902   (let ((ward (if (< arg 0) -1 1))
4903         (arg (abs arg)))
4904     (while (and (> arg 0)
4905                 (not (eobp))
4906                 (gnus-browse-unsubscribe-group)
4907                 (zerop (gnus-browse-next-group ward)))
4908       (setq arg (1- arg)))
4909     (gnus-group-position-cursor)
4910     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4911     arg))
4912
4913 (defun gnus-browse-group-name ()
4914   (save-excursion
4915     (beginning-of-line)
4916     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4917         ()
4918       (gnus-group-prefixed-name 
4919        (buffer-substring (match-beginning 1) (match-end 1))
4920        gnus-browse-current-method))))
4921   
4922 (defun gnus-browse-unsubscribe-group ()
4923   (let ((sub nil)
4924         (buffer-read-only nil)
4925         group)
4926     (save-excursion
4927       (beginning-of-line)
4928       (if (= (following-char) ?K) (setq sub t))
4929       (setq group (gnus-browse-group-name))
4930       (beginning-of-line)
4931       (delete-char 1)
4932       (if sub
4933           (progn
4934             (gnus-group-change-level 
4935              (list t group gnus-level-default-subscribed
4936                    nil nil gnus-browse-current-method) 
4937              gnus-level-default-subscribed gnus-level-killed
4938              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4939              t)
4940             (insert ? ))
4941         (gnus-group-change-level 
4942          group gnus-level-killed gnus-level-default-subscribed)
4943         (insert ?K)))
4944     t))
4945
4946 (defun gnus-browse-exit ()
4947   "Quit browsing and return to the group buffer."
4948   (interactive)
4949   (if (eq major-mode 'gnus-browse-mode)
4950       (kill-buffer (current-buffer)))
4951   (if gnus-browse-return-buffer
4952       (gnus-configure-windows 'server)
4953     (gnus-configure-windows 'group)))
4954
4955 (defun gnus-browse-describe-briefly ()
4956   "Give a one line description of the group mode commands."
4957   (interactive)
4958   (gnus-message 6
4959    (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")))
4960       
4961 \f
4962 ;;;
4963 ;;; Gnus summary mode
4964 ;;;
4965
4966 (defvar gnus-summary-mode-map nil)
4967 (defvar gnus-summary-mark-map nil)
4968 (defvar gnus-summary-mscore-map nil)
4969 (defvar gnus-summary-article-map nil)
4970 (defvar gnus-summary-thread-map nil)
4971 (defvar gnus-summary-goto-map nil)
4972 (defvar gnus-summary-exit-map nil)
4973 (defvar gnus-summary-various-map nil)
4974 (defvar gnus-summary-interest-map nil)
4975 (defvar gnus-summary-sort-map nil)
4976 (defvar gnus-summary-backend-map nil)
4977 (defvar gnus-summary-save-map nil)
4978 (defvar gnus-summary-wash-map nil)
4979 (defvar gnus-summary-help-map nil)
4980
4981 (put 'gnus-summary-mode 'mode-class 'special)
4982
4983 (if gnus-summary-mode-map
4984     nil
4985   (setq gnus-summary-mode-map (make-keymap))
4986   (suppress-keymap gnus-summary-mode-map)
4987
4988   ;; Non-orthogonal keys
4989
4990   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4991   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4992   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4993   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4994   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4995   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4996   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4997   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4998   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4999   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5000   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5001   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5002   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5003   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5004   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5005   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5006   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5007   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5008   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5009   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5010   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5011   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5012   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5013   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5014   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5015   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5016   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5017   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5018   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5019   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5020   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5021   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5022   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5023   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5024   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5025   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5026   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5027   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5028   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5029   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5030   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5031   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5032   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5033   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5034   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5035   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5036   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5037   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5038   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5039   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5040   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5041   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5042   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5043   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5044   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5045   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5046   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5047   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5048   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5049   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5050   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5051   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5052   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5053   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5054   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5055   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5056   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5057   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5058   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5059   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5060   (define-key gnus-summary-mode-map "V" 'gnus-version)
5061   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5062   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5063   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5064   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5065   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5066   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5067   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5068   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5069 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5070   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5071   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5072   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5073 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5074   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5075   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5076   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5077   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5078   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5079
5080
5081   ;; Sort of orthogonal keymap
5082   (define-prefix-command 'gnus-summary-mark-map)
5083   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5084   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5085   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5086   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5087   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5088   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5089   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5090   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5091   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5092   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5093   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5094   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5095   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5096   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5097   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5098   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5099   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5100   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5101   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5102   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5103   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5104   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5105   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5106   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5107
5108   (define-prefix-command 'gnus-summary-mscore-map)
5109   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5110   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5111   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5112   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5113   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5114
5115   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5116   
5117   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5118   
5119   (define-prefix-command 'gnus-summary-goto-map)
5120   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5121   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5122   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5123   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5124   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5125   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5126   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5127   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5128   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5129   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5130   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5131   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5132   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5133   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5134
5135
5136   (define-prefix-command 'gnus-summary-thread-map)
5137   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5138   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5139   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5140   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5141   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5142   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5143   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5144   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5145   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5146   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5147   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5148   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5149   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5150   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5151
5152   
5153   (define-prefix-command 'gnus-summary-exit-map)
5154   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5155   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5156   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5157   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5158   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5159   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5160   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5161   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5162   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5163   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5164   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5165
5166
5167   (define-prefix-command 'gnus-summary-article-map)
5168   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5169   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5170   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5171   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5172   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5173   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5174   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5175   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5176   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5177   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5178   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5179   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5180   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5181   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5182   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5183   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5184   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5185   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5186
5187
5188   (define-prefix-command 'gnus-summary-wash-map)
5189   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5190   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5191   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5192   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5193   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5194   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5195   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5196   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5197   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5198   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5199   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5200   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5201
5202   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5203   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5204   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5205   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5206   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5207   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5208
5209
5210   (define-prefix-command 'gnus-summary-help-map)
5211   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5212   (define-key gnus-summary-help-map "v" 'gnus-version)
5213   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5214   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5215   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5216   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5217
5218
5219   (define-prefix-command 'gnus-summary-backend-map)
5220   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5221   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5222   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5223   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5224   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5225   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5226   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5227   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5228   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5229
5230
5231   (define-prefix-command 'gnus-summary-save-map)
5232   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5233   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5234   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5235   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5236   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5237   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5238   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5239   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5240 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5241
5242   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5243   
5244   (define-prefix-command 'gnus-summary-various-map)
5245   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5246   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5247   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5248   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5249   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5250   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5251   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5252   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5253   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5254   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5255   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5256
5257   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5258
5259   (define-prefix-command 'gnus-summary-sort-map)
5260   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5261   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5262   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5263   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5264   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5265   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5266
5267   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5268   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5269   )
5270
5271
5272 \f
5273
5274 (defun gnus-summary-mode ()
5275   "Major mode for reading articles.
5276
5277 All normal editing commands are switched off.
5278 \\<gnus-summary-mode-map>
5279 Each line in this buffer represents one article.  To read an
5280 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5281 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5282 respectively.
5283
5284 You can also post articles and send mail from this buffer.  To 
5285 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5286 of an article, type `\\[gnus-summary-reply]'.
5287
5288 There are approx. one gazillion commands you can execute in this 
5289 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5290
5291 The following commands are available:
5292
5293 \\{gnus-summary-mode-map}"
5294   (interactive)
5295   (if gnus-visual (gnus-summary-make-menu-bar))
5296   (kill-all-local-variables)
5297   (let ((locals gnus-summary-local-variables))
5298     (while locals
5299       (if (consp (car locals))
5300           (progn
5301             (make-local-variable (car (car locals)))
5302             (set (car (car locals)) (eval (cdr (car locals)))))
5303         (make-local-variable (car locals))
5304         (set (car locals) nil))
5305       (setq locals (cdr locals))))
5306   (gnus-make-thread-indent-array)
5307   (gnus-update-format-specifications)
5308   (setq mode-line-modified "-- ")
5309   (make-local-variable 'mode-line-format)
5310   (setq mode-line-format (copy-sequence mode-line-format))
5311   (and (equal (nth 3 mode-line-format) "   ")
5312        (setcar (nthcdr 3 mode-line-format) ""))
5313   (setq major-mode 'gnus-summary-mode)
5314   (setq mode-name "Summary")
5315   (make-local-variable 'minor-mode-alist)
5316   (use-local-map gnus-summary-mode-map)
5317   (buffer-disable-undo (current-buffer))
5318   (setq buffer-read-only t)             ;Disable modification
5319   (setq truncate-lines t)
5320   (setq selective-display t)
5321   (setq selective-display-ellipses t)   ;Display `...'
5322   (setq buffer-display-table gnus-summary-display-table)
5323   (run-hooks 'gnus-summary-mode-hook))
5324
5325 (defun gnus-summary-make-display-table ()
5326   ;; Change the display table.  Odd characters have a tendency to mess
5327   ;; up nicely formatted displays - we make all possible glyphs
5328   ;; display only a single character.
5329
5330   ;; We start from the standard display table, if any.
5331   (setq gnus-summary-display-table 
5332         (or (copy-sequence standard-display-table)
5333             (make-display-table)))
5334   ;; Nix out all the control chars...
5335   (let ((i 32))
5336     (while (>= (setq i (1- i)) 0)
5337       (aset gnus-summary-display-table i [??])))
5338   ;; ... but not newline and cr, of course. (cr is necessary for the
5339   ;; selective display).  
5340   (aset gnus-summary-display-table ?\n nil)
5341   (aset gnus-summary-display-table ?\r nil)
5342   ;; We nix out any glyphs over 126 that are not set already.  
5343   (let ((i 256))
5344     (while (>= (setq i (1- i)) 127)
5345       ;; Only modify if the entry is nil.
5346       (or (aref gnus-summary-display-table i) 
5347           (aset gnus-summary-display-table i [??])))))
5348
5349 (defun gnus-summary-clear-local-variables ()
5350   (let ((locals gnus-summary-local-variables))
5351     (while locals
5352       (if (consp (car locals))
5353           (and (vectorp (car (car locals)))
5354                (set (car (car locals)) nil))
5355         (and (vectorp (car locals))
5356              (set (car locals) nil)))
5357       (setq locals (cdr locals)))))
5358
5359 (defun gnus-mouse-pick-article (e)
5360   (interactive "e")
5361   (mouse-set-point e)
5362   (gnus-summary-next-page nil t))
5363
5364 (defun gnus-summary-setup-buffer (group)
5365   "Initialize summary buffer."
5366   (let ((buffer (concat "*Summary " group "*")))
5367     (if (get-buffer buffer)
5368         (progn
5369           (set-buffer buffer)
5370           (not gnus-newsgroup-begin))
5371       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5372       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5373       (gnus-add-current-to-buffer-list)
5374       (gnus-summary-mode)
5375       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5376       (setq gnus-newsgroup-name group)
5377       t)))
5378
5379 (defun gnus-set-global-variables ()
5380   ;; Set the global equivalents of the summary buffer-local variables
5381   ;; to the latest values they had. These reflect the summary buffer
5382   ;; that was in action when the last article was fetched.
5383   (if (eq major-mode 'gnus-summary-mode) 
5384       (progn
5385         (setq gnus-summary-buffer (current-buffer))
5386         (let ((name gnus-newsgroup-name)
5387               (marked gnus-newsgroup-marked)
5388               (unread gnus-newsgroup-unreads)
5389               (headers gnus-current-headers)
5390               (score-file gnus-current-score-file))
5391           (save-excursion
5392             (set-buffer gnus-group-buffer)
5393             (setq gnus-newsgroup-name name)
5394             (setq gnus-newsgroup-marked marked)
5395             (setq gnus-newsgroup-unreads unread)
5396             (setq gnus-current-headers headers)
5397             (setq gnus-current-score-file score-file))))))
5398
5399 (defun gnus-summary-insert-dummy-line (sformat subject number)
5400   (if (not sformat) 
5401       (setq sformat gnus-summary-dummy-line-format-spec))
5402   (let (b)
5403     (beginning-of-line)
5404     (setq b (point))
5405     (insert (eval sformat))
5406     (add-text-properties
5407      b (1+ b)
5408      (list 'gnus-number number 
5409            'gnus-mark gnus-dummy-mark
5410            'gnus-level 0))))
5411
5412 (defvar gnus-thread-indent-array nil)
5413 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5414 (defun gnus-make-thread-indent-array ()
5415   (let ((n 200))
5416     (if (and gnus-thread-indent-array
5417              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5418         nil
5419       (setq gnus-thread-indent-array (make-vector 201 "")
5420             gnus-thread-indent-array-level gnus-thread-indent-level)
5421       (while (>= n 0)
5422         (aset gnus-thread-indent-array n
5423               (make-string (* n gnus-thread-indent-level) ? ))
5424         (setq n (1- n))))))
5425
5426 (defun gnus-summary-insert-line 
5427   (sformat header level current unread replied expirable subject-or-nil
5428            &optional dummy score)
5429   (or sformat (setq sformat gnus-summary-line-format-spec))
5430   (let* ((indentation (aref gnus-thread-indent-array level))
5431          (lines (header-lines header))
5432          (score (or score gnus-summary-default-score 0))
5433          (score-char
5434           (if (or (null gnus-summary-default-score)
5435                   (<= (abs (- score gnus-summary-default-score))
5436                       gnus-summary-zcore-fuzz)) ? 
5437             (if (< score gnus-summary-default-score)
5438                 gnus-score-below-mark gnus-score-over-mark)))
5439          (replied (if replied gnus-replied-mark ? ))
5440          (from (header-from header))
5441          (name-address (funcall gnus-extract-address-components from))
5442          (address (car (cdr name-address)))
5443          (name (or (car name-address) (car (cdr name-address))))
5444          (subject (header-subject header))
5445          (number (header-number header))
5446          (opening-bracket (if dummy ?\< ?\[))
5447          (closing-bracket (if dummy ?\> ?\]))
5448          (buffer-read-only nil)
5449          (b (progn (beginning-of-line) (point))))
5450     (or (numberp lines) (setq lines 0))
5451     (insert (eval sformat))
5452     (add-text-properties
5453      b (1+ b) (list 'gnus-number number 
5454                     'gnus-mark (or unread gnus-unread-mark)
5455                     'gnus-level level))))
5456
5457 (defun gnus-summary-update-line (&optional dont-update)
5458   ;; Update summary line after change.
5459   (or (not gnus-summary-default-score)
5460       gnus-summary-inhibit-highlight
5461       (let ((gnus-summary-inhibit-highlight t)
5462             (article (gnus-summary-article-number)))
5463         (progn
5464           (or dont-update
5465               (if (and gnus-summary-mark-below
5466                        (< (gnus-summary-article-score)
5467                           gnus-summary-mark-below))
5468                   (and (not (memq article gnus-newsgroup-marked))
5469                        (not (memq article gnus-newsgroup-dormant))
5470                        (memq article gnus-newsgroup-unreads)
5471                        (gnus-summary-mark-article nil gnus-low-score-mark))
5472                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5473                      (gnus-summary-mark-article nil gnus-unread-mark))))
5474           (and gnus-visual
5475                (run-hooks 'gnus-summary-update-hook))))))
5476
5477 (defun gnus-summary-update-lines (&optional beg end)
5478   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5479   (let ((beg (or beg (point-min)))
5480         (end (or end (point-max))))
5481     (save-excursion
5482       (set-buffer gnus-summary-buffer)
5483       (goto-char beg)
5484       (while (and (not (eobp)) (< (point) end))
5485         (gnus-summary-update-line)
5486         (forward-line 1)))))
5487
5488 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5489   ;; Sum up all elements (and sub-elements) in a list.
5490   (let ((number 
5491          (if (listp thread) 
5492              (apply 
5493               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5494            1)))
5495     (if char 
5496         (if (> number 1) gnus-not-empty-thread-mark
5497           gnus-empty-thread-mark)
5498       number)))
5499
5500 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5501   "Start reading news in newsgroup GROUP.
5502 If SHOW-ALL is non-nil, already read articles are also listed.
5503 If NO-ARTICLE is non-nil, no article is selected initially."
5504   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5505   (let* ((new-group (gnus-summary-setup-buffer group))
5506          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5507     (cond 
5508      ((not new-group)
5509       (gnus-set-global-variables)
5510       (gnus-kill-buffer kill-buffer)
5511       (gnus-configure-windows 'summary)
5512       (gnus-set-mode-line 'summary)
5513       (gnus-summary-position-cursor)
5514       (message "")
5515       t)
5516      ((null did-select) 
5517       (and (eq major-mode 'gnus-summary-mode)
5518            (not (equal (current-buffer) kill-buffer))
5519            (progn
5520              (kill-buffer (current-buffer))
5521              (set-buffer gnus-group-buffer)
5522              (gnus-group-next-unread-group 1)))
5523       (message "Can't select group")
5524       nil)
5525      ((eq did-select 'quit)
5526       (and (eq major-mode 'gnus-summary-mode)
5527            (not (equal (current-buffer) kill-buffer))
5528            (kill-buffer (current-buffer)))
5529       (gnus-kill-buffer kill-buffer)
5530       (gnus-configure-windows 'group)
5531       (gnus-group-next-unread-group 1)
5532       (signal 'quit nil))
5533      (t
5534       (gnus-set-global-variables)
5535       ;; Save the active value in effect when the group was entered.
5536       (setq gnus-newsgroup-active 
5537             (gnus-copy-sequence
5538              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5539       ;; You can change the subjects in this hook.
5540       (run-hooks 'gnus-select-group-hook)
5541       ;; Do score processing.
5542       (and gnus-use-scoring (gnus-possibly-score-headers))
5543       ;; Update the format specifiers.
5544       (gnus-update-format-specifications)
5545       ;; Generate the summary buffer.
5546       (gnus-summary-prepare)
5547       (if (zerop (buffer-size))
5548           (cond (gnus-newsgroup-dormant
5549                  (gnus-summary-show-all-dormant))
5550                 ((and gnus-newsgroup-scored show-all)
5551                  (gnus-summary-show-all-expunged))))
5552       ;; Function `gnus-apply-kill-file' must be called in this hook.
5553       (run-hooks 'gnus-apply-kill-hook)
5554       (if (zerop (buffer-size))
5555           (progn
5556             ;; This newsgroup is empty.
5557             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5558             (gnus-message 6 "No unread news")
5559             (gnus-kill-buffer kill-buffer)
5560             nil)
5561         ;;(save-excursion
5562         ;;  (if kill-buffer
5563         ;;      (let ((gnus-summary-buffer kill-buffer))
5564         ;;      (gnus-configure-windows 'group))))
5565         ;; Hide conversation thread subtrees.  We cannot do this in
5566         ;; gnus-summary-prepare-hook since kill processing may not
5567         ;; work with hidden articles.
5568         (and gnus-show-threads
5569              gnus-thread-hide-subtree
5570              (gnus-summary-hide-all-threads))
5571         ;; Show first unread article if requested.
5572         (goto-char (point-min))
5573         (if (and (not no-article)
5574                  gnus-auto-select-first
5575                  (gnus-summary-first-unread-article))
5576             ()
5577           (gnus-configure-windows 'summary))
5578         (gnus-set-mode-line 'summary)
5579         (gnus-summary-position-cursor)
5580         ;; If in async mode, we send some info to the backend.
5581         (and gnus-newsgroup-async
5582              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5583              (gnus-request-asynchronous 
5584               gnus-newsgroup-name
5585               (if (and gnus-asynchronous-article-function
5586                        (fboundp gnus-asynchronous-article-function))
5587                   (funcall gnus-asynchronous-article-function
5588                            gnus-newsgroup-threads)
5589                 gnus-newsgroup-threads)))
5590         (gnus-kill-buffer kill-buffer)
5591         (if (not (get-buffer-window gnus-group-buffer))
5592             ()
5593           ;; gotta use windows, because recenter does wierd stuff if
5594           ;; the current buffer ain't the displayed window.
5595           (let ((owin (selected-window))) 
5596             (select-window (get-buffer-window gnus-group-buffer))
5597             (and (gnus-group-goto-group group)
5598                  (recenter))
5599             (select-window owin))))
5600       t))))
5601
5602 (defun gnus-summary-prepare ()
5603   ;; Generate the summary buffer.
5604   (let ((buffer-read-only nil))
5605     (erase-buffer)
5606     (gnus-summary-prepare-threads 
5607      (if gnus-show-threads
5608          (gnus-gather-threads 
5609           (gnus-sort-threads 
5610            (if (and gnus-summary-expunge-below
5611                     (not gnus-fetch-old-headers))
5612                (gnus-make-threads-and-expunge)
5613              (gnus-make-threads))))
5614        gnus-newsgroup-headers)
5615      0 nil nil t)
5616     ;; Erase header retrieval message.
5617     (gnus-summary-update-lines)
5618     (message "")
5619     ;; Remove the final newline.
5620     ;;(goto-char (point-max))
5621     ;;(delete-char -1)
5622     ;; Call hooks for modifying summary buffer.
5623     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5624     (goto-char (point-min))
5625     (run-hooks 'gnus-summary-prepare-hook)))
5626
5627 (defun gnus-subject-equal (s1 s2)
5628   (cond 
5629    ((numberp gnus-summary-gather-subject-limit)
5630     (string= (if (> (length s1) gnus-summary-gather-subject-limit)
5631                  (substring s1 0 gnus-summary-gather-subject-limit)
5632                s1)
5633              (if (> (length s2) gnus-summary-gather-subject-limit)
5634                  (substring s2 0 gnus-summary-gather-subject-limit)
5635                s2)))
5636    ((eq 'fuzzy gnus-summary-gather-subject-limit)
5637     (string= (gnus-simplify-subject-fuzzy s1)
5638              (gnus-simplify-subject-fuzzy s2)))
5639    (t
5640     (string= s1 s2))))
5641
5642 (defun gnus-gather-threads (threads)
5643   "Gather threads that have lost their roots."
5644   (if (not gnus-summary-make-false-root)
5645       threads 
5646     (let ((hashtb (gnus-make-hashtable 1023))
5647           (prev threads)
5648           (result threads)
5649           subject hthread whole-subject)
5650       (while threads
5651         (setq whole-subject 
5652               (setq subject (header-subject (car (car threads)))))
5653         (if gnus-summary-gather-subject-limit
5654             (or (and (numberp gnus-summary-gather-subject-limit)
5655                      (> (length subject) gnus-summary-gather-subject-limit)
5656                      (setq subject
5657                            (substring subject 0 
5658                                       gnus-summary-gather-subject-limit)))
5659                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5660                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5661           (setq subject (gnus-simplify-subject-re subject)))
5662         (if (setq hthread 
5663                   (gnus-gethash subject hashtb))
5664             (progn
5665               (or (stringp (car (car hthread)))
5666                   (setcar hthread (list whole-subject (car hthread))))
5667               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5668                                            (list (car threads))))
5669               (setcdr prev (cdr threads))
5670               (setq threads prev))
5671           (gnus-sethash subject threads hashtb))
5672         (setq prev threads)
5673         (setq threads (cdr threads)))
5674       result)))
5675
5676 (defun gnus-make-threads ()
5677   ;; This function takes the dependencies already made by 
5678   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5679   ;; through the dependecies in the hash table and finds all the
5680   ;; roots. Roots do not refer back to any valid articles.
5681   (let (roots)
5682     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5683          (gnus-build-old-threads))
5684     (mapatoms
5685      (lambda (refs)
5686        (if (not (car (symbol-value refs)))
5687            (setq roots (append (cdr (symbol-value refs)) roots))
5688          ;; Ok, these refer back to valid articles, but if
5689          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5690          ;; the root has the same subject as its children. The children
5691          ;; that do not are made into roots and removed from the list
5692          ;; of children. 
5693          (or gnus-thread-ignore-subject
5694              (let* ((prev (symbol-value refs))
5695                     (subject (gnus-simplify-subject-re 
5696                               (header-subject (car prev))))
5697                     (headers (cdr prev)))
5698                (while headers
5699                  (if (not (string= subject
5700                                    (gnus-simplify-subject-re 
5701                                     (header-subject (car headers)))))
5702                      (progn
5703                        (setq roots (cons (car headers) roots))
5704                        (setcdr prev (cdr headers)))
5705                    (setq prev headers))
5706                  (setq headers (cdr headers)))))))
5707      gnus-newsgroup-dependencies)
5708     
5709     (mapcar 'gnus-trim-thread
5710             (apply 'append
5711                    (mapcar 'gnus-cut-thread
5712                            (mapcar 'gnus-make-sub-thread roots))))))
5713   
5714 (defun gnus-make-threads-and-expunge ()
5715   ;; This function takes the dependencies already made by 
5716   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5717   ;; through the dependecies in the hash table and finds all the
5718   ;; roots. Roots do not refer back to any valid articles.
5719   (let ((default (or gnus-summary-default-score 0))
5720         (below gnus-summary-expunge-below)
5721         roots article)
5722     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5723          (gnus-build-old-threads))
5724     (mapatoms
5725      (lambda (refs)
5726        (if (not (car (symbol-value refs)))
5727            ;; These articles do not refer back to any other articles -
5728            ;; they are roots.
5729            (let ((headers (cdr (symbol-value refs))))
5730              ;; We weed out the low-scored articles.
5731              (while headers
5732                (if (not (< (or (cdr (assq (header-number (car headers))
5733                                           gnus-newsgroup-scored)) default)
5734                            below))
5735                    ;; It is over.
5736                    (setq roots (cons (car headers) roots))
5737                  ;; It is below, so we mark it as read.
5738                  (setq gnus-newsgroup-unreads
5739                        (delq (header-number (car headers))
5740                              gnus-newsgroup-unreads)))
5741                (setq headers (cdr headers))))
5742          ;; Ok, these refer back to valid articles, but if
5743          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5744          ;; the root has the same subject as its children. The children
5745          ;; that do not are made into roots and removed from the list
5746          ;; of children. 
5747          (or gnus-thread-ignore-subject
5748              (let* ((prev (symbol-value refs))
5749                     (subject (gnus-simplify-subject-re 
5750                               (header-subject (car prev))))
5751                     (headers (cdr prev)))
5752                (while headers
5753                  (if (not (string= subject
5754                                    (gnus-simplify-subject-re 
5755                                     (header-subject (car headers)))))
5756                      (progn
5757                        (if (not (< (or (cdr (assq (header-number (car headers))
5758                                                   gnus-newsgroup-scored))
5759                                        default) below))
5760                            (setq roots (cons (car headers) roots))
5761                          (setq gnus-newsgroup-unreads
5762                                (delq (header-number (car headers))
5763                                      gnus-newsgroup-unreads)))
5764                        (setcdr prev (cdr headers)))
5765                    (setq prev headers))
5766                  (setq headers (cdr headers)))))
5767          ;; If this article is expunged, some of the children might be
5768          ;; roots.  
5769          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5770                                gnus-newsgroup-scored)) default)
5771                 below)
5772              (let* ((prev (symbol-value refs))
5773                     (headers (cdr prev)))
5774                (while headers
5775                  (setq article (header-number (car headers)))
5776                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5777                                  default) below))
5778                      (progn (setq roots (cons (car headers) roots))
5779                             (setq prev headers))
5780                    (setq gnus-newsgroup-unreads 
5781                          (delq article gnus-newsgroup-unreads))
5782                    (setcdr prev (cdr headers)))
5783                  (setq headers (cdr headers))))
5784            ;; It was not expunged, but we look at expunged children.
5785            (let* ((prev (symbol-value refs))
5786                   (headers (cdr prev))
5787                   article)
5788              (while headers
5789                (setq article (header-number (car headers)))
5790                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5791                                default) below))
5792                    (setq prev headers)
5793                  (setq gnus-newsgroup-unreads 
5794                        (delq article gnus-newsgroup-unreads))
5795                  (setcdr prev (cdr headers)))
5796                (setq headers (cdr headers)))))))
5797      gnus-newsgroup-dependencies)
5798
5799     (mapcar 'gnus-trim-thread
5800             (apply 'append
5801                    (mapcar 'gnus-cut-thread
5802                            (mapcar 'gnus-make-sub-thread roots))))))
5803   
5804 (defun gnus-cut-thread (thread)
5805   ;; Remove leaf dormant or ancient articles from THREAD.
5806   (let ((head (car thread))
5807         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5808     (if (and (null tail)
5809              (let ((number (header-number head)))
5810                (or (memq number gnus-newsgroup-ancient)
5811                    (memq number gnus-newsgroup-dormant)
5812                    (and gnus-summary-expunge-below
5813                         (eq gnus-fetch-old-headers 'some)
5814                         (< (or (cdr (assq number gnus-newsgroup-scored))
5815                                gnus-summary-default-score 0)
5816                            gnus-summary-expunge-below)
5817                         (progn
5818                           (setq gnus-newsgroup-unreads
5819                                 (delq number gnus-newsgroup-unreads))
5820                           t)))))
5821         nil
5822       (list (cons head tail)))))
5823
5824 (defun gnus-trim-thread (thread)
5825   ;; Remove root ancient articles with only one child from THREAD.
5826   (if (and (eq gnus-fetch-old-headers 'some)
5827            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5828            (= (length thread) 2))
5829       (gnus-trim-thread (nth 1 thread))
5830     thread))
5831
5832 (defun gnus-make-sub-thread (root)
5833   ;; This function makes a sub-tree for a node in the tree.
5834   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5835                                               gnus-newsgroup-dependencies)))))
5836     (cons root (mapcar 'gnus-make-sub-thread children))))
5837
5838 (defun gnus-build-old-threads ()
5839   ;; Look at all the articles that refer back to old articles, and
5840   ;; fetch the headers for the articles that aren't there. This will
5841   ;; build complete threads - if the roots haven't been expired by the
5842   ;; server, that is.
5843   (let (id heads)
5844     (mapatoms
5845      (lambda (refs)
5846        (if (not (car (symbol-value refs)))
5847            (progn
5848              (setq heads (cdr (symbol-value refs)))
5849              (while heads
5850                (if (not (memq (header-number (car heads))
5851                               gnus-newsgroup-dormant))
5852                    (progn
5853                      (setq id (symbol-name refs))
5854                      (while (and (setq id (gnus-build-get-header id))
5855                                  (not (car (gnus-gethash 
5856                                             id gnus-newsgroup-dependencies)))))
5857                      (setq heads nil))
5858                  (setq heads (cdr heads)))))))
5859      gnus-newsgroup-dependencies)))
5860
5861 (defun gnus-build-get-header (id)
5862   ;; Look through the buffer of NOV lines and find the header to
5863   ;; ID. Enter this line into the dependencies hash table, and return
5864   ;; the id of the parent article (if any).
5865   (let ((deps gnus-newsgroup-dependencies)
5866         found header)
5867     (prog1
5868         (save-excursion
5869           (set-buffer nntp-server-buffer)
5870           (goto-char (point-min))
5871           (while (and (not found) (search-forward id nil t))
5872             (beginning-of-line)
5873             (setq found (looking-at 
5874                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5875                                  (regexp-quote id))))
5876             (or found (beginning-of-line 2)))
5877           (if found
5878               (let (ref)
5879                 (beginning-of-line)
5880                 (and
5881                  (setq header (gnus-nov-parse-line 
5882                                (read (current-buffer)) deps))
5883                  (setq ref (header-references header))
5884                  (string-match "\\(<[^>]+>\\) *$" ref)
5885                  (substring ref (match-beginning 1) (match-end 1))))))
5886       (and header
5887            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5888                  gnus-newsgroup-ancient (cons (header-number header)
5889                                               gnus-newsgroup-ancient))))))
5890
5891 ;; Re-build the thread containing ID.
5892 (defun gnus-rebuild-thread (id)
5893   (let ((dep gnus-newsgroup-dependencies)
5894         (buffer-read-only nil)
5895         parent headers refs thread art)
5896     (while (and id (setq headers
5897                          (car (setq art (gnus-gethash (downcase id) dep)))))
5898       (setq parent art)
5899       (setq id (and (setq refs (header-references headers))
5900                     (string-match "\\(<[^>]+>\\) *$" refs)
5901                     (substring refs (match-beginning 1) (match-end 1)))))
5902     (setq thread (gnus-make-sub-thread (car parent)))
5903     (gnus-rebuild-remove-articles thread)
5904     (let ((beg (point)))
5905       (gnus-summary-prepare-threads (list thread) 0)
5906       (gnus-summary-update-lines beg (point)))))
5907
5908 ;; Delete all lines in the summary buffer that correspond to articles
5909 ;; in this thread.
5910 (defun gnus-rebuild-remove-articles (thread)
5911   (and (gnus-summary-goto-subject (header-number (car thread)))
5912        (gnus-delete-line))
5913   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5914
5915 (defun gnus-sort-threads (threads)
5916   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5917   (let ((fun gnus-thread-sort-functions))
5918     (while fun
5919       (setq threads (sort threads (car fun))
5920             fun (cdr fun))))
5921   threads)
5922
5923 (defun gnus-thread-header (thread)
5924   ;; Return header of first article in THREAD.
5925   (if (consp thread)
5926       (if (stringp (car thread))
5927           (car (car (cdr thread)))
5928         (car thread))
5929     thread))
5930
5931 (defun gnus-thread-sort-by-number (h1 h2)
5932   "Sort threads by root article number."
5933   (let ((h1 (gnus-thread-header h1))
5934         (h2 (gnus-thread-header h2)))
5935     (< (header-number h1) (header-number h2))))
5936
5937 (defun gnus-thread-sort-by-author (h1 h2)
5938   "Sort threads by root author."
5939   (let ((h1 (gnus-thread-header h1))
5940         (h2 (gnus-thread-header h2)))
5941     (string-lessp
5942      (let ((extract (funcall 
5943                      gnus-extract-address-components (header-from h1))))
5944        (or (car extract) (cdr extract)))
5945      (let ((extract (funcall
5946                      gnus-extract-address-components (header-from h2))))
5947        (or (car extract) (cdr extract))))))
5948
5949 (defun gnus-thread-sort-by-subject (h1 h2)
5950   "Sort threads by root subject."
5951   (let ((h1 (gnus-thread-header h1))
5952         (h2 (gnus-thread-header h2)))
5953     (string-lessp
5954      (downcase (gnus-simplify-subject (header-subject h1)))
5955      (downcase (gnus-simplify-subject (header-subject h2))))))
5956
5957 (defun gnus-thread-sort-by-date (h1 h2)
5958   "Sort threads by root article date."
5959   (let ((h1 (gnus-thread-header h1))
5960         (h2 (gnus-thread-header h2)))
5961     (string-lessp
5962      (gnus-sortable-date (header-date h1))
5963      (gnus-sortable-date (header-date h2)))))
5964
5965 (defun gnus-thread-sort-by-score (h1 h2)
5966   "Sort threads by root article score.
5967 Unscored articles will be counted as having a score of zero."
5968   (let ((h1 (gnus-thread-header h1))
5969         (h2 (gnus-thread-header h2)))
5970     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5971           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5972       (> (or (cdr s1) gnus-summary-default-score 0)
5973          (or (cdr s2) gnus-summary-default-score 0)))))
5974
5975 (defun gnus-thread-sort-by-total-score (h1 h2)
5976   "Sort threads by the sum of all scores in the thread.
5977 Unscored articles will be counted as having a score of zero."
5978   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5979
5980 (defun gnus-thread-total-score (thread)
5981   ;;  This function find the total score of THREAD.
5982   (if (consp thread)
5983       (if (stringp (car thread))
5984           (apply gnus-thread-score-function 0
5985                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5986         (gnus-thread-total-score-1 thread))
5987     (gnus-thread-total-score-1 (list thread))))
5988
5989 (defun gnus-thread-total-score-1 (root)
5990   ;; This function find the total score of the thread below ROOT.
5991   (setq root (car root))
5992   (apply gnus-thread-score-function
5993          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5994              gnus-summary-default-score 0)
5995          (mapcar 'gnus-thread-total-score
5996                  (cdr (gnus-gethash (downcase (header-id root))
5997                                     gnus-newsgroup-dependencies)))))
5998
5999 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6000 (defvar gnus-tmp-prev-subject "")
6001 (defvar gnus-tmp-adopt-thread nil)
6002
6003 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
6004 (defun gnus-summary-prepare-threads 
6005   (threads level &optional not-child no-subject cull)
6006   "Prepare summary buffer from THREADS and indentation LEVEL.  
6007 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6008 or a straight list of headers."
6009   (let (thread header number subject clevel)
6010     (while threads
6011       (setq thread (car threads)
6012             threads (cdr threads))
6013       ;; If `thread' is a cons, hierarchical threads are used.  If not,
6014       ;; `thread' is the header.
6015       (if (consp thread)
6016           (setq header (car thread))
6017         (setq header thread)
6018         (and cull
6019              (or (memq (setq number (header-number header))
6020                        gnus-newsgroup-dormant)
6021                  (and gnus-summary-expunge-below
6022                       (< (or (cdr (assq number gnus-newsgroup-scored))
6023                              gnus-summary-default-score 0)
6024                          gnus-summary-expunge-below)))
6025              (progn
6026                (setq header nil)
6027                (setq gnus-newsgroup-unreads 
6028                      (delq number gnus-newsgroup-unreads)))))
6029       (cond 
6030        ((stringp header)
6031         ;; The header is a dummy root.
6032         (cond ((eq gnus-summary-make-false-root 'adopt)
6033                ;; We let the first article adopt the rest.
6034                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6035                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6036                (setq thread (cdr (cdr thread)))
6037                (while thread
6038                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
6039                  (setq thread (cdr thread))))
6040               ((eq gnus-summary-make-false-root 'dummy)
6041                ;; We output a dummy root.
6042                (gnus-summary-insert-dummy-line 
6043                 nil header (header-number (car (car (cdr thread)))))
6044                (setq clevel 1))
6045               ((eq gnus-summary-make-false-root 'empty)
6046                ;; We print the articles with empty subject fields. 
6047                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6048                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6049                (setq thread (cdr (cdr thread)))
6050                (while thread
6051                  (gnus-summary-prepare-threads 
6052                   (list (car thread)) 0 nil
6053                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6054                             (not (string=  
6055                                   (gnus-simplify-subject-re 
6056                                    (header-subject (car (car thread))))
6057                                   (gnus-simplify-subject-re header))))))
6058                  (setq thread (cdr thread))))
6059               (t
6060                ;; We do not make a root for the gathered
6061                ;; sub-threads at all.  
6062                (setq clevel 0)))
6063         ;; Print the sub-threads.
6064         (and (consp thread) (cdr thread)
6065              (gnus-summary-prepare-threads (cdr thread) clevel)))
6066        ;; The header is a real article.
6067        (header
6068         (setq number (header-number header)
6069               subject (header-subject header))
6070         (and gnus-newsgroup-async
6071              (setq gnus-newsgroup-threads
6072                    (cons (cons (header-number header)
6073                                (header-lines header)) gnus-newsgroup-threads)))
6074         (gnus-summary-insert-line
6075          nil header level nil 
6076          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6077                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6078                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6079                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6080                (t gnus-ancient-mark))
6081          (memq number gnus-newsgroup-replied)
6082          (memq number gnus-newsgroup-expirable)
6083          (if no-subject 
6084              gnus-summary-same-subject
6085            (if (or (zerop level)
6086                    (and gnus-thread-ignore-subject
6087                         (not (string= 
6088                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6089                               (gnus-simplify-subject-re subject)))))
6090                subject
6091              gnus-summary-same-subject))
6092          not-child
6093          (cdr (assq number gnus-newsgroup-scored)))
6094         (setq gnus-tmp-prev-subject subject)
6095         ;; Recursively print subthreads.
6096         (and (consp thread) (cdr thread)
6097              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6098
6099 (defun gnus-select-newsgroup (group &optional read-all)
6100   "Select newsgroup GROUP.
6101 If READ-ALL is non-nil, all articles in the group are selected."
6102   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6103          (info (nth 2 entry))
6104          articles)
6105     (gnus-check-news-server
6106      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6107
6108     (or (gnus-server-opened gnus-current-select-method)
6109         (gnus-open-server gnus-current-select-method)
6110         (error "Couldn't open server"))
6111     
6112     (or (and (eq (car entry) t)
6113              (gnus-activate-newsgroup (car info)))
6114         (gnus-request-group group t)
6115         (progn
6116           (kill-buffer (current-buffer))
6117           (error "Couldn't request group %s: %s" 
6118                  group (gnus-status-message group))))
6119
6120     (setq gnus-newsgroup-name group)
6121     (setq gnus-newsgroup-unselected nil)
6122     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6123
6124     (and gnus-asynchronous
6125          (gnus-check-backend-function 
6126           'request-asynchronous gnus-newsgroup-name)
6127          (setq gnus-newsgroup-async
6128                (gnus-request-asynchronous gnus-newsgroup-name)))
6129
6130     (setq articles (gnus-articles-to-read group read-all))
6131
6132     (cond 
6133      ((null articles) 
6134       (gnus-message 3 "Couldn't select newsgroup")
6135       'quit)
6136      ((eq articles 0) nil)
6137      (t
6138       ;; Init the dependencies hash table.
6139       (setq gnus-newsgroup-dependencies 
6140             (gnus-make-hashtable (length articles)))
6141       ;; Retrieve the headers and read them in.
6142       (setq gnus-newsgroup-headers 
6143             (if (eq 'nov (setq gnus-headers-retrieved-by
6144                                (gnus-retrieve-headers 
6145                                 (if (and gnus-fetch-old-headers 
6146                                          (not (eq 1 (car articles))))
6147                                     (cons 1 articles)
6148                                   articles)
6149                                 gnus-newsgroup-name)))
6150                 (progn
6151                   (gnus-get-newsgroup-headers-xover articles))
6152               ;; If we were to fetch old headers, but the backend didn't
6153               ;; support XOVER, then it is possible we fetched one article
6154               ;; that we shouldn't have. If that's the case, we pop it off the
6155               ;; list of headers.
6156               (if (not gnus-fetch-old-headers)
6157                   ()
6158                 (save-excursion
6159                   (set-buffer nntp-server-buffer)
6160                   (goto-char (point-min))
6161                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6162                        (delete-region 
6163                         (point) 
6164                         (search-forward "\n.\n" nil t)))))
6165               (gnus-get-newsgroup-headers)))
6166       ;; Remove canceled articles from the list of unread articles.
6167       (setq gnus-newsgroup-unreads
6168             (gnus-set-sorted-intersection 
6169              gnus-newsgroup-unreads
6170              (mapcar (lambda (headers) (header-number headers))
6171                      gnus-newsgroup-headers)))
6172       ;; Adjust and set lists of article marks.
6173       (and info
6174            (let (marked)
6175              (gnus-adjust-marked-articles info)
6176              (setq gnus-newsgroup-marked 
6177                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6178              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6179              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6180              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6181              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6182              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6183              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6184              (setq gnus-newsgroup-processable nil)))
6185       ;; Check whether auto-expire is to be done in this group.
6186       (setq gnus-newsgroup-auto-expire
6187             (or (and (stringp gnus-auto-expirable-newsgroups)
6188                      (string-match gnus-auto-expirable-newsgroups group))
6189                 (memq 'auto-expire (nth 5 info))))
6190       ;; First and last article in this newsgroup.
6191       (and gnus-newsgroup-headers
6192            (setq gnus-newsgroup-begin 
6193                  (header-number (car gnus-newsgroup-headers)))
6194            (setq gnus-newsgroup-end
6195                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6196       (setq gnus-reffed-article-number -1)
6197       ;; GROUP is successfully selected.
6198       (or gnus-newsgroup-headers t)))))
6199
6200 (defun gnus-articles-to-read (group read-all)
6201   ;; Find out what articles the user wants to read.
6202   (let* ((articles
6203           ;; Select all articles if `read-all' is non-nil, or if all the
6204           ;; unread articles are dormant articles.
6205           (if (or (and read-all (not (numberp read-all)))
6206                   (= (length gnus-newsgroup-unreads) 
6207                      (length gnus-newsgroup-dormant)))
6208               (gnus-uncompress-range 
6209                (gnus-gethash group gnus-active-hashtb))
6210             gnus-newsgroup-unreads))
6211          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6212          (scored (length scored-list))
6213          (number (length articles))
6214          (marked (+ (length gnus-newsgroup-marked)
6215                     (length gnus-newsgroup-dormant)))
6216          (select
6217           (cond 
6218            ((numberp read-all)
6219             read-all)
6220            (t
6221             (condition-case ()
6222                 (cond ((and (or (<= scored marked)
6223                                 (= scored number))
6224                             (numberp gnus-large-newsgroup)
6225                             (> number gnus-large-newsgroup))
6226                        (let ((input
6227                               (read-string
6228                                (format
6229                                 "How many articles from %s (default %d): "
6230                                 gnus-newsgroup-name number))))
6231                          (if (string-match "^[ \t]*$" input)
6232                              number input)))
6233                       ((and (> scored marked) (< scored number))
6234                        (let ((input
6235                               (read-string
6236                                (format 
6237                                 "%s %s (%d scored, %d total): "
6238                                 "How many articles from"
6239                                 group scored number))))
6240                          (if (string-match "^[ \t]*$" input)
6241                              number input)))
6242                       (t number))
6243               (quit nil))))))
6244     (setq select (if (stringp select) (string-to-number select) select))
6245     (if (or (null select) (zerop select))
6246         select
6247       (if (and (not (zerop scored)) (<= (abs select) scored))
6248           (progn
6249             (setq articles (sort scored-list '<))
6250             (setq number (length articles)))
6251         (setq articles (copy-sequence articles)))
6252
6253       (if (< (abs select) number)
6254           (if (< select 0) 
6255               ;; Select the N oldest articles.
6256               (setcdr (nthcdr (1- (abs select)) articles) nil)
6257             ;; Select the N most recent articles.
6258             (setq articles (nthcdr (- number select) articles))))
6259       (setq gnus-newsgroup-unselected
6260             (gnus-sorted-intersection
6261              gnus-newsgroup-unreads
6262              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6263       articles)))
6264
6265 (defun gnus-killed-articles (killed articles)
6266   (let (out)
6267     (while articles
6268       (if (inline (gnus-member-of-range (car articles) killed))
6269           (setq out (cons (car articles) out)))
6270       (setq articles (cdr articles)))
6271     out))
6272
6273 (defun gnus-adjust-marked-articles (info &optional active)
6274   "Remove all marked articles that are no longer legal."
6275   (let ((marked-lists (nth 3 info))
6276         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6277         m prev)
6278     ;; There are many types of marked articles.
6279     (while marked-lists
6280       (setq m (cdr (setq prev (car marked-lists))))
6281       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6282              ;; Make sure that all ticked articles are a subset of the
6283              ;; unread/unselected articles.
6284              (while m
6285                (if (or (memq (car m) gnus-newsgroup-unreads)
6286                        (memq (car m) gnus-newsgroup-unselected))
6287                    (setq prev m)
6288                  (setcdr prev (cdr m)))
6289                (setq m (cdr m))))
6290             ((eq 'score (car prev))
6291              ;; Scored articles should be a subset of
6292              ;; unread/unselected articles. 
6293              (while m
6294                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6295                        (memq (car (car m)) gnus-newsgroup-unreads))
6296                    (setq prev m)
6297                  (setcdr prev (cdr m)))
6298                (setq m (cdr m))))
6299             ((eq 'bookmark (car prev))
6300              ;; Bookmarks should be a subset of active articles.
6301              (while m
6302                (if (< (car (car m)) (car active))
6303                    (setcdr prev (cdr m))
6304                  (setq prev m))
6305                (setq m (cdr m))))
6306             ((eq 'killed (car prev))
6307              ;; Articles that have been through the kill process are
6308              ;; to be a subset of active articles.
6309              (while (and m (< (or (and (numberp (car m)) (car m))
6310                                   (cdr (car m)))
6311                               (car active)))
6312                (setcdr prev (cdr m))
6313                (setq m (cdr m)))
6314              (if (and m (< (or (and (numberp (car m)) (car m))
6315                                (car (car m)))
6316                            (car active))) 
6317                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6318             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6319              ;; The replied and expirable articles have to be articles
6320              ;; that are active. 
6321              (while m
6322                (if (< (car m) (car active))
6323                    (setcdr prev (cdr m))
6324                  (setq prev m))
6325                (setq m (cdr m)))))
6326       (setq marked-lists (cdr marked-lists)))
6327     ;; Remove all lists that are empty.
6328     (setq marked-lists (nth 3 info))
6329     (if marked-lists
6330         (progn
6331           (while (= 1 (length (car marked-lists)))
6332             (setq marked-lists (cdr marked-lists)))
6333           (setq m (cdr (setq prev marked-lists)))
6334           (while m
6335             (if (= 1 (length (car m)))
6336                 (setcdr prev (cdr m))
6337               (setq prev m))
6338             (setq m (cdr m)))
6339           (setcar (nthcdr 3 info) marked-lists)))
6340     ;; Finally, if there are no marked lists at all left, and if there
6341     ;; are no elements after the lists in the info list, we just chop
6342     ;; the info list off before the marked lists.
6343     (and (null marked-lists) 
6344          (not (nthcdr 4 info))
6345          (setcdr (nthcdr 2 info) nil)))
6346   info)
6347
6348 (defun gnus-set-marked-articles 
6349   (info ticked replied expirable killed dormant bookmark score) 
6350   "Enter the various lists of marked articles into the newsgroup info list."
6351   (let (newmarked)
6352     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6353     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6354     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6355                                          newmarked)))
6356     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6357     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6358     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6359                                         newmarked)))
6360     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6361     (if (nthcdr 3 info)
6362         (progn
6363           (setcar (nthcdr 3 info) newmarked)
6364           (and (not newmarked)
6365                (not (nthcdr 4 info))
6366                (setcdr (nthcdr 2 info) nil)))
6367       (if newmarked
6368           (setcdr (nthcdr 2 info) (list newmarked))))))
6369
6370 (defun gnus-add-marked-articles (group type articles &optional info force)
6371   ;; Add ARTICLES of TYPE to the info of GROUP.
6372   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6373   ;; add, but replace marked articles of TYPE with ARTICLES.
6374   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6375         marked m)
6376     (or (not info)
6377         (and (not (setq marked (nthcdr 3 info)))
6378              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6379         (and (not (setq m (assq type (car marked))))
6380              (setcar marked (cons (cons type articles) (car marked))))
6381         (if force
6382             (setcdr m articles)
6383           (nconc m articles)))))
6384          
6385 (defun gnus-set-mode-line (where)
6386   "This function sets the mode line of the article or summary buffers.
6387 If WHERE is `summary', the summary mode line format will be used."
6388   (if (memq where gnus-updated-mode-lines)
6389       (let (mode-string)
6390         (save-excursion
6391           (set-buffer gnus-summary-buffer)
6392           (let* ((mformat (if (eq where 'article) 
6393                               gnus-article-mode-line-format-spec
6394                             gnus-summary-mode-line-format-spec))
6395                  (group-name gnus-newsgroup-name)
6396                  (article-number (or gnus-current-article 0))
6397                  (unread (- (length gnus-newsgroup-unreads)
6398                             (length gnus-newsgroup-dormant)))
6399                  (unread-and-unticked 
6400                   (- unread (length gnus-newsgroup-marked)))
6401                  (unselected (length gnus-newsgroup-unselected))
6402                  (unread-and-unselected
6403                   (cond ((and (zerop unread-and-unticked)
6404                               (zerop unselected)) "")
6405                         ((zerop unselected) 
6406                          (format "{%d more}" unread-and-unticked))
6407                         (t (format "{%d(+%d) more}"
6408                                    unread-and-unticked unselected))))
6409                  (subject
6410                   (if gnus-current-headers
6411                       (header-subject gnus-current-headers) ""))
6412                  (max-len (and gnus-mode-non-string-length
6413                                (- (frame-width) gnus-mode-non-string-length)))
6414                  header) ;; passed as argument to any user-format-funcs
6415             (setq mode-string (eval mformat))
6416             (or (numberp max-len)
6417                 (setq max-len (length mode-string)))
6418             (if (< max-len 4) (setq max-len 4))
6419             (if (> (length mode-string) max-len)
6420                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6421                 ;;  function `substring' might cut on a middle
6422                 ;;  of multi-octet character.
6423                 (setq mode-string 
6424                       (concat (gnus-truncate-string mode-string (- max-len 3))
6425                               "...")))
6426             (setq mode-string (format (format "%%-%ds" max-len)
6427                                       mode-string))))
6428         (setq mode-line-buffer-identification mode-string)
6429         (set-buffer-modified-p t))))
6430
6431 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6432   "Go through the HEADERS list and add all Xrefs to a hash table.
6433 The resulting hash table is returned, or nil if no Xrefs were found."
6434   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6435          (prefix (if (and 
6436                       (gnus-group-foreign-p from-newsgroup)
6437                       (not (memq 'virtual 
6438                                  (assoc (symbol-name (car from-method))
6439                                         gnus-valid-select-methods))))
6440                      (gnus-group-real-prefix from-newsgroup)))
6441          (xref-hashtb (make-vector 63 0))
6442          start group entry number xrefs header)
6443     (while headers
6444       (setq header (car headers))
6445       (if (and (setq xrefs (header-xref header))
6446                (not (memq (header-number header) unreads)))
6447           (progn
6448             (setq start 0)
6449             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6450               (setq start (match-end 0))
6451               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6452                                                     (match-end 1))))
6453               (setq number 
6454                     (string-to-int (substring xrefs (match-beginning 2) 
6455                                               (match-end 2))))
6456               (if (setq entry (gnus-gethash group xref-hashtb))
6457                   (setcdr entry (cons number (cdr entry)))
6458                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6459       (setq headers (cdr headers)))
6460     (if start xref-hashtb nil)))
6461
6462 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6463   "Look through all the headers and mark the Xrefs as read."
6464   (let ((virtual (memq 'virtual 
6465                        (assoc (symbol-name (car (gnus-find-method-for-group 
6466                                                  from-newsgroup)))
6467                               gnus-valid-select-methods)))
6468         name entry info xref-hashtb idlist method
6469         nth4)
6470     (save-excursion
6471       (set-buffer gnus-group-buffer)
6472       (if (setq xref-hashtb 
6473                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6474           (mapatoms 
6475            (lambda (group)
6476              (if (string= from-newsgroup (setq name (symbol-name group)))
6477                  ()
6478                (setq idlist (symbol-value group))
6479                ;; Dead groups are not updated.
6480                (if (and (prog1 
6481                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6482                                   info (nth 2 entry))
6483                           (if (stringp (setq nth4 (nth 4 info)))
6484                               (setq nth4 (gnus-server-to-method nth4))))
6485                         ;; Only do the xrefs if the group has the same
6486                         ;; select method as the group we have just read.
6487                         (or (gnus-methods-equal-p 
6488                              nth4 (gnus-find-method-for-group from-newsgroup))
6489                             virtual
6490                             (equal nth4 
6491                                    (setq method (gnus-find-method-for-group 
6492                                                  from-newsgroup)))
6493                             (and (equal (car nth4) (car method))
6494                                  (equal (nth 1 nth4) (nth 1 method))))
6495                         gnus-use-cross-reference
6496                         (or (not (eq gnus-use-cross-reference t))
6497                             virtual
6498                             ;; Only do cross-references on subscribed
6499                             ;; groups, if that is what is wanted.  
6500                             (<= (nth 1 info) gnus-level-subscribed)))
6501                    (gnus-group-make-articles-read name idlist expirable))))
6502            xref-hashtb)))))
6503
6504 (defun gnus-group-make-articles-read (group articles expirable)
6505   (let* ((num 0)
6506          (entry (gnus-gethash group gnus-newsrc-hashtb))
6507          (info (nth 2 entry))
6508          (active (gnus-gethash group gnus-active-hashtb))
6509          exps expirable range)
6510     ;; First peel off all illegal article numbers.
6511     (if active
6512         (let ((ids articles)
6513               (ticked (cdr (assq 'tick (nth 3 info))))
6514               (dormant (cdr (assq 'dormant (nth 3 info))))
6515               id)
6516           (setq exps nil)
6517           (while ids
6518             (setq id (car ids))
6519             (if (or (> id (cdr active))
6520                     (< id (car active))
6521                     (memq id ticked)
6522                     (memq id dormant))
6523                 (setq articles (delq id articles)))
6524             (and (memq id expirable)
6525                  (setq exps (cons id exps)))
6526             (setq ids (cdr ids)))))
6527     ;; Update expirable articles.
6528     (gnus-add-marked-articles nil 'expirable exps info)
6529     (and active
6530          (null (nth 2 info))
6531          (> (car active) 1)
6532          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6533     (setcar (nthcdr 2 info)
6534             (setq range
6535                   (gnus-add-to-range 
6536                    (nth 2 info) 
6537                    (setq articles (sort articles '<)))))
6538     ;; Then we have to re-compute how many unread
6539     ;; articles there are in this group.
6540     (if active
6541         (progn
6542           (cond 
6543            ((not range)
6544             (setq num (- (1+ (cdr active)) (car active))))
6545            ((not (listp (cdr range)))
6546             (setq num (- (cdr active) (- (1+ (cdr range)) 
6547                                          (car range)))))
6548            (t
6549             (while range
6550               (if (numberp (car range))
6551                   (setq num (1+ num))
6552                 (setq num (+ num (- (1+ (cdr (car range)))
6553                                     (car (car range))))))
6554               (setq range (cdr range)))
6555             (setq num (- (cdr active) num))))
6556           ;; Update the number of unread articles.
6557           (setcar 
6558            entry 
6559            (max 0 (- num 
6560                      (length (cdr (assq 'tick (nth 3 info))))
6561                      (length 
6562                       (cdr (assq 'dormant (nth 3 info)))))))
6563           ;; Update the group buffer.
6564           (gnus-group-update-group group t)))))
6565
6566 (defun gnus-methods-equal-p (m1 m2)
6567   (let ((m1 (or m1 gnus-select-method))
6568         (m2 (or m2 gnus-select-method)))
6569     (or (equal m1 m2)
6570         (and (eq (car m1) (car m2))
6571              (or (not (memq 'address (assoc (symbol-name (car m1))
6572                                             gnus-valid-select-methods)))
6573                  (equal (nth 1 m1) (nth 1 m2)))))))
6574
6575 (defsubst gnus-header-value ()
6576   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6577
6578 (defvar gnus-newsgroup-none-id 0)
6579
6580 (defun gnus-get-newsgroup-headers ()
6581   (setq gnus-article-internal-prepare-hook nil)
6582   (let ((cur nntp-server-buffer)
6583         (dependencies gnus-newsgroup-dependencies)
6584         headers id dep end ref)
6585     (save-excursion
6586       (set-buffer nntp-server-buffer)
6587       (goto-char (point-min))
6588       ;; Search to the beginning of the next header. Error messages
6589       ;; do not begin with 2 or 3.
6590       (while (re-search-forward "^[23][0-9]+ " nil t)
6591         (let ((header (make-vector 9 nil))
6592               (case-fold-search t)
6593               (p (point))
6594               in-reply-to)
6595           (setq id nil
6596                 ref nil)
6597           (header-set-number header (read cur))
6598           ;; This implementation of this function, with nine
6599           ;; search-forwards instead of the one re-search-forward and
6600           ;; a case (which basically was the old function) is actually
6601           ;; about twice as fast, even though it looks messier. You
6602           ;; can't have everything, I guess. Speed and elegance
6603           ;; doesn't always come hand in hand.
6604           (save-restriction
6605             (narrow-to-region (point) (or (save-excursion 
6606                                             (search-forward "\n.\n" nil t))
6607                                           (point)))
6608             (if (search-forward "\nfrom: " nil t)
6609                 (header-set-from header (gnus-header-value))
6610               (header-set-from header "(nobody)"))
6611             (goto-char p)
6612             (if (search-forward "\nsubject: " nil t)
6613                 (header-set-subject header (gnus-header-value))
6614               (header-set-subject header "(none)"))
6615             (goto-char p)
6616             (and (search-forward "\nxref: " nil t)
6617                  (header-set-xref header (gnus-header-value)))
6618             (goto-char p)
6619             (or (numberp (and (search-forward "\nlines: " nil t)
6620                               (header-set-lines header (read cur))))
6621                 (header-set-lines header 0))
6622             (goto-char p)
6623             (and (search-forward "\ndate: " nil t)
6624                  (header-set-date header (gnus-header-value)))
6625             (goto-char p)
6626             (if (search-forward "\nmessage-id: " nil t)
6627                 (header-set-id header (setq id (gnus-header-value)))
6628               ;; If there was no message-id, we just fake one to make
6629               ;; subsequent routines simpler.
6630               (header-set-id 
6631                header 
6632                (setq id (concat "none+" 
6633                                 (int-to-string 
6634                                  (setq gnus-newsgroup-none-id 
6635                                        (1+ gnus-newsgroup-none-id)))))))
6636             (goto-char p)
6637             (if (search-forward "\nreferences: " nil t)
6638                 (progn
6639                   (header-set-references header (gnus-header-value))
6640                   (setq end (match-end 0))
6641                   (save-excursion
6642                     (setq ref 
6643                           (downcase
6644                            (buffer-substring
6645                             (progn 
6646                               (end-of-line)
6647                               (search-backward ">" end t)
6648                               (1+ (point)))
6649                             (progn
6650                               (search-backward "<" end t)
6651                               (point)))))))
6652               ;; Get the references from the in-reply-to header if there
6653               ;; ware no references and the in-reply-to header looks
6654               ;; promising. 
6655               (if (and (search-forward "\nin-reply-to: " nil t)
6656                        (setq in-reply-to (gnus-header-value))
6657                        (string-match "<[^>]+>" in-reply-to))
6658                   (progn
6659                     (header-set-references 
6660                      header 
6661                      (setq ref (substring in-reply-to (match-beginning 0)
6662                                           (match-end 0))))
6663                     (setq ref (downcase ref)))
6664                 (setq ref "none")))
6665             ;; We do some threading while we read the headers. The
6666             ;; message-id and the last reference are both entered into
6667             ;; the same hash table. Some tippy-toeing around has to be
6668             ;; done in case an article has arrived before the article
6669             ;; which it refers to.
6670             (if (boundp (setq dep (intern (downcase id) dependencies)))
6671                 (if (car (symbol-value dep))
6672                     ;; An article with this Message-ID has already
6673                     ;; been seen, so we ignore this one, except we add
6674                     ;; any additional Xrefs (in case the two articles
6675                     ;; came from different servers.
6676                     (progn
6677                       (header-set-xref 
6678                        (car (symbol-value dep))
6679                        (concat (or (header-xref (car (symbol-value dep))) "")
6680                                (or (header-xref header) "")))
6681                       (setq header nil))
6682                   (setcar (symbol-value dep) header))
6683               (set dep (list header)))
6684             (if header
6685                 (progn
6686                   (if (boundp (setq dep (intern ref dependencies)))
6687                       (setcdr (symbol-value dep) 
6688                               (cons header (cdr (symbol-value dep))))
6689                     (set dep (list nil header)))
6690                   (setq headers (cons header headers))))
6691             (goto-char (point-max))))))
6692     (nreverse headers)))
6693
6694 ;; The following macros and functions were written by Felix Lee
6695 ;; <flee@cse.psu.edu>. 
6696
6697 (defmacro gnus-nov-read-integer ()
6698   '(prog1
6699        (if (= (following-char) ?\t)
6700            0
6701          (let ((num (condition-case nil (read buffer) (error nil))))
6702            (if (numberp num) num 0)))
6703      (or (eobp) (forward-char 1))))
6704
6705 (defmacro gnus-nov-skip-field ()
6706   '(search-forward "\t" eol 'move))
6707
6708 (defmacro gnus-nov-field ()
6709   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6710
6711 ;; Goes through the xover lines and returns a list of vectors
6712 (defun gnus-get-newsgroup-headers-xover (sequence)
6713   "Parse the news overview data in the server buffer, and return a
6714 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6715   ;; Get the Xref when the users reads the articles since most/some
6716   ;; NNTP servers do not include Xrefs when using XOVER.
6717   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6718   (let ((cur nntp-server-buffer)
6719         (dependencies gnus-newsgroup-dependencies)
6720         number headers header)
6721     (save-excursion
6722       (set-buffer nntp-server-buffer)
6723       (goto-char (point-min))
6724       (while (and sequence (not (eobp)))
6725         (setq number (read cur))
6726         (while (and sequence (< (car sequence) number))
6727           (setq sequence (cdr sequence)))
6728         (and sequence 
6729              (eq number (car sequence))
6730              (progn
6731                (setq sequence (cdr sequence))
6732                (if (setq header 
6733                          (inline (gnus-nov-parse-line number dependencies)))
6734                    (setq headers (cons header headers)))))
6735         (forward-line 1))
6736       (setq headers (nreverse headers)))
6737     headers))
6738
6739 ;; This function has to be called with point after the article number
6740 ;; on the beginning of the line.
6741 (defun gnus-nov-parse-line (number dependencies)
6742   (let ((none 0)
6743         (eol (gnus-point-at-eol)) 
6744         (buffer (current-buffer))
6745         header ref id dep)
6746
6747     ;; overview: [num subject from date id refs chars lines misc]
6748     (narrow-to-region (point) eol)
6749     (forward-char)
6750
6751     (condition-case nil
6752         (setq header
6753               (vector 
6754                number                   ; number
6755                (gnus-nov-field)         ; subject
6756                (gnus-nov-field)         ; from
6757                (gnus-nov-field)         ; date
6758                (setq id (or (gnus-nov-field)
6759                             (concat "none+"
6760                                     (int-to-string 
6761                                      (setq none (1+ none)))))) ; id
6762                (progn
6763                  (save-excursion
6764                    (let ((beg (point)))
6765                      (search-forward "\t" eol)
6766                      (if (search-backward ">" beg t)
6767                          (setq ref 
6768                                (downcase 
6769                                 (buffer-substring 
6770                                  (1+ (point))
6771                                  (progn
6772                                    (search-backward "<" beg t)
6773                                    (point)))))
6774                        (setq ref nil))))
6775                  (gnus-nov-field))      ; refs
6776                (gnus-nov-read-integer)  ; chars
6777                (gnus-nov-read-integer)  ; lines
6778                (if (= (following-char) ?\n)
6779                    nil
6780                  (gnus-nov-field))      ; misc
6781                ))
6782       (error (progn 
6783                (ding)
6784                (message "Strange nov line.")
6785                (setq header nil)
6786                (goto-char eol))))
6787
6788     (widen)
6789
6790     ;; We build the thread tree.
6791     (and header
6792          (if (boundp (setq dep (intern (downcase id) dependencies)))
6793              (if (car (symbol-value dep))
6794                  ;; An article with this Message-ID has already been seen,
6795                  ;; so we ignore this one, except we add any additional
6796                  ;; Xrefs (in case the two articles came from different
6797                  ;; servers.
6798                  (progn
6799                    (header-set-xref 
6800                     (car (symbol-value dep))
6801                     (concat (or (header-xref (car (symbol-value dep))) "")
6802                             (or (header-xref header) "")))
6803                    (setq header nil))
6804                (setcar (symbol-value dep) header))
6805            (set dep (list header))))
6806     (if header
6807         (progn
6808           (if (boundp (setq dep (intern (or ref "none") 
6809                                         dependencies)))
6810               (setcdr (symbol-value dep) 
6811                       (cons header (cdr (symbol-value dep))))
6812             (set dep (list nil header)))))
6813     header))
6814
6815 (defun gnus-article-get-xrefs ()
6816   "Fill in the Xref value in `gnus-current-headers', if necessary.
6817 This is meant to be called in `gnus-article-internal-prepare-hook'."
6818   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6819                                  gnus-current-headers)))
6820     (or (not gnus-use-cross-reference)
6821         (not headers)
6822         (and (header-xref headers)
6823              (not (string= (header-xref headers) "")))
6824         (let ((case-fold-search t)
6825               xref)
6826           (save-restriction
6827             (gnus-narrow-to-headers)
6828             (goto-char (point-min))
6829             (if (or (and (eq (downcase (following-char)) ?x)
6830                          (looking-at "Xref:"))
6831                     (search-forward "\nXref:" nil t))
6832                 (progn
6833                   (goto-char (1+ (match-end 0)))
6834                   (setq xref (buffer-substring (point) 
6835                                                (progn (end-of-line) (point))))
6836                   (header-set-xref headers xref))))))))
6837
6838 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6839 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6840
6841 ;; Return a header specified by a NUMBER.
6842 (defun gnus-get-header-by-number (number)
6843   (save-excursion
6844     (set-buffer gnus-summary-buffer)
6845     (or gnus-newsgroup-headers-hashtb-by-number
6846         (gnus-make-headers-hashtable-by-number))
6847     (gnus-gethash (int-to-string number)
6848                   gnus-newsgroup-headers-hashtb-by-number)))
6849
6850 (defun gnus-make-headers-hashtable-by-number ()
6851   "Make hashtable for the variable gnus-newsgroup-headers by number."
6852   (save-excursion
6853     (set-buffer gnus-summary-buffer)
6854     (let ((headers gnus-newsgroup-headers)
6855           header)
6856       (setq gnus-newsgroup-headers-hashtb-by-number
6857             (gnus-make-hashtable (length headers)))
6858       (while headers
6859         (setq header (car headers))
6860         (gnus-sethash (int-to-string (header-number header))
6861                       header gnus-newsgroup-headers-hashtb-by-number)
6862         (setq headers (cdr headers))))))
6863
6864 (defun gnus-more-header-backward ()
6865   "Find new header backward."
6866   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6867         (artnum gnus-newsgroup-begin)
6868         (header nil))
6869     (while (and (not header)
6870                 (> artnum first))
6871       (setq artnum (1- artnum))
6872       (setq header (gnus-read-header artnum)))
6873     header))
6874
6875 (defun gnus-more-header-forward (&optional backward)
6876   "Find new header forward.
6877 If BACKWARD, find new header backward instead."
6878   (if backward
6879       (gnus-more-header-backward)
6880     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6881           (artnum gnus-newsgroup-end)
6882           (header nil))
6883       (while (and (not header)
6884                   (< artnum last))
6885         (setq artnum (1+ artnum))
6886         (setq header (gnus-read-header artnum)))
6887       header)))
6888
6889 (defun gnus-extend-newsgroup (header &optional backward)
6890   "Extend newsgroup selection with HEADER.
6891 Optional argument BACKWARD means extend toward backward."
6892   (if header
6893       (let ((artnum (header-number header)))
6894         (setq gnus-newsgroup-headers
6895               (if backward
6896                   (cons header gnus-newsgroup-headers)
6897                 (nconc gnus-newsgroup-headers (list header))))
6898         (setq gnus-newsgroup-unselected
6899               (delq artnum gnus-newsgroup-unselected))
6900         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6901         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6902
6903 (defun gnus-summary-work-articles (n)
6904   "Return a list of articles to be worked upon. The prefix argument,
6905 the list of process marked articles, and the current article will be
6906 taken into consideration."
6907   (let (articles)
6908     (if (and n (numberp n))
6909         (let ((backward (< n 0))
6910               (n (abs n)))
6911           (save-excursion
6912             (while (and (> n 0)
6913                         (setq articles (cons (gnus-summary-article-number) 
6914                                              articles))
6915                         (gnus-summary-search-forward nil nil backward))
6916               (setq n (1- n))))
6917           (sort articles (function <)))
6918       (or (reverse gnus-newsgroup-processable)
6919           (list (gnus-summary-article-number))))))
6920
6921 (defun gnus-summary-search-group (&optional backward use-level)
6922   "Search for next unread newsgroup.
6923 If optional argument BACKWARD is non-nil, search backward instead."
6924   (save-excursion
6925     (set-buffer gnus-group-buffer)
6926     (if (gnus-group-search-forward 
6927          backward nil (if use-level (gnus-group-group-level) nil))
6928         (gnus-group-group-name))))
6929
6930 (defun gnus-summary-best-group (&optional exclude-group)
6931   "Find the name of the best unread group.
6932 If EXCLUDE-GROUP, do not go to this group."
6933   (save-excursion
6934     (set-buffer gnus-group-buffer)
6935     (save-excursion
6936       (gnus-group-best-unread-group exclude-group))))
6937
6938 (defun gnus-summary-search-subject (&optional backward unread subject)
6939   "Search for article forward.
6940 If BACKWARD is non-nil, search backward.
6941 If UNREAD is non-nil, only unread articles are selected.
6942 If SUBJECT is non-nil, the article which has the same subject will be
6943 searched for." 
6944   (let ((func (if backward 'previous-single-property-change
6945                 'next-single-property-change))
6946         (beg (point))
6947         (did t)
6948         pos psubject)
6949     (beginning-of-line)
6950     (and gnus-summary-check-current unread
6951          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
6952          (setq did nil))
6953     (if (not did)
6954         ()
6955       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6956       (while
6957           (and 
6958            (setq pos (funcall func (point) 'gnus-number))
6959            (goto-char (if backward (1- pos) pos))
6960            (setq did
6961                  (not (and
6962                        (or (not unread)
6963                            (eq (get-text-property (point) 'gnus-mark)
6964                                gnus-unread-mark))
6965                        (or (not subject)
6966                            (and (setq psubject (gnus-summary-subject-string))
6967                                 (gnus-subject-eq subject psubject))))))
6968            (if backward (if (bobp) nil (forward-char -1) t)
6969              (if (eobp) nil (forward-char 1) t)))))
6970     (if did
6971         (progn (goto-char beg) nil)
6972       (prog1
6973           (get-text-property (point) 'gnus-number)
6974         (gnus-summary-position-cursor)))))
6975
6976 (defun gnus-subject-eq (s1 s2)
6977   (cond
6978    ((null gnus-summary-gather-subject-limit)
6979     (equal (gnus-simplify-subject-re s1)
6980            (gnus-simplify-subject-re s2)))
6981    ((eq gnus-summary-gather-subject-limit 'fuzzy)
6982     (equal (gnus-simplify-subject-fuzzy s1)
6983            (gnus-simplify-subject-fuzzy s2)))
6984    ((numberp gnus-summary-gather-subject-limit)
6985     (equal (substring s1 gnus-summary-gather-subject-limit)
6986            (substring s2 gnus-summary-gather-subject-limit)))
6987    (t
6988     (equal s1 s2))))
6989     
6990 (defun gnus-summary-search-forward (&optional unread subject backward)
6991   "Search for article forward.
6992 If UNREAD is non-nil, only unread articles are selected.
6993 If SUBJECT is non-nil, the article which has the same subject will be
6994 searched for. 
6995 If BACKWARD is non-nil, the search will be performed backwards instead."
6996   (gnus-summary-search-subject backward unread subject))
6997
6998 (defun gnus-summary-search-backward (&optional unread subject)
6999   "Search for article backward.
7000 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7001 If 2nd optional argument SUBJECT is non-nil, the article which has
7002 the same subject will be searched for."
7003   (gnus-summary-search-forward unread subject t))
7004
7005 (defun gnus-summary-article-number (&optional number-or-nil)
7006   "The article number of the article on the current line.
7007 If there isn's an article number here, then we return the current
7008 article number."
7009   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7010     (if number-or-nil number (or number gnus-current-article))))
7011
7012 (defun gnus-summary-thread-level ()
7013   "The thread level of the article on the current line."
7014   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7015       0))
7016
7017 (defun gnus-summary-pseudo-article ()
7018   "The thread level of the article on the current line."
7019   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7020
7021 (defun gnus-summary-article-mark ()
7022   "The mark on the current line."
7023   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7024
7025 (defun gnus-summary-subject-string ()
7026   "Return current subject string or nil if nothing."
7027   (let ((article (gnus-summary-article-number))
7028         header)
7029     (and article 
7030          (setq header (gnus-get-header-by-number article))
7031          (vectorp header)
7032          (header-subject header))))
7033
7034 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7035 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7036 (defun gnus-summary-article-score ()
7037   "Return current article score."
7038   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7039       gnus-summary-default-score 0))
7040
7041 (defun gnus-summary-recenter ()
7042   "Center point in the summary window.
7043 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7044 displayed, no centering will be performed." 
7045   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7046   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7047   (let* ((top (cond ((< (window-height) 4) 0)
7048                     ((< (window-height) 7) 1)
7049                     (t 2)))
7050          (height (1- (window-height)))
7051          (bottom (save-excursion (goto-char (point-max))
7052                                  (forward-line (- height))
7053                                  (point)))
7054          (window (get-buffer-window (current-buffer))))
7055     (and 
7056      ;; The user has to want it,
7057      gnus-auto-center-summary 
7058      ;; the article buffer must be displayed,
7059      (get-buffer-window gnus-article-buffer)
7060      ;; Set the window start to either `bottom', which is the biggest
7061      ;; possible valid number, or the second line from the top,
7062      ;; whichever is the least.
7063      (set-window-start
7064       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7065
7066 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7067 (defun gnus-short-group-name (group &optional levels)
7068   "Collapse GROUP name LEVELS."
7069   (let* ((name "") (foreign "") (depth -1) (skip 1)
7070          (levels (or levels
7071                      (progn
7072                        (while (string-match "\\." group skip)
7073                          (setq skip (match-end 0)
7074                                depth (+ depth 1)))
7075                        depth))))
7076     (if (string-match ":" group)
7077         (setq foreign (substring group 0 (match-end 0))
7078               group (substring group (match-end 0))))
7079     (while group
7080       (if (and (string-match "\\." group) (> levels 0))
7081           (setq name (concat name (substring group 0 1))
7082                 group (substring group (match-end 0))
7083                 levels (- levels 1)
7084                 name (concat name "."))
7085         (setq name (concat foreign name group)
7086               group nil)))
7087     name))
7088
7089 (defun gnus-summary-jump-to-group (newsgroup)
7090   "Move point to NEWSGROUP in group mode buffer."
7091   ;; Keep update point of group mode buffer if visible.
7092   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7093       (save-window-excursion
7094         ;; Take care of tree window mode.
7095         (if (get-buffer-window gnus-group-buffer)
7096             (pop-to-buffer gnus-group-buffer))
7097         (gnus-group-jump-to-group newsgroup))
7098     (save-excursion
7099       ;; Take care of tree window mode.
7100       (if (get-buffer-window gnus-group-buffer)
7101           (pop-to-buffer gnus-group-buffer)
7102         (set-buffer gnus-group-buffer))
7103       (gnus-group-jump-to-group newsgroup))))
7104
7105 ;; This function returns a list of article numbers based on the
7106 ;; difference between the ranges of read articles in this group and
7107 ;; the range of active articles.
7108 (defun gnus-list-of-unread-articles (group)
7109   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7110          (active (gnus-gethash group gnus-active-hashtb))
7111          (last (cdr active))
7112          first nlast unread)
7113     ;; If none are read, then all are unread. 
7114     (if (not read)
7115         (setq first (car active))
7116       ;; If the range of read articles is a single range, then the
7117       ;; first unread article is the article after the last read
7118       ;; article. Sounds logical, doesn't it?
7119       (if (not (listp (cdr read)))
7120           (setq first (1+ (cdr read)))
7121         ;; `read' is a list of ranges.
7122         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7123                                 (car (car read)))) 1)
7124             (setq first 1))
7125         (while read
7126           (if first 
7127               (while (< first nlast)
7128                 (setq unread (cons first unread))
7129                 (setq first (1+ first))))
7130           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7131           (setq nlast (if (atom (car (cdr read))) 
7132                           (car (cdr read))
7133                         (car (car (cdr read)))))
7134           (setq read (cdr read)))))
7135     ;; And add the last unread articles.
7136     (while (<= first last)
7137       (setq unread (cons first unread))
7138       (setq first (1+ first)))
7139     ;; Return the list of unread articles.
7140     (nreverse unread)))
7141
7142 (defun gnus-list-of-read-articles (group)
7143   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7144         (active (gnus-gethash group gnus-active-hashtb)))
7145     (and info active
7146          (gnus-sorted-complement 
7147           (gnus-uncompress-range active) 
7148           (gnus-list-of-unread-articles group)))))
7149
7150 ;; Various summary commands
7151
7152 (defun gnus-summary-universal-argument ()
7153   "Perform any operation on all articles marked with the process mark."
7154   (interactive)
7155   (gnus-set-global-variables)
7156   (let ((articles (reverse gnus-newsgroup-processable))
7157         func)
7158     (or articles (error "No articles marked"))
7159     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7160         (error "Undefined key"))
7161     (while articles
7162       (gnus-summary-goto-subject (car articles))
7163       (command-execute func)
7164       (gnus-summary-remove-process-mark (car articles))
7165       (setq articles (cdr articles)))))
7166
7167 (defun gnus-summary-toggle-truncation (arg)
7168   "Toggle truncation of summary lines.
7169 With arg, turn line truncation on iff arg is positive."
7170   (interactive "P")
7171   (setq truncate-lines
7172         (if (null arg) (not truncate-lines)
7173           (> (prefix-numeric-value arg) 0)))
7174   (redraw-display))
7175
7176 (defun gnus-summary-reselect-current-group (all)
7177   "Once exit and then reselect the current newsgroup.
7178 The prefix argument ALL means to select all articles."
7179   (interactive "P")
7180   (gnus-set-global-variables)
7181   (let ((current-subject (gnus-summary-article-number))
7182         (group gnus-newsgroup-name))
7183     (setq gnus-newsgroup-begin nil)
7184     (gnus-summary-exit t)
7185     ;; We have to adjust the point of group mode buffer because the
7186     ;; current point was moved to the next unread newsgroup by
7187     ;; exiting.
7188     (gnus-summary-jump-to-group group)
7189     (gnus-group-read-group all t)
7190     (gnus-summary-goto-subject current-subject)))
7191
7192 (defun gnus-summary-rescan-group (all)
7193   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7194   (interactive "P")
7195   (gnus-set-global-variables)
7196   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7197   (let ((group gnus-newsgroup-name))
7198     (gnus-summary-exit)
7199     (gnus-summary-jump-to-group group)
7200     (save-excursion
7201       (set-buffer gnus-group-buffer)
7202       (gnus-group-get-new-news-this-group 1))
7203     (gnus-summary-jump-to-group group)
7204     (gnus-group-read-group all)))
7205
7206 (defun gnus-summary-update-info ()
7207   (let* ((group gnus-newsgroup-name))
7208     (if gnus-newsgroup-kill-headers
7209         (setq gnus-newsgroup-killed
7210               (gnus-compress-sequence
7211                (nconc
7212                 (gnus-set-sorted-intersection
7213                  (gnus-uncompress-range gnus-newsgroup-killed)
7214                  (setq gnus-newsgroup-unselected
7215                        (sort gnus-newsgroup-unselected '<)))
7216                 (setq gnus-newsgroup-unreads
7217                       (sort gnus-newsgroup-unreads '<))) t)))
7218     (or (listp (cdr gnus-newsgroup-killed))
7219         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7220     (let ((headers gnus-newsgroup-headers))
7221       (gnus-close-group group)
7222       (run-hooks 'gnus-exit-group-hook)
7223       (gnus-update-read-articles 
7224        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7225        gnus-newsgroup-marked
7226        t gnus-newsgroup-replied gnus-newsgroup-expirable
7227        gnus-newsgroup-killed gnus-newsgroup-dormant
7228        gnus-newsgroup-bookmarks 
7229        (and gnus-save-score gnus-newsgroup-scored))
7230       (and gnus-use-cross-reference
7231            (gnus-mark-xrefs-as-read 
7232             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7233       ;; Do adaptive scoring, and possibly save score files.
7234       (and gnus-newsgroup-adaptive
7235            (gnus-score-adaptive))
7236       (and gnus-use-scoring 
7237            (fboundp 'gnus-score-save)
7238            (funcall 'gnus-score-save))
7239       ;; Do not switch windows but change the buffer to work.
7240       (set-buffer gnus-group-buffer)
7241       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7242           (gnus-group-update-group group)))))
7243   
7244 (defun gnus-summary-exit (&optional temporary)
7245   "Exit reading current newsgroup, and then return to group selection mode.
7246 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7247   (interactive)
7248   (gnus-set-global-variables)
7249   (gnus-kill-save-kill-buffer)
7250   (let* ((group gnus-newsgroup-name)
7251          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7252                                                   gnus-newsgroup-name))))
7253          (mode major-mode)
7254          (buf (current-buffer)))
7255     (gnus-summary-update-info) ; Make all changes in this group permanent.
7256     ;; Make sure where I was, and go to next newsgroup.
7257     (or quit-config
7258         (progn
7259           (gnus-group-jump-to-group group)
7260           (gnus-group-next-unread-group 1)))
7261     (if temporary
7262         nil                             ;Nothing to do.
7263       (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7264       ;; We set all buffer-local variables to nil. It is unclear why
7265       ;; this is needed, but if we don't, buffer-local variables are
7266       ;; not garbage-collected, it seems. This would the lead to en
7267       ;; ever-growing Emacs.
7268       (set-buffer buf)
7269       (gnus-summary-clear-local-variables)
7270       ;; We clear the global counterparts of the buffer-local
7271       ;; variables as well, just to be on the safe side.
7272       (gnus-configure-windows 'group)
7273       (gnus-summary-clear-local-variables)
7274       ;; Return to group mode buffer. 
7275       (if (eq mode 'gnus-summary-mode)
7276           (gnus-kill-buffer buf))
7277       (if (get-buffer gnus-article-buffer)
7278           (bury-buffer gnus-article-buffer))
7279       (setq gnus-current-select-method gnus-select-method)
7280       (pop-to-buffer gnus-group-buffer)
7281       (if (not quit-config)
7282           (progn
7283             (gnus-group-jump-to-group group)
7284             (gnus-group-next-unread-group 1))
7285         (if (not (buffer-name (car quit-config)))
7286             (gnus-configure-windows 'group)
7287           (set-buffer (car quit-config))
7288           (and (eq major-mode 'gnus-summary-mode)
7289                (gnus-set-global-variables))
7290           (gnus-configure-windows (cdr quit-config)))))))
7291
7292 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7293 (defun gnus-summary-exit-no-update (&optional no-questions)
7294   "Quit reading current newsgroup without updating read article info."
7295   (interactive)
7296   (let* ((group gnus-newsgroup-name)
7297          (quit-config (nth 1 (assoc 'quit-config 
7298                                     (gnus-find-method-for-group group)))))
7299     (if (or no-questions
7300             gnus-expert-user
7301             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7302         (progn
7303           (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7304           (gnus-close-group group)
7305           (gnus-summary-clear-local-variables)
7306           (set-buffer gnus-group-buffer)
7307           (gnus-summary-clear-local-variables)
7308           ;; Return to group selection mode.
7309           (gnus-configure-windows 'group)
7310           (if (get-buffer gnus-summary-buffer)
7311               (kill-buffer gnus-summary-buffer))
7312           (if (get-buffer gnus-article-buffer)
7313               (bury-buffer gnus-article-buffer))
7314           (if (equal (gnus-group-group-name) group)
7315               (gnus-group-next-unread-group 1))
7316           (if quit-config
7317               (progn
7318                 (if (not (buffer-name (car quit-config)))
7319                     (gnus-configure-windows 'group)
7320                   (set-buffer (car quit-config))
7321                   (and (eq major-mode 'gnus-summary-mode)
7322                        (gnus-set-global-variables))
7323                   (gnus-configure-windows (cdr quit-config)))))))))
7324
7325 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7326 (defun gnus-summary-fetch-faq (group)
7327   "Fetch the FAQ for the current group."
7328   (interactive (list gnus-newsgroup-name))
7329   (gnus-configure-windows 'summary-faq)
7330   (find-file (concat gnus-group-faq-directory group)))
7331
7332 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7333 (defun gnus-summary-describe-group (force)
7334   "Describe the current newsgroup."
7335   (interactive "P")
7336   (gnus-group-describe-group force gnus-newsgroup-name))
7337
7338 (defun gnus-summary-describe-briefly ()
7339   "Describe summary mode commands briefly."
7340   (interactive)
7341   (gnus-message 6
7342     (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")))
7343
7344 ;; Walking around group mode buffer from summary mode.
7345
7346 (defun gnus-summary-next-group (&optional no-article group backward)
7347   "Exit current newsgroup and then select next unread newsgroup.
7348 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7349 If BACKWARD, go to previous group instead."
7350   (interactive "P")
7351   (gnus-set-global-variables)
7352   (let ((ingroup gnus-newsgroup-name)
7353         (sumbuf (current-buffer))
7354         num)
7355     (set-buffer gnus-group-buffer)
7356     (if (and group
7357              (or (and (numberp (setq num (car (gnus-gethash
7358                                                group gnus-newsrc-hashtb))))
7359                       (< num 1))
7360                  (null num)))
7361         (progn
7362           (gnus-group-jump-to-group group)
7363           (setq group nil))
7364       (gnus-group-jump-to-group ingroup))
7365     (gnus-summary-search-group backward)
7366     (let ((group (or group (gnus-summary-search-group backward))))
7367       (set-buffer sumbuf)
7368       (gnus-summary-exit t)             ;Update all information.
7369       (if (null group)
7370           (gnus-summary-exit-no-update t)
7371         (gnus-group-jump-to-group ingroup)
7372         (setq group (gnus-summary-search-group backward))
7373         (gnus-message 5 "Selecting %s..." group)
7374         (set-buffer gnus-group-buffer)
7375         ;; We are now in group mode buffer.
7376         ;; Make sure group mode buffer point is on GROUP.
7377         (gnus-group-jump-to-group group)
7378         (if (not (eq gnus-auto-select-next 'quietly))
7379             (progn
7380               (gnus-summary-read-group group nil no-article sumbuf)
7381               (and (string= gnus-newsgroup-name ingroup)
7382                    (bufferp sumbuf) (buffer-name sumbuf)
7383                    (progn
7384                      (set-buffer (setq gnus-summary-buffer sumbuf))
7385                      (gnus-summary-exit-no-update t))))
7386           (let ((prevgroup group))
7387             (gnus-group-jump-to-group ingroup)
7388             (setq group (gnus-summary-search-group backward))
7389             (gnus-summary-read-group group nil no-article sumbuf)
7390             (while (and (string= gnus-newsgroup-name ingroup)
7391                         (bufferp sumbuf) 
7392                         (buffer-name sumbuf)
7393                         (not (string= prevgroup (gnus-group-group-name))))
7394               (set-buffer gnus-group-buffer)
7395               (gnus-summary-read-group 
7396                (setq prevgroup (gnus-group-group-name)) 
7397                nil no-article sumbuf))
7398             (and (string= prevgroup (gnus-group-group-name))
7399                  ;; We have reached the final group in the group
7400                  ;; buffer.
7401                  (progn
7402                    (if (buffer-name sumbuf)
7403                        (progn
7404                          (set-buffer sumbuf)
7405                          (gnus-summary-exit)))))))))))
7406
7407 (defun gnus-summary-prev-group (no-article)
7408   "Exit current newsgroup and then select previous unread newsgroup.
7409 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7410   (interactive "P")
7411   (gnus-summary-next-group no-article nil t))
7412
7413 ;; Walking around summary lines.
7414
7415 (defun gnus-summary-first-subject (unread)
7416   "Go to the first unread subject.
7417 If UNREAD is non-nil, go to the first unread article.
7418 Returns nil if there are no unread articles."
7419   (interactive "P")
7420   (prog1
7421       (cond ((not unread)
7422              (goto-char (point-min)))
7423             ((gnus-goto-char 
7424               (text-property-any 
7425                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7426              t)
7427             (t 
7428              ;; There are no unread articles.
7429              (gnus-message 3 "No more unread articles")
7430              nil))
7431     (gnus-summary-position-cursor)))
7432
7433 (defun gnus-summary-next-subject (n &optional unread dont-display)
7434   "Go to next N'th summary line.
7435 If N is negative, go to the previous N'th subject line.
7436 If UNREAD is non-nil, only unread articles are selected.
7437 The difference between N and the actual number of steps taken is
7438 returned."
7439   (interactive "p")
7440   (let ((backward (< n 0))
7441         (n (abs n)))
7442     (while (and (> n 0)
7443                 (gnus-summary-search-forward unread nil backward))
7444       (setq n (1- n)))
7445     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7446                                (if unread " unread" "")))
7447     (or dont-display
7448         (progn
7449           (gnus-summary-recenter)
7450           (gnus-summary-position-cursor)))
7451   n))
7452
7453 (defun gnus-summary-next-unread-subject (n)
7454   "Go to next N'th unread summary line."
7455   (interactive "p")
7456   (gnus-summary-next-subject n t))
7457
7458 (defun gnus-summary-prev-subject (n &optional unread)
7459   "Go to previous N'th summary line.
7460 If optional argument UNREAD is non-nil, only unread article is selected."
7461   (interactive "p")
7462   (gnus-summary-next-subject (- n) unread))
7463
7464 (defun gnus-summary-prev-unread-subject (n)
7465   "Go to previous N'th unread summary line."
7466   (interactive "p")
7467   (gnus-summary-next-subject (- n) t))
7468
7469 (defun gnus-summary-goto-subject (article)
7470   "Go the subject line of ARTICLE."
7471   (interactive
7472    (list
7473     (string-to-int
7474      (completing-read "Article number: "
7475                       (mapcar
7476                        (lambda (headers)
7477                          (list
7478                           (int-to-string (header-number headers))))
7479                        gnus-newsgroup-headers)
7480                       nil 'require-match))))
7481   (or article (error "No article number"))
7482   (let ((b (point)))
7483     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7484                                                 'gnus-number article)))
7485         ()
7486       (gnus-summary-show-thread)
7487       ;; Skip dummy articles. 
7488       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7489           (forward-line 1))
7490       (prog1
7491           (if (not (eobp))
7492               article
7493             (goto-char b)
7494             nil)
7495         (gnus-summary-position-cursor)))))
7496
7497 ;; Walking around summary lines with displaying articles.
7498
7499 (defun gnus-summary-expand-window ()
7500   "Make the summary buffer take up the entire Emacs frame."
7501   (interactive)
7502   (gnus-set-global-variables)
7503   (gnus-configure-windows 'summary))
7504
7505 (defun gnus-summary-display-article (article &optional all-header)
7506   "Display ARTICLE in article buffer."
7507   (gnus-set-global-variables)
7508   (if (null article)
7509       nil
7510     (prog1
7511         (gnus-article-prepare article all-header)
7512       (gnus-summary-show-thread)
7513       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7514           (progn
7515             (forward-line 1)
7516             (gnus-summary-position-cursor)))
7517       (run-hooks 'gnus-select-article-hook)
7518       (gnus-summary-recenter)
7519       (gnus-summary-goto-subject article)
7520       ;; Successfully display article.
7521       (gnus-summary-update-line)
7522       (gnus-article-set-window-start 
7523        (cdr (assq article gnus-newsgroup-bookmarks)))
7524       t)))
7525
7526 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7527   "Select the current article.
7528 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7529 non-nil, the article will be re-fetched even if it already present in
7530 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7531 be displayed."
7532   (and (not pseudo) (gnus-summary-pseudo-article)
7533        (error "This is a pseudo-article."))
7534   (let ((article (or article (gnus-summary-article-number)))
7535         (all-headers (not (not all-headers))) ;Must be T or NIL.
7536         did) 
7537     (prog1
7538         (save-excursion
7539           (set-buffer gnus-summary-buffer)
7540           (if (or (null gnus-current-article)
7541                   (null gnus-article-current)
7542                   (not (eq article (cdr gnus-article-current)))
7543                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7544                   force)
7545               ;; The requested article is different from the current article.
7546               (progn
7547                 (gnus-summary-display-article article all-headers)
7548                 (setq did article))
7549             (if all-headers (gnus-article-show-all-headers))
7550             nil))
7551       (if did 
7552           (gnus-article-set-window-start 
7553            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7554
7555 (defun gnus-summary-set-current-mark (&optional current-mark)
7556   "Obsolete function."
7557   nil)
7558
7559 (defun gnus-summary-next-article (unread &optional subject backward)
7560   "Select the next article.
7561 If UNREAD, only unread articles are selected.
7562 If SUBJECT, only articles with SUBJECT are selected.
7563 If BACKWARD, the previous article is selected instead of the next."
7564   (interactive "P")
7565   (gnus-set-global-variables)
7566   (let (header)
7567     (cond
7568      ;; Is there such an article?
7569      ((gnus-summary-display-article 
7570        (gnus-summary-search-forward unread subject backward))
7571       (gnus-summary-position-cursor))
7572      ;; If not, we try the first unread, if that is wanted.
7573      ((and subject
7574            gnus-auto-select-same
7575            (gnus-summary-first-unread-article))
7576       (gnus-message 6 "Wrapped"))
7577      ;; Try to get next/previous article not displayed in this group.
7578      ((and gnus-auto-extend-newsgroup
7579            (not unread) (not subject)
7580            (setq header (gnus-more-header-forward backward)))
7581       (gnus-extend-newsgroup header backward)
7582       (let ((buffer-read-only nil))
7583         (goto-char (if backward (point-min) (point-max)))
7584         (gnus-summary-prepare-threads (list header) 0))
7585       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7586                                    gnus-newsgroup-end)))
7587      ;; Go to next/previous group.
7588      (t
7589       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7590           (gnus-summary-jump-to-group gnus-newsgroup-name))
7591       (let ((cmd (aref (this-command-keys) 0))
7592             (group 
7593              (if (eq gnus-keep-same-level 'best) 
7594                  (gnus-summary-best-group gnus-newsgroup-name)
7595                (gnus-summary-search-group backward gnus-keep-same-level))))
7596         ;; For some reason, the group window gets selected. We change
7597         ;; it back.  
7598         (select-window (get-buffer-window (current-buffer)))
7599         ;; Keep just the event type of CMD.
7600         (and (listp cmd) (setq cmd (car cmd)))
7601         ;; Select next unread newsgroup automagically.
7602         (cond 
7603          ((not gnus-auto-select-next)
7604           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7605          ((eq gnus-auto-select-next 'quietly)
7606           ;; Select quietly.
7607           (if (assoc 'quit-config (gnus-find-method-for-group 
7608                                    gnus-newsgroup-name))
7609               (gnus-summary-exit)
7610             (gnus-message 7 "No more%s articles (%s)..."
7611                           (if unread " unread" "") 
7612                           (if group (concat "selecting " group)
7613                             "exiting"))
7614             (gnus-summary-next-group nil group backward)))
7615          (t
7616           (let ((keystrokes '(?\C-n ?\C-p))
7617                 key)
7618             (while (or (null key) (memq key keystrokes))
7619               (gnus-message 
7620                7 "No more%s articles%s" (if unread " unread" "")
7621                (if (and group (not (assoc 'quit-config
7622                                           (gnus-find-method-for-group 
7623                                            gnus-newsgroup-name))))
7624                    (format " (Type %s for %s [%s])"
7625                            (single-key-description cmd) group
7626                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7627                  (format " (Type %s to exit %s)"
7628                          (single-key-description cmd)
7629                          gnus-newsgroup-name)))
7630               ;; Confirm auto selection.
7631               (let* ((event (read-event)))
7632                 (setq key (if (listp event) (car event) event))
7633                 (if (memq key keystrokes)
7634                     (let ((obuf (current-buffer)))
7635                       (switch-to-buffer gnus-group-buffer)
7636                       (gnus-group-jump-to-group group)
7637                       (execute-kbd-macro (char-to-string key))
7638                       (setq group (gnus-group-group-name))
7639                       (switch-to-buffer obuf)))))
7640             (if (equal key cmd)
7641                 (if (or (not group) (assoc 'quit-config
7642                                            (gnus-find-method-for-group
7643                                             gnus-newsgroup-name)))
7644                     (gnus-summary-exit)
7645                   (gnus-summary-next-group nil group backward))
7646               (setq unread-command-events (list key)))))))))))
7647
7648 (defun gnus-summary-next-unread-article ()
7649   "Select unread article after current one."
7650   (interactive)
7651   (gnus-summary-next-article t (and gnus-auto-select-same
7652                                     (gnus-summary-subject-string))))
7653
7654 (defun gnus-summary-prev-article (unread &optional subject)
7655   "Select the article after the current one.
7656 If UNREAD is non-nil, only unread articles are selected."
7657   (interactive "P")
7658   (gnus-summary-next-article unread subject t))
7659
7660 (defun gnus-summary-prev-unread-article ()
7661   "Select unred article before current one."
7662   (interactive)
7663   (gnus-summary-prev-article t (and gnus-auto-select-same
7664                                     (gnus-summary-subject-string))))
7665
7666 (defun gnus-summary-next-page (lines &optional circular)
7667   "Show next page of selected article.
7668 If end of article, select next article.
7669 Argument LINES specifies lines to be scrolled up.
7670 If CIRCULAR is non-nil, go to the start of the article instead of 
7671 instead of selecting the next article when reaching the end of the
7672 current article." 
7673   (interactive "P")
7674   (setq gnus-summary-buffer (current-buffer))
7675   (gnus-set-global-variables)
7676   (let ((article (gnus-summary-article-number))
7677         (endp nil))
7678     (gnus-configure-windows 'article)
7679     (if (or (null gnus-current-article)
7680             (null gnus-article-current)
7681             (/= article (cdr gnus-article-current))
7682             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7683         ;; Selected subject is different from current article's.
7684         (gnus-summary-display-article article)
7685       (gnus-eval-in-buffer-window
7686        gnus-article-buffer
7687        (setq endp (gnus-article-next-page lines)))
7688       (if endp
7689           (cond (circular
7690                  (gnus-summary-beginning-of-article))
7691                 (lines
7692                  (gnus-message 3 "End of message"))
7693                 ((null lines)
7694                  (gnus-summary-next-unread-article)))))
7695     (gnus-summary-recenter)
7696     (gnus-summary-position-cursor)))
7697
7698 (defun gnus-summary-prev-page (lines)
7699   "Show previous page of selected article.
7700 Argument LINES specifies lines to be scrolled down."
7701   (interactive "P")
7702   (gnus-set-global-variables)
7703   (let ((article (gnus-summary-article-number)))
7704     (gnus-configure-windows 'article)
7705     (if (or (null gnus-current-article)
7706             (null gnus-article-current)
7707             (/= article (cdr gnus-article-current))
7708             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7709         ;; Selected subject is different from current article's.
7710         (gnus-summary-display-article article)
7711       (gnus-summary-recenter)
7712       (gnus-eval-in-buffer-window gnus-article-buffer
7713         (gnus-article-prev-page lines))))
7714   (gnus-summary-position-cursor))
7715
7716 (defun gnus-summary-scroll-up (lines)
7717   "Scroll up (or down) one line current article.
7718 Argument LINES specifies lines to be scrolled up (or down if negative)."
7719   (interactive "p")
7720   (gnus-set-global-variables)
7721   (gnus-configure-windows 'article)
7722   (or (gnus-summary-select-article nil nil 'pseudo)
7723       (gnus-eval-in-buffer-window 
7724        gnus-article-buffer
7725        (cond ((> lines 0)
7726               (if (gnus-article-next-page lines)
7727                   (gnus-message 3 "End of message")))
7728              ((< lines 0)
7729               (gnus-article-prev-page (- lines))))))
7730   (gnus-summary-recenter)
7731   (gnus-summary-position-cursor))
7732
7733 (defun gnus-summary-next-same-subject ()
7734   "Select next article which has the same subject as current one."
7735   (interactive)
7736   (gnus-set-global-variables)
7737   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7738
7739 (defun gnus-summary-prev-same-subject ()
7740   "Select previous article which has the same subject as current one."
7741   (interactive)
7742   (gnus-set-global-variables)
7743   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7744
7745 (defun gnus-summary-next-unread-same-subject ()
7746   "Select next unread article which has the same subject as current one."
7747   (interactive)
7748   (gnus-set-global-variables)
7749   (gnus-summary-next-article t (gnus-summary-subject-string)))
7750
7751 (defun gnus-summary-prev-unread-same-subject ()
7752   "Select previous unread article which has the same subject as current one."
7753   (interactive)
7754   (gnus-set-global-variables)
7755   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7756
7757 (defun gnus-summary-first-unread-article ()
7758   "Select the first unread article. 
7759 Return nil if there are no unread articles."
7760   (interactive)
7761   (gnus-set-global-variables)
7762   (prog1
7763       (if (gnus-summary-first-subject t)
7764           (gnus-summary-display-article (gnus-summary-article-number)))
7765     (gnus-summary-position-cursor)))
7766
7767 (defun gnus-summary-best-unread-article ()
7768   "Select the unread article with the highest score."
7769   (interactive)
7770   (gnus-set-global-variables)
7771   (let ((scored gnus-newsgroup-scored)
7772         (best -1000000)
7773         article art)
7774     (while scored
7775       (or (> best (cdr (car scored)))
7776           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7777                (not (memq art gnus-newsgroup-marked))
7778                (not (memq art gnus-newsgroup-dormant))
7779                (if (= best (cdr (car scored)))
7780                    (setq article (min art article))
7781                  (setq article art)
7782                  (setq best (cdr (car scored))))))
7783       (setq scored (cdr scored)))
7784     (if article 
7785         (gnus-summary-goto-article article)
7786       (gnus-summary-first-unread-article))
7787     (gnus-summary-position-cursor)))
7788
7789 (defun gnus-summary-goto-article (article &optional all-headers)
7790   "Fetch ARTICLE and display it if it exists.
7791 If ALL-HEADERS is non-nil, no header lines are hidden."
7792   (interactive
7793    (list
7794     (string-to-int
7795      (completing-read 
7796       "Article number: "
7797       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7798               gnus-newsgroup-headers) 
7799       nil 'require-match))))
7800   (prog1
7801       (and (gnus-summary-goto-subject article)
7802            (gnus-summary-display-article article all-headers))
7803     (gnus-summary-position-cursor)))
7804
7805 (defun gnus-summary-goto-last-article ()
7806   "Go to the previously read article."
7807   (interactive)
7808   (prog1
7809       (and gnus-last-article
7810            (gnus-summary-goto-article gnus-last-article))
7811     (gnus-summary-position-cursor)))
7812
7813 (defun gnus-summary-pop-article (number)
7814   "Pop one article off the history and go to the previous.
7815 NUMBER articles will be popped off."
7816   (interactive "p")
7817   (let (to)
7818     (setq gnus-newsgroup-history
7819           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7820     (if to
7821         (gnus-summary-goto-article (car to))
7822       (error "Article history empty")))
7823   (gnus-summary-position-cursor))
7824
7825 ;; Summary article oriented commands
7826
7827 (defun gnus-summary-refer-parent-article (n)
7828   "Refer parent article N times.
7829 The difference between N and the number of articles fetched is returned."
7830   (interactive "p")
7831   (gnus-set-global-variables)
7832   (while 
7833       (and 
7834        (> n 0)
7835        (let ((ref (header-references (gnus-get-header-by-number
7836                                       (gnus-summary-article-number)))))
7837          (if (and ref (not (equal ref ""))
7838                   (string-match "<[^<>]*>[ \t]*$" ref))
7839              (gnus-summary-refer-article 
7840               (substring ref (match-beginning 0) (match-end 0)))
7841            (gnus-message 1 "No references in article %d"
7842                          (gnus-summary-article-number))
7843            nil)))
7844     (setq n (1- n)))
7845   (gnus-summary-position-cursor)
7846   n)
7847     
7848 (defun gnus-summary-refer-article (message-id)
7849   "Refer article specified by MESSAGE-ID.
7850 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7851   (interactive "sMessage-ID: ")
7852   (if (or (not (stringp message-id))
7853           (zerop (length message-id)))
7854       ()
7855     ;; Construct the correct Message-ID if necessary.
7856     ;; Suggested by tale@pawl.rpi.edu.
7857     (or (string-match "^<" message-id)
7858         (setq message-id (concat "<" message-id)))
7859     (or (string-match ">$" message-id)
7860         (setq message-id (concat message-id ">")))
7861     (let ((header (car (gnus-gethash (downcase message-id)
7862                                      gnus-newsgroup-dependencies))))
7863       (if header
7864           (or (gnus-summary-goto-article (header-number header))
7865               ;; The header has been read, but the article had been
7866               ;; expunged, so we insert it again.
7867               (progn
7868                 (gnus-summary-insert-line
7869                  nil header 0 nil gnus-read-mark nil nil
7870                  (header-subject header))
7871                 (forward-line -1)
7872                 (header-number header)))
7873         (let ((gnus-override-method gnus-refer-article-method)
7874               (gnus-ancient-mark gnus-read-mark)
7875               (tmp-buf (get-buffer-create " *gnus refer"))
7876               (tmp-point (window-start
7877                           (get-buffer-window gnus-article-buffer)))
7878               number)
7879           (and gnus-refer-article-method
7880                (or (gnus-server-opened gnus-refer-article-method)
7881                    (gnus-open-server gnus-refer-article-method)))
7882           ;; Save the old article buffer.
7883           (save-excursion
7884             (set-buffer tmp-buf)
7885             (buffer-disable-undo (current-buffer))
7886             (insert-buffer-substring gnus-article-buffer))
7887           (prog1
7888               (if (gnus-article-prepare 
7889                    message-id nil (gnus-read-header message-id))
7890                   (progn
7891                     (setq number (header-number gnus-current-headers))
7892                     (gnus-rebuild-thread message-id)
7893                     (gnus-summary-goto-subject number)
7894                     (gnus-summary-recenter)
7895                     (gnus-article-set-window-start 
7896                      (cdr (assq number gnus-newsgroup-bookmarks)))
7897                     message-id)
7898                 ;; We restore the old article buffer.
7899                 (save-excursion
7900                   (set-buffer gnus-article-buffer)
7901                   (let ((buffer-read-only nil))
7902                     (insert-buffer-substring tmp-buf)
7903                     (and tmp-point
7904                          (set-window-start (get-buffer-window (current-buffer))
7905                                            tmp-point))))
7906                 nil)
7907             (kill-buffer tmp-buf)))))))
7908
7909 (defun gnus-summary-enter-digest-group ()
7910   "Enter a digest group based on the current article."
7911   (interactive)
7912   (gnus-set-global-variables)
7913   (gnus-summary-select-article)
7914   ;; We do not want a narrowed article.
7915   (gnus-summary-stop-page-breaking)
7916   (let ((name (format "%s-%d" 
7917                       (gnus-group-prefixed-name 
7918                        gnus-newsgroup-name (list 'nndoc "")) 
7919                       gnus-current-article))
7920         (ogroup gnus-newsgroup-name)
7921         (buf (current-buffer)))
7922     (if (gnus-group-read-ephemeral-group 
7923          name (list 'nndoc name
7924                     (list 'nndoc-address (get-buffer gnus-article-buffer))
7925                     '(nndoc-article-type digest))
7926          t)
7927         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
7928                 (list (list (cons 'to-group ogroup))))
7929       (switch-to-buffer buf)
7930       (gnus-set-global-variables)
7931       (gnus-configure-windows 'summary)
7932       (gnus-message 3 "Article not a digest?"))))
7933
7934 (defun gnus-summary-isearch-article ()
7935   "Do incremental search forward on current article."
7936   (interactive)
7937   (gnus-set-global-variables)
7938   (gnus-summary-select-article)
7939   (gnus-eval-in-buffer-window 
7940    gnus-article-buffer (isearch-forward)))
7941
7942 (defun gnus-summary-search-article-forward (regexp &optional backward)
7943   "Search for an article containing REGEXP forward.
7944 If BACKWARD, search backward instead."
7945   (interactive
7946    (list (read-string
7947           (format "Search article %s (regexp%s): "
7948                   (if current-prefix-arg "backward" "forward")
7949                   (if gnus-last-search-regexp
7950                       (concat ", default " gnus-last-search-regexp)
7951                     "")))
7952          current-prefix-arg))
7953   (gnus-set-global-variables)
7954   (if (string-equal regexp "")
7955       (setq regexp (or gnus-last-search-regexp ""))
7956     (setq gnus-last-search-regexp regexp))
7957   (if (gnus-summary-search-article regexp backward)
7958       (gnus-article-set-window-start 
7959        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
7960     (error "Search failed: \"%s\"" regexp)))
7961
7962 (defun gnus-summary-search-article-backward (regexp)
7963   "Search for an article containing REGEXP backward."
7964   (interactive
7965    (list (read-string
7966           (format "Search article backward (regexp%s): "
7967                   (if gnus-last-search-regexp
7968                       (concat ", default " gnus-last-search-regexp)
7969                     "")))))
7970   (gnus-summary-search-article-forward regexp 'backward))
7971
7972 (defun gnus-summary-search-article (regexp &optional backward)
7973   "Search for an article containing REGEXP.
7974 Optional argument BACKWARD means do search for backward.
7975 gnus-select-article-hook is not called during the search."
7976   (let ((gnus-select-article-hook nil)  ;Disable hook.
7977         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7978         (re-search
7979          (if backward
7980              (function re-search-backward) (function re-search-forward)))
7981         (found nil)
7982         (last nil))
7983     ;; Hidden thread subtrees must be searched for ,too.
7984     (gnus-summary-show-all-threads)
7985     (if (eobp) (forward-line -1))
7986     ;; First of all, search current article.
7987     ;; We don't want to read article again from NNTP server nor reset
7988     ;; current point.
7989     (gnus-summary-select-article)
7990     (gnus-message 9 "Searching article: %d..." gnus-current-article)
7991     (setq last gnus-current-article)
7992     (gnus-eval-in-buffer-window gnus-article-buffer
7993       (save-restriction
7994         (widen)
7995         ;; Begin search from current point.
7996         (setq found (funcall re-search regexp nil t))))
7997     ;; Then search next articles.
7998     (while (and (not found)
7999                 (gnus-summary-display-article 
8000                  (gnus-summary-search-subject backward nil nil)))
8001       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8002       (gnus-eval-in-buffer-window gnus-article-buffer
8003         (save-restriction
8004           (widen)
8005           (goto-char (if backward (point-max) (point-min)))
8006           (setq found (funcall re-search regexp nil t)))))
8007     (message "")
8008     ;; Adjust article pointer.
8009     (or (eq last gnus-current-article)
8010         (setq gnus-last-article last))
8011     ;; Return T if found such article.
8012     found))
8013
8014 (defun gnus-summary-execute-command (header regexp command &optional backward)
8015   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8016 If HEADER is an empty string (or nil), the match is done on the entire
8017 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8018   (interactive
8019    (list (let ((completion-ignore-case t))
8020            (completing-read 
8021             "Header name: "
8022             (mapcar (lambda (string) (list string))
8023                     '("Number" "Subject" "From" "Lines" "Date"
8024                       "Message-ID" "Xref" "References"))
8025             nil 'require-match))
8026          (read-string "Regexp: ")
8027          (read-key-sequence "Command: ")
8028          current-prefix-arg))
8029   (gnus-set-global-variables)
8030   ;; Hidden thread subtrees must be searched as well.
8031   (gnus-summary-show-all-threads)
8032   ;; We don't want to change current point nor window configuration.
8033   (save-excursion
8034     (save-window-excursion
8035       (gnus-message 6 "Executing %s..." (key-description command))
8036       ;; We'd like to execute COMMAND interactively so as to give arguments.
8037       (gnus-execute header regexp
8038                     (` (lambda ()
8039                          (call-interactively '(, (key-binding command)))))
8040                     backward)
8041       (gnus-message 6 "Executing %s...done" (key-description command)))))
8042
8043 (defun gnus-summary-beginning-of-article ()
8044   "Scroll the article back to the beginning."
8045   (interactive)
8046   (gnus-set-global-variables)
8047   (gnus-summary-select-article)
8048   (gnus-configure-windows 'article)
8049   (gnus-eval-in-buffer-window
8050    gnus-article-buffer
8051    (widen)
8052    (goto-char (point-min))
8053    (and gnus-break-pages (gnus-narrow-to-page))))
8054
8055 (defun gnus-summary-end-of-article ()
8056   "Scroll to the end of the article."
8057   (interactive)
8058   (gnus-set-global-variables)
8059   (gnus-summary-select-article)
8060   (gnus-configure-windows 'article)
8061   (gnus-eval-in-buffer-window 
8062    gnus-article-buffer
8063    (widen)
8064    (goto-char (point-max))
8065    (and gnus-break-pages (gnus-narrow-to-page))))
8066
8067 (defun gnus-summary-show-article ()
8068   "Force re-fetching of the current article."
8069   (interactive)
8070   (gnus-set-global-variables)
8071   (gnus-summary-select-article gnus-have-all-headers t))
8072
8073 (defun gnus-summary-verbose-header (arg)
8074   "Toggle permanent full header display.
8075 If ARG is a positive number, turn header display on.
8076 If ARG is a negative number, turn header display off."
8077   (interactive "P")
8078   (gnus-set-global-variables)
8079   (gnus-summary-toggle-header arg)
8080   (setq gnus-have-all-headers
8081         (cond ((or (not (numberp arg))
8082                    (zerop arg))
8083                (not gnus-have-all-headers))
8084               ((natnump arg)
8085                t))))
8086
8087 (defun gnus-summary-toggle-header (arg)
8088   "Show the headers if they are hidden, or hide them if they are shown.
8089 If ARG is a positive number, show the entire header.
8090 If ARG is a negative number, hide the unwanted header lines."
8091   (interactive "P")
8092   (gnus-set-global-variables)
8093   (save-excursion
8094     (set-buffer gnus-article-buffer)
8095     (let ((buffer-read-only nil))
8096       (if (numberp arg) 
8097           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8098                                                 gnus-hidden-properties)
8099             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8100         (if (text-property-any (point-min) (point-max) 'invisible t)
8101             (remove-text-properties (point-min) (point-max)
8102                                     gnus-hidden-properties)
8103           (let ((gnus-have-all-headers nil))
8104             (run-hooks 'gnus-article-display-hook))))
8105       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8106
8107 (defun gnus-summary-show-all-headers ()
8108   "Make all header lines visible."
8109   (interactive)
8110   (gnus-set-global-variables)
8111   (gnus-article-show-all-headers))
8112
8113 (defun gnus-summary-toggle-mime (arg)
8114   "Toggle MIME processing.
8115 If ARG is a positive number, turn MIME processing on."
8116   (interactive "P")
8117   (gnus-set-global-variables)
8118   (setq gnus-show-mime
8119         (if (null arg) (not gnus-show-mime)
8120           (> (prefix-numeric-value arg) 0)))
8121   (gnus-summary-select-article t 'force))
8122
8123 (defun gnus-summary-caesar-message (rotnum)
8124   "Caesar rotates all letters of current message by 13/47 places.
8125 With prefix arg, specifies the number of places to rotate each letter forward.
8126 Caesar rotates Japanese letters by 47 places in any case."
8127   (interactive "P")
8128   (gnus-set-global-variables)
8129   (gnus-summary-select-article)
8130   (let ((mail-header-separator "")) ; !!! Is this necessary?
8131     (gnus-overload-functions)
8132     (gnus-eval-in-buffer-window 
8133      gnus-article-buffer
8134      (save-restriction
8135        (widen)
8136        ;; We don't want to jump to the beginning of the message.
8137        ;; `save-excursion' does not do its job.
8138        (move-to-window-line 0)
8139        (let ((last (point)))
8140          (news-caesar-buffer-body rotnum)
8141          (goto-char last)
8142          (recenter 0))))))
8143
8144 (defun gnus-summary-stop-page-breaking ()
8145   "Stop page breaking in the current article."
8146   (interactive)
8147   (gnus-set-global-variables)
8148   (gnus-summary-select-article)
8149   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8150
8151 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8152
8153 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8154   "Move the current article to a different newsgroup.
8155 If N is a positive number, move the N next articles.
8156 If N is a negative number, move the N previous articles.
8157 If N is nil and any articles have been marked with the process mark,
8158 move those articles instead.
8159 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8160 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8161 re-spool using this method.
8162 For this function to work, both the current newsgroup and the
8163 newsgroup that you want to move to have to support the `request-move'
8164 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8165   (interactive "P")
8166   (gnus-set-global-variables)
8167   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8168       (error "The current newsgroup does not support article moving"))
8169   (let ((articles (gnus-summary-work-articles n))
8170         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8171         art-group)
8172     (if (and (not to-newsgroup) (not select-method))
8173         (setq to-newsgroup
8174               (completing-read 
8175                (format "Where do you want to move %s? %s"
8176                        (if (> (length articles) 1)
8177                            (format "these %d articles" (length articles))
8178                          "this article")
8179                        (if gnus-current-move-group
8180                            (format "(%s default) " gnus-current-move-group)
8181                          ""))
8182                gnus-active-hashtb nil nil prefix)))
8183     (if to-newsgroup
8184         (progn
8185           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8186               (setq to-newsgroup (or gnus-current-move-group "")))
8187           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8188               (gnus-activate-newsgroup to-newsgroup)
8189               (error "No such group: %s" to-newsgroup))
8190           (setq gnus-current-move-group to-newsgroup)))
8191     (or (gnus-check-backend-function 'request-accept-article 
8192                                      (or select-method to-newsgroup))
8193         (error "%s does not support article moving" to-newsgroup))
8194     (gnus-message 6 "Moving to %s: %s..." 
8195                   (or select-method to-newsgroup) articles)
8196     (while articles
8197       (if (setq art-group
8198                 (gnus-request-move-article 
8199                  (car articles)                   ; Article to move
8200                  gnus-newsgroup-name              ; From newsgrouo
8201                  (nth 1 (gnus-find-method-for-group 
8202                          gnus-newsgroup-name))    ; Server
8203                  (list 'gnus-request-accept-article 
8204                        (if select-method
8205                            (list 'quote select-method)
8206                          to-newsgroup)
8207                        (not (cdr articles)))     ; Accept form
8208                  (not (cdr articles))))          ; Only save nov last time
8209           (let* ((buffer-read-only nil)
8210                  (entry 
8211                   (or
8212                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8213                    (gnus-gethash 
8214                     (gnus-group-prefixed-name 
8215                      (car art-group) 
8216                      (if select-method (list select-method "")
8217                        (gnus-find-method-for-group to-newsgroup)))
8218                     gnus-newsrc-hashtb)))
8219                  (info (nth 2 entry))
8220                  (article (car articles)))
8221             (gnus-summary-goto-subject article)
8222             (beginning-of-line)
8223             (delete-region (point)
8224                            (progn (forward-line 1) (point)))
8225             (if (not (memq article gnus-newsgroup-unreads))
8226                 (setcar (cdr (cdr info))
8227                         (gnus-add-to-range (nth 2 info) 
8228                                            (list (cdr art-group)))))
8229             ;; Copy any marks over to the new group.
8230             (let ((marks '((tick . gnus-newsgroup-marked)
8231                            (dormant . gnus-newsgroup-dormant)
8232                            (expire . gnus-newsgroup-expirable)
8233                            (bookmark . gnus-newsgroup-bookmarks)
8234                         ;   (score . gnus-newsgroup-scored)
8235                            (reply . gnus-newsgroup-replied)))
8236                   (to-article (cdr art-group)))
8237               (while marks
8238                 (if (memq article (symbol-value (cdr (car marks))))
8239                     (gnus-add-marked-articles 
8240                      (car info) (car (car marks)) (list to-article) info))
8241                 (setq marks (cdr marks))))
8242             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8243             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8244             (setq gnus-newsgroup-dormant
8245                   (delq article gnus-newsgroup-dormant)))
8246         (gnus-message 1 "Couldn't move article %s" (car articles)))
8247       (gnus-summary-remove-process-mark (car articles))
8248       (setq articles (cdr articles)))))
8249
8250 (defun gnus-summary-respool-article (n &optional respool-method)
8251   "Respool the current article.
8252 The article will be squeezed through the mail spooling process again,
8253 which means that it will be put in some mail newsgroup or other
8254 depending on `nnmail-split-methods'.
8255 If N is a positive number, respool the N next articles.
8256 If N is a negative number, respool the N previous articles.
8257 If N is nil and any articles have been marked with the process mark,
8258 respool those articles instead.
8259
8260 Respooling can be done both from mail groups and \"real\" newsgroups.
8261 In the former case, the articles in question will be moved from the
8262 current group into whatever groups they are destined to.  In the
8263 latter case, they will be copied into the relevant groups."
8264   (interactive "P")
8265   (gnus-set-global-variables)
8266   (let ((respool-methods (gnus-methods-using 'respool))
8267         (methname 
8268          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8269     (or respool-method
8270         (setq respool-method
8271               (completing-read
8272                "What method do you want to use when respooling? "
8273                respool-methods nil t methname)))
8274     (or (string= respool-method "")
8275         (if (assoc (symbol-name
8276                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8277                    respool-methods)
8278             (gnus-summary-move-article n nil (intern respool-method))
8279           (gnus-summary-copy-article n nil (intern respool-method))))))
8280
8281 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8282 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8283   "Move the current article to a different newsgroup.
8284 If N is a positive number, move the N next articles.
8285 If N is a negative number, move the N previous articles.
8286 If N is nil and any articles have been marked with the process mark,
8287 move those articles instead.
8288 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8289 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8290 re-spool using this method.
8291 For this function to work, the newsgroup that you want to move to have
8292 to support the `request-move' and `request-accept'
8293 functions. (Ie. mail newsgroups at present.)"
8294   (interactive "P")
8295   (gnus-set-global-variables)
8296   (let ((articles (gnus-summary-work-articles n))
8297         (copy-buf (get-buffer-create "*copy work*"))
8298         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8299         art-group)
8300     (buffer-disable-undo copy-buf)
8301     (if (and (not to-newsgroup) (not select-method))
8302         (setq to-newsgroup
8303               (completing-read 
8304                (format "Where do you want to copy %s? %s"
8305                        (if (> (length articles) 1)
8306                            (format "these %d articles" (length articles))
8307                          "this article")
8308                        (if gnus-current-move-group
8309                            (format "(%s default) " gnus-current-move-group)
8310                          ""))
8311                gnus-active-hashtb nil nil prefix)))
8312     (if to-newsgroup
8313         (progn
8314           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8315               (setq to-newsgroup (or gnus-current-move-group "")))
8316           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8317               (gnus-activate-newsgroup to-newsgroup)
8318               (error "No such group: %s" to-newsgroup))
8319           (setq gnus-current-move-group to-newsgroup)))
8320     (or (gnus-check-backend-function 'request-accept-article 
8321                                      (or select-method to-newsgroup))
8322         (error "%s does not support article copying" to-newsgroup))
8323     (gnus-message 6 "Copying to %s: %s..." 
8324                   (or select-method to-newsgroup) articles)
8325     (while articles
8326       (if (setq art-group
8327                 (save-excursion
8328                   (set-buffer copy-buf)
8329                   (gnus-request-article-this-buffer
8330                    (car articles) gnus-newsgroup-name)
8331                   (gnus-request-accept-article
8332                    (if select-method (quote select-method) to-newsgroup)
8333                    (not (cdr articles)))))
8334           (let* ((entry 
8335                   (or
8336                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8337                    (gnus-gethash 
8338                     (gnus-group-prefixed-name 
8339                      (car art-group) 
8340                      (if select-method (list select-method "")
8341                        (gnus-find-method-for-group to-newsgroup)))
8342                     gnus-newsrc-hashtb)))
8343                  (info (nth 2 entry))
8344                  (article (car articles)))
8345             (if (not (memq article gnus-newsgroup-unreads))
8346                 (setcar (cdr (cdr info))
8347                         (gnus-add-to-range (nth 2 info) 
8348                                            (list (cdr art-group)))))
8349             ;; Copy any marks over to the new group.
8350             (let ((marks '((tick . gnus-newsgroup-marked)
8351                            (dormant . gnus-newsgroup-dormant)
8352                            (expire . gnus-newsgroup-expirable)
8353                            (bookmark . gnus-newsgroup-bookmarks)
8354                         ;   (score . gnus-newsgroup-scored)
8355                            (reply . gnus-newsgroup-replied)))
8356                   (to-article (cdr art-group)))
8357               (while marks
8358                 (if (memq article (symbol-value (cdr (car marks))))
8359                     (gnus-add-marked-articles 
8360                      (car info) (car (car marks)) (list to-article) info))
8361                 (setq marks (cdr marks)))))
8362         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8363       (gnus-summary-remove-process-mark (car articles))
8364       (setq articles (cdr articles)))
8365     (kill-buffer copy-buf)))
8366
8367 (defun gnus-summary-import-article (file)
8368   "Import a random file into a mail newsgroup."
8369   (interactive "fImport file: ")
8370   (let ((group gnus-newsgroup-name)
8371         atts)
8372     (or (gnus-check-backend-function 'request-accept-article group)
8373         (error "%s does not support article importing" group))
8374     (or (file-readable-p file)
8375         (not (file-regular-p file))
8376         (error "Can't read %s" file))
8377     (save-excursion
8378       (set-buffer (get-buffer-create " *import file*"))
8379       (buffer-disable-undo (current-buffer))
8380       (erase-buffer)
8381       (insert-file-contents file)
8382       (goto-char (point-min))
8383       (if (nnheader-article-p)
8384           ()
8385         (setq atts (file-attributes file))
8386         (insert "From: " (read-string "From: ") "\n"
8387                 "Subject: " (read-string "Subject: ") "\n"
8388                 "Date: " (current-time-string (nth 5 atts)) "\n"
8389                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8390       (gnus-request-accept-article group t)
8391       (kill-buffer (current-buffer)))))
8392
8393 (defun gnus-summary-expire-articles ()
8394   "Expire all articles that are marked as expirable in the current group."
8395   (interactive)
8396   (if (and gnus-newsgroup-expirable
8397            (gnus-check-backend-function 
8398             'request-expire-articles gnus-newsgroup-name))
8399       (let ((expirable gnus-newsgroup-expirable))
8400         ;; The list of articles that weren't expired is returned.
8401         (setq gnus-newsgroup-expirable 
8402               (gnus-request-expire-articles gnus-newsgroup-expirable
8403                                             gnus-newsgroup-name))
8404         ;; We go through the old list of expirable, and mark all
8405         ;; really expired articles as non-existent.
8406         (while expirable
8407           (or (memq (car expirable) gnus-newsgroup-expirable)
8408               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8409           (setq expirable (cdr expirable))))))
8410
8411 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8412 (defun gnus-summary-delete-article (n)
8413   "Delete the N next (mail) articles.
8414 This command actually deletes articles. This is not a marking
8415 command. The article will disappear forever from you life, never to
8416 return. 
8417 If N is negative, delete backwards.
8418 If N is nil and articles have been marked with the process mark,
8419 delete these instead."
8420   (interactive "P")
8421   (or (gnus-check-backend-function 'request-expire-articles 
8422                                    gnus-newsgroup-name)
8423       (error "The current newsgroup does not support article deletion."))
8424   ;; Compute the list of articles to delete.
8425   (let ((articles (gnus-summary-work-articles n))
8426         not-deleted)
8427     (if (and gnus-novice-user
8428              (not (gnus-y-or-n-p 
8429                    (format "Do you really want to delete %s forever? "
8430                            (if (> (length articles) 1) "these articles"
8431                              "this article")))))
8432         ()
8433       ;; Delete the articles.
8434       (setq not-deleted (gnus-request-expire-articles 
8435                          articles gnus-newsgroup-name 'force))
8436       (while articles
8437         (gnus-summary-remove-process-mark (car articles))       
8438         ;; The backend might not have been able to delete the article
8439         ;; after all.  
8440         (or (memq (car articles) not-deleted)
8441             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8442         (setq articles (cdr articles))))
8443     (gnus-summary-position-cursor)
8444     not-deleted))
8445
8446 (defun gnus-summary-edit-article ()
8447   "Enter into a buffer and edit the current article.
8448 This will have permanent effect only in mail groups."
8449   (interactive)
8450   (or (gnus-check-backend-function 
8451        'request-replace-article gnus-newsgroup-name)
8452       (error "The current newsgroup does not support article editing."))
8453   (gnus-summary-select-article t)
8454   (other-window 1)
8455   (gnus-message 6 "C-c C-c to end edits")
8456   (setq buffer-read-only nil)
8457   (text-mode)
8458   (use-local-map (copy-keymap (current-local-map)))
8459   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8460   (buffer-enable-undo)
8461   (goto-char (point-min))
8462   (search-forward "\n\n" nil t))
8463
8464 (defun gnus-summary-edit-article-done ()
8465   "Make edits to the current article permanent."
8466   (interactive)
8467   (if (not (gnus-request-replace-article 
8468             (cdr gnus-article-current) (car gnus-article-current) 
8469             (current-buffer)))
8470       (error "Couldn't replace article.")
8471     (gnus-article-mode)
8472     (use-local-map gnus-article-mode-map)
8473     (setq buffer-read-only t)
8474     (buffer-disable-undo (current-buffer))
8475     (pop-to-buffer gnus-summary-buffer)))      
8476
8477 (defun gnus-summary-fancy-query ()
8478   "Query where the fancy respool algorithm would put this article."
8479   (interactive)
8480   (gnus-summary-select-article)
8481   (save-excursion
8482     (set-buffer gnus-article-buffer)
8483     (save-restriction
8484       (goto-char (point-min))
8485       (search-forward "\n\n")
8486       (narrow-to-region (point-min) (point))
8487       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8488
8489 ;; Summary score commands.
8490
8491 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8492
8493 (defun gnus-summary-raise-score (n)
8494   "Raise the score of the current article by N."
8495   (interactive "p")
8496   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8497
8498 (defun gnus-summary-lower-score (n)
8499   "Lower the score of the current article by N."
8500   (interactive "p")
8501   (gnus-summary-raise-score (- n)))
8502
8503 (defun gnus-summary-set-score (n)
8504   "Set the score of the current article to N."
8505   (interactive "p")
8506   ;; Skip dummy header line.
8507   (save-excursion
8508     (gnus-summary-show-thread)
8509     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8510         (forward-line 1))
8511     (let ((buffer-read-only nil))
8512       ;; Set score.
8513       (gnus-summary-update-mark
8514        (if (= n (or gnus-summary-default-score 0)) ? 
8515          (if (< n (or gnus-summary-default-score 0)) 
8516              gnus-score-below-mark gnus-score-over-mark)) 'score))
8517     (let* ((article (gnus-summary-article-number))
8518            (score (assq article gnus-newsgroup-scored)))
8519       (if score (setcdr score n)
8520         (setq gnus-newsgroup-scored 
8521               (cons (cons article n) gnus-newsgroup-scored))))
8522     (gnus-summary-update-line)))
8523
8524 (defun gnus-summary-current-score ()
8525   "Return the score of the current article."
8526   (interactive)
8527   (message "%s" (gnus-summary-article-score)))
8528
8529 ;; Summary marking commands.
8530
8531 (defun gnus-summary-raise-same-subject-and-select (score)
8532   "Raise articles which has the same subject with SCORE and select the next."
8533   (interactive "p")
8534   (let ((subject (gnus-summary-subject-string)))
8535     (gnus-summary-raise-score score)
8536     (while (gnus-summary-search-subject nil nil subject)
8537       (gnus-summary-raise-score score))
8538     (gnus-summary-next-article t)))
8539
8540 (defun gnus-summary-raise-same-subject (score)
8541   "Raise articles which has the same subject with SCORE."
8542   (interactive "p")
8543   (let ((subject (gnus-summary-subject-string)))
8544     (gnus-summary-raise-score score)
8545     (while (gnus-summary-search-subject nil nil subject)
8546       (gnus-summary-raise-score score))
8547     (gnus-summary-next-subject 1 t)))
8548
8549 (defun gnus-score-default (level)
8550   (if level (prefix-numeric-value level) 
8551     gnus-score-interactive-default-score))
8552
8553 (defun gnus-summary-raise-thread (score)
8554   "Raise the score of the articles in the current thread with SCORE."
8555   (interactive "P")
8556   (setq score (1- (gnus-score-default score)))
8557   (let (e)
8558     (save-excursion
8559       (let ((level (gnus-summary-thread-level)))
8560         (gnus-summary-raise-score score)
8561         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8562                     (> (gnus-summary-thread-level) level))
8563           (gnus-summary-raise-score score))
8564         (setq e (point))))
8565     (let ((gnus-summary-check-current t))
8566       (or (zerop (gnus-summary-next-subject 1 t))
8567           (goto-char e))))
8568   (gnus-summary-recenter)
8569   (gnus-summary-position-cursor)
8570   (gnus-set-mode-line 'summary))
8571
8572 (defun gnus-summary-lower-same-subject-and-select (score)
8573   "Raise articles which has the same subject with SCORE and select the next."
8574   (interactive "p")
8575   (gnus-summary-raise-same-subject-and-select (- score)))
8576
8577 (defun gnus-summary-lower-same-subject (score)
8578   "Raise articles which has the same subject with SCORE."
8579   (interactive "p")
8580   (gnus-summary-raise-same-subject (- score)))
8581
8582 (defun gnus-summary-lower-thread (score)
8583   "Lower score of articles in the current thread with SCORE."
8584   (interactive "P")
8585   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8586
8587 (defun gnus-summary-kill-same-subject-and-select (unmark)
8588   "Mark articles which has the same subject as read, and then select the next.
8589 If UNMARK is positive, remove any kind of mark.
8590 If UNMARK is negative, tick articles."
8591   (interactive "P")
8592   (if unmark
8593       (setq unmark (prefix-numeric-value unmark)))
8594   (let ((count
8595          (gnus-summary-mark-same-subject
8596           (gnus-summary-subject-string) unmark)))
8597     ;; Select next unread article. If auto-select-same mode, should
8598     ;; select the first unread article.
8599     (gnus-summary-next-article t (and gnus-auto-select-same
8600                                       (gnus-summary-subject-string)))
8601     (gnus-message 7 "%d articles are marked as %s"
8602                   count (if unmark "unread" "read"))))
8603
8604 (defun gnus-summary-kill-same-subject (unmark)
8605   "Mark articles which has the same subject as read. 
8606 If UNMARK is positive, remove any kind of mark.
8607 If UNMARK is negative, tick articles."
8608   (interactive "P")
8609   (if unmark
8610       (setq unmark (prefix-numeric-value unmark)))
8611   (let ((count
8612          (gnus-summary-mark-same-subject
8613           (gnus-summary-subject-string) unmark)))
8614     ;; If marked as read, go to next unread subject.
8615     (if (null unmark)
8616         ;; Go to next unread subject.
8617         (gnus-summary-next-subject 1 t))
8618     (gnus-message 7 "%d articles are marked as %s"
8619                   count (if unmark "unread" "read"))))
8620
8621 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8622   "Mark articles with same SUBJECT as read, and return marked number.
8623 If optional argument UNMARK is positive, remove any kinds of marks.
8624 If optional argument UNMARK is negative, mark articles as unread instead."
8625   (let ((count 1))
8626     (save-excursion
8627       (cond ((null unmark)
8628              (gnus-summary-mark-as-read nil gnus-killed-mark))
8629             ((> unmark 0)
8630              (gnus-summary-tick-article nil t))
8631             (t
8632              (gnus-summary-tick-article)))
8633       (while (and subject
8634                   (gnus-summary-search-forward nil subject))
8635         (cond ((null unmark)
8636                (gnus-summary-mark-as-read nil gnus-killed-mark))
8637               ((> unmark 0)
8638                (gnus-summary-tick-article nil t))
8639               (t
8640                (gnus-summary-tick-article)))
8641         (setq count (1+ count))))
8642     ;; Hide killed thread subtrees.  Does not work properly always.
8643     ;;(and (null unmark)
8644     ;;     gnus-thread-hide-killed
8645     ;;     (gnus-summary-hide-thread))
8646     ;; Return number of articles marked as read.
8647     count))
8648
8649 (defun gnus-summary-mark-as-processable (n &optional unmark)
8650   "Set the process mark on the next N articles.
8651 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8652 the process mark instead.  The difference between N and the actual
8653 number of articles marked is returned."
8654   (interactive "p")
8655   (let ((backward (< n 0))
8656         (n (abs n)))
8657   (while (and 
8658           (> n 0)
8659           (if unmark
8660               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8661             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8662           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8663     (setq n (1- n)))
8664   (if (/= 0 n) (gnus-message 7 "No more articles"))
8665   (gnus-summary-recenter)
8666   (gnus-summary-position-cursor)
8667   n))
8668
8669 (defun gnus-summary-unmark-as-processable (n)
8670   "Remove the process mark from the next N articles.
8671 If N is negative, mark backward instead.  The difference between N and
8672 the actual number of articles marked is returned."
8673   (interactive "p")
8674   (gnus-summary-mark-as-processable n t))
8675
8676 (defun gnus-summary-unmark-all-processable ()
8677   "Remove the process mark from all articles."
8678   (interactive)
8679   (save-excursion
8680     (while gnus-newsgroup-processable
8681       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8682   (gnus-summary-position-cursor))
8683
8684 (defun gnus-summary-mark-as-expirable (n)
8685   "Mark N articles forward as expirable.
8686 If N is negative, mark backward instead. The difference between N and
8687 the actual number of articles marked is returned."
8688   (interactive "p")
8689   (gnus-summary-mark-forward n gnus-expirable-mark))
8690
8691 (defun gnus-summary-mark-article-as-replied (article)
8692   "Mark ARTICLE replied and update the summary line."
8693   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8694   (let ((buffer-read-only nil))
8695     (if (gnus-summary-goto-subject article)
8696         (progn
8697           (gnus-summary-update-mark gnus-replied-mark 'replied)
8698           t))))
8699
8700 (defun gnus-summary-set-bookmark (article)
8701   "Set a bookmark in current article."
8702   (interactive (list (gnus-summary-article-number)))
8703   (if (or (not (get-buffer gnus-article-buffer))
8704           (not gnus-current-article)
8705           (not gnus-article-current)
8706           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8707       (error "No current article selected"))
8708   ;; Remove old bookmark, if one exists.
8709   (let ((old (assq article gnus-newsgroup-bookmarks)))
8710     (if old (setq gnus-newsgroup-bookmarks 
8711                   (delq old gnus-newsgroup-bookmarks))))
8712   ;; Set the new bookmark, which is on the form 
8713   ;; (article-number . line-number-in-body).
8714   (setq gnus-newsgroup-bookmarks 
8715         (cons 
8716          (cons article 
8717                (save-excursion
8718                  (set-buffer gnus-article-buffer)
8719                  (count-lines
8720                   (min (point)
8721                        (save-excursion
8722                          (goto-char (point-min))
8723                          (search-forward "\n\n" nil t)
8724                          (point)))
8725                   (point))))
8726          gnus-newsgroup-bookmarks))
8727   (gnus-message 6 "A bookmark has been added to the current article."))
8728
8729 (defun gnus-summary-remove-bookmark (article)
8730   "Remove the bookmark from the current article."
8731   (interactive (list (gnus-summary-article-number)))
8732   ;; Remove old bookmark, if one exists.
8733   (let ((old (assq article gnus-newsgroup-bookmarks)))
8734     (if old 
8735         (progn
8736           (setq gnus-newsgroup-bookmarks 
8737                 (delq old gnus-newsgroup-bookmarks))
8738           (gnus-message 6 "Removed bookmark."))
8739       (gnus-message 6 "No bookmark in current article."))))
8740
8741 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8742 (defun gnus-summary-mark-as-dormant (n)
8743   "Mark N articles forward as dormant.
8744 If N is negative, mark backward instead.  The difference between N and
8745 the actual number of articles marked is returned."
8746   (interactive "p")
8747   (gnus-summary-mark-forward n gnus-dormant-mark))
8748
8749 (defun gnus-summary-set-process-mark (article)
8750   "Set the process mark on ARTICLE and update the summary line."
8751   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8752   (let ((buffer-read-only nil))
8753     (if (gnus-summary-goto-subject article)
8754         (progn
8755           (gnus-summary-show-thread)
8756           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8757                (forward-line 1))
8758           (gnus-summary-update-mark gnus-process-mark 'replied)
8759           t))))
8760
8761 (defun gnus-summary-remove-process-mark (article)
8762   "Remove the process mark from ARTICLE and update the summary line."
8763   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8764   (let ((buffer-read-only nil))
8765     (if (gnus-summary-goto-subject article)
8766         (progn
8767           (gnus-summary-show-thread)
8768           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8769                (forward-line 1))
8770           (gnus-summary-update-mark ?  'replied)
8771           (if (memq article gnus-newsgroup-replied) 
8772               (gnus-summary-update-mark gnus-replied-mark 'replied))
8773           t))))
8774
8775 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8776   "Mark N articles as read forwards.
8777 If N is negative, mark backwards instead.
8778 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8779 marked as unread. 
8780 The difference between N and the actual number of articles marked is
8781 returned."
8782   (interactive "p")
8783   (gnus-set-global-variables)
8784   (let ((backward (< n 0))
8785         (n (abs n))
8786         (mark (or mark gnus-del-mark)))
8787   (while (and (> n 0)
8788               (gnus-summary-mark-article nil mark no-expire)
8789               (zerop (gnus-summary-next-subject 
8790                       (if backward -1 1) gnus-summary-goto-unread t)))
8791     (setq n (1- n)))
8792   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8793   (gnus-summary-recenter)
8794   (gnus-summary-position-cursor)
8795   (gnus-set-mode-line 'summary)
8796   n))
8797
8798 (defun gnus-summary-mark-article (&optional article mark no-expire)
8799   "Mark ARTICLE with MARK.
8800 MARK can be any character.
8801 Five MARK strings are reserved: ?  (unread), 
8802 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8803 If MARK is nil, then the default character ?D is used.
8804 If ARTICLE is nil, then the article on the current line will be
8805 marked." 
8806   ;; If no mark is given, then we check auto-expiring.
8807   (and (not no-expire)
8808        gnus-newsgroup-auto-expire 
8809        (or (not mark)
8810            (and (numberp mark) 
8811                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
8812                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
8813                     (= mark gnus-read-mark) (= mark gnus-ancient-mark))))
8814        (setq mark gnus-expirable-mark))
8815   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8816          (article (or article (gnus-summary-article-number))))
8817     (or article (error "No article on current line"))
8818     (if (or (= mark gnus-unread-mark) 
8819             (= mark gnus-ticked-mark) 
8820             (= mark gnus-dormant-mark))
8821         (gnus-mark-article-as-unread article mark)
8822       (gnus-mark-article-as-read article mark))
8823
8824     ;; See whether the article is to be put in the cache.
8825     (and gnus-use-cache
8826          (save-excursion
8827            (gnus-cache-possibly-enter-article 
8828             gnus-newsgroup-name article 
8829             (gnus-get-header-by-number article)
8830             (= mark gnus-ticked-mark)
8831             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8832
8833     (if (gnus-summary-goto-subject article)
8834         (let ((buffer-read-only nil))
8835           (gnus-summary-show-thread)
8836           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8837                (forward-line 1))
8838           ;; Fix the mark.
8839           (gnus-summary-update-mark mark 'unread)
8840           t))))
8841
8842 (defun gnus-summary-update-mark (mark type)
8843   (beginning-of-line)
8844   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8845         plist)
8846     (if (not forward)
8847         ()
8848       (forward-char forward)
8849       (setq plist (text-properties-at (point)))
8850       (delete-char 1)
8851       (insert mark)
8852       (and plist (add-text-properties (1- (point)) (point) plist))
8853       (and (eq type 'unread)
8854            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
8855       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8856   
8857 (defun gnus-mark-article-as-read (article &optional mark)
8858   "Enter ARTICLE in the pertinent lists and remove it from others."
8859   ;; Make the article expirable.
8860   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8861     (if (= mark gnus-expirable-mark)
8862         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8863       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8864     ;; Remove from unread and marked lists.
8865     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8866     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8867     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8868     ;; Possibly remove from cache, if that is used. 
8869     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
8870
8871 (defun gnus-mark-article-as-unread (article &optional mark)
8872   "Enter ARTICLE in the pertinent lists and remove it from others."
8873   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8874     ;; Add to unread list.
8875     (or (memq article gnus-newsgroup-unreads)
8876         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
8877     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
8878     ;; lists.  Otherwise, it must be added to the list.
8879     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8880     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8881     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8882     (if (= mark gnus-ticked-mark)
8883         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
8884     (if (= mark gnus-dormant-mark)
8885         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
8886
8887 (defalias 'gnus-summary-mark-as-unread-forward 
8888   'gnus-summary-tick-article-forward)
8889 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8890                'gnus-summary-tick-article-forward)
8891 (defun gnus-summary-tick-article-forward (n)
8892   "Tick N articles forwards.
8893 If N is negative, tick backwards instead.
8894 The difference between N and the number of articles ticked is returned."
8895   (interactive "p")
8896   (gnus-summary-mark-forward n gnus-ticked-mark))
8897
8898 (defalias 'gnus-summary-mark-as-unread-backward 
8899   'gnus-summary-tick-article-backward)
8900 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8901                'gnus-summary-tick-article-backward)
8902 (defun gnus-summary-tick-article-backward (n)
8903   "Tick N articles backwards.
8904 The difference between N and the number of articles ticked is returned."
8905   (interactive "p")
8906   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8907
8908 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8909 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8910 (defun gnus-summary-tick-article (&optional article clear-mark)
8911   "Mark current article as unread.
8912 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8913 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8914   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8915                                        gnus-ticked-mark)))
8916
8917 (defun gnus-summary-mark-as-read-forward (n)
8918   "Mark N articles as read forwards.
8919 If N is negative, mark backwards instead.
8920 The difference between N and the actual number of articles marked is
8921 returned."
8922   (interactive "p")
8923   (gnus-summary-mark-forward n gnus-del-mark t))
8924
8925 (defun gnus-summary-mark-as-read-backward (n)
8926   "Mark the N articles as read backwards.
8927 The difference between N and the actual number of articles marked is
8928 returned."
8929   (interactive "p")
8930   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8931
8932 (defun gnus-summary-mark-as-read (&optional article mark)
8933   "Mark current article as read.
8934 ARTICLE specifies the article to be marked as read.
8935 MARK specifies a string to be inserted at the beginning of the line."
8936   (gnus-summary-mark-article article mark))
8937
8938 (defun gnus-summary-clear-mark-forward (n)
8939   "Clear marks from N articles forward.
8940 If N is negative, clear backward instead.
8941 The difference between N and the number of marks cleared is returned."
8942   (interactive "p")
8943   (gnus-summary-mark-forward n gnus-unread-mark))
8944
8945 (defun gnus-summary-clear-mark-backward (n)
8946   "Clear marks from N articles backward.
8947 The difference between N and the number of marks cleared is returned."
8948   (interactive "p")
8949   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8950
8951 (defun gnus-summary-mark-unread-as-read ()
8952   "Intended to be used by `gnus-summary-mark-article-hook'."
8953   (or (memq gnus-current-article gnus-newsgroup-marked)
8954       (memq gnus-current-article gnus-newsgroup-dormant)
8955       (memq gnus-current-article gnus-newsgroup-expirable)
8956       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8957
8958 (defun gnus-summary-mark-region-as-read (point mark all)
8959   "Mark all unread articles between point and mark as read.
8960 If given a prefix, mark all articles between point and mark as read,
8961 even ticked and dormant ones."
8962   (interactive "r\nP")
8963   (save-excursion
8964     (goto-char point)
8965     (beginning-of-line)
8966     (while (and 
8967             (< (point) mark)
8968             (progn
8969               (and
8970                (or all
8971                    (and
8972                     (not (memq (gnus-summary-article-number)
8973                                gnus-newsgroup-marked))
8974                     (not (memq (gnus-summary-article-number)
8975                                gnus-newsgroup-dormant))))
8976                (gnus-summary-mark-article
8977                 (gnus-summary-article-number) gnus-del-mark))
8978               t)
8979             (zerop (forward-line 1))))))
8980
8981 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8982 (defalias 'gnus-summary-delete-marked-as-read 
8983   'gnus-summary-remove-lines-marked-as-read)
8984 (make-obsolete 'gnus-summary-delete-marked-as-read 
8985                'gnus-summary-remove-lines-marked-as-read)
8986 (defun gnus-summary-remove-lines-marked-as-read ()
8987   "Remove lines that are marked as read."
8988   (interactive)
8989   (gnus-summary-remove-lines-marked-with 
8990    (concat (mapconcat
8991             (lambda (char) (char-to-string (symbol-value char)))
8992             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
8993               gnus-killed-mark gnus-kill-file-mark
8994               gnus-low-score-mark gnus-expirable-mark)
8995             ""))))
8996
8997 (defalias 'gnus-summary-delete-marked-with 
8998   'gnus-summary-remove-lines-marked-with)
8999 (make-obsolete 'gnus-summary-delete-marked-with 
9000                'gnus-summary-remove-lines-marked-with)
9001 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9002 (defun gnus-summary-remove-lines-marked-with (marks)
9003   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9004   (interactive "sMarks: ")
9005   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9006   (gnus-set-global-variables)
9007   (let ((buffer-read-only nil)
9008         (marks (concat "^[" marks "]")))
9009     (goto-char (point-min))
9010     (if gnus-newsgroup-adaptive
9011         (gnus-score-remove-lines-adaptive marks)
9012       (while (re-search-forward marks nil t)
9013         (gnus-delete-line)))
9014     ;; If we use dummy roots, we have to do an additional sweep over
9015     ;; the buffer.
9016     (if (not (eq gnus-summary-make-false-root 'dummy))
9017         ()
9018       (goto-char (point-min))
9019       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9020       (while (re-search-forward marks nil t)
9021         (if (gnus-subject-equal
9022              (gnus-summary-subject-string)
9023              (progn
9024                (forward-line 1)
9025                (gnus-summary-subject-string)))
9026             ()
9027           (forward-line -1)
9028           (gnus-delete-line)))))
9029   (or (zerop (buffer-size))
9030       (if (eobp)
9031           (gnus-summary-prev-subject 1)
9032         (gnus-summary-position-cursor))))
9033
9034 (defun gnus-summary-expunge-below (score)
9035   "Remove articles with score less than SCORE."
9036   (interactive "P")
9037   (gnus-set-global-variables)
9038   (setq score (if score
9039                   (prefix-numeric-value score)
9040                 (or gnus-summary-default-score 0)))
9041   (save-excursion
9042     (set-buffer gnus-summary-buffer)
9043     (goto-char (point-min))
9044     (let ((buffer-read-only nil)
9045           beg)
9046       (while (not (eobp))
9047         (if (< (gnus-summary-article-score) score)
9048             (progn
9049               (setq beg (point))
9050               (forward-line 1)
9051               (delete-region beg (point)))
9052           (forward-line 1)))
9053       ;; Adjust point.
9054       (or (zerop (buffer-size))
9055           (if (eobp)
9056               (gnus-summary-prev-subject 1)
9057             (gnus-summary-position-cursor))))))
9058
9059 (defun gnus-summary-mark-below (score mark)
9060   "Mark articles with score less than SCORE with MARK."
9061   (interactive "P\ncMark: ")
9062   (gnus-set-global-variables)
9063   (setq score (if score
9064                   (prefix-numeric-value score)
9065                 (or gnus-summary-default-score 0)))
9066   (save-excursion
9067     (set-buffer gnus-summary-buffer)
9068     (goto-char (point-min))
9069     (while (not (eobp))
9070       (and (< (gnus-summary-article-score) score)
9071            (gnus-summary-mark-article nil mark))
9072       (forward-line 1))))
9073
9074 (defun gnus-summary-kill-below (score)
9075   "Mark articles with score below SCORE as read."
9076   (interactive "P")
9077   (gnus-summary-mark-below score gnus-killed-mark))
9078
9079 (defun gnus-summary-clear-above (score)
9080   "Clear all marks from articles with score above SCORE."
9081   (interactive "P")
9082   (gnus-summary-mark-above score gnus-unread-mark))
9083
9084 (defun gnus-summary-tick-above (score)
9085   "Tick all articles with score above SCORE."
9086   (interactive "P")
9087   (gnus-summary-mark-above score gnus-ticked-mark))
9088
9089 (defun gnus-summary-mark-above (score mark)
9090   "Mark articles with score over SCORE with MARK."
9091   (interactive "P\ncMark: ")
9092   (setq score (if score
9093                   (prefix-numeric-value score)
9094                 (or gnus-summary-default-score 0)))
9095   (save-excursion
9096     (set-buffer gnus-summary-buffer)
9097     (goto-char (point-min))
9098     (while (not (eobp))
9099       (if (> (gnus-summary-article-score) score)
9100           (progn
9101             (gnus-summary-mark-article nil mark)
9102             (forward-line 1))
9103         (forward-line 1)))))
9104
9105 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9106 (defun gnus-summary-show-all-expunged ()
9107   "Display all the hidden articles that were expunged for low scores."
9108   (interactive)
9109   (let ((buffer-read-only nil))
9110     (let ((scored gnus-newsgroup-scored)
9111           headers h)
9112       (while scored
9113         (or (gnus-summary-goto-subject (car (car scored)))
9114             (and (setq h (gnus-get-header-by-number (car (car scored))))
9115                  (< (cdr (car scored)) gnus-summary-expunge-below)
9116                  (setq headers (cons h headers))))
9117         (setq scored (cdr scored)))
9118       (or headers (error "No expunged articles hidden."))
9119       (goto-char (point-min))
9120       (save-excursion 
9121         (gnus-summary-update-lines 
9122          (point)
9123          (progn
9124            (gnus-summary-prepare-threads (nreverse headers) 0)
9125            (point)))))
9126     (goto-char (point-min))
9127     (gnus-summary-position-cursor)))
9128
9129 (defun gnus-summary-show-all-dormant ()
9130   "Display all the hidden articles that are marked as dormant."
9131   (interactive)
9132   (let ((buffer-read-only nil))
9133     (goto-char (point-min))
9134     (let ((dormant gnus-newsgroup-dormant)
9135           headers h)
9136       (while dormant
9137         (or (gnus-summary-goto-subject (car dormant))
9138             (and (setq h (gnus-get-header-by-number (car dormant)))
9139                  (setq headers (cons h headers))))
9140         (setq dormant (cdr dormant)))
9141       (or headers (error "No dormant articles hidden."))
9142       (save-excursion 
9143         (gnus-summary-update-lines 
9144          (point)
9145          (progn
9146            (gnus-summary-prepare-threads (nreverse headers) 0)
9147            (point)))))
9148     (goto-char (point-min))
9149     (gnus-summary-position-cursor)))
9150
9151 (defun gnus-summary-hide-all-dormant ()
9152   "Hide all dormant articles."
9153   (interactive)
9154   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9155   (gnus-summary-position-cursor))
9156
9157 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9158   "Mark all articles not marked as unread in this newsgroup as read.
9159 If prefix argument ALL is non-nil, all articles are marked as read.
9160 If QUIETLY is non-nil, no questions will be asked.
9161 If TO-HERE is non-nil, it should be a point in the buffer. All
9162 articles before this point will be marked as read.
9163 The number of articles marked as read is returned."
9164   (interactive "P")
9165   (prog1
9166       (if (or quietly
9167               (not gnus-interactive-catchup) ;Without confirmation?
9168               gnus-expert-user
9169               (gnus-y-or-n-p
9170                (if all
9171                    "Mark absolutely all articles as read? "
9172                  "Mark all unread articles as read? ")))
9173           (if (and not-mark 
9174                    (not gnus-newsgroup-adaptive)
9175                    (not gnus-newsgroup-auto-expire))
9176               (progn
9177                 (and all (setq gnus-newsgroup-marked nil
9178                                gnus-newsgroup-dormant nil))
9179                 (setq gnus-newsgroup-unreads 
9180                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9181             ;; We actually mark all articles as canceled, which we
9182             ;; have to do when using auto-expiry or adaptive scoring. 
9183             (let ((unreads (length gnus-newsgroup-unreads)))
9184               (if (gnus-summary-first-subject (not all))
9185                   (while (and (if to-here (< (point) to-here) t)
9186                               (gnus-summary-mark-as-read nil gnus-catchup-mark)
9187                               (gnus-summary-search-subject nil (not all)))))
9188               (- unreads (length gnus-newsgroup-unreads))
9189               (or to-here
9190                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9191     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9192       (if (and (not to-here) (eq 'nnvirtual (car method)))
9193           (nnvirtual-catchup-group
9194            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9195     (gnus-summary-position-cursor)))
9196
9197 (defun gnus-summary-catchup-to-here (&optional all)
9198   "Mark all unticked articles before the current one as read.
9199 If ALL is non-nil, also mark ticked and dormant articles as read."
9200   (interactive)
9201   (save-excursion
9202     (end-of-line)
9203     (gnus-summary-catchup all t (point))
9204     (gnus-set-mode-line 'summary))
9205   (gnus-summary-position-cursor))
9206
9207 (defun gnus-summary-catchup-all (&optional quietly)
9208   "Mark all articles in this newsgroup as read."
9209   (interactive)
9210   (gnus-summary-catchup t quietly))
9211
9212 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9213   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9214 If prefix argument ALL is non-nil, all articles are marked as read."
9215   (interactive "P")
9216   (gnus-summary-catchup all quietly nil 'fast)
9217   ;; Select next newsgroup or exit.
9218   (if (eq gnus-auto-select-next 'quietly)
9219       (gnus-summary-next-group nil)
9220     (gnus-summary-exit)))
9221
9222 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9223   "Mark all articles in this newsgroup as read, and then exit."
9224   (interactive)
9225   (gnus-summary-catchup-and-exit t quietly))
9226
9227 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9228 (defun gnus-summary-catchup-and-goto-next-group (all)
9229   "Mark all articles in this group as read and select the next group.
9230 If given a prefix, mark all articles, unread as well as ticked, as
9231 read." 
9232   (interactive "P")
9233   (gnus-summary-catchup all)
9234   (gnus-summary-next-group))
9235
9236 ;; Thread-based commands.
9237
9238 (defun gnus-summary-toggle-threads (arg)
9239   "Toggle showing conversation threads.
9240 If ARG is positive number, turn showing conversation threads on."
9241   (interactive "P")
9242   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9243     (setq gnus-show-threads
9244           (if (null arg) (not gnus-show-threads)
9245             (> (prefix-numeric-value arg) 0)))
9246     (gnus-summary-prepare)
9247     (gnus-summary-goto-subject current)
9248     (gnus-summary-position-cursor)))
9249
9250 (defun gnus-summary-show-all-threads ()
9251   "Show all threads."
9252   (interactive)
9253   (save-excursion
9254     (let ((buffer-read-only nil))
9255       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9256   (gnus-summary-position-cursor))
9257
9258 (defun gnus-summary-show-thread ()
9259   "Show thread subtrees.
9260 Returns nil if no thread was there to be shown."
9261   (interactive)
9262   (prog1
9263       (save-excursion
9264         (let ((buffer-read-only nil)
9265               ;; first goto end then to beg, to have point at beg after let
9266               (end (progn (end-of-line) (point)))
9267               (beg (progn (beginning-of-line) (point))))
9268           (prog1
9269               ;; Any hidden lines here?
9270               (search-forward "\r" end t)
9271             (subst-char-in-region beg end ?\^M ?\n t))))
9272     (gnus-summary-position-cursor)))
9273
9274 (defun gnus-summary-hide-all-threads ()
9275   "Hide all thread subtrees."
9276   (interactive)
9277   (save-excursion
9278     (goto-char (point-min))
9279     (gnus-summary-hide-thread)
9280     (while (and (not (eobp)) (zerop (forward-line 1)))
9281       (gnus-summary-hide-thread)))
9282   (gnus-summary-position-cursor))
9283
9284 (defun gnus-summary-hide-thread ()
9285   "Hide thread subtrees.
9286 Returns nil if no threads were there to be hidden."
9287   (interactive)
9288   (let ((buffer-read-only nil)
9289         (start (point))
9290         (level (gnus-summary-thread-level))
9291         (end (point)))
9292     ;; Go forward until either the buffer ends or the subthread
9293     ;; ends. 
9294     (if (eobp)
9295         ()
9296       (while (and (zerop (forward-line 1))
9297                   (> (gnus-summary-thread-level) level))
9298         (setq end (point)))
9299       (prog1
9300           (save-excursion
9301             (goto-char end)
9302             (search-backward "\n" start t))
9303         (subst-char-in-region start end ?\n ?\^M t)
9304         (forward-line -1)))))
9305
9306 (defun gnus-summary-go-to-next-thread (&optional previous)
9307   "Go to the same level (or less) next thread.
9308 If PREVIOUS is non-nil, go to previous thread instead.
9309 Return the article number moved to, or nil if moving was impossible."
9310   (let ((level (gnus-summary-thread-level))
9311         (article (gnus-summary-article-number)))
9312     (if previous 
9313         (while (and (zerop (forward-line -1))
9314                     (> (gnus-summary-thread-level) level)))
9315       (while (and (save-excursion
9316                     (forward-line 1)
9317                     (not (eobp)))
9318                   (zerop (forward-line 1))
9319                   (> (gnus-summary-thread-level) level))))
9320     (gnus-summary-recenter)
9321     (gnus-summary-position-cursor)
9322     (let ((oart (gnus-summary-article-number)))
9323       (and (/= oart article) oart))))
9324
9325 (defun gnus-summary-next-thread (n)
9326   "Go to the same level next N'th thread.
9327 If N is negative, search backward instead.
9328 Returns the difference between N and the number of skips actually
9329 done."
9330   (interactive "p")
9331   (let ((backward (< n 0))
9332         (n (abs n)))
9333   (while (and (> n 0)
9334               (gnus-summary-go-to-next-thread backward))
9335     (setq n (1- n)))
9336   (gnus-summary-position-cursor)
9337   (if (/= 0 n) (gnus-message 7 "No more threads"))
9338   n))
9339
9340 (defun gnus-summary-prev-thread (n)
9341   "Go to the same level previous N'th thread.
9342 Returns the difference between N and the number of skips actually
9343 done."
9344   (interactive "p")
9345   (gnus-summary-next-thread (- n)))
9346
9347 (defun gnus-summary-go-down-thread (&optional same)
9348   "Go down one level in the current thread.
9349 If SAME is non-nil, also move to articles of the same level."
9350   (let ((level (gnus-summary-thread-level))
9351         (start (point)))
9352     (if (and (zerop (forward-line 1))
9353              (> (gnus-summary-thread-level) level))
9354         t
9355       (goto-char start)
9356       nil)))
9357
9358 (defun gnus-summary-go-up-thread ()
9359   "Go up one level in the current thread."
9360   (let ((level (gnus-summary-thread-level))
9361         (start (point)))
9362     (while (and (zerop (forward-line -1))
9363                 (>= (gnus-summary-thread-level) level)))
9364     (if (>= (gnus-summary-thread-level) level)
9365         (progn
9366           (goto-char start)
9367           nil)
9368       t)))
9369
9370 (defun gnus-summary-down-thread (n)
9371   "Go down thread N steps.
9372 If N is negative, go up instead.
9373 Returns the difference between N and how many steps down that were
9374 taken."
9375   (interactive "p")
9376   (let ((up (< n 0))
9377         (n (abs n)))
9378   (while (and (> n 0)
9379               (if up (gnus-summary-go-up-thread)
9380                 (gnus-summary-go-down-thread)))
9381     (setq n (1- n)))
9382   (gnus-summary-position-cursor)
9383   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9384   n))
9385
9386 (defun gnus-summary-up-thread (n)
9387   "Go up thread N steps.
9388 If N is negative, go up instead.
9389 Returns the difference between N and how many steps down that were
9390 taken."
9391   (interactive "p")
9392   (gnus-summary-down-thread (- n)))
9393
9394 (defun gnus-summary-kill-thread (unmark)
9395   "Mark articles under current thread as read.
9396 If the prefix argument is positive, remove any kinds of marks.
9397 If the prefix argument is negative, tick articles instead."
9398   (interactive "P")
9399   (if unmark
9400       (setq unmark (prefix-numeric-value unmark)))
9401   (let ((killing t)
9402         (level (gnus-summary-thread-level)))
9403     (save-excursion
9404       (while killing
9405         ;; Mark the article...
9406         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9407               ((> unmark 0) (gnus-summary-tick-article nil t))
9408               (t (gnus-summary-tick-article)))
9409         ;; ...and go forward until either the buffer ends or the subtree
9410         ;; ends. 
9411         (if (not (and (zerop (forward-line 1))
9412                       (> (gnus-summary-thread-level) level)))
9413             (setq killing nil))))
9414     ;; Hide killed subtrees.
9415     (and (null unmark)
9416          gnus-thread-hide-killed
9417          (gnus-summary-hide-thread))
9418     ;; If marked as read, go to next unread subject.
9419     (if (null unmark)
9420         ;; Go to next unread subject.
9421         (gnus-summary-next-subject 1 t)))
9422   (gnus-set-mode-line 'summary))
9423
9424 ;; Summary sorting commands
9425
9426 (defun gnus-summary-sort-by-number (&optional reverse)
9427   "Sort summary buffer by article number.
9428 Argument REVERSE means reverse order."
9429   (interactive "P")
9430   (gnus-summary-sort 
9431    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9432
9433 (defun gnus-summary-sort-by-author (&optional reverse)
9434   "Sort summary buffer by author name alphabetically.
9435 If case-fold-search is non-nil, case of letters is ignored.
9436 Argument REVERSE means reverse order."
9437   (interactive "P")
9438   (gnus-summary-sort
9439    (cons
9440     (lambda ()
9441       (let ((extract (funcall
9442                       gnus-extract-address-components
9443                       (header-from (gnus-get-header-by-number
9444                                     (gnus-summary-article-number))))))
9445         (or (car extract) (cdr extract))))
9446     'gnus-thread-sort-by-author)
9447    reverse))
9448
9449 (defun gnus-summary-sort-by-subject (&optional reverse)
9450   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9451 If case-fold-search is non-nil, case of letters is ignored.
9452 Argument REVERSE means reverse order."
9453   (interactive "P")
9454   (gnus-summary-sort
9455    (cons
9456     (lambda ()
9457       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9458     'gnus-thread-sort-by-subject)
9459    reverse))
9460
9461 (defun gnus-summary-sort-by-date (&optional reverse)
9462   "Sort summary buffer by date.
9463 Argument REVERSE means reverse order."
9464   (interactive "P")
9465   (gnus-summary-sort
9466    (cons
9467     (lambda ()
9468       (gnus-sortable-date
9469        (header-date 
9470         (gnus-get-header-by-number (gnus-summary-article-number)))))
9471     'gnus-thread-sort-by-date)
9472    reverse))
9473
9474 (defun gnus-summary-sort-by-score (&optional reverse)
9475   "Sort summary buffer by score.
9476 Argument REVERSE means reverse order."
9477   (interactive "P")
9478   (gnus-summary-sort 
9479    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9480    (not reverse)))
9481
9482 (defvar gnus-summary-already-sorted nil)
9483 (defun gnus-summary-sort (predicate reverse)
9484   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9485   (if gnus-summary-already-sorted
9486       ()
9487     (let (buffer-read-only)
9488       (if (not gnus-show-threads)
9489           (progn
9490             (goto-char (point-min))
9491             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9492         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9493               (gnus-summary-prepare-hook nil)
9494               (gnus-summary-already-sorted nil))
9495           (gnus-summary-prepare)
9496           (and gnus-show-threads
9497                gnus-thread-hide-subtree
9498                (gnus-summary-hide-all-threads))
9499           ;; If in async mode, we send some info to the backend.
9500           (and gnus-newsgroup-async
9501                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9502                (gnus-request-asynchronous 
9503                 gnus-newsgroup-name
9504                 (if (and gnus-asynchronous-article-function
9505                          (fboundp gnus-asynchronous-article-function))
9506                     (funcall gnus-asynchronous-article-function
9507                              gnus-newsgroup-threads)))))))))
9508
9509   
9510 (defun gnus-sortable-date (date)
9511   "Make sortable string by string-lessp from DATE.
9512 Timezone package is used."
9513   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9514          (year   (aref date 0))
9515          (month  (aref date 1))
9516          (day    (aref date 2)))
9517     (timezone-make-sortable-date 
9518      year month day 
9519      (timezone-make-time-string
9520       (aref date 3) (aref date 4) (aref date 5)))))
9521
9522
9523 ;; Summary saving commands.
9524
9525 (defun gnus-summary-save-article (n)
9526   "Save the current article using the default saver function.
9527 If N is a positive number, save the N next articles.
9528 If N is a negative number, save the N previous articles.
9529 If N is nil and any articles have been marked with the process mark,
9530 save those articles instead.
9531 The variable `gnus-default-article-saver' specifies the saver function."
9532   (interactive "P")
9533   (let ((articles (gnus-summary-work-articles n)))
9534     (while articles
9535       (let ((header (gnus-get-header-by-number (car articles))))
9536         (if (vectorp header)
9537             (progn
9538               (gnus-summary-select-article t nil nil (car articles))
9539               (or gnus-save-all-headers
9540                   (gnus-article-hide-headers t))
9541               ;; Remove any X-Gnus lines.
9542               (save-excursion
9543                 (save-restriction
9544                   (set-buffer gnus-article-buffer)
9545                   (let ((buffer-read-only nil))
9546                     (goto-char (point-min))
9547                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9548                                                   (point-max)))
9549                     (while (re-search-forward "^X-Gnus" nil t)
9550                       (beginning-of-line)
9551                       (delete-region (point)
9552                                      (progn (forward-line 1) (point))))
9553                     (widen))))
9554               (save-excursion
9555                 (if gnus-default-article-saver
9556                     (funcall gnus-default-article-saver)
9557                   (error "No default saver is defined."))))
9558           (if (assq 'name header)
9559               (gnus-copy-file (cdr (assq 'name header)))
9560             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9561       (gnus-summary-remove-process-mark (car articles))
9562       (setq articles (cdr articles)))
9563     (gnus-summary-position-cursor)
9564     n))
9565
9566 (defun gnus-summary-pipe-output (arg)
9567   "Pipe the current article to a subprocess.
9568 If N is a positive number, pipe the N next articles.
9569 If N is a negative number, pipe the N previous articles.
9570 If N is nil and any articles have been marked with the process mark,
9571 pipe those articles instead."
9572   (interactive "P")
9573   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9574     (gnus-summary-save-article arg)))
9575
9576 (defun gnus-summary-save-article-mail (arg)
9577   "Append the current article to an mail file.
9578 If N is a positive number, save the N next articles.
9579 If N is a negative number, save the N previous articles.
9580 If N is nil and any articles have been marked with the process mark,
9581 save those articles instead."
9582   (interactive "P")
9583   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9584     (gnus-summary-save-article arg)))
9585
9586 (defun gnus-summary-save-article-rmail (arg)
9587   "Append the current article to an rmail file.
9588 If N is a positive number, save the N next articles.
9589 If N is a negative number, save the N previous articles.
9590 If N is nil and any articles have been marked with the process mark,
9591 save those articles instead."
9592   (interactive "P")
9593   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9594     (gnus-summary-save-article arg)))
9595
9596 (defun gnus-summary-save-article-file (arg)
9597   "Append the current article to a file.
9598 If N is a positive number, save the N next articles.
9599 If N is a negative number, save the N previous articles.
9600 If N is nil and any articles have been marked with the process mark,
9601 save those articles instead."
9602   (interactive "P")
9603   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9604     (gnus-summary-save-article arg)))
9605
9606 (defun gnus-read-save-file-name (prompt default-name)
9607   (let ((methods gnus-split-methods)
9608         split-name)
9609     (if (not gnus-split-methods)
9610         ()
9611       (save-excursion
9612         (set-buffer gnus-article-buffer)
9613         (gnus-narrow-to-headers)
9614         (while methods
9615           (goto-char (point-min))
9616           (and (condition-case () 
9617                    (re-search-forward (car (car methods)) nil t)
9618                  (error nil))
9619                (setq split-name (cons (nth 1 (car methods)) split-name)))
9620           (setq methods (cdr methods)))
9621         (widen)))
9622     (cond ((null split-name)
9623            (read-file-name
9624             (concat prompt " (default "
9625                     (file-name-nondirectory default-name) ") ")
9626             (file-name-directory default-name)
9627             default-name))
9628           ((= 1 (length split-name))
9629            (read-file-name
9630             (concat prompt " (default " (car split-name) ") ")
9631             gnus-article-save-directory
9632             (concat gnus-article-save-directory (car split-name))))
9633           (t
9634            (setq split-name (mapcar (lambda (el) (list el))
9635                                     (nreverse split-name)))
9636            (let ((result (completing-read 
9637                           (concat prompt " ")
9638                           split-name nil nil)))
9639              (concat gnus-article-save-directory
9640                      (if (string= result "")
9641                          (car (car split-name))
9642                        result)))))))
9643
9644 (defun gnus-summary-save-in-rmail (&optional filename)
9645   "Append this article to Rmail file.
9646 Optional argument FILENAME specifies file name.
9647 Directory to save to is default to `gnus-article-save-directory' which
9648 is initialized from the SAVEDIR environment variable."
9649   (interactive)
9650   (let ((default-name
9651           (funcall gnus-rmail-save-name gnus-newsgroup-name
9652                    gnus-current-headers gnus-newsgroup-last-rmail)))
9653     (or filename
9654         (setq filename (gnus-read-save-file-name 
9655                         "Save in rmail file:" default-name)))
9656     (gnus-make-directory (file-name-directory filename))
9657     (gnus-eval-in-buffer-window 
9658      gnus-article-buffer
9659      (save-excursion
9660        (save-restriction
9661          (widen)
9662          (gnus-output-to-rmail filename))))
9663     ;; Remember the directory name to save articles.
9664     (setq gnus-newsgroup-last-rmail filename)))
9665
9666 (defun gnus-summary-save-in-mail (&optional filename)
9667   "Append this article to Unix mail file.
9668 Optional argument FILENAME specifies file name.
9669 Directory to save to is default to `gnus-article-save-directory' which
9670 is initialized from the SAVEDIR environment variable."
9671   (interactive)
9672   (let ((default-name
9673           (funcall gnus-mail-save-name gnus-newsgroup-name
9674                    gnus-current-headers gnus-newsgroup-last-mail)))
9675     (or filename
9676         (setq filename (gnus-read-save-file-name 
9677                         "Save in Unix mail file:" default-name)))
9678     (setq filename
9679           (expand-file-name filename
9680                             (and default-name
9681                                  (file-name-directory default-name))))
9682     (gnus-make-directory (file-name-directory filename))
9683     (gnus-eval-in-buffer-window 
9684      gnus-article-buffer
9685      (save-excursion
9686        (save-restriction
9687          (widen)
9688          (if (and (file-readable-p filename) (rmail-file-p filename))
9689              (gnus-output-to-rmail filename)
9690            (rmail-output filename 1 t t)))))
9691     ;; Remember the directory name to save articles.
9692     (setq gnus-newsgroup-last-mail filename)))
9693
9694 (defun gnus-summary-save-in-file (&optional filename)
9695   "Append this article to file.
9696 Optional argument FILENAME specifies file name.
9697 Directory to save to is default to `gnus-article-save-directory' which
9698 is initialized from the SAVEDIR environment variable."
9699   (interactive)
9700   (let ((default-name
9701           (funcall gnus-file-save-name gnus-newsgroup-name
9702                    gnus-current-headers gnus-newsgroup-last-file)))
9703     (or filename
9704         (setq filename (gnus-read-save-file-name 
9705                         "Save in file:" default-name)))
9706     (gnus-make-directory (file-name-directory filename))
9707     (gnus-eval-in-buffer-window 
9708      gnus-article-buffer
9709      (save-excursion
9710        (save-restriction
9711          (widen)
9712          (gnus-output-to-file filename))))
9713     ;; Remember the directory name to save articles.
9714     (setq gnus-newsgroup-last-file filename)))
9715
9716 (defun gnus-summary-save-in-pipe (&optional command)
9717   "Pipe this article to subprocess."
9718   (interactive)
9719   (let ((command (read-string "Shell command on article: "
9720                               gnus-last-shell-command)))
9721     (if (string-equal command "")
9722         (setq command gnus-last-shell-command))
9723     (gnus-eval-in-buffer-window 
9724      gnus-article-buffer
9725      (save-restriction
9726        (widen)
9727        (shell-command-on-region (point-min) (point-max) command nil)))
9728     (setq gnus-last-shell-command command)))
9729
9730 ;; Summary extract commands
9731
9732 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9733   (let ((buffer-read-only nil)
9734         (article (gnus-summary-article-number))
9735         b)
9736     (or (gnus-summary-goto-subject article)
9737         (error (format "No such article: %d" article)))
9738     (or gnus-newsgroup-headers-hashtb-by-number
9739         (gnus-make-headers-hashtable-by-number))
9740     (gnus-summary-position-cursor)
9741     ;; If all commands are to be bunched up on one line, we collect
9742     ;; them here.  
9743     (if gnus-view-pseudos-separately
9744         ()
9745       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9746             files action)
9747         (while ps
9748           (setq action (cdr (assq 'action (car ps))))
9749           (setq files (list (cdr (assq 'name (car ps)))))
9750           (while (and ps (cdr ps)
9751                       (string= (or action "1")
9752                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9753             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9754             (setcdr ps (cdr (cdr ps))))
9755           (if (not files)
9756               ()
9757             (if (not (string-match "%s" action))
9758                 (setq files (cons " " files)))
9759             (setq files (cons " " files))
9760             (and (assq 'execute (car ps))
9761                  (setcdr (assq 'execute (car ps))
9762                          (funcall (if (string-match "%s" action)
9763                                       'format 'concat)
9764                                   action 
9765                                   (mapconcat (lambda (f) f) files " ")))))
9766           (setq ps (cdr ps)))))
9767     (if (and gnus-view-pseudos (not not-view))
9768         (while pslist
9769           (and (assq 'execute (car pslist))
9770                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9771                                      (eq gnus-view-pseudos 'not-confirm)))
9772           (setq pslist (cdr pslist)))
9773       (save-excursion
9774         (while pslist
9775           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9776                                          (gnus-summary-article-number)))
9777           (forward-line 1)
9778           (setq b (point))
9779           (insert "          " (file-name-nondirectory 
9780                                 (cdr (assq 'name (car pslist))))
9781                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9782           (add-text-properties 
9783            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9784                           'gnus-mark gnus-unread-mark 
9785                           'gnus-level 0
9786                           'gnus-pseudo (car pslist)))
9787           (forward-line -1)
9788           (gnus-sethash (int-to-string gnus-reffed-article-number)
9789                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9790           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9791           (setq pslist (cdr pslist)))))))
9792
9793 (defun gnus-pseudos< (p1 p2)
9794   (let ((c1 (cdr (assq 'action p1)))
9795         (c2 (cdr (assq 'action p2))))
9796     (and c1 c2 (string< c1 c2))))
9797
9798 (defun gnus-request-pseudo-article (props)
9799   (cond ((assq 'execute props)
9800          (gnus-execute-command (cdr (assq 'execute props)))))
9801   (let ((gnus-current-article (gnus-summary-article-number)))
9802     (run-hooks 'gnus-mark-article-hook)))
9803
9804 (defun gnus-execute-command (command &optional automatic)
9805   (save-excursion
9806     (gnus-article-setup-buffer)
9807     (set-buffer gnus-article-buffer)
9808     (let ((command (if automatic command (read-string "Command: " command)))
9809           (buffer-read-only nil))
9810       (erase-buffer)
9811       (insert "$ " command "\n\n")
9812       (if gnus-view-pseudo-asynchronously
9813           (start-process "gnus-execute" nil "sh" "-c" command)
9814         (call-process "sh" nil t nil "-c" command)))))
9815
9816 (defun gnus-copy-file (file &optional to)
9817   "Copy FILE to TO."
9818   (interactive
9819    (list (read-file-name "Copy file: " default-directory)
9820          (read-file-name "Copy file to: " default-directory)))
9821   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9822   (and (file-directory-p to) 
9823        (setq to (concat (file-name-as-directory to)
9824                         (file-name-nondirectory file))))
9825   (copy-file file to))
9826
9827 ;; Summary kill commands.
9828
9829 (defun gnus-summary-edit-global-kill (article)
9830   "Edit the \"global\" kill file."
9831   (interactive (list (gnus-summary-article-number)))
9832   (gnus-group-edit-global-kill article))
9833
9834 (defun gnus-summary-edit-local-kill ()
9835   "Edit a local kill file applied to the current newsgroup."
9836   (interactive)
9837   (setq gnus-current-headers 
9838         (gnus-gethash 
9839          (int-to-string (gnus-summary-article-number))
9840          gnus-newsgroup-headers-hashtb-by-number))
9841   (gnus-set-global-variables)
9842   (gnus-group-edit-local-kill 
9843    (gnus-summary-article-number) gnus-newsgroup-name))
9844
9845 \f
9846 ;;;
9847 ;;; Gnus article mode
9848 ;;;
9849
9850 (put 'gnus-article-mode 'mode-class 'special)
9851
9852 (if gnus-article-mode-map
9853     nil
9854   (setq gnus-article-mode-map (make-keymap))
9855   (suppress-keymap gnus-article-mode-map)
9856   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9857   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9858   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9859   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9860   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9861   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9862   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9863   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9864   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9865   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9866   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9867   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
9868   
9869   ;; Duplicate almost all summary keystrokes in the article mode map.
9870   (let ((commands 
9871          (list 
9872           " " "\177" "\r" "n" "p" "N" "P" "\M-\C-n" "\M-\C-p"
9873           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
9874           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
9875           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
9876           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
9877           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
9878           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
9879           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
9880           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
9881           "\C-c\C-i" "x" "X" "s" "t" "g" "?" "l"
9882           "\C-c\C-v\C-v" "\C-d" "v" 
9883 ;;        "Mt" "M!" "Md" "Mr"
9884 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
9885 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
9886 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
9887 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
9888 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
9889 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
9890 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
9891 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
9892 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
9893 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
9894 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
9895           )))
9896     (while commands
9897       (define-key gnus-article-mode-map (car commands) 
9898         'gnus-article-summary-command)
9899       (setq commands (cdr commands))))
9900
9901   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
9902 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
9903                          "=")))
9904     (while commands
9905       (define-key gnus-article-mode-map (car commands) 
9906         'gnus-article-summary-command-nosave)
9907       (setq commands (cdr commands)))))
9908
9909
9910 (defun gnus-article-mode ()
9911   "Major mode for displaying an article.
9912
9913 All normal editing commands are switched off.
9914
9915 The following commands are available:
9916
9917 \\<gnus-article-mode-map>
9918 \\[gnus-article-next-page]\t Scroll the article one page forwards
9919 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9920 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9921 \\[gnus-article-show-summary]\t Display the summary buffer
9922 \\[gnus-article-mail]\t Send a reply to the address near point
9923 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9924 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9925 \\[gnus-info-find-node]\t Go to the Gnus info node"
9926   (interactive)
9927   (if gnus-visual (gnus-article-make-menu-bar))
9928   (kill-all-local-variables)
9929   (setq mode-line-modified "-- ")
9930   (make-local-variable 'mode-line-format)
9931   (setq mode-line-format (copy-sequence mode-line-format))
9932   (and (equal (nth 3 mode-line-format) "   ")
9933        (setcar (nthcdr 3 mode-line-format) ""))
9934   (setq mode-name "Article")
9935   (setq major-mode 'gnus-article-mode)
9936   (make-local-variable 'minor-mode-alist)
9937   (or (assq 'gnus-show-mime minor-mode-alist)
9938       (setq minor-mode-alist
9939             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9940   (use-local-map gnus-article-mode-map)
9941   (make-local-variable 'page-delimiter)
9942   (setq page-delimiter gnus-page-delimiter)
9943   (buffer-disable-undo (current-buffer))
9944   (setq buffer-read-only t)             ;Disable modification
9945   (run-hooks 'gnus-article-mode-hook))
9946
9947 (defun gnus-article-setup-buffer ()
9948   "Initialize article mode buffer."
9949   (if (get-buffer gnus-article-buffer)
9950       (save-excursion
9951         (set-buffer gnus-article-buffer)
9952         (buffer-disable-undo (current-buffer))
9953         (setq buffer-read-only t)
9954         (gnus-add-current-to-buffer-list)
9955         (or (eq major-mode 'gnus-article-mode)
9956             (gnus-article-mode)))
9957     (save-excursion
9958       (set-buffer (get-buffer-create gnus-article-buffer))
9959       (gnus-add-current-to-buffer-list)
9960       (gnus-article-mode))))
9961
9962 ;; Set article window start at LINE, where LINE is the number of lines
9963 ;; from the head of the article.
9964 (defun gnus-article-set-window-start (&optional line)
9965   (set-window-start 
9966    (get-buffer-window gnus-article-buffer)
9967    (save-excursion
9968      (set-buffer gnus-article-buffer)
9969      (goto-char (point-min))
9970      (if (not line)
9971          (point-min)
9972        (gnus-message 6 "Moved to bookmark")
9973        (search-forward "\n\n" nil t)
9974        (forward-line line)
9975        (point)))))
9976
9977 (defun gnus-request-article-this-buffer (article group)
9978   "Get an article and insert it into this buffer."
9979   (setq group (or group gnus-newsgroup-name))
9980
9981   ;; Open server if it has closed.
9982   (gnus-check-news-server (gnus-find-method-for-group group))
9983
9984   ;; Using `gnus-request-article' directly will insert the article into
9985   ;; `nntp-server-buffer' - so we'll save some time by not having to
9986   ;; copy it from the server buffer into the article buffer.
9987
9988   ;; We only request an article by message-id when we do not have the
9989   ;; headers for it, so we'll have to get those.
9990   (and (stringp article) 
9991        (let ((gnus-override-method gnus-refer-article-method))
9992          (gnus-read-header article)))
9993
9994   ;; If the article number is negative, that means that this article
9995   ;; doesn't belong in this newsgroup (possibly), so we find its
9996   ;; message-id and request it by id instead of number.
9997   (if (not (numberp article))
9998       ()
9999     (save-excursion
10000       (set-buffer gnus-summary-buffer)
10001       (let ((header (gnus-get-header-by-number article)))
10002         (if (< article 0)
10003             (if (vectorp header)
10004                 ;; It's a real article.
10005                 (setq article (header-id header))
10006               ;; It is an extracted pseudo-article.
10007               (setq article 'pseudo)
10008               (gnus-request-pseudo-article header)))
10009
10010         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10011           (if (not (eq (car method) 'nneething))
10012               ()
10013             (let ((dir (concat (file-name-as-directory (nth 1 method))
10014                                (header-subject header))))
10015               (if (file-directory-p dir)
10016                   (progn
10017                     (setq article 'nneething)
10018                     (gnus-group-enter-directory dir)))))))))
10019
10020   ;; Check the cache.
10021   (if (and gnus-use-cache
10022            (numberp article)
10023            (gnus-cache-request-article article group))
10024       'article
10025     ;; Get the article and into the article buffer.
10026     (if (or (stringp article) (numberp article))
10027         (progn
10028           (erase-buffer)
10029           (let ((gnus-override-method 
10030                  (and (stringp article) gnus-refer-article-method)))
10031             (and (gnus-request-article article group (current-buffer))
10032                  'article)))
10033       article)))
10034
10035 (defun gnus-read-header (id)
10036   "Read the headers of article ID and enter them into the Gnus system."
10037   (or gnus-newsgroup-headers-hashtb-by-number
10038       (gnus-make-headers-hashtable-by-number))
10039   (let (header)
10040     (if (not (setq header 
10041                    (car (if (let ((gnus-nov-is-evil t))
10042                               (gnus-retrieve-headers 
10043                                (list id) gnus-newsgroup-name))
10044                             (gnus-get-newsgroup-headers)))))
10045         nil
10046       (if (stringp id)
10047           (header-set-number header gnus-reffed-article-number))
10048       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10049       (gnus-sethash (int-to-string (header-number header)) header
10050                     gnus-newsgroup-headers-hashtb-by-number)
10051       (if (stringp id)
10052           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10053       (setq gnus-current-headers header)
10054       header)))
10055
10056 (defun gnus-article-prepare (article &optional all-headers header)
10057   "Prepare ARTICLE in article mode buffer.
10058 ARTICLE should either be an article number or a Message-ID.
10059 If ARTICLE is an id, HEADER should be the article headers.
10060 If ALL-HEADERS is non-nil, no headers are hidden."
10061   (save-excursion
10062     ;; Make sure we start in a summary buffer.
10063     (or (eq major-mode 'gnus-summary-mode)
10064         (set-buffer gnus-summary-buffer))
10065     (setq gnus-summary-buffer (current-buffer))
10066     ;; Make sure the connection to the server is alive.
10067     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10068         (progn
10069           (gnus-check-news-server 
10070            (gnus-find-method-for-group gnus-newsgroup-name))
10071           (gnus-request-group gnus-newsgroup-name t)))
10072     (or gnus-newsgroup-headers-hashtb-by-number
10073         (gnus-make-headers-hashtable-by-number))
10074     (let* ((article (if header (header-number header) article))
10075            (summary-buffer (current-buffer))
10076            (internal-hook gnus-article-internal-prepare-hook)
10077            (group gnus-newsgroup-name)
10078            result)
10079       (save-excursion
10080         (gnus-article-setup-buffer)
10081         (set-buffer gnus-article-buffer)
10082         (if (not (setq result (let ((buffer-read-only nil))
10083                                 (gnus-request-article-this-buffer 
10084                                  article group))))
10085             ;; There is no such article.
10086             (save-excursion
10087               (if (not (numberp article))
10088                   ()
10089                 (setq gnus-article-current 
10090                       (cons gnus-newsgroup-name article))
10091                 (set-buffer gnus-summary-buffer)
10092                 (setq gnus-current-article article)
10093                 (gnus-summary-mark-as-read article gnus-canceled-mark))
10094               (gnus-message 1 "No such article (may be canceled)")
10095               (ding)
10096               nil)
10097           (if (or (eq result 'pseudo) (eq result 'nneething))
10098               (progn
10099                 (save-excursion
10100                   (set-buffer summary-buffer)
10101                   (setq gnus-last-article gnus-current-article
10102                         gnus-newsgroup-history (cons gnus-current-article
10103                                                      gnus-newsgroup-history)
10104                         gnus-current-article 0
10105                         gnus-current-headers nil
10106                         gnus-article-current nil)
10107                   (if (eq result 'nneething)
10108                       (gnus-configure-windows 'summary)
10109                     (gnus-configure-windows 'article))
10110                   (gnus-set-global-variables))
10111                 (gnus-set-mode-line 'article))
10112             ;; The result from the `request' was an actual article -
10113             ;; or at least some text that is now displayed in the
10114             ;; article buffer.
10115             (if (and (numberp article)
10116                      (not (eq article gnus-current-article)))
10117                 ;; Seems like a new article has been selected.
10118                 ;; `gnus-current-article' must be an article number.
10119                 (save-excursion
10120                   (set-buffer summary-buffer)
10121                   (setq gnus-last-article gnus-current-article
10122                         gnus-newsgroup-history (cons gnus-current-article
10123                                                      gnus-newsgroup-history)
10124                         gnus-current-article article
10125                         gnus-current-headers 
10126                         (gnus-get-header-by-number gnus-current-article)
10127                         gnus-article-current 
10128                         (cons gnus-newsgroup-name gnus-current-article))
10129                   (gnus-summary-show-thread)
10130                   (run-hooks 'gnus-mark-article-hook)
10131                   (gnus-set-mode-line 'summary)
10132                   (and gnus-visual 
10133                        (run-hooks 'gnus-visual-mark-article-hook))
10134                   ;; Set the global newsgroup variables here.
10135                   ;; Suggested by Jim Sisolak
10136                   ;; <sisolak@trans4.neep.wisc.edu>.
10137                   (gnus-set-global-variables)
10138                   (and gnus-use-cache 
10139                        (gnus-cache-possibly-enter-article
10140                         group article
10141                         (gnus-get-header-by-number article)
10142                         (memq article gnus-newsgroup-marked)
10143                         (memq article gnus-newsgroup-dormant)
10144                         (memq article gnus-newsgroup-unreads)))))
10145             ;; gnus-have-all-headers must be either T or NIL.
10146             (setq gnus-have-all-headers
10147                   (not (not (or all-headers gnus-show-all-headers))))
10148             ;; Hooks for getting information from the article.
10149             ;; This hook must be called before being narrowed.
10150             (let (buffer-read-only)
10151               (run-hooks 'internal-hook)
10152               (run-hooks 'gnus-article-prepare-hook)
10153               ;; Decode MIME message.
10154               (if (and gnus-show-mime
10155                        (or (not gnus-strict-mime)
10156                            (gnus-fetch-field "Mime-Version")))
10157                   (funcall gnus-show-mime-method))
10158               ;; Perform the article display hooks.
10159               (run-hooks 'gnus-article-display-hook))
10160             ;; Do page break.
10161             (goto-char (point-min))
10162             (and gnus-break-pages (gnus-narrow-to-page))
10163             (gnus-set-mode-line 'article)
10164             (gnus-configure-windows 'article)
10165             (goto-char (point-min))
10166             t))))))
10167
10168 (defun gnus-article-show-all-headers ()
10169   "Show all article headers in article mode buffer."
10170   (save-excursion 
10171     (gnus-article-setup-buffer)
10172     (set-buffer gnus-article-buffer)
10173     (let ((buffer-read-only nil))
10174       (remove-text-properties (point-min) (point-max) 
10175                               gnus-hidden-properties))))
10176
10177 (defun gnus-article-hide-headers-if-wanted ()
10178   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10179 Provided for backwards compatability."
10180   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10181       (gnus-article-hide-headers)))
10182
10183 (defun gnus-article-hide-headers (&optional delete)
10184   "Hide unwanted headers and possibly sort them as well."
10185   (interactive "P")
10186   (save-excursion
10187     (set-buffer gnus-article-buffer)
10188     (save-restriction
10189       (let ((sorted gnus-sorted-header-list)
10190             (buffer-read-only nil)
10191             want-list beg want-l)
10192         ;; First we narrow to just the headers.
10193         (widen)
10194         (goto-char (point-min))
10195         ;; Hide any "From " lines at the beginning of (mail) articles. 
10196         (while (looking-at "From ")
10197           (forward-line 1))
10198         (if (bobp) 
10199             (add-text-properties (point-min) (point) gnus-hidden-properties))
10200         ;; Then treat the rest of the header lines.
10201         (narrow-to-region 
10202          (point) 
10203          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10204         ;; Then we use the two regular expressions
10205         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10206         ;; select which header lines is to remain visible in the
10207         ;; article buffer.
10208         (goto-char (point-min))
10209         (while (re-search-forward "^[^ \t]*:" nil t)
10210           (beginning-of-line)
10211           ;; We add the headers we want to keep to a list and delete
10212           ;; them from the buffer.
10213           (if (or (and (stringp gnus-visible-headers)
10214                        (looking-at gnus-visible-headers))
10215                   (and (not (stringp gnus-visible-headers))
10216                        (stringp gnus-ignored-headers)
10217                        (not (looking-at gnus-ignored-headers))))
10218               (progn
10219                 (setq beg (point))
10220                 (forward-line 1)
10221                 ;; Be sure to get multi-line headers...
10222                 (re-search-forward "^[^ \t]*:" nil t)
10223                 (beginning-of-line)
10224                 (setq want-list 
10225                       (cons (buffer-substring beg (point)) want-list))
10226                 (delete-region beg (point))
10227                 (goto-char beg))
10228             (forward-line 1)))
10229         ;; Next we perform the sorting by looking at
10230         ;; `gnus-sorted-header-list'. 
10231         (goto-char (point-min))
10232         (while (and sorted want-list)
10233           (setq want-l want-list)
10234           (while (and want-l
10235                       (not (string-match (car sorted) (car want-l))))
10236             (setq want-l (cdr want-l)))
10237           (if want-l 
10238               (progn
10239                 (insert (car want-l))
10240                 (setq want-list (delq (car want-l) want-list))))
10241           (setq sorted (cdr sorted)))
10242         ;; Any headers that were not matched by the sorted list we
10243         ;; just tack on the end of the visible header list.
10244         (while want-list
10245           (insert (car want-list))
10246           (setq want-list (cdr want-list)))
10247         ;; And finally we make the unwanted headers invisible.
10248         (if delete
10249             (delete-region (point) (point-max))
10250           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10251           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10252
10253 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10254 (defun gnus-article-treat-overstrike ()
10255   "Translate overstrikes into bold text."
10256   (interactive)
10257   (save-excursion
10258     (set-buffer gnus-article-buffer)
10259     (let ((buffer-read-only nil))
10260       (while (search-forward "\b" nil t)
10261         (let ((next (following-char))
10262               (previous (char-after (- (point) 2))))
10263           (cond ((eq next previous)
10264                  (delete-region (- (point) 2) (point))
10265                  (put-text-property (point) (1+ (point))
10266                                     'face 'bold))
10267                 ((eq next ?_)
10268                  (delete-region (1- (point)) (1+ (point)))
10269                  (put-text-property (1- (point)) (point)
10270                                     'face 'underline))
10271                 ((eq previous ?_)
10272                  (delete-region (- (point) 2) (point))
10273                  (put-text-property (point) (1+ (point))
10274                                     'face 'underline))))))))
10275
10276 (defun gnus-article-word-wrap ()
10277   "Format too long lines."
10278   (interactive)
10279   (save-excursion
10280     (set-buffer gnus-article-buffer)
10281     (let ((buffer-read-only nil))
10282       (goto-char (point-min))
10283       (search-forward "\n\n" nil t)
10284       (end-of-line 1)
10285       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10286             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10287             (adaptive-fill-mode t))
10288         (while (not (eobp))
10289           (and (>= (current-column) (min fill-column (window-width)))
10290                (/= (preceding-char) ?:)
10291                (fill-paragraph nil))
10292           (end-of-line 2))))))
10293
10294 (defun gnus-article-remove-cr ()
10295   "Remove carriage returns from an article."
10296   (interactive)
10297   (save-excursion
10298     (set-buffer gnus-article-buffer)
10299     (let ((buffer-read-only nil))
10300       (goto-char (point-min))
10301       (while (search-forward "\r" nil t)
10302         (replace-match "" t t)))))
10303
10304 (defun gnus-article-display-x-face (&optional force)
10305   "Look for an X-Face header and display it if present."
10306   (interactive (list 'force))
10307   (save-excursion
10308     (set-buffer gnus-article-buffer)
10309     (let ((inhibit-point-motion-hooks t)
10310           (case-fold-search nil))
10311       (save-restriction
10312         (goto-char (point-min))
10313         (search-forward "\n\n")
10314         (narrow-to-region (point-min) (point))
10315         (goto-char (point-min))
10316         (if (or (not gnus-article-x-face-command)
10317                 (and (not force)
10318                      (or (not gnus-article-x-face-too-ugly)
10319                          (string-match gnus-article-x-face-too-ugly
10320                                        (mail-fetch-field "from"))))
10321                 (progn
10322                   (goto-char (point-min))
10323                   (not (re-search-forward "^X-Face: " nil t))))
10324             nil
10325           (let ((beg (point))
10326                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10327             (if (symbolp gnus-article-x-face-command)
10328                 (and (or (fboundp gnus-article-x-face-command)
10329                          (error "%s is not a function"
10330                                 gnus-article-x-face-command))
10331                      (funcall gnus-article-x-face-command beg end))
10332               (call-process-region beg end "sh" nil 0 nil
10333                                    "-c" gnus-article-x-face-command))))))))
10334
10335 (defun gnus-article-de-quoted-unreadable (&optional force)
10336   "Do a naïve translation of a quoted-printable-encoded article.
10337 This is in no way, shape or form meant as a replacement for real MIME
10338 processing, but is simply a stop-gap measure until MIME support is
10339 written.
10340 If FORCE, decode the article whether it is marked as quoted-printable
10341 or not." 
10342   (interactive (list 'force))
10343   (save-excursion
10344     (set-buffer gnus-article-buffer)
10345     (let ((case-fold-search t)
10346           (buffer-read-only nil)
10347           (type (gnus-fetch-field "content-transfer-encoding")))
10348       (if (or force (and type (string-match "quoted-printable" type)))
10349           (progn
10350             (goto-char (point-min))
10351             (search-forward "\n\n" nil 'move)
10352             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10353
10354 (defun gnus-mime-decode-quoted-printable (from to)
10355   ;; Decode quoted-printable from region between FROM and TO.
10356   (save-excursion
10357     (goto-char from)
10358     (while (search-forward "=" to t)
10359       (cond ((eq (following-char) ?\n)
10360              (delete-char -1)
10361              (delete-char 1))
10362             ((looking-at "[0-9A-F][0-9A-F]")
10363              (delete-char -1)
10364              (insert (hexl-hex-string-to-integer
10365                       (buffer-substring (point) (+ 2 (point)))))
10366              (delete-char 2))
10367             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10368
10369 (defvar gnus-article-time-units
10370   (list (cons 'year (* 365.25 24 60 60))
10371         (cons 'week (* 7 24 60 60))
10372         (cons 'day (* 24 60 60))
10373         (cons 'hour (* 60 60))
10374         (cons 'minute 60)
10375         (cons 'second 1)))
10376
10377 (defun gnus-article-date-ut (&optional type)
10378   "Convert DATE date to universal time in the current article.
10379 If TYPE is `local', convert to local time; if it is `lapsed', output
10380 how much time has lapsed since DATE."
10381   (interactive (list 'ut))
10382   (let ((date (header-date (or gnus-current-headers 
10383                                (gnus-get-header-by-number
10384                                 (gnus-summary-article-number))"")))
10385         (date-regexp "^Date: \\|^X-Sent: "))
10386     (if (or (not date)
10387             (string= date ""))
10388         ()
10389       (save-excursion
10390         (set-buffer gnus-article-buffer)
10391         (let ((buffer-read-only nil))
10392           (goto-char (point-min))
10393           (if (and (re-search-forward date-regexp nil t)
10394                    (progn 
10395                      (beginning-of-line)
10396                      (looking-at date-regexp)))
10397               (delete-region (gnus-point-at-bol)
10398                              (progn (end-of-line) (1+ (point))))
10399             (goto-char (point-min))
10400             (goto-char (- (search-forward "\n\n") 2)))
10401           (insert
10402            (cond 
10403             ((eq type 'local)
10404              (concat "Date: " (condition-case ()
10405                                   (timezone-make-date-arpa-standard date)
10406                                 (error date))
10407                      "\n"))
10408             ((eq type 'ut)
10409              (concat "Date: "
10410                      (condition-case ()
10411                          (timezone-make-date-arpa-standard date nil "UT")
10412                        (error date))
10413                      "\n"))
10414             ((eq type 'lapsed)
10415              ;; If the date is seriously mangled, the timezone
10416              ;; functions are liable to bug out, so we condition-case
10417              ;; the entire thing.  
10418              (let* ((sec (condition-case ()
10419                              (max (- (gnus-seconds-since-epoch 
10420                                       (timezone-make-date-arpa-standard
10421                                        (current-time-string) 
10422                                        (current-time-zone) "UT"))
10423                                      (gnus-seconds-since-epoch 
10424                                       (timezone-make-date-arpa-standard 
10425                                        date nil "UT")))
10426                                   0)
10427                            (error 0)))
10428                     num prev)
10429                (concat
10430                 "X-Sent: "
10431                 (mapconcat 
10432                  (lambda (unit)
10433                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10434                        ""
10435                      (setq sec (- sec (* num (cdr unit))))
10436                      (prog1
10437                          (concat (if prev ", " "") (int-to-string (floor num))
10438                                  " " (symbol-name (car unit))
10439                                  (if (> num 1) "s" ""))
10440                        (setq prev t))))
10441                  gnus-article-time-units "")
10442                 " ago\n")))
10443             (t
10444              (error "Unknown conversion type: %s" type)))))))))
10445
10446 (defun gnus-article-date-local ()
10447   "Convert the current article date to the local timezone."
10448   (interactive)
10449   (gnus-article-date-ut 'local))
10450
10451 (defun gnus-article-date-lapsed ()
10452   "Convert the current article date to time lapsed since it was sent."
10453   (interactive)
10454   (gnus-article-date-ut 'lapsed))
10455
10456 (defun gnus-article-maybe-highlight ()
10457   (if gnus-visual (gnus-article-highlight)))
10458
10459 ;; Article savers.
10460
10461 (defun gnus-output-to-rmail (file-name)
10462   "Append the current article to an Rmail file named FILE-NAME."
10463   (require 'rmail)
10464   ;; Most of these codes are borrowed from rmailout.el.
10465   (setq file-name (expand-file-name file-name))
10466   (setq rmail-default-rmail-file file-name)
10467   (let ((artbuf (current-buffer))
10468         (tmpbuf (get-buffer-create " *Gnus-output*")))
10469     (save-excursion
10470       (or (get-file-buffer file-name)
10471           (file-exists-p file-name)
10472           (if (gnus-yes-or-no-p
10473                (concat "\"" file-name "\" does not exist, create it? "))
10474               (let ((file-buffer (create-file-buffer file-name)))
10475                 (save-excursion
10476                   (set-buffer file-buffer)
10477                   (rmail-insert-rmail-file-header)
10478                   (let ((require-final-newline nil))
10479                     (write-region (point-min) (point-max) file-name t 1)))
10480                 (kill-buffer file-buffer))
10481             (error "Output file does not exist")))
10482       (set-buffer tmpbuf)
10483       (buffer-disable-undo (current-buffer))
10484       (erase-buffer)
10485       (insert-buffer-substring artbuf)
10486       (gnus-convert-article-to-rmail)
10487       ;; Decide whether to append to a file or to an Emacs buffer.
10488       (let ((outbuf (get-file-buffer file-name)))
10489         (if (not outbuf)
10490             (append-to-file (point-min) (point-max) file-name)
10491           ;; File has been visited, in buffer OUTBUF.
10492           (set-buffer outbuf)
10493           (let ((buffer-read-only nil)
10494                 (msg (and (boundp 'rmail-current-message)
10495                           (symbol-value 'rmail-current-message))))
10496             ;; If MSG is non-nil, buffer is in RMAIL mode.
10497             (if msg
10498                 (progn (widen)
10499                        (narrow-to-region (point-max) (point-max))))
10500             (insert-buffer-substring tmpbuf)
10501             (if msg
10502                 (progn
10503                   (goto-char (point-min))
10504                   (widen)
10505                   (search-backward "\^_")
10506                   (narrow-to-region (point) (point-max))
10507                   (goto-char (1+ (point-min)))
10508                   (rmail-count-new-messages t)
10509                   (rmail-show-message msg)))))))
10510     (kill-buffer tmpbuf)))
10511
10512 (defun gnus-output-to-file (file-name)
10513   "Append the current article to a file named FILE-NAME."
10514   (setq file-name (expand-file-name file-name))
10515   (let ((artbuf (current-buffer))
10516         (tmpbuf (get-buffer-create " *Gnus-output*")))
10517     (save-excursion
10518       (set-buffer tmpbuf)
10519       (buffer-disable-undo (current-buffer))
10520       (erase-buffer)
10521       (insert-buffer-substring artbuf)
10522       ;; Append newline at end of the buffer as separator, and then
10523       ;; save it to file.
10524       (goto-char (point-max))
10525       (insert "\n")
10526       (append-to-file (point-min) (point-max) file-name))
10527     (kill-buffer tmpbuf)))
10528
10529 (defun gnus-convert-article-to-rmail ()
10530   "Convert article in current buffer to Rmail message format."
10531   (let ((buffer-read-only nil))
10532     ;; Convert article directly into Babyl format.
10533     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10534     (goto-char (point-min))
10535     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10536     (while (search-forward "\n\^_" nil t) ;single char
10537       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10538     (goto-char (point-max))
10539     (insert "\^_")))
10540
10541 (defun gnus-narrow-to-page (&optional arg)
10542   "Make text outside current page invisible except for page delimiter.
10543 A numeric arg specifies to move forward or backward by that many pages,
10544 thus showing a page other than the one point was originally in."
10545   (interactive "P")
10546   (setq arg (if arg (prefix-numeric-value arg) 0))
10547   (save-excursion
10548     (forward-page -1)                   ;Beginning of current page.
10549     (widen)
10550     (if (> arg 0)
10551         (forward-page arg)
10552       (if (< arg 0)
10553           (forward-page (1- arg))))
10554     ;; Find the end of the page.
10555     (forward-page)
10556     ;; If we stopped due to end of buffer, stay there.
10557     ;; If we stopped after a page delimiter, put end of restriction
10558     ;; at the beginning of that line.
10559     ;; These are commented out.
10560     ;;    (if (save-excursion (beginning-of-line)
10561     ;;                  (looking-at page-delimiter))
10562     ;;  (beginning-of-line))
10563     (narrow-to-region (point)
10564                       (progn
10565                         ;; Find the top of the page.
10566                         (forward-page -1)
10567                         ;; If we found beginning of buffer, stay there.
10568                         ;; If extra text follows page delimiter on same line,
10569                         ;; include it.
10570                         ;; Otherwise, show text starting with following line.
10571                         (if (and (eolp) (not (bobp)))
10572                             (forward-line 1))
10573                         (point)))))
10574
10575 (defun gnus-gmt-to-local ()
10576   "Rewrite Date header described in GMT to local in current buffer.
10577 Intended to be used with gnus-article-prepare-hook."
10578   (save-excursion
10579     (save-restriction
10580       (widen)
10581       (goto-char (point-min))
10582       (narrow-to-region (point-min)
10583                         (progn (search-forward "\n\n" nil 'move) (point)))
10584       (goto-char (point-min))
10585       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10586           (let ((buffer-read-only nil)
10587                 (date (buffer-substring-no-properties
10588                        (match-beginning 1) (match-end 1))))
10589             (delete-region (match-beginning 1) (match-end 1))
10590             (insert
10591              (timezone-make-date-arpa-standard 
10592               date nil (current-time-zone))))))))
10593
10594
10595 ;; Article mode commands
10596
10597 (defun gnus-article-next-page (lines)
10598   "Show next page of current article.
10599 If end of article, return non-nil. Otherwise return nil.
10600 Argument LINES specifies lines to be scrolled up."
10601   (interactive "P")
10602   (move-to-window-line -1)
10603   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10604   (if (save-excursion
10605         (end-of-line)
10606         (and (pos-visible-in-window-p)  ;Not continuation line.
10607              (eobp)))
10608       ;; Nothing in this page.
10609       (if (or (not gnus-break-pages)
10610               (save-excursion
10611                 (save-restriction
10612                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10613           t                             ;Nothing more.
10614         (gnus-narrow-to-page 1)         ;Go to next page.
10615         nil)
10616     ;; More in this page.
10617     (condition-case ()
10618         (scroll-up lines)
10619       (end-of-buffer
10620        ;; Long lines may cause an end-of-buffer error.
10621        (goto-char (point-max))))
10622     nil))
10623
10624 (defun gnus-article-prev-page (lines)
10625   "Show previous page of current article.
10626 Argument LINES specifies lines to be scrolled down."
10627   (interactive "P")
10628   (move-to-window-line 0)
10629   (if (and gnus-break-pages
10630            (bobp)
10631            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10632       (progn
10633         (gnus-narrow-to-page -1) ;Go to previous page.
10634         (goto-char (point-max))
10635         (recenter -1))
10636     (scroll-down lines)))
10637
10638 (defun gnus-article-refer-article ()
10639   "Read article specified by message-id around point."
10640   (interactive)
10641   (search-forward ">" nil t)    ;Move point to end of "<....>".
10642   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10643       (let ((message-id
10644              (buffer-substring (match-beginning 1) (match-end 1))))
10645         (set-buffer gnus-summary-buffer)
10646         (gnus-summary-refer-article message-id))
10647     (error "No references around point")))
10648
10649 (defun gnus-article-show-summary ()
10650   "Reconfigure windows to show summary buffer."
10651   (interactive)
10652   (gnus-configure-windows 'article)
10653   (gnus-summary-goto-subject gnus-current-article))
10654
10655 (defun gnus-article-describe-briefly ()
10656   "Describe article mode commands briefly."
10657   (interactive)
10658   (gnus-message 6
10659    (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")))
10660
10661 (defun gnus-article-summary-command ()
10662   "Execute the last keystroke in the summary buffer."
10663   (interactive)
10664   (let ((obuf (current-buffer))
10665         (owin (current-window-configuration))
10666         func)
10667     (switch-to-buffer gnus-summary-buffer 'norecord)
10668     (setq func (lookup-key (current-local-map) (this-command-keys)))
10669     (call-interactively func)
10670     (set-buffer obuf)
10671     (set-window-configuration owin)
10672     (set-window-start (get-buffer-window (current-buffer)) (point))))
10673
10674 (defun gnus-article-summary-command-nosave ()
10675   "Execute the last keystroke in the summary buffer."
10676   (interactive)
10677   (let (func)
10678     (switch-to-buffer gnus-summary-buffer 'norecord)
10679     (setq func (lookup-key (current-local-map) (this-command-keys)))
10680     (call-interactively func)))
10681
10682 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10683 ;; Modified by tower@prep Nov 86
10684 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10685
10686 (defun gnus-caesar-region (&optional n)
10687   "Caesar rotation of region by N, default 13, for decrypting netnews.
10688 ROT47 will be performed for Japanese text in any case."
10689   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10690                    (list (prefix-numeric-value current-prefix-arg))
10691                  (list nil)))
10692   (cond ((not (numberp n)) (setq n 13))
10693         (t (setq n (mod n 26))))        ;canonicalize N
10694   (if (not (zerop n))           ; no action needed for a rot of 0
10695       (progn
10696         (if (or (not (boundp 'caesar-translate-table))
10697                 (not caesar-translate-table)
10698                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10699             (let ((i 0) 
10700                   (lower "abcdefghijklmnopqrstuvwxyz")
10701                   upper)
10702               (gnus-message 9 "Building caesar-translate-table...")
10703               (setq caesar-translate-table (make-vector 256 0))
10704               (while (< i 256)
10705                 (aset caesar-translate-table i i)
10706                 (setq i (1+ i)))
10707               (setq lower (concat lower lower)
10708                     upper (upcase lower)
10709                     i 0)
10710               (while (< i 26)
10711                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10712                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10713                 (setq i (1+ i)))
10714               ;; ROT47 for Japanese text.
10715               ;; Thanks to ichikawa@flab.fujitsu.junet.
10716               (setq i 161)
10717               (let ((t1 (logior ?O 128))
10718                     (t2 (logior ?! 128))
10719                     (t3 (logior ?~ 128)))
10720                 (while (< i 256)
10721                   (aset caesar-translate-table i
10722                         (let ((v (aref caesar-translate-table i)))
10723                           (if (<= v t1) (if (< v t2) v (+ v 47))
10724                             (if (<= v t3) (- v 47) v))))
10725                   (setq i (1+ i))))
10726               (gnus-message 9 "Building caesar-translate-table...done")))
10727         (let ((from (region-beginning))
10728               (to (region-end))
10729               (i 0) str len)
10730           (setq str (buffer-substring from to))
10731           (setq len (length str))
10732           (while (< i len)
10733             (aset str i (aref caesar-translate-table (aref str i)))
10734             (setq i (1+ i)))
10735           (goto-char from)
10736           (delete-region from to)
10737           (insert str)))))
10738
10739 \f
10740 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10741
10742 ;;;###autoload
10743 (defalias 'gnus-batch-kill 'gnus-batch-score)
10744 ;;;###autoload
10745 (defun gnus-batch-score ()
10746   "Run batched scoring.
10747 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10748 Newsgroups is a list of strings in Bnews format.  If you want to score
10749 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10750 score the alt hierarchy, you'd say \"!alt.all\"."
10751   (interactive)
10752   (let* ((yes-and-no
10753           (gnus-newsrc-parse-options
10754            (apply (function concat)
10755                   (mapcar (lambda (g) (concat g " "))
10756                           command-line-args-left))))
10757          (gnus-expert-user t)
10758          (nnmail-spool-file nil)
10759          (gnus-use-dribble-file nil)
10760          (yes (car yes-and-no))
10761          (no (cdr yes-and-no))
10762          group newsrc entry
10763          ;; Disable verbose message.
10764          gnus-novice-user gnus-large-newsgroup)
10765     ;; Eat all arguments.
10766     (setq command-line-args-left nil)
10767     ;; Start Gnus.
10768     (gnus)
10769     ;; Apply kills to specified newsgroups in command line arguments.
10770     (setq newsrc (cdr gnus-newsrc-alist))
10771     (while newsrc
10772       (setq group (car (car newsrc)))
10773       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10774       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10775                (and (car entry)
10776                     (or (eq (car entry) t)
10777                         (not (zerop (car entry)))))
10778                (if yes (string-match yes group) t)
10779                (or (null no) (not (string-match no group))))
10780           (progn
10781             (gnus-summary-read-group group nil t)
10782             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10783                  (gnus-summary-exit))))
10784       (setq newsrc (cdr newsrc)))
10785     ;; Exit Emacs.
10786     (switch-to-buffer gnus-group-buffer)
10787     (gnus-group-save-newsrc)))
10788
10789 (defun gnus-apply-kill-file ()
10790   "Apply a kill file to the current newsgroup.
10791 Returns the number of articles marked as read."
10792   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10793           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10794       (gnus-apply-kill-file-internal)
10795     0))
10796
10797 (defun gnus-kill-save-kill-buffer ()
10798   (save-excursion
10799     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10800       (if (get-file-buffer file)
10801           (progn
10802             (set-buffer (get-file-buffer file))
10803             (and (buffer-modified-p) (save-buffer))
10804             (kill-buffer (current-buffer)))))))
10805
10806 (defvar gnus-kill-file-name "KILL"
10807   "Suffix of the kill files.")
10808
10809 (defun gnus-newsgroup-kill-file (newsgroup)
10810   "Return the name of a kill file name for NEWSGROUP.
10811 If NEWSGROUP is nil, return the global kill file name instead."
10812   (cond ((or (null newsgroup)
10813              (string-equal newsgroup ""))
10814          ;; The global KILL file is placed at top of the directory.
10815          (expand-file-name gnus-kill-file-name
10816                            (or gnus-kill-files-directory "~/News")))
10817         ((gnus-use-long-file-name 'not-kill)
10818          ;; Append ".KILL" to newsgroup name.
10819          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10820                            (or gnus-kill-files-directory "~/News")))
10821         (t
10822          ;; Place "KILL" under the hierarchical directory.
10823          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10824                                    "/" gnus-kill-file-name)
10825                            (or gnus-kill-files-directory "~/News")))))
10826
10827 \f
10828 ;;;
10829 ;;; Dribble file
10830 ;;;
10831
10832 (defvar gnus-dribble-ignore nil)
10833
10834 (defun gnus-dribble-file-name ()
10835   (concat gnus-startup-file "-dribble"))
10836
10837 (defun gnus-dribble-open ()
10838   (save-excursion 
10839     (set-buffer 
10840      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10841     (buffer-disable-undo (current-buffer))
10842     (bury-buffer gnus-dribble-buffer)
10843     (auto-save-mode t)
10844     (goto-char (point-max))))
10845
10846 (defun gnus-dribble-enter (string)
10847   (if (and (not gnus-dribble-ignore)
10848            gnus-dribble-buffer
10849            (buffer-name gnus-dribble-buffer))
10850       (let ((obuf (current-buffer)))
10851         (set-buffer gnus-dribble-buffer)
10852         (insert string "\n")
10853         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10854         (set-buffer obuf))))
10855
10856 (defun gnus-dribble-read-file ()
10857   (let ((dribble-file (gnus-dribble-file-name)))
10858     (save-excursion 
10859       (set-buffer (setq gnus-dribble-buffer 
10860                         (get-buffer-create 
10861                          (file-name-nondirectory dribble-file))))
10862       (gnus-add-current-to-buffer-list)
10863       (erase-buffer)
10864       (set-visited-file-name dribble-file)
10865       (buffer-disable-undo (current-buffer))
10866       (bury-buffer (current-buffer))
10867       (set-buffer-modified-p nil)
10868       (let ((auto (make-auto-save-file-name))
10869             (gnus-dribble-ignore t))
10870         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10871             (progn
10872               (if (file-newer-than-file-p auto dribble-file)
10873                   (setq dribble-file auto))
10874               (insert-file-contents dribble-file)
10875               (if (not (zerop (buffer-size)))
10876                   (set-buffer-modified-p t))
10877               (if (gnus-y-or-n-p 
10878                    "Auto-save file exists. Do you want to read it? ")
10879                   (progn
10880                     (gnus-message 5 "Reading %s..." dribble-file) 
10881                     (eval-buffer (current-buffer))
10882                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10883
10884 (defun gnus-dribble-delete-file ()
10885   (if (file-exists-p (gnus-dribble-file-name))
10886       (delete-file (gnus-dribble-file-name)))
10887   (if gnus-dribble-buffer
10888       (save-excursion
10889         (set-buffer gnus-dribble-buffer)
10890         (let ((auto (make-auto-save-file-name)))
10891           (if (file-exists-p auto)
10892               (delete-file auto))
10893           (erase-buffer)
10894           (set-buffer-modified-p nil)))))
10895
10896 (defun gnus-dribble-save ()
10897   (if (and gnus-dribble-buffer
10898            (buffer-name gnus-dribble-buffer))
10899       (save-excursion
10900         (set-buffer gnus-dribble-buffer)
10901         (save-buffer))))
10902
10903 (defun gnus-dribble-clear ()
10904   (save-excursion
10905     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10906         (progn
10907           (set-buffer gnus-dribble-buffer)
10908           (erase-buffer)
10909           (set-buffer-modified-p nil)
10910           (setq buffer-saved-size (buffer-size))))))
10911
10912 ;;;
10913 ;;; Server Communication
10914 ;;;
10915
10916 ;; All the Gnus backends have the same interface, and should return
10917 ;; data in a similar format. Below is an overview of what functions
10918 ;; these packages must supply and what results they should return.
10919 ;;
10920 ;; Variables:
10921 ;;
10922 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10923 ;; buffer. 
10924 ;;
10925 ;; Functions for the imaginary backend `choke':
10926 ;;
10927 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10928 ;; Should return all headers for all ARTICLES, or return NOV lines for
10929 ;; the same.
10930 ;;
10931 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10932 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10933 ;; must be returned.
10934 ;;
10935 ;; `choke-close-group GROUP &optional SERVER'
10936 ;; Close group. Most backends won't have to do anything with this
10937 ;; call, but it is an opportunity to clean up, if that is needed. It
10938 ;; is called when Gnus exits a group.
10939 ;;
10940 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10941 ;; Return ARTICLE, which is either an article number or
10942 ;; message-id. Note that not all backends can return articles based on
10943 ;; message-id. 
10944 ;;
10945 ;; `choke-request-list SERVER'
10946 ;; Return a list of all newsgroups on SERVER.
10947 ;;
10948 ;; `choke-request-list-newsgroups SERVER'
10949 ;; Return a list of descriptions of all newsgroups on SERVER.
10950 ;;
10951 ;; `choke-request-newgroups DATE &optional SERVER'
10952 ;; Return a list of all groups that have arrived after DATE on
10953 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10954 ;; always check whether the groups are old or not. Backends that do
10955 ;; not store date information may just return the entire list of
10956 ;; groups, although this might not be a good idea in general.
10957 ;;
10958 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10959 ;; Should return a buffer that is suitable for "posting". nnspool and
10960 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10961 ;; buffer. This function should fill out the appropriate headers. 
10962 ;;
10963 ;; `choke-request-post &optional SERVER'
10964 ;; Function that will be called from a buffer to be posted. 
10965 ;;
10966 ;; `choke-open-server SERVER &optional ARGUMENT'
10967 ;; Open a connection to SERVER.
10968 ;;
10969 ;; `choke-close-server &optional SERVER'
10970 ;; Close the connection to SERVER.
10971 ;;
10972 ;; `choke-server-opened &optional SERVER'
10973 ;; Whether the conenction to SERVER is opened or not.
10974 ;;
10975 ;; `choke-server-status &optional SERVER'
10976 ;; Should return a status string (not in the nntp buffer, but as the
10977 ;; result of the function).
10978 ;;
10979 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10980 ;; Optional function for retrieving active file info on all groups in
10981 ;; GROUPS.  Two return formats are supported: The normal active file
10982 ;; format, and a list of GROUP lines.  This function should return (as
10983 ;; a function value) either `active' or `group', depending on what
10984 ;; format it returns.
10985 ;;
10986 ;; The following functions are optional and apply only to backends
10987 ;; that are able to control the contents of their groups totally
10988 ;; (ie. mail backends.)  Backends that aren't able to do that
10989 ;; shouldn't define these functions at all. Gnus will check for their
10990 ;; presence before attempting to call them.
10991 ;;
10992 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10993 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10994 ;; backends will not be able to expire articles. Should return a list
10995 ;; of all articles that were not expired.
10996 ;;
10997 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10998 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10999 ;; Removes any information it has added to the article (extra headers,
11000 ;; whatever - make it as clean as possible), and then passes the
11001 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11002 ;; function described below. If the ACCEPT-FORM returns a non-nil
11003 ;; value, the article should then be deleted. If LAST is nil, that
11004 ;; means that there will be further calls to this function. This might
11005 ;; be taken as an advice not to save buffers/internal variables just
11006 ;; yet, but wait until the last call to speed things up.
11007 ;;
11008 ;; `choke-request-accept-article GROUP &optional LAST' 
11009 ;; The contents of the current buffer will be put into GROUP.  There
11010 ;; should, of course, be an article in the current buffer.  This
11011 ;; function is normally only called by the function described above,
11012 ;; and LAST works the same way as in that function.
11013 ;;
11014 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11015 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11016 ;; This provides an easy interface for allowing editing of
11017 ;; articles. Note that even headers may be edited, so the backend has
11018 ;; to update any tables (nov buffers, etc) that it maintains after
11019 ;; replacing the article.
11020 ;;
11021 ;; `choke-request-create-group GROUP &optional SERVER'
11022 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11023 ;; might be a group that already exists, but hasn't been registered
11024 ;; yet. 
11025 ;;
11026 ;; All these functions must return nil if they couldn't service the
11027 ;; request. If the optional arguments are not supplied, some "current"
11028 ;; or "default" values should be used. In short, one should emulate an
11029 ;; NNTP server, in a way.
11030 ;;
11031 ;; If you want to write a new backend, you just have to supply the
11032 ;; functions listed above. In addition, you must enter the new backend
11033 ;; into the list of valid select methods:
11034 ;; (setq gnus-valid-select-methods 
11035 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11036 ;; The first element in this list is the name of the backend. Other
11037 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11038 ;; groups), `none' (neither), `respool' (for groups that can control
11039 ;; their contents). 
11040
11041 (defun gnus-start-news-server (&optional confirm)
11042   "Open a method for getting news.
11043 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11044   (let (how)
11045     (if gnus-current-select-method
11046         ;; Stream is already opened.
11047         nil
11048       ;; Open NNTP server.
11049       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11050       (if confirm
11051           (progn
11052             ;; Read server name with completion.
11053             (setq gnus-nntp-server
11054                   (completing-read "NNTP server: "
11055                                    (mapcar (lambda (server) (list server))
11056                                            (cons (list gnus-nntp-server)
11057                                                  gnus-secondary-servers))
11058                                    nil nil gnus-nntp-server))))
11059
11060       (if (and gnus-nntp-server 
11061                (stringp gnus-nntp-server)
11062                (not (string= gnus-nntp-server "")))
11063           (setq gnus-select-method
11064                 (cond ((or (string= gnus-nntp-server "")
11065                            (string= gnus-nntp-server "::"))
11066                        (list 'nnspool (system-name)))
11067                       ((string-match "^:" gnus-nntp-server)
11068                        (list 'nnmh gnus-nntp-server 
11069                              (list 'nnmh-directory 
11070                                    (file-name-as-directory
11071                                     (expand-file-name
11072                                      (concat "~/" (substring
11073                                                    gnus-nntp-server 1)))))
11074                              (list 'nnmh-get-new-mail nil)))
11075                       (t
11076                        (list 'nntp gnus-nntp-server)))))
11077
11078       (setq how (car gnus-select-method))
11079       (cond ((eq how 'nnspool)
11080              (require 'nnspool)
11081              (gnus-message 5 "Looking up local news spool..."))
11082             ((eq how 'nnmh)
11083              (require 'nnmh)
11084              (gnus-message 5 "Looking up mh spool..."))
11085             (t
11086              (require 'nntp)))
11087       (setq gnus-current-select-method gnus-select-method)
11088       (run-hooks 'gnus-open-server-hook)
11089       (or 
11090        ;; gnus-open-server-hook might have opened it
11091        (gnus-server-opened gnus-select-method)  
11092        (gnus-open-server gnus-select-method)
11093        (gnus-y-or-n-p
11094         (format
11095          "%s server on %s can't be opened. Continue? "
11096          (car gnus-select-method) (nth 1 gnus-select-method)))
11097        (progn
11098          (gnus-message 1 "Couldn't open server on %s" 
11099                        (nth 1 gnus-select-method))
11100          (ding)
11101          nil)))))
11102
11103 (defun gnus-check-news-server (&optional method)
11104   "If the news server is down, start it up again."
11105   (let ((method (if method method gnus-select-method)))
11106     (and (stringp method)
11107          (setq method (gnus-server-to-method method)))
11108     (if (gnus-server-opened method)
11109         ;; Stream is already opened.
11110         t
11111       ;; Open server.
11112       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11113       (run-hooks 'gnus-open-server-hook)
11114       (or (gnus-server-opened method)
11115           (gnus-open-server method))
11116       (message ""))))
11117
11118 (defun gnus-nntp-message (&optional message)
11119   "Check the status of the NNTP server.
11120 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11121 is returned insted of the status string."
11122   (let ((status (gnus-status-message (gnus-find-method-for-group 
11123                                       gnus-newsgroup-name)))
11124         (message (or message "")))
11125     (if (and (stringp status) (> (length status) 0))
11126         status message)))
11127
11128 (defun gnus-get-function (method function)
11129   (and (stringp method)
11130        (setq method (gnus-server-to-method method)))
11131   (let ((func (intern (format "%s-%s" (car method) function))))
11132     (if (not (fboundp func)) 
11133         (progn
11134           (require (car method))
11135           (if (not (fboundp func)) 
11136               (error "No such function: %s" func))))
11137     func))
11138
11139 ;;; Interface functions to the backends.
11140
11141 (defun gnus-open-server (method)
11142   (funcall (gnus-get-function method 'open-server)
11143            (nth 1 method) (nthcdr 2 method)))
11144
11145 (defun gnus-close-server (method)
11146   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11147
11148 (defun gnus-request-list (method)
11149   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11150
11151 (defun gnus-request-list-newsgroups (method)
11152   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11153
11154 (defun gnus-request-newgroups (date method)
11155   (funcall (gnus-get-function method 'request-newgroups) 
11156            date (nth 1 method)))
11157
11158 (defun gnus-server-opened (method)
11159   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11160
11161 (defun gnus-status-message (method)
11162   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11163                   method)))
11164     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11165
11166 (defun gnus-request-group (group &optional dont-check)
11167   (let ((method (gnus-find-method-for-group group)))
11168 ;    (and t (message "%s GROUP %s" (car method) group))
11169     (funcall (gnus-get-function method 'request-group) 
11170              (gnus-group-real-name group) (nth 1 method) dont-check)))
11171
11172 (defun gnus-request-asynchronous (group &optional articles)
11173   (let ((method (gnus-find-method-for-group group)))
11174     (funcall (gnus-get-function method 'request-asynchronous) 
11175              (gnus-group-real-name group) (nth 1 method) articles)))
11176
11177 (defun gnus-list-active-group (group)
11178   (let ((method (gnus-find-method-for-group group))
11179         (func 'list-active-group))
11180     (and (gnus-check-backend-function func group)
11181          (funcall (gnus-get-function method func) 
11182                   (gnus-group-real-name group) (nth 1 method)))))
11183
11184 (defun gnus-request-group-description (group)
11185   (let ((method (gnus-find-method-for-group group))
11186         (func 'request-group-description))
11187     (and (gnus-check-backend-function func group)
11188          (funcall (gnus-get-function method func) 
11189                   (gnus-group-real-name group) (nth 1 method)))))
11190
11191 (defun gnus-close-group (group)
11192   (let ((method (gnus-find-method-for-group group)))
11193     (funcall (gnus-get-function method 'close-group) 
11194              (gnus-group-real-name group) (nth 1 method))))
11195
11196 (defun gnus-retrieve-headers (articles group)
11197   (let ((method (gnus-find-method-for-group group)))
11198     (if (and gnus-use-cache (numberp (car articles)))
11199         (gnus-cache-retrieve-headers articles group)
11200       (funcall (gnus-get-function method 'retrieve-headers) 
11201                articles (gnus-group-real-name group) (nth 1 method)))))
11202
11203 (defun gnus-retrieve-groups (groups method)
11204   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11205
11206 (defun gnus-request-article (article group &optional buffer)
11207   (let ((method (gnus-find-method-for-group group)))
11208     (funcall (gnus-get-function method 'request-article) 
11209              article (gnus-group-real-name group) (nth 1 method) buffer)))
11210
11211 (defun gnus-request-head (article group)
11212   (let ((method (gnus-find-method-for-group group)))
11213     (funcall (gnus-get-function method 'request-head) 
11214              article (gnus-group-real-name group) (nth 1 method))))
11215
11216 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11217 (defun gnus-request-post-buffer (post group subject header artbuf
11218                                       info follow-to respect-poster)
11219    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11220                                             group gnus-newsrc-hashtb)))))
11221           (method
11222            (if (and gnus-post-method
11223                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11224                     (memq 'post (assoc
11225                                  (format "%s" (car (gnus-find-method-for-group
11226                                                     gnus-newsgroup-name)))
11227                                         gnus-valid-select-methods)))
11228                gnus-post-method
11229              (gnus-find-method-for-group gnus-newsgroup-name))))
11230      (or (gnus-server-opened method)
11231          (gnus-open-server method)
11232          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11233      (let ((mail-self-blind nil)
11234            (mail-archive-file-name nil))
11235        (funcall (gnus-get-function method 'request-post-buffer) 
11236                 post group subject header artbuf info follow-to
11237                 respect-poster))))
11238
11239 (defun gnus-request-post (method &optional force)
11240   (and (stringp method)
11241        (setq method (gnus-server-to-method method)))
11242   (and (not force) gnus-post-method
11243        (memq 'post (assoc (format "%s" (car method))
11244                           gnus-valid-select-methods))
11245        (setq method gnus-post-method))
11246   (funcall (gnus-get-function method 'request-post) 
11247            (nth 1 method)))
11248
11249 (defun gnus-request-expire-articles (articles group &optional force)
11250   (let ((method (gnus-find-method-for-group group)))
11251     (funcall (gnus-get-function method 'request-expire-articles) 
11252              articles (gnus-group-real-name group) (nth 1 method)
11253              force)))
11254
11255 (defun gnus-request-move-article 
11256   (article group server accept-function &optional last)
11257   (let ((method (gnus-find-method-for-group group)))
11258     (funcall (gnus-get-function method 'request-move-article) 
11259              article (gnus-group-real-name group) 
11260              (nth 1 method) accept-function last)))
11261
11262 (defun gnus-request-accept-article (group &optional last)
11263   (let ((func (if (symbolp group) group
11264                 (car (gnus-find-method-for-group group)))))
11265     (funcall (intern (format "%s-request-accept-article" func))
11266              (if (stringp group) (gnus-group-real-name group) group)
11267              last)))
11268
11269 (defun gnus-request-replace-article (article group buffer)
11270   (let ((func (car (gnus-find-method-for-group group))))
11271     (funcall (intern (format "%s-request-replace-article" func))
11272              article (gnus-group-real-name group) buffer)))
11273
11274 (defun gnus-request-create-group (group)
11275   (let ((method (gnus-find-method-for-group group)))
11276     (funcall (gnus-get-function method 'request-create-group) 
11277              (gnus-group-real-name group) (nth 1 method))))
11278
11279 (defun gnus-member-of-valid (symbol group)
11280   (memq symbol (assoc
11281                 (format "%s" (car (gnus-find-method-for-group group)))
11282                 gnus-valid-select-methods)))
11283
11284 (defsubst gnus-secondary-method-p (method)
11285   (member method gnus-secondary-select-methods))
11286
11287 (defun gnus-find-method-for-group (group &optional info)
11288   (or gnus-override-method
11289       (and (not group)
11290            gnus-select-method)
11291       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11292             method)
11293         (if (or (not info)
11294                 (not (setq method (nth 4 info))))
11295             (setq method gnus-select-method)
11296           (setq method
11297                 (cond ((stringp method)
11298                        (gnus-server-to-method method))
11299                       ((stringp (car method))
11300                        (gnus-server-extend-method group method))
11301                       (t
11302                        method))))
11303         (gnus-server-add-address method))))
11304
11305 (defun gnus-check-backend-function (func group)
11306   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11307                  group)))
11308     (fboundp (intern (format "%s-%s" method func)))))
11309
11310 (defun gnus-methods-using (method)
11311   (let ((valids gnus-valid-select-methods)
11312         outs)
11313     (while valids
11314       (if (memq method (car valids)) 
11315           (setq outs (cons (car valids) outs)))
11316       (setq valids (cdr valids)))
11317     outs))
11318
11319 ;;; 
11320 ;;; Active & Newsrc File Handling
11321 ;;;
11322
11323 ;; Newsrc related functions.
11324 ;; Gnus internal format of gnus-newsrc-alist:
11325 ;; (("alt.general" 3 (1 . 1))
11326 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11327 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11328 ;; The first item is the group name; the second is the subscription
11329 ;; level; the third is either a range of a list of ranges of read
11330 ;; articles, the optional fourth element is a list of marked articles,
11331 ;; the optional fifth element is the select method.
11332 ;;
11333 ;; Gnus internal format of gnus-newsrc-hashtb:
11334 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11335 ;; This is the entry for "alt.misc". The first element is the number
11336 ;; of unread articles in "alt.misc". The cdr of this entry is the
11337 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11338 ;; trivial to remove or add new elements into gnus-newsrc-alist
11339 ;; without scanning the entire list. So, to get the actual information
11340 ;; of "alt.misc", you'd say something like 
11341 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11342 ;;
11343 ;; Gnus internal format of gnus-active-hashtb:
11344 ;; ((1 . 1))
11345 ;;  (5 . 10))
11346 ;;  (67 . 99)) ...)
11347 ;; The only element in each entry in this hash table is a range of
11348 ;; (possibly) available articles. (Articles in this range may have
11349 ;; been expired or canceled.)
11350 ;;
11351 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11352 ;; ("alt.misc" "alt.test" "alt.general" ...)
11353
11354 (defun gnus-setup-news (&optional rawfile level)
11355   "Setup news information.
11356 If RAWFILE is non-nil, the .newsrc file will also be read.
11357 If LEVEL is non-nil, the news will be set up at level LEVEL."
11358   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11359     ;; Clear some variables to re-initialize news information.
11360     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11361
11362     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11363     (if init (gnus-read-newsrc-file rawfile))
11364
11365     ;; Read the active file and create `gnus-active-hashtb'.
11366     ;; If `gnus-read-active-file' is nil, then we just create an empty
11367     ;; hash table. The partial filling out of the hash table will be
11368     ;; done in `gnus-get-unread-articles'.
11369     (if (and gnus-read-active-file 
11370              (not level))
11371         (gnus-read-active-file)
11372       (setq gnus-active-hashtb (make-vector 4095 0)))
11373
11374     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11375
11376     ;; Find the number of unread articles in each non-dead group.
11377     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11378     ;; Find new newsgroups and treat them.
11379     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11380              (gnus-server-opened gnus-select-method))
11381         (gnus-find-new-newsgroups))
11382     (if (and init gnus-check-bogus-newsgroups 
11383              gnus-read-active-file (not level)
11384              (gnus-server-opened gnus-select-method))
11385         (gnus-check-bogus-newsgroups))))
11386
11387 (defun gnus-find-new-newsgroups ()
11388   "Search for new newsgroups and add them.
11389 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11390 The `-n' option line from .newsrc is respected."
11391   (interactive)
11392   (or (gnus-check-first-time-used)
11393       (if (or (consp gnus-check-new-newsgroups)
11394               (eq gnus-check-new-newsgroups 'ask-server))
11395           (gnus-ask-server-for-new-groups)
11396         (let ((groups 0)
11397               group new-newsgroups)
11398           (or gnus-have-read-active-file (gnus-read-active-file))
11399           (setq gnus-newsrc-last-checked-date (current-time-string))
11400           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11401           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11402           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11403           (mapatoms
11404            (lambda (sym)
11405              (setq group (symbol-name sym))
11406              (if (or (gnus-gethash group gnus-killed-hashtb)
11407                      (gnus-gethash group gnus-newsrc-hashtb))
11408                  ()
11409                (let ((do-sub (gnus-matches-options-n group)))
11410                  (cond ((eq do-sub 'subscribe)
11411                         (setq groups (1+ groups))
11412                         (gnus-sethash group group gnus-killed-hashtb)
11413                         (funcall 
11414                          gnus-subscribe-options-newsgroup-method group))
11415                        ((eq do-sub 'ignore)
11416                         nil)
11417                        (t
11418                         (setq groups (1+ groups))
11419                         (gnus-sethash group group gnus-killed-hashtb)
11420                         (if gnus-subscribe-hierarchical-interactive
11421                             (setq new-newsgroups (cons group new-newsgroups))
11422                           (funcall gnus-subscribe-newsgroup-method group)))))))
11423            gnus-active-hashtb)
11424           (if new-newsgroups 
11425               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11426           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11427           (if (> groups 0)
11428               (gnus-message 6 "%d new newsgroup%s arrived." 
11429                             groups (if (> groups 1) "s have" " has")))))))
11430
11431 (defun gnus-matches-options-n (group)
11432   ;; Returns `subscribe' if the group is to be uncoditionally
11433   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11434   ;; no match for the group.
11435
11436   ;; First we check the two user variables.
11437   (cond
11438    ((and gnus-options-subscribe
11439          (string-match gnus-options-subscribe group))
11440     'subscribe)
11441    ((and gnus-options-not-subscribe
11442          (string-match gnus-options-not-subscribe group))
11443     'ignore)
11444    ;; Then we go through the list that was retrieved from the .newsrc
11445    ;; file.  This list has elements on the form 
11446    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11447    ;; is in the reverse order of the options line) is returned.
11448    (t
11449     (let ((regs gnus-newsrc-options-n))
11450       (while (and regs
11451                   (not (string-match (car (car regs)) group)))
11452         (setq regs (cdr regs)))
11453       (and regs (cdr (car regs)))))))
11454
11455 (defun gnus-ask-server-for-new-groups ()
11456   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11457          (methods (cons gnus-select-method 
11458                         (append
11459                          (and (consp gnus-check-new-newsgroups)
11460                               gnus-check-new-newsgroups)
11461                          gnus-secondary-select-methods)))
11462          (groups 0)
11463          (new-date (current-time-string))
11464          hashtb group new-newsgroups got-new)
11465     ;; Go thorugh both primary and secondary select methods and
11466     ;; request new newsgroups.  
11467     (while methods
11468       (and (or (gnus-server-opened (car methods))
11469                (gnus-open-server (car methods)))
11470            (gnus-request-newgroups date (car methods))
11471            (save-excursion
11472              (setq got-new t)
11473              (set-buffer nntp-server-buffer)
11474              (or hashtb (setq hashtb (gnus-make-hashtable 
11475                                       (count-lines (point-min) (point-max)))))
11476              ;; Enter all the new groups in a hashtable.
11477              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11478       (setq methods (cdr methods)))
11479     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11480     ;; Now all new groups from all select methods are in `hashtb'.
11481     (mapatoms
11482      (lambda (group-sym)
11483        (setq group (symbol-name group-sym))
11484        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11485                (member group gnus-zombie-list)
11486                (member group gnus-killed-list))
11487            ;; The group is already known.
11488            ()
11489          (and (symbol-value group-sym)
11490               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11491          (let ((do-sub (gnus-matches-options-n group)))
11492            (cond ((eq do-sub 'subscribe)
11493                   (setq groups (1+ groups))
11494                   (gnus-sethash group group gnus-killed-hashtb)
11495                   (funcall 
11496                    gnus-subscribe-options-newsgroup-method group))
11497                  ((eq do-sub 'ignore)
11498                   nil)
11499                  (t
11500                   (setq groups (1+ groups))
11501                   (gnus-sethash group group gnus-killed-hashtb)
11502                   (if gnus-subscribe-hierarchical-interactive
11503                       (setq new-newsgroups (cons group new-newsgroups))
11504                     (funcall gnus-subscribe-newsgroup-method group)))))))
11505      hashtb)
11506     (if new-newsgroups 
11507         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11508     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11509     (if (> groups 0)
11510         (gnus-message 6 "%d new newsgroup%s arrived." 
11511                       groups (if (> groups 1) "s have" " has")))
11512     got-new))
11513
11514 (defun gnus-check-first-time-used ()
11515   (if (or (> (length gnus-newsrc-alist) 1)
11516           (file-exists-p gnus-startup-file)
11517           (file-exists-p (concat gnus-startup-file ".el"))
11518           (file-exists-p (concat gnus-startup-file ".eld")))
11519       nil
11520     (gnus-message 6 "First time user; subscribing you to default groups")
11521     (or gnus-have-read-active-file (gnus-read-active-file))
11522     (setq gnus-newsrc-last-checked-date (current-time-string))
11523     (let ((groups gnus-default-subscribed-newsgroups)
11524           group)
11525       (if (eq groups t)
11526           nil
11527         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11528         (mapatoms
11529          (lambda (sym)
11530            (setq group (symbol-name sym))
11531            (let ((do-sub (gnus-matches-options-n group)))
11532              (cond ((eq do-sub 'subscribe)
11533                     (gnus-sethash group group gnus-killed-hashtb)
11534                     (funcall 
11535                      gnus-subscribe-options-newsgroup-method group))
11536                    ((eq do-sub 'ignore)
11537                     nil)
11538                    (t
11539                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11540          gnus-active-hashtb)
11541         (while groups
11542           (if (gnus-gethash (car groups) gnus-active-hashtb)
11543               (gnus-group-change-level 
11544                (car groups) gnus-level-default-subscribed gnus-level-killed))
11545           (setq groups (cdr groups)))
11546         (gnus-group-make-help-group)
11547         (and gnus-novice-user
11548              (gnus-message 7 "`A k' to list killed groups"))))))
11549
11550 (defun gnus-subscribe-group (group previous &optional method)
11551   (gnus-group-change-level 
11552    (if method
11553        (list t group gnus-level-default-subscribed nil nil method)
11554      group) 
11555    gnus-level-default-subscribed gnus-level-killed previous t))
11556
11557 ;; `gnus-group-change-level' is the fundamental function for changing
11558 ;; subscription levels of newsgroups. This might mean just changing
11559 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11560 ;; again, which subscribes/unsubscribes a group, which is equally
11561 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11562 ;; from 8-9 to 1-7 means that you remove the group from the list of
11563 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11564 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11565 ;; which is trivial.
11566 ;; ENTRY can either be a string (newsgroup name) or a list (if
11567 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11568 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11569 ;; entries. 
11570 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11571 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11572 ;; after. 
11573 (defun gnus-group-change-level (entry level &optional oldlevel
11574                                       previous fromkilled)
11575   (let (group info active num)
11576     ;; Glean what info we can from the arguments
11577     (if (consp entry)
11578         (if fromkilled (setq group (nth 1 entry))
11579           (setq group (car (nth 2 entry))))
11580       (setq group entry))
11581     (if (and (stringp entry)
11582              oldlevel 
11583              (< oldlevel gnus-level-zombie))
11584         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11585     (if (and (not oldlevel)
11586              (consp entry))
11587         (setq oldlevel (car (cdr (nth 2 entry)))))
11588     (if (stringp previous)
11589         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11590
11591     (gnus-dribble-enter
11592      (format "(gnus-group-change-level %S %S %S %S %S)" 
11593              group level oldlevel (car (nth 2 previous)) fromkilled))
11594     
11595     ;; Then we remove the newgroup from any old structures, if needed.
11596     ;; If the group was killed, we remove it from the killed or zombie
11597     ;; list. If not, and it is in fact going to be killed, we remove
11598     ;; it from the newsrc hash table and assoc.
11599     (cond ((>= oldlevel gnus-level-zombie)
11600            (if (= oldlevel gnus-level-zombie)
11601                (setq gnus-zombie-list (delete group gnus-zombie-list))
11602              (setq gnus-killed-list (delete group gnus-killed-list))))
11603           (t
11604            (if (>= level gnus-level-zombie)
11605                (progn
11606                  (gnus-sethash (car (nth 2 entry))
11607                                nil gnus-newsrc-hashtb)
11608                  (if (nth 3 entry)
11609                      (setcdr (gnus-gethash (car (nth 3 entry))
11610                                            gnus-newsrc-hashtb)
11611                              (cdr entry)))
11612                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11613
11614     ;; Finally we enter (if needed) the list where it is supposed to
11615     ;; go, and change the subscription level. If it is to be killed,
11616     ;; we enter it into the killed or zombie list.
11617     (cond ((>= level gnus-level-zombie)
11618            ;; Remove from the hash table.
11619            (gnus-sethash group nil gnus-newsrc-hashtb)
11620            (or (gnus-group-foreign-p group)
11621                ;; We do not enter foreign groups into the list of dead
11622                ;; groups.  
11623                (if (= level gnus-level-zombie)
11624                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11625                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11626           (t
11627            ;; If the list is to be entered into the newsrc assoc, and
11628            ;; it was killed, we have to create an entry in the newsrc
11629            ;; hashtb format and fix the pointers in the newsrc assoc.
11630            (if (>= oldlevel gnus-level-zombie)
11631                (progn
11632                  (if (listp entry)
11633                      (progn
11634                        (setq info (cdr entry))
11635                        (setq num (car entry)))
11636                    (setq active (gnus-gethash group gnus-active-hashtb))
11637                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11638                    ;; Check whether the group is foreign. If so, the
11639                    ;; foreign select method has to be entered into the
11640                    ;; info. 
11641                    (let ((method (gnus-group-method-name group)))
11642                      (if (eq method gnus-select-method)
11643                          (setq info (list group level nil))
11644                        (setq info (list group level nil nil method)))))
11645                  (or previous 
11646                      (setq previous 
11647                            (let ((p gnus-newsrc-alist))
11648                              (while (cdr (cdr p))
11649                                (setq p (cdr p)))
11650                              p)))
11651                  (setq entry (cons info (cdr (cdr previous))))
11652                  (if (cdr previous)
11653                      (progn
11654                        (setcdr (cdr previous) entry)
11655                        (gnus-sethash group (cons num (cdr previous)) 
11656                                      gnus-newsrc-hashtb))
11657                    (setcdr previous entry)
11658                    (gnus-sethash group (cons num previous)
11659                                  gnus-newsrc-hashtb))
11660                  (if (cdr entry)
11661                      (setcdr (gnus-gethash (car (car (cdr entry)))
11662                                            gnus-newsrc-hashtb)
11663                              entry)))
11664              ;; It was alive, and it is going to stay alive, so we
11665              ;; just change the level and don't change any pointers or
11666              ;; hash table entries.
11667              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11668
11669 (defun gnus-kill-newsgroup (newsgroup)
11670   "Obsolete function. Kills a newsgroup."
11671   (gnus-group-change-level
11672    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11673
11674 (defun gnus-check-bogus-newsgroups (&optional confirm)
11675   "Remove bogus newsgroups.
11676 If CONFIRM is non-nil, the user has to confirm the deletion of every
11677 newsgroup." 
11678   (let ((newsrc (cdr gnus-newsrc-alist))
11679         bogus group entry)
11680     (gnus-message 5 "Checking bogus newsgroups...")
11681     (or gnus-have-read-active-file (gnus-read-active-file))
11682     ;; Find all bogus newsgroup that are subscribed.
11683     (while newsrc
11684       (setq group (car (car newsrc)))
11685       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11686               (nth 4 (car newsrc))      ; Foreign
11687               (and confirm
11688                    (not (gnus-y-or-n-p
11689                          (format "Remove bogus newsgroup: %s " group)))))
11690           ;; Don't remove.
11691           ()
11692         ;; Found a bogus newsgroup.
11693         (setq bogus (cons group bogus)))
11694       (setq newsrc (cdr newsrc)))
11695     ;; Remove all bogus subscribed groups by first killing them, and
11696     ;; then removing them from the list of killed groups.
11697     (while bogus
11698       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11699            (progn
11700              (gnus-group-change-level entry gnus-level-killed)
11701              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11702       (setq bogus (cdr bogus)))
11703     ;; Then we remove all bogus groups from the list of killed and
11704     ;; zombie groups. They are are removed without confirmation.
11705     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11706           killed)
11707       (while dead-lists
11708         (setq killed (symbol-value (car dead-lists)))
11709         (while killed
11710           (setq group (car killed))
11711           (or (gnus-gethash group gnus-active-hashtb)
11712               ;; The group is bogus.
11713               (set (car dead-lists)
11714                    (delete group (symbol-value (car dead-lists)))))
11715           (setq killed (cdr killed)))
11716         (setq dead-lists (cdr dead-lists))))
11717     (gnus-message 5 "Checking bogus newsgroups...done")))
11718
11719 (defun gnus-check-duplicate-killed-groups ()
11720   "Remove duplicates from the list of killed groups."
11721   (interactive)
11722   (let ((killed gnus-killed-list))
11723     (while killed
11724       (gnus-message 9 "%d" (length killed))
11725       (setcdr killed (delete (car killed) (cdr killed)))
11726       (setq killed (cdr killed)))))
11727
11728 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11729 ;; and compute how many unread articles there are in each group.
11730 (defun gnus-get-unread-articles (&optional level) 
11731   (let* ((newsrc (cdr gnus-newsrc-alist))
11732          (level (or level (1+ gnus-level-subscribed)))
11733          (foreign-level
11734           (min 
11735            (cond ((and gnus-activate-foreign-newsgroups 
11736                        (not (numberp gnus-activate-foreign-newsgroups)))
11737                   (1+ gnus-level-subscribed))
11738                  ((numberp gnus-activate-foreign-newsgroups)
11739                   gnus-activate-foreign-newsgroups)
11740                  (t 0))
11741            level))
11742          info group active virtuals method)
11743     (gnus-message 5 "Checking new news...")
11744
11745     (while newsrc
11746       (setq info (car newsrc)
11747             group (car info)
11748             active (gnus-gethash group gnus-active-hashtb))
11749
11750       ;; Check newsgroups. If the user doesn't want to check them, or
11751       ;; they can't be checked (for instance, if the news server can't
11752       ;; be reached) we just set the number of unread articles in this
11753       ;; newsgroup to t. This means that Gnus thinks that there are
11754       ;; unread articles, but it has no idea how many.
11755       (if (and (setq method (nth 4 info))
11756                (not (gnus-server-equal gnus-select-method
11757                                        (gnus-server-get-method nil method)))
11758                (not (gnus-secondary-method-p method)))
11759           ;; These groups are foreign. Check the level.
11760           (if (<= (nth 1 info) foreign-level)
11761               (if (eq (car (if (stringp method) 
11762                                (gnus-server-to-method method)
11763                              (nth 4 info))) 'nnvirtual)
11764                   ;; We have to activate the virtual groups after all
11765                   ;; the others, so we just pop them on a list for
11766                   ;; now. 
11767                   (setq virtuals (cons info virtuals))
11768                 (and (setq active (gnus-activate-newsgroup (car info)))
11769                      ;; Close the groups as we look at them!
11770                      (gnus-close-group group))))
11771                 
11772         ;; These groups are native or secondary. 
11773         (if (and (not gnus-have-read-active-file)
11774                  (<= (nth 1 info) level))
11775             (setq active (gnus-activate-newsgroup (car info)))))
11776       
11777       (if active
11778           (gnus-get-unread-articles-in-group info active)
11779         ;; The group couldn't be reached, so we nix out the number of
11780         ;; unread articles and stuff.
11781         (gnus-sethash group nil gnus-active-hashtb)
11782         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
11783
11784       (setq newsrc (cdr newsrc)))
11785
11786     ;; Activate the virtual groups. This has to be done after all the
11787     ;; other groups. 
11788     ;; !!! If one virtual group contains another virtual group, even
11789     ;; doing it this way might cause problems.
11790    (while virtuals
11791       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11792            (gnus-get-unread-articles-in-group (car virtuals) active))
11793       (setq virtuals (cdr virtuals)))
11794
11795     (gnus-message 5 "Checking new news...done")))
11796
11797 ;; Create a hash table out of the newsrc alist. The `car's of the
11798 ;; alist elements are used as keys.
11799 (defun gnus-make-hashtable-from-newsrc-alist ()
11800   (let ((alist gnus-newsrc-alist)
11801         (ohashtb gnus-newsrc-hashtb)
11802         prev)
11803     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11804     (setq alist 
11805           (setq prev (setq gnus-newsrc-alist 
11806                            (if (equal (car (car gnus-newsrc-alist))
11807                                       "dummy.group")
11808                                gnus-newsrc-alist
11809                              (cons (list "dummy.group" 0 nil) alist)))))
11810     (while alist
11811       (gnus-sethash (car (car alist)) 
11812                     (cons (and ohashtb (car (gnus-gethash 
11813                                              (car (car alist)) ohashtb))) 
11814                           prev) gnus-newsrc-hashtb)
11815       (setq prev alist
11816             alist (cdr alist)))))
11817
11818 (defun gnus-make-hashtable-from-killed ()
11819   "Create a hash table from the killed and zombie lists."
11820   (let ((lists '(gnus-killed-list gnus-zombie-list))
11821         list)
11822     (setq gnus-killed-hashtb 
11823           (gnus-make-hashtable 
11824            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11825     (while lists
11826       (setq list (symbol-value (car lists)))
11827       (setq lists (cdr lists))
11828       (while list
11829         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11830         (setq list (cdr list))))))
11831
11832 (defun gnus-get-unread-articles-in-group (info active)
11833   (let* ((range (nth 2 info))
11834          (num 0)
11835          (marked (nth 3 info)))
11836     ;; If a cache is present, we may have to alter the active info.
11837     (and gnus-use-cache
11838          (gnus-cache-possibly-alter-active (car info) active))
11839     ;; Modify the list of read articles according to what articles 
11840     ;; are available; then tally the unread articles and add the
11841     ;; number to the group hash table entry.
11842     (cond ((zerop (cdr active))
11843            (setq num 0))
11844           ((not range)
11845            (setq num (- (1+ (cdr active)) (car active))))
11846           ((not (listp (cdr range)))
11847            ;; Fix a single (num . num) range according to the
11848            ;; active hash table.
11849            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11850            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11851            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11852            ;; Compute number of unread articles.
11853            (setq num (max 0 (- (cdr active) 
11854                                (- (1+ (cdr range)) (car range))))))
11855           (t
11856            ;; The read list is a list of ranges. Fix them according to
11857            ;; the active hash table.
11858            ;; First peel off any elements that are below the lower
11859            ;; active limit. 
11860            (while (and (cdr range) 
11861                        (>= (car active) 
11862                            (or (and (atom (car (cdr range))) (car (cdr range)))
11863                                (car (car (cdr range))))))
11864              (if (numberp (car range))
11865                  (setcar range 
11866                          (cons (car range) 
11867                                (or (and (numberp (car (cdr range)))
11868                                         (car (cdr range))) 
11869                                    (cdr (car (cdr range))))))
11870                (setcdr (car range) 
11871                        (or (and (numberp (nth 1 range)) (nth 1 range))
11872                            (cdr (car (cdr range))))))
11873              (setcdr range (cdr (cdr range))))
11874            ;; Adjust the first element to be the same as the lower limit. 
11875            (if (and (not (atom (car range))) 
11876                     (< (cdr (car range)) (car active)))
11877                (setcdr (car range) (1- (car active))))
11878            ;; Then we want to peel off any elements that are higher
11879            ;; than the upper active limit.  
11880            (let ((srange range))
11881              ;; Go past all legal elements.
11882              (while (and (cdr srange) 
11883                          (<= (or (and (atom (car (cdr srange)))
11884                                       (car (cdr srange)))
11885                                  (car (car (cdr srange)))) (cdr active)))
11886                (setq srange (cdr srange)))
11887              (if (cdr srange)
11888                  ;; Nuke all remaining illegal elements.
11889                  (setcdr srange nil))
11890
11891              ;; Adjust the final element.
11892              (if (and (not (atom (car srange)))
11893                       (> (cdr (car srange)) (cdr active)))
11894                  (setcdr (car srange) (cdr active))))
11895            ;; Compute the number of unread articles.
11896            (while range
11897              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11898                                          (cdr (car range))))
11899                                  (or (and (atom (car range)) (car range))
11900                                      (car (car range))))))
11901              (setq range (cdr range)))
11902            (setq num (max 0 (- (cdr active) num)))))
11903     (and info
11904          (progn
11905            (and (assq 'tick marked)
11906                 (inline (gnus-remove-illegal-marked-articles
11907                          (assq 'tick marked) (nth 2 info))))
11908            (and (assq 'dormant marked)
11909                 (inline (gnus-remove-illegal-marked-articles
11910                          (assq 'dormant marked) (nth 2 info))))
11911            (setcar
11912             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11913             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11914                                 (length (cdr (assq 'dormant marked)))))))))
11915     num))
11916
11917 (defun gnus-remove-illegal-marked-articles (marked ranges)
11918   (let ((m (cdr marked)))
11919     ;; Make sure that all ticked articles are a subset of the unread
11920     ;; articles. 
11921     (while m
11922       (if (gnus-member-of-range (car m) ranges)
11923           (setcdr marked (cdr m))
11924         (setq marked m))
11925       (setq m (cdr m)))))
11926
11927 (defun gnus-activate-newsgroup (group)
11928   (let ((method (gnus-find-method-for-group group))
11929         active)
11930     (and (or (gnus-server-opened method) (gnus-open-server method))
11931          (gnus-request-group group)
11932          (save-excursion
11933            (set-buffer nntp-server-buffer)
11934            (goto-char (point-min))
11935            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11936                 (progn
11937                   (goto-char (match-beginning 1))
11938                   (gnus-sethash 
11939                    group (setq active (cons (read (current-buffer))
11940                                             (read (current-buffer))))
11941                    gnus-active-hashtb))
11942                 active)))))
11943
11944 (defun gnus-update-read-articles 
11945   (group unread unselected ticked &optional domarks replied expirable killed
11946          dormant bookmark score)
11947   "Update the list of read and ticked articles in GROUP using the
11948 UNREAD and TICKED lists.
11949 Note: UNSELECTED has to be sorted over `<'.
11950 Returns whether the updating was successful."
11951   (let* ((active (or gnus-newsgroup-active 
11952                      (gnus-gethash group gnus-active-hashtb)))
11953          (entry (gnus-gethash group gnus-newsrc-hashtb))
11954          (info (nth 2 entry))
11955          (marked (nth 3 info))
11956          (prev 1)
11957          (unread (sort (copy-sequence unread) (function <)))
11958          read)
11959     (if (or (not info) (not active))
11960         ;; There is no info on this group if it was, in fact,
11961         ;; killed. Gnus stores no information on killed groups, so
11962         ;; there's nothing to be done. 
11963         ;; One could store the information somewhere temporarily,
11964         ;; perhaps... Hmmm... 
11965         ()
11966       ;; Remove any negative articles numbers.
11967       (while (and unread (< (car unread) 0))
11968         (setq unread (cdr unread)))
11969       ;; Remove any expired article numbers
11970       (while (and unread (< (car unread) (car active)))
11971         (setq unread (cdr unread)))
11972       (while (and ticked (< (car ticked) (car active)))
11973         (setq ticked (cdr ticked)))
11974       (while (and dormant (< (car dormant) (car active)))
11975         (setq dormant (cdr dormant)))
11976       (setq unread (sort (append unselected unread) '<))
11977       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11978       (setcar entry (max 0 (- (length unread) (length ticked) 
11979                               (length dormant))))
11980       ;; Compute the ranges of read articles by looking at the list of
11981       ;; unread articles.  
11982       (while unread
11983         (if (/= (car unread) prev)
11984             (setq read (cons (if (= prev (1- (car unread))) prev
11985                                (cons prev (1- (car unread)))) read)))
11986         (setq prev (1+ (car unread)))
11987         (setq unread (cdr unread)))
11988       (if (<= prev (cdr active))
11989           (setq read (cons (cons prev (cdr active)) read)))
11990       ;; Enter this list into the group info.
11991       (setcar (cdr (cdr info)) 
11992               (if (> (length read) 1) (nreverse read) read))
11993       ;; Enter the list of ticked articles.
11994       (gnus-set-marked-articles 
11995        info ticked
11996        (if domarks replied (cdr (assq 'reply marked)))
11997        (if domarks expirable (cdr (assq 'expire marked)))
11998        (if domarks killed (cdr (assq 'killed marked)))
11999        (if domarks dormant (cdr (assq 'dormant marked)))
12000        (if domarks bookmark (cdr (assq 'bookmark marked)))
12001        (if domarks score (cdr (assq 'score marked))))
12002       t)))
12003
12004 (defun gnus-make-articles-unread (group articles)
12005   "Mark ARTICLES in GROUP as unread."
12006   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12007                           (gnus-gethash (gnus-group-real-name group)
12008                                         gnus-newsrc-hashtb))))
12009          (ranges (nth 2 info))
12010          news)
12011     (while articles
12012       (and (gnus-member-of-range (car articles) ranges)
12013            (setq news (cons (car articles) news)))
12014       (setq articles (cdr articles)))
12015     (if (not news)
12016         ()
12017       (setcar (nthcdr 2 info)
12018               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12019       (gnus-group-update-group group t))))
12020
12021 ;; Get the active file(s) from the backend(s).
12022 (defun gnus-read-active-file ()
12023   (gnus-group-set-mode-line)
12024   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12025                          (gnus-open-server gnus-select-method))
12026                      ;; The native server is available.
12027                      (cons gnus-select-method gnus-secondary-select-methods)
12028                    ;; The native server is down, so we just do the
12029                    ;; secondary ones.   
12030                    gnus-secondary-select-methods))
12031         list-type)
12032     (setq gnus-have-read-active-file nil)
12033     (save-excursion
12034       (set-buffer nntp-server-buffer)
12035       (while methods
12036         (let* ((where (nth 1 (car methods)))
12037                (mesg (format "Reading active file%s via %s..."
12038                              (if (and where (not (zerop (length where))))
12039                                  (concat " from " where) "")
12040                              (car (car methods)))))
12041           (gnus-message 5 mesg)
12042           (gnus-check-news-server (car methods))
12043           (cond 
12044            ((and (eq gnus-read-active-file 'some)
12045                  (gnus-check-backend-function
12046                   'retrieve-groups (car (car methods))))
12047             (let ((newsrc (cdr gnus-newsrc-alist))
12048                   groups)
12049               (while newsrc
12050                 (and (gnus-server-equal 
12051                       (gnus-find-method-for-group
12052                        (car (car newsrc)) (car newsrc))
12053                       (gnus-server-get-method nil (car methods)))
12054                      (setq groups (cons (car (car newsrc)) groups)))
12055                 (setq newsrc (cdr newsrc)))
12056               (setq list-type (gnus-retrieve-groups groups (car methods)))
12057               (cond ((not list-type)
12058                      (gnus-message 
12059                       1 "Cannot read partial active file from %s server." 
12060                       (car (car methods)))
12061                      (ding)
12062                      (sit-for 2))
12063                     ((eq list-type 'active)
12064                      (gnus-active-to-gnus-format (car methods)))
12065                     (t
12066                      (gnus-groups-to-gnus-format (car methods))))))
12067            (t
12068             (if (not (gnus-request-list (car methods)))
12069                 (progn
12070                   (gnus-message 1 "Cannot read active file from %s server." 
12071                                 (car (car methods)))
12072                   (ding))
12073               (gnus-active-to-gnus-format (car methods))
12074               (setq gnus-have-read-active-file t)
12075               (gnus-message 5 "%sdone" mesg)))))
12076         (setq methods (cdr methods))))))
12077
12078 ;; Read an active file and place the results in `gnus-active-hashtb'.
12079 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12080   (let ((cur (current-buffer))
12081         (hashtb (or hashtb 
12082                     (if (and gnus-active-hashtb 
12083                              (not (equal method gnus-select-method)))
12084                         gnus-active-hashtb
12085                       (setq gnus-active-hashtb
12086                             (if (equal method gnus-select-method)
12087                                 (gnus-make-hashtable 
12088                                  (count-lines (point-min) (point-max)))
12089                               (gnus-make-hashtable 4096)))))))
12090     ;; Delete unnecessary lines.
12091     (goto-char (point-min))
12092     (while (search-forward "\nto." nil t)
12093       (delete-region (1+ (match-beginning 0)) 
12094                      (progn (forward-line 1) (point))))
12095     (or (string= gnus-ignored-newsgroups "")
12096         (progn
12097           (goto-char (point-min))
12098           (delete-matching-lines gnus-ignored-newsgroups)))
12099     ;; If these are groups from a foreign select method, we insert the
12100     ;; group prefix in front of the group names. 
12101     (and method (not (eq method gnus-select-method))
12102          (let ((prefix (gnus-group-prefixed-name "" method)))
12103            (goto-char (point-min))
12104            (while (and (not (eobp))
12105                        (progn (insert prefix)
12106                               (zerop (forward-line 1)))))))
12107     (goto-char (point-min))
12108     ;; Store active file in hashtable.
12109     (goto-char (point-min))
12110     (if (string-match "%[oO]" gnus-group-line-format)
12111         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12112         ;; If we want information on moderated groups, we use this
12113         ;; loop...   
12114         (let* ((mod-hashtb (make-vector 7 0))
12115                (m (intern "m" mod-hashtb))
12116                group max min)
12117           (while (not (eobp))
12118             (condition-case nil
12119                 (progn
12120                   (narrow-to-region (point) (gnus-point-at-eol))
12121                   (setq group (let ((obarray hashtb)) (read cur)))
12122                   (if (and (numberp (setq max (read cur)))
12123                            (numberp (setq min (read cur))))
12124                       (set group (cons min max))
12125                     (set group nil))
12126                   ;; Enter moderated groups into a list.
12127                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12128                       (setq gnus-moderated-list 
12129                             (cons (symbol-name group) gnus-moderated-list))))
12130               (error nil))
12131             (widen)
12132             (forward-line 1)))
12133       ;; And if we do not care about moderation, we use this loop,
12134       ;; which is faster.
12135       (let (group max min)
12136         (while (not (eobp))
12137           (condition-case ()
12138               (progn
12139                 (narrow-to-region (point) (gnus-point-at-eol))
12140                 ;; group gets set to a symbol interned in the hash table
12141                 ;; (what a hack!!)
12142                 (setq group (let ((obarray hashtb)) (read cur)))
12143                 (if (and (numberp (setq max (read cur)))
12144                          (numberp (setq min (read cur))))
12145                     (set group (cons min max))
12146                   (set group nil)))
12147             (error 
12148              (progn 
12149                (if ignore-errors
12150                    (set group nil)
12151                  (ding) 
12152                  (gnus-message 3 "Warning - illegal active: %s"
12153                                (buffer-substring 
12154                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12155                  nil))))
12156           (widen)
12157           (forward-line 1))))))
12158
12159 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12160   ;; Parse a "groups" active file.
12161   (let ((cur (current-buffer))
12162         (hashtb (or hashtb 
12163                     (if method
12164                         gnus-active-hashtb
12165                       (setq gnus-active-hashtb
12166                             (gnus-make-hashtable 
12167                              (count-lines (point-min) (point-max)))))))
12168         (prefix (and method (not (eq method gnus-select-method))
12169                      (gnus-group-prefixed-name "" method))))
12170
12171     (goto-char (point-min))
12172     (condition-case ()
12173         ;; We split this into to separate loops, one with the prefix
12174         ;; and one without to speed the reading up somewhat.
12175         (if prefix
12176             (let (min max opoint)
12177               (while (not (eobp))
12178                 (read cur) (read cur)
12179                 (setq min (read cur)
12180                       max (read cur)
12181                       opoint (point))
12182                 (skip-chars-forward " \t")
12183                 (insert prefix)
12184                 (goto-char opoint)
12185                 (set (let ((obarray hashtb)) (read cur)) 
12186                      (cons min max))
12187                 (forward-line 1)))
12188           (let (min max)
12189             (while (not (eobp))
12190               (if (= (following-char) ?2)
12191                   (progn
12192                     (read cur) (read cur)
12193                     (setq min (read cur)
12194                           max (read cur))
12195                     (set (let ((obarray hashtb)) (read cur)) 
12196                          (cons min max))))
12197               (forward-line 1))))
12198       (error 
12199        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12200
12201 (defun gnus-read-newsrc-file (&optional force)
12202   "Read startup file.
12203 If FORCE is non-nil, the .newsrc file is read."
12204   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12205   ;; Reset variables that might be defined in the .newsrc.eld file.
12206   (let ((variables gnus-variable-list))
12207     (while variables
12208       (set (car variables) nil)
12209       (setq variables (cdr variables))))
12210   (let* ((newsrc-file gnus-current-startup-file)
12211          (quick-file (concat newsrc-file ".el")))
12212     (save-excursion
12213       ;; We always load the .newsrc.eld file. If always contains
12214       ;; much information that can not be gotten from the .newsrc
12215       ;; file (ticked articles, killed groups, foreign methods, etc.)
12216       (gnus-read-newsrc-el-file quick-file)
12217  
12218       (if (or force
12219               (and (file-newer-than-file-p newsrc-file quick-file)
12220                    (file-newer-than-file-p newsrc-file 
12221                                            (concat quick-file "d")))
12222               (not gnus-newsrc-alist))
12223           ;; We read the .newsrc file. Note that if there if a
12224           ;; .newsrc.eld file exists, it has already been read, and
12225           ;; the `gnus-newsrc-hashtb' has been created. While reading
12226           ;; the .newsrc file, Gnus will only use the information it
12227           ;; can find there for changing the data already read -
12228           ;; ie. reading the .newsrc file will not trash the data
12229           ;; already read (except for read articles).
12230           (save-excursion
12231             (gnus-message 5 "Reading %s..." newsrc-file)
12232             (set-buffer (find-file-noselect newsrc-file))
12233             (buffer-disable-undo (current-buffer))
12234             (gnus-newsrc-to-gnus-format)
12235             (kill-buffer (current-buffer))
12236             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12237
12238 (defun gnus-read-newsrc-el-file (file)
12239   (let ((ding-file (concat file "d")))
12240     ;; We always, always read the .eld file.
12241     (gnus-message 5 "Reading %s..." ding-file)
12242     (let (gnus-newsrc-assoc)
12243       (condition-case nil
12244           (load ding-file t t t)
12245         (error nil))
12246       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12247     (let ((inhibit-quit t))
12248       (gnus-uncompress-newsrc-alist))
12249     (gnus-make-hashtable-from-newsrc-alist)
12250     (if (not (file-newer-than-file-p file ding-file))
12251         ()
12252       ;; Old format quick file
12253       (gnus-message 5 "Reading %s..." file)
12254       ;; The .el file is newer than the .eld file, so we read that one
12255       ;; as well. 
12256       (gnus-read-old-newsrc-el-file file))))
12257
12258 ;; Parse the old-style quick startup file
12259 (defun gnus-read-old-newsrc-el-file (file)
12260   (let (newsrc killed marked group m)
12261     (prog1
12262         (let ((gnus-killed-assoc nil)
12263               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12264           (prog1
12265               (condition-case nil
12266                   (load file t t t)
12267                 (error nil))
12268             (setq newsrc gnus-newsrc-assoc
12269                   killed gnus-killed-assoc
12270                   marked gnus-marked-assoc)))
12271       (setq gnus-newsrc-alist nil)
12272       (while newsrc
12273         (setq group (car newsrc))
12274         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12275           (if info
12276               (progn
12277                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12278                 (setcar (cdr info)
12279                         (if (nth 1 group) gnus-level-default-subscribed 
12280                           gnus-level-default-unsubscribed))
12281                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12282             (setq gnus-newsrc-alist
12283                   (cons 
12284                    (setq info
12285                          (list (car group)
12286                                (if (nth 1 group) gnus-level-default-subscribed
12287                                  gnus-level-default-unsubscribed) 
12288                                (cdr (cdr group))))
12289                    gnus-newsrc-alist)))
12290           (if (setq m (assoc (car group) marked))
12291             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12292         (setq newsrc (cdr newsrc)))
12293       (setq newsrc killed)
12294       (while newsrc
12295         (setcar newsrc (car (car newsrc)))
12296         (setq newsrc (cdr newsrc)))
12297       (setq gnus-killed-list killed))
12298     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12299     (gnus-make-hashtable-from-newsrc-alist)))
12300       
12301 (defun gnus-make-newsrc-file (file)
12302   "Make server dependent file name by catenating FILE and server host name."
12303   (let* ((file (expand-file-name file nil))
12304          (real-file (concat file "-" (nth 1 gnus-select-method))))
12305     (if (file-exists-p real-file)
12306         real-file file)))
12307
12308 (defun gnus-uncompress-newsrc-alist ()
12309   ;; Uncompress all lists of marked articles in the newsrc assoc.
12310   (let ((newsrc gnus-newsrc-alist)
12311         marked)
12312     (while newsrc
12313       (if (not (setq marked (nth 3 (car newsrc))))
12314           ()
12315         (while marked
12316           (or (eq 'score (car (car marked)))
12317               (eq 'bookmark (car (car marked)))
12318               (eq 'killed (car (car marked)))
12319               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12320           (setq marked (cdr marked))))
12321       (setq newsrc (cdr newsrc)))))
12322
12323 (defun gnus-compress-newsrc-alist ()
12324   ;; Compress all lists of marked articles in the newsrc assoc.
12325   (let ((newsrc gnus-newsrc-alist)
12326         marked)
12327     (while newsrc
12328       (if (not (setq marked (nth 3 (car newsrc))))
12329           ()
12330         (while marked
12331           (or (eq 'score (car (car marked)))
12332               (eq 'bookmark (car (car marked)))
12333               (eq 'killed (car (car marked)))
12334               (setcdr (car marked) 
12335                       (condition-case ()
12336                           (gnus-compress-sequence 
12337                            (sort (cdr (car marked)) '<) t)
12338                         (error (cdr (car marked))))))
12339           (setq marked (cdr marked))))
12340       (setq newsrc (cdr newsrc)))))
12341
12342 (defun gnus-newsrc-to-gnus-format ()
12343   (setq gnus-newsrc-options "")
12344   (setq gnus-newsrc-options-n nil)
12345
12346   (or gnus-active-hashtb
12347       (setq gnus-active-hashtb (make-vector 4095 0)))
12348   (let ((buf (current-buffer))
12349         (already-read (> (length gnus-newsrc-alist) 1))
12350         group subscribed options-symbol newsrc
12351         symbol reads num1)
12352     (goto-char (point-min))
12353     ;; We intern the symbol `options' in the active hashtb so that we
12354     ;; can `eq' against it later.
12355     (setq options-symbol (intern "options" gnus-active-hashtb))
12356   
12357     (while (not (eobp))
12358       ;; We first read the first word on the line by narrowing and
12359       ;; then reading into `gnus-active-hashtb'.  Most groups will
12360       ;; already exist in that hashtb, so this will save some string
12361       ;; space.
12362       (narrow-to-region
12363        (point)
12364        (progn (skip-chars-forward "^ \t!:\n") (point)))
12365       (goto-char (point-min))
12366       (setq symbol 
12367             (and (/= (point-min) (point-max))
12368                  (let ((obarray gnus-active-hashtb)) (read buf))))
12369       (widen)
12370       ;; Now, the symbol we have read is either `options' or a group
12371       ;; name.  If it is an options line, we just add it to a string. 
12372       (cond 
12373        ((eq symbol options-symbol)
12374         (setq gnus-newsrc-options
12375               ;; This concatting is quite inefficient, but since our
12376               ;; thorough studies show that approx 99.37% of all
12377               ;; .newsrc files only contain a single options line, we
12378               ;; don't give a damn, frankly, my dear.
12379               (concat gnus-newsrc-options
12380                       (buffer-substring 
12381                        (gnus-point-at-bol)
12382                        ;; Options may continue on the next line.
12383                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12384                                 (progn (beginning-of-line) (point)))
12385                            (point))))))
12386        (symbol
12387         ;; It was a group name.
12388         (setq subscribed (= (following-char) ?:)
12389               group (symbol-name symbol)
12390               reads nil)
12391         (if (eolp)
12392             ;; If the line ends here, this is clearly a buggy line, so
12393             ;; we put point a the beginning of line and let the cond
12394             ;; below do the error handling.
12395             (beginning-of-line)
12396           ;; We skip to the beginning of the ranges.
12397           (skip-chars-forward "!: \t"))
12398         ;; We are now at the beginning of the list of read articles.
12399         ;; We read them range by range.
12400         (while
12401             (cond 
12402              ((looking-at "[0-9]+")
12403               ;; We narrow and read a number instead of buffer-substring/
12404               ;; string-to-int because it's faster. narrow/widen is
12405               ;; faster than save-restriction/narrow, and save-restriction
12406               ;; produces a garbage object.
12407               (setq num1 (progn
12408                            (narrow-to-region (match-beginning 0) (match-end 0))
12409                            (read buf)))
12410               (widen)
12411               ;; If the next character is a dash, then this is a range.
12412               (if (= (following-char) ?-)
12413                   (progn
12414                     ;; We read the upper bound of the range.
12415                     (forward-char 1)
12416                     (if (not (looking-at "[0-9]+"))
12417                         ;; This is a buggy line, by we pretend that
12418                         ;; it's kinda OK. Perhaps the user should be
12419                         ;; dinged? 
12420                         (setq reads (cons num1 reads))
12421                       (setq reads 
12422                             (cons 
12423                              (cons num1 (progn
12424                                           (narrow-to-region (match-beginning 0) 
12425                                                             (match-end 0))
12426                                           (read buf)))
12427                              reads))
12428                       (widen)))
12429                 ;; It was just a simple number, so we add it to the
12430                 ;; list of ranges.
12431                 (setq reads (cons num1 reads)))
12432               ;; If the next char in ?\n, then we have reached the end
12433               ;; of the line and return nil.
12434               (/= (following-char) ?\n))
12435              ((= (following-char) ?\n)
12436               ;; End of line, so we end.
12437               nil)
12438              (t
12439               ;; Not numbers and not eol, so this might be a buggy
12440               ;; line... 
12441               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12442                   (progn
12443                     ;; The line was buggy.
12444                     (setq group nil)
12445                     (gnus-message 3 "Mangled line: %s" 
12446                                   (buffer-substring (gnus-point-at-bol) 
12447                                                     (gnus-point-at-eol)))
12448                     (ding)
12449                     (sit-for 1)))
12450               nil))
12451           ;; Skip past ", ". Spaces are illegal in these ranges, but
12452           ;; we allow them, because it's a common mistake to put a
12453           ;; space after the comma.
12454           (skip-chars-forward ", "))
12455
12456         ;; We have already read .newsrc.eld, so we gently update the
12457         ;; data in the hash table with the information we have just
12458         ;; read. 
12459         (if (not group)
12460             ()
12461           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12462                 level)
12463             (if info
12464                 ;; There is an entry for this file in the alist.
12465                 (progn
12466                   (setcar (nthcdr 2 info) (nreverse reads))
12467                   ;; We update the level very gently.  In fact, we
12468                   ;; only change it if there's been a status change
12469                   ;; from subscribed to unsubscribed, or vice versa.
12470                   (setq level (nth 1 info))
12471                   (cond ((and (<= level gnus-level-subscribed)
12472                               (not subscribed))
12473                          (setq level (if reads
12474                                          gnus-level-default-unsubscribed 
12475                                        (1+ gnus-level-default-unsubscribed))))
12476                         ((and (> level gnus-level-subscribed) subscribed)
12477                          (setq level gnus-level-default-subscribed)))
12478                   (setcar (cdr info) level))
12479               ;; This is a new group.
12480               (setq info (list group 
12481                                (if subscribed
12482                                    gnus-level-default-subscribed 
12483                                  (if reads
12484                                      (1+ gnus-level-subscribed)
12485                                    gnus-level-default-unsubscribed))
12486                                (nreverse reads))))
12487             (setq newsrc (cons info newsrc))))))
12488       (forward-line 1))
12489     
12490     (setq newsrc (nreverse newsrc))
12491
12492     (if (not already-read)
12493         ()
12494       ;; We now have two newsrc lists - `newsrc', which is what we
12495       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12496       ;; what we've read from .newsrc.eld. We have to merge these
12497       ;; lists. We do this by "attaching" any (foreign) groups in the
12498       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12499       (let ((rc (cdr gnus-newsrc-alist))
12500             (prev gnus-newsrc-alist)
12501             entry mentry)
12502         (while rc
12503           (or (null (nth 4 (car rc))) ; It's a native group.
12504               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12505               (if (setq entry (assoc (car (car prev)) newsrc))
12506                   (setcdr (setq mentry (memq entry newsrc))
12507                           (cons (car rc) (cdr mentry)))
12508                 (setq newsrc (cons (car rc) newsrc))))
12509           (setq prev rc
12510                 rc (cdr rc)))))
12511
12512     (setq gnus-newsrc-alist newsrc)
12513     ;; We make the newsrc hashtb.
12514     (gnus-make-hashtable-from-newsrc-alist)
12515
12516     ;; Finally, if we read some options lines, we parse them.
12517     (or (string= gnus-newsrc-options "")
12518         (gnus-newsrc-parse-options gnus-newsrc-options))))
12519
12520 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12521 ;; The return value will be a list on the form
12522 ;; ((regexp1 . ignore)
12523 ;;  (regexp2 . subscribe)...)
12524 ;; When handling new newsgroups, groups that match a `ignore' regexp
12525 ;; will be ignored, and groups that match a `subscribe' regexp will be
12526 ;; subscribed. A line like
12527 ;; options -n !all rec.all
12528 ;; will lead to a list that looks like
12529 ;; (("^rec\\..+" . subscribe) 
12530 ;;  ("^.+" . ignore))
12531 ;; So all "rec.*" groups will be subscribed, while all the other
12532 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12533 ;; different from "options -n rec.all !all". 
12534 (defun gnus-newsrc-parse-options (options)
12535   (let (out eol)
12536     (save-excursion
12537       (gnus-set-work-buffer)
12538       (insert (regexp-quote options))
12539       ;; First we treat all continuation lines.
12540       (goto-char (point-min))
12541       (while (re-search-forward "\n[ \t]+" nil t)
12542         (replace-match " " t t))
12543       ;; Then we transform all "all"s into ".+"s.
12544       (goto-char (point-min))
12545       (while (re-search-forward "\\ball\\b" nil t)
12546         (replace-match ".+" t t))
12547       (goto-char (point-min))
12548       ;; We remove all other options than the "-n" ones.
12549       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12550         (replace-match " ")
12551         (forward-char -1))
12552       (goto-char (point-min))
12553
12554       ;; We are only interested in "options -n" lines - we
12555       ;; ignore the other option lines.
12556       (while (re-search-forward "[ \t]-n" nil t)
12557         (setq eol 
12558               (or (save-excursion
12559                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12560                          (- (point) 2)))
12561                   (gnus-point-at-eol)))
12562         ;; Search for all "words"...
12563         (while (re-search-forward "[^ \t,\n]+" eol t)
12564           (if (= (char-after (match-beginning 0)) ?!)
12565               ;; If the word begins with a bang (!), this is a "not"
12566               ;; spec. We put this spec (minus the bang) and the
12567               ;; symbol `ignore' into the list.
12568               (setq out (cons (cons (concat 
12569                                      "^" (buffer-substring 
12570                                           (1+ (match-beginning 0))
12571                                           (match-end 0)))
12572                                     'ignore) out))
12573             ;; There was no bang, so this is a "yes" spec.
12574             (setq out (cons (cons (concat 
12575                                    "^" (buffer-substring (match-beginning 0)
12576                                                          (match-end 0)))
12577                                   'subscribe) out)))))
12578     
12579       (setq gnus-newsrc-options-n out))))
12580                
12581
12582 (defun gnus-save-newsrc-file ()
12583   "Save .newsrc file."
12584   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12585   ;; from the variable gnus-newsrc-alist.
12586   (and (or gnus-newsrc-alist gnus-killed-list)
12587        gnus-current-startup-file
12588        (let ((make-backup-files t)
12589              (version-control nil)
12590              (require-final-newline t)) ;Don't ask even if requested.
12591          ;; You can stop or change version control of backup file.
12592          ;; Suggested by jason@violet.berkeley.edu.
12593          (run-hooks 'gnus-save-newsrc-hook)
12594          (save-excursion
12595            (if (or (not gnus-dribble-buffer)
12596                    (not (buffer-name gnus-dribble-buffer))
12597                    (zerop (save-excursion
12598                             (set-buffer gnus-dribble-buffer)
12599                             (buffer-size))))
12600                (gnus-message 4 "(No changes need to be saved)")
12601              (if gnus-save-newsrc-file
12602                  (progn
12603                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12604                    ;; Make backup file of master newsrc.
12605                    (gnus-gnus-to-newsrc-format)
12606                    (gnus-message 5 "Saving %s...done"
12607                                  gnus-current-startup-file)))
12608              ;; Quickly loadable .newsrc.
12609              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12610              (gnus-add-current-to-buffer-list)
12611              (buffer-disable-undo (current-buffer))
12612              (erase-buffer)
12613              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12614              (gnus-gnus-to-quick-newsrc-format)
12615              (write-region 1 (point-max) 
12616                            (concat gnus-current-startup-file ".eld") 
12617                            nil 'nomesg)
12618              (kill-buffer (current-buffer))
12619              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12620              (gnus-dribble-delete-file))))))
12621
12622 (defun gnus-gnus-to-quick-newsrc-format ()
12623   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12624   (insert ";; (ding) Gnus startup file.\n")
12625   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12626   (insert ";; to read .newsrc.\n")
12627   (let ((variables gnus-variable-list)
12628         (inhibit-quit t)
12629         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12630         variable)
12631     ;; insert lisp expressions.
12632     (gnus-compress-newsrc-alist)
12633     (while variables
12634       (setq variable (car variables))
12635       (and (boundp variable)
12636            (symbol-value variable)
12637            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12638            (insert "(setq " (symbol-name variable) " '"
12639                    (prin1-to-string (symbol-value variable))
12640                    ")\n"))
12641       (setq variables (cdr variables)))
12642     (gnus-uncompress-newsrc-alist)))
12643
12644
12645 (defun gnus-gnus-to-newsrc-format ()
12646   ;; Generate and save the .newsrc file.
12647   (let ((newsrc (cdr gnus-newsrc-alist))
12648         info ranges range)
12649     (save-excursion
12650       (set-buffer (create-file-buffer gnus-startup-file))
12651       (buffer-disable-undo (current-buffer))
12652       (erase-buffer)
12653       ;; Write options.
12654       (if gnus-newsrc-options (insert gnus-newsrc-options))
12655       ;; Write subscribed and unsubscribed.
12656       (while newsrc
12657         (setq info (car newsrc))
12658         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12659             (progn
12660               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12661                                      "!" ":"))
12662               (if (setq ranges (nth 2 info))
12663                   (progn
12664                     (insert " ")
12665                     (if (not (listp (cdr ranges)))
12666                         (if (= (car ranges) (cdr ranges))
12667                             (insert (int-to-string (car ranges)))
12668                           (insert (int-to-string (car ranges)) "-" 
12669                                   (int-to-string (cdr ranges))))
12670                       (while ranges
12671                         (setq range (car ranges)
12672                               ranges (cdr ranges))
12673                         (if (or (atom range) (= (car range) (cdr range)))
12674                             (insert (int-to-string 
12675                                      (or (and (atom range) range) 
12676                                          (car range))))
12677                           (insert (int-to-string (car range)) "-"
12678                                   (int-to-string (cdr range))))
12679                         (if ranges (insert ","))))))
12680               (insert "\n")))
12681         (setq newsrc (cdr newsrc)))
12682       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12683       (kill-buffer (current-buffer)))))
12684
12685 (defun gnus-read-all-descriptions-files ()
12686   (let ((methods (nconc (list gnus-select-method) 
12687                         gnus-secondary-select-methods)))
12688     (while methods
12689       (gnus-read-descriptions-file (car methods))
12690       (setq methods (cdr methods)))
12691     t))
12692
12693 (defun gnus-read-descriptions-file (&optional method)
12694   (let ((method (or method gnus-select-method)))
12695     ;; We create the hashtable whether we manage to read the desc file
12696     ;; to avoid trying to re-read after a failed read.
12697     (or gnus-description-hashtb
12698         (setq gnus-description-hashtb 
12699               (gnus-make-hashtable (length gnus-active-hashtb))))
12700     ;; Mark this method's desc file as read.
12701     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12702                   gnus-description-hashtb)
12703
12704     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12705     (cond 
12706      ((not (or (gnus-server-opened method)
12707                (gnus-open-server method)))
12708       (gnus-message 1 "Couldn't open server")
12709       nil)
12710      ((not (gnus-request-list-newsgroups method))
12711       (gnus-message 1 "Couldn't read newsgroups descriptions")
12712       nil)
12713      (t
12714       (let (group)
12715         (save-excursion
12716           (save-restriction
12717             (set-buffer nntp-server-buffer)
12718             (goto-char (point-min))
12719             (if (or (search-forward "\n.\n" nil t)
12720                     (goto-char (point-max)))
12721                 (progn
12722                   (beginning-of-line)
12723                   (narrow-to-region (point-min) (point))))
12724             (goto-char (point-min))
12725             (while (not (eobp))
12726               ;; If we get an error, we set group to 0, which is not a
12727               ;; symbol... 
12728               (setq group 
12729                     (condition-case ()
12730                         (let ((obarray gnus-description-hashtb))
12731                           ;; Group is set to a symbol interned in this
12732                           ;; hash table.
12733                           (read nntp-server-buffer))
12734                       (error 0)))
12735               (skip-chars-forward " \t")
12736               ;; ... which leads to this line being effectively ignored.
12737               (and (symbolp group)
12738                    (set group (buffer-substring 
12739                                (point) (progn (end-of-line) (point)))))
12740               (forward-line 1))))
12741         (gnus-message 5 "Reading descriptions file...done")
12742         t)))))
12743
12744 (defun gnus-group-get-description (group)
12745   ;; Get the description of a group by sending XGTITLE to the server.
12746   (and (gnus-request-group-description group)
12747        (save-excursion
12748          (set-buffer nntp-server-buffer)
12749          (goto-char (point-min))
12750          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12751               (buffer-substring (match-beginning 1) (match-end 1))))))
12752
12753 ;;;
12754 ;;; Server
12755 ;;;
12756
12757 (defvar gnus-server-mode-hook nil
12758   "Hook run in `gnus-server-mode' buffers.")
12759
12760 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12761   "Format of server lines.
12762 It works along the same lines as a normal formatting string,
12763 with some simple extensions.")
12764
12765 (defvar gnus-server-mode-line-format "(ding) List of servers"
12766   "The format specification for the server mode line.")
12767
12768 (defconst gnus-server-line-format-alist
12769   (list (list ?h 'how ?s)
12770         (list ?n 'name ?s)
12771         (list ?w 'where ?s)
12772         ))
12773
12774 (defconst gnus-server-mode-line-format-alist 
12775   (list (list ?S 'news-server ?s)
12776         (list ?M 'news-method ?s)
12777         (list ?u 'user-defined ?s)))
12778
12779 (defvar gnus-server-line-format-spec nil)
12780 (defvar gnus-server-mode-line-format-spec nil)
12781 (defvar gnus-server-killed-servers nil)
12782
12783 (defvar gnus-server-mode-map nil)
12784 (put 'gnus-server-mode 'mode-class 'special)
12785
12786 (if gnus-server-mode-map
12787     nil
12788   (setq gnus-server-mode-map (make-sparse-keymap))
12789   (suppress-keymap gnus-server-mode-map)
12790   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12791   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12792   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12793   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12794   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12795   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12796   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12797   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12798   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12799   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12800
12801 (defun gnus-server-mode ()
12802   "Major mode for listing and editing servers.
12803
12804 All normal editing commands are switched off.
12805 \\<gnus-server-mode-map>
12806
12807 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12808
12809 The following commands are available:
12810
12811 \\{gnus-server-mode-map}"
12812   (interactive)
12813   (if gnus-visual (gnus-server-make-menu-bar))
12814   (kill-all-local-variables)
12815   (setq mode-line-modified "-- ")
12816   (make-local-variable 'mode-line-format)
12817   (setq mode-line-format (copy-sequence mode-line-format))
12818   (and (equal (nth 3 mode-line-format) "   ")
12819        (setcar (nthcdr 3 mode-line-format) ""))
12820   (setq major-mode 'gnus-server-mode)
12821   (setq mode-name "Server")
12822 ;  (gnus-group-set-mode-line)
12823   (setq mode-line-process nil)
12824   (use-local-map gnus-server-mode-map)
12825   (buffer-disable-undo (current-buffer))
12826   (setq truncate-lines t)
12827   (setq buffer-read-only t)
12828   (run-hooks 'gnus-server-mode-hook))
12829
12830 (defun gnus-server-insert-server-line (sformat name method)
12831   (let* ((sformat (or sformat gnus-server-line-format-spec))
12832          (how (car method))
12833          (where (nth 1 method))
12834          b)
12835     (beginning-of-line)
12836     (setq b (point))
12837     ;; Insert the text.
12838     (insert (eval sformat))
12839     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
12840
12841 (defun gnus-server-setup-buffer ()
12842   (if (get-buffer gnus-server-buffer)
12843       ()
12844     (save-excursion
12845       (set-buffer (get-buffer-create gnus-server-buffer))
12846       (gnus-server-mode)
12847       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12848
12849 (defun gnus-server-prepare ()
12850   (setq gnus-server-mode-line-format-spec 
12851         (gnus-parse-format gnus-server-mode-line-format 
12852                            gnus-server-mode-line-format-alist))
12853   (setq gnus-server-line-format-spec 
12854         (gnus-parse-format gnus-server-line-format 
12855                            gnus-server-line-format-alist))
12856   (let ((alist gnus-server-alist)
12857         (buffer-read-only nil))
12858     (erase-buffer)
12859     (while alist
12860       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12861       (setq alist (cdr alist))))
12862   (goto-char (point-min))
12863   (gnus-server-position-cursor))
12864
12865 (defun gnus-server-server-name ()
12866   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12867     (and server (symbol-name server))))
12868
12869 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12870
12871 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12872
12873 (defun gnus-server-update-server (server)
12874   (save-excursion
12875     (set-buffer gnus-server-buffer)
12876     (let ((buffer-read-only nil)
12877           (info (cdr (assoc server gnus-server-alist))))
12878       (gnus-dribble-enter 
12879        (concat "(gnus-server-set-info \"" server "\" '"
12880                (prin1-to-string info) ")"))
12881       ;; Buffer may be narrowed.
12882       (save-restriction
12883         (widen)
12884         (if (gnus-server-goto-server server)
12885             (delete-region (progn (beginning-of-line) (point))
12886                            (progn (forward-line 1) (point))))
12887         (let ((entry (assoc server gnus-server-alist)))
12888           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12889           (gnus-server-position-cursor))))))
12890
12891 (defun gnus-server-set-info (server info)
12892   ;; Enter a select method into the virtual server alist.
12893   (gnus-dribble-enter 
12894    (concat "(gnus-server-set-info \"" server "\" '"
12895            (prin1-to-string info) ")"))
12896   (let* ((server (nth 1 info))
12897          (entry (assoc server gnus-server-alist)))
12898     (if entry (setcdr entry info)
12899       (setq gnus-server-alist
12900             (nconc gnus-server-alist (list (cons server info)))))))
12901
12902 (defun gnus-server-to-method (server)
12903   ;; Map virtual server names to select methods.
12904   (or (and (equal server "native") gnus-select-method)
12905       (cdr (assoc server gnus-server-alist))))
12906
12907 (defun gnus-server-extend-method (group method)
12908   ;; This function "extends" a virtual server.  If the server is
12909   ;; "hello", and the select method is ("hello" (my-var "something")) 
12910   ;; in the group "alt.alt", this will result in a new virtual server
12911   ;; called "helly+alt.alt".
12912   (let ((entry
12913          (gnus-copy-sequence 
12914           (if (equal (car method) "native") gnus-select-method
12915               (cdr (assoc (car method) gnus-server-alist))))))
12916     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12917     (nconc entry (cdr method))))
12918
12919 (defun gnus-server-get-method (group method)
12920   ;; Input either a server name, and extended server name, or a
12921   ;; select method, and return a select method. 
12922   (cond ((stringp method)
12923          (gnus-server-to-method method))
12924         ((stringp (car method))
12925          (gnus-server-extend-method group method))
12926         (t
12927          (gnus-server-add-address method))))
12928
12929 (defun gnus-server-add-address (method)
12930   (let ((method-name (symbol-name (car method))))
12931     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12932              (not (assq (intern (concat method-name "-address")) method)))
12933         (append method (list (list (intern (concat method-name "-address"))
12934                                    (nth 1 method))))
12935       method)))
12936
12937 (defun gnus-server-equal (s1 s2)
12938   (or (equal s1 s2)
12939       (and (= (length s1) (length s2))
12940            (progn
12941              (while (and s1 (member (car s1) s2))
12942                (setq s1 (cdr s1)))
12943              (null s1)))))
12944
12945 ;;; Interactive server functions.
12946
12947 (defun gnus-server-kill-server (server)
12948   "Kill the server on the current line."
12949   (interactive (list (gnus-server-server-name)))
12950   (or (gnus-server-goto-server server)
12951       (if server (error "No such server: %s" server)
12952         (error "No server on the current line")))
12953   (let ((buffer-read-only nil))
12954     (delete-region (progn (beginning-of-line) (point))
12955                    (progn (forward-line 1) (point))))
12956   (setq gnus-server-killed-servers 
12957         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12958   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12959                                 gnus-server-alist))
12960   (gnus-server-position-cursor))
12961
12962 (defun gnus-server-yank-server ()
12963   "Yank the previously killed server."
12964   (interactive)
12965   (or gnus-server-killed-servers
12966       (error "No killed servers to be yanked"))
12967   (let ((alist gnus-server-alist)
12968         (server (gnus-server-server-name))
12969         (killed (car gnus-server-killed-servers)))
12970     (if (not server) 
12971         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12972       (if (string= server (car (car gnus-server-alist)))
12973           (setq gnus-server-alist (cons killed gnus-server-alist))
12974         (while (and (cdr alist)
12975                     (not (string= server (car (car (cdr alist))))))
12976           (setq alist (cdr alist)))
12977         (setcdr alist (cons killed (cdr alist)))))
12978     (gnus-server-update-server (car killed))
12979     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12980     (gnus-server-position-cursor)))
12981
12982 (defun gnus-server-exit ()
12983   "Return to the group buffer."
12984   (interactive)
12985   (kill-buffer (current-buffer))
12986   (switch-to-buffer gnus-group-buffer))
12987
12988 (defun gnus-server-list-servers ()
12989   "List all available servers."
12990   (interactive)
12991   (let ((cur (gnus-server-server-name)))
12992     (gnus-server-prepare)
12993     (if cur (gnus-server-goto-server cur)
12994       (goto-char (point-max))
12995       (forward-line -1))
12996     (gnus-server-position-cursor)))
12997
12998 (defun gnus-server-copy-server (from to)
12999   (interactive
13000    (list
13001     (or (gnus-server-server-name)
13002         (error "No server on the current line"))
13003     (read-string "Copy to: ")))
13004   (or from (error "No server on current line"))
13005   (or (and to (not (string= to ""))) (error "No name to copy to"))
13006   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13007   (or (assoc from gnus-server-alist) 
13008       (error "%s: no such server" from))
13009   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13010     (setcar to-entry to)
13011     (setcar (nthcdr 2 to-entry) to)
13012     (setq gnus-server-killed-servers 
13013           (cons to-entry gnus-server-killed-servers))
13014     (gnus-server-yank-server)))
13015
13016 (defun gnus-server-add-server (how where)
13017   (interactive 
13018    (list (intern (completing-read "Server method: "
13019                                   gnus-valid-select-methods nil t))
13020          (read-string "Server name: ")))
13021   (setq gnus-server-killed-servers 
13022         (cons (list where how where) gnus-server-killed-servers))
13023   (gnus-server-yank-server))
13024
13025 (defun gnus-server-goto-server (server)
13026   "Jump to a server line."
13027   (interactive
13028    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13029   (let ((to (text-property-any (point-min) (point-max) 
13030                                'gnus-server (intern server))))
13031     (and to
13032          (progn
13033            (goto-char to) 
13034            (gnus-server-position-cursor)))))
13035
13036 (defun gnus-server-edit-server (server)
13037   "Edit the server on the current line."
13038   (interactive (list (gnus-server-server-name)))
13039   (or server
13040       (error "No server on current line"))
13041   (let ((winconf (current-window-configuration)))
13042     (get-buffer-create gnus-server-edit-buffer)
13043     (gnus-configure-windows 'edit-server)
13044     (gnus-add-current-to-buffer-list)
13045     (emacs-lisp-mode)
13046     (make-local-variable 'gnus-prev-winconf)
13047     (setq gnus-prev-winconf winconf)
13048     (use-local-map (copy-keymap (current-local-map)))
13049     (let ((done-func '(lambda () 
13050                         "Exit editing mode and update the information."
13051                         (interactive)
13052                         (gnus-server-edit-server-done 'group))))
13053       (setcar (cdr (nth 4 done-func)) server)
13054       (local-set-key "\C-c\C-c" done-func))
13055     (erase-buffer)
13056     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13057     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13058
13059 (defun gnus-server-edit-server-done (server)
13060   (interactive)
13061   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13062   (goto-char (point-min))
13063   (let ((form (read (current-buffer)))
13064         (winconf gnus-prev-winconf))
13065     (gnus-server-set-info server form)
13066     (kill-buffer (current-buffer))
13067     (and winconf (set-window-configuration winconf))
13068     (set-buffer gnus-server-buffer)
13069     (gnus-server-update-server (gnus-server-server-name))
13070     (gnus-server-position-cursor)))
13071
13072 (defun gnus-server-read-server (server)
13073   "Browse a server."
13074   (interactive (list (gnus-server-server-name)))
13075   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13076
13077 (defun gnus-mouse-pick-server (e)
13078   (interactive "e")
13079   (mouse-set-point e)
13080   (gnus-server-read-server (gnus-server-server-name)))
13081
13082 ;;;
13083 ;;; entry points into gnus-score.el
13084 ;;;
13085
13086 ;;; Finding score files. 
13087
13088 (defvar gnus-global-score-files nil
13089   "*List of global score files and directories.
13090 Set this variable if you want to use people's score files.  One entry
13091 for each score file or each score file directory.  Gnus will decide
13092 by itself what score files are applicable to which group.
13093
13094 Say you want to use the single score file
13095 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13096 score files in the \"/ftp.some-where:/pub/score\" directory.
13097
13098  (setq gnus-global-score-files
13099        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13100          \"/ftp.some-where:/pub/score\"))")
13101
13102 (defun gnus-score-score-files (group)
13103   "Return a list of all possible score files."
13104   ;; Search and set any global score files.
13105   (and gnus-global-score-files 
13106        (or gnus-internal-global-score-files
13107            (gnus-score-search-global-directories gnus-global-score-files)))
13108   ;; Fix the kill-file dir variable.
13109   (setq gnus-kill-files-directory 
13110         (file-name-as-directory
13111          (or gnus-kill-files-directory "~/News/")))
13112   ;; If we can't read it, there are no score files.
13113   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13114       (setq gnus-score-file-list nil)
13115     (if (gnus-use-long-file-name 'not-score)
13116         ;; We want long file names.
13117         (if (or (not gnus-score-file-list)
13118                 (not (car gnus-score-file-list))
13119                 (gnus-file-newer-than gnus-kill-files-directory
13120                                       (car gnus-score-file-list)))
13121               (setq gnus-score-file-list 
13122                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13123                           (nreverse 
13124                            (directory-files 
13125                             gnus-kill-files-directory t 
13126                             (gnus-score-file-regexp))))))
13127       ;; We do not use long file names, so we have to do some
13128       ;; directory traversing.  
13129       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13130             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13131             dir files suffix)
13132         (while suffixes
13133           (setq dir (expand-file-name
13134                      (concat gnus-kill-files-directory
13135                              (gnus-replace-chars-in-string group ?. ?/))))
13136           (setq suffix (car suffixes)
13137                 suffixes (cdr suffixes))
13138           (if (file-exists-p (concat dir "/" suffix))
13139               (setq files (cons (concat dir "/" suffix) files)))
13140           (while (>= (1+ (length dir)) mdir)
13141             (and (file-exists-p (concat dir "/all/" suffix))
13142                  (setq files (cons (concat dir "/all/" suffix) files)))
13143             (string-match "/[^/]*$" dir)
13144             (setq dir (substring dir 0 (match-beginning 0)))))
13145         (setq gnus-score-file-list 
13146               (cons nil (nreverse files)))))
13147     (cdr gnus-score-file-list)))
13148
13149 (defun gnus-score-file-regexp ()
13150   (concat "\\(" gnus-score-file-suffix 
13151           "\\|" gnus-adaptive-file-suffix "\\)$"))
13152         
13153 (defun gnus-score-find-bnews (group)
13154   "Return a list of score files for GROUP.
13155 The score files are those files in the ~/News directory which matches
13156 GROUP using BNews sys file syntax."
13157   (let* ((sfiles (append (gnus-score-score-files group)
13158                          gnus-internal-global-score-files))
13159          (kill-dir (file-name-as-directory 
13160                     (expand-file-name gnus-kill-files-directory)))
13161          (klen (length kill-dir))
13162          ofiles not-match regexp)
13163     (save-excursion
13164       (set-buffer (get-buffer-create "*gnus score files*"))
13165       (buffer-disable-undo (current-buffer))
13166       ;; Go through all score file names and create regexp with them
13167       ;; as the source.  
13168       (while sfiles
13169         (erase-buffer)
13170         (insert (car sfiles))
13171         (goto-char (point-min))
13172         ;; First remove the suffix itself.
13173         (re-search-forward (concat "." (gnus-score-file-regexp)))
13174         (replace-match "" t t) 
13175         (goto-char (point-min))
13176         (if (looking-at (regexp-quote kill-dir))
13177             ;; If the file name was just "SCORE", `klen' is one character
13178             ;; too much.
13179             (delete-char (min (1- (point-max)) klen))
13180           (goto-char (point-max))
13181           (search-backward "/")
13182           (delete-region (1+ (point)) (point-min)))
13183         ;; If short file names were used, we have to translate slashes.
13184         (goto-char (point-min))
13185         (while (search-forward "/" nil t)
13186           (replace-match "." t t))
13187         ;; Translate "all" to ".*".
13188         (while (search-forward "all" nil t)
13189           (replace-match ".*" t t))
13190         (goto-char (point-min))
13191         ;; Deal with "not."s.
13192         (if (looking-at "not.")
13193             (progn
13194               (setq not-match t)
13195               (setq regexp (buffer-substring 5 (point-max))))
13196           (setq regexp (buffer-substring 1 (point-max)))
13197           (setq not-match nil))
13198         ;; Finally - if this resulting regexp matches the group name,
13199         ;; we add this score file to the list of score files
13200         ;; applicable to this group.
13201         (if (or (and not-match
13202                      (not (string-match regexp group)))
13203                 (and (not not-match)
13204                      (string-match regexp group)))
13205             (setq ofiles (cons (car sfiles) ofiles)))
13206         (setq sfiles (cdr sfiles)))
13207       (kill-buffer (current-buffer))
13208       ;; Slight kludge here - the last score file returned should be
13209       ;; the local score file, whether it exists or not. This is so
13210       ;; that any score commands the user enters will go to the right
13211       ;; file, and not end up in some global score file.
13212       (let ((localscore
13213              (expand-file-name
13214               (if (gnus-use-long-file-name 'not-score)
13215                   (concat gnus-kill-files-directory group "." 
13216                           gnus-score-file-suffix)
13217                 (concat gnus-kill-files-directory
13218                         (gnus-replace-chars-in-string group ?. ?/)
13219                         "/" gnus-score-file-suffix)))))
13220         (and (member localscore ofiles)
13221              (delete localscore ofiles))
13222         (setq ofiles (cons localscore ofiles)))
13223       (nreverse ofiles))))
13224
13225 (defun gnus-score-find-single (group)
13226   "Return list containing the score file for GROUP."
13227   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13228         (gnus-score-file-name group)))
13229
13230 (defun gnus-score-find-hierarchical (group)
13231   "Return list of score files for GROUP.
13232 This includes the score file for the group and all its parents."
13233   (let ((all (copy-sequence '(nil)))
13234         (start 0))
13235     (while (string-match "\\." group (1+ start))
13236       (setq start (match-beginning 0))
13237       (setq all (cons (substring group 0 start) all)))
13238     (setq all (cons group all))
13239     (nconc
13240      (mapcar (lambda (newsgroup)
13241                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13242              (setq all (nreverse all)))
13243      (mapcar 'gnus-score-file-name all))))
13244
13245 (defvar gnus-score-file-alist-cache nil)
13246
13247 (defun gnus-score-find-alist (group)
13248   "Return list of score files for GROUP.
13249 The list is determined from the variable gnus-score-file-alist."
13250   (let ((alist gnus-score-file-multiple-match-alist)
13251         score-files)
13252     ;; if this group has been seen before, return the cached entry
13253     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13254         (cdr score-files)       ; ensures caching of groups with no matches
13255       ;; handle the multiple match alist
13256       (while alist
13257         (and (string-match (car (car alist)) group)
13258              (setq score-files
13259                    (nconc score-files (cdr (car alist)))))
13260         (setq alist (cdr alist)))
13261       (setq alist gnus-score-file-single-match-alist)
13262       ;; handle the single match alist
13263       (catch 'done
13264         (while alist
13265           (and (string-match (car (car alist)) group)
13266                ;; progn used just in case ("regexp") has no files
13267                ;; and score-files is still nil. -sj
13268                ;; this can be construed as a "stop searching here" feature :>
13269                ;; and used to simplify regexps in the single-alist 
13270                (progn
13271                  (setq score-files
13272                        (append score-files (cdr (car alist))))
13273                  (throw 'done nil)))
13274           (setq alist (cdr alist))))
13275       ;; cache the score files
13276       (setq gnus-score-file-alist-cache
13277             (cons (cons group score-files) gnus-score-file-alist-cache))
13278       score-files)))
13279
13280
13281 (defun gnus-possibly-score-headers (&optional trace)
13282   (let ((func gnus-score-find-score-files-function)
13283         score-files)
13284     (and func (not (listp func))
13285          (setq func (list func)))
13286     ;; Go through all the functions for finding score files (or actual
13287     ;; scores) and add them to a list.
13288     (setq score-files (copy-sequence
13289                        (gnus-score-find-alist gnus-newsgroup-name)))
13290     (while func
13291       (and (symbolp (car func))
13292            (fboundp (car func))
13293            (setq score-files 
13294                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13295       (setq func (cdr func)))
13296     (if score-files (gnus-score-headers score-files trace))))
13297
13298 (defun gnus-score-file-name (newsgroup &optional suffix)
13299   "Return the name of a score file for NEWSGROUP."
13300   (let ((suffix (or suffix gnus-score-file-suffix)))
13301     (cond  ((or (null newsgroup)
13302                 (string-equal newsgroup ""))
13303             ;; The global score file is placed at top of the directory.
13304             (expand-file-name 
13305              suffix (or gnus-kill-files-directory "~/News")))
13306            ((gnus-use-long-file-name 'not-score)
13307             ;; Append ".SCORE" to newsgroup name.
13308             (expand-file-name (concat newsgroup "." suffix)
13309                               (or gnus-kill-files-directory "~/News")))
13310            (t
13311             ;; Place "SCORE" under the hierarchical directory.
13312             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13313                                       "/" suffix)
13314                               (or gnus-kill-files-directory "~/News"))))))
13315
13316 (defun gnus-score-search-global-directories (files)
13317   "Scan all global score directories for score files."
13318   ;; Set the variable `gnus-internal-global-score-files' to all
13319   ;; available global score files.
13320   (interactive (list gnus-global-score-files))
13321   (let (out)
13322     (while files
13323       (if (string-match "/$" (car files))
13324           (setq out (nconc (directory-files 
13325                             (car files) t
13326                             (concat (gnus-score-file-regexp) "$"))))
13327         (setq out (cons (car files) out)))
13328       (setq files (cdr files)))
13329     (setq gnus-internal-global-score-files out)))
13330
13331 ;; Allow redefinition of Gnus functions.
13332
13333 (gnus-ems-redefine)
13334
13335 (provide 'gnus)
13336
13337 ;;; gnus.el ends here