*** 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.1"
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           (thread nil)
1849           pos)
1850       (gnus-set-work-buffer)
1851       (gnus-summary-insert-line 
1852        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1853       (goto-char (point-min))
1854       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1855                                          (- (point) 2)))))
1856       (goto-char (point-min))
1857       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1858                                           (- (point) 2))) pos))
1859       (goto-char (point-min))
1860       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1861                                         (- (point) 2))) pos))
1862       (setq gnus-summary-mark-positions pos))))
1863
1864 (defun gnus-format-max-width (form length)
1865   (let* ((val (eval form))
1866          (valstr (if (numberp val) (int-to-string val) val)))
1867     (if (> (length valstr) length)
1868         (substring valstr 0 length)
1869       valstr)))
1870
1871 (defun gnus-set-mouse-face (string)
1872   ;; Set mouse face property on STRING.
1873   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1874   string)
1875
1876 (defun gnus-parse-format (format spec-alist)
1877   ;; This function parses the FORMAT string with the help of the
1878   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1879   ;; string.  If the FORMAT string contains the specifiers %( and %)
1880   ;; the text between them will have the mouse-face text property.
1881   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1882       (if (and gnus-visual gnus-mouse-face)
1883           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1884                 (button (substring format (match-beginning 2) (match-end 2)))
1885                 (post (substring format (match-beginning 3) (match-end 3))))
1886             (list 'concat
1887                   (gnus-parse-simple-format pre spec-alist)
1888                   (list 'gnus-set-mouse-face
1889                         (gnus-parse-simple-format button spec-alist))
1890                   (gnus-parse-simple-format post spec-alist)))
1891         (gnus-parse-simple-format
1892          (concat (substring format (match-beginning 1) (match-end 1))
1893                  (substring format (match-beginning 2) (match-end 2))
1894                  (substring format (match-beginning 3) (match-end 3)))
1895          spec-alist))
1896     (gnus-parse-simple-format format spec-alist)))
1897
1898 (defun gnus-parse-simple-format (format spec-alist)
1899   ;; This function parses the FORMAT string with the help of the
1900   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1901   ;; string. The list will consist of the symbol `format', a format
1902   ;; specification string, and a list of forms depending on the
1903   ;; SPEC-ALIST.
1904   (let ((max-width 0)
1905         spec flist fstring newspec elem beg)
1906     (save-excursion
1907       (gnus-set-work-buffer)
1908       (insert format)
1909       (goto-char (point-min))
1910       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1911         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1912                                                      (match-end 2))))
1913         ;; First check if there are any specs that look anything like
1914         ;; "%12,12A", ie. with a "max width specification". These have
1915         ;; to be treated specially.
1916         (if (setq beg (match-beginning 1))
1917             (setq max-width 
1918                   (string-to-int 
1919                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1920           (setq max-width 0)
1921           (setq beg (match-beginning 2)))
1922         ;; Find the specification from `spec-alist'.
1923         (if (not (setq elem (cdr (assq spec spec-alist))))
1924             (setq elem '("*" ?s)))
1925         ;; Treat user defined format specifiers specially
1926         (and (eq (car elem) 'user-defined)
1927              (setq elem
1928                    (list 
1929                     (list (intern (concat "gnus-user-format-function-"
1930                                           (buffer-substring
1931                                            (match-beginning 3)
1932                                            (match-end 3))))
1933                           'header)
1934                     ?s))
1935              (delete-region (match-beginning 3) (match-end 3)))
1936         (if (not (zerop max-width))
1937             (let ((el (car elem)))
1938               (cond ((= (car (cdr elem)) ?c) 
1939                      (setq el (list 'char-to-string el)))
1940                     ((= (car (cdr elem)) ?d)
1941                      (numberp el) (setq el (list 'int-to-string el))))
1942               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1943                                 flist))
1944               (setq newspec ?s))
1945           (setq flist (cons (car elem) flist))
1946           (setq newspec (car (cdr elem))))
1947         ;; Remove the old specification (and possibly a ",12" string).
1948         (delete-region beg (match-end 2))
1949         ;; Insert the new specification.
1950         (goto-char beg)
1951         (insert newspec))
1952       (setq fstring (buffer-substring 1 (point-max))))
1953     (cons 'format (cons fstring (nreverse flist)))))
1954
1955 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1956 (defun gnus-read-init-file ()
1957   (and gnus-init-file
1958        (or (and (file-exists-p gnus-init-file) 
1959                 ;; Don't try to load a directory.
1960                 (not (file-directory-p gnus-init-file)))
1961            (file-exists-p (concat gnus-init-file ".el"))
1962            (file-exists-p (concat gnus-init-file ".elc")))
1963        (load gnus-init-file nil t)))
1964
1965 (defun gnus-set-work-buffer ()
1966   (if (get-buffer gnus-work-buffer)
1967       (progn
1968         (set-buffer gnus-work-buffer)
1969         (erase-buffer))
1970     (set-buffer (get-buffer-create gnus-work-buffer))
1971     (kill-all-local-variables)
1972     (buffer-disable-undo (current-buffer))
1973     (gnus-add-current-to-buffer-list)))
1974
1975 ;; Article file names when saving.
1976
1977 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1978   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1979 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1980 Otherwise, it is like ~/News/news/group/num."
1981   (let ((default
1982           (expand-file-name
1983            (concat (if (gnus-use-long-file-name 'not-save)
1984                        (gnus-capitalize-newsgroup newsgroup)
1985                      (gnus-newsgroup-directory-form newsgroup))
1986                    "/" (int-to-string (header-number headers)))
1987            (or gnus-article-save-directory "~/News"))))
1988     (if (and last-file
1989              (string-equal (file-name-directory default)
1990                            (file-name-directory last-file))
1991              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1992         default
1993       (or last-file default))))
1994
1995 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1996   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1997 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1998 Otherwise, it is like ~/News/news/group/num."
1999   (let ((default
2000           (expand-file-name
2001            (concat (if (gnus-use-long-file-name 'not-save)
2002                        newsgroup
2003                      (gnus-newsgroup-directory-form newsgroup))
2004                    "/" (int-to-string (header-number headers)))
2005            (or gnus-article-save-directory "~/News"))))
2006     (if (and last-file
2007              (string-equal (file-name-directory default)
2008                            (file-name-directory last-file))
2009              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2010         default
2011       (or last-file default))))
2012
2013 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2014   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2015 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2016 Otherwise, it is like ~/News/news/group/news."
2017   (or last-file
2018       (expand-file-name
2019        (if (gnus-use-long-file-name 'not-save)
2020            (gnus-capitalize-newsgroup newsgroup)
2021          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2022        (or gnus-article-save-directory "~/News"))))
2023
2024 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2025   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2026 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2027 Otherwise, it is like ~/News/news/group/news."
2028   (or last-file
2029       (expand-file-name
2030        (if (gnus-use-long-file-name 'not-save)
2031            newsgroup
2032          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2033        (or gnus-article-save-directory "~/News"))))
2034
2035 ;; For subscribing new newsgroup
2036
2037 (defun gnus-subscribe-hierarchical-interactive (groups)
2038   (let ((groups (sort groups 'string<))
2039         prefixes prefix start ans group starts)
2040     (while groups
2041       (setq prefixes (list "^"))
2042       (while (and groups prefixes)
2043         (while (not (string-match (car prefixes) (car groups)))
2044           (setq prefixes (cdr prefixes)))
2045         (setq prefix (car prefixes))
2046         (setq start (1- (length prefix)))
2047         (if (and (string-match "[^\\.]\\." (car groups) start)
2048                  (cdr groups)
2049                  (setq prefix 
2050                        (concat "^" (substring (car groups) 0 (match-end 0))))
2051                  (string-match prefix (car (cdr groups))))
2052             (progn
2053               (setq prefixes (cons prefix prefixes))
2054               (message "Descend hierarchy %s? ([y]nsq): " 
2055                        (substring prefix 1 (1- (length prefix))))
2056               (setq ans (read-char))
2057               (cond ((= ans ?n)
2058                      (while (and groups 
2059                                  (string-match prefix 
2060                                                (setq group (car groups))))
2061                        (setq gnus-killed-list 
2062                              (cons group gnus-killed-list))
2063                        (gnus-sethash group group gnus-killed-hashtb)
2064                        (setq groups (cdr groups)))
2065                      (setq starts (cdr starts)))
2066                     ((= ans ?s)
2067                      (while (and groups 
2068                                  (string-match prefix 
2069                                                (setq group (car groups))))
2070                        (gnus-sethash group group gnus-killed-hashtb)
2071                        (gnus-subscribe-alphabetically (car groups))
2072                        (setq groups (cdr groups)))
2073                      (setq starts (cdr starts)))
2074                     ((= ans ?q)
2075                      (while groups
2076                        (setq group (car groups))
2077                        (setq gnus-killed-list (cons group gnus-killed-list))
2078                        (gnus-sethash group group gnus-killed-hashtb)
2079                        (setq groups (cdr groups))))
2080                     (t nil)))
2081           (message "Subscribe %s? ([n]yq)" (car groups))
2082           (setq ans (read-char))
2083           (setq group (car groups))
2084           (cond ((= ans ?y)
2085                  (gnus-subscribe-alphabetically (car groups))
2086                  (gnus-sethash group group gnus-killed-hashtb))
2087                 ((= ans ?q)
2088                  (while groups
2089                    (setq group (car groups))
2090                    (setq gnus-killed-list (cons group gnus-killed-list))
2091                    (gnus-sethash group group gnus-killed-hashtb)
2092                    (setq groups (cdr groups))))
2093                 (t 
2094                  (setq gnus-killed-list (cons group gnus-killed-list))
2095                  (gnus-sethash group group gnus-killed-hashtb)))
2096           (setq groups (cdr groups)))))))
2097
2098 (defun gnus-subscribe-randomly (newsgroup)
2099   "Subscribe new NEWSGROUP by making it the first newsgroup."
2100   (gnus-subscribe-newsgroup newsgroup))
2101
2102 (defun gnus-subscribe-alphabetically (newgroup)
2103   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2104   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2105   (let ((groups (cdr gnus-newsrc-alist))
2106         before)
2107     (while (and (not before) groups)
2108       (if (string< newgroup (car (car groups)))
2109           (setq before (car (car groups)))
2110         (setq groups (cdr groups))))
2111     (gnus-subscribe-newsgroup newgroup before)))
2112
2113 (defun gnus-subscribe-hierarchically (newgroup)
2114   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2115   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2116   (save-excursion
2117     (set-buffer (find-file-noselect gnus-current-startup-file))
2118     (let ((groupkey newgroup)
2119           before)
2120       (while (and (not before) groupkey)
2121         (goto-char (point-min))
2122         (let ((groupkey-re
2123                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2124           (while (and (re-search-forward groupkey-re nil t)
2125                       (progn
2126                         (setq before (buffer-substring
2127                                       (match-beginning 1) (match-end 1)))
2128                         (string< before newgroup)))))
2129         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2130         (setq groupkey
2131               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2132                   (substring groupkey (match-beginning 1) (match-end 1)))))
2133       (gnus-subscribe-newsgroup newgroup before))))
2134
2135 (defun gnus-subscribe-interactively (newsgroup)
2136   "Subscribe new NEWSGROUP interactively.
2137 It is inserted in hierarchical newsgroup order if subscribed. If not,
2138 it is killed."
2139   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2140       (gnus-subscribe-hierarchically newsgroup)
2141     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2142
2143 (defun gnus-subscribe-zombies (newsgroup)
2144   "Make new NEWSGROUP a zombie group."
2145   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2146
2147 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2148   "Subscribe new NEWSGROUP.
2149 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2150 the first newsgroup."
2151   ;; We subscribe the group by changing its level to `subscribed'.
2152   (gnus-group-change-level 
2153    newsgroup gnus-level-default-subscribed
2154    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2155   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2156
2157 ;; For directories
2158
2159 (defun gnus-newsgroup-directory-form (newsgroup)
2160   "Make hierarchical directory name from NEWSGROUP name."
2161   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2162         (len (length newsgroup))
2163         idx)
2164     ;; If this is a foreign group, we don't want to translate the
2165     ;; entire name.  
2166     (if (setq idx (string-match ":" newsgroup))
2167         (aset newsgroup idx ?/)
2168       (setq idx 0))
2169     ;; Replace all occurrences of `.' with `/'.
2170     (while (< idx len)
2171       (if (= (aref newsgroup idx) ?.)
2172           (aset newsgroup idx ?/))
2173       (setq idx (1+ idx)))
2174     newsgroup))
2175
2176 (defun gnus-make-directory (dir)
2177   "Make DIRECTORY recursively."
2178   (let* ((dir (expand-file-name dir default-directory))
2179          dirs)
2180     (if (string-match "/$" dir)
2181         (setq dir (substring dir 0 (match-beginning 0))))
2182     (while (not (file-exists-p dir))
2183       (setq dirs (cons dir dirs))
2184       (string-match "/[^/]+$" dir)
2185       (setq dir (substring dir 0 (match-beginning 0))))
2186     (while dirs
2187       (make-directory (car dirs))
2188       (setq dirs (cdr dirs)))))
2189
2190 (defun gnus-capitalize-newsgroup (newsgroup)
2191   "Capitalize NEWSGROUP name."
2192   (and (not (zerop (length newsgroup)))
2193        (concat (char-to-string (upcase (aref newsgroup 0)))
2194                (substring newsgroup 1))))
2195
2196 ;; Var
2197
2198 (defun gnus-simplify-subject (subject &optional re-only)
2199   "Remove `Re:' and words in parentheses.
2200 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2201   (let ((case-fold-search t))           ;Ignore case.
2202     ;; Remove `Re:' and `Re^N:'.
2203     (if (string-match "^re:[ \t]*" subject)
2204         (setq subject (substring subject (match-end 0))))
2205     ;; Remove words in parentheses from end.
2206     (or re-only
2207         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2208           (setq subject (substring subject 0 (match-beginning 0)))))
2209     ;; Return subject string.
2210     subject))
2211
2212 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2213 ;; all whitespace.
2214 (defun gnus-simplify-subject-fuzzy (subject)
2215   (let ((case-fold-search t))
2216     (save-excursion
2217       (gnus-set-work-buffer)
2218       (insert subject)
2219       (inline (gnus-simplify-buffer-fuzzy))
2220       (buffer-string))))
2221
2222 (defun gnus-simplify-buffer-fuzzy ()
2223   (goto-char (point-min))
2224   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2225   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2226                             nil t)
2227     (replace-match "" t t))
2228   (goto-char (point-min))
2229   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2230     (replace-match "" t t))
2231   (goto-char (point-min))
2232   (while (re-search-forward "[ \t]+" nil t)
2233     (replace-match " " t t))
2234   (goto-char (point-min))
2235   (while (re-search-forward "[ \t]+$" nil t)
2236     (replace-match "" t t))
2237   (goto-char (point-min))
2238   (while (re-search-forward "^[ \t]+" nil t)
2239     (replace-match "" t t))
2240   (if gnus-simplify-subject-fuzzy-regexp
2241       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2242         (replace-match "" t t)))
2243     )
2244
2245 ;; Add the current buffer to the list of buffers to be killed on exit. 
2246 (defun gnus-add-current-to-buffer-list ()
2247   (or (memq (current-buffer) gnus-buffer-list)
2248       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2249
2250 (defun gnus-string> (s1 s2)
2251   (not (or (string< s1 s2)
2252            (string= s1 s2))))
2253
2254 ;; Functions accessing headers.
2255 ;; Functions are more convenient than macros in some cases.
2256
2257 (defun gnus-header-number (header)
2258   (header-number header))
2259
2260 (defun gnus-header-subject (header)
2261   (header-subject header))
2262
2263 (defun gnus-header-from (header)
2264   (header-from header))
2265
2266 (defun gnus-header-xref (header)
2267   (header-xref header))
2268
2269 (defun gnus-header-lines (header)
2270   (header-lines header))
2271
2272 (defun gnus-header-date (header)
2273   (header-date header))
2274
2275 (defun gnus-header-id (header)
2276   (header-id header))
2277
2278 (defun gnus-header-references (header)
2279   (header-references header))
2280
2281 ;;; General various misc type functions.
2282
2283 (defun gnus-clear-system ()
2284   "Clear all variables and buffers."
2285   ;; Clear Gnus variables.
2286   (let ((variables gnus-variable-list))
2287     (while variables
2288       (set (car variables) nil)
2289       (setq variables (cdr variables))))
2290   ;; Clear other internal variables.
2291   (setq gnus-list-of-killed-groups nil
2292         gnus-have-read-active-file nil
2293         gnus-newsrc-alist nil
2294         gnus-newsrc-hashtb nil
2295         gnus-killed-list nil
2296         gnus-zombie-list nil
2297         gnus-killed-hashtb nil
2298         gnus-active-hashtb nil
2299         gnus-moderated-list nil
2300         gnus-description-hashtb nil
2301         gnus-newsgroup-headers nil
2302         gnus-newsgroup-headers-hashtb-by-number nil
2303         gnus-newsgroup-name nil
2304         gnus-server-alist nil
2305         gnus-current-select-method nil)
2306   ;; Reset any score variables.
2307   (and (boundp 'gnus-score-cache)
2308        (set 'gnus-score-cache nil))
2309   (and (boundp 'gnus-internal-global-score-files)
2310        (set 'gnus-internal-global-score-files nil))
2311   ;; Kill the startup file.
2312   (and gnus-current-startup-file
2313        (get-file-buffer gnus-current-startup-file)
2314        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2315   ;; Save any cache buffers.
2316   (and gnus-use-cache (gnus-cache-save-buffers))
2317   ;; Clear the dribble buffer.
2318   (gnus-dribble-clear)
2319   ;; Kill global KILL file buffer.
2320   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2321       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2322   (gnus-kill-buffer nntp-server-buffer)
2323   ;; Kill Gnus buffers.
2324   (while gnus-buffer-list
2325     (gnus-kill-buffer (car gnus-buffer-list))
2326     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2327
2328 (defun gnus-windows-old-to-new (setting)
2329   (if (symbolp setting)
2330       (setq setting 
2331             (cond ((eq setting 'SelectArticle)
2332                    'article)
2333                   ((eq setting 'SelectSubject)
2334                    'summary)
2335                   ((eq setting 'SelectNewsgroup)
2336                    'group)
2337                   (t setting))))
2338   (if (or (listp setting)
2339           (not (and gnus-window-configuration
2340                     (memq setting '(group summary article)))))
2341       setting
2342     (let* ((setting (if (eq setting 'group) 
2343                         (if (assq 'newsgroup gnus-window-configuration)
2344                             'newsgroup
2345                           'newsgroups) setting))
2346            (elem (car (cdr (assq setting gnus-window-configuration))))
2347            (total (apply '+ elem))
2348            (types '(group summary article))
2349            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2350            (i 0)
2351            perc
2352            out)
2353       (while (< i 3)
2354         (or (zerop (nth i elem))
2355             (progn
2356               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2357               (setq out (cons (if (eq pbuf (nth i types))
2358                                   (vector (nth i types) perc 'point)
2359                                 (vector (nth i types) perc))
2360                               out))))
2361         (setq i (1+ i)))
2362       (list (nreverse out)))))
2363            
2364 (defun gnus-add-configuration (conf)
2365   (setq gnus-buffer-configuration 
2366         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2367                          gnus-buffer-configuration))))
2368
2369 (defun gnus-configure-windows (setting)
2370   (setq setting (gnus-windows-old-to-new setting))
2371   (let ((r (if (symbolp setting)
2372                   (cdr (assq setting gnus-buffer-configuration))
2373                 setting))
2374         (in-buf (current-buffer))
2375         rule val w height hor ohor heights sub jump-buffer
2376         rel total to-buf)
2377     (or r (error "No such setting: %s" setting))
2378
2379     ;; Either remove all windows or just remove all Gnus windows.
2380     (if gnus-use-full-window
2381         (delete-other-windows)
2382       (gnus-remove-some-windows)
2383       (switch-to-buffer nntp-server-buffer))
2384
2385     (while r
2386       (setq hor (car r)
2387             ohor nil)
2388
2389       ;; We have to do the (possible) horizontal splitting before the
2390       ;; vertical. 
2391       (if (and (listp (car hor)) 
2392                (eq (car (car hor)) 'horizontal))
2393           (progn
2394             (split-window 
2395              nil
2396              (if (integerp (nth 1 (car hor)))
2397                  (nth 1 (car hor))
2398                (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2399              t)
2400             (setq hor (cdr hor))))
2401
2402       ;; Go through the rules and eval the elements that are to be
2403       ;; evaled.  
2404       (while hor
2405         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2406             (progn
2407               ;; Expand short buffer name.
2408               (setq w (aref val 0))
2409               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2410                    (progn
2411                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2412                      (aset val 0 w)))
2413               (setq ohor (cons val ohor))))
2414         (setq hor (cdr hor)))
2415       (setq rule (cons (nreverse ohor) rule))
2416       (setq r (cdr r)))
2417     (setq rule (nreverse rule))
2418
2419     ;; We tally the window sizes.
2420     (setq total (window-height))
2421     (while rule
2422       (setq hor (car rule))
2423       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2424           (setq hor (cdr hor)))
2425       (setq sub 0)
2426       (while hor
2427         (setq rel (aref (car hor) 1)
2428               heights (cons
2429                        (cond ((and (floatp rel) (= 1.0 rel))
2430                               'x)
2431                              ((integerp rel)
2432                               rel)
2433                              (t
2434                               (max (floor (* total rel)) 4)))
2435                        heights)
2436               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2437               hor (cdr hor)))
2438       (setq heights (nreverse heights)
2439             hor (car rule))
2440
2441       ;; We then go through these heighs and create windows for them.
2442       (while heights
2443         (setq height (car heights)
2444               heights (cdr heights))
2445         (and (eq height 'x)
2446              (setq height (- total sub)))
2447         (and heights
2448              (split-window nil height))
2449         (setq to-buf (aref (car hor) 0))
2450         (switch-to-buffer 
2451          (cond ((not to-buf)
2452                 in-buf)
2453                ((symbolp to-buf)
2454                 (symbol-value (aref (car hor) 0)))
2455                (t
2456                 (aref (car hor) 0))))
2457         (and (> (length (car hor)) 2)
2458              (eq (aref (car hor) 2) 'point)
2459              (setq jump-buffer (current-buffer)))
2460         (other-window 1)
2461         (setq hor (cdr hor)))
2462       
2463       (setq rule (cdr rule)))
2464
2465     ;; Finally, we pop to the buffer that's supposed to have point. 
2466     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2467
2468     (select-window (get-buffer-window jump-buffer))
2469     (set-buffer jump-buffer)))
2470       
2471 (defun gnus-remove-some-windows ()
2472   (let ((buffers gnus-window-to-buffer)
2473         (first t)
2474         buf)
2475     (save-excursion
2476       ;; Remove windows on all known Gnus buffers.
2477       (while buffers
2478         (setq buf (cdr (car buffers)))
2479         (if (symbolp buf)
2480             (setq buf (and (boundp buf) (symbol-value buf))))
2481         (and buf 
2482              (get-buffer-window buf)
2483              (progn
2484                (if first
2485                    (progn
2486                      (pop-to-buffer buf)
2487                      (switch-to-buffer nntp-server-buffer)
2488                      (setq first nil))
2489                  (delete-window (get-buffer-window buf)))))
2490         (setq buffers (cdr buffers)))
2491       ;; Remove windows on *all* summary buffers.
2492       (let ((buffers (buffer-list)))
2493         (while buffers
2494           (if (and (string-match 
2495                     "^\\*Summary" (or (buffer-name (car buffers)) ""))
2496                    (get-buffer-window (car buffers)))
2497               (delete-window (get-buffer-window (car buffers))))
2498           (setq buffers (cdr buffers)))))))
2499                           
2500 (defun gnus-version ()
2501   "Version numbers of this version of Gnus."
2502   (interactive)
2503   (let ((methods gnus-valid-select-methods)
2504         (mess gnus-version)
2505         meth)
2506     ;; Go through all the legal select methods and add their version
2507     ;; numbers to the total version string. Only the backends that are
2508     ;; currently in use will have their message numbers taken into
2509     ;; consideration. 
2510     (while methods
2511       (setq meth (intern (concat (car (car methods)) "-version")))
2512       (and (boundp meth)
2513            (stringp (symbol-value meth))
2514            (setq mess (concat mess "; " (symbol-value meth))))
2515       (setq methods (cdr methods)))
2516     (gnus-message 2 mess)))
2517
2518 (defun gnus-info-find-node ()
2519   "Find Info documentation of Gnus."
2520   (interactive)
2521   ;; Enlarge info window if needed.
2522   (let ((mode major-mode))
2523     (gnus-configure-windows 'info)
2524     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2525
2526 (defun gnus-overload-functions (&optional overloads)
2527   "Overload functions specified by optional argument OVERLOADS.
2528 If nothing is specified, use the variable gnus-overload-functions."
2529   (let ((defs nil)
2530         (overloads (or overloads gnus-overload-functions)))
2531     (while overloads
2532       (setq defs (car overloads))
2533       (setq overloads (cdr overloads))
2534       ;; Load file before overloading function if necessary.  Make
2535       ;; sure we cannot use `require' always.
2536       (and (not (fboundp (car defs)))
2537            (car (cdr (cdr defs)))
2538            (load (car (cdr (cdr defs))) nil 'nomessage))
2539       (fset (car defs) (car (cdr defs))))))
2540
2541 (defun gnus-replace-chars-in-string (string from to)
2542   "Replace characters in STRING from FROM to TO."
2543   (let ((string (substring string 0))   ;Copy string.
2544         (len (length string))
2545         (idx 0))
2546     ;; Replace all occurrences of FROM with TO.
2547     (while (< idx len)
2548       (if (= (aref string idx) from)
2549           (aset string idx to))
2550       (setq idx (1+ idx)))
2551     string))
2552
2553 (defun gnus-days-between (date1 date2)
2554   ;; Return the number of days between date1 and date2.
2555   (- (gnus-day-number date1) (gnus-day-number date2)))
2556
2557 (defun gnus-day-number (date)
2558   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2559                      (timezone-parse-date date))))
2560     (timezone-absolute-from-gregorian 
2561      (nth 1 dat) (nth 2 dat) (car dat))))
2562
2563 ;; Returns a floating point number that says how many seconds have
2564 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2565 (defun gnus-seconds-since-epoch (date)
2566   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2567                         (timezone-parse-date date)))
2568          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2569                         (timezone-parse-time
2570                          (aref (timezone-parse-date date) 3))))
2571          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2572                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2573          (tday (- (timezone-absolute-from-gregorian 
2574                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2575                   (timezone-absolute-from-gregorian 
2576                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2577     (+ (nth 2 ttime)
2578        (* (nth 1 ttime) 60)
2579        (* 1.0 (nth 0 ttime) 60 60)
2580        (* 1.0 tday 60 60 24))))
2581
2582 (defun gnus-file-newer-than (file date)
2583   (let ((fdate (nth 5 (file-attributes file))))
2584     (or (> (car fdate) (car date))
2585         (and (= (car fdate) (car date))
2586              (> (nth 1 fdate) (nth 1 date))))))
2587
2588 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2589 ;; the echo area.
2590 (defun gnus-y-or-n-p (prompt)
2591   (prog1
2592       (y-or-n-p prompt)
2593     (message "")))
2594
2595 (defun gnus-yes-or-no-p (prompt)
2596   (prog1
2597       (yes-or-no-p prompt)
2598     (message "")))
2599
2600 ;; Check whether to use long file names.
2601 (defun gnus-use-long-file-name (symbol)
2602   ;; The variable has to be set...
2603   (and gnus-use-long-file-name
2604        ;; If it isn't a list, then we return t.
2605        (or (not (listp gnus-use-long-file-name))
2606            ;; If it is a list, and the list contains `symbol', we
2607            ;; return nil.  
2608            (not (memq symbol gnus-use-long-file-name)))))
2609
2610 ;; I suspect there's a better way, but I haven't taken the time to do
2611 ;; it yet. -erik selberg@cs.washington.edu
2612 (defun gnus-dd-mmm (messy-date)
2613   "Return a string like DD-MMM from a big messy string"
2614   (let ((datevec (timezone-parse-date messy-date)))
2615     (format "%2s-%s"
2616             (or (aref datevec 2) "??")
2617             (capitalize
2618              (or (car 
2619                   (nth (1- (string-to-number (aref datevec 1)))
2620                        timezone-months-assoc))
2621                  "???")))))
2622
2623 ;; Make a hash table (default and minimum size is 255).
2624 ;; Optional argument HASHSIZE specifies the table size.
2625 (defun gnus-make-hashtable (&optional hashsize)
2626   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2627
2628 ;; Make a number that is suitable for hashing; bigger than MIN and one
2629 ;; less than 2^x.
2630 (defun gnus-create-hash-size (min)
2631   (let ((i 1))
2632     (while (< i min)
2633       (setq i (* 2 i)))
2634     (1- i)))
2635
2636 ;; Show message if message has a lower level than `gnus-verbose'. 
2637 ;; Guide-line for numbers:
2638 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2639 ;; for things that take a long time, 7 - not very important messages
2640 ;; on stuff, 9 - messages inside loops.
2641 (defun gnus-message (level &rest args)
2642   (if (<= level gnus-verbose)
2643       (apply 'message args)
2644     ;; We have to do this format thingie here even if the result isn't
2645     ;; shown - the return value has to be the same as the return value
2646     ;; from `message'.
2647     (apply 'format args)))
2648
2649 ;; Generate a unique new group name.
2650 (defun gnus-generate-new-group-name (leaf)
2651   (let ((name leaf)
2652         (num 0))
2653     (while (gnus-gethash name gnus-newsrc-hashtb)
2654       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2655     name))
2656
2657 (defun gnus-find-file-noselect (file &optional force)
2658   "Does vaguely the same as find-file-noselect. No hooks are run."
2659   (let (buf insert)
2660     (if (setq buf (get-file-buffer file))
2661         (setq insert force)
2662       (setq buf (create-file-buffer file))
2663       (setq insert t))
2664     (if (not insert)
2665         buf
2666       (save-excursion
2667         (set-buffer buf)
2668         (erase-buffer)
2669         (and (file-readable-p file)
2670              (insert-file-contents file))
2671         (set-visited-file-name file)
2672         (set-buffer-modified-p nil)
2673         (current-buffer)))))
2674
2675 ;;; List and range functions
2676
2677 (defun gnus-last-element (list)
2678   "Return last element of LIST."
2679   (while (cdr list)
2680     (setq list (cdr list)))
2681   (car list))
2682
2683 (defun gnus-copy-sequence (list)
2684   "Do a complete, total copy of a list."
2685   (if (and (consp list) (not (consp (cdr list))))
2686       (cons (car list) (cdr list))
2687     (mapcar (lambda (elem) (if (consp elem) 
2688                                (if (consp (cdr elem))
2689                                    (gnus-copy-sequence elem)
2690                                  (cons (car elem) (cdr elem)))
2691                              elem))
2692             list)))
2693
2694 (defun gnus-set-difference (list1 list2)
2695   "Return a list of elements of LIST1 that do not appear in LIST2."
2696   (let ((list1 (copy-sequence list1)))
2697     (while list2
2698       (setq list1 (delq (car list2) list1))
2699       (setq list2 (cdr list2)))
2700     list1))
2701
2702 (defun gnus-sorted-complement (list1 list2)
2703   "Return a list of elements of LIST1 that do not appear in LIST2.
2704 Both lists have to be sorted over <."
2705   (let (out)
2706     (if (or (null list1) (null list2))
2707         (or list1 list2)
2708       (while (and list1 list2)
2709         (cond ((= (car list1) (car list2))
2710                (setq list1 (cdr list1)
2711                      list2 (cdr list2)))
2712               ((< (car list1) (car list2))
2713                (setq out (cons (car list1) out))
2714                (setq list1 (cdr list1)))
2715               (t
2716                (setq out (cons (car list2) out))
2717                (setq list2 (cdr list2)))))
2718       (nconc (nreverse out) (or list1 list2)))))
2719
2720 (defun gnus-intersection (list1 list2)      
2721   (let ((result nil))
2722     (while list2
2723       (if (memq (car list2) list1)
2724           (setq result (cons (car list2) result)))
2725       (setq list2 (cdr list2)))
2726     result))
2727
2728 (defun gnus-sorted-intersection (list1 list2)
2729   ;; LIST1 and LIST2 have to be sorted over <.
2730   (let (out)
2731     (while (and list1 list2)
2732       (cond ((= (car list1) (car list2))
2733              (setq out (cons (car list1) out)
2734                    list1 (cdr list1)
2735                    list2 (cdr list2)))
2736             ((< (car list1) (car list2))
2737              (setq list1 (cdr list1)))
2738             (t
2739              (setq list2 (cdr list2)))))
2740     (nreverse out)))
2741
2742 (defun gnus-set-sorted-intersection (list1 list2)
2743   ;; LIST1 and LIST2 have to be sorted over <.
2744   ;; This function modifies LIST1.
2745   (let* ((top (cons nil list1))
2746          (prev top))
2747   (while (and list1 list2)
2748     (cond ((= (car list1) (car list2))
2749            (setq prev list1
2750                  list1 (cdr list1)
2751                  list2 (cdr list2)))
2752           ((< (car list1) (car list2))
2753            (setcdr prev (cdr list1))
2754            (setq list1 (cdr list1)))
2755           (t
2756            (setq list2 (cdr list2)))))
2757   (setcdr prev nil)
2758   (cdr top)))
2759
2760 (defun gnus-compress-sequence (numbers &optional always-list)
2761   "Convert list of numbers to a list of ranges or a single range.
2762 If ALWAYS-LIST is non-nil, this function will always release a list of
2763 ranges."
2764   (let* ((first (car numbers))
2765          (last (car numbers))
2766          result)
2767     (if (null numbers)
2768         nil
2769       (if (not (listp (cdr numbers)))
2770           numbers
2771         (while numbers
2772           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2773                 ((= (1+ last) (car numbers)) ;Still in sequence
2774                  (setq last (car numbers)))
2775                 (t                      ;End of one sequence
2776                  (setq result 
2777                        (cons (if (= first last) first
2778                                (cons first last)) result))
2779                  (setq first (car numbers))
2780                  (setq last  (car numbers))))
2781           (setq numbers (cdr numbers)))
2782         (if (and (not always-list) (null result))
2783             (if (= first last) (list first) (cons first last))
2784           (nreverse (cons (if (= first last) first (cons first last))
2785                           result)))))))
2786
2787 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2788 (defun gnus-uncompress-range (ranges)
2789   "Expand a list of ranges into a list of numbers.
2790 RANGES is either a single range on the form `(num . num)' or a list of
2791 these ranges."
2792   (let (first last result)
2793     (cond 
2794      ((null ranges)
2795       nil)
2796      ((not (listp (cdr ranges)))
2797       (setq first (car ranges))
2798       (setq last (cdr ranges))
2799       (while (<= first last)
2800         (setq result (cons first result))
2801         (setq first (1+ first)))
2802       (nreverse result))
2803      (t
2804       (while ranges
2805         (if (atom (car ranges))
2806             (if (numberp (car ranges))
2807                 (setq result (cons (car ranges) result)))
2808           (setq first (car (car ranges)))
2809           (setq last  (cdr (car ranges)))
2810           (while (<= first last)
2811             (setq result (cons first result))
2812             (setq first (1+ first))))
2813         (setq ranges (cdr ranges)))
2814       (nreverse result)))))
2815
2816 (defun gnus-add-to-range (ranges list)
2817   "Return a list of ranges that has all articles from both RANGES and LIST.
2818 Note: LIST has to be sorted over `<'."
2819   (if (not ranges)
2820       (gnus-compress-sequence list t)
2821     (setq list (copy-sequence list))
2822     (or (listp (cdr ranges))
2823         (setq ranges (list ranges)))
2824     (let ((out ranges)
2825           ilist lowest highest temp)
2826       (while (and ranges list)
2827         (setq ilist list)
2828         (setq lowest (or (and (atom (car ranges)) (car ranges))
2829                          (car (car ranges))))
2830         (while (and list (cdr list) (< (car (cdr list)) lowest))
2831           (setq list (cdr list)))
2832         (if (< (car ilist) lowest)
2833             (progn
2834               (setq temp list)
2835               (setq list (cdr list))
2836               (setcdr temp nil)
2837               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2838         (setq highest (or (and (atom (car ranges)) (car ranges))
2839                           (cdr (car ranges))))
2840         (while (and list (<= (car list) highest))
2841           (setq list (cdr list)))
2842         (setq ranges (cdr ranges)))
2843       (if list
2844           (setq out (nconc (gnus-compress-sequence list t) out)))
2845       (setq out (sort out (lambda (r1 r2) 
2846                             (< (or (and (atom r1) r1) (car r1))
2847                                (or (and (atom r2) r2) (car r2))))))
2848       (setq ranges out)
2849       (while ranges
2850         (if (atom (car ranges))
2851             (if (cdr ranges)
2852                 (if (atom (car (cdr ranges)))
2853                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2854                         (progn
2855                           (setcar ranges (cons (car ranges) 
2856                                                (car (cdr ranges))))
2857                           (setcdr ranges (cdr (cdr ranges)))))
2858                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2859                       (progn
2860                         (setcar (car (cdr ranges)) (car ranges))
2861                         (setcar ranges (car (cdr ranges)))
2862                         (setcdr ranges (cdr (cdr ranges)))))))
2863           (if (cdr ranges)
2864               (if (atom (car (cdr ranges)))
2865                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2866                       (progn
2867                         (setcdr (car ranges) (car (cdr ranges)))
2868                         (setcdr ranges (cdr (cdr ranges)))))
2869                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2870                     (progn
2871                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2872                       (setcdr ranges (cdr (cdr ranges))))))))
2873         (setq ranges (cdr ranges)))
2874       out)))
2875
2876 (defun gnus-remove-from-range (ranges list)
2877   "Return a list of ranges that has all articles from LIST removed from RANGES.
2878 Note: LIST has to be sorted over `<'."
2879   ;; !!! This function shouldn't look like this, but I've got a headache.
2880   (gnus-compress-sequence 
2881    (gnus-sorted-complement
2882     (gnus-uncompress-range ranges) list)))
2883
2884 (defun gnus-member-of-range (number ranges)
2885   (if (not (listp (cdr ranges)))
2886       (and (>= number (car ranges)) 
2887            (<= number (cdr ranges)))
2888     (let ((not-stop t))
2889       (while (and ranges 
2890                   (if (numberp (car ranges))
2891                       (>= number (car ranges))
2892                     (>= number (car (car ranges))))
2893                   not-stop)
2894         (if (if (numberp (car ranges))
2895                 (= number (car ranges))
2896               (and (>= number (car (car ranges)))
2897                    (<= number (cdr (car ranges)))))
2898             (setq not-stop nil))
2899         (setq ranges (cdr ranges)))
2900       (not not-stop))))
2901
2902 \f
2903 ;;;
2904 ;;; Gnus group mode
2905 ;;;
2906
2907 (defvar gnus-group-mode-map nil)
2908 (defvar gnus-group-group-map nil)
2909 (defvar gnus-group-mark-map nil)
2910 (defvar gnus-group-list-map nil)
2911 (defvar gnus-group-sub-map nil)
2912 (put 'gnus-group-mode 'mode-class 'special)
2913
2914 (if gnus-group-mode-map
2915     nil
2916   (setq gnus-group-mode-map (make-keymap))
2917   (suppress-keymap gnus-group-mode-map)
2918   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2919   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2920   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2921   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2922   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2923   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2924   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2925   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2926   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2927   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2928   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2929   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2930   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2931   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2932   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2933   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2934   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2935   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2936   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2937   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2938   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2939   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2940   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2941   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2942   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2943   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2944   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2945   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2946   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2947   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2948   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2949   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2950   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2951   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2952   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2953   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2954   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2955   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2956   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2957   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2958   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2959   (define-key gnus-group-mode-map "V" 'gnus-version)
2960   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2961   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2962   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2963   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2964   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2965   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2966   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2967   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2968   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2969   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2970   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
2971   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2972   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2973   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
2974   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
2975
2976   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
2977   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
2978   (define-prefix-command 'gnus-group-mark-map)
2979   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
2980   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
2981   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
2982   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
2983
2984   (define-prefix-command 'gnus-group-group-map)
2985   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
2986   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
2987   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
2988   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
2989   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
2990   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
2991   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
2992   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
2993   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
2994   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
2995   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
2996   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
2997   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
2998   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
2999   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3000   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3001   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3002   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3003
3004   (define-prefix-command 'gnus-group-list-map)
3005   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3006   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3007   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3008   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3009   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3010   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3011   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3012   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3013   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3014
3015   (define-prefix-command 'gnus-group-sub-map)
3016   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3017   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3018   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3019   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3020   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3021   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3022   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3023   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3024
3025 (defun gnus-group-mode ()
3026   "Major mode for reading news.
3027
3028 All normal editing commands are switched off.
3029 \\<gnus-group-mode-map>
3030 The group buffer lists (some of) the groups available.  For instance,
3031 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3032 lists all zombie groups. 
3033
3034 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3035 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3036
3037 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3038
3039 The following commands are available:
3040
3041 \\{gnus-group-mode-map}"
3042   (interactive)
3043   (if gnus-visual (gnus-group-make-menu-bar))
3044   (kill-all-local-variables)
3045   (setq mode-line-modified "-- ")
3046   (make-local-variable 'mode-line-format)
3047   (setq mode-line-format (copy-sequence mode-line-format))
3048   (and (equal (nth 3 mode-line-format) "   ")
3049        (setcar (nthcdr 3 mode-line-format) ""))
3050   (setq major-mode 'gnus-group-mode)
3051   (setq mode-name "Group")
3052   (gnus-group-set-mode-line)
3053   (setq mode-line-process nil)
3054   (use-local-map gnus-group-mode-map)
3055   (buffer-disable-undo (current-buffer))
3056   (setq truncate-lines t)
3057   (setq buffer-read-only t)
3058   (run-hooks 'gnus-group-mode-hook))
3059
3060 (defun gnus-mouse-pick-group (e)
3061   (interactive "e")
3062   (mouse-set-point e)
3063   (gnus-group-read-group nil))
3064
3065 ;;;###autoload
3066 (defun gnus-no-server (&optional arg)
3067   "Read network news.
3068 If ARG is a positive number, Gnus will use that as the
3069 startup level. If ARG is nil, Gnus will be started at level 2. 
3070 If ARG is non-nil and not a positive number, Gnus will
3071 prompt the user for the name of an NNTP server to use.
3072 As opposed to `gnus', this command will not connect to the local server."
3073   (interactive "P")
3074   (setq gnus-group-use-permanent-levels t)
3075   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3076
3077 (defalias '\(ding\) 'gnus)
3078
3079 ;;;###autoload
3080 (defun gnus (&optional arg dont-connect)
3081   "Read network news.
3082 If ARG is non-nil and a positive number, Gnus will use that as the
3083 startup level. If ARG is non-nil and not a positive number, Gnus will
3084 prompt the user for the name of an NNTP server to use."
3085   (interactive "P")
3086   (if (get-buffer gnus-group-buffer)
3087       (progn
3088         (switch-to-buffer gnus-group-buffer)
3089         (gnus-group-get-new-news))
3090     (gnus-clear-system)
3091     (nnheader-init-server-buffer)
3092     (gnus-read-init-file)
3093     (let ((level (and arg (numberp arg) (> arg 0) arg))
3094           did-connect)
3095       (unwind-protect
3096           (progn
3097             (gnus-group-setup-buffer)
3098             (or dont-connect 
3099                 (setq did-connect
3100                       (gnus-start-news-server (and arg (not level))))))
3101         (if (and (not dont-connect) 
3102                  (not did-connect))
3103             (gnus-group-quit)
3104           (run-hooks 'gnus-startup-hook)
3105           ;; NNTP server is successfully open. 
3106           (gnus-update-format-specifications)
3107           (gnus-summary-make-display-table)
3108           (let ((buffer-read-only nil))
3109             (erase-buffer)
3110             (if (not gnus-inhibit-startup-message)
3111                 (progn
3112                   (gnus-group-startup-message)
3113                   (sit-for 0))))
3114           (gnus-setup-news nil level)
3115           (and gnus-use-dribble-file (gnus-dribble-open))
3116           (gnus-group-list-groups level)
3117           (gnus-configure-windows 'group))))))
3118
3119 (defun gnus-group-startup-message (&optional x y)
3120   "Insert startup message in current buffer."
3121   ;; Insert the message.
3122   (erase-buffer)
3123   (insert
3124    (format "
3125     %s
3126            A newsreader 
3127       for GNU Emacs
3128
3129         Based on GNUS 
3130              written by 
3131      Masanobu UMEDA
3132
3133     Lars Magne 
3134          Ingebrigtsen 
3135       larsi@ifi.uio.no
3136
3137            gnus-version))
3138   ;; And then hack it.
3139   ;; 18 is the longest line.
3140   (indent-rigidly (point-min) (point-max) 
3141                   (/ (max (- (window-width) (or x 28)) 0) 2))
3142   (goto-char (point-min))
3143   ;; +4 is fuzzy factor.
3144   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3145
3146 (defun gnus-group-setup-buffer ()
3147   (or (get-buffer gnus-group-buffer)
3148       (progn
3149         (switch-to-buffer gnus-group-buffer)
3150         (gnus-add-current-to-buffer-list)
3151         (gnus-group-mode)
3152         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3153
3154 (defun gnus-group-list-groups (level &optional unread)
3155   "List newsgroups with level LEVEL or lower that have unread articles.
3156 Default is all subscribed groups.
3157 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3158   (interactive (list (and current-prefix-arg
3159                           (prefix-numeric-value current-prefix-arg))))
3160   (if gnus-group-use-permanent-levels
3161       (progn
3162         (setq gnus-group-default-list-level 
3163               (or level gnus-group-default-list-level))
3164         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3165     (setq level (or level gnus-group-default-list-level 
3166                     gnus-level-subscribed)))
3167   (gnus-group-setup-buffer)     ;May call from out of group buffer
3168   (let ((case-fold-search nil)
3169         (group (gnus-group-group-name)))
3170     (funcall gnus-group-prepare-function level unread nil)
3171     (if (zerop (buffer-size))
3172         (gnus-message 5 gnus-no-groups-message)
3173       (goto-char (point-min))
3174       (if (not group)
3175           ;; Go to the first group with unread articles.
3176           (gnus-group-search-forward nil nil nil t)
3177         ;; Find the right group to put point on. If the current group
3178         ;; has disapeared in the new listing, try to find the next
3179         ;; one. If no next one can be found, just leave point at the
3180         ;; first newsgroup in the buffer.
3181         (if (not (gnus-goto-char
3182                   (text-property-any (point-min) (point-max) 
3183                                      'gnus-group (intern group))))
3184             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3185               (while (and newsrc
3186                           (not (gnus-goto-char 
3187                                 (text-property-any 
3188                                  (point-min) (point-max) 'gnus-group 
3189                                  (intern (car (car newsrc)))))))
3190                 (setq newsrc (cdr newsrc)))
3191               (or newsrc (progn (goto-char (point-max))
3192                                 (forward-line -1))))))
3193       ;; Adjust cursor point.
3194       (gnus-group-position-cursor))))
3195
3196 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3197   "List all newsgroups with unread articles of level LEVEL or lower.
3198 If ALL is non-nil, list groups that have no unread articles.
3199 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3200 If REGEXP, only list groups matching REGEXP."
3201   (set-buffer gnus-group-buffer)
3202   (let ((buffer-read-only nil)
3203         (newsrc (cdr gnus-newsrc-alist))
3204         (lowest (or lowest 1))
3205         info clevel unread group)
3206     (erase-buffer)
3207     (if (< lowest gnus-level-zombie)
3208         ;; List living groups.
3209         (while newsrc
3210           (setq info (car newsrc)
3211                 group (car info)
3212                 newsrc (cdr newsrc)
3213                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3214           (and unread ; This group might be bogus
3215                (or (not regexp)
3216                    (string-match regexp group))
3217                (<= (setq clevel (car (cdr info))) level) 
3218                (>= clevel lowest)
3219                (or all            ; We list all groups?
3220                    (eq unread t)  ; We list unactivated groups
3221                    (> unread 0)   ; We list groups with unread articles
3222                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3223                (gnus-group-insert-group-line 
3224                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3225
3226     ;; List dead groups.
3227     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3228          (gnus-group-prepare-flat-list-dead 
3229           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3230           gnus-level-zombie ?Z
3231           regexp))
3232     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3233          (gnus-group-prepare-flat-list-dead 
3234           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3235           gnus-level-killed ?K regexp))
3236
3237     (gnus-group-set-mode-line)
3238     (setq gnus-have-all-newsgroups all)
3239     (run-hooks 'gnus-group-prepare-hook)))
3240
3241 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3242   ;; List zombies and killed lists somehwat faster, which was
3243   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3244   ;; this by ignoring the group format specification altogether.
3245   (let (group beg)
3246     (while groups
3247       (setq group (car groups)
3248             groups (cdr groups))
3249       (if (or (not regexp)
3250               (string-match regexp group))
3251           (progn
3252             (setq beg (point))
3253             (insert (format " %c     *: %s\n" mark group))
3254             (add-text-properties 
3255              beg (1+ beg) 
3256              (list 'gnus-group (intern group)
3257                    'gnus-unread t
3258                    'gnus-level level)))))))
3259
3260 (defun gnus-group-real-name (group)
3261   "Find the real name of a foreign newsgroup."
3262   (if (string-match ":[^:]+$" group)
3263       (substring group (1+ (match-beginning 0)))
3264     group))
3265
3266 (defun gnus-group-prefixed-name (group method)
3267   "Return the whole name from GROUP and METHOD."
3268   (and (stringp method) (setq method (gnus-server-to-method method)))
3269   (concat (format "%s" (car method))
3270           (if (and 
3271                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3272                (not (string= (nth 1 method) "")))
3273               (concat "+" (nth 1 method)))
3274           ":" group))
3275
3276 (defun gnus-group-real-prefix (group)
3277   "Return the prefix of the current group name."
3278   (if (string-match "^[^:]+:" group)
3279       (substring group 0 (match-end 0))
3280     ""))
3281
3282 (defun gnus-group-method-name (group)
3283   "Return the method used for selecting GROUP."
3284   (let ((prefix (gnus-group-real-prefix group)))
3285     (if (equal prefix "")
3286         gnus-select-method
3287       (if (string-match "^[^\\+]+\\+" prefix)
3288           (list (intern (substring prefix 0 (1- (match-end 0))))
3289                 (substring prefix (match-end 0) (1- (length prefix))))
3290         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3291
3292 (defun gnus-group-foreign-p (group)
3293   "Return nil if GROUP is native, non-nil if it is foreign."
3294   (string-match ":" group))
3295
3296 (defun gnus-group-set-info (info &optional method-only-group part)
3297   (let* ((entry (gnus-gethash
3298                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3299          (part-info info)
3300          (info (if method-only-group (nth 2 entry) info)))
3301     (if (not method-only-group)
3302         ()
3303       (or entry
3304           (error "Trying to change non-existent group %s" method-only-group))
3305       ;; We have recevied parts of the actual group info - either the
3306       ;; select method or the group parameters.  We first check
3307       ;; whether we have to extend the info, and if so, do that.
3308       (let ((len (length info))
3309             (total (if (eq part 'method) 5 6)))
3310         (and (< len total)
3311              (setcdr (nthcdr (1- len) info)
3312                      (make-list (- total len) nil)))
3313         ;; Then we enter the new info.
3314         (setcar (nthcdr (1- total) info) part-info)))
3315     ;; We uncompress some lists of marked articles.
3316     (let (marked)
3317       (if (not (setq marked (nth 3 info)))
3318           ()
3319         (while marked
3320           (or (eq 'score (car (car marked)))
3321               (eq 'bookmark (car (car marked)))
3322               (eq 'killed (car (car marked)))
3323               (setcdr (car marked) 
3324                       (gnus-uncompress-range (cdr (car marked)))))
3325           (setq marked (cdr marked)))))
3326     (if entry
3327         ()
3328       ;; This is a new group, so we just create it.
3329       (save-excursion
3330         (set-buffer gnus-group-buffer)
3331         (if (nth 4 info)
3332             ;; It's a foreign group...
3333             (gnus-group-make-group 
3334              (gnus-group-real-name (car info))
3335              (prin1-to-string (car (nth 4 info)))
3336              (nth 1 (nth 4 info)))
3337           ;; It's a native group.
3338           (gnus-group-make-group
3339            (car info)
3340            (prin1-to-string (car gnus-select-method))
3341            (nth 1 gnus-select-method)))
3342         (gnus-message 6 "Note: New group created")
3343         (setq entry 
3344               (gnus-gethash (gnus-group-prefixed-name 
3345                              (gnus-group-real-name (car info))
3346                              (or (nth 4 info) gnus-select-method))
3347                             gnus-newsrc-hashtb))))
3348     ;; Whether it was a new group or not, we now have the entry, so we
3349     ;; can do the update.
3350     (if entry
3351         (progn
3352           (setcar (nthcdr 2 entry) info)
3353           (if (and (not (eq (car entry) t)) 
3354                    (gnus-gethash (car info) gnus-active-hashtb))
3355               (let ((marked (nth 3 info)))
3356                 (setcar entry 
3357                         (max 0 (- (length (gnus-list-of-unread-articles 
3358                                            (car info)))
3359                                   (length (cdr (assq 'tick marked)))
3360                                   (length (cdr (assq 'dormant marked)))))))))
3361       (error "No such group: %s" (car info)))))
3362
3363 (defun gnus-group-set-method-info (group select-method)
3364   (gnus-group-set-info select-method group 'method))
3365
3366 (defun gnus-group-set-params-info (group params)
3367   (gnus-group-set-info params group 'params))
3368
3369 (defun gnus-group-update-group-line ()
3370   "This function updates the current line in the newsgroup buffer and
3371 moves the point to the colon."
3372   (let* ((buffer-read-only nil)
3373          (group (gnus-group-group-name))
3374          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3375     (if entry
3376         (gnus-dribble-enter 
3377          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3378                  ")")))
3379     (beginning-of-line)
3380     (delete-region (point) (progn (forward-line 1) (point)))
3381     (gnus-group-insert-group-line-info group)
3382     (forward-line -1)
3383     (gnus-group-position-cursor)))
3384
3385 (defun gnus-group-insert-group-line-info (group)
3386   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3387         active info)
3388     (if entry
3389         (progn
3390           (setq info (nth 2 entry))
3391           (gnus-group-insert-group-line 
3392            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3393       (setq active (gnus-gethash group gnus-active-hashtb))
3394       (gnus-group-insert-group-line 
3395        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3396                    gnus-level-killed)
3397        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3398
3399 (defun gnus-group-insert-group-line (gformat group level marked number method)
3400   (let* ((gformat (or gformat gnus-group-line-format-spec))
3401          (active (gnus-gethash group gnus-active-hashtb))
3402          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3403          (number-of-dormant (length (cdr (assq 'dormant marked))))
3404          (number-of-ticked (length (cdr (assq 'tick marked))))
3405          (number-of-ticked-and-dormant
3406           (+ number-of-ticked number-of-dormant))
3407          (number-of-unread-unticked 
3408           (if (numberp number) (int-to-string (max 0 number))
3409             "*"))
3410          (number-of-read
3411           (if (numberp number)
3412               (max 0 (- number-total number))
3413             "*"))
3414          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3415                            ((<= level gnus-level-unsubscribed) ?U)
3416                            ((= level gnus-level-zombie) ?Z)
3417                            (t ?K)))
3418          (qualified-group (gnus-group-real-name group))
3419          (newsgroup-description 
3420           (if gnus-description-hashtb
3421               (or (gnus-gethash group gnus-description-hashtb) "")
3422             ""))
3423          (moderated (if (member group gnus-moderated-list) ?m ? ))
3424          (moderated-string (if (eq moderated ?m) "(m)" ""))
3425          (method (gnus-server-get-method group method))
3426          (news-server (or (car (cdr method)) ""))
3427          (news-method (or (car method) ""))
3428          (news-method-string 
3429           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3430          (marked (if (and 
3431                       (numberp number) 
3432                       (zerop number)
3433                       (> number-of-ticked 0))
3434                      ?* ? ))
3435          (number (if (eq number t) "*" (+ number number-of-dormant 
3436                                           number-of-ticked)))
3437          (process-marked (if (member qualified-group gnus-group-marked)
3438                              gnus-process-mark ? ))
3439          (buffer-read-only nil)
3440          b)
3441     (beginning-of-line)
3442     (setq b (point))
3443     ;; Insert the text.
3444     (insert (eval gformat))
3445
3446     (add-text-properties 
3447      b (1+ b) (list 'gnus-group (intern group)
3448                     'gnus-unread (if (numberp number)
3449                                      (string-to-int number-of-unread-unticked)
3450                                    t)
3451                     'gnus-marked marked
3452                     'gnus-level level))))
3453
3454 (defun gnus-group-update-group (group &optional visible-only)
3455   "Update newsgroup info of GROUP.
3456 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3457   (save-excursion
3458     (set-buffer gnus-group-buffer)
3459     (let ((buffer-read-only nil)
3460           visible)
3461       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3462         (if entry
3463             (gnus-dribble-enter 
3464              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3465                      ")"))))
3466       ;; Buffer may be narrowed.
3467       (save-restriction
3468         (widen)
3469         ;; Search a line to modify.  If the buffer is large, the search
3470         ;; takes long time.  In most cases, current point is on the line
3471         ;; we are looking for.  So, first of all, check current line. 
3472         (if (or (progn
3473                   (beginning-of-line)
3474                   (eq (get-text-property (point) 'gnus-group)
3475                       (intern group)))
3476                 (progn
3477                   (gnus-goto-char 
3478                    (text-property-any 
3479                     (point-min) (point-max) 'gnus-group (intern group)))))
3480             ;; GROUP is listed in current buffer. So, delete old line.
3481             (progn
3482               (setq visible t)
3483               (beginning-of-line)
3484               (delete-region (point) (progn (forward-line 1) (point))))
3485           ;; No such line in the buffer, find out where it's supposed to
3486           ;; go, and insert it there (or at the end of the buffer).
3487           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3488           (or visible-only
3489               (let ((entry 
3490                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3491                 (while (and entry
3492                             (car entry)
3493                             (not
3494                              (gnus-goto-char
3495                               (text-property-any
3496                                (point-min) (point-max) 
3497                                'gnus-group (intern (car (car entry)))))))
3498                   (setq entry (cdr entry)))
3499                 (or entry (goto-char (point-max)))))))
3500       (if (or visible (not visible-only))
3501           (gnus-group-insert-group-line-info group))
3502       (gnus-group-set-mode-line))))
3503
3504 (defun gnus-group-set-mode-line ()
3505   (if (memq 'group gnus-updated-mode-lines)
3506       (let* ((gformat (or gnus-group-mode-line-format-spec
3507                           (setq gnus-group-mode-line-format-spec
3508                                 (gnus-parse-format 
3509                                  gnus-group-mode-line-format 
3510                                  gnus-group-mode-line-format-alist))))
3511              (news-server (car (cdr gnus-select-method)))
3512              (news-method (car gnus-select-method))
3513              (max-len 60)
3514              (mode-string (eval gformat)))
3515         (setq mode-string (eval gformat))
3516         (if (> (length mode-string) max-len) 
3517             (setq mode-string (substring mode-string 0 (- max-len 4))))
3518         (setq mode-line-buffer-identification mode-string)
3519         (set-buffer-modified-p t))))
3520
3521 (defun gnus-group-group-name ()
3522   "Get the name of the newsgroup on the current line."
3523   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3524     (and group (symbol-name group))))
3525
3526 (defun gnus-group-group-level ()
3527   "Get the level of the newsgroup on the current line."
3528   (get-text-property (gnus-point-at-bol) 'gnus-level))
3529
3530 (defun gnus-group-search-forward (&optional backward all level first-too)
3531   "Find the next newsgroup with unread articles.
3532 If BACKWARD is non-nil, find the previous newsgroup instead.
3533 If ALL is non-nil, just find any newsgroup.
3534 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3535 group exists.
3536 If FIRST-TOO, the current line is also eligible as a target."
3537   (let ((way (if backward -1 1))
3538         (low 10)
3539         (beg (point))
3540         pos found)
3541     (if (and backward (progn (beginning-of-line)) (bobp))
3542         nil
3543       (or first-too (forward-line way))
3544       (while (and 
3545               (not (eobp))
3546               (not (setq 
3547                     found 
3548                     (and (or all
3549                              (and
3550                               (let ((unread 
3551                                      (get-text-property (point) 'gnus-unread)))
3552                                 (or (eq unread t) (and unread (> unread 0))))
3553                               (let ((lev (get-text-property
3554                                           (point) 'gnus-level)))
3555                                 (and lev (<= (get-text-property 
3556                                               (point) 'gnus-level)
3557                                              gnus-level-subscribed)))))
3558                          (or (not level)
3559                              (let ((lev (get-text-property (point) 'gnus-level)))
3560                                (if (and lev (<= lev level))
3561                                    t
3562                                  (if (< lev low)
3563                                      (progn
3564                                        (setq low lev)
3565                                        (setq pos (point))))
3566                                  nil))))))
3567               (zerop (forward-line way)))))
3568     (if found 
3569         (progn (gnus-group-position-cursor) t)
3570       (if pos (goto-char pos) (goto-char beg))
3571       nil)))
3572
3573 ;;; Gnus group mode commands
3574
3575 ;; Group marking.
3576
3577 (defun gnus-group-mark-group (n &optional unmark)
3578   "Mark the current group."
3579   (interactive "p")
3580   (let ((buffer-read-only nil)
3581         group)
3582     (while 
3583         (and (> n 0) 
3584              (setq group (gnus-group-group-name))
3585              (progn
3586                (beginning-of-line)
3587                (forward-char 2)
3588                (delete-char 1)
3589                (if unmark
3590                    (progn
3591                      (insert " ")
3592                      (setq gnus-group-marked (delete group gnus-group-marked)))
3593                  (insert "#")
3594                  (setq gnus-group-marked
3595                        (cons group (delete group gnus-group-marked))))
3596                t)
3597              (zerop (gnus-group-next-group 1)))
3598       (setq n (1- n)))
3599     (gnus-summary-position-cursor)
3600     n))
3601
3602 (defun gnus-group-unmark-group (n)
3603   "Remove the mark from the current group."
3604   (interactive "p")
3605   (gnus-group-mark-group n 'unmark))
3606
3607 (defun gnus-group-mark-region (unmark beg end)
3608   "Mark all groups between point and mark.
3609 If UNMARK, remove the mark instead."
3610   (interactive "P\nr")
3611   (let ((num (count-lines beg end)))
3612     (save-excursion
3613       (goto-char beg)
3614       (- num (gnus-group-mark-group num unmark)))))
3615
3616 (defun gnus-group-remove-mark (group)
3617   (and (gnus-group-goto-group group)
3618        (save-excursion
3619          (gnus-group-mark-group 1 'unmark))))
3620
3621 ;; Return a list of groups to work on.  Take into consideration N (the
3622 ;; prefix) and the list of marked groups.
3623 (defun gnus-group-process-prefix (n)
3624   (cond (n
3625          (setq n (prefix-numeric-value n))
3626          ;; There is a prefix, so we return a list of the N next
3627          ;; groups. 
3628          (let ((way (if (< n 0) -1 1))
3629                (n (abs n))
3630                group groups)
3631            (save-excursion
3632              (while (and (> n 0)
3633                          (setq group (gnus-group-group-name)))
3634                (setq groups (cons group groups))
3635                (setq n (1- n))
3636                (forward-line way)))
3637            (nreverse groups)))
3638         (gnus-group-marked
3639          ;; No prefix, but a list of marked articles.
3640          (reverse gnus-group-marked))
3641         (t
3642          ;; Neither marked articles or a prefix, so we return the
3643          ;; current group.
3644          (let ((group (gnus-group-group-name)))
3645            (and group (list group))))))
3646
3647 ;; Selecting groups.
3648
3649 (defun gnus-group-read-group (all &optional no-article group)
3650   "Read news in this newsgroup.
3651 If argument ALL is non-nil, already read articles become readable.
3652 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3653   (interactive "P")
3654   (let ((group (or group (gnus-group-group-name)))
3655         number active marked entry)
3656     (or group (error "No group on current line"))
3657     (setq marked 
3658           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3659     ;; This group might be a dead group. In that case we have to get
3660     ;; the number of unread articles from `gnus-active-hashtb'.
3661     (if entry
3662         (setq number (car entry))
3663       (if (setq active (gnus-gethash group gnus-active-hashtb))
3664           (setq number (- (1+ (cdr active)) (car active)))))
3665     (gnus-summary-read-group 
3666      group (or all (and (numberp number) 
3667                         (zerop (+ number (length (cdr (assq 'tick marked)))
3668                                   (length (cdr (assq 'dormant marked)))))))
3669      no-article)))
3670
3671 (defun gnus-group-select-group (all)
3672   "Select this newsgroup.
3673 No article is selected automatically.
3674 If argument ALL is non-nil, already read articles become readable."
3675   (interactive "P")
3676   (gnus-group-read-group all t))
3677
3678 ;; Enter a group that is not in the group buffer. Non-nil is returned
3679 ;; if selection was successful.
3680 (defun gnus-group-read-ephemeral-group 
3681   (group method &optional activate quit-config)
3682   (let ((group (if (gnus-group-foreign-p group) group
3683                  (gnus-group-prefixed-name group method))))
3684     (gnus-sethash 
3685      group
3686      (list t nil (list group gnus-level-default-subscribed nil nil 
3687                        (append method
3688                                (list
3689                                 (list 'quit-config 
3690                                       (if quit-config quit-config
3691                                         (cons (current-buffer) 'summary)))))))
3692      gnus-newsrc-hashtb)
3693     (set-buffer gnus-group-buffer)
3694     (or (gnus-server-opened method)
3695         (gnus-open-server method)
3696         (error "Unable to contact server: %s" (gnus-status-message method)))
3697     (if activate (gnus-request-group group))
3698     (condition-case ()
3699         (gnus-group-read-group t t group)
3700       (error nil)
3701       (quit nil))
3702     (not (equal major-mode 'gnus-group-mode))))
3703   
3704 (defun gnus-group-jump-to-group (group)
3705   "Jump to newsgroup GROUP."
3706   (interactive 
3707    (list (completing-read 
3708           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3709
3710   (if (equal group "")
3711       (error "Empty group name"))
3712
3713   (let ((b (text-property-any 
3714             (point-min) (point-max) 'gnus-group (intern group))))
3715     (if b
3716         ;; Either go to the line in the group buffer...
3717         (goto-char b)
3718       ;; ... or insert the line.
3719       (or
3720        (gnus-gethash group gnus-active-hashtb)
3721        (gnus-activate-newsgroup group)
3722        (error "%s error: %s" group (gnus-status-message group)))
3723
3724       (gnus-group-update-group group)
3725       (goto-char (text-property-any 
3726                   (point-min) (point-max) 'gnus-group (intern group)))))
3727   ;; Adjust cursor point.
3728   (gnus-group-position-cursor))
3729
3730 (defun gnus-group-goto-group (group)
3731   "Goto to newsgroup GROUP."
3732   (let ((b (text-property-any (point-min) (point-max) 
3733                               'gnus-group (intern group))))
3734     (and b (goto-char b))))
3735
3736 (defun gnus-group-next-group (n)
3737   "Go to next N'th newsgroup.
3738 If N is negative, search backward instead.
3739 Returns the difference between N and the number of skips actually
3740 done."
3741   (interactive "p")
3742   (gnus-group-next-unread-group n t))
3743
3744 (defun gnus-group-next-unread-group (n &optional all level)
3745   "Go to next N'th unread newsgroup.
3746 If N is negative, search backward instead.
3747 If ALL is non-nil, choose any newsgroup, unread or not.
3748 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3749 such group can be found, the next group with a level higher than
3750 LEVEL.
3751 Returns the difference between N and the number of skips actually
3752 made."
3753   (interactive "p")
3754   (let ((backward (< n 0))
3755         (n (abs n)))
3756     (while (and (> n 0)
3757                 (gnus-group-search-forward 
3758                  backward (or (not gnus-group-goto-unread) all) level))
3759       (setq n (1- n)))
3760     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3761                                (if level " on this level or higher" "")))
3762     n))
3763
3764 (defun gnus-group-prev-group (n)
3765   "Go to previous N'th newsgroup.
3766 Returns the difference between N and the number of skips actually
3767 done."
3768   (interactive "p")
3769   (gnus-group-next-unread-group (- n) t))
3770
3771 (defun gnus-group-prev-unread-group (n)
3772   "Go to previous N'th unread newsgroup.
3773 Returns the difference between N and the number of skips actually
3774 done."  
3775   (interactive "p")
3776   (gnus-group-next-unread-group (- n)))
3777
3778 (defun gnus-group-next-unread-group-same-level (n)
3779   "Go to next N'th unread newsgroup on the same level.
3780 If N is negative, search backward instead.
3781 Returns the difference between N and the number of skips actually
3782 done."
3783   (interactive "p")
3784   (gnus-group-next-unread-group n t (gnus-group-group-level))
3785   (gnus-group-position-cursor))
3786
3787 (defun gnus-group-prev-unread-group-same-level (n)
3788   "Go to next N'th unread newsgroup on the same level.
3789 Returns the difference between N and the number of skips actually
3790 done."
3791   (interactive "p")
3792   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3793   (gnus-group-position-cursor))
3794
3795 (defun gnus-group-best-unread-group (&optional exclude-group)
3796   "Go to the group with the highest level.
3797 If EXCLUDE-GROUP, do not go to that group."
3798   (interactive)
3799   (goto-char (point-min))
3800   (let ((best 100000)
3801         unread best-point)
3802     (while (setq unread (get-text-property (point) 'gnus-unread))
3803       (if (and (numberp unread) (> unread 0))
3804           (progn
3805             (if (and (< (get-text-property (point) 'gnus-level) best)
3806                      (or (not exclude-group)
3807                          (not (equal exclude-group (gnus-group-group-name)))))
3808                 (progn 
3809                   (setq best (get-text-property (point) 'gnus-level))
3810                   (setq best-point (point))))))
3811       (forward-line 1))
3812     (if best-point (goto-char best-point))
3813     (gnus-summary-position-cursor)
3814     (and best-point (gnus-group-group-name))))
3815
3816 (defun gnus-group-first-unread-group ()
3817   "Go to the first group with unread articles."
3818   (interactive)
3819   (goto-char (point-min))
3820   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3821       (gnus-group-next-unread-group 1))
3822   (gnus-group-position-cursor))
3823
3824 (defun gnus-group-enter-server-mode ()
3825   "Jump to the server buffer."
3826   (interactive)
3827   (gnus-server-setup-buffer)
3828   (gnus-configure-windows 'server)
3829   (gnus-server-prepare))
3830
3831 (defun gnus-group-make-group (name method &optional address)
3832   "Add a new newsgroup.
3833 The user will be prompted for a NAME, for a select METHOD, and an
3834 ADDRESS."
3835   (interactive
3836    (cons 
3837     (read-string "Group name: ")
3838     (let ((method
3839            (completing-read 
3840             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3841             nil t)))
3842       (if (assoc method gnus-valid-select-methods)
3843           (list method
3844                 (if (memq 'prompt-address
3845                           (assoc method gnus-valid-select-methods))
3846                     (read-string "Address: ")
3847                   ""))
3848         (list method nil)))))
3849   
3850   (let* ((meth (if address (list (intern method) address) method))
3851          (nname (gnus-group-prefixed-name name meth))
3852          info)
3853     (and (gnus-gethash nname gnus-newsrc-hashtb)
3854          (error "Group %s already exists" nname))
3855     (gnus-group-change-level 
3856      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3857      gnus-level-default-subscribed gnus-level-killed 
3858      (and (gnus-group-group-name)
3859           (gnus-gethash (gnus-group-group-name)
3860                         gnus-newsrc-hashtb))
3861      t)
3862     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3863     (gnus-dribble-enter 
3864      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3865     (gnus-group-insert-group-line-info nname)
3866
3867     (and (gnus-check-backend-function 'request-create-group nname)
3868          (gnus-request-create-group nname))))
3869
3870 (defun gnus-group-edit-group (group &optional part)
3871   "Edit the group on the current line."
3872   (interactive (list (gnus-group-group-name)))
3873   (let ((done-func '(lambda () 
3874                       "Exit editing mode and update the information."
3875                       (interactive)
3876                       (gnus-group-edit-group-done 'part 'group)))
3877         (part (or part 'info))
3878         (winconf (current-window-configuration))
3879         info)
3880     (or group (error "No group on current line"))
3881     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3882         (error "Killed group; can't be edited"))
3883     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3884     (gnus-configure-windows 'edit-group)
3885     (gnus-add-current-to-buffer-list)
3886     (emacs-lisp-mode)
3887     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3888     (use-local-map (copy-keymap emacs-lisp-mode-map))
3889     (local-set-key "\C-c\C-c" done-func)
3890     (make-local-variable 'gnus-prev-winconf)
3891     (setq gnus-prev-winconf winconf)
3892     ;; We modify the func to let it know what part it is editing.
3893     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3894     (setcar (cdr (cdr (nth 4 done-func))) group)
3895     (erase-buffer)
3896     (insert
3897      (cond 
3898       ((eq part 'method)
3899        ";; Type `C-c C-c' after editing the select method.\n\n")
3900       ((eq part 'params)
3901        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3902       ((eq part 'info)
3903        ";; Type `C-c C-c' after editing the group info.\n\n")))
3904     (let ((cinfo (gnus-copy-sequence info))
3905           marked)
3906       (if (not (setq marked (nth 3 cinfo)))
3907           ()
3908         (while marked
3909           (or (eq 'score (car (car marked)))
3910               (eq 'bookmark (car (car marked)))
3911               (eq 'killed (car (car marked)))
3912               (not (numberp (car (cdr (car marked)))))
3913               (setcdr (car marked) 
3914                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3915           (setq marked (cdr marked))))
3916       (insert 
3917        (pp-to-string
3918         (cond ((eq part 'method)
3919                (or (nth 4 info) "native"))
3920               ((eq part 'params)
3921                (nth 5 info))
3922               (t
3923                cinfo)))
3924        "\n"))))
3925
3926 (defun gnus-group-edit-group-method (group)
3927   "Edit the select method of GROUP."
3928   (interactive (list (gnus-group-group-name)))
3929   (gnus-group-edit-group group 'method))
3930
3931 (defun gnus-group-edit-group-parameters (group)
3932   "Edit the group parameters of GROUP."
3933   (interactive (list (gnus-group-group-name)))
3934   (gnus-group-edit-group group 'params))
3935
3936 (defun gnus-group-edit-group-done (part group)
3937   "Get info from buffer, update variables and jump to the group buffer."
3938   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3939   (goto-char (point-min))
3940   (let ((form (read (current-buffer)))
3941         (winconf gnus-prev-winconf))
3942     (if (eq part 'info) 
3943         (gnus-group-set-info form)
3944       (gnus-group-set-info form group part))
3945     (kill-buffer (current-buffer))
3946     (and winconf (set-window-configuration winconf))
3947     (set-buffer gnus-group-buffer)
3948     (gnus-group-update-group (gnus-group-group-name))
3949     (gnus-group-position-cursor)))
3950
3951 (defun gnus-group-make-help-group ()
3952   "Create the (ding) Gnus documentation group."
3953   (interactive)
3954   (let ((path load-path)
3955         name)
3956     (and (gnus-gethash (setq name (gnus-group-prefixed-name
3957                                    "gnus-help" '(nndoc "gnus-help")))
3958                        gnus-newsrc-hashtb)
3959          (error "Documentation group already exists"))
3960     (while (and path
3961                 (not (file-exists-p (concat (file-name-as-directory (car path))
3962                                             "doc.txt"))))
3963       (setq path (cdr path)))
3964     (or path (error "Couldn't find doc group"))
3965     (gnus-group-make-group 
3966      (gnus-group-real-name name)
3967      (list 'nndoc name
3968            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
3969            (list 'nndoc-article-type 'mbox))))
3970   (gnus-group-position-cursor))
3971
3972 (defun gnus-group-make-doc-group (file type)
3973   "Create a group that uses a single file as the source."
3974   (interactive 
3975    (list (read-file-name "File name: ") 
3976          (let ((err "")
3977                found char)
3978            (while (not found)
3979              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
3980              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
3981                                ((= char ?b) 'babyl)
3982                                ((= char ?d) 'digest)
3983                                (t (setq err "%c unknown. " char)
3984                                   nil))))
3985            found)))
3986   (let* ((file (expand-file-name file))
3987          (name (gnus-generate-new-group-name
3988                 (gnus-group-prefixed-name
3989                  (file-name-nondirectory file) '(nndoc "")))))
3990     (gnus-group-make-group 
3991      (gnus-group-real-name name)
3992      (list 'nndoc name
3993            (list 'nndoc-address file)
3994            (list 'nndoc-article-type type)))))
3995
3996 (defun gnus-group-make-archive-group ()
3997   "Create the (ding) Gnus archive group."
3998   (interactive)
3999   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4000                      gnus-newsrc-hashtb)
4001        (error "Archive group already exists"))
4002   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4003   (gnus-group-position-cursor))
4004
4005 (defun gnus-group-make-directory-group (dir)
4006   "Create an nndir group.
4007 The user will be prompted for a directory. The contents of this
4008 directory will be used as a newsgroup. The directory should contain
4009 mail messages or news articles in files that have numeric names."
4010   (interactive
4011    (list (read-file-name "Create group from directory: ")))
4012   (or (file-exists-p dir) (error "No such directory"))
4013   (or (file-directory-p dir) (error "Not a directory"))
4014   (gnus-group-make-group dir "nndir" dir)
4015   (gnus-group-position-cursor))
4016
4017 (defun gnus-group-make-kiboze-group (group address scores)
4018   "Create an nnkiboze group.
4019 The user will be prompted for a name, a regexp to match groups, and
4020 score file entries for articles to include in the group."
4021   (interactive
4022    (list
4023     (read-string "nnkiboze group name: ")
4024     (read-string "Source groups (regexp): ")
4025     (let ((headers (mapcar (lambda (group) (list group))
4026                            '("subject" "from" "number" "date" "message-id"
4027                              "references" "chars" "lines" "xref")))
4028           scores header regexp regexps)
4029       (while (not (equal "" (setq header (completing-read 
4030                                           "Match on header: " headers nil t))))
4031         (setq regexps nil)
4032         (while (not (equal "" (setq regexp (read-string 
4033                                             (format "Match on %s (string): "
4034                                                     header)))))
4035           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4036         (setq scores (cons (cons header regexps) scores)))
4037       scores)))
4038   (gnus-group-make-group group "nnkiboze" address)
4039   (save-excursion
4040     (gnus-set-work-buffer)
4041     (let (emacs-lisp-mode-hook)
4042       (pp scores (current-buffer)))
4043     (write-region (point-min) (point-max) 
4044                   (concat (or gnus-kill-files-directory "~/News")
4045                           "nnkiboze:" group "." gnus-score-file-suffix)))
4046   (gnus-group-position-cursor))
4047
4048 (defun gnus-group-add-to-virtual (n vgroup)
4049   "Add the current group to a virtual group."
4050   (interactive
4051    (list current-prefix-arg
4052          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4053                           "nnvirtual:")))
4054   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4055       (error "%s is not an nnvirtual group" vgroup))
4056   (let* ((groups (gnus-group-process-prefix n))
4057          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4058     (setcar (cdr method)
4059             (concat 
4060              (nth 1 method) "\\|"
4061              (mapconcat 
4062               (lambda (s) 
4063                 (gnus-group-remove-mark s)
4064                 (concat "\\(^" (regexp-quote s) "$\\)"))
4065               groups "\\|"))))
4066   (gnus-group-position-cursor))
4067
4068 (defun gnus-group-make-empty-virtual (group)
4069   "Create a new, fresh, empty virtual group."
4070   (interactive "sCreate new, empty virtual group: ")
4071   (let* ((method (list 'nnvirtual "^$"))
4072          (pgroup (gnus-group-prefixed-name group method)))
4073     ;; Check whether it exists already.
4074     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4075          (error "Group %s already exists." pgroup))
4076     ;; Subscribe the new group after the group on the current line.
4077     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4078     (gnus-group-update-group pgroup)
4079     (forward-line -1)
4080     (gnus-group-position-cursor)))
4081
4082 (defun gnus-group-enter-directory (dir)
4083   "Enter an ephemeral nneething group."
4084   (interactive "DDirectory to read: ")
4085   (let* ((method (list 'nneething dir))
4086          (leaf (gnus-group-prefixed-name
4087                 (file-name-nondirectory (directory-file-name dir))
4088                 method))
4089          (name (gnus-generate-new-group-name leaf)))
4090     (let ((nneething-read-only t))
4091       (or (gnus-group-read-ephemeral-group 
4092            name method t
4093            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4094                                       'summary 'group)))
4095           (error "Couldn't enter %s" dir)))))
4096
4097 ;; Group sorting commands
4098 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4099
4100 (defun gnus-group-sort-groups ()
4101   "Sort the group buffer using `gnus-group-sort-function'."
4102   (interactive)
4103   (setq gnus-newsrc-alist 
4104         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4105   (gnus-make-hashtable-from-newsrc-alist)
4106   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4107
4108 (defun gnus-group-sort-by-alphabet (info1 info2)
4109   (string< (car info1) (car info2)))
4110
4111 (defun gnus-group-sort-by-unread (info1 info2)
4112   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4113         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4114     (< (or (and (numberp n1) n1) 0)
4115        (or (and (numberp n2) n2) 0))))
4116
4117 (defun gnus-group-sort-by-level (info1 info2)
4118   (< (nth 1 info1) (nth 1 info2)))
4119
4120 ;; Group catching up.
4121
4122 (defun gnus-group-catchup-current (n &optional all)
4123   "Mark all articles not marked as unread in current newsgroup as read.
4124 If prefix argument N is numeric, the ARG next newsgroups will be
4125 caught up. If ALL is non-nil, marked articles will also be marked as
4126 read. Cross references (Xref: header) of articles are ignored.
4127 The difference between N and actual number of newsgroups that were
4128 caught up is returned."
4129   (interactive "P")
4130   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4131                gnus-expert-user
4132                (gnus-y-or-n-p
4133                 (if all
4134                     "Do you really want to mark all articles as read? "
4135                   "Mark all unread articles as read? "))))
4136       n
4137     (let ((groups (gnus-group-process-prefix n))
4138           (ret 0))
4139       (while groups
4140         ;; Virtual groups have to be given special treatment. 
4141         (let ((method (gnus-find-method-for-group (car groups))))
4142           (if (eq 'nnvirtual (car method))
4143               (nnvirtual-catchup-group
4144                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4145         (gnus-group-remove-mark (car groups))
4146         (if (prog1
4147                 (gnus-group-goto-group (car groups))
4148               (gnus-group-catchup (car groups) all))
4149             (gnus-group-update-group-line)
4150           (setq ret (1+ ret)))
4151         (setq groups (cdr groups)))
4152       (gnus-group-next-unread-group 1)
4153       ret)))
4154
4155 (defun gnus-group-catchup-current-all (n)
4156   "Mark all articles in current newsgroup as read.
4157 Cross references (Xref: header) of articles are ignored."
4158   (interactive "P")
4159   (gnus-group-catchup-current n 'all))
4160
4161 (defun gnus-group-catchup (group &optional all)
4162   "Mark all articles in GROUP as read.
4163 If ALL is non-nil, all articles are marked as read.
4164 The return value is the number of articles that were marked as read,
4165 or nil if no action could be taken."
4166   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4167          (num (car entry))
4168          (marked (nth 3 (nth 2 entry))))
4169     (if (not (numberp (car entry)))
4170         (gnus-message 1 "Can't catch up; non-active group")
4171       ;; Do the updating only if the newsgroup isn't killed.
4172       (if (not entry)
4173           ()
4174         (gnus-update-read-articles 
4175          group (and (not all) (append (cdr (assq 'tick marked))
4176                                       (cdr (assq 'dormant marked))))
4177          nil (and (not all) (cdr (assq 'tick marked))))
4178         (and all marked
4179              (setcar (nthcdr 3 (nth 2 entry)) 
4180                      (delq (assq 'dormant marked) 
4181                            (nth 3 (nth 2 entry)))))))
4182     num))
4183
4184 (defun gnus-group-expire-articles (n)
4185   "Expire all expirable articles in the current newsgroup."
4186   (interactive "P")
4187   (let ((groups (gnus-group-process-prefix n))
4188         group)
4189     (or groups (error "No groups to expire"))
4190     (while groups
4191       (setq group (car groups)
4192             groups (cdr groups))
4193       (gnus-group-remove-mark group)
4194       (if (not (gnus-check-backend-function 'request-expire-articles group))
4195           ()
4196         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4197                (expirable (if (memq 'total-expire (nth 5 info))
4198                               (cons nil (gnus-list-of-read-articles group))
4199                             (assq 'expire (nth 3 info)))))
4200           (and expirable 
4201                (setcdr expirable
4202                        (gnus-request-expire-articles 
4203                         (cdr expirable) group))))))))
4204
4205 (defun gnus-group-expire-all-groups ()
4206   "Expire all expirable articles in all newsgroups."
4207   (interactive)
4208   (gnus-message 5 "Expiring...")
4209   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4210                                    (cdr gnus-newsrc-alist))))
4211     (gnus-group-expire-articles nil))
4212   (gnus-message 5 "Expiring...done"))
4213
4214 (defun gnus-group-set-current-level (n level)
4215   "Set the level of the next N groups to LEVEL."
4216   (interactive "P\nnLevel: ")
4217   (or (and (>= level 1) (<= level gnus-level-killed))
4218       (error "Illegal level: %d" level))
4219   (let ((groups (gnus-group-process-prefix n))
4220         group)
4221     (while groups
4222       (setq group (car groups)
4223             groups (cdr groups))
4224       (gnus-group-remove-mark group)
4225       (gnus-message 6 "Changed level of %s from %d to %d" 
4226                     group (gnus-group-group-level) level)
4227       (gnus-group-change-level group level
4228                                (gnus-group-group-level))
4229       (gnus-group-update-group-line)))
4230   (gnus-group-position-cursor))
4231
4232 (defun gnus-group-unsubscribe-current-group (n)
4233   "Toggle subscription of the current group.
4234 If given numerical prefix, toggle the N next groups."
4235   (interactive "P")
4236   (let ((groups (gnus-group-process-prefix n))
4237         group)
4238     (while groups
4239       (setq group (car groups)
4240             groups (cdr groups))
4241       (gnus-group-remove-mark group)
4242       (gnus-group-unsubscribe-group
4243        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4244                  gnus-level-default-unsubscribed
4245                gnus-level-default-subscribed))
4246       (gnus-group-update-group-line))
4247     (gnus-group-next-group 1)))
4248
4249 (defun gnus-group-unsubscribe-group (group &optional level)
4250   "Toggle subscribe from/to unsubscribe GROUP.
4251 New newsgroup is added to .newsrc automatically."
4252   (interactive
4253    (list (completing-read "Group: " gnus-active-hashtb nil 
4254                           gnus-have-read-active-file)))
4255   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4256     (cond (newsrc
4257            ;; Toggle subscription flag.
4258            (gnus-group-change-level 
4259             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4260                                            gnus-level-subscribed) 
4261                                        (1+ gnus-level-subscribed)
4262                                      gnus-level-default-subscribed)))
4263            (gnus-group-update-group group))
4264           ((and (stringp group)
4265                 (or (not gnus-have-read-active-file)
4266                     (gnus-gethash group gnus-active-hashtb)))
4267            ;; Add new newsgroup.
4268            (gnus-group-change-level 
4269             group 
4270             (if level level gnus-level-default-subscribed) 
4271             (or (and (member group gnus-zombie-list) 
4272                      gnus-level-zombie) 
4273                 gnus-level-killed)
4274             (and (gnus-group-group-name)
4275                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4276            (gnus-group-update-group group))
4277           (t (error "No such newsgroup: %s" group)))
4278     (gnus-group-position-cursor)))
4279
4280 (defun gnus-group-transpose-groups (n)
4281   "Move the current newsgroup up N places.
4282 If given a negative prefix, move down instead. The difference between
4283 N and the number of steps taken is returned." 
4284   (interactive "p")
4285   (or (gnus-group-group-name)
4286       (error "No group on current line"))
4287   (gnus-group-kill-group 1)
4288   (prog1
4289       (forward-line (- n))
4290     (gnus-group-yank-group)
4291     (gnus-group-position-cursor)))
4292
4293 (defun gnus-group-kill-all-zombies ()
4294   "Kill all zombie newsgroups."
4295   (interactive)
4296   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4297   (setq gnus-zombie-list nil)
4298   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4299   (goto-char (point-min))
4300   (gnus-group-position-cursor))
4301
4302 (defun gnus-group-kill-region (begin end)
4303   "Kill newsgroups in current region (excluding current point).
4304 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4305   (interactive "r")
4306   (let ((lines
4307          ;; Count lines.
4308          (save-excursion
4309            (count-lines
4310             (progn
4311               (goto-char begin)
4312               (beginning-of-line)
4313               (point))
4314             (progn
4315               (goto-char end)
4316               (beginning-of-line)
4317               (point))))))
4318     (goto-char begin)
4319     (beginning-of-line)                 ;Important when LINES < 1
4320     (gnus-group-kill-group lines)))
4321
4322 (defun gnus-group-kill-group (n)
4323   "The the next N groups.
4324 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4325 However, only groups that were alive can be yanked; already killed 
4326 groups or zombie groups can't be yanked.
4327 The return value is the name of the (last) group that was killed."
4328   (interactive "P")
4329   (let ((buffer-read-only nil)
4330         (groups (gnus-group-process-prefix n))
4331         group entry level)
4332     (while groups
4333       (setq group (car groups)
4334             groups (cdr groups))
4335       (gnus-group-remove-mark group)
4336       (setq level (gnus-group-group-level))
4337       (gnus-delete-line)
4338       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4339           (setq gnus-list-of-killed-groups 
4340                 (cons (cons (car entry) (nth 2 entry)) 
4341                       gnus-list-of-killed-groups)))
4342       (gnus-group-change-level 
4343        (if entry entry group) gnus-level-killed (if entry nil level)))
4344     (gnus-group-position-cursor)
4345     group))
4346
4347 (defun gnus-group-yank-group (&optional arg)
4348   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4349 inserting it before the current newsgroup.  The numeric ARG specifies
4350 how many newsgroups are to be yanked.  The name of the (last)
4351 newsgroup yanked is returned."
4352   (interactive "p")
4353   (if (not arg) (setq arg 1))
4354   (let (info group prev)
4355     (while (>= (setq arg (1- arg)) 0)
4356       (if (not (setq info (car gnus-list-of-killed-groups)))
4357           (error "No more newsgroups to yank"))
4358       (setq group (nth 2 info))
4359       ;; Find which newsgroup to insert this one before - search
4360       ;; backward until something suitable is found. If there are no
4361       ;; other newsgroups in this buffer, just make this newsgroup the
4362       ;; first newsgroup.
4363       (setq prev (gnus-group-group-name))
4364       (gnus-group-change-level 
4365        info (nth 2 info) gnus-level-killed 
4366        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4367        t)
4368       (gnus-group-insert-group-line-info (nth 1 info))
4369       (setq gnus-list-of-killed-groups 
4370             (cdr gnus-list-of-killed-groups)))
4371     (forward-line -1)
4372     (gnus-group-position-cursor)
4373     group))
4374       
4375 (defun gnus-group-list-all-groups (arg)
4376   "List all newsgroups with level ARG or lower.
4377 Default is gnus-level-unsubscribed, which lists all subscribed and most
4378 unsubscribed groups."
4379   (interactive "P")
4380   (setq arg (or arg gnus-level-unsubscribed))
4381   (gnus-group-list-groups arg t))
4382
4383 (defun gnus-group-list-killed ()
4384   "List all killed newsgroups in the group buffer."
4385   (interactive)
4386   (if (not gnus-killed-list)
4387       (gnus-message 6 "No killed groups")
4388     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4389     (goto-char (point-min)))
4390   (gnus-group-position-cursor))
4391
4392 (defun gnus-group-list-zombies ()
4393   "List all zombie newsgroups in the group buffer."
4394   (interactive)
4395   (if (not gnus-zombie-list)
4396       (gnus-message 6 "No zombie groups")
4397     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4398     (goto-char (point-min)))
4399   (gnus-group-position-cursor))
4400
4401 (defun gnus-group-get-new-news (&optional arg)
4402   "Get newly arrived articles.
4403 If ARG is non-nil, it should be a number between one and nine to
4404 specify which levels you are interested in re-scanning."
4405   (interactive "P")
4406   (run-hooks 'gnus-get-new-news-hook)
4407   (let ((level arg))
4408     (if gnus-group-use-permanent-levels
4409         (if level
4410             (setq gnus-group-default-list-level level)
4411           (setq level (or gnus-group-default-list-level 
4412                           gnus-level-subscribed))))
4413     (if (and gnus-read-active-file (not level))
4414         (progn
4415           (gnus-read-active-file)
4416           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4417       (let ((gnus-read-active-file nil)
4418             (gnus-have-read-active-file (not arg)))
4419         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4420     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4421                                 gnus-group-default-list-level
4422                                 gnus-level-subscribed)
4423                             gnus-have-all-newsgroups)))
4424
4425 (defun gnus-group-get-new-news-this-group (n)
4426   "Check for newly arrived news in the current group (and the N-1 next groups).
4427 The difference between N and the number of newsgroup checked is returned.
4428 If N is negative, this group and the N-1 previous groups will be checked."
4429   (interactive "P")
4430   (let* ((groups (gnus-group-process-prefix n))
4431          (ret (if (numberp n) (- n (length groups)) 0))
4432          group)
4433     (while groups
4434       (setq group (car groups)
4435             groups (cdr groups))
4436       (gnus-group-remove-mark group)
4437       (or (gnus-get-new-news-in-group group)
4438           (progn 
4439             (ding) 
4440             (message "%s error: %s" group (gnus-status-message group))
4441             (sit-for 2))))
4442     ;; !!! I don't know why the buffer scrolls forward when updating
4443     ;; the first line in the group buffer, but it does. So we set the
4444     ;; window start forcibly.
4445 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4446     (gnus-group-next-unread-group 1 t)
4447     (gnus-summary-position-cursor)
4448     ret))
4449
4450 (defun gnus-get-new-news-in-group (group)
4451   (and group 
4452        (gnus-activate-newsgroup group)
4453        (progn
4454          (gnus-get-unread-articles-in-group 
4455           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4456           (gnus-gethash group gnus-active-hashtb))
4457          (gnus-group-update-group-line)
4458          t)))
4459
4460 (defun gnus-group-fetch-faq (group)
4461   "Fetch the FAQ for the current group."
4462   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4463   (or group (error "No group name given"))
4464   (let ((file (concat gnus-group-faq-directory group))) 
4465     (if (not (file-exists-p file))
4466         (error "No such file: %s" file)
4467       (find-file file))))
4468   
4469 (defun gnus-group-describe-group (force &optional group)
4470   "Display a description of the current newsgroup."
4471   (interactive (list current-prefix-arg (gnus-group-group-name)))
4472   (and force (setq gnus-description-hashtb nil))
4473   (let ((method (gnus-find-method-for-group group))
4474         desc)
4475     (or group (error "No group name given"))
4476     (and (or (and gnus-description-hashtb
4477                   ;; We check whether this group's method has been
4478                   ;; queried for a description file.  
4479                   (gnus-gethash 
4480                    (gnus-group-prefixed-name "" method) 
4481                    gnus-description-hashtb))
4482              (setq desc (gnus-group-get-description group))
4483              (gnus-read-descriptions-file method))
4484          (message
4485           (or desc (gnus-gethash group gnus-description-hashtb)
4486               "No description available")))))
4487
4488 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4489 (defun gnus-group-describe-all-groups (force)
4490   "Pop up a buffer with descriptions of all newsgroups."
4491   (interactive "P")
4492   (and force (setq gnus-description-hashtb nil))
4493   (if (not (or gnus-description-hashtb
4494                (gnus-read-all-descriptions-files)))
4495       (error "Couldn't request descriptions file"))
4496   (let ((buffer-read-only nil)
4497         b)
4498     (erase-buffer)
4499     (mapatoms
4500      (lambda (group)
4501        (setq b (point))
4502        (insert (format "      *: %-20s %s\n" (symbol-name group)
4503                        (symbol-value group)))
4504        (add-text-properties 
4505         b (1+ b) (list 'gnus-group group
4506                        'gnus-unread t 'gnus-marked nil
4507                        'gnus-level (1+ gnus-level-subscribed))))
4508      gnus-description-hashtb)
4509     (goto-char (point-min))
4510     (gnus-group-position-cursor)))
4511
4512 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4513 (defun gnus-group-apropos (regexp &optional search-description)
4514   "List all newsgroups that have names that match a regexp."
4515   (interactive "sGnus apropos (regexp): ")
4516   (let ((prev "")
4517         (obuf (current-buffer))
4518         groups des)
4519     ;; Go through all newsgroups that are known to Gnus.
4520     (mapatoms 
4521      (lambda (group)
4522        (and (string-match regexp (symbol-name group))
4523             (setq groups (cons (symbol-name group) groups))))
4524      gnus-active-hashtb)
4525     ;; Go through all descriptions that are known to Gnus. 
4526     (if search-description
4527         (mapatoms 
4528          (lambda (group)
4529            (and (string-match regexp (symbol-value group))
4530                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4531                 (setq groups (cons (symbol-name group) groups))))
4532          gnus-description-hashtb))
4533     (if (not groups)
4534         (gnus-message 3 "No groups matched \"%s\"." regexp)
4535       ;; Print out all the groups.
4536       (save-excursion
4537         (pop-to-buffer "*Gnus Help*")
4538         (buffer-disable-undo (current-buffer))
4539         (erase-buffer)
4540         (setq groups (sort groups 'string<))
4541         (while groups
4542           ;; Groups may be entered twice into the list of groups.
4543           (if (not (string= (car groups) prev))
4544               (progn
4545                 (insert (setq prev (car groups)) "\n")
4546                 (if (and gnus-description-hashtb
4547                          (setq des (gnus-gethash (car groups) 
4548                                                  gnus-description-hashtb)))
4549                     (insert "  " des "\n"))))
4550           (setq groups (cdr groups)))
4551         (goto-char (point-min))))
4552     (pop-to-buffer obuf)))
4553
4554 (defun gnus-group-description-apropos (regexp)
4555   "List all newsgroups that have names or descriptions that match a regexp."
4556   (interactive "sGnus description apropos (regexp): ")
4557   (if (not (or gnus-description-hashtb
4558                (gnus-read-all-descriptions-files)))
4559       (error "Couldn't request descriptions file"))
4560   (gnus-group-apropos regexp t))
4561
4562 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4563 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4564   "List all groups with unread articles that match REGEXP.
4565 If the prefix LEVEL is non-nil, it should be a number that says which
4566 level to cut off listing groups. 
4567 If ALL, also list groups with no unread articles.
4568 If LOWEST, don't list groups with level lower than LOWEST."
4569   (interactive "P\nsList newsgroups matching: ")
4570   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4571                            all (or lowest 1) regexp)
4572   (goto-char (point-min))
4573   (gnus-group-position-cursor))
4574
4575 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4576   "List all groups that match REGEXP.
4577 If the prefix LEVEL is non-nil, it should be a number that says which
4578 level to cut off listing groups. 
4579 If LOWEST, don't list groups with level lower than LOWEST."
4580   (interactive "P\nsList newsgroups matching: ")
4581   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4582
4583 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4584 (defun gnus-group-save-newsrc ()
4585   "Save the Gnus startup files."
4586   (interactive)
4587   (gnus-save-newsrc-file))
4588
4589 (defun gnus-group-restart (&optional arg)
4590   "Force Gnus to read the .newsrc file."
4591   (interactive "P")
4592   (gnus-save-newsrc-file)
4593   (gnus-setup-news 'force)
4594   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4595
4596 (defun gnus-group-read-init-file ()
4597   "Read the Gnus elisp init file."
4598   (interactive)
4599   (gnus-read-init-file))
4600
4601 (defun gnus-group-check-bogus-groups (silent)
4602   "Check bogus newsgroups.
4603 If given a prefix, don't ask for confirmation before removing a bogus
4604 group."
4605   (interactive "P")
4606   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4607   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4608
4609 (defun gnus-group-edit-global-kill (article &optional group)
4610   "Edit the global kill file.
4611 If GROUP, edit that local kill file instead."
4612   (interactive "P")
4613   (setq gnus-current-kill-article article)
4614   (gnus-kill-file-edit-file group)
4615   (gnus-message 6
4616    (substitute-command-keys
4617     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4618
4619 (defun gnus-group-edit-local-kill (article group)
4620   "Edit a local kill file."
4621   (interactive (list nil (gnus-group-group-name)))
4622   (gnus-group-edit-global-kill article group))
4623
4624 (defun gnus-group-force-update ()
4625   "Update `.newsrc' file."
4626   (interactive)
4627   (gnus-save-newsrc-file))
4628
4629 (defun gnus-group-suspend ()
4630   "Suspend the current Gnus session.
4631 In fact, cleanup buffers except for group mode buffer.
4632 The hook gnus-suspend-gnus-hook is called before actually suspending."
4633   (interactive)
4634   (run-hooks 'gnus-suspend-gnus-hook)
4635   ;; Kill Gnus buffers except for group mode buffer.
4636   (let ((group-buf (get-buffer gnus-group-buffer)))
4637     ;; Do this on a separate list in case the user does a ^G before we finish
4638     (let ((gnus-buffer-list
4639            (delq group-buf (delq gnus-dribble-buffer
4640                                  (append gnus-buffer-list nil)))))
4641       (while gnus-buffer-list
4642         (gnus-kill-buffer (car gnus-buffer-list))
4643         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4644     (if group-buf
4645         (progn
4646           (setq gnus-buffer-list (list group-buf))
4647           (bury-buffer group-buf)
4648           (delete-windows-on group-buf t)))))
4649
4650 (defun gnus-group-clear-dribble ()
4651   "Clear all information from the dribble buffer."
4652   (interactive)
4653   (gnus-dribble-clear))
4654
4655 (defun gnus-group-exit ()
4656   "Quit reading news after updating .newsrc.eld and .newsrc.
4657 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4658   (interactive)
4659   (if (or noninteractive                ;For gnus-batch-kill
4660           (zerop (buffer-size))         ;No news is good news.
4661           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4662           (not gnus-interactive-exit)   ;Without confirmation
4663           gnus-expert-user
4664           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4665       (progn
4666         (if gnus-use-full-window
4667             (delete-other-windows)
4668           (gnus-remove-some-windows))
4669         (run-hooks 'gnus-exit-gnus-hook)
4670         (gnus-offer-save-summaries)
4671         (gnus-save-newsrc-file)
4672         (gnus-close-backends)
4673         (gnus-clear-system))))
4674
4675 (defun gnus-close-backends ()
4676   ;; Send a close request to all backends that support such a request. 
4677   (let ((methods gnus-valid-select-methods)
4678         func)
4679     (while methods
4680       (if (fboundp (setq func (intern (concat (car (car methods))
4681                                               "-request-close"))))
4682           (funcall func))
4683       (setq methods (cdr methods)))))
4684
4685 (defun gnus-group-quit ()
4686   "Quit reading news without updating .newsrc.eld or .newsrc.
4687 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4688   (interactive)
4689   (if (or noninteractive                ;For gnus-batch-kill
4690           (zerop (buffer-size))
4691           (not (gnus-server-opened gnus-select-method))
4692           gnus-expert-user
4693           (not gnus-current-startup-file)
4694           (gnus-yes-or-no-p
4695            (format "Quit reading news without saving %s? "
4696                    (file-name-nondirectory gnus-current-startup-file))))
4697       (progn
4698         (run-hooks 'gnus-exit-gnus-hook)
4699         (if gnus-use-full-window
4700             (delete-other-windows)
4701           (gnus-remove-some-windows))
4702         (gnus-dribble-save)
4703         (gnus-close-backends)
4704         (gnus-clear-system))))
4705
4706 (defun gnus-offer-save-summaries ()
4707   (let ((buffers (buffer-list)))
4708     (save-excursion
4709       (while buffers
4710         (and 
4711          ;; We look for buffers with "Summary" in the name.
4712          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4713          (progn
4714            (set-buffer (car buffers))
4715            ;; We check that this is, indeed, a summary buffer.
4716            (eq major-mode 'gnus-summary-mode)) 
4717          ;; We ask the user whether she wants to save the info.
4718          (gnus-y-or-n-p
4719                (format "Update summary buffer %s? " (buffer-name)))
4720          ;; We do it by simply exiting.
4721          (gnus-summary-exit))
4722         (setq buffers (cdr buffers))))))
4723
4724 (defun gnus-group-describe-briefly ()
4725   "Give a one line description of the group mode commands."
4726   (interactive)
4727   (gnus-message 6
4728    (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")))
4729
4730 (defun gnus-group-browse-foreign-server (method)
4731   "Browse a foreign news server.
4732 If called interactively, this function will ask for a select method
4733  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4734 If not, METHOD should be a list where the first element is the method
4735 and the second element is the address."
4736   (interactive
4737    (list (let ((how (completing-read 
4738                      "Which backend: "
4739                      (append gnus-valid-select-methods gnus-server-alist)
4740                      nil t "nntp")))
4741            ;; We either got a backend name or a virtual server name.
4742            ;; If the first, we also need an address.
4743            (if (assoc how gnus-valid-select-methods)
4744                (list (intern how)
4745                      ;; Suggested by mapjph@bath.ac.uk.
4746                      (completing-read 
4747                       "Address: " 
4748                       (mapcar (lambda (server) (list server))
4749                               gnus-secondary-servers)))
4750              ;; We got a server name, so we find the method.
4751              (gnus-server-to-method how)))))
4752   (gnus-browse-foreign-server method))
4753
4754 \f
4755 ;;;
4756 ;;; Browse Server Mode
4757 ;;;
4758
4759 (defvar gnus-browse-mode-hook nil)
4760 (defvar gnus-browse-mode-map nil)
4761 (put 'gnus-browse-mode 'mode-class 'special)
4762
4763 (if gnus-browse-mode-map
4764     nil
4765   (setq gnus-browse-mode-map (make-keymap))
4766   (suppress-keymap gnus-browse-mode-map)
4767   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4768   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4769   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4770   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4771   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4772   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4773   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4774   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4775   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4776   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4777   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4778   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4779   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4780   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4781   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4782   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4783   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4784   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4785   )
4786
4787 (defvar gnus-browse-current-method nil)
4788 (defvar gnus-browse-return-buffer nil)
4789
4790 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4791
4792 (defun gnus-browse-foreign-server (method &optional return-buffer)
4793   (setq gnus-browse-current-method method)
4794   (setq gnus-browse-return-buffer return-buffer)
4795   (let ((gnus-select-method method)
4796         groups group)
4797     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4798     (or (gnus-server-opened method)
4799         (gnus-open-server method)
4800         (error "Unable to contact server: %s" (gnus-status-message method)))
4801     (or (gnus-request-list method)
4802         (error "Couldn't request list: %s" (gnus-status-message method)))
4803     (get-buffer-create gnus-browse-buffer)
4804     (gnus-add-current-to-buffer-list)
4805     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4806     (gnus-configure-windows 'browse)
4807     (buffer-disable-undo (current-buffer))
4808     (let ((buffer-read-only nil))
4809       (erase-buffer))
4810     (gnus-browse-mode)
4811     (setq mode-line-buffer-identification
4812           (format
4813            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4814     (save-excursion
4815       (set-buffer nntp-server-buffer)
4816       (let ((cur (current-buffer)))
4817         (goto-char (point-min))
4818         (or (string= gnus-ignored-newsgroups "")
4819             (delete-matching-lines gnus-ignored-newsgroups))
4820         (while (re-search-forward 
4821                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4822           (goto-char (match-end 1))
4823           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4824                                                      (match-end 1))
4825                                    (max 0 (- (1+ (read cur)) (read cur))))
4826                              groups)))))
4827     (setq groups (sort groups 
4828                        (lambda (l1 l2)
4829                          (string< (car l1) (car l2)))))
4830     (let ((buffer-read-only nil))
4831       (while groups
4832         (setq group (car groups))
4833         (insert 
4834          (format "K%7d: %s\n" (cdr group) (car group)))
4835         (setq groups (cdr groups))))
4836     (switch-to-buffer (current-buffer))
4837     (goto-char (point-min))
4838     (gnus-group-position-cursor)))
4839
4840 (defun gnus-browse-mode ()
4841   "Major mode for browsing a foreign server.
4842
4843 All normal editing commands are switched off.
4844
4845 \\<gnus-browse-mode-map>
4846 The only things you can do in this buffer is
4847
4848 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4849 The group will be inserted into the group buffer upon exit from this
4850 buffer.  
4851
4852 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4853
4854 3) `\\[gnus-browse-exit]' to return to the group buffer."
4855   (interactive)
4856   (kill-all-local-variables)
4857   (if gnus-visual (gnus-browse-make-menu-bar))
4858   (setq mode-line-modified "-- ")
4859   (make-local-variable 'mode-line-format)
4860   (setq mode-line-format (copy-sequence mode-line-format))
4861   (and (equal (nth 3 mode-line-format) "   ")
4862        (setcar (nthcdr 3 mode-line-format) ""))
4863   (setq major-mode 'gnus-browse-mode)
4864   (setq mode-name "Browse Server")
4865   (setq mode-line-process nil)
4866   (use-local-map gnus-browse-mode-map)
4867   (buffer-disable-undo (current-buffer))
4868   (setq truncate-lines t)
4869   (setq buffer-read-only t)
4870   (run-hooks 'gnus-browse-mode-hook))
4871
4872 (defun gnus-browse-read-group (&optional no-article)
4873   "Enter the group at the current line."
4874   (interactive)
4875   (let ((group (gnus-browse-group-name)))
4876     (or (gnus-group-read-ephemeral-group 
4877          group gnus-browse-current-method nil
4878          (cons (current-buffer) 'browse))
4879         (error "Couldn't enter %s" group))))
4880
4881 (defun gnus-browse-select-group ()
4882   "Select the current group."
4883   (interactive)
4884   (gnus-browse-read-group 'no))
4885
4886 (defun gnus-browse-next-group (n)
4887   "Go to the next group."
4888   (interactive "p")
4889   (prog1
4890       (forward-line n)
4891     (gnus-group-position-cursor)))
4892
4893 (defun gnus-browse-prev-group (n)
4894   "Go to the next group."
4895   (interactive "p")
4896   (gnus-browse-next-group (- n)))
4897
4898 (defun gnus-browse-unsubscribe-current-group (arg)
4899   "(Un)subscribe to the next ARG groups."
4900   (interactive "p")
4901   (and (eobp)
4902        (error "No group at current line."))
4903   (let ((ward (if (< arg 0) -1 1))
4904         (arg (abs arg)))
4905     (while (and (> arg 0)
4906                 (not (eobp))
4907                 (gnus-browse-unsubscribe-group)
4908                 (zerop (gnus-browse-next-group ward)))
4909       (setq arg (1- arg)))
4910     (gnus-group-position-cursor)
4911     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4912     arg))
4913
4914 (defun gnus-browse-group-name ()
4915   (save-excursion
4916     (beginning-of-line)
4917     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4918         ()
4919       (gnus-group-prefixed-name 
4920        (buffer-substring (match-beginning 1) (match-end 1))
4921        gnus-browse-current-method))))
4922   
4923 (defun gnus-browse-unsubscribe-group ()
4924   (let ((sub nil)
4925         (buffer-read-only nil)
4926         group)
4927     (save-excursion
4928       (beginning-of-line)
4929       (if (= (following-char) ?K) (setq sub t))
4930       (setq group (gnus-browse-group-name))
4931       (beginning-of-line)
4932       (delete-char 1)
4933       (if sub
4934           (progn
4935             (gnus-group-change-level 
4936              (list t group gnus-level-default-subscribed
4937                    nil nil gnus-browse-current-method) 
4938              gnus-level-default-subscribed gnus-level-killed
4939              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4940              t)
4941             (insert ? ))
4942         (gnus-group-change-level 
4943          group gnus-level-killed gnus-level-default-subscribed)
4944         (insert ?K)))
4945     t))
4946
4947 (defun gnus-browse-exit ()
4948   "Quit browsing and return to the group buffer."
4949   (interactive)
4950   (if (eq major-mode 'gnus-browse-mode)
4951       (kill-buffer (current-buffer)))
4952   (if gnus-browse-return-buffer
4953       (gnus-configure-windows 'server)
4954     (gnus-configure-windows 'group)))
4955
4956 (defun gnus-browse-describe-briefly ()
4957   "Give a one line description of the group mode commands."
4958   (interactive)
4959   (gnus-message 6
4960    (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")))
4961       
4962 \f
4963 ;;;
4964 ;;; Gnus summary mode
4965 ;;;
4966
4967 (defvar gnus-summary-mode-map nil)
4968 (defvar gnus-summary-mark-map nil)
4969 (defvar gnus-summary-mscore-map nil)
4970 (defvar gnus-summary-article-map nil)
4971 (defvar gnus-summary-thread-map nil)
4972 (defvar gnus-summary-goto-map nil)
4973 (defvar gnus-summary-exit-map nil)
4974 (defvar gnus-summary-various-map nil)
4975 (defvar gnus-summary-interest-map nil)
4976 (defvar gnus-summary-sort-map nil)
4977 (defvar gnus-summary-backend-map nil)
4978 (defvar gnus-summary-save-map nil)
4979 (defvar gnus-summary-wash-map nil)
4980 (defvar gnus-summary-help-map nil)
4981
4982 (put 'gnus-summary-mode 'mode-class 'special)
4983
4984 (if gnus-summary-mode-map
4985     nil
4986   (setq gnus-summary-mode-map (make-keymap))
4987   (suppress-keymap gnus-summary-mode-map)
4988
4989   ;; Non-orthogonal keys
4990
4991   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4992   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4993   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4994   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4995   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4996   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4997   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4998   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4999   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5000   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5001   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5002   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5003   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5004   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5005   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5006   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5007   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5008   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5009   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5010   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5011   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5012   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5013   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5014   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5015   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5016   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5017   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5018   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5019   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5020   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5021   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5022   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5023   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5024   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5025   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5026   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5027   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5028   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5029   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5030   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5031   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5032   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5033   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5034   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5035   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5036   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5037   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5038   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5039   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5040   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5041   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5042   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5043   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5044   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5045   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5046   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5047   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5048   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5049   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5050   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5051   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5052   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5053   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5054   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5055   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5056   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5057   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5058   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5059   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5060   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5061   (define-key gnus-summary-mode-map "V" 'gnus-version)
5062   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5063   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5064   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5065   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5066   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5067   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5068   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5069   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5070 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5071   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5072   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5073   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5074 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5075   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5076   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5077   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5078   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5079   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5080
5081
5082   ;; Sort of orthogonal keymap
5083   (define-prefix-command 'gnus-summary-mark-map)
5084   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5085   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5086   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5087   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5088   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5089   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5090   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5091   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5092   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5093   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5094   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5095   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5096   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5097   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5098   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5099   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5100   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5101   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5102   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5103   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5104   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5105   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5106   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5107   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5108
5109   (define-prefix-command 'gnus-summary-mscore-map)
5110   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5111   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5112   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5113   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5114   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5115
5116   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5117   
5118   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5119   
5120   (define-prefix-command 'gnus-summary-goto-map)
5121   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5122   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5123   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5124   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5125   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5126   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5127   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5128   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5129   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5130   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5131   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5132   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5133   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5134   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5135
5136
5137   (define-prefix-command 'gnus-summary-thread-map)
5138   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5139   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5140   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5141   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5142   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5143   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5144   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5145   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5146   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5147   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5148   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5149   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5150   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5151   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5152
5153   
5154   (define-prefix-command 'gnus-summary-exit-map)
5155   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5156   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5157   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5158   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5159   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5160   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5161   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5162   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5163   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5164   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5165   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5166
5167
5168   (define-prefix-command 'gnus-summary-article-map)
5169   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5170   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5171   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5172   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5173   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5174   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5175   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5176   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5177   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5178   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5179   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5180   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5181   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5182   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5183   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5184   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5185   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5186   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5187
5188
5189   (define-prefix-command 'gnus-summary-wash-map)
5190   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5191   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5192   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5193   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5194   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5195   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5196   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5197   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5198   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5199   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5200   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5201   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5202
5203   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5204   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5205   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5206   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5207   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5208   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5209
5210
5211   (define-prefix-command 'gnus-summary-help-map)
5212   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5213   (define-key gnus-summary-help-map "v" 'gnus-version)
5214   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5215   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5216   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5217   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5218
5219
5220   (define-prefix-command 'gnus-summary-backend-map)
5221   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5222   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5223   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5224   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5225   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5226   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5227   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5228   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5229   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5230
5231
5232   (define-prefix-command 'gnus-summary-save-map)
5233   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5234   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5235   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5236   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5237   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5238   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5239   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5240   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5241 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5242
5243   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5244   
5245   (define-prefix-command 'gnus-summary-various-map)
5246   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5247   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5248   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5249   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5250   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5251   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5252   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5253   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5254   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5255   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5256   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5257
5258   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5259
5260   (define-prefix-command 'gnus-summary-sort-map)
5261   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5262   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5263   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5264   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5265   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5266   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5267
5268   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5269   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5270   )
5271
5272
5273 \f
5274
5275 (defun gnus-summary-mode ()
5276   "Major mode for reading articles.
5277
5278 All normal editing commands are switched off.
5279 \\<gnus-summary-mode-map>
5280 Each line in this buffer represents one article.  To read an
5281 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5282 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5283 respectively.
5284
5285 You can also post articles and send mail from this buffer.  To 
5286 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5287 of an article, type `\\[gnus-summary-reply]'.
5288
5289 There are approx. one gazillion commands you can execute in this 
5290 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5291
5292 The following commands are available:
5293
5294 \\{gnus-summary-mode-map}"
5295   (interactive)
5296   (if gnus-visual (gnus-summary-make-menu-bar))
5297   (kill-all-local-variables)
5298   (let ((locals gnus-summary-local-variables))
5299     (while locals
5300       (if (consp (car locals))
5301           (progn
5302             (make-local-variable (car (car locals)))
5303             (set (car (car locals)) (eval (cdr (car locals)))))
5304         (make-local-variable (car locals))
5305         (set (car locals) nil))
5306       (setq locals (cdr locals))))
5307   (gnus-make-thread-indent-array)
5308   (gnus-update-format-specifications)
5309   (setq mode-line-modified "-- ")
5310   (make-local-variable 'mode-line-format)
5311   (setq mode-line-format (copy-sequence mode-line-format))
5312   (and (equal (nth 3 mode-line-format) "   ")
5313        (setcar (nthcdr 3 mode-line-format) ""))
5314   (setq major-mode 'gnus-summary-mode)
5315   (setq mode-name "Summary")
5316   (make-local-variable 'minor-mode-alist)
5317   (use-local-map gnus-summary-mode-map)
5318   (buffer-disable-undo (current-buffer))
5319   (setq buffer-read-only t)             ;Disable modification
5320   (setq truncate-lines t)
5321   (setq selective-display t)
5322   (setq selective-display-ellipses t)   ;Display `...'
5323   (setq buffer-display-table gnus-summary-display-table)
5324   (run-hooks 'gnus-summary-mode-hook))
5325
5326 (defun gnus-summary-make-display-table ()
5327   ;; Change the display table.  Odd characters have a tendency to mess
5328   ;; up nicely formatted displays - we make all possible glyphs
5329   ;; display only a single character.
5330
5331   ;; We start from the standard display table, if any.
5332   (setq gnus-summary-display-table 
5333         (or (copy-sequence standard-display-table)
5334             (make-display-table)))
5335   ;; Nix out all the control chars...
5336   (let ((i 32))
5337     (while (>= (setq i (1- i)) 0)
5338       (aset gnus-summary-display-table i [??])))
5339   ;; ... but not newline and cr, of course. (cr is necessary for the
5340   ;; selective display).  
5341   (aset gnus-summary-display-table ?\n nil)
5342   (aset gnus-summary-display-table ?\r nil)
5343   ;; We nix out any glyphs over 126 that are not set already.  
5344   (let ((i 256))
5345     (while (>= (setq i (1- i)) 127)
5346       ;; Only modify if the entry is nil.
5347       (or (aref gnus-summary-display-table i) 
5348           (aset gnus-summary-display-table i [??])))))
5349
5350 (defun gnus-summary-clear-local-variables ()
5351   (let ((locals gnus-summary-local-variables))
5352     (while locals
5353       (if (consp (car locals))
5354           (and (vectorp (car (car locals)))
5355                (set (car (car locals)) nil))
5356         (and (vectorp (car locals))
5357              (set (car locals) nil)))
5358       (setq locals (cdr locals)))))
5359
5360 (defun gnus-mouse-pick-article (e)
5361   (interactive "e")
5362   (mouse-set-point e)
5363   (gnus-summary-next-page nil t))
5364
5365 (defun gnus-summary-setup-buffer (group)
5366   "Initialize summary buffer."
5367   (let ((buffer (concat "*Summary " group "*")))
5368     (if (get-buffer buffer)
5369         (progn
5370           (set-buffer buffer)
5371           (not gnus-newsgroup-begin))
5372       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5373       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5374       (gnus-add-current-to-buffer-list)
5375       (gnus-summary-mode)
5376       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5377       (setq gnus-newsgroup-name group)
5378       t)))
5379
5380 (defun gnus-set-global-variables ()
5381   ;; Set the global equivalents of the summary buffer-local variables
5382   ;; to the latest values they had. These reflect the summary buffer
5383   ;; that was in action when the last article was fetched.
5384   (if (eq major-mode 'gnus-summary-mode) 
5385       (progn
5386         (setq gnus-summary-buffer (current-buffer))
5387         (let ((name gnus-newsgroup-name)
5388               (marked gnus-newsgroup-marked)
5389               (unread gnus-newsgroup-unreads)
5390               (headers gnus-current-headers)
5391               (score-file gnus-current-score-file))
5392           (save-excursion
5393             (set-buffer gnus-group-buffer)
5394             (setq gnus-newsgroup-name name)
5395             (setq gnus-newsgroup-marked marked)
5396             (setq gnus-newsgroup-unreads unread)
5397             (setq gnus-current-headers headers)
5398             (setq gnus-current-score-file score-file))))))
5399
5400 (defun gnus-summary-insert-dummy-line (sformat subject number)
5401   (if (not sformat) 
5402       (setq sformat gnus-summary-dummy-line-format-spec))
5403   (let (b)
5404     (beginning-of-line)
5405     (setq b (point))
5406     (insert (eval sformat))
5407     (add-text-properties
5408      b (1+ b)
5409      (list 'gnus-number number 
5410            'gnus-mark gnus-dummy-mark
5411            'gnus-level 0))))
5412
5413 (defvar gnus-thread-indent-array nil)
5414 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5415 (defun gnus-make-thread-indent-array ()
5416   (let ((n 200))
5417     (if (and gnus-thread-indent-array
5418              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5419         nil
5420       (setq gnus-thread-indent-array (make-vector 201 "")
5421             gnus-thread-indent-array-level gnus-thread-indent-level)
5422       (while (>= n 0)
5423         (aset gnus-thread-indent-array n
5424               (make-string (* n gnus-thread-indent-level) ? ))
5425         (setq n (1- n))))))
5426
5427 (defun gnus-summary-insert-line 
5428   (sformat header level current unread replied expirable subject-or-nil
5429            &optional dummy score)
5430   (or sformat (setq sformat gnus-summary-line-format-spec))
5431   (let* ((indentation (aref gnus-thread-indent-array level))
5432          (lines (header-lines header))
5433          (score (or score gnus-summary-default-score 0))
5434          (score-char
5435           (if (or (null gnus-summary-default-score)
5436                   (<= (abs (- score gnus-summary-default-score))
5437                       gnus-summary-zcore-fuzz)) ? 
5438             (if (< score gnus-summary-default-score)
5439                 gnus-score-below-mark gnus-score-over-mark)))
5440          (replied (if replied gnus-replied-mark ? ))
5441          (from (header-from header))
5442          (name-address (funcall gnus-extract-address-components from))
5443          (address (car (cdr name-address)))
5444          (name (or (car name-address) (car (cdr name-address))))
5445          (subject (header-subject header))
5446          (number (header-number header))
5447          (opening-bracket (if dummy ?\< ?\[))
5448          (closing-bracket (if dummy ?\> ?\]))
5449          (buffer-read-only nil)
5450          (b (progn (beginning-of-line) (point))))
5451     (or (numberp lines) (setq lines 0))
5452     (insert (eval sformat))
5453     (add-text-properties
5454      b (1+ b) (list 'gnus-number number 
5455                     'gnus-mark (or unread gnus-unread-mark)
5456                     'gnus-level level))))
5457
5458 (defun gnus-summary-update-line (&optional dont-update)
5459   ;; Update summary line after change.
5460   (or (not gnus-summary-default-score)
5461       gnus-summary-inhibit-highlight
5462       (let ((gnus-summary-inhibit-highlight t)
5463             (article (gnus-summary-article-number)))
5464         (progn
5465           (or dont-update
5466               (if (and gnus-summary-mark-below
5467                        (< (gnus-summary-article-score)
5468                           gnus-summary-mark-below))
5469                   (and (not (memq article gnus-newsgroup-marked))
5470                        (not (memq article gnus-newsgroup-dormant))
5471                        (memq article gnus-newsgroup-unreads)
5472                        (gnus-summary-mark-article nil gnus-low-score-mark))
5473                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5474                      (gnus-summary-mark-article nil gnus-unread-mark))))
5475           (and gnus-visual
5476                (run-hooks 'gnus-summary-update-hook))))))
5477
5478 (defun gnus-summary-update-lines (&optional beg end)
5479   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5480   (let ((beg (or beg (point-min)))
5481         (end (or end (point-max))))
5482     (save-excursion
5483       (set-buffer gnus-summary-buffer)
5484       (goto-char beg)
5485       (while (and (not (eobp)) (< (point) end))
5486         (gnus-summary-update-line)
5487         (forward-line 1)))))
5488
5489 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5490   ;; Sum up all elements (and sub-elements) in a list.
5491   (let ((number 
5492          (if (listp thread) 
5493              (apply 
5494               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5495            1)))
5496     (if char 
5497         (if (> number 1) gnus-not-empty-thread-mark
5498           gnus-empty-thread-mark)
5499       number)))
5500
5501 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5502   "Start reading news in newsgroup GROUP.
5503 If SHOW-ALL is non-nil, already read articles are also listed.
5504 If NO-ARTICLE is non-nil, no article is selected initially."
5505   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5506   (let* ((new-group (gnus-summary-setup-buffer group))
5507          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5508     (cond 
5509      ((not new-group)
5510       (gnus-set-global-variables)
5511       (gnus-kill-buffer kill-buffer)
5512       (gnus-configure-windows 'summary)
5513       (gnus-set-mode-line 'summary)
5514       (gnus-summary-position-cursor)
5515       (message "")
5516       t)
5517      ((null did-select) 
5518       (and (eq major-mode 'gnus-summary-mode)
5519            (not (equal (current-buffer) kill-buffer))
5520            (progn
5521              (kill-buffer (current-buffer))
5522              (set-buffer gnus-group-buffer)
5523              (gnus-group-next-unread-group 1)))
5524       (message "Can't select group")
5525       nil)
5526      ((eq did-select 'quit)
5527       (and (eq major-mode 'gnus-summary-mode)
5528            (not (equal (current-buffer) kill-buffer))
5529            (kill-buffer (current-buffer)))
5530       (gnus-kill-buffer kill-buffer)
5531       (gnus-configure-windows 'group)
5532       (gnus-group-next-unread-group 1)
5533       (signal 'quit nil))
5534      (t
5535       (gnus-set-global-variables)
5536       ;; Save the active value in effect when the group was entered.
5537       (setq gnus-newsgroup-active 
5538             (gnus-copy-sequence
5539              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5540       ;; You can change the subjects in this hook.
5541       (run-hooks 'gnus-select-group-hook)
5542       ;; Do score processing.
5543       (and gnus-use-scoring (gnus-possibly-score-headers))
5544       ;; Update the format specifiers.
5545       (gnus-update-format-specifications)
5546       ;; Generate the summary buffer.
5547       (gnus-summary-prepare)
5548       (if (zerop (buffer-size))
5549           (cond (gnus-newsgroup-dormant
5550                  (gnus-summary-show-all-dormant))
5551                 ((and gnus-newsgroup-scored show-all)
5552                  (gnus-summary-show-all-expunged))))
5553       ;; Function `gnus-apply-kill-file' must be called in this hook.
5554       (run-hooks 'gnus-apply-kill-hook)
5555       (if (zerop (buffer-size))
5556           (progn
5557             ;; This newsgroup is empty.
5558             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5559             (gnus-message 6 "No unread news")
5560             (gnus-kill-buffer kill-buffer)
5561             nil)
5562         ;;(save-excursion
5563         ;;  (if kill-buffer
5564         ;;      (let ((gnus-summary-buffer kill-buffer))
5565         ;;      (gnus-configure-windows 'group))))
5566         ;; Hide conversation thread subtrees.  We cannot do this in
5567         ;; gnus-summary-prepare-hook since kill processing may not
5568         ;; work with hidden articles.
5569         (and gnus-show-threads
5570              gnus-thread-hide-subtree
5571              (gnus-summary-hide-all-threads))
5572         ;; Show first unread article if requested.
5573         (goto-char (point-min))
5574         (if (and (not no-article)
5575                  gnus-auto-select-first
5576                  (gnus-summary-first-unread-article))
5577             ()
5578           (gnus-configure-windows 'summary))
5579         (gnus-set-mode-line 'summary)
5580         (gnus-summary-position-cursor)
5581         ;; If in async mode, we send some info to the backend.
5582         (and gnus-newsgroup-async
5583              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5584              (gnus-request-asynchronous 
5585               gnus-newsgroup-name
5586               (if (and gnus-asynchronous-article-function
5587                        (fboundp gnus-asynchronous-article-function))
5588                   (funcall gnus-asynchronous-article-function
5589                            gnus-newsgroup-threads)
5590                 gnus-newsgroup-threads)))
5591         (gnus-kill-buffer kill-buffer)
5592         (if (not (get-buffer-window gnus-group-buffer))
5593             ()
5594           ;; gotta use windows, because recenter does wierd stuff if
5595           ;; the current buffer ain't the displayed window.
5596           (let ((owin (selected-window))) 
5597             (select-window (get-buffer-window gnus-group-buffer))
5598             (and (gnus-group-goto-group group)
5599                  (recenter))
5600             (select-window owin))))
5601       t))))
5602
5603 (defun gnus-summary-prepare ()
5604   ;; Generate the summary buffer.
5605   (let ((buffer-read-only nil))
5606     (erase-buffer)
5607     (gnus-summary-prepare-threads 
5608      (if gnus-show-threads
5609          (gnus-gather-threads 
5610           (gnus-sort-threads 
5611            (if (and gnus-summary-expunge-below
5612                     (not gnus-fetch-old-headers))
5613                (gnus-make-threads-and-expunge)
5614              (gnus-make-threads))))
5615        gnus-newsgroup-headers)
5616      0 nil nil t)
5617     ;; Erase header retrieval message.
5618     (gnus-summary-update-lines)
5619     (message "")
5620     ;; Remove the final newline.
5621     ;;(goto-char (point-max))
5622     ;;(delete-char -1)
5623     ;; Call hooks for modifying summary buffer.
5624     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5625     (goto-char (point-min))
5626     (run-hooks 'gnus-summary-prepare-hook)))
5627
5628 (defun gnus-subject-equal (s1 s2)
5629   (cond 
5630    ((numberp gnus-summary-gather-subject-limit)
5631     (string= (if (> (length s1) gnus-summary-gather-subject-limit)
5632                  (substring s1 0 gnus-summary-gather-subject-limit)
5633                s1)
5634              (if (> (length s2) gnus-summary-gather-subject-limit)
5635                  (substring s2 0 gnus-summary-gather-subject-limit)
5636                s2)))
5637    ((eq 'fuzzy gnus-summary-gather-subject-limit)
5638     (string= (gnus-simplify-subject-fuzzy s1)
5639              (gnus-simplify-subject-fuzzy s2)))
5640    (t
5641     (string= s1 s2))))
5642
5643 (defun gnus-gather-threads (threads)
5644   "Gather threads that have lost their roots."
5645   (if (not gnus-summary-make-false-root)
5646       threads 
5647     (let ((hashtb (gnus-make-hashtable 1023))
5648           (prev threads)
5649           (result threads)
5650           subject hthread whole-subject)
5651       (while threads
5652         (setq whole-subject 
5653               (setq subject (header-subject (car (car threads)))))
5654         (if gnus-summary-gather-subject-limit
5655             (or (and (numberp gnus-summary-gather-subject-limit)
5656                      (> (length subject) gnus-summary-gather-subject-limit)
5657                      (setq subject
5658                            (substring subject 0 
5659                                       gnus-summary-gather-subject-limit)))
5660                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5661                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5662           (setq subject (gnus-simplify-subject-re subject)))
5663         (if (setq hthread 
5664                   (gnus-gethash subject hashtb))
5665             (progn
5666               (or (stringp (car (car hthread)))
5667                   (setcar hthread (list whole-subject (car hthread))))
5668               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5669                                            (list (car threads))))
5670               (setcdr prev (cdr threads))
5671               (setq threads prev))
5672           (gnus-sethash subject threads hashtb))
5673         (setq prev threads)
5674         (setq threads (cdr threads)))
5675       result)))
5676
5677 (defun gnus-make-threads ()
5678   ;; This function takes the dependencies already made by 
5679   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5680   ;; through the dependecies in the hash table and finds all the
5681   ;; roots. Roots do not refer back to any valid articles.
5682   (let (roots)
5683     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5684          (gnus-build-old-threads))
5685     (mapatoms
5686      (lambda (refs)
5687        (if (not (car (symbol-value refs)))
5688            (setq roots (append (cdr (symbol-value refs)) roots))
5689          ;; Ok, these refer back to valid articles, but if
5690          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5691          ;; the root has the same subject as its children. The children
5692          ;; that do not are made into roots and removed from the list
5693          ;; of children. 
5694          (or gnus-thread-ignore-subject
5695              (let* ((prev (symbol-value refs))
5696                     (subject (gnus-simplify-subject-re 
5697                               (header-subject (car prev))))
5698                     (headers (cdr prev)))
5699                (while headers
5700                  (if (not (string= subject
5701                                    (gnus-simplify-subject-re 
5702                                     (header-subject (car headers)))))
5703                      (progn
5704                        (setq roots (cons (car headers) roots))
5705                        (setcdr prev (cdr headers)))
5706                    (setq prev headers))
5707                  (setq headers (cdr headers)))))))
5708      gnus-newsgroup-dependencies)
5709     
5710     (mapcar 'gnus-trim-thread
5711             (apply 'append
5712                    (mapcar 'gnus-cut-thread
5713                            (mapcar 'gnus-make-sub-thread roots))))))
5714   
5715 (defun gnus-make-threads-and-expunge ()
5716   ;; This function takes the dependencies already made by 
5717   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5718   ;; through the dependecies in the hash table and finds all the
5719   ;; roots. Roots do not refer back to any valid articles.
5720   (let ((default (or gnus-summary-default-score 0))
5721         (below gnus-summary-expunge-below)
5722         roots article)
5723     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5724          (gnus-build-old-threads))
5725     (mapatoms
5726      (lambda (refs)
5727        (if (not (car (symbol-value refs)))
5728            ;; These articles do not refer back to any other articles -
5729            ;; they are roots.
5730            (let ((headers (cdr (symbol-value refs))))
5731              ;; We weed out the low-scored articles.
5732              (while headers
5733                (if (not (< (or (cdr (assq (header-number (car headers))
5734                                           gnus-newsgroup-scored)) default)
5735                            below))
5736                    ;; It is over.
5737                    (setq roots (cons (car headers) roots))
5738                  ;; It is below, so we mark it as read.
5739                  (setq gnus-newsgroup-unreads
5740                        (delq (header-number (car headers))
5741                              gnus-newsgroup-unreads)))
5742                (setq headers (cdr headers))))
5743          ;; Ok, these refer back to valid articles, but if
5744          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5745          ;; the root has the same subject as its children. The children
5746          ;; that do not are made into roots and removed from the list
5747          ;; of children. 
5748          (or gnus-thread-ignore-subject
5749              (let* ((prev (symbol-value refs))
5750                     (subject (gnus-simplify-subject-re 
5751                               (header-subject (car prev))))
5752                     (headers (cdr prev)))
5753                (while headers
5754                  (if (not (string= subject
5755                                    (gnus-simplify-subject-re 
5756                                     (header-subject (car headers)))))
5757                      (progn
5758                        (if (not (< (or (cdr (assq (header-number (car headers))
5759                                                   gnus-newsgroup-scored))
5760                                        default) below))
5761                            (setq roots (cons (car headers) roots))
5762                          (setq gnus-newsgroup-unreads
5763                                (delq (header-number (car headers))
5764                                      gnus-newsgroup-unreads)))
5765                        (setcdr prev (cdr headers)))
5766                    (setq prev headers))
5767                  (setq headers (cdr headers)))))
5768          ;; If this article is expunged, some of the children might be
5769          ;; roots.  
5770          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5771                                gnus-newsgroup-scored)) default)
5772                 below)
5773              (let* ((prev (symbol-value refs))
5774                     (headers (cdr prev)))
5775                (while headers
5776                  (setq article (header-number (car headers)))
5777                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5778                                  default) below))
5779                      (progn (setq roots (cons (car headers) roots))
5780                             (setq prev headers))
5781                    (setq gnus-newsgroup-unreads 
5782                          (delq article gnus-newsgroup-unreads))
5783                    (setcdr prev (cdr headers)))
5784                  (setq headers (cdr headers))))
5785            ;; It was not expunged, but we look at expunged children.
5786            (let* ((prev (symbol-value refs))
5787                   (headers (cdr prev))
5788                   article)
5789              (while headers
5790                (setq article (header-number (car headers)))
5791                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5792                                default) below))
5793                    (setq prev headers)
5794                  (setq gnus-newsgroup-unreads 
5795                        (delq article gnus-newsgroup-unreads))
5796                  (setcdr prev (cdr headers)))
5797                (setq headers (cdr headers)))))))
5798      gnus-newsgroup-dependencies)
5799
5800     (mapcar 'gnus-trim-thread
5801             (apply 'append
5802                    (mapcar 'gnus-cut-thread
5803                            (mapcar 'gnus-make-sub-thread roots))))))
5804   
5805 (defun gnus-cut-thread (thread)
5806   ;; Remove leaf dormant or ancient articles from THREAD.
5807   (let ((head (car thread))
5808         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5809     (if (and (null tail)
5810              (let ((number (header-number head)))
5811                (or (memq number gnus-newsgroup-ancient)
5812                    (memq number gnus-newsgroup-dormant)
5813                    (and gnus-summary-expunge-below
5814                         (eq gnus-fetch-old-headers 'some)
5815                         (< (or (cdr (assq number gnus-newsgroup-scored))
5816                                gnus-summary-default-score 0)
5817                            gnus-summary-expunge-below)
5818                         (progn
5819                           (setq gnus-newsgroup-unreads
5820                                 (delq number gnus-newsgroup-unreads))
5821                           t)))))
5822         nil
5823       (list (cons head tail)))))
5824
5825 (defun gnus-trim-thread (thread)
5826   ;; Remove root ancient articles with only one child from THREAD.
5827   (if (and (eq gnus-fetch-old-headers 'some)
5828            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5829            (= (length thread) 2))
5830       (gnus-trim-thread (nth 1 thread))
5831     thread))
5832
5833 (defun gnus-make-sub-thread (root)
5834   ;; This function makes a sub-tree for a node in the tree.
5835   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5836                                               gnus-newsgroup-dependencies)))))
5837     (cons root (mapcar 'gnus-make-sub-thread children))))
5838
5839 (defun gnus-build-old-threads ()
5840   ;; Look at all the articles that refer back to old articles, and
5841   ;; fetch the headers for the articles that aren't there. This will
5842   ;; build complete threads - if the roots haven't been expired by the
5843   ;; server, that is.
5844   (let (id heads)
5845     (mapatoms
5846      (lambda (refs)
5847        (if (not (car (symbol-value refs)))
5848            (progn
5849              (setq heads (cdr (symbol-value refs)))
5850              (while heads
5851                (if (not (memq (header-number (car heads))
5852                               gnus-newsgroup-dormant))
5853                    (progn
5854                      (setq id (symbol-name refs))
5855                      (while (and (setq id (gnus-build-get-header id))
5856                                  (not (car (gnus-gethash 
5857                                             id gnus-newsgroup-dependencies)))))
5858                      (setq heads nil))
5859                  (setq heads (cdr heads)))))))
5860      gnus-newsgroup-dependencies)))
5861
5862 (defun gnus-build-get-header (id)
5863   ;; Look through the buffer of NOV lines and find the header to
5864   ;; ID. Enter this line into the dependencies hash table, and return
5865   ;; the id of the parent article (if any).
5866   (let ((deps gnus-newsgroup-dependencies)
5867         found header)
5868     (prog1
5869         (save-excursion
5870           (set-buffer nntp-server-buffer)
5871           (goto-char (point-min))
5872           (while (and (not found) (search-forward id nil t))
5873             (beginning-of-line)
5874             (setq found (looking-at 
5875                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5876                                  (regexp-quote id))))
5877             (or found (beginning-of-line 2)))
5878           (if found
5879               (let (ref)
5880                 (beginning-of-line)
5881                 (and
5882                  (setq header (gnus-nov-parse-line 
5883                                (read (current-buffer)) deps))
5884                  (setq ref (header-references header))
5885                  (string-match "\\(<[^>]+>\\) *$" ref)
5886                  (substring ref (match-beginning 1) (match-end 1))))))
5887       (and header
5888            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5889                  gnus-newsgroup-ancient (cons (header-number header)
5890                                               gnus-newsgroup-ancient))))))
5891
5892 ;; Re-build the thread containing ID.
5893 (defun gnus-rebuild-thread (id)
5894   (let ((dep gnus-newsgroup-dependencies)
5895         (buffer-read-only nil)
5896         parent headers refs thread art)
5897     (while (and id (setq headers
5898                          (car (setq art (gnus-gethash (downcase id) dep)))))
5899       (setq parent art)
5900       (setq id (and (setq refs (header-references headers))
5901                     (string-match "\\(<[^>]+>\\) *$" refs)
5902                     (substring refs (match-beginning 1) (match-end 1)))))
5903     (setq thread (gnus-make-sub-thread (car parent)))
5904     (gnus-rebuild-remove-articles thread)
5905     (let ((beg (point)))
5906       (gnus-summary-prepare-threads (list thread) 0)
5907       (gnus-summary-update-lines beg (point)))))
5908
5909 ;; Delete all lines in the summary buffer that correspond to articles
5910 ;; in this thread.
5911 (defun gnus-rebuild-remove-articles (thread)
5912   (and (gnus-summary-goto-subject (header-number (car thread)))
5913        (gnus-delete-line))
5914   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5915
5916 (defun gnus-sort-threads (threads)
5917   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5918   (let ((fun gnus-thread-sort-functions))
5919     (while fun
5920       (setq threads (sort threads (car fun))
5921             fun (cdr fun))))
5922   threads)
5923
5924 (defun gnus-thread-header (thread)
5925   ;; Return header of first article in THREAD.
5926   (if (consp thread)
5927       (if (stringp (car thread))
5928           (car (car (cdr thread)))
5929         (car thread))
5930     thread))
5931
5932 (defun gnus-thread-sort-by-number (h1 h2)
5933   "Sort threads by root article number."
5934   (let ((h1 (gnus-thread-header h1))
5935         (h2 (gnus-thread-header h2)))
5936     (< (header-number h1) (header-number h2))))
5937
5938 (defun gnus-thread-sort-by-author (h1 h2)
5939   "Sort threads by root author."
5940   (let ((h1 (gnus-thread-header h1))
5941         (h2 (gnus-thread-header h2)))
5942     (string-lessp
5943      (let ((extract (funcall 
5944                      gnus-extract-address-components (header-from h1))))
5945        (or (car extract) (cdr extract)))
5946      (let ((extract (funcall
5947                      gnus-extract-address-components (header-from h2))))
5948        (or (car extract) (cdr extract))))))
5949
5950 (defun gnus-thread-sort-by-subject (h1 h2)
5951   "Sort threads by root subject."
5952   (let ((h1 (gnus-thread-header h1))
5953         (h2 (gnus-thread-header h2)))
5954     (string-lessp
5955      (downcase (gnus-simplify-subject (header-subject h1)))
5956      (downcase (gnus-simplify-subject (header-subject h2))))))
5957
5958 (defun gnus-thread-sort-by-date (h1 h2)
5959   "Sort threads by root article date."
5960   (let ((h1 (gnus-thread-header h1))
5961         (h2 (gnus-thread-header h2)))
5962     (string-lessp
5963      (gnus-sortable-date (header-date h1))
5964      (gnus-sortable-date (header-date h2)))))
5965
5966 (defun gnus-thread-sort-by-score (h1 h2)
5967   "Sort threads by root article score.
5968 Unscored articles will be counted as having a score of zero."
5969   (let ((h1 (gnus-thread-header h1))
5970         (h2 (gnus-thread-header h2)))
5971     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5972           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5973       (> (or (cdr s1) gnus-summary-default-score 0)
5974          (or (cdr s2) gnus-summary-default-score 0)))))
5975
5976 (defun gnus-thread-sort-by-total-score (h1 h2)
5977   "Sort threads by the sum of all scores in the thread.
5978 Unscored articles will be counted as having a score of zero."
5979   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5980
5981 (defun gnus-thread-total-score (thread)
5982   ;;  This function find the total score of THREAD.
5983   (if (consp thread)
5984       (if (stringp (car thread))
5985           (apply gnus-thread-score-function 0
5986                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5987         (gnus-thread-total-score-1 thread))
5988     (gnus-thread-total-score-1 (list thread))))
5989
5990 (defun gnus-thread-total-score-1 (root)
5991   ;; This function find the total score of the thread below ROOT.
5992   (setq root (car root))
5993   (apply gnus-thread-score-function
5994          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5995              gnus-summary-default-score 0)
5996          (mapcar 'gnus-thread-total-score
5997                  (cdr (gnus-gethash (downcase (header-id root))
5998                                     gnus-newsgroup-dependencies)))))
5999
6000 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6001 (defvar gnus-tmp-prev-subject "")
6002 (defvar gnus-tmp-adopt-thread nil)
6003
6004 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
6005 (defun gnus-summary-prepare-threads 
6006   (threads level &optional not-child no-subject cull)
6007   "Prepare summary buffer from THREADS and indentation LEVEL.  
6008 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6009 or a straight list of headers."
6010   (let (thread header number subject clevel)
6011     (while threads
6012       (setq thread (car threads)
6013             threads (cdr threads))
6014       ;; If `thread' is a cons, hierarchical threads are used.  If not,
6015       ;; `thread' is the header.
6016       (if (consp thread)
6017           (setq header (car thread))
6018         (setq header thread)
6019         (and cull
6020              (or (memq (setq number (header-number header))
6021                        gnus-newsgroup-dormant)
6022                  (and gnus-summary-expunge-below
6023                       (< (or (cdr (assq number gnus-newsgroup-scored))
6024                              gnus-summary-default-score 0)
6025                          gnus-summary-expunge-below)))
6026              (progn
6027                (setq header nil)
6028                (setq gnus-newsgroup-unreads 
6029                      (delq number gnus-newsgroup-unreads)))))
6030       (cond 
6031        ((stringp header)
6032         ;; The header is a dummy root.
6033         (cond ((eq gnus-summary-make-false-root 'adopt)
6034                ;; We let the first article adopt the rest.
6035                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6036                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6037                (setq thread (cdr (cdr thread)))
6038                (while thread
6039                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
6040                  (setq thread (cdr thread))))
6041               ((eq gnus-summary-make-false-root 'dummy)
6042                ;; We output a dummy root.
6043                (gnus-summary-insert-dummy-line 
6044                 nil header (header-number (car (car (cdr thread)))))
6045                (setq clevel 1))
6046               ((eq gnus-summary-make-false-root 'empty)
6047                ;; We print the articles with empty subject fields. 
6048                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6049                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6050                (setq thread (cdr (cdr thread)))
6051                (while thread
6052                  (gnus-summary-prepare-threads 
6053                   (list (car thread)) 0 nil
6054                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6055                             (not (string=  
6056                                   (gnus-simplify-subject-re 
6057                                    (header-subject (car (car thread))))
6058                                   (gnus-simplify-subject-re header))))))
6059                  (setq thread (cdr thread))))
6060               (t
6061                ;; We do not make a root for the gathered
6062                ;; sub-threads at all.  
6063                (setq clevel 0)))
6064         ;; Print the sub-threads.
6065         (and (consp thread) (cdr thread)
6066              (gnus-summary-prepare-threads (cdr thread) clevel)))
6067        ;; The header is a real article.
6068        (header
6069         (setq number (header-number header)
6070               subject (header-subject header))
6071         (and gnus-newsgroup-async
6072              (setq gnus-newsgroup-threads
6073                    (cons (cons (header-number header)
6074                                (header-lines header)) gnus-newsgroup-threads)))
6075         (gnus-summary-insert-line
6076          nil header level nil 
6077          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6078                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6079                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6080                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6081                (t gnus-ancient-mark))
6082          (memq number gnus-newsgroup-replied)
6083          (memq number gnus-newsgroup-expirable)
6084          (if no-subject 
6085              gnus-summary-same-subject
6086            (if (or (zerop level)
6087                    (and gnus-thread-ignore-subject
6088                         (not (string= 
6089                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6090                               (gnus-simplify-subject-re subject)))))
6091                subject
6092              gnus-summary-same-subject))
6093          not-child
6094          (cdr (assq number gnus-newsgroup-scored)))
6095         (setq gnus-tmp-prev-subject subject)
6096         ;; Recursively print subthreads.
6097         (and (consp thread) (cdr thread)
6098              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6099
6100 (defun gnus-select-newsgroup (group &optional read-all)
6101   "Select newsgroup GROUP.
6102 If READ-ALL is non-nil, all articles in the group are selected."
6103   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6104          (info (nth 2 entry))
6105          articles)
6106     (gnus-check-news-server
6107      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6108
6109     (or (gnus-server-opened gnus-current-select-method)
6110         (gnus-open-server gnus-current-select-method)
6111         (error "Couldn't open server"))
6112     
6113     (or (and (eq (car entry) t)
6114              (gnus-activate-newsgroup (car info)))
6115         (gnus-request-group group t)
6116         (progn
6117           (kill-buffer (current-buffer))
6118           (error "Couldn't request group %s: %s" 
6119                  group (gnus-status-message group))))
6120
6121     (setq gnus-newsgroup-name group)
6122     (setq gnus-newsgroup-unselected nil)
6123     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6124
6125     (and gnus-asynchronous
6126          (gnus-check-backend-function 
6127           'request-asynchronous gnus-newsgroup-name)
6128          (setq gnus-newsgroup-async
6129                (gnus-request-asynchronous gnus-newsgroup-name)))
6130
6131     (setq articles (gnus-articles-to-read group read-all))
6132
6133     (cond 
6134      ((null articles) 
6135       (gnus-message 3 "Couldn't select newsgroup")
6136       'quit)
6137      ((eq articles 0) nil)
6138      (t
6139       ;; Init the dependencies hash table.
6140       (setq gnus-newsgroup-dependencies 
6141             (gnus-make-hashtable (length articles)))
6142       ;; Retrieve the headers and read them in.
6143       (setq gnus-newsgroup-headers 
6144             (if (eq 'nov (setq gnus-headers-retrieved-by
6145                                (gnus-retrieve-headers 
6146                                 (if (and gnus-fetch-old-headers 
6147                                          (not (eq 1 (car articles))))
6148                                     (cons 1 articles)
6149                                   articles)
6150                                 gnus-newsgroup-name)))
6151                 (progn
6152                   (gnus-get-newsgroup-headers-xover articles))
6153               ;; If we were to fetch old headers, but the backend didn't
6154               ;; support XOVER, then it is possible we fetched one article
6155               ;; that we shouldn't have. If that's the case, we pop it off the
6156               ;; list of headers.
6157               (if (not gnus-fetch-old-headers)
6158                   ()
6159                 (save-excursion
6160                   (set-buffer nntp-server-buffer)
6161                   (goto-char (point-min))
6162                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6163                        (delete-region 
6164                         (point) 
6165                         (search-forward "\n.\n" nil t)))))
6166               (gnus-get-newsgroup-headers)))
6167       ;; Remove canceled articles from the list of unread articles.
6168       (setq gnus-newsgroup-unreads
6169             (gnus-set-sorted-intersection 
6170              gnus-newsgroup-unreads
6171              (mapcar (lambda (headers) (header-number headers))
6172                      gnus-newsgroup-headers)))
6173       ;; Adjust and set lists of article marks.
6174       (and info
6175            (let (marked)
6176              (gnus-adjust-marked-articles info)
6177              (setq gnus-newsgroup-marked 
6178                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6179              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6180              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6181              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6182              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6183              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6184              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6185              (setq gnus-newsgroup-processable nil)))
6186       ;; Check whether auto-expire is to be done in this group.
6187       (setq gnus-newsgroup-auto-expire
6188             (or (and (stringp gnus-auto-expirable-newsgroups)
6189                      (string-match gnus-auto-expirable-newsgroups group))
6190                 (memq 'auto-expire (nth 5 info))))
6191       ;; First and last article in this newsgroup.
6192       (and gnus-newsgroup-headers
6193            (setq gnus-newsgroup-begin 
6194                  (header-number (car gnus-newsgroup-headers)))
6195            (setq gnus-newsgroup-end
6196                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6197       (setq gnus-reffed-article-number -1)
6198       ;; GROUP is successfully selected.
6199       (or gnus-newsgroup-headers t)))))
6200
6201 (defun gnus-articles-to-read (group read-all)
6202   ;; Find out what articles the user wants to read.
6203   (let* ((articles
6204           ;; Select all articles if `read-all' is non-nil, or if all the
6205           ;; unread articles are dormant articles.
6206           (if (or (and read-all (not (numberp read-all)))
6207                   (= (length gnus-newsgroup-unreads) 
6208                      (length gnus-newsgroup-dormant)))
6209               (gnus-uncompress-range 
6210                (gnus-gethash group gnus-active-hashtb))
6211             gnus-newsgroup-unreads))
6212          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6213          (scored (length scored-list))
6214          (number (length articles))
6215          (marked (+ (length gnus-newsgroup-marked)
6216                     (length gnus-newsgroup-dormant)))
6217          (select
6218           (cond 
6219            ((numberp read-all)
6220             read-all)
6221            (t
6222             (condition-case ()
6223                 (cond ((and (or (<= scored marked)
6224                                 (= scored number))
6225                             (numberp gnus-large-newsgroup)
6226                             (> number gnus-large-newsgroup))
6227                        (let ((input
6228                               (read-string
6229                                (format
6230                                 "How many articles from %s (default %d): "
6231                                 gnus-newsgroup-name number))))
6232                          (if (string-match "^[ \t]*$" input)
6233                              number input)))
6234                       ((and (> scored marked) (< scored number))
6235                        (let ((input
6236                               (read-string
6237                                (format 
6238                                 "%s %s (%d scored, %d total): "
6239                                 "How many articles from"
6240                                 group scored number))))
6241                          (if (string-match "^[ \t]*$" input)
6242                              number input)))
6243                       (t number))
6244               (quit nil))))))
6245     (setq select (if (stringp select) (string-to-number select) select))
6246     (if (or (null select) (zerop select))
6247         select
6248       (if (and (not (zerop scored)) (<= (abs select) scored))
6249           (progn
6250             (setq articles (sort scored-list '<))
6251             (setq number (length articles)))
6252         (setq articles (copy-sequence articles)))
6253
6254       (if (< (abs select) number)
6255           (if (< select 0) 
6256               ;; Select the N oldest articles.
6257               (setcdr (nthcdr (1- (abs select)) articles) nil)
6258             ;; Select the N most recent articles.
6259             (setq articles (nthcdr (- number select) articles))))
6260       (setq gnus-newsgroup-unselected
6261             (gnus-sorted-intersection
6262              gnus-newsgroup-unreads
6263              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6264       articles)))
6265
6266 (defun gnus-killed-articles (killed articles)
6267   (let (out)
6268     (while articles
6269       (if (inline (gnus-member-of-range (car articles) killed))
6270           (setq out (cons (car articles) out)))
6271       (setq articles (cdr articles)))
6272     out))
6273
6274 (defun gnus-adjust-marked-articles (info &optional active)
6275   "Remove all marked articles that are no longer legal."
6276   (let ((marked-lists (nth 3 info))
6277         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6278         m prev)
6279     ;; There are many types of marked articles.
6280     (while marked-lists
6281       (setq m (cdr (setq prev (car marked-lists))))
6282       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6283              ;; Make sure that all ticked articles are a subset of the
6284              ;; unread/unselected articles.
6285              (while m
6286                (if (or (memq (car m) gnus-newsgroup-unreads)
6287                        (memq (car m) gnus-newsgroup-unselected))
6288                    (setq prev m)
6289                  (setcdr prev (cdr m)))
6290                (setq m (cdr m))))
6291             ((eq 'score (car prev))
6292              ;; Scored articles should be a subset of
6293              ;; unread/unselected articles. 
6294              (while m
6295                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6296                        (memq (car (car m)) gnus-newsgroup-unreads))
6297                    (setq prev m)
6298                  (setcdr prev (cdr m)))
6299                (setq m (cdr m))))
6300             ((eq 'bookmark (car prev))
6301              ;; Bookmarks should be a subset of active articles.
6302              (while m
6303                (if (< (car (car m)) (car active))
6304                    (setcdr prev (cdr m))
6305                  (setq prev m))
6306                (setq m (cdr m))))
6307             ((eq 'killed (car prev))
6308              ;; Articles that have been through the kill process are
6309              ;; to be a subset of active articles.
6310              (while (and m (< (or (and (numberp (car m)) (car m))
6311                                   (cdr (car m)))
6312                               (car active)))
6313                (setcdr prev (cdr m))
6314                (setq m (cdr m)))
6315              (if (and m (< (or (and (numberp (car m)) (car m))
6316                                (car (car m)))
6317                            (car active))) 
6318                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6319             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6320              ;; The replied and expirable articles have to be articles
6321              ;; that are active. 
6322              (while m
6323                (if (< (car m) (car active))
6324                    (setcdr prev (cdr m))
6325                  (setq prev m))
6326                (setq m (cdr m)))))
6327       (setq marked-lists (cdr marked-lists)))
6328     ;; Remove all lists that are empty.
6329     (setq marked-lists (nth 3 info))
6330     (if marked-lists
6331         (progn
6332           (while (= 1 (length (car marked-lists)))
6333             (setq marked-lists (cdr marked-lists)))
6334           (setq m (cdr (setq prev marked-lists)))
6335           (while m
6336             (if (= 1 (length (car m)))
6337                 (setcdr prev (cdr m))
6338               (setq prev m))
6339             (setq m (cdr m)))
6340           (setcar (nthcdr 3 info) marked-lists)))
6341     ;; Finally, if there are no marked lists at all left, and if there
6342     ;; are no elements after the lists in the info list, we just chop
6343     ;; the info list off before the marked lists.
6344     (and (null marked-lists) 
6345          (not (nthcdr 4 info))
6346          (setcdr (nthcdr 2 info) nil)))
6347   info)
6348
6349 (defun gnus-set-marked-articles 
6350   (info ticked replied expirable killed dormant bookmark score) 
6351   "Enter the various lists of marked articles into the newsgroup info list."
6352   (let (newmarked)
6353     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6354     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6355     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6356                                          newmarked)))
6357     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6358     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6359     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6360                                         newmarked)))
6361     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6362     (if (nthcdr 3 info)
6363         (progn
6364           (setcar (nthcdr 3 info) newmarked)
6365           (and (not newmarked)
6366                (not (nthcdr 4 info))
6367                (setcdr (nthcdr 2 info) nil)))
6368       (if newmarked
6369           (setcdr (nthcdr 2 info) (list newmarked))))))
6370
6371 (defun gnus-add-marked-articles (group type articles &optional info force)
6372   ;; Add ARTICLES of TYPE to the info of GROUP.
6373   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6374   ;; add, but replace marked articles of TYPE with ARTICLES.
6375   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6376         marked m)
6377     (or (not info)
6378         (and (not (setq marked (nthcdr 3 info)))
6379              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6380         (and (not (setq m (assq type (car marked))))
6381              (setcar marked (cons (cons type articles) (car marked))))
6382         (if force
6383             (setcdr m articles)
6384           (nconc m articles)))))
6385          
6386 (defun gnus-set-mode-line (where)
6387   "This function sets the mode line of the article or summary buffers.
6388 If WHERE is `summary', the summary mode line format will be used."
6389   (if (memq where gnus-updated-mode-lines)
6390       (let (mode-string)
6391         (save-excursion
6392           (set-buffer gnus-summary-buffer)
6393           (let* ((mformat (if (eq where 'article) 
6394                               gnus-article-mode-line-format-spec
6395                             gnus-summary-mode-line-format-spec))
6396                  (group-name gnus-newsgroup-name)
6397                  (article-number (or gnus-current-article 0))
6398                  (unread (- (length gnus-newsgroup-unreads)
6399                             (length gnus-newsgroup-dormant)))
6400                  (unread-and-unticked 
6401                   (- unread (length gnus-newsgroup-marked)))
6402                  (unselected (length gnus-newsgroup-unselected))
6403                  (unread-and-unselected
6404                   (cond ((and (zerop unread-and-unticked)
6405                               (zerop unselected)) "")
6406                         ((zerop unselected) 
6407                          (format "{%d more}" unread-and-unticked))
6408                         (t (format "{%d(+%d) more}"
6409                                    unread-and-unticked unselected))))
6410                  (subject
6411                   (if gnus-current-headers
6412                       (header-subject gnus-current-headers) ""))
6413                  (max-len (and gnus-mode-non-string-length
6414                                (- (frame-width) gnus-mode-non-string-length)))
6415                  header) ;; passed as argument to any user-format-funcs
6416             (setq mode-string (eval mformat))
6417             (or (numberp max-len)
6418                 (setq max-len (length mode-string)))
6419             (if (< max-len 4) (setq max-len 4))
6420             (if (> (length mode-string) max-len)
6421                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6422                 ;;  function `substring' might cut on a middle
6423                 ;;  of multi-octet character.
6424                 (setq mode-string 
6425                       (concat (gnus-truncate-string mode-string (- max-len 3))
6426                               "...")))
6427             (setq mode-string (format (format "%%-%ds" max-len)
6428                                       mode-string))))
6429         (setq mode-line-buffer-identification mode-string)
6430         (set-buffer-modified-p t))))
6431
6432 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6433   "Go through the HEADERS list and add all Xrefs to a hash table.
6434 The resulting hash table is returned, or nil if no Xrefs were found."
6435   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6436          (prefix (if (and 
6437                       (gnus-group-foreign-p from-newsgroup)
6438                       (not (memq 'virtual 
6439                                  (assoc (symbol-name (car from-method))
6440                                         gnus-valid-select-methods))))
6441                      (gnus-group-real-prefix from-newsgroup)))
6442          (xref-hashtb (make-vector 63 0))
6443          start group entry number xrefs header)
6444     (while headers
6445       (setq header (car headers))
6446       (if (and (setq xrefs (header-xref header))
6447                (not (memq (header-number header) unreads)))
6448           (progn
6449             (setq start 0)
6450             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6451               (setq start (match-end 0))
6452               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6453                                                     (match-end 1))))
6454               (setq number 
6455                     (string-to-int (substring xrefs (match-beginning 2) 
6456                                               (match-end 2))))
6457               (if (setq entry (gnus-gethash group xref-hashtb))
6458                   (setcdr entry (cons number (cdr entry)))
6459                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6460       (setq headers (cdr headers)))
6461     (if start xref-hashtb nil)))
6462
6463 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6464   "Look through all the headers and mark the Xrefs as read."
6465   (let ((virtual (memq 'virtual 
6466                        (assoc (symbol-name (car (gnus-find-method-for-group 
6467                                                  from-newsgroup)))
6468                               gnus-valid-select-methods)))
6469         name entry info xref-hashtb idlist method
6470         nth4)
6471     (save-excursion
6472       (set-buffer gnus-group-buffer)
6473       (if (setq xref-hashtb 
6474                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6475           (mapatoms 
6476            (lambda (group)
6477              (if (string= from-newsgroup (setq name (symbol-name group)))
6478                  ()
6479                (setq idlist (symbol-value group))
6480                ;; Dead groups are not updated.
6481                (if (and (prog1 
6482                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6483                                   info (nth 2 entry))
6484                           (if (stringp (setq nth4 (nth 4 info)))
6485                               (setq nth4 (gnus-server-to-method nth4))))
6486                         ;; Only do the xrefs if the group has the same
6487                         ;; select method as the group we have just read.
6488                         (or (gnus-methods-equal-p 
6489                              nth4 (gnus-find-method-for-group from-newsgroup))
6490                             virtual
6491                             (equal nth4 
6492                                    (setq method (gnus-find-method-for-group 
6493                                                  from-newsgroup)))
6494                             (and (equal (car nth4) (car method))
6495                                  (equal (nth 1 nth4) (nth 1 method))))
6496                         gnus-use-cross-reference
6497                         (or (not (eq gnus-use-cross-reference t))
6498                             virtual
6499                             ;; Only do cross-references on subscribed
6500                             ;; groups, if that is what is wanted.  
6501                             (<= (nth 1 info) gnus-level-subscribed)))
6502                    (gnus-group-make-articles-read name idlist expirable))))
6503            xref-hashtb)))))
6504
6505 (defun gnus-group-make-articles-read (group articles expirable)
6506   (let* ((num 0)
6507          (entry (gnus-gethash group gnus-newsrc-hashtb))
6508          (info (nth 2 entry))
6509          (active (gnus-gethash group gnus-active-hashtb))
6510          exps expirable range)
6511     ;; First peel off all illegal article numbers.
6512     (if active
6513         (let ((ids articles)
6514               (ticked (cdr (assq 'tick (nth 3 info))))
6515               (dormant (cdr (assq 'dormant (nth 3 info))))
6516               id)
6517           (setq exps nil)
6518           (while ids
6519             (setq id (car ids))
6520             (if (or (> id (cdr active))
6521                     (< id (car active))
6522                     (memq id ticked)
6523                     (memq id dormant))
6524                 (setq articles (delq id articles)))
6525             (and (memq id expirable)
6526                  (setq exps (cons id exps)))
6527             (setq ids (cdr ids)))))
6528     ;; Update expirable articles.
6529     (gnus-add-marked-articles nil 'expirable exps info)
6530     (and active
6531          (null (nth 2 info))
6532          (> (car active) 1)
6533          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6534     (setcar (nthcdr 2 info)
6535             (setq range
6536                   (gnus-add-to-range 
6537                    (nth 2 info) 
6538                    (setq articles (sort articles '<)))))
6539     ;; Then we have to re-compute how many unread
6540     ;; articles there are in this group.
6541     (if active
6542         (progn
6543           (cond 
6544            ((not range)
6545             (setq num (- (1+ (cdr active)) (car active))))
6546            ((not (listp (cdr range)))
6547             (setq num (- (cdr active) (- (1+ (cdr range)) 
6548                                          (car range)))))
6549            (t
6550             (while range
6551               (if (numberp (car range))
6552                   (setq num (1+ num))
6553                 (setq num (+ num (- (1+ (cdr (car range)))
6554                                     (car (car range))))))
6555               (setq range (cdr range)))
6556             (setq num (- (cdr active) num))))
6557           ;; Update the number of unread articles.
6558           (setcar 
6559            entry 
6560            (max 0 (- num 
6561                      (length (cdr (assq 'tick (nth 3 info))))
6562                      (length 
6563                       (cdr (assq 'dormant (nth 3 info)))))))
6564           ;; Update the group buffer.
6565           (gnus-group-update-group group t)))))
6566
6567 (defun gnus-methods-equal-p (m1 m2)
6568   (let ((m1 (or m1 gnus-select-method))
6569         (m2 (or m2 gnus-select-method)))
6570     (or (equal m1 m2)
6571         (and (eq (car m1) (car m2))
6572              (or (not (memq 'address (assoc (symbol-name (car m1))
6573                                             gnus-valid-select-methods)))
6574                  (equal (nth 1 m1) (nth 1 m2)))))))
6575
6576 (defsubst gnus-header-value ()
6577   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6578
6579 (defvar gnus-newsgroup-none-id 0)
6580
6581 (defun gnus-get-newsgroup-headers ()
6582   (setq gnus-article-internal-prepare-hook nil)
6583   (let ((cur nntp-server-buffer)
6584         (dependencies gnus-newsgroup-dependencies)
6585         headers id dep end ref)
6586     (save-excursion
6587       (set-buffer nntp-server-buffer)
6588       (goto-char (point-min))
6589       ;; Search to the beginning of the next header. Error messages
6590       ;; do not begin with 2 or 3.
6591       (while (re-search-forward "^[23][0-9]+ " nil t)
6592         (let ((header (make-vector 9 nil))
6593               (case-fold-search t)
6594               (p (point))
6595               in-reply-to)
6596           (setq id nil
6597                 ref nil)
6598           (header-set-number header (read cur))
6599           ;; This implementation of this function, with nine
6600           ;; search-forwards instead of the one re-search-forward and
6601           ;; a case (which basically was the old function) is actually
6602           ;; about twice as fast, even though it looks messier. You
6603           ;; can't have everything, I guess. Speed and elegance
6604           ;; doesn't always come hand in hand.
6605           (save-restriction
6606             (narrow-to-region (point) (or (save-excursion 
6607                                             (search-forward "\n.\n" nil t))
6608                                           (point)))
6609             (if (search-forward "\nfrom: " nil t)
6610                 (header-set-from header (gnus-header-value))
6611               (header-set-from header "(nobody)"))
6612             (goto-char p)
6613             (if (search-forward "\nsubject: " nil t)
6614                 (header-set-subject header (gnus-header-value))
6615               (header-set-subject header "(none)"))
6616             (goto-char p)
6617             (and (search-forward "\nxref: " nil t)
6618                  (header-set-xref header (gnus-header-value)))
6619             (goto-char p)
6620             (or (numberp (and (search-forward "\nlines: " nil t)
6621                               (header-set-lines header (read cur))))
6622                 (header-set-lines header 0))
6623             (goto-char p)
6624             (and (search-forward "\ndate: " nil t)
6625                  (header-set-date header (gnus-header-value)))
6626             (goto-char p)
6627             (if (search-forward "\nmessage-id: " nil t)
6628                 (header-set-id header (setq id (gnus-header-value)))
6629               ;; If there was no message-id, we just fake one to make
6630               ;; subsequent routines simpler.
6631               (header-set-id 
6632                header 
6633                (setq id (concat "none+" 
6634                                 (int-to-string 
6635                                  (setq gnus-newsgroup-none-id 
6636                                        (1+ gnus-newsgroup-none-id)))))))
6637             (goto-char p)
6638             (if (search-forward "\nreferences: " nil t)
6639                 (progn
6640                   (header-set-references header (gnus-header-value))
6641                   (setq end (match-end 0))
6642                   (save-excursion
6643                     (setq ref 
6644                           (downcase
6645                            (buffer-substring
6646                             (progn 
6647                               (end-of-line)
6648                               (search-backward ">" end t)
6649                               (1+ (point)))
6650                             (progn
6651                               (search-backward "<" end t)
6652                               (point)))))))
6653               ;; Get the references from the in-reply-to header if there
6654               ;; ware no references and the in-reply-to header looks
6655               ;; promising. 
6656               (if (and (search-forward "\nin-reply-to: " nil t)
6657                        (setq in-reply-to (gnus-header-value))
6658                        (string-match "<[^>]+>" in-reply-to))
6659                   (progn
6660                     (header-set-references 
6661                      header 
6662                      (setq ref (substring in-reply-to (match-beginning 0)
6663                                           (match-end 0))))
6664                     (setq ref (downcase ref)))
6665                 (setq ref "none")))
6666             ;; We do some threading while we read the headers. The
6667             ;; message-id and the last reference are both entered into
6668             ;; the same hash table. Some tippy-toeing around has to be
6669             ;; done in case an article has arrived before the article
6670             ;; which it refers to.
6671             (if (boundp (setq dep (intern (downcase id) dependencies)))
6672                 (if (car (symbol-value dep))
6673                     ;; An article with this Message-ID has already
6674                     ;; been seen, so we ignore this one, except we add
6675                     ;; any additional Xrefs (in case the two articles
6676                     ;; came from different servers.
6677                     (progn
6678                       (header-set-xref 
6679                        (car (symbol-value dep))
6680                        (concat (or (header-xref (car (symbol-value dep))) "")
6681                                (or (header-xref header) "")))
6682                       (setq header nil))
6683                   (setcar (symbol-value dep) header))
6684               (set dep (list header)))
6685             (if header
6686                 (progn
6687                   (if (boundp (setq dep (intern ref dependencies)))
6688                       (setcdr (symbol-value dep) 
6689                               (cons header (cdr (symbol-value dep))))
6690                     (set dep (list nil header)))
6691                   (setq headers (cons header headers))))
6692             (goto-char (point-max))))))
6693     (nreverse headers)))
6694
6695 ;; The following macros and functions were written by Felix Lee
6696 ;; <flee@cse.psu.edu>. 
6697
6698 (defmacro gnus-nov-read-integer ()
6699   '(prog1
6700        (if (= (following-char) ?\t)
6701            0
6702          (let ((num (condition-case nil (read buffer) (error nil))))
6703            (if (numberp num) num 0)))
6704      (or (eobp) (forward-char 1))))
6705
6706 (defmacro gnus-nov-skip-field ()
6707   '(search-forward "\t" eol 'move))
6708
6709 (defmacro gnus-nov-field ()
6710   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6711
6712 ;; Goes through the xover lines and returns a list of vectors
6713 (defun gnus-get-newsgroup-headers-xover (sequence)
6714   "Parse the news overview data in the server buffer, and return a
6715 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6716   ;; Get the Xref when the users reads the articles since most/some
6717   ;; NNTP servers do not include Xrefs when using XOVER.
6718   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6719   (let ((cur nntp-server-buffer)
6720         (dependencies gnus-newsgroup-dependencies)
6721         number headers header)
6722     (save-excursion
6723       (set-buffer nntp-server-buffer)
6724       (goto-char (point-min))
6725       (while (and sequence (not (eobp)))
6726         (setq number (read cur))
6727         (while (and sequence (< (car sequence) number))
6728           (setq sequence (cdr sequence)))
6729         (and sequence 
6730              (eq number (car sequence))
6731              (progn
6732                (setq sequence (cdr sequence))
6733                (if (setq header 
6734                          (inline (gnus-nov-parse-line number dependencies)))
6735                    (setq headers (cons header headers)))))
6736         (forward-line 1))
6737       (setq headers (nreverse headers)))
6738     headers))
6739
6740 ;; This function has to be called with point after the article number
6741 ;; on the beginning of the line.
6742 (defun gnus-nov-parse-line (number dependencies)
6743   (let ((none 0)
6744         (eol (gnus-point-at-eol)) 
6745         (buffer (current-buffer))
6746         header ref id dep)
6747
6748     ;; overview: [num subject from date id refs chars lines misc]
6749     (narrow-to-region (point) eol)
6750     (forward-char)
6751
6752     (condition-case nil
6753         (setq header
6754               (vector 
6755                number                   ; number
6756                (gnus-nov-field)         ; subject
6757                (gnus-nov-field)         ; from
6758                (gnus-nov-field)         ; date
6759                (setq id (or (gnus-nov-field)
6760                             (concat "none+"
6761                                     (int-to-string 
6762                                      (setq none (1+ none)))))) ; id
6763                (progn
6764                  (save-excursion
6765                    (let ((beg (point)))
6766                      (search-forward "\t" eol)
6767                      (if (search-backward ">" beg t)
6768                          (setq ref 
6769                                (downcase 
6770                                 (buffer-substring 
6771                                  (1+ (point))
6772                                  (progn
6773                                    (search-backward "<" beg t)
6774                                    (point)))))
6775                        (setq ref nil))))
6776                  (gnus-nov-field))      ; refs
6777                (gnus-nov-read-integer)  ; chars
6778                (gnus-nov-read-integer)  ; lines
6779                (if (= (following-char) ?\n)
6780                    nil
6781                  (gnus-nov-field))      ; misc
6782                ))
6783       (error (progn 
6784                (ding)
6785                (message "Strange nov line.")
6786                (setq header nil)
6787                (goto-char eol))))
6788
6789     (widen)
6790
6791     ;; We build the thread tree.
6792     (and header
6793          (if (boundp (setq dep (intern (downcase id) dependencies)))
6794              (if (car (symbol-value dep))
6795                  ;; An article with this Message-ID has already been seen,
6796                  ;; so we ignore this one, except we add any additional
6797                  ;; Xrefs (in case the two articles came from different
6798                  ;; servers.
6799                  (progn
6800                    (header-set-xref 
6801                     (car (symbol-value dep))
6802                     (concat (or (header-xref (car (symbol-value dep))) "")
6803                             (or (header-xref header) "")))
6804                    (setq header nil))
6805                (setcar (symbol-value dep) header))
6806            (set dep (list header))))
6807     (if header
6808         (progn
6809           (if (boundp (setq dep (intern (or ref "none") 
6810                                         dependencies)))
6811               (setcdr (symbol-value dep) 
6812                       (cons header (cdr (symbol-value dep))))
6813             (set dep (list nil header)))))
6814     header))
6815
6816 (defun gnus-article-get-xrefs ()
6817   "Fill in the Xref value in `gnus-current-headers', if necessary.
6818 This is meant to be called in `gnus-article-internal-prepare-hook'."
6819   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6820                                  gnus-current-headers)))
6821     (or (not gnus-use-cross-reference)
6822         (not headers)
6823         (and (header-xref headers)
6824              (not (string= (header-xref headers) "")))
6825         (let ((case-fold-search t)
6826               xref)
6827           (save-restriction
6828             (gnus-narrow-to-headers)
6829             (goto-char (point-min))
6830             (if (or (and (eq (downcase (following-char)) ?x)
6831                          (looking-at "Xref:"))
6832                     (search-forward "\nXref:" nil t))
6833                 (progn
6834                   (goto-char (1+ (match-end 0)))
6835                   (setq xref (buffer-substring (point) 
6836                                                (progn (end-of-line) (point))))
6837                   (header-set-xref headers xref))))))))
6838
6839 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6840 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6841
6842 ;; Return a header specified by a NUMBER.
6843 (defun gnus-get-header-by-number (number)
6844   (save-excursion
6845     (set-buffer gnus-summary-buffer)
6846     (or gnus-newsgroup-headers-hashtb-by-number
6847         (gnus-make-headers-hashtable-by-number))
6848     (gnus-gethash (int-to-string number)
6849                   gnus-newsgroup-headers-hashtb-by-number)))
6850
6851 (defun gnus-make-headers-hashtable-by-number ()
6852   "Make hashtable for the variable gnus-newsgroup-headers by number."
6853   (save-excursion
6854     (set-buffer gnus-summary-buffer)
6855     (let ((headers gnus-newsgroup-headers)
6856           header)
6857       (setq gnus-newsgroup-headers-hashtb-by-number
6858             (gnus-make-hashtable (length headers)))
6859       (while headers
6860         (setq header (car headers))
6861         (gnus-sethash (int-to-string (header-number header))
6862                       header gnus-newsgroup-headers-hashtb-by-number)
6863         (setq headers (cdr headers))))))
6864
6865 (defun gnus-more-header-backward ()
6866   "Find new header backward."
6867   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6868         (artnum gnus-newsgroup-begin)
6869         (header nil))
6870     (while (and (not header)
6871                 (> artnum first))
6872       (setq artnum (1- artnum))
6873       (setq header (gnus-read-header artnum)))
6874     header))
6875
6876 (defun gnus-more-header-forward (&optional backward)
6877   "Find new header forward.
6878 If BACKWARD, find new header backward instead."
6879   (if backward
6880       (gnus-more-header-backward)
6881     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6882           (artnum gnus-newsgroup-end)
6883           (header nil))
6884       (while (and (not header)
6885                   (< artnum last))
6886         (setq artnum (1+ artnum))
6887         (setq header (gnus-read-header artnum)))
6888       header)))
6889
6890 (defun gnus-extend-newsgroup (header &optional backward)
6891   "Extend newsgroup selection with HEADER.
6892 Optional argument BACKWARD means extend toward backward."
6893   (if header
6894       (let ((artnum (header-number header)))
6895         (setq gnus-newsgroup-headers
6896               (if backward
6897                   (cons header gnus-newsgroup-headers)
6898                 (nconc gnus-newsgroup-headers (list header))))
6899         (setq gnus-newsgroup-unselected
6900               (delq artnum gnus-newsgroup-unselected))
6901         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6902         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6903
6904 (defun gnus-summary-work-articles (n)
6905   "Return a list of articles to be worked upon. The prefix argument,
6906 the list of process marked articles, and the current article will be
6907 taken into consideration."
6908   (let (articles)
6909     (if (and n (numberp n))
6910         (let ((backward (< n 0))
6911               (n (abs n)))
6912           (save-excursion
6913             (while (and (> n 0)
6914                         (setq articles (cons (gnus-summary-article-number) 
6915                                              articles))
6916                         (gnus-summary-search-forward nil nil backward))
6917               (setq n (1- n))))
6918           (sort articles (function <)))
6919       (or (reverse gnus-newsgroup-processable)
6920           (list (gnus-summary-article-number))))))
6921
6922 (defun gnus-summary-search-group (&optional backward use-level)
6923   "Search for next unread newsgroup.
6924 If optional argument BACKWARD is non-nil, search backward instead."
6925   (save-excursion
6926     (set-buffer gnus-group-buffer)
6927     (if (gnus-group-search-forward 
6928          backward nil (if use-level (gnus-group-group-level) nil))
6929         (gnus-group-group-name))))
6930
6931 (defun gnus-summary-best-group (&optional exclude-group)
6932   "Find the name of the best unread group.
6933 If EXCLUDE-GROUP, do not go to this group."
6934   (save-excursion
6935     (set-buffer gnus-group-buffer)
6936     (save-excursion
6937       (gnus-group-best-unread-group exclude-group))))
6938
6939 (defun gnus-summary-search-subject (&optional backward unread subject)
6940   "Search for article forward.
6941 If BACKWARD is non-nil, search backward.
6942 If UNREAD is non-nil, only unread articles are selected.
6943 If SUBJECT is non-nil, the article which has the same subject will be
6944 searched for." 
6945   (let ((func (if backward 'previous-single-property-change
6946                 'next-single-property-change))
6947         (beg (point))
6948         (did t)
6949         pos psubject)
6950     (beginning-of-line)
6951     (and gnus-summary-check-current unread
6952          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
6953          (setq did nil))
6954     (if (not did)
6955         ()
6956       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6957       (while
6958           (and 
6959            (setq pos (funcall func (point) 'gnus-number))
6960            (goto-char (if backward (1- pos) pos))
6961            (setq did
6962                  (not (and
6963                        (or (not unread)
6964                            (eq (get-text-property (point) 'gnus-mark)
6965                                gnus-unread-mark))
6966                        (or (not subject)
6967                            (and (setq psubject (gnus-summary-subject-string))
6968                                 (gnus-subject-eq subject psubject))))))
6969            (if backward (if (bobp) nil (forward-char -1) t)
6970              (if (eobp) nil (forward-char 1) t)))))
6971     (if did
6972         (progn (goto-char beg) nil)
6973       (prog1
6974           (get-text-property (point) 'gnus-number)
6975         (gnus-summary-position-cursor)))))
6976
6977 (defun gnus-subject-eq (s1 s2)
6978   (cond
6979    ((null gnus-summary-gather-subject-limit)
6980     (equal (gnus-simplify-subject-re s1)
6981            (gnus-simplify-subject-re s2)))
6982    ((eq gnus-summary-gather-subject-limit 'fuzzy)
6983     (equal (gnus-simplify-subject-fuzzy s1)
6984            (gnus-simplify-subject-fuzzy s2)))
6985    ((numberp gnus-summary-gather-subject-limit)
6986     (equal (substring s1 gnus-summary-gather-subject-limit)
6987            (substring s2 gnus-summary-gather-subject-limit)))
6988    (t
6989     (equal s1 s2))))
6990     
6991 (defun gnus-summary-search-forward (&optional unread subject backward)
6992   "Search for article forward.
6993 If UNREAD is non-nil, only unread articles are selected.
6994 If SUBJECT is non-nil, the article which has the same subject will be
6995 searched for. 
6996 If BACKWARD is non-nil, the search will be performed backwards instead."
6997   (gnus-summary-search-subject backward unread subject))
6998
6999 (defun gnus-summary-search-backward (&optional unread subject)
7000   "Search for article backward.
7001 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7002 If 2nd optional argument SUBJECT is non-nil, the article which has
7003 the same subject will be searched for."
7004   (gnus-summary-search-forward unread subject t))
7005
7006 (defun gnus-summary-article-number (&optional number-or-nil)
7007   "The article number of the article on the current line.
7008 If there isn's an article number here, then we return the current
7009 article number."
7010   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7011     (if number-or-nil number (or number gnus-current-article))))
7012
7013 (defun gnus-summary-thread-level ()
7014   "The thread level of the article on the current line."
7015   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7016       0))
7017
7018 (defun gnus-summary-pseudo-article ()
7019   "The thread level of the article on the current line."
7020   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7021
7022 (defun gnus-summary-article-mark ()
7023   "The mark on the current line."
7024   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7025
7026 (defun gnus-summary-subject-string ()
7027   "Return current subject string or nil if nothing."
7028   (let ((article (gnus-summary-article-number))
7029         header)
7030     (and article 
7031          (setq header (gnus-get-header-by-number article))
7032          (vectorp header)
7033          (header-subject header))))
7034
7035 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7036 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7037 (defun gnus-summary-article-score ()
7038   "Return current article score."
7039   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7040       gnus-summary-default-score 0))
7041
7042 (defun gnus-summary-recenter ()
7043   "Center point in the summary window.
7044 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7045 displayed, no centering will be performed." 
7046   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7047   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7048   (let* ((top (cond ((< (window-height) 4) 0)
7049                     ((< (window-height) 7) 1)
7050                     (t 2)))
7051          (height (1- (window-height)))
7052          (bottom (save-excursion (goto-char (point-max))
7053                                  (forward-line (- height))
7054                                  (point)))
7055          (window (get-buffer-window (current-buffer))))
7056     (and 
7057      ;; The user has to want it,
7058      gnus-auto-center-summary 
7059      ;; the article buffer must be displayed,
7060      (get-buffer-window gnus-article-buffer)
7061      ;; Set the window start to either `bottom', which is the biggest
7062      ;; possible valid number, or the second line from the top,
7063      ;; whichever is the least.
7064      (set-window-start
7065       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7066
7067 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7068 (defun gnus-short-group-name (group &optional levels)
7069   "Collapse GROUP name LEVELS."
7070   (let* ((name "") (foreign "") (depth -1) (skip 1)
7071          (levels (or levels
7072                      (progn
7073                        (while (string-match "\\." group skip)
7074                          (setq skip (match-end 0)
7075                                depth (+ depth 1)))
7076                        depth))))
7077     (if (string-match ":" group)
7078         (setq foreign (substring group 0 (match-end 0))
7079               group (substring group (match-end 0))))
7080     (while group
7081       (if (and (string-match "\\." group) (> levels 0))
7082           (setq name (concat name (substring group 0 1))
7083                 group (substring group (match-end 0))
7084                 levels (- levels 1)
7085                 name (concat name "."))
7086         (setq name (concat foreign name group)
7087               group nil)))
7088     name))
7089
7090 (defun gnus-summary-jump-to-group (newsgroup)
7091   "Move point to NEWSGROUP in group mode buffer."
7092   ;; Keep update point of group mode buffer if visible.
7093   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7094       (save-window-excursion
7095         ;; Take care of tree window mode.
7096         (if (get-buffer-window gnus-group-buffer)
7097             (pop-to-buffer gnus-group-buffer))
7098         (gnus-group-jump-to-group newsgroup))
7099     (save-excursion
7100       ;; Take care of tree window mode.
7101       (if (get-buffer-window gnus-group-buffer)
7102           (pop-to-buffer gnus-group-buffer)
7103         (set-buffer gnus-group-buffer))
7104       (gnus-group-jump-to-group newsgroup))))
7105
7106 ;; This function returns a list of article numbers based on the
7107 ;; difference between the ranges of read articles in this group and
7108 ;; the range of active articles.
7109 (defun gnus-list-of-unread-articles (group)
7110   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7111          (active (gnus-gethash group gnus-active-hashtb))
7112          (last (cdr active))
7113          first nlast unread)
7114     ;; If none are read, then all are unread. 
7115     (if (not read)
7116         (setq first (car active))
7117       ;; If the range of read articles is a single range, then the
7118       ;; first unread article is the article after the last read
7119       ;; article. Sounds logical, doesn't it?
7120       (if (not (listp (cdr read)))
7121           (setq first (1+ (cdr read)))
7122         ;; `read' is a list of ranges.
7123         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7124                                 (car (car read)))) 1)
7125             (setq first 1))
7126         (while read
7127           (if first 
7128               (while (< first nlast)
7129                 (setq unread (cons first unread))
7130                 (setq first (1+ first))))
7131           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7132           (setq nlast (if (atom (car (cdr read))) 
7133                           (car (cdr read))
7134                         (car (car (cdr read)))))
7135           (setq read (cdr read)))))
7136     ;; And add the last unread articles.
7137     (while (<= first last)
7138       (setq unread (cons first unread))
7139       (setq first (1+ first)))
7140     ;; Return the list of unread articles.
7141     (nreverse unread)))
7142
7143 (defun gnus-list-of-read-articles (group)
7144   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7145         (active (gnus-gethash group gnus-active-hashtb)))
7146     (and info active
7147          (gnus-sorted-complement 
7148           (gnus-uncompress-range active) 
7149           (gnus-list-of-unread-articles group)))))
7150
7151 ;; Various summary commands
7152
7153 (defun gnus-summary-universal-argument ()
7154   "Perform any operation on all articles marked with the process mark."
7155   (interactive)
7156   (gnus-set-global-variables)
7157   (let ((articles (reverse gnus-newsgroup-processable))
7158         func)
7159     (or articles (error "No articles marked"))
7160     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7161         (error "Undefined key"))
7162     (while articles
7163       (gnus-summary-goto-subject (car articles))
7164       (command-execute func)
7165       (gnus-summary-remove-process-mark (car articles))
7166       (setq articles (cdr articles)))))
7167
7168 (defun gnus-summary-toggle-truncation (arg)
7169   "Toggle truncation of summary lines.
7170 With arg, turn line truncation on iff arg is positive."
7171   (interactive "P")
7172   (setq truncate-lines
7173         (if (null arg) (not truncate-lines)
7174           (> (prefix-numeric-value arg) 0)))
7175   (redraw-display))
7176
7177 (defun gnus-summary-reselect-current-group (all)
7178   "Once exit and then reselect the current newsgroup.
7179 The prefix argument ALL means to select all articles."
7180   (interactive "P")
7181   (gnus-set-global-variables)
7182   (let ((current-subject (gnus-summary-article-number))
7183         (group gnus-newsgroup-name))
7184     (setq gnus-newsgroup-begin nil)
7185     (gnus-summary-exit t)
7186     ;; We have to adjust the point of group mode buffer because the
7187     ;; current point was moved to the next unread newsgroup by
7188     ;; exiting.
7189     (gnus-summary-jump-to-group group)
7190     (gnus-group-read-group all t)
7191     (gnus-summary-goto-subject current-subject)))
7192
7193 (defun gnus-summary-rescan-group (all)
7194   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7195   (interactive "P")
7196   (gnus-set-global-variables)
7197   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7198   (let ((group gnus-newsgroup-name))
7199     (gnus-summary-exit)
7200     (gnus-summary-jump-to-group group)
7201     (save-excursion
7202       (set-buffer gnus-group-buffer)
7203       (gnus-group-get-new-news-this-group 1))
7204     (gnus-summary-jump-to-group group)
7205     (gnus-group-read-group all)))
7206
7207 (defun gnus-summary-update-info ()
7208   (let* ((group gnus-newsgroup-name))
7209     (if gnus-newsgroup-kill-headers
7210         (setq gnus-newsgroup-killed
7211               (gnus-compress-sequence
7212                (nconc
7213                 (gnus-set-sorted-intersection
7214                  (gnus-uncompress-range gnus-newsgroup-killed)
7215                  (setq gnus-newsgroup-unselected
7216                        (sort gnus-newsgroup-unselected '<)))
7217                 (setq gnus-newsgroup-unreads
7218                       (sort gnus-newsgroup-unreads '<))) t)))
7219     (or (listp (cdr gnus-newsgroup-killed))
7220         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7221     (let ((headers gnus-newsgroup-headers))
7222       (gnus-close-group group)
7223       (run-hooks 'gnus-exit-group-hook)
7224       (gnus-update-read-articles 
7225        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7226        gnus-newsgroup-marked
7227        t gnus-newsgroup-replied gnus-newsgroup-expirable
7228        gnus-newsgroup-killed gnus-newsgroup-dormant
7229        gnus-newsgroup-bookmarks 
7230        (and gnus-save-score gnus-newsgroup-scored))
7231       (and gnus-use-cross-reference
7232            (gnus-mark-xrefs-as-read 
7233             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7234       ;; Do adaptive scoring, and possibly save score files.
7235       (and gnus-newsgroup-adaptive
7236            (gnus-score-adaptive))
7237       (and gnus-use-scoring 
7238            (fboundp 'gnus-score-save)
7239            (funcall 'gnus-score-save))
7240       ;; Do not switch windows but change the buffer to work.
7241       (set-buffer gnus-group-buffer)
7242       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7243           (gnus-group-update-group group)))))
7244   
7245 (defun gnus-summary-exit (&optional temporary)
7246   "Exit reading current newsgroup, and then return to group selection mode.
7247 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7248   (interactive)
7249   (gnus-set-global-variables)
7250   (gnus-kill-save-kill-buffer)
7251   (let* ((group gnus-newsgroup-name)
7252          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7253                                                   gnus-newsgroup-name))))
7254          (mode major-mode)
7255          (buf (current-buffer)))
7256     (gnus-summary-update-info) ; Make all changes in this group permanent.
7257     ;; Make sure where I was, and go to next newsgroup.
7258     (or quit-config
7259         (progn
7260           (gnus-group-jump-to-group group)
7261           (gnus-group-next-unread-group 1)))
7262     (if temporary
7263         nil                             ;Nothing to do.
7264       (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7265       ;; We set all buffer-local variables to nil. It is unclear why
7266       ;; this is needed, but if we don't, buffer-local variables are
7267       ;; not garbage-collected, it seems. This would the lead to en
7268       ;; ever-growing Emacs.
7269       (set-buffer buf)
7270       (gnus-summary-clear-local-variables)
7271       ;; We clear the global counterparts of the buffer-local
7272       ;; variables as well, just to be on the safe side.
7273       (gnus-configure-windows 'group)
7274       (gnus-summary-clear-local-variables)
7275       ;; Return to group mode buffer. 
7276       (if (eq mode 'gnus-summary-mode)
7277           (gnus-kill-buffer buf))
7278       (if (get-buffer gnus-article-buffer)
7279           (bury-buffer gnus-article-buffer))
7280       (setq gnus-current-select-method gnus-select-method)
7281       (pop-to-buffer gnus-group-buffer)
7282       (if (not quit-config)
7283           (progn
7284             (gnus-group-jump-to-group group)
7285             (gnus-group-next-unread-group 1))
7286         (if (not (buffer-name (car quit-config)))
7287             (gnus-configure-windows 'group)
7288           (set-buffer (car quit-config))
7289           (and (eq major-mode 'gnus-summary-mode)
7290                (gnus-set-global-variables))
7291           (gnus-configure-windows (cdr quit-config)))))))
7292
7293 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7294 (defun gnus-summary-exit-no-update (&optional no-questions)
7295   "Quit reading current newsgroup without updating read article info."
7296   (interactive)
7297   (let* ((group gnus-newsgroup-name)
7298          (quit-config (nth 1 (assoc 'quit-config 
7299                                     (gnus-find-method-for-group group)))))
7300     (if (or no-questions
7301             gnus-expert-user
7302             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7303         (progn
7304           (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7305           (gnus-close-group group)
7306           (gnus-summary-clear-local-variables)
7307           (set-buffer gnus-group-buffer)
7308           (gnus-summary-clear-local-variables)
7309           ;; Return to group selection mode.
7310           (gnus-configure-windows 'group)
7311           (if (get-buffer gnus-summary-buffer)
7312               (kill-buffer gnus-summary-buffer))
7313           (if (get-buffer gnus-article-buffer)
7314               (bury-buffer gnus-article-buffer))
7315           (if (equal (gnus-group-group-name) group)
7316               (gnus-group-next-unread-group 1))
7317           (if quit-config
7318               (progn
7319                 (if (not (buffer-name (car quit-config)))
7320                     (gnus-configure-windows 'group)
7321                   (set-buffer (car quit-config))
7322                   (and (eq major-mode 'gnus-summary-mode)
7323                        (gnus-set-global-variables))
7324                   (gnus-configure-windows (cdr quit-config)))))))))
7325
7326 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7327 (defun gnus-summary-fetch-faq (group)
7328   "Fetch the FAQ for the current group."
7329   (interactive (list gnus-newsgroup-name))
7330   (gnus-configure-windows 'summary-faq)
7331   (find-file (concat gnus-group-faq-directory group)))
7332
7333 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7334 (defun gnus-summary-describe-group (force)
7335   "Describe the current newsgroup."
7336   (interactive "P")
7337   (gnus-group-describe-group force gnus-newsgroup-name))
7338
7339 (defun gnus-summary-describe-briefly ()
7340   "Describe summary mode commands briefly."
7341   (interactive)
7342   (gnus-message 6
7343     (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")))
7344
7345 ;; Walking around group mode buffer from summary mode.
7346
7347 (defun gnus-summary-next-group (&optional no-article group backward)
7348   "Exit current newsgroup and then select next unread newsgroup.
7349 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7350 If BACKWARD, go to previous group instead."
7351   (interactive "P")
7352   (gnus-set-global-variables)
7353   (let ((ingroup gnus-newsgroup-name)
7354         (sumbuf (current-buffer))
7355         num)
7356     (set-buffer gnus-group-buffer)
7357     (if (and group
7358              (or (and (numberp (setq num (car (gnus-gethash
7359                                                group gnus-newsrc-hashtb))))
7360                       (< num 1))
7361                  (null num)))
7362         (progn
7363           (gnus-group-jump-to-group group)
7364           (setq group nil))
7365       (gnus-group-jump-to-group ingroup))
7366     (gnus-summary-search-group backward)
7367     (let ((group (or group (gnus-summary-search-group backward))))
7368       (set-buffer sumbuf)
7369       (gnus-summary-exit t)             ;Update all information.
7370       (if (null group)
7371           (gnus-summary-exit-no-update t)
7372         (gnus-group-jump-to-group ingroup)
7373         (setq group (gnus-summary-search-group backward))
7374         (gnus-message 5 "Selecting %s..." group)
7375         (set-buffer gnus-group-buffer)
7376         ;; We are now in group mode buffer.
7377         ;; Make sure group mode buffer point is on GROUP.
7378         (gnus-group-jump-to-group group)
7379         (if (not (eq gnus-auto-select-next 'quietly))
7380             (progn
7381               (gnus-summary-read-group group nil no-article sumbuf)
7382               (and (string= gnus-newsgroup-name ingroup)
7383                    (bufferp sumbuf) (buffer-name sumbuf)
7384                    (progn
7385                      (set-buffer (setq gnus-summary-buffer sumbuf))
7386                      (gnus-summary-exit-no-update t))))
7387           (let ((prevgroup group))
7388             (gnus-group-jump-to-group ingroup)
7389             (setq group (gnus-summary-search-group backward))
7390             (gnus-summary-read-group group nil no-article sumbuf)
7391             (while (and (string= gnus-newsgroup-name ingroup)
7392                         (bufferp sumbuf) 
7393                         (buffer-name sumbuf)
7394                         (not (string= prevgroup (gnus-group-group-name))))
7395               (set-buffer gnus-group-buffer)
7396               (gnus-summary-read-group 
7397                (setq prevgroup (gnus-group-group-name)) 
7398                nil no-article sumbuf))
7399             (and (string= prevgroup (gnus-group-group-name))
7400                  ;; We have reached the final group in the group
7401                  ;; buffer.
7402                  (progn
7403                    (if (buffer-name sumbuf)
7404                        (progn
7405                          (set-buffer sumbuf)
7406                          (gnus-summary-exit)))))))))))
7407
7408 (defun gnus-summary-prev-group (no-article)
7409   "Exit current newsgroup and then select previous unread newsgroup.
7410 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7411   (interactive "P")
7412   (gnus-summary-next-group no-article nil t))
7413
7414 ;; Walking around summary lines.
7415
7416 (defun gnus-summary-first-subject (unread)
7417   "Go to the first unread subject.
7418 If UNREAD is non-nil, go to the first unread article.
7419 Returns nil if there are no unread articles."
7420   (interactive "P")
7421   (prog1
7422       (cond ((not unread)
7423              (goto-char (point-min)))
7424             ((gnus-goto-char 
7425               (text-property-any 
7426                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7427              t)
7428             (t 
7429              ;; There are no unread articles.
7430              (gnus-message 3 "No more unread articles")
7431              nil))
7432     (gnus-summary-position-cursor)))
7433
7434 (defun gnus-summary-next-subject (n &optional unread dont-display)
7435   "Go to next N'th summary line.
7436 If N is negative, go to the previous N'th subject line.
7437 If UNREAD is non-nil, only unread articles are selected.
7438 The difference between N and the actual number of steps taken is
7439 returned."
7440   (interactive "p")
7441   (let ((backward (< n 0))
7442         (n (abs n)))
7443     (while (and (> n 0)
7444                 (gnus-summary-search-forward unread nil backward))
7445       (setq n (1- n)))
7446     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7447                                (if unread " unread" "")))
7448     (or dont-display
7449         (progn
7450           (gnus-summary-recenter)
7451           (gnus-summary-position-cursor)))
7452   n))
7453
7454 (defun gnus-summary-next-unread-subject (n)
7455   "Go to next N'th unread summary line."
7456   (interactive "p")
7457   (gnus-summary-next-subject n t))
7458
7459 (defun gnus-summary-prev-subject (n &optional unread)
7460   "Go to previous N'th summary line.
7461 If optional argument UNREAD is non-nil, only unread article is selected."
7462   (interactive "p")
7463   (gnus-summary-next-subject (- n) unread))
7464
7465 (defun gnus-summary-prev-unread-subject (n)
7466   "Go to previous N'th unread summary line."
7467   (interactive "p")
7468   (gnus-summary-next-subject (- n) t))
7469
7470 (defun gnus-summary-goto-subject (article)
7471   "Go the subject line of ARTICLE."
7472   (interactive
7473    (list
7474     (string-to-int
7475      (completing-read "Article number: "
7476                       (mapcar
7477                        (lambda (headers)
7478                          (list
7479                           (int-to-string (header-number headers))))
7480                        gnus-newsgroup-headers)
7481                       nil 'require-match))))
7482   (or article (error "No article number"))
7483   (let ((b (point)))
7484     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7485                                                 'gnus-number article)))
7486         ()
7487       (gnus-summary-show-thread)
7488       ;; Skip dummy articles. 
7489       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7490           (forward-line 1))
7491       (prog1
7492           (if (not (eobp))
7493               article
7494             (goto-char b)
7495             nil)
7496         (gnus-summary-position-cursor)))))
7497
7498 ;; Walking around summary lines with displaying articles.
7499
7500 (defun gnus-summary-expand-window ()
7501   "Make the summary buffer take up the entire Emacs frame."
7502   (interactive)
7503   (gnus-set-global-variables)
7504   (gnus-configure-windows 'summary))
7505
7506 (defun gnus-summary-display-article (article &optional all-header)
7507   "Display ARTICLE in article buffer."
7508   (gnus-set-global-variables)
7509   (if (null article)
7510       nil
7511     (prog1
7512         (gnus-article-prepare article all-header)
7513       (gnus-summary-show-thread)
7514       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7515           (progn
7516             (forward-line 1)
7517             (gnus-summary-position-cursor)))
7518       (run-hooks 'gnus-select-article-hook)
7519       (gnus-summary-recenter)
7520       (gnus-summary-goto-subject article)
7521       ;; Successfully display article.
7522       (gnus-summary-update-line)
7523       (gnus-article-set-window-start 
7524        (cdr (assq article gnus-newsgroup-bookmarks)))
7525       t)))
7526
7527 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7528   "Select the current article.
7529 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7530 non-nil, the article will be re-fetched even if it already present in
7531 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7532 be displayed."
7533   (and (not pseudo) (gnus-summary-pseudo-article)
7534        (error "This is a pseudo-article."))
7535   (let ((article (or article (gnus-summary-article-number)))
7536         (all-headers (not (not all-headers))) ;Must be T or NIL.
7537         did) 
7538     (prog1
7539         (save-excursion
7540           (set-buffer gnus-summary-buffer)
7541           (if (or (null gnus-current-article)
7542                   (null gnus-article-current)
7543                   (not (eq article (cdr gnus-article-current)))
7544                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7545                   force)
7546               ;; The requested article is different from the current article.
7547               (progn
7548                 (gnus-summary-display-article article all-headers)
7549                 (setq did article))
7550             (if all-headers (gnus-article-show-all-headers))
7551             nil))
7552       (if did 
7553           (gnus-article-set-window-start 
7554            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7555
7556 (defun gnus-summary-set-current-mark (&optional current-mark)
7557   "Obsolete function."
7558   nil)
7559
7560 (defun gnus-summary-next-article (unread &optional subject backward)
7561   "Select the next article.
7562 If UNREAD, only unread articles are selected.
7563 If SUBJECT, only articles with SUBJECT are selected.
7564 If BACKWARD, the previous article is selected instead of the next."
7565   (interactive "P")
7566   (gnus-set-global-variables)
7567   (let (header)
7568     (cond
7569      ;; Is there such an article?
7570      ((gnus-summary-display-article 
7571        (gnus-summary-search-forward unread subject backward))
7572       (gnus-summary-position-cursor))
7573      ;; If not, we try the first unread, if that is wanted.
7574      ((and subject
7575            gnus-auto-select-same
7576            (gnus-summary-first-unread-article))
7577       (gnus-message 6 "Wrapped"))
7578      ;; Try to get next/previous article not displayed in this group.
7579      ((and gnus-auto-extend-newsgroup
7580            (not unread) (not subject)
7581            (setq header (gnus-more-header-forward backward)))
7582       (gnus-extend-newsgroup header backward)
7583       (let ((buffer-read-only nil))
7584         (goto-char (if backward (point-min) (point-max)))
7585         (gnus-summary-prepare-threads (list header) 0))
7586       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7587                                    gnus-newsgroup-end)))
7588      ;; Go to next/previous group.
7589      (t
7590       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7591           (gnus-summary-jump-to-group gnus-newsgroup-name))
7592       (let ((cmd (aref (this-command-keys) 0))
7593             (group 
7594              (if (eq gnus-keep-same-level 'best) 
7595                  (gnus-summary-best-group gnus-newsgroup-name)
7596                (gnus-summary-search-group backward gnus-keep-same-level))))
7597         ;; For some reason, the group window gets selected. We change
7598         ;; it back.  
7599         (select-window (get-buffer-window (current-buffer)))
7600         ;; Keep just the event type of CMD.
7601         (and (listp cmd) (setq cmd (car cmd)))
7602         ;; Select next unread newsgroup automagically.
7603         (cond 
7604          ((not gnus-auto-select-next)
7605           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7606          ((eq gnus-auto-select-next 'quietly)
7607           ;; Select quietly.
7608           (if (assoc 'quit-config (gnus-find-method-for-group 
7609                                    gnus-newsgroup-name))
7610               (gnus-summary-exit)
7611             (gnus-message 7 "No more%s articles (%s)..."
7612                           (if unread " unread" "") 
7613                           (if group (concat "selecting " group)
7614                             "exiting"))
7615             (gnus-summary-next-group nil group backward)))
7616          (t
7617           (let ((keystrokes '(?\C-n ?\C-p))
7618                 key)
7619             (while (or (null key) (memq key keystrokes))
7620               (gnus-message 
7621                7 "No more%s articles%s" (if unread " unread" "")
7622                (if (and group (not (assoc 'quit-config
7623                                           (gnus-find-method-for-group 
7624                                            gnus-newsgroup-name))))
7625                    (format " (Type %s for %s [%s])"
7626                            (single-key-description cmd) group
7627                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7628                  (format " (Type %s to exit %s)"
7629                          (single-key-description cmd)
7630                          gnus-newsgroup-name)))
7631               ;; Confirm auto selection.
7632               (let* ((event (read-event)))
7633                 (setq key (if (listp event) (car event) event))
7634                 (if (memq key keystrokes)
7635                     (let ((obuf (current-buffer)))
7636                       (switch-to-buffer gnus-group-buffer)
7637                       (gnus-group-jump-to-group group)
7638                       (execute-kbd-macro (char-to-string key))
7639                       (setq group (gnus-group-group-name))
7640                       (switch-to-buffer obuf)))))
7641             (if (equal key cmd)
7642                 (if (or (not group) (assoc 'quit-config
7643                                            (gnus-find-method-for-group
7644                                             gnus-newsgroup-name)))
7645                     (gnus-summary-exit)
7646                   (gnus-summary-next-group nil group backward))
7647               (setq unread-command-events (list key)))))))))))
7648
7649 (defun gnus-summary-next-unread-article ()
7650   "Select unread article after current one."
7651   (interactive)
7652   (gnus-summary-next-article t (and gnus-auto-select-same
7653                                     (gnus-summary-subject-string))))
7654
7655 (defun gnus-summary-prev-article (unread &optional subject)
7656   "Select the article after the current one.
7657 If UNREAD is non-nil, only unread articles are selected."
7658   (interactive "P")
7659   (gnus-summary-next-article unread subject t))
7660
7661 (defun gnus-summary-prev-unread-article ()
7662   "Select unred article before current one."
7663   (interactive)
7664   (gnus-summary-prev-article t (and gnus-auto-select-same
7665                                     (gnus-summary-subject-string))))
7666
7667 (defun gnus-summary-next-page (lines &optional circular)
7668   "Show next page of selected article.
7669 If end of article, select next article.
7670 Argument LINES specifies lines to be scrolled up.
7671 If CIRCULAR is non-nil, go to the start of the article instead of 
7672 instead of selecting the next article when reaching the end of the
7673 current article." 
7674   (interactive "P")
7675   (setq gnus-summary-buffer (current-buffer))
7676   (gnus-set-global-variables)
7677   (let ((article (gnus-summary-article-number))
7678         (endp nil))
7679     (gnus-configure-windows 'article)
7680     (if (or (null gnus-current-article)
7681             (null gnus-article-current)
7682             (/= article (cdr gnus-article-current))
7683             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7684         ;; Selected subject is different from current article's.
7685         (gnus-summary-display-article article)
7686       (gnus-eval-in-buffer-window
7687        gnus-article-buffer
7688        (setq endp (gnus-article-next-page lines)))
7689       (if endp
7690           (cond (circular
7691                  (gnus-summary-beginning-of-article))
7692                 (lines
7693                  (gnus-message 3 "End of message"))
7694                 ((null lines)
7695                  (gnus-summary-next-unread-article)))))
7696     (gnus-summary-recenter)
7697     (gnus-summary-position-cursor)))
7698
7699 (defun gnus-summary-prev-page (lines)
7700   "Show previous page of selected article.
7701 Argument LINES specifies lines to be scrolled down."
7702   (interactive "P")
7703   (gnus-set-global-variables)
7704   (let ((article (gnus-summary-article-number)))
7705     (gnus-configure-windows 'article)
7706     (if (or (null gnus-current-article)
7707             (null gnus-article-current)
7708             (/= article (cdr gnus-article-current))
7709             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7710         ;; Selected subject is different from current article's.
7711         (gnus-summary-display-article article)
7712       (gnus-summary-recenter)
7713       (gnus-eval-in-buffer-window gnus-article-buffer
7714         (gnus-article-prev-page lines))))
7715   (gnus-summary-position-cursor))
7716
7717 (defun gnus-summary-scroll-up (lines)
7718   "Scroll up (or down) one line current article.
7719 Argument LINES specifies lines to be scrolled up (or down if negative)."
7720   (interactive "p")
7721   (gnus-set-global-variables)
7722   (gnus-configure-windows 'article)
7723   (or (gnus-summary-select-article nil nil 'pseudo)
7724       (gnus-eval-in-buffer-window 
7725        gnus-article-buffer
7726        (cond ((> lines 0)
7727               (if (gnus-article-next-page lines)
7728                   (gnus-message 3 "End of message")))
7729              ((< lines 0)
7730               (gnus-article-prev-page (- lines))))))
7731   (gnus-summary-recenter)
7732   (gnus-summary-position-cursor))
7733
7734 (defun gnus-summary-next-same-subject ()
7735   "Select next article which has the same subject as current one."
7736   (interactive)
7737   (gnus-set-global-variables)
7738   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7739
7740 (defun gnus-summary-prev-same-subject ()
7741   "Select previous article which has the same subject as current one."
7742   (interactive)
7743   (gnus-set-global-variables)
7744   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7745
7746 (defun gnus-summary-next-unread-same-subject ()
7747   "Select next unread article which has the same subject as current one."
7748   (interactive)
7749   (gnus-set-global-variables)
7750   (gnus-summary-next-article t (gnus-summary-subject-string)))
7751
7752 (defun gnus-summary-prev-unread-same-subject ()
7753   "Select previous unread article which has the same subject as current one."
7754   (interactive)
7755   (gnus-set-global-variables)
7756   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7757
7758 (defun gnus-summary-first-unread-article ()
7759   "Select the first unread article. 
7760 Return nil if there are no unread articles."
7761   (interactive)
7762   (gnus-set-global-variables)
7763   (prog1
7764       (if (gnus-summary-first-subject t)
7765           (gnus-summary-display-article (gnus-summary-article-number)))
7766     (gnus-summary-position-cursor)))
7767
7768 (defun gnus-summary-best-unread-article ()
7769   "Select the unread article with the highest score."
7770   (interactive)
7771   (gnus-set-global-variables)
7772   (let ((scored gnus-newsgroup-scored)
7773         (best -1000000)
7774         article art)
7775     (while scored
7776       (or (> best (cdr (car scored)))
7777           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7778                (not (memq art gnus-newsgroup-marked))
7779                (not (memq art gnus-newsgroup-dormant))
7780                (if (= best (cdr (car scored)))
7781                    (setq article (min art article))
7782                  (setq article art)
7783                  (setq best (cdr (car scored))))))
7784       (setq scored (cdr scored)))
7785     (if article 
7786         (gnus-summary-goto-article article)
7787       (gnus-summary-first-unread-article))
7788     (gnus-summary-position-cursor)))
7789
7790 (defun gnus-summary-goto-article (article &optional all-headers)
7791   "Fetch ARTICLE and display it if it exists.
7792 If ALL-HEADERS is non-nil, no header lines are hidden."
7793   (interactive
7794    (list
7795     (string-to-int
7796      (completing-read 
7797       "Article number: "
7798       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7799               gnus-newsgroup-headers) 
7800       nil 'require-match))))
7801   (prog1
7802       (and (gnus-summary-goto-subject article)
7803            (gnus-summary-display-article article all-headers))
7804     (gnus-summary-position-cursor)))
7805
7806 (defun gnus-summary-goto-last-article ()
7807   "Go to the previously read article."
7808   (interactive)
7809   (prog1
7810       (and gnus-last-article
7811            (gnus-summary-goto-article gnus-last-article))
7812     (gnus-summary-position-cursor)))
7813
7814 (defun gnus-summary-pop-article (number)
7815   "Pop one article off the history and go to the previous.
7816 NUMBER articles will be popped off."
7817   (interactive "p")
7818   (let (to)
7819     (setq gnus-newsgroup-history
7820           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7821     (if to
7822         (gnus-summary-goto-article (car to))
7823       (error "Article history empty")))
7824   (gnus-summary-position-cursor))
7825
7826 ;; Summary article oriented commands
7827
7828 (defun gnus-summary-refer-parent-article (n)
7829   "Refer parent article N times.
7830 The difference between N and the number of articles fetched is returned."
7831   (interactive "p")
7832   (gnus-set-global-variables)
7833   (while 
7834       (and 
7835        (> n 0)
7836        (let ((ref (header-references (gnus-get-header-by-number
7837                                       (gnus-summary-article-number)))))
7838          (if (and ref (not (equal ref ""))
7839                   (string-match "<[^<>]*>[ \t]*$" ref))
7840              (gnus-summary-refer-article 
7841               (substring ref (match-beginning 0) (match-end 0)))
7842            (gnus-message 1 "No references in article %d"
7843                          (gnus-summary-article-number))
7844            nil)))
7845     (setq n (1- n)))
7846   (gnus-summary-position-cursor)
7847   n)
7848     
7849 (defun gnus-summary-refer-article (message-id)
7850   "Refer article specified by MESSAGE-ID.
7851 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7852   (interactive "sMessage-ID: ")
7853   (if (or (not (stringp message-id))
7854           (zerop (length message-id)))
7855       ()
7856     ;; Construct the correct Message-ID if necessary.
7857     ;; Suggested by tale@pawl.rpi.edu.
7858     (or (string-match "^<" message-id)
7859         (setq message-id (concat "<" message-id)))
7860     (or (string-match ">$" message-id)
7861         (setq message-id (concat message-id ">")))
7862     (let ((header (car (gnus-gethash (downcase message-id)
7863                                      gnus-newsgroup-dependencies))))
7864       (if header
7865           (or (gnus-summary-goto-article (header-number header))
7866               ;; The header has been read, but the article had been
7867               ;; expunged, so we insert it again.
7868               (progn
7869                 (gnus-summary-insert-line
7870                  nil header 0 nil gnus-read-mark nil nil
7871                  (header-subject header))
7872                 (forward-line -1)
7873                 (header-number header)))
7874         (let ((gnus-override-method gnus-refer-article-method)
7875               (gnus-ancient-mark gnus-read-mark)
7876               (tmp-buf (get-buffer-create " *gnus refer"))
7877               (tmp-point (window-start
7878                           (get-buffer-window gnus-article-buffer)))
7879               number)
7880           (and gnus-refer-article-method
7881                (or (gnus-server-opened gnus-refer-article-method)
7882                    (gnus-open-server gnus-refer-article-method)))
7883           ;; Save the old article buffer.
7884           (save-excursion
7885             (set-buffer tmp-buf)
7886             (buffer-disable-undo (current-buffer))
7887             (insert-buffer-substring gnus-article-buffer))
7888           (prog1
7889               (if (gnus-article-prepare 
7890                    message-id nil (gnus-read-header message-id))
7891                   (progn
7892                     (setq number (header-number gnus-current-headers))
7893                     (gnus-rebuild-thread message-id)
7894                     (gnus-summary-goto-subject number)
7895                     (gnus-summary-recenter)
7896                     (gnus-article-set-window-start 
7897                      (cdr (assq number gnus-newsgroup-bookmarks)))
7898                     message-id)
7899                 ;; We restore the old article buffer.
7900                 (save-excursion
7901                   (set-buffer gnus-article-buffer)
7902                   (let ((buffer-read-only nil))
7903                     (insert-buffer-substring tmp-buf)
7904                     (and tmp-point
7905                          (set-window-start (get-buffer-window (current-buffer))
7906                                            tmp-point))))
7907                 nil)
7908             (kill-buffer tmp-buf)))))))
7909
7910 (defun gnus-summary-enter-digest-group ()
7911   "Enter a digest group based on the current article."
7912   (interactive)
7913   (gnus-set-global-variables)
7914   (gnus-summary-select-article)
7915   ;; We do not want a narrowed article.
7916   (gnus-summary-stop-page-breaking)
7917   (let ((name (format "%s-%d" 
7918                       (gnus-group-prefixed-name 
7919                        gnus-newsgroup-name (list 'nndoc "")) 
7920                       gnus-current-article))
7921         (ogroup gnus-newsgroup-name)
7922         (buf (current-buffer)))
7923     (if (gnus-group-read-ephemeral-group 
7924          name (list 'nndoc name
7925                     (list 'nndoc-address (get-buffer gnus-article-buffer))
7926                     '(nndoc-article-type digest))
7927          t)
7928         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
7929                 (list (list (cons 'to-group ogroup))))
7930       (switch-to-buffer buf)
7931       (gnus-set-global-variables)
7932       (gnus-configure-windows 'summary)
7933       (gnus-message 3 "Article not a digest?"))))
7934
7935 (defun gnus-summary-isearch-article ()
7936   "Do incremental search forward on current article."
7937   (interactive)
7938   (gnus-set-global-variables)
7939   (gnus-summary-select-article)
7940   (gnus-eval-in-buffer-window 
7941    gnus-article-buffer (isearch-forward)))
7942
7943 (defun gnus-summary-search-article-forward (regexp &optional backward)
7944   "Search for an article containing REGEXP forward.
7945 If BACKWARD, search backward instead."
7946   (interactive
7947    (list (read-string
7948           (format "Search article %s (regexp%s): "
7949                   (if current-prefix-arg "backward" "forward")
7950                   (if gnus-last-search-regexp
7951                       (concat ", default " gnus-last-search-regexp)
7952                     "")))
7953          current-prefix-arg))
7954   (gnus-set-global-variables)
7955   (if (string-equal regexp "")
7956       (setq regexp (or gnus-last-search-regexp ""))
7957     (setq gnus-last-search-regexp regexp))
7958   (if (gnus-summary-search-article regexp backward)
7959       (gnus-article-set-window-start 
7960        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
7961     (error "Search failed: \"%s\"" regexp)))
7962
7963 (defun gnus-summary-search-article-backward (regexp)
7964   "Search for an article containing REGEXP backward."
7965   (interactive
7966    (list (read-string
7967           (format "Search article backward (regexp%s): "
7968                   (if gnus-last-search-regexp
7969                       (concat ", default " gnus-last-search-regexp)
7970                     "")))))
7971   (gnus-summary-search-article-forward regexp 'backward))
7972
7973 (defun gnus-summary-search-article (regexp &optional backward)
7974   "Search for an article containing REGEXP.
7975 Optional argument BACKWARD means do search for backward.
7976 gnus-select-article-hook is not called during the search."
7977   (let ((gnus-select-article-hook nil)  ;Disable hook.
7978         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7979         (re-search
7980          (if backward
7981              (function re-search-backward) (function re-search-forward)))
7982         (found nil)
7983         (last nil))
7984     ;; Hidden thread subtrees must be searched for ,too.
7985     (gnus-summary-show-all-threads)
7986     (if (eobp) (forward-line -1))
7987     ;; First of all, search current article.
7988     ;; We don't want to read article again from NNTP server nor reset
7989     ;; current point.
7990     (gnus-summary-select-article)
7991     (gnus-message 9 "Searching article: %d..." gnus-current-article)
7992     (setq last gnus-current-article)
7993     (gnus-eval-in-buffer-window gnus-article-buffer
7994       (save-restriction
7995         (widen)
7996         ;; Begin search from current point.
7997         (setq found (funcall re-search regexp nil t))))
7998     ;; Then search next articles.
7999     (while (and (not found)
8000                 (gnus-summary-display-article 
8001                  (gnus-summary-search-subject backward nil nil)))
8002       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8003       (gnus-eval-in-buffer-window gnus-article-buffer
8004         (save-restriction
8005           (widen)
8006           (goto-char (if backward (point-max) (point-min)))
8007           (setq found (funcall re-search regexp nil t)))))
8008     (message "")
8009     ;; Adjust article pointer.
8010     (or (eq last gnus-current-article)
8011         (setq gnus-last-article last))
8012     ;; Return T if found such article.
8013     found))
8014
8015 (defun gnus-summary-execute-command (header regexp command &optional backward)
8016   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8017 If HEADER is an empty string (or nil), the match is done on the entire
8018 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8019   (interactive
8020    (list (let ((completion-ignore-case t))
8021            (completing-read 
8022             "Header name: "
8023             (mapcar (lambda (string) (list string))
8024                     '("Number" "Subject" "From" "Lines" "Date"
8025                       "Message-ID" "Xref" "References"))
8026             nil 'require-match))
8027          (read-string "Regexp: ")
8028          (read-key-sequence "Command: ")
8029          current-prefix-arg))
8030   (gnus-set-global-variables)
8031   ;; Hidden thread subtrees must be searched as well.
8032   (gnus-summary-show-all-threads)
8033   ;; We don't want to change current point nor window configuration.
8034   (save-excursion
8035     (save-window-excursion
8036       (gnus-message 6 "Executing %s..." (key-description command))
8037       ;; We'd like to execute COMMAND interactively so as to give arguments.
8038       (gnus-execute header regexp
8039                     (` (lambda ()
8040                          (call-interactively '(, (key-binding command)))))
8041                     backward)
8042       (gnus-message 6 "Executing %s...done" (key-description command)))))
8043
8044 (defun gnus-summary-beginning-of-article ()
8045   "Scroll the article back to the beginning."
8046   (interactive)
8047   (gnus-set-global-variables)
8048   (gnus-summary-select-article)
8049   (gnus-configure-windows 'article)
8050   (gnus-eval-in-buffer-window
8051    gnus-article-buffer
8052    (widen)
8053    (goto-char (point-min))
8054    (and gnus-break-pages (gnus-narrow-to-page))))
8055
8056 (defun gnus-summary-end-of-article ()
8057   "Scroll to the end of the article."
8058   (interactive)
8059   (gnus-set-global-variables)
8060   (gnus-summary-select-article)
8061   (gnus-configure-windows 'article)
8062   (gnus-eval-in-buffer-window 
8063    gnus-article-buffer
8064    (widen)
8065    (goto-char (point-max))
8066    (and gnus-break-pages (gnus-narrow-to-page))))
8067
8068 (defun gnus-summary-show-article ()
8069   "Force re-fetching of the current article."
8070   (interactive)
8071   (gnus-set-global-variables)
8072   (gnus-summary-select-article gnus-have-all-headers t))
8073
8074 (defun gnus-summary-verbose-header (arg)
8075   "Toggle permanent full header display.
8076 If ARG is a positive number, turn header display on.
8077 If ARG is a negative number, turn header display off."
8078   (interactive "P")
8079   (gnus-set-global-variables)
8080   (gnus-summary-toggle-header arg)
8081   (setq gnus-have-all-headers
8082         (cond ((or (not (numberp arg))
8083                    (zerop arg))
8084                (not gnus-have-all-headers))
8085               ((natnump arg)
8086                t))))
8087
8088 (defun gnus-summary-toggle-header (arg)
8089   "Show the headers if they are hidden, or hide them if they are shown.
8090 If ARG is a positive number, show the entire header.
8091 If ARG is a negative number, hide the unwanted header lines."
8092   (interactive "P")
8093   (gnus-set-global-variables)
8094   (save-excursion
8095     (set-buffer gnus-article-buffer)
8096     (let ((buffer-read-only nil))
8097       (if (numberp arg) 
8098           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8099                                                 gnus-hidden-properties)
8100             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8101         (if (text-property-any (point-min) (point-max) 'invisible t)
8102             (remove-text-properties (point-min) (point-max)
8103                                     gnus-hidden-properties)
8104           (let ((gnus-have-all-headers nil))
8105             (run-hooks 'gnus-article-display-hook))))
8106       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8107
8108 (defun gnus-summary-show-all-headers ()
8109   "Make all header lines visible."
8110   (interactive)
8111   (gnus-set-global-variables)
8112   (gnus-article-show-all-headers))
8113
8114 (defun gnus-summary-toggle-mime (arg)
8115   "Toggle MIME processing.
8116 If ARG is a positive number, turn MIME processing on."
8117   (interactive "P")
8118   (gnus-set-global-variables)
8119   (setq gnus-show-mime
8120         (if (null arg) (not gnus-show-mime)
8121           (> (prefix-numeric-value arg) 0)))
8122   (gnus-summary-select-article t 'force))
8123
8124 (defun gnus-summary-caesar-message (rotnum)
8125   "Caesar rotates all letters of current message by 13/47 places.
8126 With prefix arg, specifies the number of places to rotate each letter forward.
8127 Caesar rotates Japanese letters by 47 places in any case."
8128   (interactive "P")
8129   (gnus-set-global-variables)
8130   (gnus-summary-select-article)
8131   (let ((mail-header-separator "")) ; !!! Is this necessary?
8132     (gnus-overload-functions)
8133     (gnus-eval-in-buffer-window 
8134      gnus-article-buffer
8135      (save-restriction
8136        (widen)
8137        ;; We don't want to jump to the beginning of the message.
8138        ;; `save-excursion' does not do its job.
8139        (move-to-window-line 0)
8140        (let ((last (point)))
8141          (news-caesar-buffer-body rotnum)
8142          (goto-char last)
8143          (recenter 0))))))
8144
8145 (defun gnus-summary-stop-page-breaking ()
8146   "Stop page breaking in the current article."
8147   (interactive)
8148   (gnus-set-global-variables)
8149   (gnus-summary-select-article)
8150   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8151
8152 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8153
8154 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8155   "Move the current article to a different newsgroup.
8156 If N is a positive number, move the N next articles.
8157 If N is a negative number, move the N previous articles.
8158 If N is nil and any articles have been marked with the process mark,
8159 move those articles instead.
8160 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8161 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8162 re-spool using this method.
8163 For this function to work, both the current newsgroup and the
8164 newsgroup that you want to move to have to support the `request-move'
8165 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8166   (interactive "P")
8167   (gnus-set-global-variables)
8168   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8169       (error "The current newsgroup does not support article moving"))
8170   (let ((articles (gnus-summary-work-articles n))
8171         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8172         art-group)
8173     (if (and (not to-newsgroup) (not select-method))
8174         (setq to-newsgroup
8175               (completing-read 
8176                (format "Where do you want to move %s? %s"
8177                        (if (> (length articles) 1)
8178                            (format "these %d articles" (length articles))
8179                          "this article")
8180                        (if gnus-current-move-group
8181                            (format "(%s default) " gnus-current-move-group)
8182                          ""))
8183                gnus-active-hashtb nil nil prefix)))
8184     (if to-newsgroup
8185         (progn
8186           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8187               (setq to-newsgroup (or gnus-current-move-group "")))
8188           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8189               (gnus-activate-newsgroup to-newsgroup)
8190               (error "No such group: %s" to-newsgroup))
8191           (setq gnus-current-move-group to-newsgroup)))
8192     (or (gnus-check-backend-function 'request-accept-article 
8193                                      (or select-method to-newsgroup))
8194         (error "%s does not support article moving" to-newsgroup))
8195     (gnus-message 6 "Moving to %s: %s..." 
8196                   (or select-method to-newsgroup) articles)
8197     (while articles
8198       (if (setq art-group
8199                 (gnus-request-move-article 
8200                  (car articles)                   ; Article to move
8201                  gnus-newsgroup-name              ; From newsgrouo
8202                  (nth 1 (gnus-find-method-for-group 
8203                          gnus-newsgroup-name))    ; Server
8204                  (list 'gnus-request-accept-article 
8205                        (if select-method
8206                            (list 'quote select-method)
8207                          to-newsgroup)
8208                        (not (cdr articles)))     ; Accept form
8209                  (not (cdr articles))))          ; Only save nov last time
8210           (let* ((buffer-read-only nil)
8211                  (entry 
8212                   (or
8213                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8214                    (gnus-gethash 
8215                     (gnus-group-prefixed-name 
8216                      (car art-group) 
8217                      (if select-method (list select-method "")
8218                        (gnus-find-method-for-group to-newsgroup)))
8219                     gnus-newsrc-hashtb)))
8220                  (info (nth 2 entry))
8221                  (article (car articles)))
8222             (gnus-summary-goto-subject article)
8223             (beginning-of-line)
8224             (delete-region (point)
8225                            (progn (forward-line 1) (point)))
8226             (if (not (memq article gnus-newsgroup-unreads))
8227                 (setcar (cdr (cdr info))
8228                         (gnus-add-to-range (nth 2 info) 
8229                                            (list (cdr art-group)))))
8230             ;; Copy any marks over to the new group.
8231             (let ((marks '((tick . gnus-newsgroup-marked)
8232                            (dormant . gnus-newsgroup-dormant)
8233                            (expire . gnus-newsgroup-expirable)
8234                            (bookmark . gnus-newsgroup-bookmarks)
8235                         ;   (score . gnus-newsgroup-scored)
8236                            (reply . gnus-newsgroup-replied)))
8237                   (to-article (cdr art-group)))
8238               (while marks
8239                 (if (memq article (symbol-value (cdr (car marks))))
8240                     (gnus-add-marked-articles 
8241                      (car info) (car (car marks)) (list to-article) info))
8242                 (setq marks (cdr marks))))
8243             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8244             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8245             (setq gnus-newsgroup-dormant
8246                   (delq article gnus-newsgroup-dormant)))
8247         (gnus-message 1 "Couldn't move article %s" (car articles)))
8248       (gnus-summary-remove-process-mark (car articles))
8249       (setq articles (cdr articles)))))
8250
8251 (defun gnus-summary-respool-article (n &optional respool-method)
8252   "Respool the current article.
8253 The article will be squeezed through the mail spooling process again,
8254 which means that it will be put in some mail newsgroup or other
8255 depending on `nnmail-split-methods'.
8256 If N is a positive number, respool the N next articles.
8257 If N is a negative number, respool the N previous articles.
8258 If N is nil and any articles have been marked with the process mark,
8259 respool those articles instead.
8260
8261 Respooling can be done both from mail groups and \"real\" newsgroups.
8262 In the former case, the articles in question will be moved from the
8263 current group into whatever groups they are destined to.  In the
8264 latter case, they will be copied into the relevant groups."
8265   (interactive "P")
8266   (gnus-set-global-variables)
8267   (let ((respool-methods (gnus-methods-using 'respool))
8268         (methname 
8269          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8270     (or respool-method
8271         (setq respool-method
8272               (completing-read
8273                "What method do you want to use when respooling? "
8274                respool-methods nil t methname)))
8275     (or (string= respool-method "")
8276         (if (assoc (symbol-name
8277                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8278                    respool-methods)
8279             (gnus-summary-move-article n nil (intern respool-method))
8280           (gnus-summary-copy-article n nil (intern respool-method))))))
8281
8282 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8283 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8284   "Move the current article to a different newsgroup.
8285 If N is a positive number, move the N next articles.
8286 If N is a negative number, move the N previous articles.
8287 If N is nil and any articles have been marked with the process mark,
8288 move those articles instead.
8289 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8290 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8291 re-spool using this method.
8292 For this function to work, the newsgroup that you want to move to have
8293 to support the `request-move' and `request-accept'
8294 functions. (Ie. mail newsgroups at present.)"
8295   (interactive "P")
8296   (gnus-set-global-variables)
8297   (let ((articles (gnus-summary-work-articles n))
8298         (copy-buf (get-buffer-create "*copy work*"))
8299         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8300         art-group)
8301     (buffer-disable-undo copy-buf)
8302     (if (and (not to-newsgroup) (not select-method))
8303         (setq to-newsgroup
8304               (completing-read 
8305                (format "Where do you want to copy %s? %s"
8306                        (if (> (length articles) 1)
8307                            (format "these %d articles" (length articles))
8308                          "this article")
8309                        (if gnus-current-move-group
8310                            (format "(%s default) " gnus-current-move-group)
8311                          ""))
8312                gnus-active-hashtb nil nil prefix)))
8313     (if to-newsgroup
8314         (progn
8315           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8316               (setq to-newsgroup (or gnus-current-move-group "")))
8317           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8318               (gnus-activate-newsgroup to-newsgroup)
8319               (error "No such group: %s" to-newsgroup))
8320           (setq gnus-current-move-group to-newsgroup)))
8321     (or (gnus-check-backend-function 'request-accept-article 
8322                                      (or select-method to-newsgroup))
8323         (error "%s does not support article copying" to-newsgroup))
8324     (gnus-message 6 "Copying to %s: %s..." 
8325                   (or select-method to-newsgroup) articles)
8326     (while articles
8327       (if (setq art-group
8328                 (save-excursion
8329                   (set-buffer copy-buf)
8330                   (gnus-request-article-this-buffer
8331                    (car articles) gnus-newsgroup-name)
8332                   (gnus-request-accept-article
8333                    (if select-method (quote select-method) to-newsgroup)
8334                    (not (cdr articles)))))
8335           (let* ((entry 
8336                   (or
8337                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8338                    (gnus-gethash 
8339                     (gnus-group-prefixed-name 
8340                      (car art-group) 
8341                      (if select-method (list select-method "")
8342                        (gnus-find-method-for-group to-newsgroup)))
8343                     gnus-newsrc-hashtb)))
8344                  (info (nth 2 entry))
8345                  (article (car articles)))
8346             (if (not (memq article gnus-newsgroup-unreads))
8347                 (setcar (cdr (cdr info))
8348                         (gnus-add-to-range (nth 2 info) 
8349                                            (list (cdr art-group)))))
8350             ;; Copy any marks over to the new group.
8351             (let ((marks '((tick . gnus-newsgroup-marked)
8352                            (dormant . gnus-newsgroup-dormant)
8353                            (expire . gnus-newsgroup-expirable)
8354                            (bookmark . gnus-newsgroup-bookmarks)
8355                         ;   (score . gnus-newsgroup-scored)
8356                            (reply . gnus-newsgroup-replied)))
8357                   (to-article (cdr art-group)))
8358               (while marks
8359                 (if (memq article (symbol-value (cdr (car marks))))
8360                     (gnus-add-marked-articles 
8361                      (car info) (car (car marks)) (list to-article) info))
8362                 (setq marks (cdr marks)))))
8363         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8364       (gnus-summary-remove-process-mark (car articles))
8365       (setq articles (cdr articles)))
8366     (kill-buffer copy-buf)))
8367
8368 (defun gnus-summary-import-article (file)
8369   "Import a random file into a mail newsgroup."
8370   (interactive "fImport file: ")
8371   (let ((group gnus-newsgroup-name)
8372         atts)
8373     (or (gnus-check-backend-function 'request-accept-article group)
8374         (error "%s does not support article importing" group))
8375     (or (file-readable-p file)
8376         (not (file-regular-p file))
8377         (error "Can't read %s" file))
8378     (save-excursion
8379       (set-buffer (get-buffer-create " *import file*"))
8380       (buffer-disable-undo (current-buffer))
8381       (erase-buffer)
8382       (insert-file-contents file)
8383       (goto-char (point-min))
8384       (if (nnheader-article-p)
8385           ()
8386         (setq atts (file-attributes file))
8387         (insert "From: " (read-string "From: ") "\n"
8388                 "Subject: " (read-string "Subject: ") "\n"
8389                 "Date: " (current-time-string (nth 5 atts)) "\n"
8390                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8391       (gnus-request-accept-article group t)
8392       (kill-buffer (current-buffer)))))
8393
8394 (defun gnus-summary-expire-articles ()
8395   "Expire all articles that are marked as expirable in the current group."
8396   (interactive)
8397   (if (and gnus-newsgroup-expirable
8398            (gnus-check-backend-function 
8399             'request-expire-articles gnus-newsgroup-name))
8400       (let ((expirable gnus-newsgroup-expirable))
8401         ;; The list of articles that weren't expired is returned.
8402         (setq gnus-newsgroup-expirable 
8403               (gnus-request-expire-articles gnus-newsgroup-expirable
8404                                             gnus-newsgroup-name))
8405         ;; We go through the old list of expirable, and mark all
8406         ;; really expired articles as non-existent.
8407         (while expirable
8408           (or (memq (car expirable) gnus-newsgroup-expirable)
8409               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8410           (setq expirable (cdr expirable))))))
8411
8412 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8413 (defun gnus-summary-delete-article (n)
8414   "Delete the N next (mail) articles.
8415 This command actually deletes articles. This is not a marking
8416 command. The article will disappear forever from you life, never to
8417 return. 
8418 If N is negative, delete backwards.
8419 If N is nil and articles have been marked with the process mark,
8420 delete these instead."
8421   (interactive "P")
8422   (or (gnus-check-backend-function 'request-expire-articles 
8423                                    gnus-newsgroup-name)
8424       (error "The current newsgroup does not support article deletion."))
8425   ;; Compute the list of articles to delete.
8426   (let ((articles (gnus-summary-work-articles n))
8427         not-deleted)
8428     (if (and gnus-novice-user
8429              (not (gnus-y-or-n-p 
8430                    (format "Do you really want to delete %s forever? "
8431                            (if (> (length articles) 1) "these articles"
8432                              "this article")))))
8433         ()
8434       ;; Delete the articles.
8435       (setq not-deleted (gnus-request-expire-articles 
8436                          articles gnus-newsgroup-name 'force))
8437       (while articles
8438         (gnus-summary-remove-process-mark (car articles))       
8439         ;; The backend might not have been able to delete the article
8440         ;; after all.  
8441         (or (memq (car articles) not-deleted)
8442             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8443         (setq articles (cdr articles))))
8444     (gnus-summary-position-cursor)
8445     not-deleted))
8446
8447 (defun gnus-summary-edit-article ()
8448   "Enter into a buffer and edit the current article.
8449 This will have permanent effect only in mail groups."
8450   (interactive)
8451   (or (gnus-check-backend-function 
8452        'request-replace-article gnus-newsgroup-name)
8453       (error "The current newsgroup does not support article editing."))
8454   (gnus-summary-select-article t)
8455   (other-window 1)
8456   (gnus-message 6 "C-c C-c to end edits")
8457   (setq buffer-read-only nil)
8458   (text-mode)
8459   (use-local-map (copy-keymap (current-local-map)))
8460   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8461   (buffer-enable-undo)
8462   (goto-char (point-min))
8463   (search-forward "\n\n" nil t))
8464
8465 (defun gnus-summary-edit-article-done ()
8466   "Make edits to the current article permanent."
8467   (interactive)
8468   (if (not (gnus-request-replace-article 
8469             (cdr gnus-article-current) (car gnus-article-current) 
8470             (current-buffer)))
8471       (error "Couldn't replace article.")
8472     (gnus-article-mode)
8473     (use-local-map gnus-article-mode-map)
8474     (setq buffer-read-only t)
8475     (buffer-disable-undo (current-buffer))
8476     (pop-to-buffer gnus-summary-buffer)))      
8477
8478 (defun gnus-summary-fancy-query ()
8479   "Query where the fancy respool algorithm would put this article."
8480   (interactive)
8481   (gnus-summary-select-article)
8482   (save-excursion
8483     (set-buffer gnus-article-buffer)
8484     (save-restriction
8485       (goto-char (point-min))
8486       (search-forward "\n\n")
8487       (narrow-to-region (point-min) (point))
8488       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8489
8490 ;; Summary score commands.
8491
8492 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8493
8494 (defun gnus-summary-raise-score (n)
8495   "Raise the score of the current article by N."
8496   (interactive "p")
8497   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8498
8499 (defun gnus-summary-lower-score (n)
8500   "Lower the score of the current article by N."
8501   (interactive "p")
8502   (gnus-summary-raise-score (- n)))
8503
8504 (defun gnus-summary-set-score (n)
8505   "Set the score of the current article to N."
8506   (interactive "p")
8507   ;; Skip dummy header line.
8508   (save-excursion
8509     (gnus-summary-show-thread)
8510     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8511         (forward-line 1))
8512     (let ((buffer-read-only nil))
8513       ;; Set score.
8514       (gnus-summary-update-mark
8515        (if (= n (or gnus-summary-default-score 0)) ? 
8516          (if (< n (or gnus-summary-default-score 0)) 
8517              gnus-score-below-mark gnus-score-over-mark)) 'score))
8518     (let* ((article (gnus-summary-article-number))
8519            (score (assq article gnus-newsgroup-scored)))
8520       (if score (setcdr score n)
8521         (setq gnus-newsgroup-scored 
8522               (cons (cons article n) gnus-newsgroup-scored))))
8523     (gnus-summary-update-line)))
8524
8525 (defun gnus-summary-current-score ()
8526   "Return the score of the current article."
8527   (interactive)
8528   (message "%s" (gnus-summary-article-score)))
8529
8530 ;; Summary marking commands.
8531
8532 (defun gnus-summary-raise-same-subject-and-select (score)
8533   "Raise articles which has the same subject with SCORE and select the next."
8534   (interactive "p")
8535   (let ((subject (gnus-summary-subject-string)))
8536     (gnus-summary-raise-score score)
8537     (while (gnus-summary-search-subject nil nil subject)
8538       (gnus-summary-raise-score score))
8539     (gnus-summary-next-article t)))
8540
8541 (defun gnus-summary-raise-same-subject (score)
8542   "Raise articles which has the same subject with SCORE."
8543   (interactive "p")
8544   (let ((subject (gnus-summary-subject-string)))
8545     (gnus-summary-raise-score score)
8546     (while (gnus-summary-search-subject nil nil subject)
8547       (gnus-summary-raise-score score))
8548     (gnus-summary-next-subject 1 t)))
8549
8550 (defun gnus-score-default (level)
8551   (if level (prefix-numeric-value level) 
8552     gnus-score-interactive-default-score))
8553
8554 (defun gnus-summary-raise-thread (score)
8555   "Raise the score of the articles in the current thread with SCORE."
8556   (interactive "P")
8557   (setq score (1- (gnus-score-default score)))
8558   (let (e)
8559     (save-excursion
8560       (let ((level (gnus-summary-thread-level)))
8561         (gnus-summary-raise-score score)
8562         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8563                     (> (gnus-summary-thread-level) level))
8564           (gnus-summary-raise-score score))
8565         (setq e (point))))
8566     (let ((gnus-summary-check-current t))
8567       (or (zerop (gnus-summary-next-subject 1 t))
8568           (goto-char e))))
8569   (gnus-summary-recenter)
8570   (gnus-summary-position-cursor)
8571   (gnus-set-mode-line 'summary))
8572
8573 (defun gnus-summary-lower-same-subject-and-select (score)
8574   "Raise articles which has the same subject with SCORE and select the next."
8575   (interactive "p")
8576   (gnus-summary-raise-same-subject-and-select (- score)))
8577
8578 (defun gnus-summary-lower-same-subject (score)
8579   "Raise articles which has the same subject with SCORE."
8580   (interactive "p")
8581   (gnus-summary-raise-same-subject (- score)))
8582
8583 (defun gnus-summary-lower-thread (score)
8584   "Lower score of articles in the current thread with SCORE."
8585   (interactive "P")
8586   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8587
8588 (defun gnus-summary-kill-same-subject-and-select (unmark)
8589   "Mark articles which has the same subject as read, and then select the next.
8590 If UNMARK is positive, remove any kind of mark.
8591 If UNMARK is negative, tick articles."
8592   (interactive "P")
8593   (if unmark
8594       (setq unmark (prefix-numeric-value unmark)))
8595   (let ((count
8596          (gnus-summary-mark-same-subject
8597           (gnus-summary-subject-string) unmark)))
8598     ;; Select next unread article. If auto-select-same mode, should
8599     ;; select the first unread article.
8600     (gnus-summary-next-article t (and gnus-auto-select-same
8601                                       (gnus-summary-subject-string)))
8602     (gnus-message 7 "%d articles are marked as %s"
8603                   count (if unmark "unread" "read"))))
8604
8605 (defun gnus-summary-kill-same-subject (unmark)
8606   "Mark articles which has the same subject as read. 
8607 If UNMARK is positive, remove any kind of mark.
8608 If UNMARK is negative, tick articles."
8609   (interactive "P")
8610   (if unmark
8611       (setq unmark (prefix-numeric-value unmark)))
8612   (let ((count
8613          (gnus-summary-mark-same-subject
8614           (gnus-summary-subject-string) unmark)))
8615     ;; If marked as read, go to next unread subject.
8616     (if (null unmark)
8617         ;; Go to next unread subject.
8618         (gnus-summary-next-subject 1 t))
8619     (gnus-message 7 "%d articles are marked as %s"
8620                   count (if unmark "unread" "read"))))
8621
8622 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8623   "Mark articles with same SUBJECT as read, and return marked number.
8624 If optional argument UNMARK is positive, remove any kinds of marks.
8625 If optional argument UNMARK is negative, mark articles as unread instead."
8626   (let ((count 1))
8627     (save-excursion
8628       (cond ((null unmark)
8629              (gnus-summary-mark-as-read nil gnus-killed-mark))
8630             ((> unmark 0)
8631              (gnus-summary-tick-article nil t))
8632             (t
8633              (gnus-summary-tick-article)))
8634       (while (and subject
8635                   (gnus-summary-search-forward nil subject))
8636         (cond ((null unmark)
8637                (gnus-summary-mark-as-read nil gnus-killed-mark))
8638               ((> unmark 0)
8639                (gnus-summary-tick-article nil t))
8640               (t
8641                (gnus-summary-tick-article)))
8642         (setq count (1+ count))))
8643     ;; Hide killed thread subtrees.  Does not work properly always.
8644     ;;(and (null unmark)
8645     ;;     gnus-thread-hide-killed
8646     ;;     (gnus-summary-hide-thread))
8647     ;; Return number of articles marked as read.
8648     count))
8649
8650 (defun gnus-summary-mark-as-processable (n &optional unmark)
8651   "Set the process mark on the next N articles.
8652 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8653 the process mark instead.  The difference between N and the actual
8654 number of articles marked is returned."
8655   (interactive "p")
8656   (let ((backward (< n 0))
8657         (n (abs n)))
8658   (while (and 
8659           (> n 0)
8660           (if unmark
8661               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8662             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8663           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8664     (setq n (1- n)))
8665   (if (/= 0 n) (gnus-message 7 "No more articles"))
8666   (gnus-summary-recenter)
8667   (gnus-summary-position-cursor)
8668   n))
8669
8670 (defun gnus-summary-unmark-as-processable (n)
8671   "Remove the process mark from the next N articles.
8672 If N is negative, mark backward instead.  The difference between N and
8673 the actual number of articles marked is returned."
8674   (interactive "p")
8675   (gnus-summary-mark-as-processable n t))
8676
8677 (defun gnus-summary-unmark-all-processable ()
8678   "Remove the process mark from all articles."
8679   (interactive)
8680   (save-excursion
8681     (while gnus-newsgroup-processable
8682       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8683   (gnus-summary-position-cursor))
8684
8685 (defun gnus-summary-mark-as-expirable (n)
8686   "Mark N articles forward as expirable.
8687 If N is negative, mark backward instead. The difference between N and
8688 the actual number of articles marked is returned."
8689   (interactive "p")
8690   (gnus-summary-mark-forward n gnus-expirable-mark))
8691
8692 (defun gnus-summary-mark-article-as-replied (article)
8693   "Mark ARTICLE replied and update the summary line."
8694   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8695   (let ((buffer-read-only nil))
8696     (if (gnus-summary-goto-subject article)
8697         (progn
8698           (gnus-summary-update-mark gnus-replied-mark 'replied)
8699           t))))
8700
8701 (defun gnus-summary-set-bookmark (article)
8702   "Set a bookmark in current article."
8703   (interactive (list (gnus-summary-article-number)))
8704   (if (or (not (get-buffer gnus-article-buffer))
8705           (not gnus-current-article)
8706           (not gnus-article-current)
8707           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8708       (error "No current article selected"))
8709   ;; Remove old bookmark, if one exists.
8710   (let ((old (assq article gnus-newsgroup-bookmarks)))
8711     (if old (setq gnus-newsgroup-bookmarks 
8712                   (delq old gnus-newsgroup-bookmarks))))
8713   ;; Set the new bookmark, which is on the form 
8714   ;; (article-number . line-number-in-body).
8715   (setq gnus-newsgroup-bookmarks 
8716         (cons 
8717          (cons article 
8718                (save-excursion
8719                  (set-buffer gnus-article-buffer)
8720                  (count-lines
8721                   (min (point)
8722                        (save-excursion
8723                          (goto-char (point-min))
8724                          (search-forward "\n\n" nil t)
8725                          (point)))
8726                   (point))))
8727          gnus-newsgroup-bookmarks))
8728   (gnus-message 6 "A bookmark has been added to the current article."))
8729
8730 (defun gnus-summary-remove-bookmark (article)
8731   "Remove the bookmark from the current article."
8732   (interactive (list (gnus-summary-article-number)))
8733   ;; Remove old bookmark, if one exists.
8734   (let ((old (assq article gnus-newsgroup-bookmarks)))
8735     (if old 
8736         (progn
8737           (setq gnus-newsgroup-bookmarks 
8738                 (delq old gnus-newsgroup-bookmarks))
8739           (gnus-message 6 "Removed bookmark."))
8740       (gnus-message 6 "No bookmark in current article."))))
8741
8742 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8743 (defun gnus-summary-mark-as-dormant (n)
8744   "Mark N articles forward as dormant.
8745 If N is negative, mark backward instead.  The difference between N and
8746 the actual number of articles marked is returned."
8747   (interactive "p")
8748   (gnus-summary-mark-forward n gnus-dormant-mark))
8749
8750 (defun gnus-summary-set-process-mark (article)
8751   "Set the process mark on ARTICLE and update the summary line."
8752   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8753   (let ((buffer-read-only nil))
8754     (if (gnus-summary-goto-subject article)
8755         (progn
8756           (gnus-summary-show-thread)
8757           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8758                (forward-line 1))
8759           (gnus-summary-update-mark gnus-process-mark 'replied)
8760           t))))
8761
8762 (defun gnus-summary-remove-process-mark (article)
8763   "Remove the process mark from ARTICLE and update the summary line."
8764   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8765   (let ((buffer-read-only nil))
8766     (if (gnus-summary-goto-subject article)
8767         (progn
8768           (gnus-summary-show-thread)
8769           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8770                (forward-line 1))
8771           (gnus-summary-update-mark ?  'replied)
8772           (if (memq article gnus-newsgroup-replied) 
8773               (gnus-summary-update-mark gnus-replied-mark 'replied))
8774           t))))
8775
8776 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8777   "Mark N articles as read forwards.
8778 If N is negative, mark backwards instead.
8779 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8780 marked as unread. 
8781 The difference between N and the actual number of articles marked is
8782 returned."
8783   (interactive "p")
8784   (gnus-set-global-variables)
8785   (let ((backward (< n 0))
8786         (n (abs n))
8787         (mark (or mark gnus-del-mark)))
8788   (while (and (> n 0)
8789               (gnus-summary-mark-article nil mark no-expire)
8790               (zerop (gnus-summary-next-subject 
8791                       (if backward -1 1) gnus-summary-goto-unread t)))
8792     (setq n (1- n)))
8793   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8794   (gnus-summary-recenter)
8795   (gnus-summary-position-cursor)
8796   (gnus-set-mode-line 'summary)
8797   n))
8798
8799 (defun gnus-summary-mark-article (&optional article mark no-expire)
8800   "Mark ARTICLE with MARK.
8801 MARK can be any character.
8802 Five MARK strings are reserved: ?  (unread), 
8803 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8804 If MARK is nil, then the default character ?D is used.
8805 If ARTICLE is nil, then the article on the current line will be
8806 marked." 
8807   ;; If no mark is given, then we check auto-expiring.
8808   (and (not no-expire)
8809        gnus-newsgroup-auto-expire 
8810        (or (not mark)
8811            (and (numberp mark) 
8812                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
8813                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
8814                     (= mark gnus-read-mark) (= mark gnus-ancient-mark))))
8815        (setq mark gnus-expirable-mark))
8816   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8817          (article (or article (gnus-summary-article-number))))
8818     (or article (error "No article on current line"))
8819     (if (or (= mark gnus-unread-mark) 
8820             (= mark gnus-ticked-mark) 
8821             (= mark gnus-dormant-mark))
8822         (gnus-mark-article-as-unread article mark)
8823       (gnus-mark-article-as-read article mark))
8824
8825     ;; See whether the article is to be put in the cache.
8826     (and gnus-use-cache
8827          (save-excursion
8828            (gnus-cache-possibly-enter-article 
8829             gnus-newsgroup-name article 
8830             (gnus-get-header-by-number article)
8831             (= mark gnus-ticked-mark)
8832             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8833
8834     (if (gnus-summary-goto-subject article)
8835         (let ((buffer-read-only nil))
8836           (gnus-summary-show-thread)
8837           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8838                (forward-line 1))
8839           ;; Fix the mark.
8840           (gnus-summary-update-mark mark 'unread)
8841           t))))
8842
8843 (defun gnus-summary-update-mark (mark type)
8844   (beginning-of-line)
8845   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8846         plist)
8847     (if (not forward)
8848         ()
8849       (forward-char forward)
8850       (setq plist (text-properties-at (point)))
8851       (delete-char 1)
8852       (insert mark)
8853       (and plist (add-text-properties (1- (point)) (point) plist))
8854       (and (eq type 'unread)
8855            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
8856       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8857   
8858 (defun gnus-mark-article-as-read (article &optional mark)
8859   "Enter ARTICLE in the pertinent lists and remove it from others."
8860   ;; Make the article expirable.
8861   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8862     (if (= mark gnus-expirable-mark)
8863         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8864       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8865     ;; Remove from unread and marked lists.
8866     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8867     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8868     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8869     ;; Possibly remove from cache, if that is used. 
8870     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
8871
8872 (defun gnus-mark-article-as-unread (article &optional mark)
8873   "Enter ARTICLE in the pertinent lists and remove it from others."
8874   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8875     ;; Add to unread list.
8876     (or (memq article gnus-newsgroup-unreads)
8877         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
8878     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
8879     ;; lists.  Otherwise, it must be added to the list.
8880     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8881     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8882     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8883     (if (= mark gnus-ticked-mark)
8884         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
8885     (if (= mark gnus-dormant-mark)
8886         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
8887
8888 (defalias 'gnus-summary-mark-as-unread-forward 
8889   'gnus-summary-tick-article-forward)
8890 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8891                'gnus-summary-tick-article-forward)
8892 (defun gnus-summary-tick-article-forward (n)
8893   "Tick N articles forwards.
8894 If N is negative, tick backwards instead.
8895 The difference between N and the number of articles ticked is returned."
8896   (interactive "p")
8897   (gnus-summary-mark-forward n gnus-ticked-mark))
8898
8899 (defalias 'gnus-summary-mark-as-unread-backward 
8900   'gnus-summary-tick-article-backward)
8901 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8902                'gnus-summary-tick-article-backward)
8903 (defun gnus-summary-tick-article-backward (n)
8904   "Tick N articles backwards.
8905 The difference between N and the number of articles ticked is returned."
8906   (interactive "p")
8907   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8908
8909 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8910 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8911 (defun gnus-summary-tick-article (&optional article clear-mark)
8912   "Mark current article as unread.
8913 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8914 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8915   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8916                                        gnus-ticked-mark)))
8917
8918 (defun gnus-summary-mark-as-read-forward (n)
8919   "Mark N articles as read forwards.
8920 If N is negative, mark backwards instead.
8921 The difference between N and the actual number of articles marked is
8922 returned."
8923   (interactive "p")
8924   (gnus-summary-mark-forward n gnus-del-mark t))
8925
8926 (defun gnus-summary-mark-as-read-backward (n)
8927   "Mark the N articles as read backwards.
8928 The difference between N and the actual number of articles marked is
8929 returned."
8930   (interactive "p")
8931   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8932
8933 (defun gnus-summary-mark-as-read (&optional article mark)
8934   "Mark current article as read.
8935 ARTICLE specifies the article to be marked as read.
8936 MARK specifies a string to be inserted at the beginning of the line."
8937   (gnus-summary-mark-article article mark))
8938
8939 (defun gnus-summary-clear-mark-forward (n)
8940   "Clear marks from N articles forward.
8941 If N is negative, clear backward instead.
8942 The difference between N and the number of marks cleared is returned."
8943   (interactive "p")
8944   (gnus-summary-mark-forward n gnus-unread-mark))
8945
8946 (defun gnus-summary-clear-mark-backward (n)
8947   "Clear marks from N articles backward.
8948 The difference between N and the number of marks cleared is returned."
8949   (interactive "p")
8950   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8951
8952 (defun gnus-summary-mark-unread-as-read ()
8953   "Intended to be used by `gnus-summary-mark-article-hook'."
8954   (or (memq gnus-current-article gnus-newsgroup-marked)
8955       (memq gnus-current-article gnus-newsgroup-dormant)
8956       (memq gnus-current-article gnus-newsgroup-expirable)
8957       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8958
8959 (defun gnus-summary-mark-region-as-read (point mark all)
8960   "Mark all unread articles between point and mark as read.
8961 If given a prefix, mark all articles between point and mark as read,
8962 even ticked and dormant ones."
8963   (interactive "r\nP")
8964   (save-excursion
8965     (goto-char point)
8966     (beginning-of-line)
8967     (while (and 
8968             (< (point) mark)
8969             (progn
8970               (and
8971                (or all
8972                    (and
8973                     (not (memq (gnus-summary-article-number)
8974                                gnus-newsgroup-marked))
8975                     (not (memq (gnus-summary-article-number)
8976                                gnus-newsgroup-dormant))))
8977                (gnus-summary-mark-article
8978                 (gnus-summary-article-number) gnus-del-mark))
8979               t)
8980             (zerop (forward-line 1))))))
8981
8982 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8983 (defalias 'gnus-summary-delete-marked-as-read 
8984   'gnus-summary-remove-lines-marked-as-read)
8985 (make-obsolete 'gnus-summary-delete-marked-as-read 
8986                'gnus-summary-remove-lines-marked-as-read)
8987 (defun gnus-summary-remove-lines-marked-as-read ()
8988   "Remove lines that are marked as read."
8989   (interactive)
8990   (gnus-summary-remove-lines-marked-with 
8991    (concat (mapconcat
8992             (lambda (char) (char-to-string (symbol-value char)))
8993             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
8994               gnus-killed-mark gnus-kill-file-mark
8995               gnus-low-score-mark gnus-expirable-mark)
8996             ""))))
8997
8998 (defalias 'gnus-summary-delete-marked-with 
8999   'gnus-summary-remove-lines-marked-with)
9000 (make-obsolete 'gnus-summary-delete-marked-with 
9001                'gnus-summary-remove-lines-marked-with)
9002 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9003 (defun gnus-summary-remove-lines-marked-with (marks)
9004   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9005   (interactive "sMarks: ")
9006   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9007   (gnus-set-global-variables)
9008   (let ((buffer-read-only nil)
9009         (marks (concat "^[" marks "]")))
9010     (goto-char (point-min))
9011     (if gnus-newsgroup-adaptive
9012         (gnus-score-remove-lines-adaptive marks)
9013       (while (re-search-forward marks nil t)
9014         (gnus-delete-line)))
9015     ;; If we use dummy roots, we have to do an additional sweep over
9016     ;; the buffer.
9017     (if (not (eq gnus-summary-make-false-root 'dummy))
9018         ()
9019       (goto-char (point-min))
9020       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9021       (while (re-search-forward marks nil t)
9022         (if (gnus-subject-equal
9023              (gnus-summary-subject-string)
9024              (progn
9025                (forward-line 1)
9026                (gnus-summary-subject-string)))
9027             ()
9028           (forward-line -1)
9029           (gnus-delete-line)))))
9030   (or (zerop (buffer-size))
9031       (if (eobp)
9032           (gnus-summary-prev-subject 1)
9033         (gnus-summary-position-cursor))))
9034
9035 (defun gnus-summary-expunge-below (score)
9036   "Remove articles with score less than SCORE."
9037   (interactive "P")
9038   (gnus-set-global-variables)
9039   (setq score (if score
9040                   (prefix-numeric-value score)
9041                 (or gnus-summary-default-score 0)))
9042   (save-excursion
9043     (set-buffer gnus-summary-buffer)
9044     (goto-char (point-min))
9045     (let ((buffer-read-only nil)
9046           beg)
9047       (while (not (eobp))
9048         (if (< (gnus-summary-article-score) score)
9049             (progn
9050               (setq beg (point))
9051               (forward-line 1)
9052               (delete-region beg (point)))
9053           (forward-line 1)))
9054       ;; Adjust point.
9055       (or (zerop (buffer-size))
9056           (if (eobp)
9057               (gnus-summary-prev-subject 1)
9058             (gnus-summary-position-cursor))))))
9059
9060 (defun gnus-summary-mark-below (score mark)
9061   "Mark articles with score less than SCORE with MARK."
9062   (interactive "P\ncMark: ")
9063   (gnus-set-global-variables)
9064   (setq score (if score
9065                   (prefix-numeric-value score)
9066                 (or gnus-summary-default-score 0)))
9067   (save-excursion
9068     (set-buffer gnus-summary-buffer)
9069     (goto-char (point-min))
9070     (while (not (eobp))
9071       (and (< (gnus-summary-article-score) score)
9072            (gnus-summary-mark-article nil mark))
9073       (forward-line 1))))
9074
9075 (defun gnus-summary-kill-below (score)
9076   "Mark articles with score below SCORE as read."
9077   (interactive "P")
9078   (gnus-summary-mark-below score gnus-killed-mark))
9079
9080 (defun gnus-summary-clear-above (score)
9081   "Clear all marks from articles with score above SCORE."
9082   (interactive "P")
9083   (gnus-summary-mark-above score gnus-unread-mark))
9084
9085 (defun gnus-summary-tick-above (score)
9086   "Tick all articles with score above SCORE."
9087   (interactive "P")
9088   (gnus-summary-mark-above score gnus-ticked-mark))
9089
9090 (defun gnus-summary-mark-above (score mark)
9091   "Mark articles with score over SCORE with MARK."
9092   (interactive "P\ncMark: ")
9093   (setq score (if score
9094                   (prefix-numeric-value score)
9095                 (or gnus-summary-default-score 0)))
9096   (save-excursion
9097     (set-buffer gnus-summary-buffer)
9098     (goto-char (point-min))
9099     (while (not (eobp))
9100       (if (> (gnus-summary-article-score) score)
9101           (progn
9102             (gnus-summary-mark-article nil mark)
9103             (forward-line 1))
9104         (forward-line 1)))))
9105
9106 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9107 (defun gnus-summary-show-all-expunged ()
9108   "Display all the hidden articles that were expunged for low scores."
9109   (interactive)
9110   (let ((buffer-read-only nil))
9111     (let ((scored gnus-newsgroup-scored)
9112           headers h)
9113       (while scored
9114         (or (gnus-summary-goto-subject (car (car scored)))
9115             (and (setq h (gnus-get-header-by-number (car (car scored))))
9116                  (< (cdr (car scored)) gnus-summary-expunge-below)
9117                  (setq headers (cons h headers))))
9118         (setq scored (cdr scored)))
9119       (or headers (error "No expunged articles hidden."))
9120       (goto-char (point-min))
9121       (save-excursion 
9122         (gnus-summary-update-lines 
9123          (point)
9124          (progn
9125            (gnus-summary-prepare-threads (nreverse headers) 0)
9126            (point)))))
9127     (goto-char (point-min))
9128     (gnus-summary-position-cursor)))
9129
9130 (defun gnus-summary-show-all-dormant ()
9131   "Display all the hidden articles that are marked as dormant."
9132   (interactive)
9133   (let ((buffer-read-only nil))
9134     (goto-char (point-min))
9135     (let ((dormant gnus-newsgroup-dormant)
9136           headers h)
9137       (while dormant
9138         (or (gnus-summary-goto-subject (car dormant))
9139             (and (setq h (gnus-get-header-by-number (car dormant)))
9140                  (setq headers (cons h headers))))
9141         (setq dormant (cdr dormant)))
9142       (or headers (error "No dormant articles hidden."))
9143       (save-excursion 
9144         (gnus-summary-update-lines 
9145          (point)
9146          (progn
9147            (gnus-summary-prepare-threads (nreverse headers) 0)
9148            (point)))))
9149     (goto-char (point-min))
9150     (gnus-summary-position-cursor)))
9151
9152 (defun gnus-summary-hide-all-dormant ()
9153   "Hide all dormant articles."
9154   (interactive)
9155   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9156   (gnus-summary-position-cursor))
9157
9158 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9159   "Mark all articles not marked as unread in this newsgroup as read.
9160 If prefix argument ALL is non-nil, all articles are marked as read.
9161 If QUIETLY is non-nil, no questions will be asked.
9162 If TO-HERE is non-nil, it should be a point in the buffer. All
9163 articles before this point will be marked as read.
9164 The number of articles marked as read is returned."
9165   (interactive "P")
9166   (prog1
9167       (if (or quietly
9168               (not gnus-interactive-catchup) ;Without confirmation?
9169               gnus-expert-user
9170               (gnus-y-or-n-p
9171                (if all
9172                    "Mark absolutely all articles as read? "
9173                  "Mark all unread articles as read? ")))
9174           (if (and not-mark 
9175                    (not gnus-newsgroup-adaptive)
9176                    (not gnus-newsgroup-auto-expire))
9177               (progn
9178                 (and all (setq gnus-newsgroup-marked nil
9179                                gnus-newsgroup-dormant nil))
9180                 (setq gnus-newsgroup-unreads 
9181                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9182             ;; We actually mark all articles as canceled, which we
9183             ;; have to do when using auto-expiry or adaptive scoring. 
9184             (let ((unreads (length gnus-newsgroup-unreads)))
9185               (if (gnus-summary-first-subject (not all))
9186                   (while (and (if to-here (< (point) to-here) t)
9187                               (gnus-summary-mark-as-read nil gnus-catchup-mark)
9188                               (gnus-summary-search-subject nil (not all)))))
9189               (- unreads (length gnus-newsgroup-unreads))
9190               (or to-here
9191                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9192     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9193       (if (and (not to-here) (eq 'nnvirtual (car method)))
9194           (nnvirtual-catchup-group
9195            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9196     (gnus-summary-position-cursor)))
9197
9198 (defun gnus-summary-catchup-to-here (&optional all)
9199   "Mark all unticked articles before the current one as read.
9200 If ALL is non-nil, also mark ticked and dormant articles as read."
9201   (interactive)
9202   (save-excursion
9203     (end-of-line)
9204     (gnus-summary-catchup all t (point))
9205     (gnus-set-mode-line 'summary))
9206   (gnus-summary-position-cursor))
9207
9208 (defun gnus-summary-catchup-all (&optional quietly)
9209   "Mark all articles in this newsgroup as read."
9210   (interactive)
9211   (gnus-summary-catchup t quietly))
9212
9213 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9214   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9215 If prefix argument ALL is non-nil, all articles are marked as read."
9216   (interactive "P")
9217   (gnus-summary-catchup all quietly nil 'fast)
9218   ;; Select next newsgroup or exit.
9219   (if (eq gnus-auto-select-next 'quietly)
9220       (gnus-summary-next-group nil)
9221     (gnus-summary-exit)))
9222
9223 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9224   "Mark all articles in this newsgroup as read, and then exit."
9225   (interactive)
9226   (gnus-summary-catchup-and-exit t quietly))
9227
9228 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9229 (defun gnus-summary-catchup-and-goto-next-group (all)
9230   "Mark all articles in this group as read and select the next group.
9231 If given a prefix, mark all articles, unread as well as ticked, as
9232 read." 
9233   (interactive "P")
9234   (gnus-summary-catchup all)
9235   (gnus-summary-next-group))
9236
9237 ;; Thread-based commands.
9238
9239 (defun gnus-summary-toggle-threads (arg)
9240   "Toggle showing conversation threads.
9241 If ARG is positive number, turn showing conversation threads on."
9242   (interactive "P")
9243   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9244     (setq gnus-show-threads
9245           (if (null arg) (not gnus-show-threads)
9246             (> (prefix-numeric-value arg) 0)))
9247     (gnus-summary-prepare)
9248     (gnus-summary-goto-subject current)
9249     (gnus-summary-position-cursor)))
9250
9251 (defun gnus-summary-show-all-threads ()
9252   "Show all threads."
9253   (interactive)
9254   (save-excursion
9255     (let ((buffer-read-only nil))
9256       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9257   (gnus-summary-position-cursor))
9258
9259 (defun gnus-summary-show-thread ()
9260   "Show thread subtrees.
9261 Returns nil if no thread was there to be shown."
9262   (interactive)
9263   (prog1
9264       (save-excursion
9265         (let ((buffer-read-only nil)
9266               ;; first goto end then to beg, to have point at beg after let
9267               (end (progn (end-of-line) (point)))
9268               (beg (progn (beginning-of-line) (point))))
9269           (prog1
9270               ;; Any hidden lines here?
9271               (search-forward "\r" end t)
9272             (subst-char-in-region beg end ?\^M ?\n t))))
9273     (gnus-summary-position-cursor)))
9274
9275 (defun gnus-summary-hide-all-threads ()
9276   "Hide all thread subtrees."
9277   (interactive)
9278   (save-excursion
9279     (goto-char (point-min))
9280     (gnus-summary-hide-thread)
9281     (while (and (not (eobp)) (zerop (forward-line 1)))
9282       (gnus-summary-hide-thread)))
9283   (gnus-summary-position-cursor))
9284
9285 (defun gnus-summary-hide-thread ()
9286   "Hide thread subtrees.
9287 Returns nil if no threads were there to be hidden."
9288   (interactive)
9289   (let ((buffer-read-only nil)
9290         (start (point))
9291         (level (gnus-summary-thread-level))
9292         (end (point)))
9293     ;; Go forward until either the buffer ends or the subthread
9294     ;; ends. 
9295     (if (eobp)
9296         ()
9297       (while (and (zerop (forward-line 1))
9298                   (> (gnus-summary-thread-level) level))
9299         (setq end (point)))
9300       (prog1
9301           (save-excursion
9302             (goto-char end)
9303             (search-backward "\n" start t))
9304         (subst-char-in-region start end ?\n ?\^M t)
9305         (forward-line -1)))))
9306
9307 (defun gnus-summary-go-to-next-thread (&optional previous)
9308   "Go to the same level (or less) next thread.
9309 If PREVIOUS is non-nil, go to previous thread instead.
9310 Return the article number moved to, or nil if moving was impossible."
9311   (let ((level (gnus-summary-thread-level))
9312         (article (gnus-summary-article-number)))
9313     (if previous 
9314         (while (and (zerop (forward-line -1))
9315                     (> (gnus-summary-thread-level) level)))
9316       (while (and (save-excursion
9317                     (forward-line 1)
9318                     (not (eobp)))
9319                   (zerop (forward-line 1))
9320                   (> (gnus-summary-thread-level) level))))
9321     (gnus-summary-recenter)
9322     (gnus-summary-position-cursor)
9323     (let ((oart (gnus-summary-article-number)))
9324       (and (/= oart article) oart))))
9325
9326 (defun gnus-summary-next-thread (n)
9327   "Go to the same level next N'th thread.
9328 If N is negative, search backward instead.
9329 Returns the difference between N and the number of skips actually
9330 done."
9331   (interactive "p")
9332   (let ((backward (< n 0))
9333         (n (abs n)))
9334   (while (and (> n 0)
9335               (gnus-summary-go-to-next-thread backward))
9336     (setq n (1- n)))
9337   (gnus-summary-position-cursor)
9338   (if (/= 0 n) (gnus-message 7 "No more threads"))
9339   n))
9340
9341 (defun gnus-summary-prev-thread (n)
9342   "Go to the same level previous N'th thread.
9343 Returns the difference between N and the number of skips actually
9344 done."
9345   (interactive "p")
9346   (gnus-summary-next-thread (- n)))
9347
9348 (defun gnus-summary-go-down-thread (&optional same)
9349   "Go down one level in the current thread.
9350 If SAME is non-nil, also move to articles of the same level."
9351   (let ((level (gnus-summary-thread-level))
9352         (start (point)))
9353     (if (and (zerop (forward-line 1))
9354              (> (gnus-summary-thread-level) level))
9355         t
9356       (goto-char start)
9357       nil)))
9358
9359 (defun gnus-summary-go-up-thread ()
9360   "Go up one level in the current thread."
9361   (let ((level (gnus-summary-thread-level))
9362         (start (point)))
9363     (while (and (zerop (forward-line -1))
9364                 (>= (gnus-summary-thread-level) level)))
9365     (if (>= (gnus-summary-thread-level) level)
9366         (progn
9367           (goto-char start)
9368           nil)
9369       t)))
9370
9371 (defun gnus-summary-down-thread (n)
9372   "Go down thread N steps.
9373 If N is negative, go up instead.
9374 Returns the difference between N and how many steps down that were
9375 taken."
9376   (interactive "p")
9377   (let ((up (< n 0))
9378         (n (abs n)))
9379   (while (and (> n 0)
9380               (if up (gnus-summary-go-up-thread)
9381                 (gnus-summary-go-down-thread)))
9382     (setq n (1- n)))
9383   (gnus-summary-position-cursor)
9384   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9385   n))
9386
9387 (defun gnus-summary-up-thread (n)
9388   "Go up thread N steps.
9389 If N is negative, go up instead.
9390 Returns the difference between N and how many steps down that were
9391 taken."
9392   (interactive "p")
9393   (gnus-summary-down-thread (- n)))
9394
9395 (defun gnus-summary-kill-thread (unmark)
9396   "Mark articles under current thread as read.
9397 If the prefix argument is positive, remove any kinds of marks.
9398 If the prefix argument is negative, tick articles instead."
9399   (interactive "P")
9400   (if unmark
9401       (setq unmark (prefix-numeric-value unmark)))
9402   (let ((killing t)
9403         (level (gnus-summary-thread-level)))
9404     (save-excursion
9405       (while killing
9406         ;; Mark the article...
9407         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9408               ((> unmark 0) (gnus-summary-tick-article nil t))
9409               (t (gnus-summary-tick-article)))
9410         ;; ...and go forward until either the buffer ends or the subtree
9411         ;; ends. 
9412         (if (not (and (zerop (forward-line 1))
9413                       (> (gnus-summary-thread-level) level)))
9414             (setq killing nil))))
9415     ;; Hide killed subtrees.
9416     (and (null unmark)
9417          gnus-thread-hide-killed
9418          (gnus-summary-hide-thread))
9419     ;; If marked as read, go to next unread subject.
9420     (if (null unmark)
9421         ;; Go to next unread subject.
9422         (gnus-summary-next-subject 1 t)))
9423   (gnus-set-mode-line 'summary))
9424
9425 ;; Summary sorting commands
9426
9427 (defun gnus-summary-sort-by-number (&optional reverse)
9428   "Sort summary buffer by article number.
9429 Argument REVERSE means reverse order."
9430   (interactive "P")
9431   (gnus-summary-sort 
9432    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9433
9434 (defun gnus-summary-sort-by-author (&optional reverse)
9435   "Sort summary buffer by author name alphabetically.
9436 If case-fold-search is non-nil, case of letters is ignored.
9437 Argument REVERSE means reverse order."
9438   (interactive "P")
9439   (gnus-summary-sort
9440    (cons
9441     (lambda ()
9442       (let ((extract (funcall
9443                       gnus-extract-address-components
9444                       (header-from (gnus-get-header-by-number
9445                                     (gnus-summary-article-number))))))
9446         (or (car extract) (cdr extract))))
9447     'gnus-thread-sort-by-author)
9448    reverse))
9449
9450 (defun gnus-summary-sort-by-subject (&optional reverse)
9451   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9452 If case-fold-search is non-nil, case of letters is ignored.
9453 Argument REVERSE means reverse order."
9454   (interactive "P")
9455   (gnus-summary-sort
9456    (cons
9457     (lambda ()
9458       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9459     'gnus-thread-sort-by-subject)
9460    reverse))
9461
9462 (defun gnus-summary-sort-by-date (&optional reverse)
9463   "Sort summary buffer by date.
9464 Argument REVERSE means reverse order."
9465   (interactive "P")
9466   (gnus-summary-sort
9467    (cons
9468     (lambda ()
9469       (gnus-sortable-date
9470        (header-date 
9471         (gnus-get-header-by-number (gnus-summary-article-number)))))
9472     'gnus-thread-sort-by-date)
9473    reverse))
9474
9475 (defun gnus-summary-sort-by-score (&optional reverse)
9476   "Sort summary buffer by score.
9477 Argument REVERSE means reverse order."
9478   (interactive "P")
9479   (gnus-summary-sort 
9480    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9481    (not reverse)))
9482
9483 (defvar gnus-summary-already-sorted nil)
9484 (defun gnus-summary-sort (predicate reverse)
9485   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9486   (if gnus-summary-already-sorted
9487       ()
9488     (let (buffer-read-only)
9489       (if (not gnus-show-threads)
9490           (progn
9491             (goto-char (point-min))
9492             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9493         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9494               (gnus-summary-prepare-hook nil)
9495               (gnus-summary-already-sorted nil))
9496           (gnus-summary-prepare)
9497           (and gnus-show-threads
9498                gnus-thread-hide-subtree
9499                (gnus-summary-hide-all-threads))
9500           ;; If in async mode, we send some info to the backend.
9501           (and gnus-newsgroup-async
9502                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9503                (gnus-request-asynchronous 
9504                 gnus-newsgroup-name
9505                 (if (and gnus-asynchronous-article-function
9506                          (fboundp gnus-asynchronous-article-function))
9507                     (funcall gnus-asynchronous-article-function
9508                              gnus-newsgroup-threads)))))))))
9509
9510   
9511 (defun gnus-sortable-date (date)
9512   "Make sortable string by string-lessp from DATE.
9513 Timezone package is used."
9514   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9515          (year   (aref date 0))
9516          (month  (aref date 1))
9517          (day    (aref date 2)))
9518     (timezone-make-sortable-date 
9519      year month day 
9520      (timezone-make-time-string
9521       (aref date 3) (aref date 4) (aref date 5)))))
9522
9523
9524 ;; Summary saving commands.
9525
9526 (defun gnus-summary-save-article (n)
9527   "Save the current article using the default saver function.
9528 If N is a positive number, save the N next articles.
9529 If N is a negative number, save the N previous articles.
9530 If N is nil and any articles have been marked with the process mark,
9531 save those articles instead.
9532 The variable `gnus-default-article-saver' specifies the saver function."
9533   (interactive "P")
9534   (let ((articles (gnus-summary-work-articles n)))
9535     (while articles
9536       (let ((header (gnus-get-header-by-number (car articles))))
9537         (if (vectorp header)
9538             (progn
9539               (gnus-summary-select-article t nil nil (car articles))
9540               (or gnus-save-all-headers
9541                   (gnus-article-hide-headers t))
9542               ;; Remove any X-Gnus lines.
9543               (save-excursion
9544                 (save-restriction
9545                   (set-buffer gnus-article-buffer)
9546                   (let ((buffer-read-only nil))
9547                     (goto-char (point-min))
9548                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9549                                                   (point-max)))
9550                     (while (re-search-forward "^X-Gnus" nil t)
9551                       (beginning-of-line)
9552                       (delete-region (point)
9553                                      (progn (forward-line 1) (point))))
9554                     (widen))))
9555               (save-excursion
9556                 (if gnus-default-article-saver
9557                     (funcall gnus-default-article-saver)
9558                   (error "No default saver is defined."))))
9559           (if (assq 'name header)
9560               (gnus-copy-file (cdr (assq 'name header)))
9561             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9562       (gnus-summary-remove-process-mark (car articles))
9563       (setq articles (cdr articles)))
9564     (gnus-summary-position-cursor)
9565     n))
9566
9567 (defun gnus-summary-pipe-output (arg)
9568   "Pipe the current article to a subprocess.
9569 If N is a positive number, pipe the N next articles.
9570 If N is a negative number, pipe the N previous articles.
9571 If N is nil and any articles have been marked with the process mark,
9572 pipe those articles instead."
9573   (interactive "P")
9574   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9575     (gnus-summary-save-article arg)))
9576
9577 (defun gnus-summary-save-article-mail (arg)
9578   "Append the current article to an mail file.
9579 If N is a positive number, save the N next articles.
9580 If N is a negative number, save the N previous articles.
9581 If N is nil and any articles have been marked with the process mark,
9582 save those articles instead."
9583   (interactive "P")
9584   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9585     (gnus-summary-save-article arg)))
9586
9587 (defun gnus-summary-save-article-rmail (arg)
9588   "Append the current article to an rmail file.
9589 If N is a positive number, save the N next articles.
9590 If N is a negative number, save the N previous articles.
9591 If N is nil and any articles have been marked with the process mark,
9592 save those articles instead."
9593   (interactive "P")
9594   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9595     (gnus-summary-save-article arg)))
9596
9597 (defun gnus-summary-save-article-file (arg)
9598   "Append the current article to a file.
9599 If N is a positive number, save the N next articles.
9600 If N is a negative number, save the N previous articles.
9601 If N is nil and any articles have been marked with the process mark,
9602 save those articles instead."
9603   (interactive "P")
9604   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9605     (gnus-summary-save-article arg)))
9606
9607 (defun gnus-read-save-file-name (prompt default-name)
9608   (let ((methods gnus-split-methods)
9609         split-name)
9610     (if (not gnus-split-methods)
9611         ()
9612       (save-excursion
9613         (set-buffer gnus-article-buffer)
9614         (gnus-narrow-to-headers)
9615         (while methods
9616           (goto-char (point-min))
9617           (and (condition-case () 
9618                    (re-search-forward (car (car methods)) nil t)
9619                  (error nil))
9620                (setq split-name (cons (nth 1 (car methods)) split-name)))
9621           (setq methods (cdr methods)))
9622         (widen)))
9623     (cond ((null split-name)
9624            (read-file-name
9625             (concat prompt " (default "
9626                     (file-name-nondirectory default-name) ") ")
9627             (file-name-directory default-name)
9628             default-name))
9629           ((= 1 (length split-name))
9630            (read-file-name
9631             (concat prompt " (default " (car split-name) ") ")
9632             gnus-article-save-directory
9633             (concat gnus-article-save-directory (car split-name))))
9634           (t
9635            (setq split-name (mapcar (lambda (el) (list el))
9636                                     (nreverse split-name)))
9637            (let ((result (completing-read 
9638                           (concat prompt " ")
9639                           split-name nil nil)))
9640              (concat gnus-article-save-directory
9641                      (if (string= result "")
9642                          (car (car split-name))
9643                        result)))))))
9644
9645 (defun gnus-summary-save-in-rmail (&optional filename)
9646   "Append this article to Rmail file.
9647 Optional argument FILENAME specifies file name.
9648 Directory to save to is default to `gnus-article-save-directory' which
9649 is initialized from the SAVEDIR environment variable."
9650   (interactive)
9651   (let ((default-name
9652           (funcall gnus-rmail-save-name gnus-newsgroup-name
9653                    gnus-current-headers gnus-newsgroup-last-rmail)))
9654     (or filename
9655         (setq filename (gnus-read-save-file-name 
9656                         "Save in rmail file:" default-name)))
9657     (gnus-make-directory (file-name-directory filename))
9658     (gnus-eval-in-buffer-window 
9659      gnus-article-buffer
9660      (save-excursion
9661        (save-restriction
9662          (widen)
9663          (gnus-output-to-rmail filename))))
9664     ;; Remember the directory name to save articles.
9665     (setq gnus-newsgroup-last-rmail filename)))
9666
9667 (defun gnus-summary-save-in-mail (&optional filename)
9668   "Append this article to Unix mail file.
9669 Optional argument FILENAME specifies file name.
9670 Directory to save to is default to `gnus-article-save-directory' which
9671 is initialized from the SAVEDIR environment variable."
9672   (interactive)
9673   (let ((default-name
9674           (funcall gnus-mail-save-name gnus-newsgroup-name
9675                    gnus-current-headers gnus-newsgroup-last-mail)))
9676     (or filename
9677         (setq filename (gnus-read-save-file-name 
9678                         "Save in Unix mail file:" default-name)))
9679     (setq filename
9680           (expand-file-name filename
9681                             (and default-name
9682                                  (file-name-directory default-name))))
9683     (gnus-make-directory (file-name-directory filename))
9684     (gnus-eval-in-buffer-window 
9685      gnus-article-buffer
9686      (save-excursion
9687        (save-restriction
9688          (widen)
9689          (if (and (file-readable-p filename) (rmail-file-p filename))
9690              (gnus-output-to-rmail filename)
9691            (rmail-output filename 1 t t)))))
9692     ;; Remember the directory name to save articles.
9693     (setq gnus-newsgroup-last-mail filename)))
9694
9695 (defun gnus-summary-save-in-file (&optional filename)
9696   "Append this article to file.
9697 Optional argument FILENAME specifies file name.
9698 Directory to save to is default to `gnus-article-save-directory' which
9699 is initialized from the SAVEDIR environment variable."
9700   (interactive)
9701   (let ((default-name
9702           (funcall gnus-file-save-name gnus-newsgroup-name
9703                    gnus-current-headers gnus-newsgroup-last-file)))
9704     (or filename
9705         (setq filename (gnus-read-save-file-name 
9706                         "Save in file:" default-name)))
9707     (gnus-make-directory (file-name-directory filename))
9708     (gnus-eval-in-buffer-window 
9709      gnus-article-buffer
9710      (save-excursion
9711        (save-restriction
9712          (widen)
9713          (gnus-output-to-file filename))))
9714     ;; Remember the directory name to save articles.
9715     (setq gnus-newsgroup-last-file filename)))
9716
9717 (defun gnus-summary-save-in-pipe (&optional command)
9718   "Pipe this article to subprocess."
9719   (interactive)
9720   (let ((command (read-string "Shell command on article: "
9721                               gnus-last-shell-command)))
9722     (if (string-equal command "")
9723         (setq command gnus-last-shell-command))
9724     (gnus-eval-in-buffer-window 
9725      gnus-article-buffer
9726      (save-restriction
9727        (widen)
9728        (shell-command-on-region (point-min) (point-max) command nil)))
9729     (setq gnus-last-shell-command command)))
9730
9731 ;; Summary extract commands
9732
9733 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9734   (let ((buffer-read-only nil)
9735         (article (gnus-summary-article-number))
9736         b)
9737     (or (gnus-summary-goto-subject article)
9738         (error (format "No such article: %d" article)))
9739     (or gnus-newsgroup-headers-hashtb-by-number
9740         (gnus-make-headers-hashtable-by-number))
9741     (gnus-summary-position-cursor)
9742     ;; If all commands are to be bunched up on one line, we collect
9743     ;; them here.  
9744     (if gnus-view-pseudos-separately
9745         ()
9746       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9747             files action)
9748         (while ps
9749           (setq action (cdr (assq 'action (car ps))))
9750           (setq files (list (cdr (assq 'name (car ps)))))
9751           (while (and ps (cdr ps)
9752                       (string= (or action "1")
9753                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9754             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9755             (setcdr ps (cdr (cdr ps))))
9756           (if (not files)
9757               ()
9758             (if (not (string-match "%s" action))
9759                 (setq files (cons " " files)))
9760             (setq files (cons " " files))
9761             (and (assq 'execute (car ps))
9762                  (setcdr (assq 'execute (car ps))
9763                          (funcall (if (string-match "%s" action)
9764                                       'format 'concat)
9765                                   action 
9766                                   (mapconcat (lambda (f) f) files " ")))))
9767           (setq ps (cdr ps)))))
9768     (if (and gnus-view-pseudos (not not-view))
9769         (while pslist
9770           (and (assq 'execute (car pslist))
9771                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9772                                      (eq gnus-view-pseudos 'not-confirm)))
9773           (setq pslist (cdr pslist)))
9774       (save-excursion
9775         (while pslist
9776           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9777                                          (gnus-summary-article-number)))
9778           (forward-line 1)
9779           (setq b (point))
9780           (insert "          " (file-name-nondirectory 
9781                                 (cdr (assq 'name (car pslist))))
9782                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9783           (add-text-properties 
9784            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9785                           'gnus-mark gnus-unread-mark 
9786                           'gnus-level 0
9787                           'gnus-pseudo (car pslist)))
9788           (forward-line -1)
9789           (gnus-sethash (int-to-string gnus-reffed-article-number)
9790                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9791           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9792           (setq pslist (cdr pslist)))))))
9793
9794 (defun gnus-pseudos< (p1 p2)
9795   (let ((c1 (cdr (assq 'action p1)))
9796         (c2 (cdr (assq 'action p2))))
9797     (and c1 c2 (string< c1 c2))))
9798
9799 (defun gnus-request-pseudo-article (props)
9800   (cond ((assq 'execute props)
9801          (gnus-execute-command (cdr (assq 'execute props)))))
9802   (let ((gnus-current-article (gnus-summary-article-number)))
9803     (run-hooks 'gnus-mark-article-hook)))
9804
9805 (defun gnus-execute-command (command &optional automatic)
9806   (save-excursion
9807     (gnus-article-setup-buffer)
9808     (set-buffer gnus-article-buffer)
9809     (let ((command (if automatic command (read-string "Command: " command)))
9810           (buffer-read-only nil))
9811       (erase-buffer)
9812       (insert "$ " command "\n\n")
9813       (if gnus-view-pseudo-asynchronously
9814           (start-process "gnus-execute" nil "sh" "-c" command)
9815         (call-process "sh" nil t nil "-c" command)))))
9816
9817 (defun gnus-copy-file (file &optional to)
9818   "Copy FILE to TO."
9819   (interactive
9820    (list (read-file-name "Copy file: " default-directory)
9821          (read-file-name "Copy file to: " default-directory)))
9822   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9823   (and (file-directory-p to) 
9824        (setq to (concat (file-name-as-directory to)
9825                         (file-name-nondirectory file))))
9826   (copy-file file to))
9827
9828 ;; Summary kill commands.
9829
9830 (defun gnus-summary-edit-global-kill (article)
9831   "Edit the \"global\" kill file."
9832   (interactive (list (gnus-summary-article-number)))
9833   (gnus-group-edit-global-kill article))
9834
9835 (defun gnus-summary-edit-local-kill ()
9836   "Edit a local kill file applied to the current newsgroup."
9837   (interactive)
9838   (setq gnus-current-headers 
9839         (gnus-gethash 
9840          (int-to-string (gnus-summary-article-number))
9841          gnus-newsgroup-headers-hashtb-by-number))
9842   (gnus-set-global-variables)
9843   (gnus-group-edit-local-kill 
9844    (gnus-summary-article-number) gnus-newsgroup-name))
9845
9846 \f
9847 ;;;
9848 ;;; Gnus article mode
9849 ;;;
9850
9851 (put 'gnus-article-mode 'mode-class 'special)
9852
9853 (if gnus-article-mode-map
9854     nil
9855   (setq gnus-article-mode-map (make-keymap))
9856   (suppress-keymap gnus-article-mode-map)
9857   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9858   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9859   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9860   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9861   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9862   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9863   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9864   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9865   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9866   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9867   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9868   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
9869   
9870   ;; Duplicate almost all summary keystrokes in the article mode map.
9871   (let ((commands 
9872          (list 
9873           " " "\177" "\r" "n" "p" "N" "P" "\M-\C-n" "\M-\C-p"
9874           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
9875           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
9876           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
9877           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
9878           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
9879           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
9880           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
9881           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
9882           "\C-c\C-i" "x" "X" "s" "t" "g" "?" "l"
9883           "\C-c\C-v\C-v" "\C-d" "v" 
9884 ;;        "Mt" "M!" "Md" "Mr"
9885 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
9886 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
9887 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
9888 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
9889 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
9890 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
9891 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
9892 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
9893 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
9894 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
9895 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
9896           )))
9897     (while commands
9898       (define-key gnus-article-mode-map (car commands) 
9899         'gnus-article-summary-command)
9900       (setq commands (cdr commands))))
9901
9902   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
9903 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
9904                          "=")))
9905     (while commands
9906       (define-key gnus-article-mode-map (car commands) 
9907         'gnus-article-summary-command-nosave)
9908       (setq commands (cdr commands)))))
9909
9910
9911 (defun gnus-article-mode ()
9912   "Major mode for displaying an article.
9913
9914 All normal editing commands are switched off.
9915
9916 The following commands are available:
9917
9918 \\<gnus-article-mode-map>
9919 \\[gnus-article-next-page]\t Scroll the article one page forwards
9920 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9921 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9922 \\[gnus-article-show-summary]\t Display the summary buffer
9923 \\[gnus-article-mail]\t Send a reply to the address near point
9924 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9925 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9926 \\[gnus-info-find-node]\t Go to the Gnus info node"
9927   (interactive)
9928   (if gnus-visual (gnus-article-make-menu-bar))
9929   (kill-all-local-variables)
9930   (setq mode-line-modified "-- ")
9931   (make-local-variable 'mode-line-format)
9932   (setq mode-line-format (copy-sequence mode-line-format))
9933   (and (equal (nth 3 mode-line-format) "   ")
9934        (setcar (nthcdr 3 mode-line-format) ""))
9935   (setq mode-name "Article")
9936   (setq major-mode 'gnus-article-mode)
9937   (make-local-variable 'minor-mode-alist)
9938   (or (assq 'gnus-show-mime minor-mode-alist)
9939       (setq minor-mode-alist
9940             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9941   (use-local-map gnus-article-mode-map)
9942   (make-local-variable 'page-delimiter)
9943   (setq page-delimiter gnus-page-delimiter)
9944   (buffer-disable-undo (current-buffer))
9945   (setq buffer-read-only t)             ;Disable modification
9946   (run-hooks 'gnus-article-mode-hook))
9947
9948 (defun gnus-article-setup-buffer ()
9949   "Initialize article mode buffer."
9950   (if (get-buffer gnus-article-buffer)
9951       (save-excursion
9952         (set-buffer gnus-article-buffer)
9953         (buffer-disable-undo (current-buffer))
9954         (setq buffer-read-only t)
9955         (gnus-add-current-to-buffer-list)
9956         (or (eq major-mode 'gnus-article-mode)
9957             (gnus-article-mode)))
9958     (save-excursion
9959       (set-buffer (get-buffer-create gnus-article-buffer))
9960       (gnus-add-current-to-buffer-list)
9961       (gnus-article-mode))))
9962
9963 ;; Set article window start at LINE, where LINE is the number of lines
9964 ;; from the head of the article.
9965 (defun gnus-article-set-window-start (&optional line)
9966   (set-window-start 
9967    (get-buffer-window gnus-article-buffer)
9968    (save-excursion
9969      (set-buffer gnus-article-buffer)
9970      (goto-char (point-min))
9971      (if (not line)
9972          (point-min)
9973        (gnus-message 6 "Moved to bookmark")
9974        (search-forward "\n\n" nil t)
9975        (forward-line line)
9976        (point)))))
9977
9978 (defun gnus-request-article-this-buffer (article group)
9979   "Get an article and insert it into this buffer."
9980   (setq group (or group gnus-newsgroup-name))
9981
9982   ;; Open server if it has closed.
9983   (gnus-check-news-server (gnus-find-method-for-group group))
9984
9985   ;; Using `gnus-request-article' directly will insert the article into
9986   ;; `nntp-server-buffer' - so we'll save some time by not having to
9987   ;; copy it from the server buffer into the article buffer.
9988
9989   ;; We only request an article by message-id when we do not have the
9990   ;; headers for it, so we'll have to get those.
9991   (and (stringp article) 
9992        (let ((gnus-override-method gnus-refer-article-method))
9993          (gnus-read-header article)))
9994
9995   ;; If the article number is negative, that means that this article
9996   ;; doesn't belong in this newsgroup (possibly), so we find its
9997   ;; message-id and request it by id instead of number.
9998   (if (not (numberp article))
9999       ()
10000     (save-excursion
10001       (set-buffer gnus-summary-buffer)
10002       (let ((header (gnus-get-header-by-number article)))
10003         (if (< article 0)
10004             (if (vectorp header)
10005                 ;; It's a real article.
10006                 (setq article (header-id header))
10007               ;; It is an extracted pseudo-article.
10008               (setq article 'pseudo)
10009               (gnus-request-pseudo-article header)))
10010
10011         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10012           (if (not (eq (car method) 'nneething))
10013               ()
10014             (let ((dir (concat (file-name-as-directory (nth 1 method))
10015                                (header-subject header))))
10016               (if (file-directory-p dir)
10017                   (progn
10018                     (setq article 'nneething)
10019                     (gnus-group-enter-directory dir)))))))))
10020
10021   ;; Check the cache.
10022   (if (and gnus-use-cache
10023            (numberp article)
10024            (gnus-cache-request-article article group))
10025       'article
10026     ;; Get the article and into the article buffer.
10027     (if (or (stringp article) (numberp article))
10028         (progn
10029           (erase-buffer)
10030           (let ((gnus-override-method 
10031                  (and (stringp article) gnus-refer-article-method)))
10032             (and (gnus-request-article article group (current-buffer))
10033                  'article)))
10034       article)))
10035
10036 (defun gnus-read-header (id)
10037   "Read the headers of article ID and enter them into the Gnus system."
10038   (or gnus-newsgroup-headers-hashtb-by-number
10039       (gnus-make-headers-hashtable-by-number))
10040   (let (header)
10041     (if (not (setq header 
10042                    (car (if (let ((gnus-nov-is-evil t))
10043                               (gnus-retrieve-headers 
10044                                (list id) gnus-newsgroup-name))
10045                             (gnus-get-newsgroup-headers)))))
10046         nil
10047       (if (stringp id)
10048           (header-set-number header gnus-reffed-article-number))
10049       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10050       (gnus-sethash (int-to-string (header-number header)) header
10051                     gnus-newsgroup-headers-hashtb-by-number)
10052       (if (stringp id)
10053           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10054       (setq gnus-current-headers header)
10055       header)))
10056
10057 (defun gnus-article-prepare (article &optional all-headers header)
10058   "Prepare ARTICLE in article mode buffer.
10059 ARTICLE should either be an article number or a Message-ID.
10060 If ARTICLE is an id, HEADER should be the article headers.
10061 If ALL-HEADERS is non-nil, no headers are hidden."
10062   (save-excursion
10063     ;; Make sure we start in a summary buffer.
10064     (or (eq major-mode 'gnus-summary-mode)
10065         (set-buffer gnus-summary-buffer))
10066     (setq gnus-summary-buffer (current-buffer))
10067     ;; Make sure the connection to the server is alive.
10068     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10069         (progn
10070           (gnus-check-news-server 
10071            (gnus-find-method-for-group gnus-newsgroup-name))
10072           (gnus-request-group gnus-newsgroup-name t)))
10073     (or gnus-newsgroup-headers-hashtb-by-number
10074         (gnus-make-headers-hashtable-by-number))
10075     (let* ((article (if header (header-number header) article))
10076            (summary-buffer (current-buffer))
10077            (internal-hook gnus-article-internal-prepare-hook)
10078            (group gnus-newsgroup-name)
10079            result)
10080       (save-excursion
10081         (gnus-article-setup-buffer)
10082         (set-buffer gnus-article-buffer)
10083         (if (not (setq result (let ((buffer-read-only nil))
10084                                 (gnus-request-article-this-buffer 
10085                                  article group))))
10086             ;; There is no such article.
10087             (save-excursion
10088               (if (not (numberp article))
10089                   ()
10090                 (setq gnus-article-current 
10091                       (cons gnus-newsgroup-name article))
10092                 (set-buffer gnus-summary-buffer)
10093                 (setq gnus-current-article article)
10094                 (gnus-summary-mark-as-read article gnus-canceled-mark))
10095               (gnus-message 1 "No such article (may be canceled)")
10096               (ding)
10097               nil)
10098           (if (or (eq result 'pseudo) (eq result 'nneething))
10099               (progn
10100                 (save-excursion
10101                   (set-buffer summary-buffer)
10102                   (setq gnus-last-article gnus-current-article
10103                         gnus-newsgroup-history (cons gnus-current-article
10104                                                      gnus-newsgroup-history)
10105                         gnus-current-article 0
10106                         gnus-current-headers nil
10107                         gnus-article-current nil)
10108                   (if (eq result 'nneething)
10109                       (gnus-configure-windows 'summary)
10110                     (gnus-configure-windows 'article))
10111                   (gnus-set-global-variables))
10112                 (gnus-set-mode-line 'article))
10113             ;; The result from the `request' was an actual article -
10114             ;; or at least some text that is now displayed in the
10115             ;; article buffer.
10116             (if (and (numberp article)
10117                      (not (eq article gnus-current-article)))
10118                 ;; Seems like a new article has been selected.
10119                 ;; `gnus-current-article' must be an article number.
10120                 (save-excursion
10121                   (set-buffer summary-buffer)
10122                   (setq gnus-last-article gnus-current-article
10123                         gnus-newsgroup-history (cons gnus-current-article
10124                                                      gnus-newsgroup-history)
10125                         gnus-current-article article
10126                         gnus-current-headers 
10127                         (gnus-get-header-by-number gnus-current-article)
10128                         gnus-article-current 
10129                         (cons gnus-newsgroup-name gnus-current-article))
10130                   (gnus-summary-show-thread)
10131                   (run-hooks 'gnus-mark-article-hook)
10132                   (gnus-set-mode-line 'summary)
10133                   (and gnus-visual 
10134                        (run-hooks 'gnus-visual-mark-article-hook))
10135                   ;; Set the global newsgroup variables here.
10136                   ;; Suggested by Jim Sisolak
10137                   ;; <sisolak@trans4.neep.wisc.edu>.
10138                   (gnus-set-global-variables)
10139                   (and gnus-use-cache 
10140                        (gnus-cache-possibly-enter-article
10141                         group article
10142                         (gnus-get-header-by-number article)
10143                         (memq article gnus-newsgroup-marked)
10144                         (memq article gnus-newsgroup-dormant)
10145                         (memq article gnus-newsgroup-unreads)))))
10146             ;; gnus-have-all-headers must be either T or NIL.
10147             (setq gnus-have-all-headers
10148                   (not (not (or all-headers gnus-show-all-headers))))
10149             ;; Hooks for getting information from the article.
10150             ;; This hook must be called before being narrowed.
10151             (let (buffer-read-only)
10152               (run-hooks 'internal-hook)
10153               (run-hooks 'gnus-article-prepare-hook)
10154               ;; Decode MIME message.
10155               (if (and gnus-show-mime
10156                        (or (not gnus-strict-mime)
10157                            (gnus-fetch-field "Mime-Version")))
10158                   (funcall gnus-show-mime-method))
10159               ;; Perform the article display hooks.
10160               (run-hooks 'gnus-article-display-hook))
10161             ;; Do page break.
10162             (goto-char (point-min))
10163             (and gnus-break-pages (gnus-narrow-to-page))
10164             (gnus-set-mode-line 'article)
10165             (gnus-configure-windows 'article)
10166             (goto-char (point-min))
10167             t))))))
10168
10169 (defun gnus-article-show-all-headers ()
10170   "Show all article headers in article mode buffer."
10171   (save-excursion 
10172     (gnus-article-setup-buffer)
10173     (set-buffer gnus-article-buffer)
10174     (let ((buffer-read-only nil))
10175       (remove-text-properties (point-min) (point-max) 
10176                               gnus-hidden-properties))))
10177
10178 (defun gnus-article-hide-headers-if-wanted ()
10179   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10180 Provided for backwards compatability."
10181   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10182       (gnus-article-hide-headers)))
10183
10184 (defun gnus-article-hide-headers (&optional delete)
10185   "Hide unwanted headers and possibly sort them as well."
10186   (interactive "P")
10187   (save-excursion
10188     (set-buffer gnus-article-buffer)
10189     (save-restriction
10190       (let ((sorted gnus-sorted-header-list)
10191             (buffer-read-only nil)
10192             want-list beg want-l)
10193         ;; First we narrow to just the headers.
10194         (widen)
10195         (goto-char (point-min))
10196         ;; Hide any "From " lines at the beginning of (mail) articles. 
10197         (while (looking-at "From ")
10198           (forward-line 1))
10199         (if (bobp) 
10200             (add-text-properties (point-min) (point) gnus-hidden-properties))
10201         ;; Then treat the rest of the header lines.
10202         (narrow-to-region 
10203          (point) 
10204          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10205         ;; Then we use the two regular expressions
10206         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10207         ;; select which header lines is to remain visible in the
10208         ;; article buffer.
10209         (goto-char (point-min))
10210         (while (re-search-forward "^[^ \t]*:" nil t)
10211           (beginning-of-line)
10212           ;; We add the headers we want to keep to a list and delete
10213           ;; them from the buffer.
10214           (if (or (and (stringp gnus-visible-headers)
10215                        (looking-at gnus-visible-headers))
10216                   (and (not (stringp gnus-visible-headers))
10217                        (stringp gnus-ignored-headers)
10218                        (not (looking-at gnus-ignored-headers))))
10219               (progn
10220                 (setq beg (point))
10221                 (forward-line 1)
10222                 ;; Be sure to get multi-line headers...
10223                 (re-search-forward "^[^ \t]*:" nil t)
10224                 (beginning-of-line)
10225                 (setq want-list 
10226                       (cons (buffer-substring beg (point)) want-list))
10227                 (delete-region beg (point))
10228                 (goto-char beg))
10229             (forward-line 1)))
10230         ;; Next we perform the sorting by looking at
10231         ;; `gnus-sorted-header-list'. 
10232         (goto-char (point-min))
10233         (while (and sorted want-list)
10234           (setq want-l want-list)
10235           (while (and want-l
10236                       (not (string-match (car sorted) (car want-l))))
10237             (setq want-l (cdr want-l)))
10238           (if want-l 
10239               (progn
10240                 (insert (car want-l))
10241                 (setq want-list (delq (car want-l) want-list))))
10242           (setq sorted (cdr sorted)))
10243         ;; Any headers that were not matched by the sorted list we
10244         ;; just tack on the end of the visible header list.
10245         (while want-list
10246           (insert (car want-list))
10247           (setq want-list (cdr want-list)))
10248         ;; And finally we make the unwanted headers invisible.
10249         (if delete
10250             (delete-region (point) (point-max))
10251           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10252           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10253
10254 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10255 (defun gnus-article-treat-overstrike ()
10256   "Translate overstrikes into bold text."
10257   (interactive)
10258   (save-excursion
10259     (set-buffer gnus-article-buffer)
10260     (let ((buffer-read-only nil))
10261       (while (search-forward "\b" nil t)
10262         (let ((next (following-char))
10263               (previous (char-after (- (point) 2))))
10264           (cond ((eq next previous)
10265                  (delete-region (- (point) 2) (point))
10266                  (put-text-property (point) (1+ (point))
10267                                     'face 'bold))
10268                 ((eq next ?_)
10269                  (delete-region (1- (point)) (1+ (point)))
10270                  (put-text-property (1- (point)) (point)
10271                                     'face 'underline))
10272                 ((eq previous ?_)
10273                  (delete-region (- (point) 2) (point))
10274                  (put-text-property (point) (1+ (point))
10275                                     'face 'underline))))))))
10276
10277 (defun gnus-article-word-wrap ()
10278   "Format too long lines."
10279   (interactive)
10280   (save-excursion
10281     (set-buffer gnus-article-buffer)
10282     (let ((buffer-read-only nil))
10283       (goto-char (point-min))
10284       (search-forward "\n\n" nil t)
10285       (end-of-line 1)
10286       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10287             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10288             (adaptive-fill-mode t))
10289         (while (not (eobp))
10290           (and (>= (current-column) (min fill-column (window-width)))
10291                (/= (preceding-char) ?:)
10292                (fill-paragraph nil))
10293           (end-of-line 2))))))
10294
10295 (defun gnus-article-remove-cr ()
10296   "Remove carriage returns from an article."
10297   (interactive)
10298   (save-excursion
10299     (set-buffer gnus-article-buffer)
10300     (let ((buffer-read-only nil))
10301       (goto-char (point-min))
10302       (while (search-forward "\r" nil t)
10303         (replace-match "" t t)))))
10304
10305 (defun gnus-article-display-x-face (&optional force)
10306   "Look for an X-Face header and display it if present."
10307   (interactive (list 'force))
10308   (save-excursion
10309     (set-buffer gnus-article-buffer)
10310     (let ((inhibit-point-motion-hooks t)
10311           (case-fold-search nil))
10312       (save-restriction
10313         (goto-char (point-min))
10314         (search-forward "\n\n")
10315         (narrow-to-region (point-min) (point))
10316         (goto-char (point-min))
10317         (if (or (not gnus-article-x-face-command)
10318                 (and (not force)
10319                      (or (not gnus-article-x-face-too-ugly)
10320                          (string-match gnus-article-x-face-too-ugly
10321                                        (mail-fetch-field "from"))))
10322                 (progn
10323                   (goto-char (point-min))
10324                   (not (re-search-forward "^X-Face: " nil t))))
10325             nil
10326           (let ((beg (point))
10327                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10328             (if (symbolp gnus-article-x-face-command)
10329                 (and (or (fboundp gnus-article-x-face-command)
10330                          (error "%s is not a function"
10331                                 gnus-article-x-face-command))
10332                      (funcall gnus-article-x-face-command beg end))
10333               (call-process-region beg end "sh" nil 0 nil
10334                                    "-c" gnus-article-x-face-command))))))))
10335
10336 (defun gnus-article-de-quoted-unreadable (&optional force)
10337   "Do a naïve translation of a quoted-printable-encoded article.
10338 This is in no way, shape or form meant as a replacement for real MIME
10339 processing, but is simply a stop-gap measure until MIME support is
10340 written.
10341 If FORCE, decode the article whether it is marked as quoted-printable
10342 or not." 
10343   (interactive (list 'force))
10344   (save-excursion
10345     (set-buffer gnus-article-buffer)
10346     (let ((case-fold-search t)
10347           (buffer-read-only nil)
10348           (type (gnus-fetch-field "content-transfer-encoding")))
10349       (if (or force (and type (string-match "quoted-printable" type)))
10350           (progn
10351             (goto-char (point-min))
10352             (search-forward "\n\n" nil 'move)
10353             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10354
10355 (defun gnus-mime-decode-quoted-printable (from to)
10356   ;; Decode quoted-printable from region between FROM and TO.
10357   (save-excursion
10358     (goto-char from)
10359     (while (search-forward "=" to t)
10360       (cond ((eq (following-char) ?\n)
10361              (delete-char -1)
10362              (delete-char 1))
10363             ((looking-at "[0-9A-F][0-9A-F]")
10364              (delete-char -1)
10365              (insert (hexl-hex-string-to-integer
10366                       (buffer-substring (point) (+ 2 (point)))))
10367              (delete-char 2))
10368             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10369
10370 (defvar gnus-article-time-units
10371   (list (cons 'year (* 365.25 24 60 60))
10372         (cons 'week (* 7 24 60 60))
10373         (cons 'day (* 24 60 60))
10374         (cons 'hour (* 60 60))
10375         (cons 'minute 60)
10376         (cons 'second 1)))
10377
10378 (defun gnus-article-date-ut (&optional type)
10379   "Convert DATE date to universal time in the current article.
10380 If TYPE is `local', convert to local time; if it is `lapsed', output
10381 how much time has lapsed since DATE."
10382   (interactive (list 'ut))
10383   (let ((date (header-date (or gnus-current-headers 
10384                                (gnus-get-header-by-number
10385                                 (gnus-summary-article-number))"")))
10386         (date-regexp "^Date: \\|^X-Sent: "))
10387     (if (or (not date)
10388             (string= date ""))
10389         ()
10390       (save-excursion
10391         (set-buffer gnus-article-buffer)
10392         (let ((buffer-read-only nil))
10393           (goto-char (point-min))
10394           (if (and (re-search-forward date-regexp nil t)
10395                    (progn 
10396                      (beginning-of-line)
10397                      (looking-at date-regexp)))
10398               (delete-region (gnus-point-at-bol)
10399                              (progn (end-of-line) (1+ (point))))
10400             (goto-char (point-min))
10401             (goto-char (- (search-forward "\n\n") 2)))
10402           (insert
10403            (cond 
10404             ((eq type 'local)
10405              (concat "Date: " (condition-case ()
10406                                   (timezone-make-date-arpa-standard date)
10407                                 (error date))
10408                      "\n"))
10409             ((eq type 'ut)
10410              (concat "Date: "
10411                      (condition-case ()
10412                          (timezone-make-date-arpa-standard date nil "UT")
10413                        (error date))
10414                      "\n"))
10415             ((eq type 'lapsed)
10416              ;; If the date is seriously mangled, the timezone
10417              ;; functions are liable to bug out, so we condition-case
10418              ;; the entire thing.  
10419              (let* ((sec (condition-case ()
10420                              (max (- (gnus-seconds-since-epoch 
10421                                       (timezone-make-date-arpa-standard
10422                                        (current-time-string) 
10423                                        (current-time-zone) "UT"))
10424                                      (gnus-seconds-since-epoch 
10425                                       (timezone-make-date-arpa-standard 
10426                                        date nil "UT")))
10427                                   0)
10428                            (error 0)))
10429                     num prev)
10430                (concat
10431                 "X-Sent: "
10432                 (mapconcat 
10433                  (lambda (unit)
10434                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10435                        ""
10436                      (setq sec (- sec (* num (cdr unit))))
10437                      (prog1
10438                          (concat (if prev ", " "") (int-to-string (floor num))
10439                                  " " (symbol-name (car unit))
10440                                  (if (> num 1) "s" ""))
10441                        (setq prev t))))
10442                  gnus-article-time-units "")
10443                 " ago\n")))
10444             (t
10445              (error "Unknown conversion type: %s" type)))))))))
10446
10447 (defun gnus-article-date-local ()
10448   "Convert the current article date to the local timezone."
10449   (interactive)
10450   (gnus-article-date-ut 'local))
10451
10452 (defun gnus-article-date-lapsed ()
10453   "Convert the current article date to time lapsed since it was sent."
10454   (interactive)
10455   (gnus-article-date-ut 'lapsed))
10456
10457 (defun gnus-article-maybe-highlight ()
10458   (if gnus-visual (gnus-article-highlight)))
10459
10460 ;; Article savers.
10461
10462 (defun gnus-output-to-rmail (file-name)
10463   "Append the current article to an Rmail file named FILE-NAME."
10464   (require 'rmail)
10465   ;; Most of these codes are borrowed from rmailout.el.
10466   (setq file-name (expand-file-name file-name))
10467   (setq rmail-default-rmail-file file-name)
10468   (let ((artbuf (current-buffer))
10469         (tmpbuf (get-buffer-create " *Gnus-output*")))
10470     (save-excursion
10471       (or (get-file-buffer file-name)
10472           (file-exists-p file-name)
10473           (if (gnus-yes-or-no-p
10474                (concat "\"" file-name "\" does not exist, create it? "))
10475               (let ((file-buffer (create-file-buffer file-name)))
10476                 (save-excursion
10477                   (set-buffer file-buffer)
10478                   (rmail-insert-rmail-file-header)
10479                   (let ((require-final-newline nil))
10480                     (write-region (point-min) (point-max) file-name t 1)))
10481                 (kill-buffer file-buffer))
10482             (error "Output file does not exist")))
10483       (set-buffer tmpbuf)
10484       (buffer-disable-undo (current-buffer))
10485       (erase-buffer)
10486       (insert-buffer-substring artbuf)
10487       (gnus-convert-article-to-rmail)
10488       ;; Decide whether to append to a file or to an Emacs buffer.
10489       (let ((outbuf (get-file-buffer file-name)))
10490         (if (not outbuf)
10491             (append-to-file (point-min) (point-max) file-name)
10492           ;; File has been visited, in buffer OUTBUF.
10493           (set-buffer outbuf)
10494           (let ((buffer-read-only nil)
10495                 (msg (and (boundp 'rmail-current-message)
10496                           (symbol-value 'rmail-current-message))))
10497             ;; If MSG is non-nil, buffer is in RMAIL mode.
10498             (if msg
10499                 (progn (widen)
10500                        (narrow-to-region (point-max) (point-max))))
10501             (insert-buffer-substring tmpbuf)
10502             (if msg
10503                 (progn
10504                   (goto-char (point-min))
10505                   (widen)
10506                   (search-backward "\^_")
10507                   (narrow-to-region (point) (point-max))
10508                   (goto-char (1+ (point-min)))
10509                   (rmail-count-new-messages t)
10510                   (rmail-show-message msg)))))))
10511     (kill-buffer tmpbuf)))
10512
10513 (defun gnus-output-to-file (file-name)
10514   "Append the current article to a file named FILE-NAME."
10515   (setq file-name (expand-file-name file-name))
10516   (let ((artbuf (current-buffer))
10517         (tmpbuf (get-buffer-create " *Gnus-output*")))
10518     (save-excursion
10519       (set-buffer tmpbuf)
10520       (buffer-disable-undo (current-buffer))
10521       (erase-buffer)
10522       (insert-buffer-substring artbuf)
10523       ;; Append newline at end of the buffer as separator, and then
10524       ;; save it to file.
10525       (goto-char (point-max))
10526       (insert "\n")
10527       (append-to-file (point-min) (point-max) file-name))
10528     (kill-buffer tmpbuf)))
10529
10530 (defun gnus-convert-article-to-rmail ()
10531   "Convert article in current buffer to Rmail message format."
10532   (let ((buffer-read-only nil))
10533     ;; Convert article directly into Babyl format.
10534     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10535     (goto-char (point-min))
10536     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10537     (while (search-forward "\n\^_" nil t) ;single char
10538       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10539     (goto-char (point-max))
10540     (insert "\^_")))
10541
10542 (defun gnus-narrow-to-page (&optional arg)
10543   "Make text outside current page invisible except for page delimiter.
10544 A numeric arg specifies to move forward or backward by that many pages,
10545 thus showing a page other than the one point was originally in."
10546   (interactive "P")
10547   (setq arg (if arg (prefix-numeric-value arg) 0))
10548   (save-excursion
10549     (forward-page -1)                   ;Beginning of current page.
10550     (widen)
10551     (if (> arg 0)
10552         (forward-page arg)
10553       (if (< arg 0)
10554           (forward-page (1- arg))))
10555     ;; Find the end of the page.
10556     (forward-page)
10557     ;; If we stopped due to end of buffer, stay there.
10558     ;; If we stopped after a page delimiter, put end of restriction
10559     ;; at the beginning of that line.
10560     ;; These are commented out.
10561     ;;    (if (save-excursion (beginning-of-line)
10562     ;;                  (looking-at page-delimiter))
10563     ;;  (beginning-of-line))
10564     (narrow-to-region (point)
10565                       (progn
10566                         ;; Find the top of the page.
10567                         (forward-page -1)
10568                         ;; If we found beginning of buffer, stay there.
10569                         ;; If extra text follows page delimiter on same line,
10570                         ;; include it.
10571                         ;; Otherwise, show text starting with following line.
10572                         (if (and (eolp) (not (bobp)))
10573                             (forward-line 1))
10574                         (point)))))
10575
10576 (defun gnus-gmt-to-local ()
10577   "Rewrite Date header described in GMT to local in current buffer.
10578 Intended to be used with gnus-article-prepare-hook."
10579   (save-excursion
10580     (save-restriction
10581       (widen)
10582       (goto-char (point-min))
10583       (narrow-to-region (point-min)
10584                         (progn (search-forward "\n\n" nil 'move) (point)))
10585       (goto-char (point-min))
10586       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10587           (let ((buffer-read-only nil)
10588                 (date (buffer-substring-no-properties
10589                        (match-beginning 1) (match-end 1))))
10590             (delete-region (match-beginning 1) (match-end 1))
10591             (insert
10592              (timezone-make-date-arpa-standard 
10593               date nil (current-time-zone))))))))
10594
10595
10596 ;; Article mode commands
10597
10598 (defun gnus-article-next-page (lines)
10599   "Show next page of current article.
10600 If end of article, return non-nil. Otherwise return nil.
10601 Argument LINES specifies lines to be scrolled up."
10602   (interactive "P")
10603   (move-to-window-line -1)
10604   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10605   (if (save-excursion
10606         (end-of-line)
10607         (and (pos-visible-in-window-p)  ;Not continuation line.
10608              (eobp)))
10609       ;; Nothing in this page.
10610       (if (or (not gnus-break-pages)
10611               (save-excursion
10612                 (save-restriction
10613                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10614           t                             ;Nothing more.
10615         (gnus-narrow-to-page 1)         ;Go to next page.
10616         nil)
10617     ;; More in this page.
10618     (condition-case ()
10619         (scroll-up lines)
10620       (end-of-buffer
10621        ;; Long lines may cause an end-of-buffer error.
10622        (goto-char (point-max))))
10623     nil))
10624
10625 (defun gnus-article-prev-page (lines)
10626   "Show previous page of current article.
10627 Argument LINES specifies lines to be scrolled down."
10628   (interactive "P")
10629   (move-to-window-line 0)
10630   (if (and gnus-break-pages
10631            (bobp)
10632            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10633       (progn
10634         (gnus-narrow-to-page -1) ;Go to previous page.
10635         (goto-char (point-max))
10636         (recenter -1))
10637     (scroll-down lines)))
10638
10639 (defun gnus-article-refer-article ()
10640   "Read article specified by message-id around point."
10641   (interactive)
10642   (search-forward ">" nil t)    ;Move point to end of "<....>".
10643   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10644       (let ((message-id
10645              (buffer-substring (match-beginning 1) (match-end 1))))
10646         (set-buffer gnus-summary-buffer)
10647         (gnus-summary-refer-article message-id))
10648     (error "No references around point")))
10649
10650 (defun gnus-article-show-summary ()
10651   "Reconfigure windows to show summary buffer."
10652   (interactive)
10653   (gnus-configure-windows 'article)
10654   (gnus-summary-goto-subject gnus-current-article))
10655
10656 (defun gnus-article-describe-briefly ()
10657   "Describe article mode commands briefly."
10658   (interactive)
10659   (gnus-message 6
10660    (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")))
10661
10662 (defun gnus-article-summary-command ()
10663   "Execute the last keystroke in the summary buffer."
10664   (interactive)
10665   (let ((obuf (current-buffer))
10666         (owin (current-window-configuration))
10667         func)
10668     (switch-to-buffer gnus-summary-buffer 'norecord)
10669     (setq func (lookup-key (current-local-map) (this-command-keys)))
10670     (call-interactively func)
10671     (set-buffer obuf)
10672     (set-window-configuration owin)
10673     (set-window-start (get-buffer-window (current-buffer)) (point))))
10674
10675 (defun gnus-article-summary-command-nosave ()
10676   "Execute the last keystroke in the summary buffer."
10677   (interactive)
10678   (let (func)
10679     (switch-to-buffer gnus-summary-buffer 'norecord)
10680     (setq func (lookup-key (current-local-map) (this-command-keys)))
10681     (call-interactively func)))
10682
10683 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10684 ;; Modified by tower@prep Nov 86
10685 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10686
10687 (defun gnus-caesar-region (&optional n)
10688   "Caesar rotation of region by N, default 13, for decrypting netnews.
10689 ROT47 will be performed for Japanese text in any case."
10690   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10691                    (list (prefix-numeric-value current-prefix-arg))
10692                  (list nil)))
10693   (cond ((not (numberp n)) (setq n 13))
10694         (t (setq n (mod n 26))))        ;canonicalize N
10695   (if (not (zerop n))           ; no action needed for a rot of 0
10696       (progn
10697         (if (or (not (boundp 'caesar-translate-table))
10698                 (not caesar-translate-table)
10699                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10700             (let ((i 0) 
10701                   (lower "abcdefghijklmnopqrstuvwxyz")
10702                   upper)
10703               (gnus-message 9 "Building caesar-translate-table...")
10704               (setq caesar-translate-table (make-vector 256 0))
10705               (while (< i 256)
10706                 (aset caesar-translate-table i i)
10707                 (setq i (1+ i)))
10708               (setq lower (concat lower lower)
10709                     upper (upcase lower)
10710                     i 0)
10711               (while (< i 26)
10712                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10713                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10714                 (setq i (1+ i)))
10715               ;; ROT47 for Japanese text.
10716               ;; Thanks to ichikawa@flab.fujitsu.junet.
10717               (setq i 161)
10718               (let ((t1 (logior ?O 128))
10719                     (t2 (logior ?! 128))
10720                     (t3 (logior ?~ 128)))
10721                 (while (< i 256)
10722                   (aset caesar-translate-table i
10723                         (let ((v (aref caesar-translate-table i)))
10724                           (if (<= v t1) (if (< v t2) v (+ v 47))
10725                             (if (<= v t3) (- v 47) v))))
10726                   (setq i (1+ i))))
10727               (gnus-message 9 "Building caesar-translate-table...done")))
10728         (let ((from (region-beginning))
10729               (to (region-end))
10730               (i 0) str len)
10731           (setq str (buffer-substring from to))
10732           (setq len (length str))
10733           (while (< i len)
10734             (aset str i (aref caesar-translate-table (aref str i)))
10735             (setq i (1+ i)))
10736           (goto-char from)
10737           (delete-region from to)
10738           (insert str)))))
10739
10740 \f
10741 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10742
10743 ;;;###autoload
10744 (defalias 'gnus-batch-kill 'gnus-batch-score)
10745 ;;;###autoload
10746 (defun gnus-batch-score ()
10747   "Run batched scoring.
10748 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10749 Newsgroups is a list of strings in Bnews format.  If you want to score
10750 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10751 score the alt hierarchy, you'd say \"!alt.all\"."
10752   (interactive)
10753   (let* ((yes-and-no
10754           (gnus-newsrc-parse-options
10755            (apply (function concat)
10756                   (mapcar (lambda (g) (concat g " "))
10757                           command-line-args-left))))
10758          (gnus-expert-user t)
10759          (nnmail-spool-file nil)
10760          (gnus-use-dribble-file nil)
10761          (yes (car yes-and-no))
10762          (no (cdr yes-and-no))
10763          group newsrc entry
10764          ;; Disable verbose message.
10765          gnus-novice-user gnus-large-newsgroup)
10766     ;; Eat all arguments.
10767     (setq command-line-args-left nil)
10768     ;; Start Gnus.
10769     (gnus)
10770     ;; Apply kills to specified newsgroups in command line arguments.
10771     (setq newsrc (cdr gnus-newsrc-alist))
10772     (while newsrc
10773       (setq group (car (car newsrc)))
10774       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10775       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10776                (and (car entry)
10777                     (or (eq (car entry) t)
10778                         (not (zerop (car entry)))))
10779                (if yes (string-match yes group) t)
10780                (or (null no) (not (string-match no group))))
10781           (progn
10782             (gnus-summary-read-group group nil t)
10783             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10784                  (gnus-summary-exit))))
10785       (setq newsrc (cdr newsrc)))
10786     ;; Exit Emacs.
10787     (switch-to-buffer gnus-group-buffer)
10788     (gnus-group-save-newsrc)))
10789
10790 (defun gnus-apply-kill-file ()
10791   "Apply a kill file to the current newsgroup.
10792 Returns the number of articles marked as read."
10793   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10794           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10795       (gnus-apply-kill-file-internal)
10796     0))
10797
10798 (defun gnus-kill-save-kill-buffer ()
10799   (save-excursion
10800     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10801       (if (get-file-buffer file)
10802           (progn
10803             (set-buffer (get-file-buffer file))
10804             (and (buffer-modified-p) (save-buffer))
10805             (kill-buffer (current-buffer)))))))
10806
10807 (defvar gnus-kill-file-name "KILL"
10808   "Suffix of the kill files.")
10809
10810 (defun gnus-newsgroup-kill-file (newsgroup)
10811   "Return the name of a kill file name for NEWSGROUP.
10812 If NEWSGROUP is nil, return the global kill file name instead."
10813   (cond ((or (null newsgroup)
10814              (string-equal newsgroup ""))
10815          ;; The global KILL file is placed at top of the directory.
10816          (expand-file-name gnus-kill-file-name
10817                            (or gnus-kill-files-directory "~/News")))
10818         ((gnus-use-long-file-name 'not-kill)
10819          ;; Append ".KILL" to newsgroup name.
10820          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10821                            (or gnus-kill-files-directory "~/News")))
10822         (t
10823          ;; Place "KILL" under the hierarchical directory.
10824          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10825                                    "/" gnus-kill-file-name)
10826                            (or gnus-kill-files-directory "~/News")))))
10827
10828 \f
10829 ;;;
10830 ;;; Dribble file
10831 ;;;
10832
10833 (defvar gnus-dribble-ignore nil)
10834
10835 (defun gnus-dribble-file-name ()
10836   (concat gnus-startup-file "-dribble"))
10837
10838 (defun gnus-dribble-open ()
10839   (save-excursion 
10840     (set-buffer 
10841      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10842     (buffer-disable-undo (current-buffer))
10843     (bury-buffer gnus-dribble-buffer)
10844     (auto-save-mode t)
10845     (goto-char (point-max))))
10846
10847 (defun gnus-dribble-enter (string)
10848   (if (and (not gnus-dribble-ignore)
10849            gnus-dribble-buffer
10850            (buffer-name gnus-dribble-buffer))
10851       (let ((obuf (current-buffer)))
10852         (set-buffer gnus-dribble-buffer)
10853         (insert string "\n")
10854         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10855         (set-buffer obuf))))
10856
10857 (defun gnus-dribble-read-file ()
10858   (let ((dribble-file (gnus-dribble-file-name)))
10859     (save-excursion 
10860       (set-buffer (setq gnus-dribble-buffer 
10861                         (get-buffer-create 
10862                          (file-name-nondirectory dribble-file))))
10863       (gnus-add-current-to-buffer-list)
10864       (erase-buffer)
10865       (set-visited-file-name dribble-file)
10866       (buffer-disable-undo (current-buffer))
10867       (bury-buffer (current-buffer))
10868       (set-buffer-modified-p nil)
10869       (let ((auto (make-auto-save-file-name))
10870             (gnus-dribble-ignore t))
10871         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10872             (progn
10873               (if (file-newer-than-file-p auto dribble-file)
10874                   (setq dribble-file auto))
10875               (insert-file-contents dribble-file)
10876               (if (not (zerop (buffer-size)))
10877                   (set-buffer-modified-p t))
10878               (if (gnus-y-or-n-p 
10879                    "Auto-save file exists. Do you want to read it? ")
10880                   (progn
10881                     (gnus-message 5 "Reading %s..." dribble-file) 
10882                     (eval-buffer (current-buffer))
10883                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10884
10885 (defun gnus-dribble-delete-file ()
10886   (if (file-exists-p (gnus-dribble-file-name))
10887       (delete-file (gnus-dribble-file-name)))
10888   (if gnus-dribble-buffer
10889       (save-excursion
10890         (set-buffer gnus-dribble-buffer)
10891         (let ((auto (make-auto-save-file-name)))
10892           (if (file-exists-p auto)
10893               (delete-file auto))
10894           (erase-buffer)
10895           (set-buffer-modified-p nil)))))
10896
10897 (defun gnus-dribble-save ()
10898   (if (and gnus-dribble-buffer
10899            (buffer-name gnus-dribble-buffer))
10900       (save-excursion
10901         (set-buffer gnus-dribble-buffer)
10902         (save-buffer))))
10903
10904 (defun gnus-dribble-clear ()
10905   (save-excursion
10906     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10907         (progn
10908           (set-buffer gnus-dribble-buffer)
10909           (erase-buffer)
10910           (set-buffer-modified-p nil)
10911           (setq buffer-saved-size (buffer-size))))))
10912
10913 ;;;
10914 ;;; Server Communication
10915 ;;;
10916
10917 ;; All the Gnus backends have the same interface, and should return
10918 ;; data in a similar format. Below is an overview of what functions
10919 ;; these packages must supply and what results they should return.
10920 ;;
10921 ;; Variables:
10922 ;;
10923 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10924 ;; buffer. 
10925 ;;
10926 ;; Functions for the imaginary backend `choke':
10927 ;;
10928 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10929 ;; Should return all headers for all ARTICLES, or return NOV lines for
10930 ;; the same.
10931 ;;
10932 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10933 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10934 ;; must be returned.
10935 ;;
10936 ;; `choke-close-group GROUP &optional SERVER'
10937 ;; Close group. Most backends won't have to do anything with this
10938 ;; call, but it is an opportunity to clean up, if that is needed. It
10939 ;; is called when Gnus exits a group.
10940 ;;
10941 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10942 ;; Return ARTICLE, which is either an article number or
10943 ;; message-id. Note that not all backends can return articles based on
10944 ;; message-id. 
10945 ;;
10946 ;; `choke-request-list SERVER'
10947 ;; Return a list of all newsgroups on SERVER.
10948 ;;
10949 ;; `choke-request-list-newsgroups SERVER'
10950 ;; Return a list of descriptions of all newsgroups on SERVER.
10951 ;;
10952 ;; `choke-request-newgroups DATE &optional SERVER'
10953 ;; Return a list of all groups that have arrived after DATE on
10954 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10955 ;; always check whether the groups are old or not. Backends that do
10956 ;; not store date information may just return the entire list of
10957 ;; groups, although this might not be a good idea in general.
10958 ;;
10959 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10960 ;; Should return a buffer that is suitable for "posting". nnspool and
10961 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10962 ;; buffer. This function should fill out the appropriate headers. 
10963 ;;
10964 ;; `choke-request-post &optional SERVER'
10965 ;; Function that will be called from a buffer to be posted. 
10966 ;;
10967 ;; `choke-open-server SERVER &optional ARGUMENT'
10968 ;; Open a connection to SERVER.
10969 ;;
10970 ;; `choke-close-server &optional SERVER'
10971 ;; Close the connection to SERVER.
10972 ;;
10973 ;; `choke-server-opened &optional SERVER'
10974 ;; Whether the conenction to SERVER is opened or not.
10975 ;;
10976 ;; `choke-server-status &optional SERVER'
10977 ;; Should return a status string (not in the nntp buffer, but as the
10978 ;; result of the function).
10979 ;;
10980 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10981 ;; Optional function for retrieving active file info on all groups in
10982 ;; GROUPS.  Two return formats are supported: The normal active file
10983 ;; format, and a list of GROUP lines.  This function should return (as
10984 ;; a function value) either `active' or `group', depending on what
10985 ;; format it returns.
10986 ;;
10987 ;; The following functions are optional and apply only to backends
10988 ;; that are able to control the contents of their groups totally
10989 ;; (ie. mail backends.)  Backends that aren't able to do that
10990 ;; shouldn't define these functions at all. Gnus will check for their
10991 ;; presence before attempting to call them.
10992 ;;
10993 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10994 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10995 ;; backends will not be able to expire articles. Should return a list
10996 ;; of all articles that were not expired.
10997 ;;
10998 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10999 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11000 ;; Removes any information it has added to the article (extra headers,
11001 ;; whatever - make it as clean as possible), and then passes the
11002 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11003 ;; function described below. If the ACCEPT-FORM returns a non-nil
11004 ;; value, the article should then be deleted. If LAST is nil, that
11005 ;; means that there will be further calls to this function. This might
11006 ;; be taken as an advice not to save buffers/internal variables just
11007 ;; yet, but wait until the last call to speed things up.
11008 ;;
11009 ;; `choke-request-accept-article GROUP &optional LAST' 
11010 ;; The contents of the current buffer will be put into GROUP.  There
11011 ;; should, of course, be an article in the current buffer.  This
11012 ;; function is normally only called by the function described above,
11013 ;; and LAST works the same way as in that function.
11014 ;;
11015 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11016 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11017 ;; This provides an easy interface for allowing editing of
11018 ;; articles. Note that even headers may be edited, so the backend has
11019 ;; to update any tables (nov buffers, etc) that it maintains after
11020 ;; replacing the article.
11021 ;;
11022 ;; `choke-request-create-group GROUP &optional SERVER'
11023 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11024 ;; might be a group that already exists, but hasn't been registered
11025 ;; yet. 
11026 ;;
11027 ;; All these functions must return nil if they couldn't service the
11028 ;; request. If the optional arguments are not supplied, some "current"
11029 ;; or "default" values should be used. In short, one should emulate an
11030 ;; NNTP server, in a way.
11031 ;;
11032 ;; If you want to write a new backend, you just have to supply the
11033 ;; functions listed above. In addition, you must enter the new backend
11034 ;; into the list of valid select methods:
11035 ;; (setq gnus-valid-select-methods 
11036 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11037 ;; The first element in this list is the name of the backend. Other
11038 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11039 ;; groups), `none' (neither), `respool' (for groups that can control
11040 ;; their contents). 
11041
11042 (defun gnus-start-news-server (&optional confirm)
11043   "Open a method for getting news.
11044 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11045   (let (how)
11046     (if gnus-current-select-method
11047         ;; Stream is already opened.
11048         nil
11049       ;; Open NNTP server.
11050       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11051       (if confirm
11052           (progn
11053             ;; Read server name with completion.
11054             (setq gnus-nntp-server
11055                   (completing-read "NNTP server: "
11056                                    (mapcar (lambda (server) (list server))
11057                                            (cons (list gnus-nntp-server)
11058                                                  gnus-secondary-servers))
11059                                    nil nil gnus-nntp-server))))
11060
11061       (if (and gnus-nntp-server 
11062                (stringp gnus-nntp-server)
11063                (not (string= gnus-nntp-server "")))
11064           (setq gnus-select-method
11065                 (cond ((or (string= gnus-nntp-server "")
11066                            (string= gnus-nntp-server "::"))
11067                        (list 'nnspool (system-name)))
11068                       ((string-match "^:" gnus-nntp-server)
11069                        (list 'nnmh gnus-nntp-server 
11070                              (list 'nnmh-directory 
11071                                    (file-name-as-directory
11072                                     (expand-file-name
11073                                      (concat "~/" (substring
11074                                                    gnus-nntp-server 1)))))
11075                              (list 'nnmh-get-new-mail nil)))
11076                       (t
11077                        (list 'nntp gnus-nntp-server)))))
11078
11079       (setq how (car gnus-select-method))
11080       (cond ((eq how 'nnspool)
11081              (require 'nnspool)
11082              (gnus-message 5 "Looking up local news spool..."))
11083             ((eq how 'nnmh)
11084              (require 'nnmh)
11085              (gnus-message 5 "Looking up mh spool..."))
11086             (t
11087              (require 'nntp)))
11088       (setq gnus-current-select-method gnus-select-method)
11089       (run-hooks 'gnus-open-server-hook)
11090       (or 
11091        ;; gnus-open-server-hook might have opened it
11092        (gnus-server-opened gnus-select-method)  
11093        (gnus-open-server gnus-select-method)
11094        (gnus-y-or-n-p
11095         (format
11096          "%s server on %s can't be opened. Continue? "
11097          (car gnus-select-method) (nth 1 gnus-select-method)))
11098        (progn
11099          (gnus-message 1 "Couldn't open server on %s" 
11100                        (nth 1 gnus-select-method))
11101          (ding)
11102          nil)))))
11103
11104 (defun gnus-check-news-server (&optional method)
11105   "If the news server is down, start it up again."
11106   (let ((method (if method method gnus-select-method)))
11107     (and (stringp method)
11108          (setq method (gnus-server-to-method method)))
11109     (if (gnus-server-opened method)
11110         ;; Stream is already opened.
11111         t
11112       ;; Open server.
11113       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11114       (run-hooks 'gnus-open-server-hook)
11115       (or (gnus-server-opened method)
11116           (gnus-open-server method))
11117       (message ""))))
11118
11119 (defun gnus-nntp-message (&optional message)
11120   "Check the status of the NNTP server.
11121 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11122 is returned insted of the status string."
11123   (let ((status (gnus-status-message (gnus-find-method-for-group 
11124                                       gnus-newsgroup-name)))
11125         (message (or message "")))
11126     (if (and (stringp status) (> (length status) 0))
11127         status message)))
11128
11129 (defun gnus-get-function (method function)
11130   (and (stringp method)
11131        (setq method (gnus-server-to-method method)))
11132   (let ((func (intern (format "%s-%s" (car method) function))))
11133     (if (not (fboundp func)) 
11134         (progn
11135           (require (car method))
11136           (if (not (fboundp func)) 
11137               (error "No such function: %s" func))))
11138     func))
11139
11140 ;;; Interface functions to the backends.
11141
11142 (defun gnus-open-server (method)
11143   (funcall (gnus-get-function method 'open-server)
11144            (nth 1 method) (nthcdr 2 method)))
11145
11146 (defun gnus-close-server (method)
11147   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11148
11149 (defun gnus-request-list (method)
11150   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11151
11152 (defun gnus-request-list-newsgroups (method)
11153   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11154
11155 (defun gnus-request-newgroups (date method)
11156   (funcall (gnus-get-function method 'request-newgroups) 
11157            date (nth 1 method)))
11158
11159 (defun gnus-server-opened (method)
11160   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11161
11162 (defun gnus-status-message (method)
11163   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11164                   method)))
11165     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11166
11167 (defun gnus-request-group (group &optional dont-check)
11168   (let ((method (gnus-find-method-for-group group)))
11169 ;    (and t (message "%s GROUP %s" (car method) group))
11170     (funcall (gnus-get-function method 'request-group) 
11171              (gnus-group-real-name group) (nth 1 method) dont-check)))
11172
11173 (defun gnus-request-asynchronous (group &optional articles)
11174   (let ((method (gnus-find-method-for-group group)))
11175     (funcall (gnus-get-function method 'request-asynchronous) 
11176              (gnus-group-real-name group) (nth 1 method) articles)))
11177
11178 (defun gnus-list-active-group (group)
11179   (let ((method (gnus-find-method-for-group group))
11180         (func 'list-active-group))
11181     (and (gnus-check-backend-function func group)
11182          (funcall (gnus-get-function method func) 
11183                   (gnus-group-real-name group) (nth 1 method)))))
11184
11185 (defun gnus-request-group-description (group)
11186   (let ((method (gnus-find-method-for-group group))
11187         (func 'request-group-description))
11188     (and (gnus-check-backend-function func group)
11189          (funcall (gnus-get-function method func) 
11190                   (gnus-group-real-name group) (nth 1 method)))))
11191
11192 (defun gnus-close-group (group)
11193   (let ((method (gnus-find-method-for-group group)))
11194     (funcall (gnus-get-function method 'close-group) 
11195              (gnus-group-real-name group) (nth 1 method))))
11196
11197 (defun gnus-retrieve-headers (articles group)
11198   (let ((method (gnus-find-method-for-group group)))
11199     (if (and gnus-use-cache (numberp (car articles)))
11200         (gnus-cache-retrieve-headers articles group)
11201       (funcall (gnus-get-function method 'retrieve-headers) 
11202                articles (gnus-group-real-name group) (nth 1 method)))))
11203
11204 (defun gnus-retrieve-groups (groups method)
11205   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11206
11207 (defun gnus-request-article (article group &optional buffer)
11208   (let ((method (gnus-find-method-for-group group)))
11209     (funcall (gnus-get-function method 'request-article) 
11210              article (gnus-group-real-name group) (nth 1 method) buffer)))
11211
11212 (defun gnus-request-head (article group)
11213   (let ((method (gnus-find-method-for-group group)))
11214     (funcall (gnus-get-function method 'request-head) 
11215              article (gnus-group-real-name group) (nth 1 method))))
11216
11217 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11218 (defun gnus-request-post-buffer (post group subject header artbuf
11219                                       info follow-to respect-poster)
11220    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11221                                             group gnus-newsrc-hashtb)))))
11222           (method
11223            (if (and gnus-post-method
11224                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11225                     (memq 'post (assoc
11226                                  (format "%s" (car (gnus-find-method-for-group
11227                                                     gnus-newsgroup-name)))
11228                                         gnus-valid-select-methods)))
11229                gnus-post-method
11230              (gnus-find-method-for-group gnus-newsgroup-name))))
11231      (or (gnus-server-opened method)
11232          (gnus-open-server method)
11233          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11234      (let ((mail-self-blind nil)
11235            (mail-archive-file-name nil))
11236        (funcall (gnus-get-function method 'request-post-buffer) 
11237                 post group subject header artbuf info follow-to
11238                 respect-poster))))
11239
11240 (defun gnus-request-post (method &optional force)
11241   (and (stringp method)
11242        (setq method (gnus-server-to-method method)))
11243   (and (not force) gnus-post-method
11244        (memq 'post (assoc (format "%s" (car method))
11245                           gnus-valid-select-methods))
11246        (setq method gnus-post-method))
11247   (funcall (gnus-get-function method 'request-post) 
11248            (nth 1 method)))
11249
11250 (defun gnus-request-expire-articles (articles group &optional force)
11251   (let ((method (gnus-find-method-for-group group)))
11252     (funcall (gnus-get-function method 'request-expire-articles) 
11253              articles (gnus-group-real-name group) (nth 1 method)
11254              force)))
11255
11256 (defun gnus-request-move-article 
11257   (article group server accept-function &optional last)
11258   (let ((method (gnus-find-method-for-group group)))
11259     (funcall (gnus-get-function method 'request-move-article) 
11260              article (gnus-group-real-name group) 
11261              (nth 1 method) accept-function last)))
11262
11263 (defun gnus-request-accept-article (group &optional last)
11264   (let ((func (if (symbolp group) group
11265                 (car (gnus-find-method-for-group group)))))
11266     (funcall (intern (format "%s-request-accept-article" func))
11267              (if (stringp group) (gnus-group-real-name group) group)
11268              last)))
11269
11270 (defun gnus-request-replace-article (article group buffer)
11271   (let ((func (car (gnus-find-method-for-group group))))
11272     (funcall (intern (format "%s-request-replace-article" func))
11273              article (gnus-group-real-name group) buffer)))
11274
11275 (defun gnus-request-create-group (group)
11276   (let ((method (gnus-find-method-for-group group)))
11277     (funcall (gnus-get-function method 'request-create-group) 
11278              (gnus-group-real-name group) (nth 1 method))))
11279
11280 (defun gnus-member-of-valid (symbol group)
11281   (memq symbol (assoc
11282                 (format "%s" (car (gnus-find-method-for-group group)))
11283                 gnus-valid-select-methods)))
11284
11285 (defsubst gnus-secondary-method-p (method)
11286   (member method gnus-secondary-select-methods))
11287
11288 (defun gnus-find-method-for-group (group &optional info)
11289   (or gnus-override-method
11290       (and (not group)
11291            gnus-select-method)
11292       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11293             method)
11294         (if (or (not info)
11295                 (not (setq method (nth 4 info))))
11296             (setq method gnus-select-method)
11297           (setq method
11298                 (cond ((stringp method)
11299                        (gnus-server-to-method method))
11300                       ((stringp (car method))
11301                        (gnus-server-extend-method group method))
11302                       (t
11303                        method))))
11304         (gnus-server-add-address method))))
11305
11306 (defun gnus-check-backend-function (func group)
11307   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11308                  group)))
11309     (fboundp (intern (format "%s-%s" method func)))))
11310
11311 (defun gnus-methods-using (method)
11312   (let ((valids gnus-valid-select-methods)
11313         outs)
11314     (while valids
11315       (if (memq method (car valids)) 
11316           (setq outs (cons (car valids) outs)))
11317       (setq valids (cdr valids)))
11318     outs))
11319
11320 ;;; 
11321 ;;; Active & Newsrc File Handling
11322 ;;;
11323
11324 ;; Newsrc related functions.
11325 ;; Gnus internal format of gnus-newsrc-alist:
11326 ;; (("alt.general" 3 (1 . 1))
11327 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11328 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11329 ;; The first item is the group name; the second is the subscription
11330 ;; level; the third is either a range of a list of ranges of read
11331 ;; articles, the optional fourth element is a list of marked articles,
11332 ;; the optional fifth element is the select method.
11333 ;;
11334 ;; Gnus internal format of gnus-newsrc-hashtb:
11335 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11336 ;; This is the entry for "alt.misc". The first element is the number
11337 ;; of unread articles in "alt.misc". The cdr of this entry is the
11338 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11339 ;; trivial to remove or add new elements into gnus-newsrc-alist
11340 ;; without scanning the entire list. So, to get the actual information
11341 ;; of "alt.misc", you'd say something like 
11342 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11343 ;;
11344 ;; Gnus internal format of gnus-active-hashtb:
11345 ;; ((1 . 1))
11346 ;;  (5 . 10))
11347 ;;  (67 . 99)) ...)
11348 ;; The only element in each entry in this hash table is a range of
11349 ;; (possibly) available articles. (Articles in this range may have
11350 ;; been expired or canceled.)
11351 ;;
11352 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11353 ;; ("alt.misc" "alt.test" "alt.general" ...)
11354
11355 (defun gnus-setup-news (&optional rawfile level)
11356   "Setup news information.
11357 If RAWFILE is non-nil, the .newsrc file will also be read.
11358 If LEVEL is non-nil, the news will be set up at level LEVEL."
11359   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11360     ;; Clear some variables to re-initialize news information.
11361     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11362
11363     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11364     (if init (gnus-read-newsrc-file rawfile))
11365
11366     ;; Read the active file and create `gnus-active-hashtb'.
11367     ;; If `gnus-read-active-file' is nil, then we just create an empty
11368     ;; hash table. The partial filling out of the hash table will be
11369     ;; done in `gnus-get-unread-articles'.
11370     (if (and gnus-read-active-file 
11371              (not level))
11372         (gnus-read-active-file)
11373       (setq gnus-active-hashtb (make-vector 4095 0)))
11374
11375     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11376
11377     ;; Find the number of unread articles in each non-dead group.
11378     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11379     ;; Find new newsgroups and treat them.
11380     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11381              (gnus-server-opened gnus-select-method))
11382         (gnus-find-new-newsgroups))
11383     (if (and init gnus-check-bogus-newsgroups 
11384              gnus-read-active-file (not level)
11385              (gnus-server-opened gnus-select-method))
11386         (gnus-check-bogus-newsgroups))))
11387
11388 (defun gnus-find-new-newsgroups ()
11389   "Search for new newsgroups and add them.
11390 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11391 The `-n' option line from .newsrc is respected."
11392   (interactive)
11393   (or (gnus-check-first-time-used)
11394       (if (or (consp gnus-check-new-newsgroups)
11395               (eq gnus-check-new-newsgroups 'ask-server))
11396           (gnus-ask-server-for-new-groups)
11397         (let ((groups 0)
11398               group new-newsgroups)
11399           (or gnus-have-read-active-file (gnus-read-active-file))
11400           (setq gnus-newsrc-last-checked-date (current-time-string))
11401           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11402           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11403           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11404           (mapatoms
11405            (lambda (sym)
11406              (setq group (symbol-name sym))
11407              (if (or (gnus-gethash group gnus-killed-hashtb)
11408                      (gnus-gethash group gnus-newsrc-hashtb))
11409                  ()
11410                (let ((do-sub (gnus-matches-options-n group)))
11411                  (cond ((eq do-sub 'subscribe)
11412                         (setq groups (1+ groups))
11413                         (gnus-sethash group group gnus-killed-hashtb)
11414                         (funcall 
11415                          gnus-subscribe-options-newsgroup-method group))
11416                        ((eq do-sub 'ignore)
11417                         nil)
11418                        (t
11419                         (setq groups (1+ groups))
11420                         (gnus-sethash group group gnus-killed-hashtb)
11421                         (if gnus-subscribe-hierarchical-interactive
11422                             (setq new-newsgroups (cons group new-newsgroups))
11423                           (funcall gnus-subscribe-newsgroup-method group)))))))
11424            gnus-active-hashtb)
11425           (if new-newsgroups 
11426               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11427           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11428           (if (> groups 0)
11429               (gnus-message 6 "%d new newsgroup%s arrived." 
11430                             groups (if (> groups 1) "s have" " has")))))))
11431
11432 (defun gnus-matches-options-n (group)
11433   ;; Returns `subscribe' if the group is to be uncoditionally
11434   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11435   ;; no match for the group.
11436
11437   ;; First we check the two user variables.
11438   (cond
11439    ((and gnus-options-subscribe
11440          (string-match gnus-options-subscribe group))
11441     'subscribe)
11442    ((and gnus-options-not-subscribe
11443          (string-match gnus-options-not-subscribe group))
11444     'ignore)
11445    ;; Then we go through the list that was retrieved from the .newsrc
11446    ;; file.  This list has elements on the form 
11447    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11448    ;; is in the reverse order of the options line) is returned.
11449    (t
11450     (let ((regs gnus-newsrc-options-n))
11451       (while (and regs
11452                   (not (string-match (car (car regs)) group)))
11453         (setq regs (cdr regs)))
11454       (and regs (cdr (car regs)))))))
11455
11456 (defun gnus-ask-server-for-new-groups ()
11457   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11458          (methods (cons gnus-select-method 
11459                         (append
11460                          (and (consp gnus-check-new-newsgroups)
11461                               gnus-check-new-newsgroups)
11462                          gnus-secondary-select-methods)))
11463          (groups 0)
11464          (new-date (current-time-string))
11465          hashtb group new-newsgroups got-new)
11466     ;; Go thorugh both primary and secondary select methods and
11467     ;; request new newsgroups.  
11468     (while methods
11469       (and (or (gnus-server-opened (car methods))
11470                (gnus-open-server (car methods)))
11471            (gnus-request-newgroups date (car methods))
11472            (save-excursion
11473              (setq got-new t)
11474              (set-buffer nntp-server-buffer)
11475              (or hashtb (setq hashtb (gnus-make-hashtable 
11476                                       (count-lines (point-min) (point-max)))))
11477              ;; Enter all the new groups in a hashtable.
11478              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11479       (setq methods (cdr methods)))
11480     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11481     ;; Now all new groups from all select methods are in `hashtb'.
11482     (mapatoms
11483      (lambda (group-sym)
11484        (setq group (symbol-name group-sym))
11485        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11486                (member group gnus-zombie-list)
11487                (member group gnus-killed-list))
11488            ;; The group is already known.
11489            ()
11490          (and (symbol-value group-sym)
11491               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11492          (let ((do-sub (gnus-matches-options-n group)))
11493            (cond ((eq do-sub 'subscribe)
11494                   (setq groups (1+ groups))
11495                   (gnus-sethash group group gnus-killed-hashtb)
11496                   (funcall 
11497                    gnus-subscribe-options-newsgroup-method group))
11498                  ((eq do-sub 'ignore)
11499                   nil)
11500                  (t
11501                   (setq groups (1+ groups))
11502                   (gnus-sethash group group gnus-killed-hashtb)
11503                   (if gnus-subscribe-hierarchical-interactive
11504                       (setq new-newsgroups (cons group new-newsgroups))
11505                     (funcall gnus-subscribe-newsgroup-method group)))))))
11506      hashtb)
11507     (if new-newsgroups 
11508         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11509     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11510     (if (> groups 0)
11511         (gnus-message 6 "%d new newsgroup%s arrived." 
11512                       groups (if (> groups 1) "s have" " has")))
11513     got-new))
11514
11515 (defun gnus-check-first-time-used ()
11516   (if (or (> (length gnus-newsrc-alist) 1)
11517           (file-exists-p gnus-startup-file)
11518           (file-exists-p (concat gnus-startup-file ".el"))
11519           (file-exists-p (concat gnus-startup-file ".eld")))
11520       nil
11521     (gnus-message 6 "First time user; subscribing you to default groups")
11522     (or gnus-have-read-active-file (gnus-read-active-file))
11523     (setq gnus-newsrc-last-checked-date (current-time-string))
11524     (let ((groups gnus-default-subscribed-newsgroups)
11525           group)
11526       (if (eq groups t)
11527           nil
11528         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11529         (mapatoms
11530          (lambda (sym)
11531            (setq group (symbol-name sym))
11532            (let ((do-sub (gnus-matches-options-n group)))
11533              (cond ((eq do-sub 'subscribe)
11534                     (gnus-sethash group group gnus-killed-hashtb)
11535                     (funcall 
11536                      gnus-subscribe-options-newsgroup-method group))
11537                    ((eq do-sub 'ignore)
11538                     nil)
11539                    (t
11540                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11541          gnus-active-hashtb)
11542         (while groups
11543           (if (gnus-gethash (car groups) gnus-active-hashtb)
11544               (gnus-group-change-level 
11545                (car groups) gnus-level-default-subscribed gnus-level-killed))
11546           (setq groups (cdr groups)))
11547         (gnus-group-make-help-group)
11548         (and gnus-novice-user
11549              (gnus-message 7 "`A k' to list killed groups"))))))
11550
11551 (defun gnus-subscribe-group (group previous &optional method)
11552   (gnus-group-change-level 
11553    (if method
11554        (list t group gnus-level-default-subscribed nil nil method)
11555      group) 
11556    gnus-level-default-subscribed gnus-level-killed previous t))
11557
11558 ;; `gnus-group-change-level' is the fundamental function for changing
11559 ;; subscription levels of newsgroups. This might mean just changing
11560 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11561 ;; again, which subscribes/unsubscribes a group, which is equally
11562 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11563 ;; from 8-9 to 1-7 means that you remove the group from the list of
11564 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11565 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11566 ;; which is trivial.
11567 ;; ENTRY can either be a string (newsgroup name) or a list (if
11568 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11569 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11570 ;; entries. 
11571 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11572 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11573 ;; after. 
11574 (defun gnus-group-change-level (entry level &optional oldlevel
11575                                       previous fromkilled)
11576   (let (group info active num)
11577     ;; Glean what info we can from the arguments
11578     (if (consp entry)
11579         (if fromkilled (setq group (nth 1 entry))
11580           (setq group (car (nth 2 entry))))
11581       (setq group entry))
11582     (if (and (stringp entry)
11583              oldlevel 
11584              (< oldlevel gnus-level-zombie))
11585         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11586     (if (and (not oldlevel)
11587              (consp entry))
11588         (setq oldlevel (car (cdr (nth 2 entry)))))
11589     (if (stringp previous)
11590         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11591
11592     (gnus-dribble-enter
11593      (format "(gnus-group-change-level %S %S %S %S %S)" 
11594              group level oldlevel (car (nth 2 previous)) fromkilled))
11595     
11596     ;; Then we remove the newgroup from any old structures, if needed.
11597     ;; If the group was killed, we remove it from the killed or zombie
11598     ;; list. If not, and it is in fact going to be killed, we remove
11599     ;; it from the newsrc hash table and assoc.
11600     (cond ((>= oldlevel gnus-level-zombie)
11601            (if (= oldlevel gnus-level-zombie)
11602                (setq gnus-zombie-list (delete group gnus-zombie-list))
11603              (setq gnus-killed-list (delete group gnus-killed-list))))
11604           (t
11605            (if (>= level gnus-level-zombie)
11606                (progn
11607                  (gnus-sethash (car (nth 2 entry))
11608                                nil gnus-newsrc-hashtb)
11609                  (if (nth 3 entry)
11610                      (setcdr (gnus-gethash (car (nth 3 entry))
11611                                            gnus-newsrc-hashtb)
11612                              (cdr entry)))
11613                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11614
11615     ;; Finally we enter (if needed) the list where it is supposed to
11616     ;; go, and change the subscription level. If it is to be killed,
11617     ;; we enter it into the killed or zombie list.
11618     (cond ((>= level gnus-level-zombie)
11619            ;; Remove from the hash table.
11620            (gnus-sethash group nil gnus-newsrc-hashtb)
11621            (or (gnus-group-foreign-p group)
11622                ;; We do not enter foreign groups into the list of dead
11623                ;; groups.  
11624                (if (= level gnus-level-zombie)
11625                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11626                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11627           (t
11628            ;; If the list is to be entered into the newsrc assoc, and
11629            ;; it was killed, we have to create an entry in the newsrc
11630            ;; hashtb format and fix the pointers in the newsrc assoc.
11631            (if (>= oldlevel gnus-level-zombie)
11632                (progn
11633                  (if (listp entry)
11634                      (progn
11635                        (setq info (cdr entry))
11636                        (setq num (car entry)))
11637                    (setq active (gnus-gethash group gnus-active-hashtb))
11638                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11639                    ;; Check whether the group is foreign. If so, the
11640                    ;; foreign select method has to be entered into the
11641                    ;; info. 
11642                    (let ((method (gnus-group-method-name group)))
11643                      (if (eq method gnus-select-method)
11644                          (setq info (list group level nil))
11645                        (setq info (list group level nil nil method)))))
11646                  (or previous 
11647                      (setq previous 
11648                            (let ((p gnus-newsrc-alist))
11649                              (while (cdr (cdr p))
11650                                (setq p (cdr p)))
11651                              p)))
11652                  (setq entry (cons info (cdr (cdr previous))))
11653                  (if (cdr previous)
11654                      (progn
11655                        (setcdr (cdr previous) entry)
11656                        (gnus-sethash group (cons num (cdr previous)) 
11657                                      gnus-newsrc-hashtb))
11658                    (setcdr previous entry)
11659                    (gnus-sethash group (cons num previous)
11660                                  gnus-newsrc-hashtb))
11661                  (if (cdr entry)
11662                      (setcdr (gnus-gethash (car (car (cdr entry)))
11663                                            gnus-newsrc-hashtb)
11664                              entry)))
11665              ;; It was alive, and it is going to stay alive, so we
11666              ;; just change the level and don't change any pointers or
11667              ;; hash table entries.
11668              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11669
11670 (defun gnus-kill-newsgroup (newsgroup)
11671   "Obsolete function. Kills a newsgroup."
11672   (gnus-group-change-level
11673    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11674
11675 (defun gnus-check-bogus-newsgroups (&optional confirm)
11676   "Remove bogus newsgroups.
11677 If CONFIRM is non-nil, the user has to confirm the deletion of every
11678 newsgroup." 
11679   (let ((newsrc (cdr gnus-newsrc-alist))
11680         bogus group entry)
11681     (gnus-message 5 "Checking bogus newsgroups...")
11682     (or gnus-have-read-active-file (gnus-read-active-file))
11683     ;; Find all bogus newsgroup that are subscribed.
11684     (while newsrc
11685       (setq group (car (car newsrc)))
11686       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11687               (nth 4 (car newsrc))      ; Foreign
11688               (and confirm
11689                    (not (gnus-y-or-n-p
11690                          (format "Remove bogus newsgroup: %s " group)))))
11691           ;; Don't remove.
11692           ()
11693         ;; Found a bogus newsgroup.
11694         (setq bogus (cons group bogus)))
11695       (setq newsrc (cdr newsrc)))
11696     ;; Remove all bogus subscribed groups by first killing them, and
11697     ;; then removing them from the list of killed groups.
11698     (while bogus
11699       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11700            (progn
11701              (gnus-group-change-level entry gnus-level-killed)
11702              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11703       (setq bogus (cdr bogus)))
11704     ;; Then we remove all bogus groups from the list of killed and
11705     ;; zombie groups. They are are removed without confirmation.
11706     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11707           killed)
11708       (while dead-lists
11709         (setq killed (symbol-value (car dead-lists)))
11710         (while killed
11711           (setq group (car killed))
11712           (or (gnus-gethash group gnus-active-hashtb)
11713               ;; The group is bogus.
11714               (set (car dead-lists)
11715                    (delete group (symbol-value (car dead-lists)))))
11716           (setq killed (cdr killed)))
11717         (setq dead-lists (cdr dead-lists))))
11718     (gnus-message 5 "Checking bogus newsgroups...done")))
11719
11720 (defun gnus-check-duplicate-killed-groups ()
11721   "Remove duplicates from the list of killed groups."
11722   (interactive)
11723   (let ((killed gnus-killed-list))
11724     (while killed
11725       (gnus-message 9 "%d" (length killed))
11726       (setcdr killed (delete (car killed) (cdr killed)))
11727       (setq killed (cdr killed)))))
11728
11729 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11730 ;; and compute how many unread articles there are in each group.
11731 (defun gnus-get-unread-articles (&optional level) 
11732   (let* ((newsrc (cdr gnus-newsrc-alist))
11733          (level (or level (1+ gnus-level-subscribed)))
11734          (foreign-level
11735           (min 
11736            (cond ((and gnus-activate-foreign-newsgroups 
11737                        (not (numberp gnus-activate-foreign-newsgroups)))
11738                   (1+ gnus-level-subscribed))
11739                  ((numberp gnus-activate-foreign-newsgroups)
11740                   gnus-activate-foreign-newsgroups)
11741                  (t 0))
11742            level))
11743          info group active virtuals method)
11744     (gnus-message 5 "Checking new news...")
11745
11746     (while newsrc
11747       (setq info (car newsrc)
11748             group (car info)
11749             active (gnus-gethash group gnus-active-hashtb))
11750
11751       ;; Check newsgroups. If the user doesn't want to check them, or
11752       ;; they can't be checked (for instance, if the news server can't
11753       ;; be reached) we just set the number of unread articles in this
11754       ;; newsgroup to t. This means that Gnus thinks that there are
11755       ;; unread articles, but it has no idea how many.
11756       (if (and (setq method (nth 4 info))
11757                (not (gnus-server-equal gnus-select-method
11758                                        (gnus-server-get-method nil method)))
11759                (not (gnus-secondary-method-p method)))
11760           ;; These groups are foreign. Check the level.
11761           (if (<= (nth 1 info) foreign-level)
11762               (if (eq (car (if (stringp method) 
11763                                (gnus-server-to-method method)
11764                              (nth 4 info))) 'nnvirtual)
11765                   ;; We have to activate the virtual groups after all
11766                   ;; the others, so we just pop them on a list for
11767                   ;; now. 
11768                   (setq virtuals (cons info virtuals))
11769                 (and (setq active (gnus-activate-newsgroup (car info)))
11770                      ;; Close the groups as we look at them!
11771                      (gnus-close-group group))))
11772                 
11773         ;; These groups are native or secondary. 
11774         (if (and (not gnus-have-read-active-file)
11775                  (<= (nth 1 info) level))
11776             (setq active (gnus-activate-newsgroup (car info)))))
11777       
11778       (if active
11779           (gnus-get-unread-articles-in-group info active)
11780         ;; The group couldn't be reached, so we nix out the number of
11781         ;; unread articles and stuff.
11782         (gnus-sethash group nil gnus-active-hashtb)
11783         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
11784
11785       (setq newsrc (cdr newsrc)))
11786
11787     ;; Activate the virtual groups. This has to be done after all the
11788     ;; other groups. 
11789     ;; !!! If one virtual group contains another virtual group, even
11790     ;; doing it this way might cause problems.
11791    (while virtuals
11792       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11793            (gnus-get-unread-articles-in-group (car virtuals) active))
11794       (setq virtuals (cdr virtuals)))
11795
11796     (gnus-message 5 "Checking new news...done")))
11797
11798 ;; Create a hash table out of the newsrc alist. The `car's of the
11799 ;; alist elements are used as keys.
11800 (defun gnus-make-hashtable-from-newsrc-alist ()
11801   (let ((alist gnus-newsrc-alist)
11802         (ohashtb gnus-newsrc-hashtb)
11803         prev)
11804     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11805     (setq alist 
11806           (setq prev (setq gnus-newsrc-alist 
11807                            (if (equal (car (car gnus-newsrc-alist))
11808                                       "dummy.group")
11809                                gnus-newsrc-alist
11810                              (cons (list "dummy.group" 0 nil) alist)))))
11811     (while alist
11812       (gnus-sethash (car (car alist)) 
11813                     (cons (and ohashtb (car (gnus-gethash 
11814                                              (car (car alist)) ohashtb))) 
11815                           prev) gnus-newsrc-hashtb)
11816       (setq prev alist
11817             alist (cdr alist)))))
11818
11819 (defun gnus-make-hashtable-from-killed ()
11820   "Create a hash table from the killed and zombie lists."
11821   (let ((lists '(gnus-killed-list gnus-zombie-list))
11822         list)
11823     (setq gnus-killed-hashtb 
11824           (gnus-make-hashtable 
11825            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11826     (while lists
11827       (setq list (symbol-value (car lists)))
11828       (setq lists (cdr lists))
11829       (while list
11830         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11831         (setq list (cdr list))))))
11832
11833 (defun gnus-get-unread-articles-in-group (info active)
11834   (let* ((range (nth 2 info))
11835          (num 0)
11836          (marked (nth 3 info)))
11837     ;; If a cache is present, we may have to alter the active info.
11838     (and gnus-use-cache
11839          (gnus-cache-possibly-alter-active (car info) active))
11840     ;; Modify the list of read articles according to what articles 
11841     ;; are available; then tally the unread articles and add the
11842     ;; number to the group hash table entry.
11843     (cond ((zerop (cdr active))
11844            (setq num 0))
11845           ((not range)
11846            (setq num (- (1+ (cdr active)) (car active))))
11847           ((not (listp (cdr range)))
11848            ;; Fix a single (num . num) range according to the
11849            ;; active hash table.
11850            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11851            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11852            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11853            ;; Compute number of unread articles.
11854            (setq num (max 0 (- (cdr active) 
11855                                (- (1+ (cdr range)) (car range))))))
11856           (t
11857            ;; The read list is a list of ranges. Fix them according to
11858            ;; the active hash table.
11859            ;; First peel off any elements that are below the lower
11860            ;; active limit. 
11861            (while (and (cdr range) 
11862                        (>= (car active) 
11863                            (or (and (atom (car (cdr range))) (car (cdr range)))
11864                                (car (car (cdr range))))))
11865              (if (numberp (car range))
11866                  (setcar range 
11867                          (cons (car range) 
11868                                (or (and (numberp (car (cdr range)))
11869                                         (car (cdr range))) 
11870                                    (cdr (car (cdr range))))))
11871                (setcdr (car range) 
11872                        (or (and (numberp (nth 1 range)) (nth 1 range))
11873                            (cdr (car (cdr range))))))
11874              (setcdr range (cdr (cdr range))))
11875            ;; Adjust the first element to be the same as the lower limit. 
11876            (if (and (not (atom (car range))) 
11877                     (< (cdr (car range)) (car active)))
11878                (setcdr (car range) (1- (car active))))
11879            ;; Then we want to peel off any elements that are higher
11880            ;; than the upper active limit.  
11881            (let ((srange range))
11882              ;; Go past all legal elements.
11883              (while (and (cdr srange) 
11884                          (<= (or (and (atom (car (cdr srange)))
11885                                       (car (cdr srange)))
11886                                  (car (car (cdr srange)))) (cdr active)))
11887                (setq srange (cdr srange)))
11888              (if (cdr srange)
11889                  ;; Nuke all remaining illegal elements.
11890                  (setcdr srange nil))
11891
11892              ;; Adjust the final element.
11893              (if (and (not (atom (car srange)))
11894                       (> (cdr (car srange)) (cdr active)))
11895                  (setcdr (car srange) (cdr active))))
11896            ;; Compute the number of unread articles.
11897            (while range
11898              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11899                                          (cdr (car range))))
11900                                  (or (and (atom (car range)) (car range))
11901                                      (car (car range))))))
11902              (setq range (cdr range)))
11903            (setq num (max 0 (- (cdr active) num)))))
11904     (and info
11905          (progn
11906            (and (assq 'tick marked)
11907                 (inline (gnus-remove-illegal-marked-articles
11908                          (assq 'tick marked) (nth 2 info))))
11909            (and (assq 'dormant marked)
11910                 (inline (gnus-remove-illegal-marked-articles
11911                          (assq 'dormant marked) (nth 2 info))))
11912            (setcar
11913             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11914             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11915                                 (length (cdr (assq 'dormant marked)))))))))
11916     num))
11917
11918 (defun gnus-remove-illegal-marked-articles (marked ranges)
11919   (let ((m (cdr marked)))
11920     ;; Make sure that all ticked articles are a subset of the unread
11921     ;; articles. 
11922     (while m
11923       (if (gnus-member-of-range (car m) ranges)
11924           (setcdr marked (cdr m))
11925         (setq marked m))
11926       (setq m (cdr m)))))
11927
11928 (defun gnus-activate-newsgroup (group)
11929   (let ((method (gnus-find-method-for-group group))
11930         active)
11931     (and (or (gnus-server-opened method) (gnus-open-server method))
11932          (gnus-request-group group)
11933          (save-excursion
11934            (set-buffer nntp-server-buffer)
11935            (goto-char (point-min))
11936            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11937                 (progn
11938                   (goto-char (match-beginning 1))
11939                   (gnus-sethash 
11940                    group (setq active (cons (read (current-buffer))
11941                                             (read (current-buffer))))
11942                    gnus-active-hashtb))
11943                 active)))))
11944
11945 (defun gnus-update-read-articles 
11946   (group unread unselected ticked &optional domarks replied expirable killed
11947          dormant bookmark score)
11948   "Update the list of read and ticked articles in GROUP using the
11949 UNREAD and TICKED lists.
11950 Note: UNSELECTED has to be sorted over `<'.
11951 Returns whether the updating was successful."
11952   (let* ((active (or gnus-newsgroup-active 
11953                      (gnus-gethash group gnus-active-hashtb)))
11954          (entry (gnus-gethash group gnus-newsrc-hashtb))
11955          (info (nth 2 entry))
11956          (marked (nth 3 info))
11957          (prev 1)
11958          (unread (sort (copy-sequence unread) (function <)))
11959          read)
11960     (if (or (not info) (not active))
11961         ;; There is no info on this group if it was, in fact,
11962         ;; killed. Gnus stores no information on killed groups, so
11963         ;; there's nothing to be done. 
11964         ;; One could store the information somewhere temporarily,
11965         ;; perhaps... Hmmm... 
11966         ()
11967       ;; Remove any negative articles numbers.
11968       (while (and unread (< (car unread) 0))
11969         (setq unread (cdr unread)))
11970       ;; Remove any expired article numbers
11971       (while (and unread (< (car unread) (car active)))
11972         (setq unread (cdr unread)))
11973       (while (and ticked (< (car ticked) (car active)))
11974         (setq ticked (cdr ticked)))
11975       (while (and dormant (< (car dormant) (car active)))
11976         (setq dormant (cdr dormant)))
11977       (setq unread (sort (append unselected unread) '<))
11978       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11979       (setcar entry (max 0 (- (length unread) (length ticked) 
11980                               (length dormant))))
11981       ;; Compute the ranges of read articles by looking at the list of
11982       ;; unread articles.  
11983       (while unread
11984         (if (/= (car unread) prev)
11985             (setq read (cons (if (= prev (1- (car unread))) prev
11986                                (cons prev (1- (car unread)))) read)))
11987         (setq prev (1+ (car unread)))
11988         (setq unread (cdr unread)))
11989       (if (<= prev (cdr active))
11990           (setq read (cons (cons prev (cdr active)) read)))
11991       ;; Enter this list into the group info.
11992       (setcar (cdr (cdr info)) 
11993               (if (> (length read) 1) (nreverse read) read))
11994       ;; Enter the list of ticked articles.
11995       (gnus-set-marked-articles 
11996        info ticked
11997        (if domarks replied (cdr (assq 'reply marked)))
11998        (if domarks expirable (cdr (assq 'expire marked)))
11999        (if domarks killed (cdr (assq 'killed marked)))
12000        (if domarks dormant (cdr (assq 'dormant marked)))
12001        (if domarks bookmark (cdr (assq 'bookmark marked)))
12002        (if domarks score (cdr (assq 'score marked))))
12003       t)))
12004
12005 (defun gnus-make-articles-unread (group articles)
12006   "Mark ARTICLES in GROUP as unread."
12007   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12008                           (gnus-gethash (gnus-group-real-name group)
12009                                         gnus-newsrc-hashtb))))
12010          (ranges (nth 2 info))
12011          news)
12012     (while articles
12013       (and (gnus-member-of-range (car articles) ranges)
12014            (setq news (cons (car articles) news)))
12015       (setq articles (cdr articles)))
12016     (if (not news)
12017         ()
12018       (setcar (nthcdr 2 info)
12019               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12020       (gnus-group-update-group group t))))
12021
12022 ;; Get the active file(s) from the backend(s).
12023 (defun gnus-read-active-file ()
12024   (gnus-group-set-mode-line)
12025   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12026                          (gnus-open-server gnus-select-method))
12027                      ;; The native server is available.
12028                      (cons gnus-select-method gnus-secondary-select-methods)
12029                    ;; The native server is down, so we just do the
12030                    ;; secondary ones.   
12031                    gnus-secondary-select-methods))
12032         list-type)
12033     (setq gnus-have-read-active-file nil)
12034     (save-excursion
12035       (set-buffer nntp-server-buffer)
12036       (while methods
12037         (let* ((where (nth 1 (car methods)))
12038                (mesg (format "Reading active file%s via %s..."
12039                              (if (and where (not (zerop (length where))))
12040                                  (concat " from " where) "")
12041                              (car (car methods)))))
12042           (gnus-message 5 mesg)
12043           (gnus-check-news-server (car methods))
12044           (cond 
12045            ((and (eq gnus-read-active-file 'some)
12046                  (gnus-check-backend-function
12047                   'retrieve-groups (car (car methods))))
12048             (let ((newsrc (cdr gnus-newsrc-alist))
12049                   groups)
12050               (while newsrc
12051                 (and (gnus-server-equal 
12052                       (gnus-find-method-for-group
12053                        (car (car newsrc)) (car newsrc))
12054                       (gnus-server-get-method nil (car methods)))
12055                      (setq groups (cons (car (car newsrc)) groups)))
12056                 (setq newsrc (cdr newsrc)))
12057               (setq list-type (gnus-retrieve-groups groups (car methods)))
12058               (cond ((not list-type)
12059                      (gnus-message 
12060                       1 "Cannot read partial active file from %s server." 
12061                       (car (car methods)))
12062                      (ding)
12063                      (sit-for 2))
12064                     ((eq list-type 'active)
12065                      (gnus-active-to-gnus-format (car methods)))
12066                     (t
12067                      (gnus-groups-to-gnus-format (car methods))))))
12068            (t
12069             (if (not (gnus-request-list (car methods)))
12070                 (progn
12071                   (gnus-message 1 "Cannot read active file from %s server." 
12072                                 (car (car methods)))
12073                   (ding))
12074               (gnus-active-to-gnus-format (car methods))
12075               (setq gnus-have-read-active-file t)
12076               (gnus-message 5 "%sdone" mesg)))))
12077         (setq methods (cdr methods))))))
12078
12079 ;; Read an active file and place the results in `gnus-active-hashtb'.
12080 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12081   (let ((cur (current-buffer))
12082         (hashtb (or hashtb 
12083                     (if (and gnus-active-hashtb 
12084                              (not (equal method gnus-select-method)))
12085                         gnus-active-hashtb
12086                       (setq gnus-active-hashtb
12087                             (if (equal method gnus-select-method)
12088                                 (gnus-make-hashtable 
12089                                  (count-lines (point-min) (point-max)))
12090                               (gnus-make-hashtable 4096)))))))
12091     ;; Delete unnecessary lines.
12092     (goto-char (point-min))
12093     (while (search-forward "\nto." nil t)
12094       (delete-region (1+ (match-beginning 0)) 
12095                      (progn (forward-line 1) (point))))
12096     (or (string= gnus-ignored-newsgroups "")
12097         (progn
12098           (goto-char (point-min))
12099           (delete-matching-lines gnus-ignored-newsgroups)))
12100     ;; If these are groups from a foreign select method, we insert the
12101     ;; group prefix in front of the group names. 
12102     (and method (not (eq method gnus-select-method))
12103          (let ((prefix (gnus-group-prefixed-name "" method)))
12104            (goto-char (point-min))
12105            (while (and (not (eobp))
12106                        (progn (insert prefix)
12107                               (zerop (forward-line 1)))))))
12108     (goto-char (point-min))
12109     ;; Store active file in hashtable.
12110     (goto-char (point-min))
12111     (if (string-match "%[oO]" gnus-group-line-format)
12112         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12113         ;; If we want information on moderated groups, we use this
12114         ;; loop...   
12115         (let* ((mod-hashtb (make-vector 7 0))
12116                (m (intern "m" mod-hashtb))
12117                group max min)
12118           (while (not (eobp))
12119             (condition-case nil
12120                 (progn
12121                   (narrow-to-region (point) (gnus-point-at-eol))
12122                   (setq group (let ((obarray hashtb)) (read cur)))
12123                   (if (and (numberp (setq max (read cur)))
12124                            (numberp (setq min (read cur))))
12125                       (set group (cons min max))
12126                     (set group nil))
12127                   ;; Enter moderated groups into a list.
12128                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12129                       (setq gnus-moderated-list 
12130                             (cons (symbol-name group) gnus-moderated-list))))
12131               (error nil))
12132             (widen)
12133             (forward-line 1)))
12134       ;; And if we do not care about moderation, we use this loop,
12135       ;; which is faster.
12136       (let (group max min)
12137         (while (not (eobp))
12138           (condition-case ()
12139               (progn
12140                 (narrow-to-region (point) (gnus-point-at-eol))
12141                 ;; group gets set to a symbol interned in the hash table
12142                 ;; (what a hack!!)
12143                 (setq group (let ((obarray hashtb)) (read cur)))
12144                 (if (and (numberp (setq max (read cur)))
12145                          (numberp (setq min (read cur))))
12146                     (set group (cons min max))
12147                   (set group nil)))
12148             (error 
12149              (progn 
12150                (if ignore-errors
12151                    (set group nil)
12152                  (ding) 
12153                  (gnus-message 3 "Warning - illegal active: %s"
12154                                (buffer-substring 
12155                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12156                  nil))))
12157           (widen)
12158           (forward-line 1))))))
12159
12160 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12161   ;; Parse a "groups" active file.
12162   (let ((cur (current-buffer))
12163         (hashtb (or hashtb 
12164                     (if method
12165                         gnus-active-hashtb
12166                       (setq gnus-active-hashtb
12167                             (gnus-make-hashtable 
12168                              (count-lines (point-min) (point-max)))))))
12169         (prefix (and method (not (eq method gnus-select-method))
12170                      (gnus-group-prefixed-name "" method))))
12171
12172     (goto-char (point-min))
12173     (condition-case ()
12174         ;; We split this into to separate loops, one with the prefix
12175         ;; and one without to speed the reading up somewhat.
12176         (if prefix
12177             (let (min max opoint)
12178               (while (not (eobp))
12179                 (read cur) (read cur)
12180                 (setq min (read cur)
12181                       max (read cur)
12182                       opoint (point))
12183                 (skip-chars-forward " \t")
12184                 (insert prefix)
12185                 (goto-char opoint)
12186                 (set (let ((obarray hashtb)) (read cur)) 
12187                      (cons min max))
12188                 (forward-line 1)))
12189           (let (min max)
12190             (while (not (eobp))
12191               (if (= (following-char) ?2)
12192                   (progn
12193                     (read cur) (read cur)
12194                     (setq min (read cur)
12195                           max (read cur))
12196                     (set (let ((obarray hashtb)) (read cur)) 
12197                          (cons min max))))
12198               (forward-line 1))))
12199       (error 
12200        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12201
12202 (defun gnus-read-newsrc-file (&optional force)
12203   "Read startup file.
12204 If FORCE is non-nil, the .newsrc file is read."
12205   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12206   ;; Reset variables that might be defined in the .newsrc.eld file.
12207   (let ((variables gnus-variable-list))
12208     (while variables
12209       (set (car variables) nil)
12210       (setq variables (cdr variables))))
12211   (let* ((newsrc-file gnus-current-startup-file)
12212          (quick-file (concat newsrc-file ".el")))
12213     (save-excursion
12214       ;; We always load the .newsrc.eld file. If always contains
12215       ;; much information that can not be gotten from the .newsrc
12216       ;; file (ticked articles, killed groups, foreign methods, etc.)
12217       (gnus-read-newsrc-el-file quick-file)
12218  
12219       (if (or force
12220               (and (file-newer-than-file-p newsrc-file quick-file)
12221                    (file-newer-than-file-p newsrc-file 
12222                                            (concat quick-file "d")))
12223               (not gnus-newsrc-alist))
12224           ;; We read the .newsrc file. Note that if there if a
12225           ;; .newsrc.eld file exists, it has already been read, and
12226           ;; the `gnus-newsrc-hashtb' has been created. While reading
12227           ;; the .newsrc file, Gnus will only use the information it
12228           ;; can find there for changing the data already read -
12229           ;; ie. reading the .newsrc file will not trash the data
12230           ;; already read (except for read articles).
12231           (save-excursion
12232             (gnus-message 5 "Reading %s..." newsrc-file)
12233             (set-buffer (find-file-noselect newsrc-file))
12234             (buffer-disable-undo (current-buffer))
12235             (gnus-newsrc-to-gnus-format)
12236             (kill-buffer (current-buffer))
12237             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12238
12239 (defun gnus-read-newsrc-el-file (file)
12240   (let ((ding-file (concat file "d")))
12241     ;; We always, always read the .eld file.
12242     (gnus-message 5 "Reading %s..." ding-file)
12243     (let (gnus-newsrc-assoc)
12244       (condition-case nil
12245           (load ding-file t t t)
12246         (error nil))
12247       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12248     (let ((inhibit-quit t))
12249       (gnus-uncompress-newsrc-alist))
12250     (gnus-make-hashtable-from-newsrc-alist)
12251     (if (not (file-newer-than-file-p file ding-file))
12252         ()
12253       ;; Old format quick file
12254       (gnus-message 5 "Reading %s..." file)
12255       ;; The .el file is newer than the .eld file, so we read that one
12256       ;; as well. 
12257       (gnus-read-old-newsrc-el-file file))))
12258
12259 ;; Parse the old-style quick startup file
12260 (defun gnus-read-old-newsrc-el-file (file)
12261   (let (newsrc killed marked group m)
12262     (prog1
12263         (let ((gnus-killed-assoc nil)
12264               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12265           (prog1
12266               (condition-case nil
12267                   (load file t t t)
12268                 (error nil))
12269             (setq newsrc gnus-newsrc-assoc
12270                   killed gnus-killed-assoc
12271                   marked gnus-marked-assoc)))
12272       (setq gnus-newsrc-alist nil)
12273       (while newsrc
12274         (setq group (car newsrc))
12275         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12276           (if info
12277               (progn
12278                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12279                 (setcar (cdr info)
12280                         (if (nth 1 group) gnus-level-default-subscribed 
12281                           gnus-level-default-unsubscribed))
12282                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12283             (setq gnus-newsrc-alist
12284                   (cons 
12285                    (setq info
12286                          (list (car group)
12287                                (if (nth 1 group) gnus-level-default-subscribed
12288                                  gnus-level-default-unsubscribed) 
12289                                (cdr (cdr group))))
12290                    gnus-newsrc-alist)))
12291           (if (setq m (assoc (car group) marked))
12292             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12293         (setq newsrc (cdr newsrc)))
12294       (setq newsrc killed)
12295       (while newsrc
12296         (setcar newsrc (car (car newsrc)))
12297         (setq newsrc (cdr newsrc)))
12298       (setq gnus-killed-list killed))
12299     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12300     (gnus-make-hashtable-from-newsrc-alist)))
12301       
12302 (defun gnus-make-newsrc-file (file)
12303   "Make server dependent file name by catenating FILE and server host name."
12304   (let* ((file (expand-file-name file nil))
12305          (real-file (concat file "-" (nth 1 gnus-select-method))))
12306     (if (file-exists-p real-file)
12307         real-file file)))
12308
12309 (defun gnus-uncompress-newsrc-alist ()
12310   ;; Uncompress all lists of marked articles in the newsrc assoc.
12311   (let ((newsrc gnus-newsrc-alist)
12312         marked)
12313     (while newsrc
12314       (if (not (setq marked (nth 3 (car newsrc))))
12315           ()
12316         (while marked
12317           (or (eq 'score (car (car marked)))
12318               (eq 'bookmark (car (car marked)))
12319               (eq 'killed (car (car marked)))
12320               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12321           (setq marked (cdr marked))))
12322       (setq newsrc (cdr newsrc)))))
12323
12324 (defun gnus-compress-newsrc-alist ()
12325   ;; Compress all lists of marked articles in the newsrc assoc.
12326   (let ((newsrc gnus-newsrc-alist)
12327         marked)
12328     (while newsrc
12329       (if (not (setq marked (nth 3 (car newsrc))))
12330           ()
12331         (while marked
12332           (or (eq 'score (car (car marked)))
12333               (eq 'bookmark (car (car marked)))
12334               (eq 'killed (car (car marked)))
12335               (setcdr (car marked) 
12336                       (condition-case ()
12337                           (gnus-compress-sequence 
12338                            (sort (cdr (car marked)) '<) t)
12339                         (error (cdr (car marked))))))
12340           (setq marked (cdr marked))))
12341       (setq newsrc (cdr newsrc)))))
12342
12343 (defun gnus-newsrc-to-gnus-format ()
12344   (setq gnus-newsrc-options "")
12345   (setq gnus-newsrc-options-n nil)
12346
12347   (or gnus-active-hashtb
12348       (setq gnus-active-hashtb (make-vector 4095 0)))
12349   (let ((buf (current-buffer))
12350         (already-read (> (length gnus-newsrc-alist) 1))
12351         group subscribed options-symbol newsrc
12352         symbol reads num1)
12353     (goto-char (point-min))
12354     ;; We intern the symbol `options' in the active hashtb so that we
12355     ;; can `eq' against it later.
12356     (setq options-symbol (intern "options" gnus-active-hashtb))
12357   
12358     (while (not (eobp))
12359       ;; We first read the first word on the line by narrowing and
12360       ;; then reading into `gnus-active-hashtb'.  Most groups will
12361       ;; already exist in that hashtb, so this will save some string
12362       ;; space.
12363       (narrow-to-region
12364        (point)
12365        (progn (skip-chars-forward "^ \t!:\n") (point)))
12366       (goto-char (point-min))
12367       (setq symbol 
12368             (and (/= (point-min) (point-max))
12369                  (let ((obarray gnus-active-hashtb)) (read buf))))
12370       (widen)
12371       ;; Now, the symbol we have read is either `options' or a group
12372       ;; name.  If it is an options line, we just add it to a string. 
12373       (cond 
12374        ((eq symbol options-symbol)
12375         (setq gnus-newsrc-options
12376               ;; This concatting is quite inefficient, but since our
12377               ;; thorough studies show that approx 99.37% of all
12378               ;; .newsrc files only contain a single options line, we
12379               ;; don't give a damn, frankly, my dear.
12380               (concat gnus-newsrc-options
12381                       (buffer-substring 
12382                        (gnus-point-at-bol)
12383                        ;; Options may continue on the next line.
12384                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12385                                 (progn (beginning-of-line) (point)))
12386                            (point))))))
12387        (symbol
12388         ;; It was a group name.
12389         (setq subscribed (= (following-char) ?:)
12390               group (symbol-name symbol)
12391               reads nil)
12392         (if (eolp)
12393             ;; If the line ends here, this is clearly a buggy line, so
12394             ;; we put point a the beginning of line and let the cond
12395             ;; below do the error handling.
12396             (beginning-of-line)
12397           ;; We skip to the beginning of the ranges.
12398           (skip-chars-forward "!: \t"))
12399         ;; We are now at the beginning of the list of read articles.
12400         ;; We read them range by range.
12401         (while
12402             (cond 
12403              ((looking-at "[0-9]+")
12404               ;; We narrow and read a number instead of buffer-substring/
12405               ;; string-to-int because it's faster. narrow/widen is
12406               ;; faster than save-restriction/narrow, and save-restriction
12407               ;; produces a garbage object.
12408               (setq num1 (progn
12409                            (narrow-to-region (match-beginning 0) (match-end 0))
12410                            (read buf)))
12411               (widen)
12412               ;; If the next character is a dash, then this is a range.
12413               (if (= (following-char) ?-)
12414                   (progn
12415                     ;; We read the upper bound of the range.
12416                     (forward-char 1)
12417                     (if (not (looking-at "[0-9]+"))
12418                         ;; This is a buggy line, by we pretend that
12419                         ;; it's kinda OK. Perhaps the user should be
12420                         ;; dinged? 
12421                         (setq reads (cons num1 reads))
12422                       (setq reads 
12423                             (cons 
12424                              (cons num1 (progn
12425                                           (narrow-to-region (match-beginning 0) 
12426                                                             (match-end 0))
12427                                           (read buf)))
12428                              reads))
12429                       (widen)))
12430                 ;; It was just a simple number, so we add it to the
12431                 ;; list of ranges.
12432                 (setq reads (cons num1 reads)))
12433               ;; If the next char in ?\n, then we have reached the end
12434               ;; of the line and return nil.
12435               (/= (following-char) ?\n))
12436              ((= (following-char) ?\n)
12437               ;; End of line, so we end.
12438               nil)
12439              (t
12440               ;; Not numbers and not eol, so this might be a buggy
12441               ;; line... 
12442               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12443                   (progn
12444                     ;; The line was buggy.
12445                     (setq group nil)
12446                     (gnus-message 3 "Mangled line: %s" 
12447                                   (buffer-substring (gnus-point-at-bol) 
12448                                                     (gnus-point-at-eol)))
12449                     (ding)
12450                     (sit-for 1)))
12451               nil))
12452           ;; Skip past ", ". Spaces are illegal in these ranges, but
12453           ;; we allow them, because it's a common mistake to put a
12454           ;; space after the comma.
12455           (skip-chars-forward ", "))
12456
12457         ;; We have already read .newsrc.eld, so we gently update the
12458         ;; data in the hash table with the information we have just
12459         ;; read. 
12460         (if (not group)
12461             ()
12462           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12463                 level)
12464             (if info
12465                 ;; There is an entry for this file in the alist.
12466                 (progn
12467                   (setcar (nthcdr 2 info) (nreverse reads))
12468                   ;; We update the level very gently.  In fact, we
12469                   ;; only change it if there's been a status change
12470                   ;; from subscribed to unsubscribed, or vice versa.
12471                   (setq level (nth 1 info))
12472                   (cond ((and (<= level gnus-level-subscribed)
12473                               (not subscribed))
12474                          (setq level (if reads
12475                                          gnus-level-default-unsubscribed 
12476                                        (1+ gnus-level-default-unsubscribed))))
12477                         ((and (> level gnus-level-subscribed) subscribed)
12478                          (setq level gnus-level-default-subscribed)))
12479                   (setcar (cdr info) level))
12480               ;; This is a new group.
12481               (setq info (list group 
12482                                (if subscribed
12483                                    gnus-level-default-subscribed 
12484                                  (if reads
12485                                      (1+ gnus-level-subscribed)
12486                                    gnus-level-default-unsubscribed))
12487                                (nreverse reads))))
12488             (setq newsrc (cons info newsrc))))))
12489       (forward-line 1))
12490     
12491     (setq newsrc (nreverse newsrc))
12492
12493     (if (not already-read)
12494         ()
12495       ;; We now have two newsrc lists - `newsrc', which is what we
12496       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12497       ;; what we've read from .newsrc.eld. We have to merge these
12498       ;; lists. We do this by "attaching" any (foreign) groups in the
12499       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12500       (let ((rc (cdr gnus-newsrc-alist))
12501             (prev gnus-newsrc-alist)
12502             entry mentry)
12503         (while rc
12504           (or (null (nth 4 (car rc))) ; It's a native group.
12505               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12506               (if (setq entry (assoc (car (car prev)) newsrc))
12507                   (setcdr (setq mentry (memq entry newsrc))
12508                           (cons (car rc) (cdr mentry)))
12509                 (setq newsrc (cons (car rc) newsrc))))
12510           (setq prev rc
12511                 rc (cdr rc)))))
12512
12513     (setq gnus-newsrc-alist newsrc)
12514     ;; We make the newsrc hashtb.
12515     (gnus-make-hashtable-from-newsrc-alist)
12516
12517     ;; Finally, if we read some options lines, we parse them.
12518     (or (string= gnus-newsrc-options "")
12519         (gnus-newsrc-parse-options gnus-newsrc-options))))
12520
12521 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12522 ;; The return value will be a list on the form
12523 ;; ((regexp1 . ignore)
12524 ;;  (regexp2 . subscribe)...)
12525 ;; When handling new newsgroups, groups that match a `ignore' regexp
12526 ;; will be ignored, and groups that match a `subscribe' regexp will be
12527 ;; subscribed. A line like
12528 ;; options -n !all rec.all
12529 ;; will lead to a list that looks like
12530 ;; (("^rec\\..+" . subscribe) 
12531 ;;  ("^.+" . ignore))
12532 ;; So all "rec.*" groups will be subscribed, while all the other
12533 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12534 ;; different from "options -n rec.all !all". 
12535 (defun gnus-newsrc-parse-options (options)
12536   (let (out eol)
12537     (save-excursion
12538       (gnus-set-work-buffer)
12539       (insert (regexp-quote options))
12540       ;; First we treat all continuation lines.
12541       (goto-char (point-min))
12542       (while (re-search-forward "\n[ \t]+" nil t)
12543         (replace-match " " t t))
12544       ;; Then we transform all "all"s into ".+"s.
12545       (goto-char (point-min))
12546       (while (re-search-forward "\\ball\\b" nil t)
12547         (replace-match ".+" t t))
12548       (goto-char (point-min))
12549       ;; We remove all other options than the "-n" ones.
12550       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12551         (replace-match " ")
12552         (forward-char -1))
12553       (goto-char (point-min))
12554
12555       ;; We are only interested in "options -n" lines - we
12556       ;; ignore the other option lines.
12557       (while (re-search-forward "[ \t]-n" nil t)
12558         (setq eol 
12559               (or (save-excursion
12560                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12561                          (- (point) 2)))
12562                   (gnus-point-at-eol)))
12563         ;; Search for all "words"...
12564         (while (re-search-forward "[^ \t,\n]+" eol t)
12565           (if (= (char-after (match-beginning 0)) ?!)
12566               ;; If the word begins with a bang (!), this is a "not"
12567               ;; spec. We put this spec (minus the bang) and the
12568               ;; symbol `ignore' into the list.
12569               (setq out (cons (cons (concat 
12570                                      "^" (buffer-substring 
12571                                           (1+ (match-beginning 0))
12572                                           (match-end 0)))
12573                                     'ignore) out))
12574             ;; There was no bang, so this is a "yes" spec.
12575             (setq out (cons (cons (concat 
12576                                    "^" (buffer-substring (match-beginning 0)
12577                                                          (match-end 0)))
12578                                   'subscribe) out)))))
12579     
12580       (setq gnus-newsrc-options-n out))))
12581                
12582
12583 (defun gnus-save-newsrc-file ()
12584   "Save .newsrc file."
12585   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12586   ;; from the variable gnus-newsrc-alist.
12587   (and (or gnus-newsrc-alist gnus-killed-list)
12588        gnus-current-startup-file
12589        (let ((make-backup-files t)
12590              (version-control nil)
12591              (require-final-newline t)) ;Don't ask even if requested.
12592          ;; You can stop or change version control of backup file.
12593          ;; Suggested by jason@violet.berkeley.edu.
12594          (run-hooks 'gnus-save-newsrc-hook)
12595          (save-excursion
12596            (if (or (not gnus-dribble-buffer)
12597                    (not (buffer-name gnus-dribble-buffer))
12598                    (zerop (save-excursion
12599                             (set-buffer gnus-dribble-buffer)
12600                             (buffer-size))))
12601                (gnus-message 4 "(No changes need to be saved)")
12602              (if gnus-save-newsrc-file
12603                  (progn
12604                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12605                    ;; Make backup file of master newsrc.
12606                    (gnus-gnus-to-newsrc-format)
12607                    (gnus-message 5 "Saving %s...done"
12608                                  gnus-current-startup-file)))
12609              ;; Quickly loadable .newsrc.
12610              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12611              (gnus-add-current-to-buffer-list)
12612              (buffer-disable-undo (current-buffer))
12613              (erase-buffer)
12614              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12615              (gnus-gnus-to-quick-newsrc-format)
12616              (write-region 1 (point-max) 
12617                            (concat gnus-current-startup-file ".eld") 
12618                            nil 'nomesg)
12619              (kill-buffer (current-buffer))
12620              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12621              (gnus-dribble-delete-file))))))
12622
12623 (defun gnus-gnus-to-quick-newsrc-format ()
12624   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12625   (insert ";; (ding) Gnus startup file.\n")
12626   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12627   (insert ";; to read .newsrc.\n")
12628   (let ((variables gnus-variable-list)
12629         (inhibit-quit t)
12630         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12631         variable)
12632     ;; insert lisp expressions.
12633     (gnus-compress-newsrc-alist)
12634     (while variables
12635       (setq variable (car variables))
12636       (and (boundp variable)
12637            (symbol-value variable)
12638            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12639            (insert "(setq " (symbol-name variable) " '"
12640                    (prin1-to-string (symbol-value variable))
12641                    ")\n"))
12642       (setq variables (cdr variables)))
12643     (gnus-uncompress-newsrc-alist)))
12644
12645
12646 (defun gnus-gnus-to-newsrc-format ()
12647   ;; Generate and save the .newsrc file.
12648   (let ((newsrc (cdr gnus-newsrc-alist))
12649         info ranges range)
12650     (save-excursion
12651       (set-buffer (create-file-buffer gnus-startup-file))
12652       (buffer-disable-undo (current-buffer))
12653       (erase-buffer)
12654       ;; Write options.
12655       (if gnus-newsrc-options (insert gnus-newsrc-options))
12656       ;; Write subscribed and unsubscribed.
12657       (while newsrc
12658         (setq info (car newsrc))
12659         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12660             (progn
12661               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12662                                      "!" ":"))
12663               (if (setq ranges (nth 2 info))
12664                   (progn
12665                     (insert " ")
12666                     (if (not (listp (cdr ranges)))
12667                         (if (= (car ranges) (cdr ranges))
12668                             (insert (int-to-string (car ranges)))
12669                           (insert (int-to-string (car ranges)) "-" 
12670                                   (int-to-string (cdr ranges))))
12671                       (while ranges
12672                         (setq range (car ranges)
12673                               ranges (cdr ranges))
12674                         (if (or (atom range) (= (car range) (cdr range)))
12675                             (insert (int-to-string 
12676                                      (or (and (atom range) range) 
12677                                          (car range))))
12678                           (insert (int-to-string (car range)) "-"
12679                                   (int-to-string (cdr range))))
12680                         (if ranges (insert ","))))))
12681               (insert "\n")))
12682         (setq newsrc (cdr newsrc)))
12683       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12684       (kill-buffer (current-buffer)))))
12685
12686 (defun gnus-read-all-descriptions-files ()
12687   (let ((methods (nconc (list gnus-select-method) 
12688                         gnus-secondary-select-methods)))
12689     (while methods
12690       (gnus-read-descriptions-file (car methods))
12691       (setq methods (cdr methods)))
12692     t))
12693
12694 (defun gnus-read-descriptions-file (&optional method)
12695   (let ((method (or method gnus-select-method)))
12696     ;; We create the hashtable whether we manage to read the desc file
12697     ;; to avoid trying to re-read after a failed read.
12698     (or gnus-description-hashtb
12699         (setq gnus-description-hashtb 
12700               (gnus-make-hashtable (length gnus-active-hashtb))))
12701     ;; Mark this method's desc file as read.
12702     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12703                   gnus-description-hashtb)
12704
12705     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12706     (cond 
12707      ((not (or (gnus-server-opened method)
12708                (gnus-open-server method)))
12709       (gnus-message 1 "Couldn't open server")
12710       nil)
12711      ((not (gnus-request-list-newsgroups method))
12712       (gnus-message 1 "Couldn't read newsgroups descriptions")
12713       nil)
12714      (t
12715       (let (group)
12716         (save-excursion
12717           (save-restriction
12718             (set-buffer nntp-server-buffer)
12719             (goto-char (point-min))
12720             (if (or (search-forward "\n.\n" nil t)
12721                     (goto-char (point-max)))
12722                 (progn
12723                   (beginning-of-line)
12724                   (narrow-to-region (point-min) (point))))
12725             (goto-char (point-min))
12726             (while (not (eobp))
12727               ;; If we get an error, we set group to 0, which is not a
12728               ;; symbol... 
12729               (setq group 
12730                     (condition-case ()
12731                         (let ((obarray gnus-description-hashtb))
12732                           ;; Group is set to a symbol interned in this
12733                           ;; hash table.
12734                           (read nntp-server-buffer))
12735                       (error 0)))
12736               (skip-chars-forward " \t")
12737               ;; ... which leads to this line being effectively ignored.
12738               (and (symbolp group)
12739                    (set group (buffer-substring 
12740                                (point) (progn (end-of-line) (point)))))
12741               (forward-line 1))))
12742         (gnus-message 5 "Reading descriptions file...done")
12743         t)))))
12744
12745 (defun gnus-group-get-description (group)
12746   ;; Get the description of a group by sending XGTITLE to the server.
12747   (and (gnus-request-group-description group)
12748        (save-excursion
12749          (set-buffer nntp-server-buffer)
12750          (goto-char (point-min))
12751          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12752               (buffer-substring (match-beginning 1) (match-end 1))))))
12753
12754 ;;;
12755 ;;; Server
12756 ;;;
12757
12758 (defvar gnus-server-mode-hook nil
12759   "Hook run in `gnus-server-mode' buffers.")
12760
12761 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12762   "Format of server lines.
12763 It works along the same lines as a normal formatting string,
12764 with some simple extensions.")
12765
12766 (defvar gnus-server-mode-line-format "(ding) List of servers"
12767   "The format specification for the server mode line.")
12768
12769 (defconst gnus-server-line-format-alist
12770   (list (list ?h 'how ?s)
12771         (list ?n 'name ?s)
12772         (list ?w 'where ?s)
12773         ))
12774
12775 (defconst gnus-server-mode-line-format-alist 
12776   (list (list ?S 'news-server ?s)
12777         (list ?M 'news-method ?s)
12778         (list ?u 'user-defined ?s)))
12779
12780 (defvar gnus-server-line-format-spec nil)
12781 (defvar gnus-server-mode-line-format-spec nil)
12782 (defvar gnus-server-killed-servers nil)
12783
12784 (defvar gnus-server-mode-map nil)
12785 (put 'gnus-server-mode 'mode-class 'special)
12786
12787 (if gnus-server-mode-map
12788     nil
12789   (setq gnus-server-mode-map (make-sparse-keymap))
12790   (suppress-keymap gnus-server-mode-map)
12791   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12792   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12793   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12794   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12795   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12796   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12797   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12798   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12799   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12800   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12801
12802 (defun gnus-server-mode ()
12803   "Major mode for listing and editing servers.
12804
12805 All normal editing commands are switched off.
12806 \\<gnus-server-mode-map>
12807
12808 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12809
12810 The following commands are available:
12811
12812 \\{gnus-server-mode-map}"
12813   (interactive)
12814   (if gnus-visual (gnus-server-make-menu-bar))
12815   (kill-all-local-variables)
12816   (setq mode-line-modified "-- ")
12817   (make-local-variable 'mode-line-format)
12818   (setq mode-line-format (copy-sequence mode-line-format))
12819   (and (equal (nth 3 mode-line-format) "   ")
12820        (setcar (nthcdr 3 mode-line-format) ""))
12821   (setq major-mode 'gnus-server-mode)
12822   (setq mode-name "Server")
12823 ;  (gnus-group-set-mode-line)
12824   (setq mode-line-process nil)
12825   (use-local-map gnus-server-mode-map)
12826   (buffer-disable-undo (current-buffer))
12827   (setq truncate-lines t)
12828   (setq buffer-read-only t)
12829   (run-hooks 'gnus-server-mode-hook))
12830
12831 (defun gnus-server-insert-server-line (sformat name method)
12832   (let* ((sformat (or sformat gnus-server-line-format-spec))
12833          (how (car method))
12834          (where (nth 1 method))
12835          b)
12836     (beginning-of-line)
12837     (setq b (point))
12838     ;; Insert the text.
12839     (insert (eval sformat))
12840     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
12841
12842 (defun gnus-server-setup-buffer ()
12843   (if (get-buffer gnus-server-buffer)
12844       ()
12845     (save-excursion
12846       (set-buffer (get-buffer-create gnus-server-buffer))
12847       (gnus-server-mode)
12848       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12849
12850 (defun gnus-server-prepare ()
12851   (setq gnus-server-mode-line-format-spec 
12852         (gnus-parse-format gnus-server-mode-line-format 
12853                            gnus-server-mode-line-format-alist))
12854   (setq gnus-server-line-format-spec 
12855         (gnus-parse-format gnus-server-line-format 
12856                            gnus-server-line-format-alist))
12857   (let ((alist gnus-server-alist)
12858         (buffer-read-only nil))
12859     (erase-buffer)
12860     (while alist
12861       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12862       (setq alist (cdr alist))))
12863   (goto-char (point-min))
12864   (gnus-server-position-cursor))
12865
12866 (defun gnus-server-server-name ()
12867   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12868     (and server (symbol-name server))))
12869
12870 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12871
12872 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12873
12874 (defun gnus-server-update-server (server)
12875   (save-excursion
12876     (set-buffer gnus-server-buffer)
12877     (let ((buffer-read-only nil)
12878           (info (cdr (assoc server gnus-server-alist))))
12879       (gnus-dribble-enter 
12880        (concat "(gnus-server-set-info \"" server "\" '"
12881                (prin1-to-string info) ")"))
12882       ;; Buffer may be narrowed.
12883       (save-restriction
12884         (widen)
12885         (if (gnus-server-goto-server server)
12886             (delete-region (progn (beginning-of-line) (point))
12887                            (progn (forward-line 1) (point))))
12888         (let ((entry (assoc server gnus-server-alist)))
12889           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12890           (gnus-server-position-cursor))))))
12891
12892 (defun gnus-server-set-info (server info)
12893   ;; Enter a select method into the virtual server alist.
12894   (gnus-dribble-enter 
12895    (concat "(gnus-server-set-info \"" server "\" '"
12896            (prin1-to-string info) ")"))
12897   (let* ((server (nth 1 info))
12898          (entry (assoc server gnus-server-alist)))
12899     (if entry (setcdr entry info)
12900       (setq gnus-server-alist
12901             (nconc gnus-server-alist (list (cons server info)))))))
12902
12903 (defun gnus-server-to-method (server)
12904   ;; Map virtual server names to select methods.
12905   (or (and (equal server "native") gnus-select-method)
12906       (cdr (assoc server gnus-server-alist))))
12907
12908 (defun gnus-server-extend-method (group method)
12909   ;; This function "extends" a virtual server.  If the server is
12910   ;; "hello", and the select method is ("hello" (my-var "something")) 
12911   ;; in the group "alt.alt", this will result in a new virtual server
12912   ;; called "helly+alt.alt".
12913   (let ((entry
12914          (gnus-copy-sequence 
12915           (if (equal (car method) "native") gnus-select-method
12916               (cdr (assoc (car method) gnus-server-alist))))))
12917     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12918     (nconc entry (cdr method))))
12919
12920 (defun gnus-server-get-method (group method)
12921   ;; Input either a server name, and extended server name, or a
12922   ;; select method, and return a select method. 
12923   (cond ((stringp method)
12924          (gnus-server-to-method method))
12925         ((stringp (car method))
12926          (gnus-server-extend-method group method))
12927         (t
12928          (gnus-server-add-address method))))
12929
12930 (defun gnus-server-add-address (method)
12931   (let ((method-name (symbol-name (car method))))
12932     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12933              (not (assq (intern (concat method-name "-address")) method)))
12934         (append method (list (list (intern (concat method-name "-address"))
12935                                    (nth 1 method))))
12936       method)))
12937
12938 (defun gnus-server-equal (s1 s2)
12939   (or (equal s1 s2)
12940       (and (= (length s1) (length s2))
12941            (progn
12942              (while (and s1 (member (car s1) s2))
12943                (setq s1 (cdr s1)))
12944              (null s1)))))
12945
12946 ;;; Interactive server functions.
12947
12948 (defun gnus-server-kill-server (server)
12949   "Kill the server on the current line."
12950   (interactive (list (gnus-server-server-name)))
12951   (or (gnus-server-goto-server server)
12952       (if server (error "No such server: %s" server)
12953         (error "No server on the current line")))
12954   (let ((buffer-read-only nil))
12955     (delete-region (progn (beginning-of-line) (point))
12956                    (progn (forward-line 1) (point))))
12957   (setq gnus-server-killed-servers 
12958         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12959   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12960                                 gnus-server-alist))
12961   (gnus-server-position-cursor))
12962
12963 (defun gnus-server-yank-server ()
12964   "Yank the previously killed server."
12965   (interactive)
12966   (or gnus-server-killed-servers
12967       (error "No killed servers to be yanked"))
12968   (let ((alist gnus-server-alist)
12969         (server (gnus-server-server-name))
12970         (killed (car gnus-server-killed-servers)))
12971     (if (not server) 
12972         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12973       (if (string= server (car (car gnus-server-alist)))
12974           (setq gnus-server-alist (cons killed gnus-server-alist))
12975         (while (and (cdr alist)
12976                     (not (string= server (car (car (cdr alist))))))
12977           (setq alist (cdr alist)))
12978         (setcdr alist (cons killed (cdr alist)))))
12979     (gnus-server-update-server (car killed))
12980     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12981     (gnus-server-position-cursor)))
12982
12983 (defun gnus-server-exit ()
12984   "Return to the group buffer."
12985   (interactive)
12986   (kill-buffer (current-buffer))
12987   (switch-to-buffer gnus-group-buffer))
12988
12989 (defun gnus-server-list-servers ()
12990   "List all available servers."
12991   (interactive)
12992   (let ((cur (gnus-server-server-name)))
12993     (gnus-server-prepare)
12994     (if cur (gnus-server-goto-server cur)
12995       (goto-char (point-max))
12996       (forward-line -1))
12997     (gnus-server-position-cursor)))
12998
12999 (defun gnus-server-copy-server (from to)
13000   (interactive
13001    (list
13002     (or (gnus-server-server-name)
13003         (error "No server on the current line"))
13004     (read-string "Copy to: ")))
13005   (or from (error "No server on current line"))
13006   (or (and to (not (string= to ""))) (error "No name to copy to"))
13007   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13008   (or (assoc from gnus-server-alist) 
13009       (error "%s: no such server" from))
13010   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13011     (setcar to-entry to)
13012     (setcar (nthcdr 2 to-entry) to)
13013     (setq gnus-server-killed-servers 
13014           (cons to-entry gnus-server-killed-servers))
13015     (gnus-server-yank-server)))
13016
13017 (defun gnus-server-add-server (how where)
13018   (interactive 
13019    (list (intern (completing-read "Server method: "
13020                                   gnus-valid-select-methods nil t))
13021          (read-string "Server name: ")))
13022   (setq gnus-server-killed-servers 
13023         (cons (list where how where) gnus-server-killed-servers))
13024   (gnus-server-yank-server))
13025
13026 (defun gnus-server-goto-server (server)
13027   "Jump to a server line."
13028   (interactive
13029    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13030   (let ((to (text-property-any (point-min) (point-max) 
13031                                'gnus-server (intern server))))
13032     (and to
13033          (progn
13034            (goto-char to) 
13035            (gnus-server-position-cursor)))))
13036
13037 (defun gnus-server-edit-server (server)
13038   "Edit the server on the current line."
13039   (interactive (list (gnus-server-server-name)))
13040   (or server
13041       (error "No server on current line"))
13042   (let ((winconf (current-window-configuration)))
13043     (get-buffer-create gnus-server-edit-buffer)
13044     (gnus-configure-windows 'edit-server)
13045     (gnus-add-current-to-buffer-list)
13046     (emacs-lisp-mode)
13047     (make-local-variable 'gnus-prev-winconf)
13048     (setq gnus-prev-winconf winconf)
13049     (use-local-map (copy-keymap (current-local-map)))
13050     (let ((done-func '(lambda () 
13051                         "Exit editing mode and update the information."
13052                         (interactive)
13053                         (gnus-server-edit-server-done 'group))))
13054       (setcar (cdr (nth 4 done-func)) server)
13055       (local-set-key "\C-c\C-c" done-func))
13056     (erase-buffer)
13057     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13058     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13059
13060 (defun gnus-server-edit-server-done (server)
13061   (interactive)
13062   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13063   (goto-char (point-min))
13064   (let ((form (read (current-buffer)))
13065         (winconf gnus-prev-winconf))
13066     (gnus-server-set-info server form)
13067     (kill-buffer (current-buffer))
13068     (and winconf (set-window-configuration winconf))
13069     (set-buffer gnus-server-buffer)
13070     (gnus-server-update-server (gnus-server-server-name))
13071     (gnus-server-position-cursor)))
13072
13073 (defun gnus-server-read-server (server)
13074   "Browse a server."
13075   (interactive (list (gnus-server-server-name)))
13076   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13077
13078 (defun gnus-mouse-pick-server (e)
13079   (interactive "e")
13080   (mouse-set-point e)
13081   (gnus-server-read-server (gnus-server-server-name)))
13082
13083 ;;;
13084 ;;; entry points into gnus-score.el
13085 ;;;
13086
13087 ;;; Finding score files. 
13088
13089 (defvar gnus-global-score-files nil
13090   "*List of global score files and directories.
13091 Set this variable if you want to use people's score files.  One entry
13092 for each score file or each score file directory.  Gnus will decide
13093 by itself what score files are applicable to which group.
13094
13095 Say you want to use the single score file
13096 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13097 score files in the \"/ftp.some-where:/pub/score\" directory.
13098
13099  (setq gnus-global-score-files
13100        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13101          \"/ftp.some-where:/pub/score\"))")
13102
13103 (defun gnus-score-score-files (group)
13104   "Return a list of all possible score files."
13105   ;; Search and set any global score files.
13106   (and gnus-global-score-files 
13107        (or gnus-internal-global-score-files
13108            (gnus-score-search-global-directories gnus-global-score-files)))
13109   ;; Fix the kill-file dir variable.
13110   (setq gnus-kill-files-directory 
13111         (file-name-as-directory
13112          (or gnus-kill-files-directory "~/News/")))
13113   ;; If we can't read it, there are no score files.
13114   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13115       (setq gnus-score-file-list nil)
13116     (if (gnus-use-long-file-name 'not-score)
13117         ;; We want long file names.
13118         (if (or (not gnus-score-file-list)
13119                 (not (car gnus-score-file-list))
13120                 (gnus-file-newer-than gnus-kill-files-directory
13121                                       (car gnus-score-file-list)))
13122               (setq gnus-score-file-list 
13123                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13124                           (nreverse 
13125                            (directory-files 
13126                             gnus-kill-files-directory t 
13127                             (gnus-score-file-regexp))))))
13128       ;; We do not use long file names, so we have to do some
13129       ;; directory traversing.  
13130       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13131             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13132             dir files suffix)
13133         (while suffixes
13134           (setq dir (expand-file-name
13135                      (concat gnus-kill-files-directory
13136                              (gnus-replace-chars-in-string group ?. ?/))))
13137           (setq suffix (car suffixes)
13138                 suffixes (cdr suffixes))
13139           (if (file-exists-p (concat dir "/" suffix))
13140               (setq files (cons (concat dir "/" suffix) files)))
13141           (while (>= (1+ (length dir)) mdir)
13142             (and (file-exists-p (concat dir "/all/" suffix))
13143                  (setq files (cons (concat dir "/all/" suffix) files)))
13144             (string-match "/[^/]*$" dir)
13145             (setq dir (substring dir 0 (match-beginning 0)))))
13146         (setq gnus-score-file-list 
13147               (cons nil (nreverse files)))))
13148     (cdr gnus-score-file-list)))
13149
13150 (defun gnus-score-file-regexp ()
13151   (concat "\\(" gnus-score-file-suffix 
13152           "\\|" gnus-adaptive-file-suffix "\\)$"))
13153         
13154 (defun gnus-score-find-bnews (group)
13155   "Return a list of score files for GROUP.
13156 The score files are those files in the ~/News directory which matches
13157 GROUP using BNews sys file syntax."
13158   (let* ((sfiles (append (gnus-score-score-files group)
13159                          gnus-internal-global-score-files))
13160          (kill-dir (file-name-as-directory 
13161                     (expand-file-name gnus-kill-files-directory)))
13162          (klen (length kill-dir))
13163          ofiles not-match regexp)
13164     (save-excursion
13165       (set-buffer (get-buffer-create "*gnus score files*"))
13166       (buffer-disable-undo (current-buffer))
13167       ;; Go through all score file names and create regexp with them
13168       ;; as the source.  
13169       (while sfiles
13170         (erase-buffer)
13171         (insert (car sfiles))
13172         (goto-char (point-min))
13173         ;; First remove the suffix itself.
13174         (re-search-forward (concat "." (gnus-score-file-regexp)))
13175         (replace-match "" t t) 
13176         (goto-char (point-min))
13177         (if (looking-at (regexp-quote kill-dir))
13178             ;; If the file name was just "SCORE", `klen' is one character
13179             ;; too much.
13180             (delete-char (min (1- (point-max)) klen))
13181           (goto-char (point-max))
13182           (search-backward "/")
13183           (delete-region (1+ (point)) (point-min)))
13184         ;; If short file names were used, we have to translate slashes.
13185         (goto-char (point-min))
13186         (while (search-forward "/" nil t)
13187           (replace-match "." t t))
13188         ;; Translate "all" to ".*".
13189         (while (search-forward "all" nil t)
13190           (replace-match ".*" t t))
13191         (goto-char (point-min))
13192         ;; Deal with "not."s.
13193         (if (looking-at "not.")
13194             (progn
13195               (setq not-match t)
13196               (setq regexp (buffer-substring 5 (point-max))))
13197           (setq regexp (buffer-substring 1 (point-max)))
13198           (setq not-match nil))
13199         ;; Finally - if this resulting regexp matches the group name,
13200         ;; we add this score file to the list of score files
13201         ;; applicable to this group.
13202         (if (or (and not-match
13203                      (not (string-match regexp group)))
13204                 (and (not not-match)
13205                      (string-match regexp group)))
13206             (setq ofiles (cons (car sfiles) ofiles)))
13207         (setq sfiles (cdr sfiles)))
13208       (kill-buffer (current-buffer))
13209       ;; Slight kludge here - the last score file returned should be
13210       ;; the local score file, whether it exists or not. This is so
13211       ;; that any score commands the user enters will go to the right
13212       ;; file, and not end up in some global score file.
13213       (let ((localscore
13214              (expand-file-name
13215               (if (gnus-use-long-file-name 'not-score)
13216                   (concat gnus-kill-files-directory group "." 
13217                           gnus-score-file-suffix)
13218                 (concat gnus-kill-files-directory
13219                         (gnus-replace-chars-in-string group ?. ?/)
13220                         "/" gnus-score-file-suffix)))))
13221         (and (member localscore ofiles)
13222              (delete localscore ofiles))
13223         (setq ofiles (cons localscore ofiles)))
13224       (nreverse ofiles))))
13225
13226 (defun gnus-score-find-single (group)
13227   "Return list containing the score file for GROUP."
13228   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13229         (gnus-score-file-name group)))
13230
13231 (defun gnus-score-find-hierarchical (group)
13232   "Return list of score files for GROUP.
13233 This includes the score file for the group and all its parents."
13234   (let ((all (copy-sequence '(nil)))
13235         (start 0))
13236     (while (string-match "\\." group (1+ start))
13237       (setq start (match-beginning 0))
13238       (setq all (cons (substring group 0 start) all)))
13239     (setq all (cons group all))
13240     (nconc
13241      (mapcar (lambda (newsgroup)
13242                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13243              (setq all (nreverse all)))
13244      (mapcar 'gnus-score-file-name all))))
13245
13246 (defvar gnus-score-file-alist-cache nil)
13247
13248 (defun gnus-score-find-alist (group)
13249   "Return list of score files for GROUP.
13250 The list is determined from the variable gnus-score-file-alist."
13251   (let ((alist gnus-score-file-multiple-match-alist)
13252         score-files)
13253     ;; if this group has been seen before, return the cached entry
13254     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13255         (cdr score-files)       ; ensures caching of groups with no matches
13256       ;; handle the multiple match alist
13257       (while alist
13258         (and (string-match (car (car alist)) group)
13259              (setq score-files
13260                    (nconc score-files (cdr (car alist)))))
13261         (setq alist (cdr alist)))
13262       (setq alist gnus-score-file-single-match-alist)
13263       ;; handle the single match alist
13264       (catch 'done
13265         (while alist
13266           (and (string-match (car (car alist)) group)
13267                ;; progn used just in case ("regexp") has no files
13268                ;; and score-files is still nil. -sj
13269                ;; this can be construed as a "stop searching here" feature :>
13270                ;; and used to simplify regexps in the single-alist 
13271                (progn
13272                  (setq score-files
13273                        (append score-files (cdr (car alist))))
13274                  (throw 'done nil)))
13275           (setq alist (cdr alist))))
13276       ;; cache the score files
13277       (setq gnus-score-file-alist-cache
13278             (cons (cons group score-files) gnus-score-file-alist-cache))
13279       score-files)))
13280
13281
13282 (defun gnus-possibly-score-headers (&optional trace)
13283   (let ((func gnus-score-find-score-files-function)
13284         score-files)
13285     (and func (not (listp func))
13286          (setq func (list func)))
13287     ;; Go through all the functions for finding score files (or actual
13288     ;; scores) and add them to a list.
13289     (setq score-files (copy-sequence
13290                        (gnus-score-find-alist gnus-newsgroup-name)))
13291     (while func
13292       (and (symbolp (car func))
13293            (fboundp (car func))
13294            (setq score-files 
13295                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13296       (setq func (cdr func)))
13297     (if score-files (gnus-score-headers score-files trace))))
13298
13299 (defun gnus-score-file-name (newsgroup &optional suffix)
13300   "Return the name of a score file for NEWSGROUP."
13301   (let ((suffix (or suffix gnus-score-file-suffix)))
13302     (cond  ((or (null newsgroup)
13303                 (string-equal newsgroup ""))
13304             ;; The global score file is placed at top of the directory.
13305             (expand-file-name 
13306              suffix (or gnus-kill-files-directory "~/News")))
13307            ((gnus-use-long-file-name 'not-score)
13308             ;; Append ".SCORE" to newsgroup name.
13309             (expand-file-name (concat newsgroup "." suffix)
13310                               (or gnus-kill-files-directory "~/News")))
13311            (t
13312             ;; Place "SCORE" under the hierarchical directory.
13313             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13314                                       "/" suffix)
13315                               (or gnus-kill-files-directory "~/News"))))))
13316
13317 (defun gnus-score-search-global-directories (files)
13318   "Scan all global score directories for score files."
13319   ;; Set the variable `gnus-internal-global-score-files' to all
13320   ;; available global score files.
13321   (interactive (list gnus-global-score-files))
13322   (let (out)
13323     (while files
13324       (if (string-match "/$" (car files))
13325           (setq out (nconc (directory-files 
13326                             (car files) t
13327                             (concat (gnus-score-file-regexp) "$"))))
13328         (setq out (cons (car files) out)))
13329       (setq files (cdr files)))
13330     (setq gnus-internal-global-score-files out)))
13331
13332 ;; Allow redefinition of Gnus functions.
13333
13334 (gnus-ems-redefine)
13335
13336 (provide 'gnus)
13337
13338 ;;; gnus.el ends here