*** 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 ;;; Code:
27
28 (eval '(run-hooks 'gnus-load-hook))
29
30 (require 'mail-utils)
31 (require 'timezone)
32 (require 'nnheader)
33
34 (eval-when-compile (require 'cl))
35
36 ;; Site dependent variables.  These variables should be defined in
37 ;; paths.el.
38
39 (defvar gnus-default-nntp-server nil
40   "Specify a default NNTP server.
41 This variable should be defined in paths.el, and should never be set
42 by the user.
43 If you want to change servers, you should use `gnus-select-method'.
44 See the documentation to that variable.")
45
46 (defconst gnus-backup-default-subscribed-newsgroups 
47   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
48   "Default default new newsgroups the first time Gnus is run.
49 Should be set in paths.el, and shouldn't be touched by the user.")
50
51 (defvar gnus-local-domain nil
52   "Local domain name without a host name.
53 The DOMAINNAME environment variable is used instead if it is defined.
54 If the `system-name' function returns the full Internet name, there is
55 no need to set this variable.")
56
57 (defvar gnus-local-organization nil
58   "String with a description of what organization (if any) the user belongs to.
59 The ORGANIZATION environment variable is used instead if it is defined.
60 If this variable contains a function, this function will be called
61 with the current newsgroup name as the argument.  The function should
62 return a string.
63
64 In any case, if the string (either in the variable, in the environment
65 variable, or returned by the function) is a file name, the contents of
66 this file will be used as the organization.")
67
68 (defvar gnus-use-generic-from nil
69   "If nil, the full host name will be the system name prepended to the domain name.
70 If this is a string, the full host name will be this string.
71 If this is non-nil, non-string, the domain name will be used as the
72 full host name.")
73
74 (defvar gnus-use-generic-path nil
75   "If nil, use the NNTP server name in the Path header.
76 If stringp, use this; if non-nil, use no host name (user name only).")
77
78
79 ;; Customization variables
80
81 ;; Don't touch this variable.
82 (defvar gnus-nntp-service "nntp"
83   "*NNTP service name (\"nntp\" or 119).
84 This is an obsolete variable, which is scarcely used.  If you use an
85 nntp server for your newsgroup and want to change the port number
86 used to 899, you would say something along these lines:
87
88  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
89
90 (defvar gnus-nntpserver-file "/etc/nntpserver"
91   "*A file with only the name of the nntp server in it.")
92
93 ;; This function is used to check both the environment variable
94 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
95 ;; an nntp server name default.
96 (defun gnus-getenv-nntpserver ()
97   (or (getenv "NNTPSERVER")
98       (and (file-readable-p gnus-nntpserver-file)
99            (save-excursion
100              (set-buffer (get-buffer-create " *gnus nntp*"))
101              (buffer-disable-undo (current-buffer))
102              (insert-file-contents gnus-nntpserver-file)
103              (let ((name (buffer-string)))
104                (prog1
105                    (if (string-match "^[ \t\n]*$" name)
106                        nil
107                      name)
108                  (kill-buffer (current-buffer))))))))
109                  
110 (defvar gnus-select-method 
111   (nconc
112    (list 'nntp (or (condition-case ()
113                        (gnus-getenv-nntpserver)
114                      (error nil))
115                    (if (and gnus-default-nntp-server
116                             (not (string= gnus-default-nntp-server "")))
117                        gnus-default-nntp-server)
118                    (system-name)))
119    (if (or (null gnus-nntp-service)
120            (equal gnus-nntp-service "nntp"))
121        nil 
122      (list gnus-nntp-service)))
123   "*Default method for selecting a newsgroup.
124 This variable should be a list, where the first element is how the
125 news is to be fetched, the second is the address. 
126
127 For instance, if you want to get your news via NNTP from
128 \"flab.flab.edu\", you could say:
129
130 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
131
132 If you want to use your local spool, say:
133
134 (setq gnus-select-method (list 'nnspool (system-name)))
135
136 If you use this variable, you must set `gnus-nntp-server' to nil.
137
138 There is a lot more to know about select methods and virtual servers -
139 see the manual for details.")
140
141 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
142 (defvar gnus-post-method nil
143   "*Preferred method for posting USENET news.
144 If this variable is nil, Gnus will use the current method to decide
145 which method to use when posting.  If it is non-nil, it will override
146 the current method.  This method will not be used in mail groups and
147 the like, only in \"real\" newsgroups.
148
149 The value must be a valid method as discussed in the documentation of
150 `gnus-select-method'.")
151
152 (defvar gnus-refer-article-method nil
153   "*Preferred method for fetching an article by Message-ID.
154 If you are reading news from the local spool (with nnspool), fetching
155 articles by Message-ID is painfully slow.  By setting this method to an
156 nntp method, you might get acceptable results.
157
158 The value of this variable must be a valid select method as discussed
159 in the documentation of `gnus-select-method'")
160
161 (defvar gnus-secondary-select-methods nil
162   "*A list of secondary methods that will be used for reading news.
163 This is a list where each element is a complete select method (see
164 `gnus-select-method').  
165
166 If, for instance, you want to read your mail with the nnml backend,
167 you could set this variable:
168
169 (setq gnus-secondary-select-methods '((nnml \"\")))")
170
171 (defvar gnus-secondary-servers nil
172   "*List of NNTP servers that the user can choose between interactively.
173 To make Gnus query you for a server, you have to give `gnus' a
174 non-numeric prefix - `C-u M-x gnus', in short.")
175
176 (defvar gnus-nntp-server nil
177   "*The name of the host running the NNTP server.
178 This variable is semi-obsolete.  Use the `gnus-select-method'
179 variable instead.")
180
181 (defvar gnus-startup-file "~/.newsrc"
182   "*Your `.newsrc' file.
183 `.newsrc-SERVER' will be used instead if that exists.")
184
185 (defvar gnus-init-file "~/.gnus"
186   "*Your Gnus elisp startup file.
187 If a file with the .el or .elc suffixes exist, it will be read
188 instead.") 
189
190 (defvar gnus-group-faq-directory
191   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
192 ;    "/ftp@ftp.uu.net:/usenet/news.answers/"
193     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
194     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
195     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
196 ;    "/ftp@ftp.Germany.EU.net:/pub/newsarchive/news.answers/"
197     "/ftp@ftp.sunet.se:/pub/usenet/"
198     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
199     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
200     "/ftp@ftp.hk.super.net:/mirror/faqs/")
201   "*Directory where the group FAQs are stored.
202 This will most commonly be on a remote machine, and the file will be
203 fetched by ange-ftp.
204
205 This variable can also be a list of directories.  In that case, the
206 first element in the list will be used by default, and the others will
207 be used as backup sites.
208
209 Note that Gnus uses an aol machine as the default directory.  If this
210 feels fundamentally unclean, just think of it as a way to finally get
211 something of value back from them.
212
213 If the default site is too slow, try one of these:
214
215    North America: mirrors.aol.com                /pub/rtfm/usenet
216                   ftp.seas.gwu.edu               /pub/rtfm
217                   rtfm.mit.edu                   /pub/usenet/news.answers
218    Europe:        ftp.uni-paderborn.de           /pub/FAQ
219                   ftp.sunet.se                   /pub/usenet
220    Asia:          nctuccca.edu.tw                /USENET/FAQ
221                   hwarang.postech.ac.kr          /pub/usenet/news.answers
222                   ftp.hk.super.net               /mirror/faqs")
223
224 (defvar gnus-group-archive-directory
225   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/" 
226   "*The address of the (ding) archives.")
227
228 (defvar gnus-group-recent-archive-directory
229   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
230   "*The address of the most recent (ding) articles.")
231
232 (defvar gnus-default-subscribed-newsgroups nil
233   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
234 It should be a list of strings.
235 If it is `t', Gnus will not do anything special the first time it is
236 started; it'll just use the normal newsgroups subscription methods.")
237
238 (defvar gnus-use-cross-reference t
239   "*Non-nil means that cross referenced articles will be marked as read.
240 If nil, ignore cross references.  If t, mark articles as read in
241 subscribed newsgroups.  If neither t nor nil, mark as read in all
242 newsgroups.") 
243
244 (defvar gnus-single-article-buffer t
245   "*If non-nil, display all articles in the same buffer.
246 If nil, each group will get its own article buffer.")
247
248 (defvar gnus-use-dribble-file t
249   "*Non-nil means that Gnus will use a dribble file to store user updates.
250 If Emacs should crash without saving the .newsrc files, complete
251 information can be restored from the dribble file.")
252
253 (defvar gnus-dribble-directory nil
254   "*The directory where dribble files will be saved.
255 If this variable is nil, the directory where the .newsrc files are
256 saved will be used.")
257
258 (defvar gnus-asynchronous nil
259   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
260
261 (defvar gnus-kill-summary-on-exit t
262   "*If non-nil, kill the summary buffer when you exit from it.
263 If nil, the summary will become a \"*Dead Summary*\" buffer, and
264 it will be killed sometime later.")
265
266 (defvar gnus-large-newsgroup 200
267   "*The number of articles which indicates a large newsgroup.
268 If the number of articles in a newsgroup is greater than this value,
269 confirmation is required for selecting the newsgroup.")
270
271 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
272 (defvar gnus-no-groups-message "No news is horrible news"
273   "*Message displayed by Gnus when no groups are available.")
274
275 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
276   "*Non-nil means that the default name of a file to save articles in is the group name.
277 If it's nil, the directory form of the group name is used instead.
278
279 If this variable is a list, and the list contains the element
280 `not-score', long file names will not be used for score files; if it
281 contains the element `not-save', long file names will not be used for
282 saving; and if it contains the element `not-kill', long file names
283 will not be used for kill files.")
284
285 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
286   "*Name of the directory articles will be saved in (default \"~/News\").
287 Initialized from the SAVEDIR environment variable.")
288
289 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
290   "*Name of the directory where kill files will be stored (default \"~/News\").
291 Initialized from the SAVEDIR environment variable.")
292
293 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
294   "*A function to save articles in your favorite format.
295 The function must be interactively callable (in other words, it must
296 be an Emacs command).
297
298 Gnus provides the following functions:
299
300 * gnus-summary-save-in-rmail (Rmail format)
301 * gnus-summary-save-in-mail (Unix mail format)
302 * gnus-summary-save-in-folder (MH folder)
303 * gnus-summary-save-in-file (article format).
304 * gnus-summary-save-in-vm (use VM's folder format).")
305
306 (defvar gnus-prompt-before-saving 'always
307   "*This variable says how much prompting is to be done when saving articles.
308 If it is nil, no prompting will be done, and the articles will be
309 saved to the default files.  If this variable is `always', each and
310 every article that is saved will be preceded by a prompt, even when
311 saving large batches of articles.  If this variable is neither nil not
312 `always', there the user will be prompted once for a file name for
313 each invocation of the saving commands.")
314
315 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
316   "*A function generating a file name to save articles in Rmail format.
317 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
318
319 (defvar gnus-mail-save-name (function gnus-plain-save-name)
320   "*A function generating a file name to save articles in Unix mail format.
321 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
322
323 (defvar gnus-folder-save-name (function gnus-folder-save-name)
324   "*A function generating a file name to save articles in MH folder.
325 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
326
327 (defvar gnus-file-save-name (function gnus-numeric-save-name)
328   "*A function generating a file name to save articles in article format.
329 The function is called with NEWSGROUP, HEADERS, and optional
330 LAST-FILE.")
331
332 (defvar gnus-split-methods 
333   '((gnus-article-archive-name))
334   "*Variable used to suggest where articles are to be saved.
335 For instance, if you would like to save articles related to Gnus in
336 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
337 you could set this variable to something like:
338
339  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
340    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
341
342 This variable is an alist where the where the key is the match and the
343 value is a list of possible files to save in if the match is non-nil.
344
345 If the match is a string, it is used as a regexp match on the
346 article.  If the match is a symbol, that symbol will be funcalled  
347 from the buffer of the article to be saved with the newsgroup as the
348 parameter.  If it is a list, it will be evaled in the same buffer.
349
350 If this form or function returns a string, this string will be used as
351 a possible file name; and if it returns a non-nil list, that list will
352 be used as possible file names.")
353
354 (defvar gnus-move-split-methods nil
355   "*Variable used to suggest where articles are to be moved to.
356 It uses the same syntax as the `gnus-split-methods' variable.")
357
358 (defvar gnus-save-score nil
359   "*If non-nil, save group scoring info.")
360
361 (defvar gnus-use-adaptive-scoring nil
362   "*If non-nil, use some adaptive scoring scheme.")
363
364 (defvar gnus-use-cache nil
365   "*If nil, Gnus will ignore the article cache.
366 If `passive', it will allow entering (and reading) articles
367 explicitly entered into the cache.  If anything else, use the
368 cache to the full extent of the law.")
369
370 (defvar gnus-keep-backlog nil
371   "*If non-nil, Gnus will keep read articles for later re-retrieval.
372 If it is a number N, then Gnus will only keep the last N articles
373 read.  If it is neither nil nor a number, Gnus will keep all read
374 articles.  This is not a good idea.")
375
376 (defvar gnus-use-nocem nil
377   "*If non-nil, Gnus will read NoCeM cancel messages.")
378
379 (defvar gnus-use-demon nil
380   "If non-nil, Gnus might use some demons.")
381
382 (defvar gnus-use-scoring t
383   "*If non-nil, enable scoring.")
384
385 (defvar gnus-use-picon nil
386   "*If non-nil, display picons.")
387
388 (defvar gnus-fetch-old-headers nil
389   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
390 If an unread article in the group refers to an older, already read (or
391 just marked as read) article, the old article will not normally be
392 displayed in the Summary buffer.  If this variable is non-nil, Gnus
393 will attempt to grab the headers to the old articles, and thereby
394 build complete threads.  If it has the value `some', only enough
395 headers to connect otherwise loose threads will be displayed.
396 This variable can also be a number.  In that case, no more than that
397 number of old headers will be fetched. 
398
399 The server has to support NOV for any of this to work.")
400
401 ;see gnus-cus.el
402 ;(defvar gnus-visual t
403 ;  "*If non-nil, will do various highlighting.
404 ;If nil, no mouse highlights (or any other highlights) will be
405 ;performed.  This might speed up Gnus some when generating large group
406 ;and summary buffers.")
407
408 (defvar gnus-novice-user t
409   "*Non-nil means that you are a usenet novice.
410 If non-nil, verbose messages may be displayed and confirmations may be
411 required.")
412
413 (defvar gnus-expert-user nil
414   "*Non-nil means that you will never be asked for confirmation about anything.
415 And that means *anything*.")
416
417 (defvar gnus-verbose 7
418   "*Integer that says how verbose Gnus should be.
419 The higher the number, the more messages Gnus will flash to say what
420 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
421 display most important messages; and at ten, Gnus will keep on
422 jabbering all the time.")
423
424 (defvar gnus-keep-same-level nil
425   "*Non-nil means that the next newsgroup after the current will be on the same level.
426 When you type, for instance, `n' after reading the last article in the
427 current newsgroup, you will go to the next newsgroup.  If this variable
428 is nil, the next newsgroup will be the next from the group
429 buffer. 
430 If this variable is non-nil, Gnus will either put you in the
431 next newsgroup with the same level, or, if no such newsgroup is
432 available, the next newsgroup with the lowest possible level higher
433 than the current level.
434 If this variable is `best', Gnus will make the next newsgroup the one
435 with the best level.")
436
437 (defvar gnus-summary-make-false-root 'adopt
438   "*nil means that Gnus won't gather loose threads.
439 If the root of a thread has expired or been read in a previous
440 session, the information necessary to build a complete thread has been
441 lost.  Instead of having many small sub-threads from this original thread
442 scattered all over the summary buffer, Gnus can gather them. 
443
444 If non-nil, Gnus will try to gather all loose sub-threads from an
445 original thread into one large thread.
446
447 If this variable is non-nil, it should be one of `none', `adopt',
448 `dummy' or `empty'.
449
450 If this variable is `none', Gnus will not make a false root, but just
451 present the sub-threads after another.
452 If this variable is `dummy', Gnus will create a dummy root that will
453 have all the sub-threads as children.
454 If this variable is `adopt', Gnus will make one of the \"children\"
455 the parent and mark all the step-children as such.
456 If this variable is `empty', the \"children\" are printed with empty
457 subject fields.  (Or rather, they will be printed with a string
458 given by the `gnus-summary-same-subject' variable.)")
459
460 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
461   "*A regexp to match subjects to be excluded from loose thread gathering.
462 As loose thread gathering is done on subjects only, that means that
463 there can be many false gatherings performed.  By rooting out certain
464 common subjects, gathering might become saner.")
465
466 (defvar gnus-summary-gather-subject-limit nil
467   "*Maximum length of subject comparisons when gathering loose threads.
468 Use nil to compare full subjects.  Setting this variable to a low
469 number will help gather threads that have been corrupted by
470 newsreaders chopping off subject lines, but it might also mean that
471 unrelated articles that have subject that happen to begin with the
472 same few characters will be incorrectly gathered.
473
474 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
475 comparing subjects.")
476
477 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
478 (defvar gnus-summary-same-subject ""
479   "*String indicating that the current article has the same subject as the previous.
480 This variable will only be used if the value of
481 `gnus-summary-make-false-root' is `empty'.")
482
483 (defvar gnus-summary-goto-unread t
484   "*If non-nil, marking commands will go to the next unread article.")
485
486 (defvar gnus-group-goto-unread t
487   "*If non-nil, movement commands will go to the next unread and subscribed group.")
488
489 (defvar gnus-check-new-newsgroups t
490   "*Non-nil means that Gnus will add new newsgroups at startup.
491 If this variable is `ask-server', Gnus will ask the server for new
492 groups since the last time it checked.  This means that the killed list
493 is no longer necessary, so you could set `gnus-save-killed-list' to
494 nil. 
495
496 A variant is to have this variable be a list of select methods.  Gnus
497 will then use the `ask-server' method on all these select methods to
498 query for new groups from all those servers.
499
500 Eg.
501   (setq gnus-check-new-newsgroups 
502         '((nntp \"some.server\") (nntp \"other.server\")))
503
504 If this variable is nil, then you have to tell Gnus explicitly to
505 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
506
507 (defvar gnus-check-bogus-newsgroups nil
508   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
509 If this variable is nil, then you have to tell Gnus explicitly to
510 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
511
512 (defvar gnus-read-active-file t
513   "*Non-nil means that Gnus will read the entire active file at startup.
514 If this variable is nil, Gnus will only know about the groups in your
515 `.newsrc' file.
516
517 If this variable is `some', Gnus will try to only read the relevant
518 parts of the active file from the server.  Not all servers support
519 this, and it might be quite slow with other servers, but this should
520 generally be faster than both the t and nil value.
521
522 If you set this variable to nil or `some', you probably still want to
523 be told about new newsgroups that arrive.  To do that, set
524 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
525 properly with all servers.")
526
527 (defvar gnus-level-subscribed 5
528   "*Groups with levels less than or equal to this variable are subscribed.")
529
530 (defvar gnus-level-unsubscribed 7
531   "*Groups with levels less than or equal to this variable are unsubscribed.
532 Groups with levels less than `gnus-level-subscribed', which should be
533 less than this variable, are subscribed.")
534
535 (defvar gnus-level-zombie 8
536   "*Groups with this level are zombie groups.")
537
538 (defvar gnus-level-killed 9
539   "*Groups with this level are killed.")
540
541 (defvar gnus-level-default-subscribed 3
542   "*New subscribed groups will be subscribed at this level.")
543
544 (defvar gnus-level-default-unsubscribed 6
545   "*New unsubscribed groups will be unsubscribed at this level.")
546
547 (defvar gnus-activate-level (1+ gnus-level-subscribed)
548   "*Groups higher than this level won't be activated on startup.
549 Setting this variable to something log might save lots of time when
550 you have many groups that you aren't interested in.")
551
552 (defvar gnus-activate-foreign-newsgroups 4
553   "*If nil, Gnus will not check foreign newsgroups at startup.
554 If it is non-nil, it should be a number between one and nine.  Foreign
555 newsgroups that have a level lower or equal to this number will be
556 activated on startup.  For instance, if you want to active all
557 subscribed newsgroups, but not the rest, you'd set this variable to 
558 `gnus-level-subscribed'.
559
560 If you subscribe to lots of newsgroups from different servers, startup
561 might take a while.  By setting this variable to nil, you'll save time,
562 but you won't be told how many unread articles there are in the
563 groups.")
564
565 (defvar gnus-save-newsrc-file t
566   "*Non-nil means that Gnus will save the `.newsrc' file.
567 Gnus always saves its own startup file, which is called
568 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
569 be readily understood by other newsreaders.  If you don't plan on
570 using other newsreaders, set this variable to nil to save some time on
571 exit.")
572
573 (defvar gnus-save-killed-list t
574   "*If non-nil, save the list of killed groups to the startup file.
575 If you set this variable to nil, you'll save both time (when starting
576 and quitting) and space (both memory and disk), but it will also mean
577 that Gnus has no record of which groups are new and which are old, so
578 the automatic new newsgroups subscription methods become meaningless.
579
580 You should always set `gnus-check-new-newsgroups' to `ask-server' or
581 nil if you set this variable to nil.")
582
583 (defvar gnus-interactive-catchup t
584   "*If non-nil, require your confirmation when catching up a group.")
585
586 (defvar gnus-interactive-post t
587   "*If non-nil, group name will be asked for when posting.")
588
589 (defvar gnus-interactive-exit t
590   "*If non-nil, require your confirmation when exiting Gnus.")
591
592 (defvar gnus-kill-killed t
593   "*If non-nil, Gnus will apply kill files to already killed articles.
594 If it is nil, Gnus will never apply kill files to articles that have
595 already been through the scoring process, which might very well save lots
596 of time.")
597
598 (defvar gnus-extract-address-components 'gnus-extract-address-components
599   "*Function for extracting address components from a From header.
600 Two pre-defined function exist: `gnus-extract-address-components',
601 which is the default, quite fast, and too simplistic solution, and
602 `mail-extract-address-components', which works much better, but is
603 slower.")
604
605 (defvar gnus-summary-default-score 0
606   "*Default article score level.
607 If this variable is nil, scoring will be disabled.")
608
609 (defvar gnus-summary-zcore-fuzz 0
610   "*Fuzziness factor for the zcore in the summary buffer.
611 Articles with scores closer than this to `gnus-summary-default-score'
612 will not be marked.")
613
614 (defvar gnus-simplify-subject-fuzzy-regexp nil
615   "*Strings to be removed when doing fuzzy matches.
616 This can either be a regular expression or list of regular expressions
617 that will be removed from subject strings if fuzzy subject
618 simplification is selected.")
619
620 (defvar gnus-permanently-visible-groups nil
621   "*Regexp to match groups that should always be listed in the group buffer.
622 This means that they will still be listed when there are no unread
623 articles in the groups.")
624
625 (defvar gnus-group-default-list-level gnus-level-subscribed
626   "*Default listing level. 
627 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
628
629 (defvar gnus-group-use-permanent-levels nil
630   "*If non-nil, once you set a level, Gnus will use this level.")
631
632 (defvar gnus-group-list-inactive-groups t
633   "*If non-nil, inactive groups will be listed.")
634
635 (defvar gnus-show-mime nil
636   "*If non-nil, do mime processing of articles.
637 The articles will simply be fed to the function given by
638 `gnus-show-mime-method'.")
639
640 (defvar gnus-strict-mime t
641   "*If nil, decode MIME header even if there is not Mime-Version field.")
642  
643 (defvar gnus-show-mime-method 'metamail-buffer
644   "*Function to process a MIME message.
645 The function is called from the article buffer.")
646
647 (defvar gnus-decode-encoded-word-method (lambda ())
648   "*Function to decode a MIME encoded-words.
649 The function is called from the article buffer.")
650  
651 (defvar gnus-show-threads t
652   "*If non-nil, display threads in summary mode.")
653
654 (defvar gnus-thread-hide-subtree nil
655   "*If non-nil, hide all threads initially.
656 If threads are hidden, you have to run the command
657 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
658 to expose hidden threads.")
659
660 (defvar gnus-thread-hide-killed t
661   "*If non-nil, hide killed threads automatically.")
662
663 (defvar gnus-thread-ignore-subject nil
664   "*If non-nil, ignore subjects and do all threading based on the Reference header.
665 If nil, which is the default, articles that have different subjects
666 from their parents will start separate threads.")
667
668 (defvar gnus-thread-operation-ignore-subject t
669   "*If non-nil, subjects will be ignored when doing thread commands.
670 This affects commands like `gnus-summary-kill-thread' and
671 `gnus-summary-lower-thread'.  
672
673 If this variable is nil, articles in the same thread with different
674 subjects will not be included in the operation in question.  If this
675 variable is `fuzzy', only articles that have subjects that are fuzzily
676 equal will be included.")
677
678 (defvar gnus-thread-indent-level 4
679   "*Number that says how much each sub-thread should be indented.")
680
681 (defvar gnus-ignored-newsgroups 
682   (purecopy (mapconcat 'identity
683                        '("^to\\."       ; not "real" groups
684                          "^[0-9. \t]+ " ; all digits in name
685                          "[][\"#'()]"   ; bogus characters
686                          )
687                        "\\|"))
688   "*A regexp to match uninteresting newsgroups in the active file.
689 Any lines in the active file matching this regular expression are
690 removed from the newsgroup list before anything else is done to it,
691 thus making them effectively non-existent.")
692
693 (defvar gnus-ignored-headers
694   "^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:"
695   "*All headers that match this regexp will be hidden.
696 This variable can also be a list of regexps of headers to be ignored.
697 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
698
699 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Resent-"
700   "*All headers that do not match this regexp will be hidden.
701 This variable can also be a list of regexp of headers to remain visible.
702 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
703
704 (defvar gnus-sorted-header-list
705   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
706     "^Cc:" "^Date:" "^Organization:")
707   "*This variable is a list of regular expressions.
708 If it is non-nil, headers that match the regular expressions will
709 be placed first in the article buffer in the sequence specified by
710 this list.")
711
712 (defvar gnus-show-all-headers nil
713   "*If non-nil, don't hide any headers.")
714
715 (defvar gnus-save-all-headers t
716   "*If non-nil, don't remove any headers before saving.")
717
718 (defvar gnus-saved-headers gnus-visible-headers
719   "*Headers to keep if `gnus-save-all-headers' is nil.
720 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
721 If that variable is nil, however, all headers that match this regexp
722 will be kept while the rest will be deleted before saving.")
723
724 (defvar gnus-inhibit-startup-message nil
725   "*If non-nil, the startup message will not be displayed.")
726
727 (defvar gnus-signature-separator "^-- *$"
728   "Regexp matching signature separator.")
729
730 (defvar gnus-auto-extend-newsgroup t
731   "*If non-nil, extend newsgroup forward and backward when requested.")
732
733 (defvar gnus-auto-select-first t
734   "*If nil, don't select the first unread article when entering a group.
735 If this variable is `best', select the highest-scored unread article
736 in the group.  If neither nil nor `best', select the first unread
737 article.
738
739 If you want to prevent automatic selection of the first unread article
740 in some newsgroups, set the variable to nil in
741 `gnus-select-group-hook'.") 
742
743 (defvar gnus-auto-select-next t
744   "*If non-nil, offer to go to the next group from the end of the previous.
745 If the value is t and the next newsgroup is empty, Gnus will exit
746 summary mode and go back to group mode.  If the value is neither nil
747 nor t, Gnus will select the following unread newsgroup.  In
748 particular, if the value is the symbol `quietly', the next unread
749 newsgroup will be selected without any confirmation, and if it is
750 `almost-quietly', the next group will be selected without any
751 confirmation if you are located on the last article in the group.")
752
753 (defvar gnus-auto-select-same nil
754   "*If non-nil, select the next article with the same subject.")
755
756 (defvar gnus-summary-check-current nil
757   "*If non-nil, consider the current article when moving.
758 The \"unread\" movement commands will stay on the same line if the
759 current article is unread.")
760
761 (defvar gnus-auto-center-summary t
762   "*If non-nil, always center the current summary buffer.")
763
764 (defvar gnus-break-pages t
765   "*If non-nil, do page breaking on articles.
766 The page delimiter is specified by the `gnus-page-delimiter'
767 variable.")
768
769 (defvar gnus-page-delimiter "^\^L"
770   "*Regexp describing what to use as article page delimiters.
771 The default value is \"^\^L\", which is a form linefeed at the
772 beginning of a line.")
773
774 (defvar gnus-use-full-window t
775   "*If non-nil, use the entire Emacs screen.")
776
777 (defvar gnus-window-configuration nil
778   "Obsolete variable.  See `gnus-buffer-configuration'.")
779
780 (defvar gnus-buffer-configuration
781   '((group
782      (vertical 1.0 
783                (group 1.0 point) 
784                (if gnus-carpal (group-carpal 4))))
785     (summary
786      (vertical 1.0
787                (summary 1.0 point)
788                (if gnus-carpal (summary-carpal 4))))
789     (article
790      (if gnus-use-picon
791          '(frame 1.0
792                  (vertical 1.0
793                            (summary 0.25 point)
794                            (if gnus-carpal (summary-carpal 4)) 
795                            (article 1.0))
796                  (vertical 1.0
797                            (picon 1.0)))
798        '(vertical 1.0
799                  (summary 0.25 point)
800                  (if gnus-carpal (summary-carpal 4)) 
801                  (article 1.0))))
802     (server
803      (vertical 1.0
804                (server 1.0 point)
805                (if gnus-carpal (server-carpal 2))))
806     (browse
807      (vertical 1.0
808                (browse 1.0 point)
809                (if gnus-carpal (browse-carpal 2))))
810     (group-mail
811      (vertical 1.0
812                (mail 1.0 point)))
813     (summary-mail
814      (vertical 1.0
815                (mail 1.0 point)))
816     (summary-reply
817      (vertical 1.0
818                (article 0.5)
819                (mail 1.0 point)))
820     (info
821      (vertical 1.0
822                (nil 1.0 point)))
823     (summary-faq
824      (vertical 1.0
825                (summary 0.25)
826                (faq 1.0 point)))
827     (edit-group
828      (vertical 1.0
829                (group 0.5)
830                (edit-group 1.0 point)))
831     (edit-server
832      (vertical 1.0
833                (server 0.5)
834                (edit-server 1.0 point)))
835     (edit-score
836      (vertical 1.0
837                (summary 0.25)
838                (edit-score 1.0 point)))
839     (post
840      (vertical 1.0
841                (post 1.0 point)))
842     (reply
843      (vertical 1.0
844                (article 0.5)
845                (mail 1.0 point)))
846     (mail-forward
847      (vertical 1.0
848                (mail 1.0 point)))
849     (post-forward
850      (vertical 1.0
851                (post 1.0 point)))
852     (reply-yank
853      (vertical 1.0
854                (mail 1.0 point)))
855     (mail-bounce
856      (vertical 1.0
857                (article 0.5)
858                (mail 1.0 point)))
859     (draft
860      (vertical 1.0
861                (draft 1.0 point)))
862     (pipe
863      (vertical 1.0
864                (summary 0.25 point) 
865                (if gnus-carpal (summary-carpal 4)) 
866                ("*Shell Command Output*" 1.0)))
867     (followup
868      (vertical 1.0
869                (article 0.5)
870                (post 1.0 point)))
871     (followup-yank
872      (vertical 1.0
873                (post 1.0 point))))
874   "Window configuration for all possible Gnus buffers.
875 This variable is a list of lists.  Each of these lists has a NAME and
876 a RULE.  The NAMEs are commonsense names like `group', which names a
877 rule used when displaying the group buffer; `summary', which names a
878 rule for what happens when you enter a group and do not display an
879 article buffer; and so on.  See the value of this variable for a
880 complete list of NAMEs.
881
882 Each RULE is a list of vectors.  The first element in this vector is
883 the name of the buffer to be displayed; the second element is the
884 percentage of the screen this buffer is to occupy (a number in the
885 0.0-0.99 range); the optional third element is `point', which should
886 be present to denote which buffer point is to go to after making this
887 buffer configuration.")
888
889 (defvar gnus-window-to-buffer
890   '((group . gnus-group-buffer)
891     (summary . gnus-summary-buffer)
892     (article . gnus-article-buffer)
893     (server . gnus-server-buffer)
894     (browse . "*Gnus Browse Server*")
895     (edit-group . gnus-group-edit-buffer)
896     (edit-server . gnus-server-edit-buffer)
897     (group-carpal . gnus-carpal-group-buffer)
898     (summary-carpal . gnus-carpal-summary-buffer)
899     (server-carpal . gnus-carpal-server-buffer)
900     (browse-carpal . gnus-carpal-browse-buffer)
901     (edit-score . gnus-score-edit-buffer)
902     (mail . gnus-mail-buffer)
903     (post . gnus-post-news-buffer)
904     (faq . gnus-faq-buffer)
905     (picon . gnus-picon-buffer)
906     (draft . gnus-draft-buffer))
907   "Mapping from short symbols to buffer names or buffer variables.")
908
909 (defvar gnus-carpal nil
910   "*If non-nil, display clickable icons.")
911
912 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
913   "*Function called with a group name when new group is detected.
914 A few pre-made functions are supplied: `gnus-subscribe-randomly'
915 inserts new groups at the beginning of the list of groups;
916 `gnus-subscribe-alphabetically' inserts new groups in strict
917 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
918 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
919 for your decision; `gnus-subscribe-killed' kills all new groups.")
920
921 ;; Suggested by a bug report by Hallvard B Furuseth.
922 ;; <h.b.furuseth@usit.uio.no>. 
923 (defvar gnus-subscribe-options-newsgroup-method
924   (function gnus-subscribe-alphabetically)
925   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
926 If, for instance, you want to subscribe to all newsgroups in the
927 \"no\" and \"alt\" hierarchies, you'd put the following in your
928 .newsrc file:
929
930 options -n no.all alt.all
931
932 Gnus will the subscribe all new newsgroups in these hierarchies with
933 the subscription method in this variable.")
934
935 (defvar gnus-subscribe-hierarchical-interactive nil
936   "*If non-nil, Gnus will offer to subscribe hierarchically.
937 When a new hierarchy appears, Gnus will ask the user:
938
939 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
940
941 If the user pressed `d', Gnus will descend the hierarchy, `y' will
942 subscribe to all newsgroups in the hierarchy and `s' will skip this
943 hierarchy in its entirety.")
944
945 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
946   "*Function used for sorting the group buffer.
947 This function will be called with group info entries as the arguments
948 for the groups to be sorted.  Pre-made functions include
949 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
950 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
951 `gnus-group-sort-by-rank'.  
952
953 This variable can also be a list of sorting functions.  In that case,
954 the most significant sort function should be the last function in the
955 list.")
956
957 ;; Mark variables suggested by Thomas Michanek
958 ;; <Thomas.Michanek@telelogic.se>. 
959 (defvar gnus-unread-mark ? 
960   "*Mark used for unread articles.")
961 (defvar gnus-ticked-mark ?!
962   "*Mark used for ticked articles.")
963 (defvar gnus-dormant-mark ??
964   "*Mark used for dormant articles.")
965 (defvar gnus-del-mark ?r
966   "*Mark used for del'd articles.")
967 (defvar gnus-read-mark ?R
968   "*Mark used for read articles.")
969 (defvar gnus-expirable-mark ?E
970   "*Mark used for expirable articles.")
971 (defvar gnus-killed-mark ?K
972   "*Mark used for killed articles.")
973 (defvar gnus-souped-mark ?F
974   "*Mark used for killed articles.")
975 (defvar gnus-kill-file-mark ?X
976   "*Mark used for articles killed by kill files.")
977 (defvar gnus-low-score-mark ?Y
978   "*Mark used for articles with a low score.")
979 (defvar gnus-catchup-mark ?C
980   "*Mark used for articles that are caught up.")
981 (defvar gnus-replied-mark ?A
982   "*Mark used for articles that have been replied to.")
983 (defvar gnus-cached-mark ?*
984   "*Mark used for articles that are in the cache.")
985 (defvar gnus-saved-mark ?S
986   "*Mark used for articles that have been saved to.")
987 (defvar gnus-process-mark ?# 
988   "*Process mark.")
989 (defvar gnus-ancient-mark ?O
990   "*Mark used for ancient articles.")
991 (defvar gnus-canceled-mark ?G
992   "*Mark used for canceled articles.")
993 (defvar gnus-score-over-mark ?+
994   "*Score mark used for articles with high scores.")
995 (defvar gnus-score-below-mark ?-
996   "*Score mark used for articles with low scores.")
997 (defvar gnus-empty-thread-mark ? 
998   "*There is no thread under the article.")
999 (defvar gnus-not-empty-thread-mark ?=
1000   "*There is a thread under the article.")
1001
1002 (defvar gnus-view-pseudo-asynchronously nil
1003   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1004
1005 (defvar gnus-view-pseudos nil
1006   "*If `automatic', pseudo-articles will be viewed automatically.
1007 If `not-confirm', pseudos will be viewed automatically, and the user
1008 will not be asked to confirm the command.")
1009
1010 (defvar gnus-view-pseudos-separately t
1011   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1012 If nil, all files that use the same viewing command will be given as a
1013 list of parameters to that command.")
1014
1015 (defvar gnus-insert-pseudo-articles t
1016   "*If non-nil, insert pseudo-articles when decoding articles.")
1017
1018 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)\n"
1019   "*Format of group lines.
1020 It works along the same lines as a normal formatting string,
1021 with some simple extensions.
1022
1023 %M    Only marked articles (character, \"*\" or \" \")
1024 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1025 %L    Level of subscribedness (integer)
1026 %N    Number of unread articles (integer)
1027 %I    Number of dormant articles (integer)
1028 %i    Number of ticked and dormant (integer)
1029 %T    Number of ticked articles (integer)
1030 %R    Number of read articles (integer)
1031 %t    Total number of articles (integer)
1032 %y    Number of unread, unticked articles (integer)
1033 %G    Group name (string)
1034 %g    Qualified group name (string)
1035 %D    Group description (string)
1036 %s    Select method (string)
1037 %o    Moderated group (char, \"m\")
1038 %p    Process mark (char)
1039 %O    Moderated group (string, \"(m)\" or \"\")
1040 %P    Topic indentation (string)
1041 %n    Select from where (string)
1042 %z    A string that look like `<%s:%n>' if a foreign select method is used
1043 %u    User defined specifier.  The next character in the format string should
1044       be a letter.  Gnus will call the function gnus-user-format-function-X,
1045       where X is the letter following %u.  The function will be passed the
1046       current header as argument.  The function should return a string, which
1047       will be inserted into the buffer just like information from any other
1048       group specifier.
1049
1050 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1051 the mouse point move inside the area.  There can only be one such area.
1052
1053 Note that this format specification is not always respected.  For
1054 reasons of efficiency, when listing killed groups, this specification
1055 is ignored altogether.  If the spec is changed considerably, your
1056 output may end up looking strange when listing both alive and killed
1057 groups.
1058
1059 If you use %o or %O, reading the active file will be slower and quite
1060 a bit of extra memory will be used. %D will also worsen performance.
1061 Also note that if you change the format specification to include any
1062 of these specs, you must probably re-start Gnus to see them go into
1063 effect.") 
1064
1065 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1066   "*The format specification of the lines in the summary buffer.
1067
1068 It works along the same lines as a normal formatting string,
1069 with some simple extensions.
1070
1071 %N   Article number, left padded with spaces (string)
1072 %S   Subject (string)
1073 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1074 %n   Name of the poster (string)
1075 %a   Extracted name of the poster (string)
1076 %A   Extracted address of the poster (string)
1077 %F   Contents of the From: header (string)
1078 %x   Contents of the Xref: header (string)
1079 %D   Date of the article (string)
1080 %d   Date of the article (string) in DD-MMM format
1081 %M   Message-id of the article (string)
1082 %r   References of the article (string)
1083 %c   Number of characters in the article (integer)
1084 %L   Number of lines in the article (integer)
1085 %I   Indentation based on thread level (a string of spaces)
1086 %T   A string with two possible values: 80 spaces if the article
1087      is on thread level two or larger and 0 spaces on level one
1088 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1089 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1090 %[   Opening bracket (character, \"[\" or \"<\")
1091 %]   Closing bracket (character, \"]\" or \">\")
1092 %>   Spaces of length thread-level (string)
1093 %<   Spaces of length (- 20 thread-level) (string)
1094 %i   Article score (number)
1095 %z   Article zcore (character)
1096 %t   Number of articles under the current thread (number).
1097 %e   Whether the thread is empty or not (character).
1098 %u   User defined specifier.  The next character in the format string should
1099      be a letter.  Gnus will call the function gnus-user-format-function-X,
1100      where X is the letter following %u.  The function will be passed the
1101      current header as argument.  The function should return a string, which
1102      will be inserted into the summary just like information from any other
1103      summary specifier.
1104
1105 Text between %( and %) will be highlighted with `gnus-mouse-face'
1106 when the mouse point is placed inside the area.  There can only be one
1107 such area.
1108
1109 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1110 with care.  For reasons of efficiency, Gnus will compute what column
1111 these characters will end up in, and \"hard-code\" that.  This means that
1112 it is illegal to have these specs after a variable-length spec.  Well,
1113 you might not be arrested, but your summary buffer will look strange,
1114 which is bad enough.
1115
1116 The smart choice is to have these specs as for to the left as
1117 possible. 
1118
1119 This restriction may disappear in later versions of Gnus.")
1120
1121 (defvar gnus-summary-dummy-line-format 
1122   "*  %(:                          :%) %S\n"
1123   "*The format specification for the dummy roots in the summary buffer.
1124 It works along the same lines as a normal formatting string,
1125 with some simple extensions.
1126
1127 %S  The subject")
1128
1129 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1130   "*The format specification for the summary mode line.
1131 It works along the same lines as a normal formatting string,
1132 with some simple extensions:
1133
1134 %G  Group name
1135 %p  Unprefixed group name
1136 %A  Current article number
1137 %V  Gnus version
1138 %U  Number of unread articles in the group
1139 %e  Number of unselected articles in the group
1140 %Z  A string with unread/unselected article counts
1141 %g  Shortish group name
1142 %S  Subject of the current article
1143 %u  User-defined spec
1144 %s  Current score file name
1145 %d  Number of dormant articles
1146 %r  Number of articles that have been marked as read in this session
1147 %E  Number of articles expunged by the score files")
1148
1149 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1150   "*The format specification for the article mode line.
1151 See `gnus-summary-mode-line-format' for a closer description.")
1152
1153 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1154   "*The format specification for the group mode line.
1155 It works along the same lines as a normal formatting string,
1156 with some simple extensions:
1157
1158 %S   The native news server.
1159 %M   The native select method.")
1160
1161 (defvar gnus-valid-select-methods
1162   '(("nntp" post address prompt-address)
1163     ("nnspool" post)
1164     ("nnvirtual" post-mail virtual prompt-address) 
1165     ("nnmbox" mail respool) 
1166     ("nnml" mail respool)
1167     ("nnmh" mail respool) 
1168     ("nndir" post-mail prompt-address address)
1169     ("nneething" none prompt-address)
1170     ("nndoc" none prompt-address) 
1171     ("nnbabyl" mail respool) 
1172     ("nnkiboze" post virtual) 
1173     ("nnsoup" post-mail)
1174     ("nnfolder" mail respool))
1175   "An alist of valid select methods.
1176 The first element of each list lists should be a string with the name
1177 of the select method.  The other elements may be be the category of
1178 this method (ie. `post', `mail', `none' or whatever) or other
1179 properties that this method has (like being respoolable).
1180 If you implement a new select method, all you should have to change is
1181 this variable.  I think.")
1182
1183 (defvar gnus-updated-mode-lines '(group article summary)
1184   "*List of buffers that should update their mode lines.
1185 The list may contain the symbols `group', `article' and `summary'.  If
1186 the corresponding symbol is present, Gnus will keep that mode line
1187 updated with information that may be pertinent. 
1188 If this variable is nil, screen refresh may be quicker.")
1189
1190 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1191 (defvar gnus-mode-non-string-length nil
1192   "*Max length of mode-line non-string contents.
1193 If this is nil, Gnus will take space as is needed, leaving the rest
1194 of the modeline intact.")
1195
1196 ;see gnus-cus.el
1197 ;(defvar gnus-mouse-face 'highlight
1198 ;  "*Face used for mouse highlighting in Gnus.
1199 ;No mouse highlights will be done if `gnus-visual' is nil.")
1200
1201 (defvar gnus-summary-mark-below nil
1202   "*Mark all articles with a score below this variable as read.
1203 This variable is local to each summary buffer and usually set by the
1204 score file.")  
1205
1206 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1207   "*List of functions used for sorting articles in the summary buffer.")
1208
1209 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1210   "*List of functions used for sorting threads in the summary buffer.
1211 By default, threads are sorted by article number.
1212
1213 Each function takes two threads and return non-nil if the first thread
1214 should be sorted before the other.  If you use more than one function,
1215 the primary sort function should be the last.
1216
1217 Ready-mady functions include `gnus-thread-sort-by-number',
1218 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1219 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1220 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1221
1222 (defvar gnus-thread-score-function '+
1223   "*Function used for calculating the total score of a thread.
1224
1225 The function is called with the scores of the article and each
1226 subthread and should then return the score of the thread.
1227
1228 Some functions you can use are `+', `max', or `min'.")
1229
1230 (defvar gnus-summary-expunge-below nil
1231   "All articles that have a score less than this variable will be expunged.")
1232
1233 (defvar gnus-thread-expunge-below nil
1234   "All threads that have a total score less than this variable will be expunged.
1235 See `gnus-thread-score-function' for en explanation of what a 
1236 \"thread score\" is.")
1237
1238 (defvar gnus-auto-subscribed-groups 
1239   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1240   "*All new groups that match this regexp will be subscribed automatically.
1241 Note that this variable only deals with new groups.  It has no effect
1242 whatsoever on old groups.")
1243
1244 (defvar gnus-options-subscribe nil
1245   "*All new groups matching this regexp will be subscribed unconditionally.
1246 Note that this variable deals only with new newsgroups.  This variable
1247 does not affect old newsgroups.")
1248
1249 (defvar gnus-options-not-subscribe nil
1250   "*All new groups matching this regexp will be ignored.
1251 Note that this variable deals only with new newsgroups.  This variable
1252 does not affect old (already subscribed) newsgroups.")
1253
1254 (defvar gnus-auto-expirable-newsgroups nil
1255   "*Groups in which to automatically mark read articles as expirable.
1256 If non-nil, this should be a regexp that should match all groups in
1257 which to perform auto-expiry.  This only makes sense for mail groups.")
1258
1259 (defvar gnus-total-expirable-newsgroups nil
1260   "*Groups in which to perform expiry of all read articles.
1261 Use with extreme caution.  All groups that match this regexp will be
1262 expiring - which means that all read articles will be deleted after
1263 (say) one week.  (This only goes for mail groups and the like, of
1264 course.)")
1265
1266 (defvar gnus-hidden-properties '(invisible t intangible t)
1267   "Property list to use for hiding text.")
1268
1269 (defvar gnus-modtime-botch nil
1270   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1271 due to the bogus appearance that .newsrc was modified on disc.")
1272
1273 ;; Hooks.
1274
1275 (defvar gnus-group-mode-hook nil
1276   "*A hook for Gnus group mode.")
1277
1278 (defvar gnus-summary-mode-hook nil
1279   "*A hook for Gnus summary mode.
1280 This hook is run before any variables are set in the summary buffer.")
1281
1282 (defvar gnus-article-mode-hook nil
1283   "*A hook for Gnus article mode.")
1284
1285 (defun gnus-summary-prepare-exit-hook nil
1286   "*A hook called when preparing to exit from the summary buffer.
1287 It calls `gnus-summary-expire-articles' by default.")
1288 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1289
1290 (defun gnus-summary-exit-hook nil
1291   "*A hook called on exit from the summary buffer.")
1292
1293 (defvar gnus-open-server-hook nil
1294   "*A hook called just before opening connection to the news server.")
1295
1296 (defvar gnus-load-hook nil
1297   "*A hook run while Gnus is loaded.")
1298
1299 (defvar gnus-startup-hook nil
1300   "*A hook called at startup.
1301 This hook is called after Gnus is connected to the NNTP server.")
1302
1303 (defvar gnus-get-new-news-hook nil
1304   "*A hook run just before Gnus checks for new news.")
1305
1306 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1307   "*A function that is called to generate the group buffer.
1308 The function is called with three arguments: The first is a number;
1309 all group with a level less or equal to that number should be listed,
1310 if the second is non-nil, empty groups should also be displayed.  If
1311 the third is non-nil, it is a number.  No groups with a level lower
1312 than this number should be displayed.
1313
1314 The only current function implemented is `gnus-group-prepare-flat'.")
1315
1316 (defvar gnus-group-prepare-hook nil
1317   "*A hook called after the group buffer has been generated.
1318 If you want to modify the group buffer, you can use this hook.")
1319
1320 (defvar gnus-summary-prepare-hook nil
1321   "*A hook called after the summary buffer has been generated.
1322 If you want to modify the summary buffer, you can use this hook.")
1323
1324 (defvar gnus-summary-generate-hook nil
1325   "*A hook run just before generating the summary buffer.
1326 This hook is commonly used to customize threading variables and the
1327 like.")
1328
1329 (defvar gnus-article-prepare-hook nil
1330   "*A hook called after an article has been prepared in the article buffer.
1331 If you want to run a special decoding program like nkf, use this hook.")
1332
1333 ;(defvar gnus-article-display-hook nil
1334 ;  "*A hook called after the article is displayed in the article buffer.
1335 ;The hook is designed to change the contents of the article
1336 ;buffer.  Typical functions that this hook may contain are
1337 ;`gnus-article-hide-headers' (hide selected headers),
1338 ;`gnus-article-maybe-highlight' (perform fancy article highlighting), 
1339 ;`gnus-article-hide-signature' (hide signature) and
1340 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1341 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1342 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1343 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1344
1345 (defvar gnus-article-x-face-command
1346   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1347   "String or function to be executed to display an X-Face header.
1348 If it is a string, the command will be executed in a sub-shell
1349 asynchronously.  The compressed face will be piped to this command.") 
1350
1351 (defvar gnus-article-x-face-too-ugly nil
1352   "Regexp matching posters whose face shouldn't be shown automatically.")
1353
1354 (defvar gnus-select-group-hook nil
1355   "*A hook called when a newsgroup is selected.
1356
1357 If you'd like to simplify subjects like the
1358 `gnus-summary-next-same-subject' command does, you can use the
1359 following hook:
1360
1361  (setq gnus-select-group-hook
1362       (list
1363         (lambda ()
1364           (mapcar (lambda (header)
1365                      (mail-header-set-subject
1366                       header
1367                       (gnus-simplify-subject
1368                        (mail-header-subject header) 're-only)))
1369                   gnus-newsgroup-headers))))")
1370
1371 (defvar gnus-select-article-hook
1372   '(gnus-summary-show-thread)
1373   "*A hook called when an article is selected.
1374 The default hook shows conversation thread subtrees of the selected
1375 article automatically using `gnus-summary-show-thread'.")
1376
1377 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1378   "*A hook called to apply kill files to a group.
1379 This hook is intended to apply a kill file to the selected newsgroup.
1380 The function `gnus-apply-kill-file' is called by default.
1381
1382 Since a general kill file is too heavy to use only for a few
1383 newsgroups, I recommend you to use a lighter hook function.  For
1384 example, if you'd like to apply a kill file to articles which contains
1385 a string `rmgroup' in subject in newsgroup `control', you can use the
1386 following hook:
1387
1388  (setq gnus-apply-kill-hook
1389       (list
1390         (lambda ()
1391           (cond ((string-match \"control\" gnus-newsgroup-name)
1392                  (gnus-kill \"Subject\" \"rmgroup\")
1393                  (gnus-expunge \"X\"))))))")
1394
1395 (defvar gnus-visual-mark-article-hook 
1396   (list 'gnus-highlight-selected-summary)
1397   "*Hook run after selecting an article in the summary buffer.
1398 It is meant to be used for highlighting the article in some way.  It
1399 is not run if `gnus-visual' is nil.")
1400
1401 (defun gnus-parse-headers-hook nil
1402   "*A hook called before parsing the headers.")
1403
1404 (defvar gnus-exit-group-hook nil
1405   "*A hook called when exiting (not quitting) summary mode.")
1406
1407 (defvar gnus-suspend-gnus-hook nil
1408   "*A hook called when suspending (not exiting) Gnus.")
1409
1410 (defvar gnus-exit-gnus-hook nil
1411   "*A hook called when exiting Gnus.")
1412
1413 (defvar gnus-save-newsrc-hook nil
1414   "*A hook called before saving any of the newsrc files.")
1415
1416 (defvar gnus-save-quick-newsrc-hook nil
1417   "*A hook called just before saving the quick newsrc file.
1418 Can be used to turn version control on or off.")
1419
1420 (defvar gnus-save-standard-newsrc-hook nil
1421   "*A hook called just before saving the standard newsrc file.
1422 Can be used to turn version control on or off.")
1423
1424 (defvar gnus-summary-update-hook 
1425   (list 'gnus-summary-highlight-line)
1426   "*A hook called when a summary line is changed.
1427 The hook will not be called if `gnus-visual' is nil.
1428
1429 The default function `gnus-summary-highlight-line' will
1430 highlight the line according to the `gnus-summary-highlight'
1431 variable.")
1432
1433 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1434   "*A hook called when an article is selected for the first time.
1435 The hook is intended to mark an article as read (or unread)
1436 automatically when it is selected.")
1437
1438 ;; Remove any hilit infestation.
1439 (add-hook 'gnus-startup-hook
1440           (lambda ()
1441             (remove-hook 'gnus-summary-prepare-hook
1442                          'hilit-rehighlight-buffer-quietly)
1443             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1444             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1445             (remove-hook 'gnus-article-prepare-hook
1446                          'hilit-rehighlight-buffer-quietly)))
1447
1448
1449 \f
1450 ;; Internal variables
1451
1452 (defconst gnus-article-mark-lists 
1453   '((marked . tick) (replied . reply) 
1454     (expirable . expire) (killed . killed)
1455     (bookmarks . bookmark) (dormant . dormant)
1456     (scored . score) (saved . save)))
1457
1458 ;; Avoid highlighting in kill files.
1459 (defvar gnus-summary-inhibit-highlight nil)
1460 (defvar gnus-newsgroup-selected-overlay nil)
1461
1462 (defvar gnus-inhibit-hiding nil)
1463 (defvar gnus-topic-indentation "")
1464 (defvar gnus-inhibit-limiting nil)
1465
1466 (defvar gnus-article-mode-map nil)
1467 (defvar gnus-dribble-buffer nil)
1468 (defvar gnus-headers-retrieved-by nil)
1469 (defvar gnus-article-reply nil)
1470 (defvar gnus-override-method nil)
1471 (defvar gnus-article-check-size nil)
1472
1473 (defvar gnus-nocem-hashtb nil)
1474
1475 (defvar gnus-current-score-file nil)
1476 (defvar gnus-newsgroup-adaptive-score-file nil)
1477 (defvar gnus-scores-exclude-files nil)
1478
1479 (defvar gnus-opened-servers nil)
1480
1481 (defvar gnus-current-move-group nil)
1482
1483 (defvar gnus-newsgroup-dependencies nil)
1484 (defvar gnus-newsgroup-async nil)
1485 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1486
1487 (defvar gnus-newsgroup-adaptive nil)
1488
1489 (defvar gnus-summary-display-table nil)
1490
1491 (defconst gnus-group-line-format-alist
1492   `((?M gnus-tmp-marked-mark ?c)
1493     (?S gnus-tmp-subscribed ?c)
1494     (?L gnus-tmp-level ?d)
1495     (?N gnus-tmp-number ?s)
1496     (?R gnus-tmp-number-of-read ?s)
1497     (?t gnus-tmp-number-total ?s)
1498     (?y gnus-tmp-number-of-unread ?s)
1499     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1500     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1501     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1502            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1503     (?g gnus-tmp-group ?s)
1504     (?G gnus-tmp-qualified-group ?s)
1505     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1506     (?D gnus-tmp-newsgroup-description ?s)
1507     (?o gnus-tmp-moderated ?c)
1508     (?O gnus-tmp-moderated-string ?s)
1509     (?p gnus-tmp-process-marked ?c)
1510     (?s gnus-tmp-news-server ?s)
1511     (?n gnus-tmp-news-method ?s)
1512     (?P gnus-topic-indentation ?s)
1513     (?z gnus-tmp-news-method-string ?s)
1514     (?u gnus-tmp-user-defined ?s)))
1515
1516 (defconst gnus-summary-line-format-alist 
1517   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1518     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1519     (?s gnus-tmp-subject-or-nil ?s)
1520     (?n gnus-tmp-name ?s)
1521     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1522         ?s)
1523     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from)) 
1524             gnus-tmp-from) ?s)
1525     (?F gnus-tmp-from ?s)
1526     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1527     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1528     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1529     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1530     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1531     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1532     (?L gnus-tmp-lines ?d)
1533     (?I gnus-tmp-indentation ?s)
1534     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1535     (?R gnus-tmp-replied ?c)
1536     (?\[ gnus-tmp-opening-bracket ?c)
1537     (?\] gnus-tmp-closing-bracket ?c)
1538     (?\> (make-string gnus-tmp-level ? ) ?s)
1539     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1540     (?i gnus-tmp-score ?d)
1541     (?z gnus-tmp-score-char ?c)
1542     (?U gnus-tmp-unread ?c)
1543     (?t (gnus-summary-number-of-articles-in-thread 
1544          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1545         ?d)
1546     (?e (gnus-summary-number-of-articles-in-thread 
1547          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1548         ?c)
1549     (?u gnus-tmp-user-defined ?s))
1550   "An alist of format specifications that can appear in summary lines,
1551 and what variables they correspond with, along with the type of the
1552 variable (string, integer, character, etc).")
1553
1554 (defconst gnus-summary-dummy-line-format-alist
1555   (` ((?S gnus-tmp-subject ?s)
1556       (?N gnus-tmp-number ?d)
1557       (?u gnus-tmp-user-defined ?s))))
1558
1559 (defconst gnus-summary-mode-line-format-alist 
1560   (` ((?G gnus-tmp-group-name ?s)
1561       (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1562       (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1563       (?A gnus-tmp-article-number ?d)
1564       (?Z gnus-tmp-unread-and-unselected ?s)
1565       (?V gnus-version ?s)
1566       (?U gnus-tmp-unread ?d)
1567       (?S gnus-tmp-subject ?s)
1568       (?e gnus-tmp-unselected ?d)
1569       (?u gnus-tmp-user-defined ?s)
1570       (?d (length gnus-newsgroup-dormant) ?d)
1571       (?t (length gnus-newsgroup-marked) ?d)
1572       (?r (length gnus-newsgroup-reads) ?d)
1573       (?E gnus-newsgroup-expunged-tally ?d)
1574       (?s (gnus-current-score-file-nondirectory) ?s))))
1575
1576 (defconst gnus-article-mode-line-format-alist  
1577   gnus-summary-mode-line-format-alist)
1578
1579 (defconst gnus-group-mode-line-format-alist 
1580   (` ((?S gnus-tmp-news-server ?s)
1581       (?M gnus-tmp-news-method ?s)
1582       (?u gnus-tmp-user-defined ?s))))
1583
1584 (defvar gnus-have-read-active-file nil)
1585
1586 (defconst gnus-maintainer
1587   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1588   "The mail address of the Gnus maintainers.")
1589
1590 (defconst gnus-version "September Gnus v0.26"
1591   "Version number for this version of Gnus.")
1592
1593 (defvar gnus-info-nodes
1594   '((gnus-group-mode            "(gnus)The Group Buffer")
1595     (gnus-summary-mode          "(gnus)The Summary Buffer")
1596     (gnus-article-mode          "(gnus)The Article Buffer"))
1597   "Assoc list of major modes and related Info nodes.")
1598
1599 (defvar gnus-group-buffer "*Group*")
1600 (defvar gnus-summary-buffer "*Summary*")
1601 (defvar gnus-article-buffer "*Article*")
1602 (defvar gnus-server-buffer "*Server*")
1603
1604 (defvar gnus-work-buffer " *gnus work*")
1605
1606 (defvar gnus-original-article-buffer " *Original Article*")
1607 (defvar gnus-original-article nil)
1608
1609 (defvar gnus-buffer-list nil
1610   "Gnus buffers that should be killed on exit.")
1611
1612 (defvar gnus-server-alist nil
1613   "List of available servers.")
1614
1615 (defvar gnus-slave nil
1616   "Whether this Gnus is a slave or not.")
1617
1618 (defvar gnus-variable-list
1619   '(gnus-newsrc-options gnus-newsrc-options-n
1620     gnus-newsrc-last-checked-date 
1621     gnus-newsrc-alist gnus-server-alist
1622     gnus-killed-list gnus-zombie-list
1623     gnus-topic-topology gnus-topic-alist
1624     gnus-format-specs)
1625   "Gnus variables saved in the quick startup file.")
1626
1627 (defvar gnus-newsrc-options nil
1628   "Options line in the .newsrc file.")
1629
1630 (defvar gnus-newsrc-options-n nil
1631   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1632
1633 (defvar gnus-newsrc-last-checked-date nil
1634   "Date Gnus last asked server for new newsgroups.")
1635
1636 (defvar gnus-topic-topology nil
1637   "The complete topic hierarchy.")
1638
1639 (defvar gnus-topic-alist nil
1640   "The complete topic-group alist.")
1641
1642 (defvar gnus-newsrc-alist nil
1643   "Assoc list of read articles.
1644 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1645
1646 (defvar gnus-newsrc-hashtb nil
1647   "Hashtable of gnus-newsrc-alist.")
1648
1649 (defvar gnus-killed-list nil
1650   "List of killed newsgroups.")
1651
1652 (defvar gnus-killed-hashtb nil
1653   "Hash table equivalent of gnus-killed-list.")
1654
1655 (defvar gnus-zombie-list nil
1656   "List of almost dead newsgroups.")
1657
1658 (defvar gnus-description-hashtb nil
1659   "Descriptions of newsgroups.")
1660
1661 (defvar gnus-list-of-killed-groups nil
1662   "List of newsgroups that have recently been killed by the user.")
1663
1664 (defvar gnus-active-hashtb nil
1665   "Hashtable of active articles.")
1666
1667 (defvar gnus-moderated-list nil
1668   "List of moderated newsgroups.")
1669
1670 (defvar gnus-group-marked nil)
1671
1672 (defvar gnus-current-startup-file nil
1673   "Startup file for the current host.")
1674
1675 (defvar gnus-last-search-regexp nil
1676   "Default regexp for article search command.")
1677
1678 (defvar gnus-last-shell-command nil
1679   "Default shell command on article.")
1680
1681 (defvar gnus-current-select-method nil
1682   "The current method for selecting a newsgroup.")
1683
1684 (defvar gnus-group-list-mode nil)
1685
1686 (defvar gnus-article-internal-prepare-hook nil)
1687
1688 (defvar gnus-newsgroup-name nil)
1689 (defvar gnus-newsgroup-begin nil)
1690 (defvar gnus-newsgroup-end nil)
1691 (defvar gnus-newsgroup-last-rmail nil)
1692 (defvar gnus-newsgroup-last-mail nil)
1693 (defvar gnus-newsgroup-last-folder nil)
1694 (defvar gnus-newsgroup-last-file nil)
1695 (defvar gnus-newsgroup-auto-expire nil)
1696 (defvar gnus-newsgroup-active nil)
1697
1698 (defvar gnus-newsgroup-data nil)
1699 (defvar gnus-newsgroup-data-reverse nil)
1700 (defvar gnus-newsgroup-limit nil)
1701 (defvar gnus-newsgroup-limits nil)
1702
1703 (defvar gnus-newsgroup-unreads nil
1704   "List of unread articles in the current newsgroup.")
1705
1706 (defvar gnus-newsgroup-unselected nil
1707   "List of unselected unread articles in the current newsgroup.")
1708
1709 (defvar gnus-newsgroup-reads nil
1710   "Alist of read articles and article marks in the current newsgroup.")
1711
1712 (defvar gnus-newsgroup-expunged-tally nil)
1713
1714 (defvar gnus-newsgroup-marked nil
1715   "List of ticked articles in the current newsgroup (a subset of unread art).")
1716
1717 (defvar gnus-newsgroup-killed nil
1718   "List of ranges of articles that have been through the scoring process.")
1719
1720 (defvar gnus-newsgroup-cached nil
1721   "List of articles that come from the article cache.")
1722
1723 (defvar gnus-newsgroup-saved nil
1724   "List of articles that have been saved.")
1725
1726 (defvar gnus-newsgroup-kill-headers nil)
1727
1728 (defvar gnus-newsgroup-replied nil
1729   "List of articles that have been replied to in the current newsgroup.")
1730
1731 (defvar gnus-newsgroup-expirable nil
1732   "List of articles in the current newsgroup that can be expired.")
1733
1734 (defvar gnus-newsgroup-processable nil
1735   "List of articles in the current newsgroup that can be processed.")
1736
1737 (defvar gnus-newsgroup-bookmarks nil
1738   "List of articles in the current newsgroup that have bookmarks.")
1739
1740 (defvar gnus-newsgroup-dormant nil
1741   "List of dormant articles in the current newsgroup.")
1742
1743 (defvar gnus-newsgroup-scored nil
1744   "List of scored articles in the current newsgroup.")
1745
1746 (defvar gnus-newsgroup-headers nil
1747   "List of article headers in the current newsgroup.")
1748
1749 (defvar gnus-newsgroup-threads nil)
1750
1751 (defvar gnus-newsgroup-prepared nil
1752   "Whether the current group has been prepared properly.")
1753
1754 (defvar gnus-newsgroup-ancient nil
1755   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1756
1757 (defvar gnus-current-article nil)
1758 (defvar gnus-article-current nil)
1759 (defvar gnus-current-headers nil)
1760 (defvar gnus-have-all-headers nil)
1761 (defvar gnus-last-article nil)
1762 (defvar gnus-newsgroup-history nil)
1763 (defvar gnus-current-kill-article nil)
1764
1765 ;; Save window configuration.
1766 (defvar gnus-prev-winconf nil)
1767
1768 (defvar gnus-summary-mark-positions nil)
1769 (defvar gnus-group-mark-positions nil)
1770
1771 (defvar gnus-reffed-article-number nil)
1772
1773 ;;; Let the byte-compiler know that we know about this variable.
1774 (defvar rmail-default-rmail-file)
1775
1776 (defvar gnus-cache-removeable-articles nil)
1777
1778 (defvar gnus-dead-summary nil)
1779
1780 (defconst gnus-summary-local-variables 
1781   '(gnus-newsgroup-name 
1782     gnus-newsgroup-begin gnus-newsgroup-end 
1783     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1784     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1785     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1786     gnus-newsgroup-unselected gnus-newsgroup-marked
1787     gnus-newsgroup-reads gnus-newsgroup-saved
1788     gnus-newsgroup-replied gnus-newsgroup-expirable
1789     gnus-newsgroup-processable gnus-newsgroup-killed
1790     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1791     gnus-newsgroup-headers gnus-newsgroup-threads
1792     gnus-newsgroup-prepared
1793     gnus-current-article gnus-current-headers gnus-have-all-headers
1794     gnus-last-article gnus-article-internal-prepare-hook
1795     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1796     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1797     gnus-newsgroup-async
1798     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1799     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1800     gnus-newsgroup-history gnus-newsgroup-ancient
1801     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1802     gnus-newsgroup-adaptive-score-file
1803     (gnus-newsgroup-expunged-tally . 0)
1804     gnus-cache-removeable-articles gnus-newsgroup-cached
1805     gnus-newsgroup-data gnus-newsgroup-data-reverse
1806     gnus-newsgroup-limit gnus-newsgroup-limits)
1807   "Variables that are buffer-local to the summary buffers.")
1808
1809 (defconst gnus-bug-message
1810   "Sending a bug report to the Gnus Towers.
1811 ========================================
1812
1813 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1814 be sent to the Gnus Bug Exterminators. 
1815
1816 At the bottom of the buffer you'll see lots of variable settings.
1817 Please do not delete those.  They will tell the Bug People what your
1818 environment is, so that it will be easier to locate the bugs.
1819
1820 If you have found a bug that makes Emacs go \"beep\", set
1821 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1822 and include the backtrace in your bug report.
1823
1824 Please describe the bug in annoying, painstaking detail.
1825
1826 Thank you for your help in stamping out bugs.
1827 ")
1828
1829 ;;; End of variables.
1830
1831 ;; Define some autoload functions Gnus might use.
1832 (eval-and-compile
1833
1834   ;; This little mapcar goes through the list below and marks the
1835   ;; symbols in question as autoloaded functions.
1836   (mapcar 
1837    (lambda (package)
1838      (let ((interactive (nth 1 (memq ':interactive package))))
1839        (mapcar 
1840         (lambda (function)
1841           (let (keymap)
1842             (when (consp function)
1843               (setq keymap (car (memq 'keymap function)))
1844               (setq function (car function)))
1845             (autoload function (car package) nil interactive keymap)))
1846         (if (eq (nth 1 package) ':interactive)
1847             (cdddr package)
1848           (cdr package)))))
1849    '(("metamail" metamail-buffer)
1850      ("info" Info-goto-node)
1851      ("hexl" hexl-hex-string-to-integer)
1852      ("pp" pp pp-to-string pp-eval-expression)
1853      ("mail-extr" mail-extract-address-components)
1854      ("nnmail" nnmail-split-fancy nnmail-article-group)
1855      ("nnvirtual" nnvirtual-catchup-group)
1856      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1857       timezone-make-sortable-date timezone-make-time-string)
1858      ("sendmail" mail-position-on-field mail-setup)
1859      ("rmailout" rmail-output)
1860      ("rnewspost" news-mail-other-window news-reply-yank-original 
1861       news-caesar-buffer-body)
1862      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1863       rmail-show-message)
1864      ("gnus-soup" :interactive t
1865       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article 
1866       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1867      ("nnsoup" nnsoup-pack-replies)
1868      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder 
1869       gnus-Folder-save-name gnus-folder-save-name)
1870      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1871      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1872       gnus-server-make-menu-bar gnus-article-make-menu-bar
1873       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1874       gnus-summary-highlight-line gnus-carpal-setup-buffer
1875       gnus-article-add-button gnus-insert-next-page-button
1876       gnus-insert-prev-page-button)
1877      ("gnus-vis" :interactive t
1878       gnus-article-push-button gnus-article-press-button 
1879       gnus-article-highlight gnus-article-highlight-some 
1880       gnus-article-highlight-headers gnus-article-highlight-signature 
1881       gnus-article-add-buttons gnus-article-add-buttons-to-head 
1882       gnus-article-next-button gnus-article-prev-button)
1883      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
1884       gnus-demon-add-disconnection gnus-demon-add-handler
1885       gnus-demon-remove-handler)
1886      ("gnus-demon" :interactive t
1887       gnus-demon-init gnus-demon-cancel)
1888      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close)
1889      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
1890      ("gnus-cite" :interactive t
1891       gnus-article-highlight-citation gnus-article-hide-citation-maybe 
1892       gnus-article-hide-citation)
1893      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal 
1894       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author 
1895       gnus-execute gnus-expunge)
1896      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
1897       gnus-cache-possibly-remove-articles gnus-cache-request-article
1898       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
1899       gnus-cache-enter-remove-article gnus-cached-article-p
1900       gnus-cache-open gnus-cache-close)
1901      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
1902       gnus-cache-remove-article)
1903      ("gnus-score" :interactive t
1904       gnus-summary-increase-score gnus-summary-lower-score
1905       gnus-score-flush-cache gnus-score-close 
1906       gnus-score-raise-same-subject-and-select 
1907       gnus-score-raise-same-subject gnus-score-default 
1908       gnus-score-raise-thread gnus-score-lower-same-subject-and-select 
1909       gnus-score-lower-same-subject gnus-score-lower-thread 
1910       gnus-possibly-score-headers)
1911      ("gnus-score" 
1912       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
1913       gnus-current-score-file-nondirectory gnus-score-adaptive
1914       gnus-score-find-trace gnus-score-file-name)
1915      ("gnus-edit" :interactive t gnus-score-customize)
1916      ("gnus-topic" :interactive t gnus-topic-mode)
1917      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
1918      ("gnus-uu" :interactive t
1919       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward 
1920       gnus-uu-mark-series gnus-uu-mark-region 
1921       gnus-uu-mark-by-regexp gnus-uu-mark-all 
1922       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu 
1923       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar 
1924       gnus-uu-decode-unshar-and-save gnus-uu-decode-save 
1925       gnus-uu-decode-binhex gnus-uu-decode-uu-view 
1926       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view 
1927       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view 
1928       gnus-uu-decode-binhex-view)
1929      ("gnus-msg" (gnus-summary-send-map keymap)
1930       gnus-mail-yank-original gnus-mail-send-and-exit
1931       gnus-sendmail-setup-mail gnus-article-mail 
1932       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
1933      ("gnus-msg" :interactive t
1934       gnus-group-post-news gnus-group-mail gnus-summary-post-news
1935       gnus-summary-followup gnus-summary-followup-with-original
1936       gnus-summary-followup-and-reply
1937       gnus-summary-followup-and-reply-with-original
1938       gnus-summary-cancel-article gnus-summary-supersede-article
1939       gnus-post-news gnus-inews-news gnus-cancel-news
1940       gnus-summary-reply gnus-summary-reply-with-original
1941       gnus-summary-mail-forward gnus-summary-mail-other-window
1942       gnus-bug)
1943      ("gnus-picon" gnus-article-display-picons)
1944      ("gnus-vm" gnus-vm-mail-setup)
1945      ("gnus-vm" :interactive t gnus-summary-save-in-vm
1946       gnus-summary-save-article-vm gnus-yank-article))))
1947
1948 \f
1949
1950 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1951 ;; If you want the cursor to go somewhere else, set these two
1952 ;; functions in some startup hook to whatever you want.
1953 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
1954 (defalias 'gnus-group-position-point 'gnus-goto-colon)
1955
1956 ;;; Various macros and substs.
1957
1958 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1959   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
1960   `(let ((GnusStartBufferWindow (selected-window)))
1961      (unwind-protect
1962          (progn
1963            (pop-to-buffer ,buffer)
1964            ,@forms)
1965        (select-window GnusStartBufferWindow))))
1966
1967 (defmacro gnus-gethash (string hashtable)
1968   "Get hash value of STRING in HASHTABLE."
1969   `(symbol-value (intern-soft ,string ,hashtable)))
1970
1971 (defmacro gnus-sethash (string value hashtable)
1972   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
1973   `(set (intern ,string ,hashtable) ,value))
1974
1975 (defmacro gnus-intern-safe (string hashtable)
1976   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
1977   `(let ((symbol (intern ,string ,hashtable)))
1978      (or (boundp symbol)
1979          (set symbol nil))
1980      symbol))
1981
1982 (defmacro gnus-group-unread (group)
1983   "Get the currently computed number of unread articles in GROUP."
1984   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
1985
1986 (defmacro gnus-active (group)
1987   "Get active info on GROUP."
1988   `(gnus-gethash ,group gnus-active-hashtb))
1989
1990 (defmacro gnus-set-active (group active)
1991   "Set GROUP's active info."
1992   `(gnus-sethash ,group ,active gnus-active-hashtb))
1993
1994 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1995 ;;   function `substring' might cut on a middle of multi-octet
1996 ;;   character.
1997 (defun gnus-truncate-string (str width)
1998   (substring str 0 width))
1999
2000 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2001 ;; to limit the length of a string.  This function is necessary since
2002 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2003 (defsubst gnus-limit-string (str width)
2004   (if (> (length str) width)
2005       (substring str 0 width)
2006     str))
2007
2008 (defsubst gnus-simplify-subject-re (subject)
2009   "Remove \"Re:\" from subject lines."
2010   (if (string-match "^[Rr][Ee]: *" subject)
2011       (substring subject (match-end 0))
2012     subject))
2013
2014 (defsubst gnus-goto-char (point)
2015   (and point (goto-char point)))
2016
2017 (defmacro gnus-buffer-exists-p (buffer)
2018   `(and ,buffer
2019         (funcall (if (stringp ,buffer) 'get-buffer 'buffer-name)
2020                  ,buffer)))
2021
2022 (defmacro gnus-kill-buffer (buffer)
2023   `(let ((buf ,buffer))
2024      (if (gnus-buffer-exists-p buf)
2025          (kill-buffer buf))))
2026
2027 (defsubst gnus-point-at-bol ()
2028   "Return point at the beginning of the line."
2029   (let ((p (point)))
2030     (beginning-of-line)
2031     (prog1
2032         (point)
2033       (goto-char p))))
2034
2035 (defsubst gnus-point-at-eol ()
2036   "Return point at the end of the line."
2037   (let ((p (point)))
2038     (end-of-line)
2039     (prog1
2040         (point)
2041       (goto-char p))))
2042
2043 ;; Delete the current line (and the next N lines.);
2044 (defmacro gnus-delete-line (&optional n)
2045   `(delete-region (progn (beginning-of-line) (point))
2046                   (progn (forward-line ,(or n 1)) (point))))
2047
2048 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2049 (defvar gnus-init-inhibit nil)
2050 (defun gnus-read-init-file (&optional inhibit-next)
2051   (if gnus-init-inhibit
2052       (setq gnus-init-inhibit nil)
2053     (setq gnus-init-inhibit inhibit-next)
2054     (and gnus-init-file
2055          (or (and (file-exists-p gnus-init-file) 
2056                   ;; Don't try to load a directory.
2057                   (not (file-directory-p gnus-init-file)))
2058              (file-exists-p (concat gnus-init-file ".el"))
2059              (file-exists-p (concat gnus-init-file ".elc")))
2060          (load gnus-init-file nil t))))
2061
2062 ;; Info access macros.
2063
2064 (defmacro gnus-info-group (info)
2065   `(nth 0 ,info))
2066 (defmacro gnus-info-rank (info)
2067   `(nth 1 ,info))
2068 (defmacro gnus-info-read (info)
2069   `(nth 2 ,info))
2070 (defmacro gnus-info-marks (info)
2071   `(nth 3 ,info))
2072 (defmacro gnus-info-method (info)
2073   `(nth 4 ,info))
2074 (defmacro gnus-info-params (info)
2075   `(nth 5 ,info))
2076
2077 (defmacro gnus-info-level (info)
2078   `(let ((rank (gnus-info-rank ,info)))
2079      (if (consp rank)
2080          (car rank)
2081        rank)))
2082 (defmacro gnus-info-score (info)
2083   `(let ((rank (gnus-info-rank ,info)))
2084      (or (and (consp rank) (cdr rank)) 0)))
2085
2086 (defmacro gnus-info-set-group (info group)
2087   `(setcar ,info ,group))
2088 (defmacro gnus-info-set-rank (info rank)
2089   `(setcar (nthcdr 1 ,info) ,rank))
2090 (defmacro gnus-info-set-read (info read)
2091   `(setcar (nthcdr 2 ,info) ,read))
2092 (defmacro gnus-info-set-marks (info marks)
2093   `(setcar (nthcdr 3 ,info) ,marks))
2094 (defmacro gnus-info-set-method (info method)
2095   `(setcar (nthcdr 4 ,info) ,method))
2096 (defmacro gnus-info-set-params (info params)
2097   `(setcar (nthcdr 5 ,info) ,params))
2098
2099 (defmacro gnus-info-set-level (info level)
2100   `(let ((rank (cdr ,info)))
2101      (if (consp (car rank))
2102          (setcar (car rank) ,level)
2103        (setcar rank ,level))))
2104 (defmacro gnus-info-set-score (info score)
2105   `(let ((rank (cdr ,info)))
2106      (if (consp (car rank))
2107          (setcdr (car rank) ,score)
2108        (setcar rank (cons (car rank) ,score)))))
2109
2110 (defmacro gnus-get-info (group)
2111   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2112
2113 (defun gnus-byte-code (func)
2114   "Return a form that can be `eval'ed based on FUNC."
2115   (let ((fval (symbol-function func)))
2116     (if (byte-code-function-p fval)
2117         (let ((flist (append fval nil)))
2118           (setcar flist 'byte-code)
2119           flist)
2120       (cons 'progn (cdr (cdr fval))))))
2121
2122 ;;; Load the user startup file.
2123 ;; (eval '(gnus-read-init-file 'inhibit))
2124
2125 ;;; Load the compatability functions. 
2126
2127 (require 'gnus-cus)
2128 (require 'gnus-ems)
2129
2130 \f
2131
2132 ;; Format specs.  The chunks below are the machine-generated forms
2133 ;; that are to be evaled as the result of the default format strings.
2134 ;; We write them in here to get them byte-compiled.  That way the
2135 ;; default actions will be quite fast, while still retaining the full
2136 ;; flexibility of the user-defined format specs. 
2137
2138 ;; First we have lots of dummy defvars to let the compiler know these
2139 ;; are really dynamic variables.
2140
2141 (defvar gnus-tmp-unread)
2142 (defvar gnus-tmp-replied)
2143 (defvar gnus-tmp-score-char)
2144 (defvar gnus-tmp-indentation)
2145 (defvar gnus-tmp-opening-bracket)
2146 (defvar gnus-tmp-lines)
2147 (defvar gnus-tmp-name)
2148 (defvar gnus-tmp-closing-bracket)
2149 (defvar gnus-tmp-subject-or-nil)
2150 (defvar gnus-tmp-subject)
2151 (defvar gnus-tmp-marked)
2152 (defvar gnus-tmp-marked-mark)
2153 (defvar gnus-tmp-subscribed)
2154 (defvar gnus-tmp-process-marked)
2155 (defvar gnus-tmp-number-of-unread)
2156 (defvar gnus-tmp-group-name)
2157 (defvar gnus-tmp-group)
2158 (defvar gnus-tmp-article-number)
2159 (defvar gnus-tmp-unread-and-unselected)
2160 (defvar gnus-tmp-news-method)
2161 (defvar gnus-tmp-news-server)
2162 (defvar gnus-tmp-article-number)
2163 (defvar gnus-mouse-face)
2164 (defvar gnus-mouse-face-prop)
2165
2166 (defun gnus-summary-line-format-spec ()
2167   (insert gnus-tmp-unread gnus-tmp-replied 
2168           gnus-tmp-score-char gnus-tmp-indentation)
2169   (put-text-property
2170    (point)
2171    (progn
2172      (insert 
2173       gnus-tmp-opening-bracket 
2174       (format "%4d: %-20s" 
2175               gnus-tmp-lines 
2176               (if (> (length gnus-tmp-name) 20) 
2177                   (substring gnus-tmp-name 0 20) 
2178                 gnus-tmp-name))
2179       gnus-tmp-closing-bracket)
2180      (point))
2181    gnus-mouse-face-prop gnus-mouse-face)
2182   (insert " " gnus-tmp-subject-or-nil "\n"))
2183
2184 (defvar gnus-summary-line-format-spec 
2185   (gnus-byte-code 'gnus-summary-line-format-spec))
2186
2187 (defun gnus-summary-dummy-line-format-spec ()
2188   (insert "*  ")
2189   (put-text-property
2190    (point)
2191    (progn
2192      (insert ":                          :")
2193      (point))
2194    gnus-mouse-face-prop gnus-mouse-face)
2195   (insert " " gnus-tmp-subject "\n"))
2196
2197 (defvar gnus-summary-dummy-line-format-spec 
2198   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2199
2200 (defun gnus-group-line-format-spec ()
2201   (insert gnus-tmp-marked-mark gnus-tmp-subscribed 
2202           gnus-tmp-process-marked
2203           gnus-topic-indentation
2204           (format "%5s: " gnus-tmp-number-of-unread))
2205   (put-text-property 
2206    (point)
2207    (progn
2208      (insert gnus-tmp-group "\n")
2209      (1- (point)))
2210    gnus-mouse-face-prop gnus-mouse-face))
2211 (defvar gnus-group-line-format-spec 
2212   (gnus-byte-code 'gnus-group-line-format-spec))
2213
2214 (defvar gnus-format-specs 
2215   `((version . ,emacs-version)
2216     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2217     (summary-dummy ,gnus-summary-dummy-line-format
2218                    ,gnus-summary-dummy-line-format-spec)
2219     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2220
2221 (defvar gnus-article-mode-line-format-spec nil)
2222 (defvar gnus-summary-mode-line-format-spec nil)
2223 (defvar gnus-group-mode-line-format-spec nil)
2224
2225 ;;; Phew.  All that gruft is over, fortunately.  
2226
2227 \f
2228 ;;;
2229 ;;; Gnus Utility Functions
2230 ;;;
2231
2232 (defun gnus-extract-address-components (from)
2233   (let (name address)
2234     ;; First find the address - the thing with the @ in it.  This may
2235     ;; not be accurate in mail addresses, but does the trick most of
2236     ;; the time in news messages.
2237     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2238         (setq address (substring from (match-beginning 0) (match-end 0))))
2239     ;; Then we check whether the "name <address>" format is used.
2240     (and address
2241          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2242          ;; Linear white space is not required.
2243          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2244          (and (setq name (substring from 0 (match-beginning 0)))
2245               ;; Strip any quotes from the name.
2246               (string-match "\".*\"" name)
2247               (setq name (substring name 1 (1- (match-end 0))))))
2248     ;; If not, then "address (name)" is used.
2249     (or name
2250         (and (string-match "(.+)" from)
2251              (setq name (substring from (1+ (match-beginning 0)) 
2252                                    (1- (match-end 0)))))
2253         (and (string-match "()" from)
2254              (setq name address))
2255         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2256         ;; XOVER might not support folded From headers.
2257         (and (string-match "(.*" from)
2258              (setq name (substring from (1+ (match-beginning 0)) 
2259                                    (match-end 0)))))
2260     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2261     (list (or name from) (or address from))))
2262
2263 (defun gnus-fetch-field (field)
2264   "Return the value of the header FIELD of current article."
2265   (save-excursion
2266     (save-restriction
2267       (let ((case-fold-search t))
2268         (gnus-narrow-to-headers)
2269         (mail-fetch-field field)))))
2270
2271 (defun gnus-goto-colon ()
2272   (beginning-of-line)
2273   (search-forward ":" (gnus-point-at-eol) t))
2274
2275 (defun gnus-narrow-to-headers ()
2276   "Narrow to the head of an article."
2277   (widen)
2278   (narrow-to-region
2279    (goto-char (point-min))
2280    (if (search-forward "\n\n" nil t)
2281        (1- (point))
2282      (point-max)))
2283   (goto-char (point-min)))
2284
2285 ;;;###autoload
2286 (defun gnus-update-format (var)
2287   "Update the format specification near point."
2288   (interactive
2289    (list
2290     (save-excursion
2291       (eval-defun nil)
2292       ;; Find the end of the current word.
2293       (re-search-forward "[ \t\n]" nil t)
2294       ;; Search backward.
2295       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2296         (match-string 1)))))
2297   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2298                               (match-string 1 var))))
2299          (entry (assq type gnus-format-specs))
2300          value spec)
2301     (when entry 
2302       (setq gnus-format-specs (delq entry gnus-format-specs)))
2303     (set
2304      (intern (format "%s-spec" var))
2305      (gnus-parse-format (setq value (symbol-value (intern var)))
2306                         (symbol-value (intern (format "%s-alist" var)))
2307                         (not (string-match "mode" var))))
2308     (setq spec (symbol-value (intern (format "%s-spec" var))))
2309     (push (list type value spec) gnus-format-specs)
2310
2311     (pop-to-buffer "*Gnus Format*")
2312     (erase-buffer)
2313     (lisp-interaction-mode)
2314     (insert (pp-to-string spec))))
2315
2316
2317 (defun gnus-update-format-specifications (&optional force)
2318   "Update all (necessary) format specifications."
2319   ;; Make the indentation array.
2320   (gnus-make-thread-indent-array)
2321
2322   (when (or force
2323             (and (assq 'version gnus-format-specs)
2324                  (not (equal emacs-version
2325                              (cdr (assq 'version gnus-format-specs))))))
2326     (setq gnus-format-specs nil))
2327
2328   (let ((types '(summary summary-dummy group 
2329                            summary-mode group-mode article-mode))
2330         old-format new-format entry type val)
2331     (while types
2332       (setq type (pop types))
2333       (setq new-format (symbol-value
2334                         (intern (format "gnus-%s-line-format" type))))
2335       (setq entry (cdr (assq type gnus-format-specs)))
2336       (if (and entry
2337                (equal (car entry) new-format))
2338           (set (intern (format "gnus-%s-line-format-spec" type)) 
2339                (car (cdr entry)))
2340         (setq val
2341               (if (not (stringp new-format)) 
2342                   ;; This is a function call or something.
2343                   new-format
2344                 ;; This is a "real" format.
2345                 (gnus-parse-format
2346                  new-format
2347                  (symbol-value 
2348                   (intern (format "gnus-%s-line-format-alist"
2349                                   (if (eq type 'article-mode)
2350                                       'summary-mode type))))
2351                  (not (string-match "mode$" (symbol-name type))))))
2352         (set (intern (format "gnus-%s-line-format-spec" type)) val)
2353         (if entry 
2354             (setcar (cdr entry) val)
2355           (push (list type new-format val) gnus-format-specs)))))
2356       
2357   (gnus-update-group-mark-positions)
2358   (gnus-update-summary-mark-positions)
2359
2360   (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
2361            (not gnus-description-hashtb)
2362            gnus-read-active-file)
2363       (gnus-read-all-descriptions-files)))
2364
2365 (defun gnus-update-summary-mark-positions ()
2366   (save-excursion
2367     (let ((gnus-replied-mark 129)
2368           (gnus-score-below-mark 130)
2369           (gnus-score-over-mark 130)
2370           (thread nil)
2371           (gnus-visual nil)
2372           pos)
2373       (gnus-set-work-buffer)
2374       (gnus-summary-insert-line 
2375        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2376       (goto-char (point-min))
2377       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2378                                          (- (point) 2)))))
2379       (goto-char (point-min))
2380       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
2381                                           (- (point) 2))) pos))
2382       (goto-char (point-min))
2383       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
2384                                         (- (point) 2))) pos))
2385       (setq gnus-summary-mark-positions pos))))
2386
2387 (defun gnus-update-group-mark-positions ()
2388   (save-excursion
2389     (let ((gnus-process-mark 128)
2390           (gnus-group-marked '("dummy.group")))
2391       (gnus-set-active "dummy.group" '(0 . 0))
2392       (gnus-set-work-buffer)
2393       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2394       (goto-char (point-min))
2395       (setq gnus-group-mark-positions
2396             (list (cons 'process (and (search-forward "\200" nil t)
2397                                       (- (point) 2))))))))
2398
2399 (defvar gnus-mouse-face-0 'highlight)
2400 (defvar gnus-mouse-face-1 'highlight)
2401 (defvar gnus-mouse-face-2 'highlight)
2402 (defvar gnus-mouse-face-3 'highlight)
2403 (defvar gnus-mouse-face-4 'highlight)
2404
2405 (defun gnus-mouse-face-function (form type)
2406   `(put-text-property
2407     (point) (progn ,@form (point))
2408     gnus-mouse-face-prop 
2409     ,(if (equal type 0)
2410          'gnus-mouse-face
2411        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2412
2413 (defvar gnus-face-0 'bold)
2414 (defvar gnus-face-1 'italic)
2415 (defvar gnus-face-2 'bold-italic)
2416 (defvar gnus-face-3 'bold)
2417 (defvar gnus-face-4 'bold)
2418
2419 (defun gnus-face-face-function (form type)
2420   `(put-text-property
2421     (point) (progn ,@form (point))
2422     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2423
2424 (defun gnus-max-width-function (el max-width)
2425   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2426   (if (symbolp el)
2427       `(if (> (length ,el) ,max-width)
2428            (substring ,el 0 ,max-width)
2429          ,el)
2430     `(let ((val (eval ,el)))
2431        (if (numberp val)
2432            (setq val (int-to-string val)))
2433        (if (> (length val) ,max-width)
2434            (substring val 0 ,max-width)
2435          val))))
2436
2437 (defun gnus-parse-format (format spec-alist &optional insert)
2438   ;; This function parses the FORMAT string with the help of the
2439   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2440   ;; string.  If the FORMAT string contains the specifiers %( and %)
2441   ;; the text between them will have the mouse-face text property.
2442   (if (string-match 
2443        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2444        format)
2445       (gnus-parse-complex-format format spec-alist)
2446     ;; This is a simple format.
2447     (gnus-parse-simple-format format spec-alist insert)))
2448
2449 (defun gnus-parse-complex-format (format spec-alist)
2450   (save-excursion
2451     (gnus-set-work-buffer)
2452     (insert format)
2453     (goto-char (point-min))
2454     (while (re-search-forward "\"" nil t)
2455       (replace-match "\\\"" nil t))
2456     (goto-char (point-min))
2457     (insert "(\"")
2458     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2459       (let ((number (if (match-beginning 1)
2460                         (match-string 1) "0"))
2461             (delim (aref (match-string 2) 0)))
2462         (if (or (= delim ?\() (= delim ?\{))
2463             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2464                                    " " number " \""))
2465           (replace-match "\")\""))))
2466     (goto-char (point-max))
2467     (insert "\")")
2468     (goto-char (point-min))
2469     (let ((form (read (current-buffer))))
2470       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2471
2472 (defun gnus-complex-form-to-spec (form spec-alist)
2473   (delq nil
2474         (mapcar
2475          (lambda (sform)
2476            (if (stringp sform)
2477                (gnus-parse-simple-format sform spec-alist t)
2478              (funcall (intern (format "gnus-%s-face-function"
2479                                       (car sform)))
2480                       (gnus-complex-form-to-spec 
2481                        (cdr (cdr sform)) spec-alist)
2482                       (nth 1 sform))))
2483          form)))
2484     
2485 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2486   ;; This function parses the FORMAT string with the help of the
2487   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2488   ;; string.  
2489   (let ((max-width 0)
2490         spec flist fstring newspec elem beg result dontinsert)
2491     (save-excursion
2492       (gnus-set-work-buffer)
2493       (insert format)
2494       (goto-char (point-min))
2495       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2496                                 nil t)
2497         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2498               (setq newspec "%"
2499                     beg (1+ (match-beginning 0)))
2500           ;; First check if there are any specs that look anything like
2501           ;; "%12,12A", ie. with a "max width specification".  These have
2502           ;; to be treated specially.
2503           (if (setq beg (match-beginning 1))
2504               (setq max-width 
2505                     (string-to-int 
2506                      (buffer-substring 
2507                       (1+ (match-beginning 1)) (match-end 1))))
2508             (setq max-width 0)
2509             (setq beg (match-beginning 2)))
2510           ;; Find the specification from `spec-alist'.
2511           (unless (setq elem (cdr (assq spec spec-alist)))
2512             (setq elem '("*" ?s)))
2513           ;; Treat user defined format specifiers specially.
2514           (when (eq (car elem) 'gnus-tmp-user-defined)
2515             (setq elem
2516                   (list 
2517                    (list (intern (concat "gnus-user-format-function-"
2518                                          (match-string 3)))
2519                          'gnus-tmp-header) ?s))
2520             (delete-region (match-beginning 3) (match-end 3)))
2521           (if (not (zerop max-width))
2522               (let ((el (car elem)))
2523                 (cond ((= (car (cdr elem)) ?c) 
2524                        (setq el (list 'char-to-string el)))
2525                       ((= (car (cdr elem)) ?d)
2526                        (numberp el) (setq el (list 'int-to-string el))))
2527                 (setq flist (cons (gnus-max-width-function el max-width)
2528                                   flist))
2529                 (setq newspec ?s))
2530             (progn
2531               (setq flist (cons (car elem) flist))
2532               (setq newspec (car (cdr elem))))))
2533         ;; Remove the old specification (and possibly a ",12" string).
2534         (delete-region beg (match-end 2))
2535         ;; Insert the new specification.
2536         (goto-char beg)
2537         (insert newspec))
2538       (setq fstring (buffer-substring 1 (point-max))))
2539     ;; Do some postprocessing to increase efficiency.
2540     (setq 
2541      result
2542      (cond 
2543       ;; Emptyness.
2544       ((string= fstring "")
2545        nil)
2546       ;; Not a format string.
2547       ((not (string-match "%" fstring))
2548        (list fstring))
2549       ;; A format string with just a single string spec.
2550       ((string= fstring "%s")
2551        (list (car flist)))
2552       ;; A single character.
2553       ((string= fstring "%c")
2554        (list (car flist)))
2555       ;; A single number.
2556       ((string= fstring "%d")
2557        (setq dontinsert)
2558        (if insert
2559            (list `(princ ,(car flist)))
2560          (list `(int-to-string ,(car flist)))))
2561       ;; Just lots of chars and strings.
2562       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2563        (nreverse flist))
2564       ;; A single string spec at the beginning of the spec.
2565       ((string-match "\\`%[sc][^%]+\\'" fstring)
2566        (list (car flist) (substring fstring 2)))
2567       ;; A single string spec in the middle of the spec.
2568       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2569        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2570       ;; A single string spec in the end of the spec.
2571       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2572        (list (match-string 1 fstring) (car flist)))
2573       ;; A more complex spec.
2574       (t
2575        (list (cons 'format (cons fstring (nreverse flist)))))))
2576
2577     (if insert
2578         (when result
2579           (if dontinsert
2580               result
2581             (cons 'insert result)))
2582       (cond ((stringp result)
2583              result)
2584             ((consp result)
2585              (cons 'concat result))
2586             (t "")))))
2587
2588 (defun gnus-eval-format (format &optional alist props)
2589   "Eval the format variable FORMAT, using ALIST.
2590 If PROPS, insert the result."
2591   (let ((form (gnus-parse-format format alist props)))
2592     (if props
2593         (add-text-properties (point) (progn (eval form) (point)) props)
2594       (eval form))))
2595
2596 (defun gnus-remove-text-with-property (prop)
2597   "Delete all text in the current buffer with text property PROP."
2598   (save-excursion
2599     (goto-char (point-min))
2600     (while (not (eobp))
2601       (while (get-text-property (point) prop)
2602         (delete-char 1))
2603       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2604
2605 (defun gnus-set-work-buffer ()
2606   (if (get-buffer gnus-work-buffer)
2607       (progn
2608         (set-buffer gnus-work-buffer)
2609         (erase-buffer))
2610     (set-buffer (get-buffer-create gnus-work-buffer))
2611     (kill-all-local-variables)
2612     (buffer-disable-undo (current-buffer))
2613     (gnus-add-current-to-buffer-list)))
2614
2615 ;; Article file names when saving.
2616
2617 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2618   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2619 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2620 Otherwise, it is like ~/News/news/group/num."
2621   (let ((default
2622           (expand-file-name
2623            (concat (if (gnus-use-long-file-name 'not-save)
2624                        (gnus-capitalize-newsgroup newsgroup)
2625                      (gnus-newsgroup-directory-form newsgroup))
2626                    "/" (int-to-string (mail-header-number headers)))
2627            (or gnus-article-save-directory "~/News"))))
2628     (if (and last-file
2629              (string-equal (file-name-directory default)
2630                            (file-name-directory last-file))
2631              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2632         default
2633       (or last-file default))))
2634
2635 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2636   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2637 If variable `gnus-use-long-file-name' is non-nil, it is
2638 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2639   (let ((default
2640           (expand-file-name
2641            (concat (if (gnus-use-long-file-name 'not-save)
2642                        newsgroup
2643                      (gnus-newsgroup-directory-form newsgroup))
2644                    "/" (int-to-string (mail-header-number headers)))
2645            (or gnus-article-save-directory "~/News"))))
2646     (if (and last-file
2647              (string-equal (file-name-directory default)
2648                            (file-name-directory last-file))
2649              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2650         default
2651       (or last-file default))))
2652
2653 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2654   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2655 If variable `gnus-use-long-file-name' is non-nil, it is
2656 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2657   (or last-file
2658       (expand-file-name
2659        (if (gnus-use-long-file-name 'not-save)
2660            (gnus-capitalize-newsgroup newsgroup)
2661          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2662        (or gnus-article-save-directory "~/News"))))
2663
2664 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2665   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2666 If variable `gnus-use-long-file-name' is non-nil, it is
2667 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2668   (or last-file
2669       (expand-file-name
2670        (if (gnus-use-long-file-name 'not-save)
2671            newsgroup
2672          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2673        (or gnus-article-save-directory "~/News"))))
2674
2675 ;; For subscribing new newsgroup
2676
2677 (defun gnus-subscribe-hierarchical-interactive (groups)
2678   (let ((groups (sort groups 'string<))
2679         prefixes prefix start ans group starts)
2680     (while groups
2681       (setq prefixes (list "^"))
2682       (while (and groups prefixes)
2683         (while (not (string-match (car prefixes) (car groups)))
2684           (setq prefixes (cdr prefixes)))
2685         (setq prefix (car prefixes))
2686         (setq start (1- (length prefix)))
2687         (if (and (string-match "[^\\.]\\." (car groups) start)
2688                  (cdr groups)
2689                  (setq prefix 
2690                        (concat "^" (substring (car groups) 0 (match-end 0))))
2691                  (string-match prefix (car (cdr groups))))
2692             (progn
2693               (setq prefixes (cons prefix prefixes))
2694               (message "Descend hierarchy %s? ([y]nsq): " 
2695                        (substring prefix 1 (1- (length prefix))))
2696               (setq ans (read-char))
2697               (cond ((= ans ?n)
2698                      (while (and groups 
2699                                  (string-match prefix 
2700                                                (setq group (car groups))))
2701                        (setq gnus-killed-list 
2702                              (cons group gnus-killed-list))
2703                        (gnus-sethash group group gnus-killed-hashtb)
2704                        (setq groups (cdr groups)))
2705                      (setq starts (cdr starts)))
2706                     ((= ans ?s)
2707                      (while (and groups 
2708                                  (string-match prefix 
2709                                                (setq group (car groups))))
2710                        (gnus-sethash group group gnus-killed-hashtb)
2711                        (gnus-subscribe-alphabetically (car groups))
2712                        (setq groups (cdr groups)))
2713                      (setq starts (cdr starts)))
2714                     ((= ans ?q)
2715                      (while groups
2716                        (setq group (car groups))
2717                        (setq gnus-killed-list (cons group gnus-killed-list))
2718                        (gnus-sethash group group gnus-killed-hashtb)
2719                        (setq groups (cdr groups))))
2720                     (t nil)))
2721           (message "Subscribe %s? ([n]yq)" (car groups))
2722           (setq ans (read-char))
2723           (setq group (car groups))
2724           (cond ((= ans ?y)
2725                  (gnus-subscribe-alphabetically (car groups))
2726                  (gnus-sethash group group gnus-killed-hashtb))
2727                 ((= ans ?q)
2728                  (while groups
2729                    (setq group (car groups))
2730                    (setq gnus-killed-list (cons group gnus-killed-list))
2731                    (gnus-sethash group group gnus-killed-hashtb)
2732                    (setq groups (cdr groups))))
2733                 (t 
2734                  (setq gnus-killed-list (cons group gnus-killed-list))
2735                  (gnus-sethash group group gnus-killed-hashtb)))
2736           (setq groups (cdr groups)))))))
2737
2738 (defun gnus-subscribe-randomly (newsgroup)
2739   "Subscribe new NEWSGROUP by making it the first newsgroup."
2740   (gnus-subscribe-newsgroup newsgroup))
2741
2742 (defun gnus-subscribe-alphabetically (newgroup)
2743   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2744   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2745   (let ((groups (cdr gnus-newsrc-alist))
2746         before)
2747     (while (and (not before) groups)
2748       (if (string< newgroup (car (car groups)))
2749           (setq before (car (car groups)))
2750         (setq groups (cdr groups))))
2751     (gnus-subscribe-newsgroup newgroup before)))
2752
2753 (defun gnus-subscribe-hierarchically (newgroup)
2754   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2755   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2756   (save-excursion
2757     (set-buffer (find-file-noselect gnus-current-startup-file))
2758     (let ((groupkey newgroup)
2759           before)
2760       (while (and (not before) groupkey)
2761         (goto-char (point-min))
2762         (let ((groupkey-re
2763                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2764           (while (and (re-search-forward groupkey-re nil t)
2765                       (progn
2766                         (setq before (match-string 1))
2767                         (string< before newgroup)))))
2768         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2769         (setq groupkey
2770               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2771                   (substring groupkey (match-beginning 1) (match-end 1)))))
2772       (gnus-subscribe-newsgroup newgroup before))))
2773
2774 (defun gnus-subscribe-interactively (group)
2775   "Subscribe the new GROUP interactively.
2776 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2777 it is killed."
2778   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2779       (gnus-subscribe-hierarchically group)
2780     (push group gnus-killed-list)))
2781
2782 (defun gnus-subscribe-zombies (group)
2783   "Make the new GROUP into a zombie group."
2784   (push group gnus-zombie-list))
2785
2786 (defun gnus-subscribe-killed (group)
2787   "Make the new GROUP a killed group."
2788   (push group gnus-killed-list))
2789
2790 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2791   "Subscribe new NEWSGROUP.
2792 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2793 the first newsgroup."
2794   ;; We subscribe the group by changing its level to `subscribed'.
2795   (gnus-group-change-level 
2796    newsgroup gnus-level-default-subscribed
2797    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2798   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2799
2800 ;; For directories
2801
2802 (defun gnus-newsgroup-directory-form (newsgroup)
2803   "Make hierarchical directory name from NEWSGROUP name."
2804   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2805         (len (length newsgroup))
2806         idx)
2807     ;; If this is a foreign group, we don't want to translate the
2808     ;; entire name.  
2809     (if (setq idx (string-match ":" newsgroup))
2810         (aset newsgroup idx ?/)
2811       (setq idx 0))
2812     ;; Replace all occurrences of `.' with `/'.
2813     (while (< idx len)
2814       (if (= (aref newsgroup idx) ?.)
2815           (aset newsgroup idx ?/))
2816       (setq idx (1+ idx)))
2817     newsgroup))
2818
2819 (defun gnus-newsgroup-saveable-name (group)
2820   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2821   ;; with dots.
2822   (gnus-replace-chars-in-string group ?/ ?.))
2823
2824 (defun gnus-make-directory (dir)
2825   "Make DIRECTORY recursively."
2826   ;; Why don't we use `(make-directory dir 'parents)'? That's just one
2827   ;; of the many mysteries of the universe.
2828   (let* ((dir (expand-file-name dir default-directory))
2829          dirs err)
2830     (if (string-match "/$" dir)
2831         (setq dir (substring dir 0 (match-beginning 0))))
2832     ;; First go down the path until we find a directory that exists.
2833     (while (not (file-exists-p dir))
2834       (setq dirs (cons dir dirs))
2835       (string-match "/[^/]+$" dir)
2836       (setq dir (substring dir 0 (match-beginning 0))))
2837     ;; Then create all the subdirs.
2838     (while (and dirs (not err))
2839       (condition-case ()
2840           (make-directory (car dirs))
2841         (error (setq err t)))
2842       (setq dirs (cdr dirs)))
2843     ;; We return whether we were successful or not. 
2844     (not dirs)))
2845
2846 (defun gnus-capitalize-newsgroup (newsgroup)
2847   "Capitalize NEWSGROUP name."
2848   (and (not (zerop (length newsgroup)))
2849        (concat (char-to-string (upcase (aref newsgroup 0)))
2850                (substring newsgroup 1))))
2851
2852 ;; Var
2853
2854 (defun gnus-simplify-subject (subject &optional re-only)
2855   "Remove `Re:' and words in parentheses.
2856 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2857   (let ((case-fold-search t))           ;Ignore case.
2858     ;; Remove `Re:' and `Re^N:'.
2859     (if (string-match "^re:[ \t]*" subject)
2860         (setq subject (substring subject (match-end 0))))
2861     ;; Remove words in parentheses from end.
2862     (or re-only
2863         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2864           (setq subject (substring subject 0 (match-beginning 0)))))
2865     ;; Return subject string.
2866     subject))
2867
2868 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2869 ;; all whitespace.
2870 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2871 (defun gnus-simplify-buffer-fuzzy ()
2872   (goto-char (point-min))
2873   (while (or
2874           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
2875           (looking-at "^[[].*:[ \t].*[]]$"))
2876     (goto-char (point-min))
2877     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2878                               nil t)
2879       (replace-match "" t t))
2880     (goto-char (point-min))
2881     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
2882       (goto-char (match-end 0))
2883       (delete-char -1)
2884       (delete-region 
2885        (progn (goto-char (match-beginning 0)))
2886        (re-search-forward ":"))))
2887   (goto-char (point-min))
2888   (while (re-search-forward "[ \t\n]*([^()]*)[ \t]*$" nil t)
2889     (replace-match "" t t))
2890   (goto-char (point-min))
2891   (while (re-search-forward "[ \t]+" nil t)
2892     (replace-match " " t t))
2893   (goto-char (point-min))
2894   (while (re-search-forward "[ \t]+$" nil t)
2895     (replace-match "" t t))
2896   (goto-char (point-min))
2897   (while (re-search-forward "^[ \t]+" nil t)
2898     (replace-match "" t t))
2899   (goto-char (point-min))
2900   (if gnus-simplify-subject-fuzzy-regexp
2901       (if (listp gnus-simplify-subject-fuzzy-regexp)
2902           (let ((list gnus-simplify-subject-fuzzy-regexp))
2903             (while list
2904               (goto-char (point-min))
2905               (while (re-search-forward (car list) nil t)
2906                 (replace-match "" t t))
2907               (setq list (cdr list))))
2908         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2909           (replace-match "" t t)))))
2910
2911 (defun gnus-simplify-subject-fuzzy (subject)
2912   "Siplify a subject string fuzzily."
2913   (let ((case-fold-search t))
2914     (save-excursion
2915       (gnus-set-work-buffer)
2916       (insert subject)
2917       (inline (gnus-simplify-buffer-fuzzy))
2918       (buffer-string))))
2919
2920 ;; Add the current buffer to the list of buffers to be killed on exit. 
2921 (defun gnus-add-current-to-buffer-list ()
2922   (or (memq (current-buffer) gnus-buffer-list)
2923       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2924
2925 (defun gnus-string> (s1 s2)
2926   (not (or (string< s1 s2)
2927            (string= s1 s2))))
2928
2929 ;; Functions accessing headers.
2930 ;; Functions are more convenient than macros in some cases.
2931
2932 (defun gnus-header-number (header)
2933   (mail-header-number header))
2934
2935 (defun gnus-header-subject (header)
2936   (mail-header-subject header))
2937
2938 (defun gnus-header-from (header)
2939   (mail-header-from header))
2940
2941 (defun gnus-header-xref (header)
2942   (mail-header-xref header))
2943
2944 (defun gnus-header-lines (header)
2945   (mail-header-lines header))
2946
2947 (defun gnus-header-date (header)
2948   (mail-header-date header))
2949
2950 (defun gnus-header-id (header)
2951   (mail-header-id header))
2952
2953 (defun gnus-header-message-id (header)
2954   (mail-header-id header))
2955
2956 (defun gnus-header-chars (header)
2957   (mail-header-chars header))
2958
2959 (defun gnus-header-references (header)
2960   (mail-header-references header))
2961
2962 ;;; General various misc type functions.
2963
2964 (defun gnus-clear-system ()
2965   "Clear all variables and buffers."
2966   ;; Clear Gnus variables.
2967   (let ((variables gnus-variable-list))
2968     (while variables
2969       (set (car variables) nil)
2970       (setq variables (cdr variables))))
2971   ;; Clear other internal variables.
2972   (setq gnus-list-of-killed-groups nil
2973         gnus-have-read-active-file nil
2974         gnus-newsrc-alist nil
2975         gnus-newsrc-hashtb nil
2976         gnus-killed-list nil
2977         gnus-zombie-list nil
2978         gnus-killed-hashtb nil
2979         gnus-active-hashtb nil
2980         gnus-moderated-list nil
2981         gnus-description-hashtb nil
2982         gnus-newsgroup-headers nil
2983         gnus-newsgroup-name nil
2984         gnus-server-alist nil
2985         gnus-opened-servers nil
2986         gnus-current-select-method nil)
2987   ;; Reset any score variables.
2988   (and gnus-use-scoring (gnus-score-close))
2989   ;; Kill the startup file.
2990   (and gnus-current-startup-file
2991        (get-file-buffer gnus-current-startup-file)
2992        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2993   ;; Save any cache buffers.
2994   (and gnus-use-cache (gnus-cache-save-buffers))
2995   ;; Clear the dribble buffer.
2996   (gnus-dribble-clear)
2997   ;; Close down NoCeM.
2998   (and gnus-use-nocem (gnus-nocem-close))
2999   ;; Shut down the demons.
3000   (and gnus-use-demon (gnus-demon-cancel))
3001   ;; Kill global KILL file buffer.
3002   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
3003       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3004   (gnus-kill-buffer nntp-server-buffer)
3005   ;; Backlog.
3006   (and gnus-keep-backlog (gnus-backlog-shutdown))
3007   ;; Kill Gnus buffers.
3008   (while gnus-buffer-list
3009     (gnus-kill-buffer (car gnus-buffer-list))
3010     (setq gnus-buffer-list (cdr gnus-buffer-list))))
3011
3012 (defun gnus-windows-old-to-new (setting)
3013   ;; First we take care of the really, really old Gnus 3 actions.
3014   (if (symbolp setting)
3015       (setq setting 
3016             (cond ((memq setting '(SelectArticle))
3017                    'article)
3018                   ((memq setting '(SelectSubject ExpandSubject))
3019                    'summary)
3020                   ((memq setting '(SelectNewsgroup ExitNewsgroup))
3021                    'group)
3022                   (t setting))))
3023   (if (or (listp setting)
3024           (not (and gnus-window-configuration
3025                     (memq setting '(group summary article)))))
3026       setting
3027     (let* ((setting (if (eq setting 'group) 
3028                         (if (assq 'newsgroup gnus-window-configuration)
3029                             'newsgroup
3030                           'newsgroups) setting))
3031            (elem (car (cdr (assq setting gnus-window-configuration))))
3032            (total (apply '+ elem))
3033            (types '(group summary article))
3034            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3035            (i 0)
3036            perc
3037            out)
3038       (while (< i 3)
3039         (or (not (numberp (nth i elem)))
3040             (zerop (nth i elem))
3041             (progn
3042               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
3043               (setq out (cons (if (eq pbuf (nth i types))
3044                                   (vector (nth i types) perc 'point)
3045                                 (vector (nth i types) perc))
3046                               out))))
3047         (setq i (1+ i)))
3048       (list (nreverse out)))))
3049            
3050 (defun gnus-add-configuration (conf)
3051   (setq gnus-buffer-configuration 
3052         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3053                          gnus-buffer-configuration))))
3054
3055 (defun gnus-configure-frame (split &optional window)
3056   "Split WINDOW according to SPLIT."
3057   (unless window
3058     (setq window (get-buffer-window (current-buffer))))
3059   (select-window window)
3060   ;; This might be an old-stylee buffer config.
3061   (when (vectorp split)
3062     (setq split (append split nil)))
3063   (when (or (consp (car split))
3064             (vectorp (car split)))
3065     (push 1.0 split)
3066     (push 'vertical split))
3067   ;; The SPLIT might be something that is to be evaled to 
3068   ;; return a new SPLIT.
3069   (while (and (not (assq (car split) gnus-window-to-buffer))
3070               (gnus-functionp (car split)))
3071     (setq split (eval split)))
3072   (let* ((type (car split))
3073          (subs (cdr (cdr split)))
3074          (len (if (eq type 'horizontal) (window-width) (window-height) ))
3075          (total 0)
3076          s result new-win rest comp-subs size sub)
3077     (cond
3078      ;; Nothing to do here.
3079      ((null split))
3080      ;; This is a buffer to be selected.
3081      ((not (or (eq type 'horizontal) (eq type 'vertical)))
3082       (let ((buffer (cond ((stringp type) type)
3083                           (t (cdr (assq type gnus-window-to-buffer)))))
3084             buf)
3085         (unless buffer
3086           (error "Illegal buffer type: %s" type))
3087         (unless (setq buf (get-buffer (if (symbolp buffer)
3088                                           (symbol-value buffer) buffer)))
3089           (setq buf (get-buffer-create (if (symbolp buffer)
3090                                            (symbol-value buffer) buffer))))
3091         (switch-to-buffer buf)
3092         ;; We return the window if it has the `point' spec.
3093         (and (memq 'point split) window)))
3094      ;; This is a normal split.
3095      (t
3096       (when (> (length subs) 0)
3097         ;; First we have to compute the sizes of all new windows.
3098         (while subs
3099           (setq sub (append (pop subs) nil))
3100           (while (and (not (assq (car sub) gnus-window-to-buffer))
3101                       (gnus-functionp (car sub)))
3102             (setq sub (eval sub)))
3103           (when sub
3104             (push sub comp-subs)
3105             (setq size (cadar comp-subs))
3106             (cond ((equal size 1.0)
3107                    (setq rest (car comp-subs))
3108                    (setq s 0))
3109                   ((floatp size)
3110                    (setq s (floor (* size len))))
3111                   ((integerp size)
3112                    (setq s size))
3113                   (t
3114                    (error "Illegal size: %s" size)))
3115             ;; Try to make sure that we are inside the safe limits.
3116             (cond ((zerop s))
3117                   ((and (eq type 'horizontal)
3118                         (< s 10))
3119                    (setq s 10))
3120                   ((and (eq type 'vertical)
3121                         (< s 4))
3122                    (setq s 4)))
3123             (setcar (cdar comp-subs) s)
3124             (incf total s)))
3125         ;; Take care of the "1.0" spec.
3126         (if rest
3127             (setcar (cdr rest) (- len total))
3128           (error "No 1.0 specs in %s" split))
3129         ;; The we do the actual splitting in a nice recursive
3130         ;; fashion.
3131         (setq comp-subs (nreverse comp-subs))
3132         (while comp-subs
3133           (if (null (cdr comp-subs))
3134               (setq new-win window)
3135             (setq new-win
3136                   (split-window window (cadar comp-subs)
3137                                 (eq type 'horizontal))))
3138           (setq result (or (gnus-configure-frame 
3139                             (car comp-subs) window) result))
3140           (select-window new-win)
3141           (setq window new-win)
3142           (setq comp-subs (cdr comp-subs))))
3143       ;; Return the proper window, if any.
3144       (when result
3145         (select-window result))))))
3146
3147 (defun gnus-configure-windows (setting &optional force)
3148   (setq setting (gnus-windows-old-to-new setting))
3149   (let ((split (if (symbolp setting)
3150                    (car (cdr (assq setting gnus-buffer-configuration)))
3151                  setting))
3152         (in-buf (current-buffer))
3153         rule val w height hor ohor heights sub jump-buffer
3154         rel total to-buf all-visible)
3155
3156     (unless split
3157       (error "No such setting: %s" setting))
3158
3159     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p split)))
3160         ;; All the windows mentioned are already visible, so we just
3161         ;; put point in the assigned buffer, and do not touch the
3162         ;; winconf. 
3163         (select-window all-visible)
3164
3165       ;; Either remove all windows or just remove all Gnus windows.
3166       (if gnus-use-full-window
3167           (delete-other-windows)
3168         (gnus-remove-some-windows)
3169         (switch-to-buffer nntp-server-buffer))
3170
3171       (switch-to-buffer nntp-server-buffer)
3172       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3173
3174 (defun gnus-all-windows-visible-p (split)
3175   (when (vectorp split)
3176     (setq split (append split nil)))
3177   (when (or (consp (car split))
3178             (vectorp (car split)))
3179     (push 1.0 split)
3180     (push 'vertical split))
3181   ;; The SPLIT might be something that is to be evaled to 
3182   ;; return a new SPLIT.
3183   (while (and (not (assq (car split) gnus-window-to-buffer))
3184               (gnus-functionp (car split)))
3185     (setq split (eval split)))
3186   (let* ((type (elt split 0)))
3187     (cond
3188      ((null split)
3189       t)
3190      ((not (or (eq type 'horizontal) (eq type 'vertical)))
3191       (let ((buffer (cond ((stringp type) type)
3192                           (t (cdr (assq type gnus-window-to-buffer)))))
3193             win buf)
3194         (unless buffer
3195           (error "Illegal buffer type: %s" type))
3196         (when (setq buf (get-buffer (if (symbolp buffer) (symbol-value buffer) 
3197                                       buffer)))
3198           (setq win (get-buffer-window buf)))
3199         (when win
3200           (if (memq 'point split)
3201               win
3202             t))))
3203      (t
3204       (let ((n (mapcar 'gnus-all-windows-visible-p
3205                        (cdr (cdr split))))
3206             (win t))
3207         (while n
3208           (cond ((windowp (car n))
3209                  (setq win (car n)))
3210                 ((null (car n))
3211                  (setq win nil)))
3212           (setq n (cdr n)))
3213         win)))))
3214
3215 (defun gnus-window-top-edge (&optional window)
3216   (nth 1 (window-edges window)))
3217
3218 (defun gnus-remove-some-windows ()
3219   (let ((buffers gnus-window-to-buffer)
3220         buf bufs lowest-buf lowest)
3221     (save-excursion
3222       ;; Remove windows on all known Gnus buffers.
3223       (while buffers
3224         (setq buf (cdr (car buffers)))
3225         (if (symbolp buf)
3226             (setq buf (and (boundp buf) (symbol-value buf))))
3227         (and buf 
3228              (get-buffer-window buf)
3229              (progn
3230                (setq bufs (cons buf bufs))
3231                (pop-to-buffer buf)
3232                (if (or (not lowest)
3233                        (< (gnus-window-top-edge) lowest))
3234                    (progn
3235                      (setq lowest (gnus-window-top-edge))
3236                      (setq lowest-buf buf)))))
3237         (setq buffers (cdr buffers)))
3238       ;; Remove windows on *all* summary buffers.
3239       (let (wins)
3240         (walk-windows
3241          (lambda (win)
3242            (let ((buf (window-buffer win)))
3243              (if (string-match  "^\\*Summary" (buffer-name buf))
3244                  (progn
3245                    (setq bufs (cons buf bufs))
3246                    (pop-to-buffer buf)
3247                    (if (or (not lowest)
3248                            (< (gnus-window-top-edge) lowest))
3249                        (progn
3250                          (setq lowest-buf buf)
3251                          (setq lowest (gnus-window-top-edge))))))))))
3252       (and lowest-buf 
3253            (progn
3254              (pop-to-buffer lowest-buf)
3255              (switch-to-buffer nntp-server-buffer)))
3256       (while bufs
3257         (and (not (eq (car bufs) lowest-buf))
3258              (delete-windows-on (car bufs)))
3259         (setq bufs (cdr bufs))))))
3260                           
3261 (defun gnus-version ()
3262   "Version numbers of this version of Gnus."
3263   (interactive)
3264   (let ((methods gnus-valid-select-methods)
3265         (mess gnus-version)
3266         meth)
3267     ;; Go through all the legal select methods and add their version
3268     ;; numbers to the total version string.  Only the backends that are
3269     ;; currently in use will have their message numbers taken into
3270     ;; consideration. 
3271     (while methods
3272       (setq meth (intern (concat (car (car methods)) "-version")))
3273       (and (boundp meth)
3274            (stringp (symbol-value meth))
3275            (setq mess (concat mess "; " (symbol-value meth))))
3276       (setq methods (cdr methods)))
3277     (gnus-message 2 mess)))
3278
3279 (defun gnus-info-find-node ()
3280   "Find Info documentation of Gnus."
3281   (interactive)
3282   ;; Enlarge info window if needed.
3283   (let ((mode major-mode))
3284     (gnus-configure-windows 'info)
3285     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
3286
3287 (defun gnus-replace-chars-in-string (string &rest pairs)
3288   "Replace characters in STRING from FROM to TO."
3289   (let ((string (substring string 0))   ;Copy string.
3290         (len (length string))
3291         (idx 0)
3292         sym to)
3293     (or (zerop (% (length pairs) 2)) 
3294         (error "Odd number of translation pairs"))
3295     (setplist 'sym pairs)
3296     ;; Replace all occurrences of FROM with TO.
3297     (while (< idx len)
3298       (if (setq to (get 'sym (aref string idx)))
3299           (aset string idx to))
3300       (setq idx (1+ idx)))
3301     string))
3302
3303 (defun gnus-days-between (date1 date2)
3304   ;; Return the number of days between date1 and date2.
3305   (- (gnus-day-number date1) (gnus-day-number date2)))
3306
3307 (defun gnus-day-number (date)
3308   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3309                      (timezone-parse-date date))))
3310     (timezone-absolute-from-gregorian 
3311      (nth 1 dat) (nth 2 dat) (car dat))))
3312
3313 ;; Returns a floating point number that says how many seconds have
3314 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
3315 (defun gnus-seconds-since-epoch (date)
3316   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
3317                         (timezone-parse-date date)))
3318          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
3319                         (timezone-parse-time
3320                          (aref (timezone-parse-date date) 3))))
3321          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
3322                         (timezone-parse-date "Jan 1 12:00:00 1970")))
3323          (tday (- (timezone-absolute-from-gregorian 
3324                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
3325                   (timezone-absolute-from-gregorian 
3326                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
3327     (+ (nth 2 ttime)
3328        (* (nth 1 ttime) 60)
3329        (* 1.0 (nth 0 ttime) 60 60)
3330        (* 1.0 tday 60 60 24))))
3331
3332 (defun gnus-file-newer-than (file date)
3333   (let ((fdate (nth 5 (file-attributes file))))
3334     (or (> (car fdate) (car date))
3335         (and (= (car fdate) (car date))
3336              (> (nth 1 fdate) (nth 1 date))))))
3337
3338 (defun gnus-group-read-only-p (&optional group)
3339   "Check whether GROUP supports editing or not.
3340 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3341 that that variable is buffer-local to the summary buffers."
3342   (let ((group (or group gnus-newsgroup-name)))
3343     (not (gnus-check-backend-function 'request-replace-article group))))
3344
3345 (defun gnus-group-total-expirable-p (group)
3346   "Check whether GROUP is total-expirable or not."
3347   (let ((params (gnus-info-params (gnus-get-info group))))
3348     (or (memq 'total-expire params) 
3349         (cdr (assq 'total-expire params)) ; (total-expire . t)
3350         (and gnus-total-expirable-newsgroups ; Check var.
3351              (string-match gnus-total-expirable-newsgroups group)))))
3352
3353 (defun gnus-group-auto-expirable-p (group)
3354   "Check whether GROUP is total-expirable or not."
3355   (let ((params (gnus-info-params (gnus-get-info group))))
3356     (or (memq 'auto-expire params) 
3357         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3358         (and gnus-auto-expirable-newsgroups ; Check var.
3359              (string-match gnus-auto-expirable-newsgroups group)))))
3360
3361 (defsubst gnus-simplify-subject-fully (subject)
3362   "Simplify a subject string according to the user's wishes."
3363   (cond
3364    ((null gnus-summary-gather-subject-limit)
3365     (gnus-simplify-subject-re subject))
3366    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3367     (gnus-simplify-subject-fuzzy subject))
3368    ((numberp gnus-summary-gather-subject-limit)
3369     (gnus-limit-string subject gnus-summary-gather-subject-limit))
3370    (t
3371     subject)))
3372
3373 (defsubst gnus-subject-equal (s1 s2 &optional simple-first) 
3374   "Check whether two subjects are equal.  If optional argument
3375 simple-first is t, first argument is already simplified."
3376   (cond
3377    ((null simple-first)
3378     (equal (gnus-simplify-subject-fully s1)
3379            (gnus-simplify-subject-fully s2)))
3380    (t
3381     (equal s1
3382            (gnus-simplify-subject-fully s2)))))
3383
3384 ;; Returns a list of writable groups.
3385 (defun gnus-writable-groups ()
3386   (let ((alist gnus-newsrc-alist)
3387         groups)
3388     (while alist
3389       (or (gnus-group-read-only-p (car (car alist)))
3390           (setq groups (cons (car (car alist)) groups)))
3391       (setq alist (cdr alist)))
3392     (nreverse groups)))
3393
3394 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3395 ;; the echo area.
3396 (defun gnus-y-or-n-p (prompt)
3397   (prog1
3398       (y-or-n-p prompt)
3399     (message "")))
3400
3401 (defun gnus-yes-or-no-p (prompt)
3402   (prog1
3403       (yes-or-no-p prompt)
3404     (message "")))
3405
3406 ;; Check whether to use long file names.
3407 (defun gnus-use-long-file-name (symbol)
3408   ;; The variable has to be set...
3409   (and gnus-use-long-file-name
3410        ;; If it isn't a list, then we return t.
3411        (or (not (listp gnus-use-long-file-name))
3412            ;; If it is a list, and the list contains `symbol', we
3413            ;; return nil.  
3414            (not (memq symbol gnus-use-long-file-name)))))
3415
3416 ;; I suspect there's a better way, but I haven't taken the time to do
3417 ;; it yet. -erik selberg@cs.washington.edu
3418 (defun gnus-dd-mmm (messy-date)
3419   "Return a string like DD-MMM from a big messy string"
3420   (let ((datevec (timezone-parse-date messy-date)))
3421     (format "%2s-%s"
3422             (or (aref datevec 2) "??")
3423             (capitalize
3424              (or (car 
3425                   (nth (1- (string-to-number (aref datevec 1)))
3426                        timezone-months-assoc))
3427                  "???")))))
3428
3429 ;; Make a hash table (default and minimum size is 255).
3430 ;; Optional argument HASHSIZE specifies the table size.
3431 (defun gnus-make-hashtable (&optional hashsize)
3432   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3433
3434 ;; Make a number that is suitable for hashing; bigger than MIN and one
3435 ;; less than 2^x.
3436 (defun gnus-create-hash-size (min)
3437   (let ((i 1))
3438     (while (< i min)
3439       (setq i (* 2 i)))
3440     (1- i)))
3441
3442 ;; Show message if message has a lower level than `gnus-verbose'. 
3443 ;; Guide-line for numbers:
3444 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3445 ;; for things that take a long time, 7 - not very important messages
3446 ;; on stuff, 9 - messages inside loops.
3447 (defun gnus-message (level &rest args)
3448   (if (<= level gnus-verbose)
3449       (apply 'message args)
3450     ;; We have to do this format thingie here even if the result isn't
3451     ;; shown - the return value has to be the same as the return value
3452     ;; from `message'.
3453     (apply 'format args)))
3454
3455 (defun gnus-functionp (form)
3456   "Return non-nil if FORM is funcallable."
3457   (or (and (symbolp form) (fboundp form))
3458       (and (listp form) (eq (car form) 'lambda))))
3459
3460 ;; Generate a unique new group name.
3461 (defun gnus-generate-new-group-name (leaf)
3462   (let ((name leaf)
3463         (num 0))
3464     (while (gnus-gethash name gnus-newsrc-hashtb)
3465       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3466     name))
3467
3468 ;; Find out whether the gnus-visual TYPE is wanted.
3469 (defun gnus-visual-p (&optional type class)
3470   (and gnus-visual                      ; Has to be non-nil, at least.
3471        (if (not type)                   ; We don't care about type.
3472            gnus-visual
3473          (if (listp gnus-visual)        ; It's a list, so we check it.
3474              (or (memq type gnus-visual)
3475                  (memq class gnus-visual))
3476            t))))
3477
3478 (defun gnus-parent-id (references)
3479   "Return the last Message-ID in REFERENCES."
3480   (and references
3481        (string-match "\\(<[^<>]+>\\) *$" references)
3482        (substring references (match-beginning 1) (match-end 1))))
3483
3484 (defun gnus-ephemeral-group-p (group)
3485   "Say whether GROUP is ephemeral or not."
3486   (assoc 'quit-config (gnus-find-method-for-group group)))
3487
3488 (defun gnus-group-quit-config (group)
3489   "Return the quit-config of GROUP."
3490   (nth 1 (assoc 'quit-config (gnus-find-method-for-group group))))
3491
3492 (defun gnus-simplify-mode-line ()
3493   "Make mode lines a bit simpler."
3494   (setq mode-line-modified "-- ")
3495   (when (listp mode-line-format)
3496     (make-local-variable 'mode-line-format)
3497     (setq mode-line-format (copy-sequence mode-line-format))
3498     (and (equal (nth 3 mode-line-format) "   ")
3499          (setcar (nthcdr 3 mode-line-format) " "))))
3500
3501 ;;; List and range functions
3502
3503 (defun gnus-last-element (list)
3504   "Return last element of LIST."
3505   (while (cdr list)
3506     (setq list (cdr list)))
3507   (car list))
3508
3509 (defun gnus-copy-sequence (list)
3510   "Do a complete, total copy of a list."
3511   (if (and (consp list) (not (consp (cdr list))))
3512       (cons (car list) (cdr list))
3513     (mapcar (lambda (elem) (if (consp elem) 
3514                                (if (consp (cdr elem))
3515                                    (gnus-copy-sequence elem)
3516                                  (cons (car elem) (cdr elem)))
3517                              elem))
3518             list)))
3519
3520 (defun gnus-set-difference (list1 list2)
3521   "Return a list of elements of LIST1 that do not appear in LIST2."
3522   (let ((list1 (copy-sequence list1)))
3523     (while list2
3524       (setq list1 (delq (car list2) list1))
3525       (setq list2 (cdr list2)))
3526     list1))
3527
3528 (defun gnus-sorted-complement (list1 list2)
3529   "Return a list of elements of LIST1 that do not appear in LIST2.
3530 Both lists have to be sorted over <."
3531   (let (out)
3532     (if (or (null list1) (null list2))
3533         (or list1 list2)
3534       (while (and list1 list2)
3535         (cond ((= (car list1) (car list2))
3536                (setq list1 (cdr list1)
3537                      list2 (cdr list2)))
3538               ((< (car list1) (car list2))
3539                (setq out (cons (car list1) out))
3540                (setq list1 (cdr list1)))
3541               (t
3542                (setq out (cons (car list2) out))
3543                (setq list2 (cdr list2)))))
3544       (nconc (nreverse out) (or list1 list2)))))
3545
3546 (defun gnus-intersection (list1 list2)      
3547   (let ((result nil))
3548     (while list2
3549       (if (memq (car list2) list1)
3550           (setq result (cons (car list2) result)))
3551       (setq list2 (cdr list2)))
3552     result))
3553
3554 (defun gnus-sorted-intersection (list1 list2)
3555   ;; LIST1 and LIST2 have to be sorted over <.
3556   (let (out)
3557     (while (and list1 list2)
3558       (cond ((= (car list1) (car list2))
3559              (setq out (cons (car list1) out)
3560                    list1 (cdr list1)
3561                    list2 (cdr list2)))
3562             ((< (car list1) (car list2))
3563              (setq list1 (cdr list1)))
3564             (t
3565              (setq list2 (cdr list2)))))
3566     (nreverse out)))
3567
3568 (defun gnus-set-sorted-intersection (list1 list2)
3569   ;; LIST1 and LIST2 have to be sorted over <.
3570   ;; This function modifies LIST1.
3571   (let* ((top (cons nil list1))
3572          (prev top))
3573     (while (and list1 list2)
3574       (cond ((= (car list1) (car list2))
3575              (setq prev list1
3576                    list1 (cdr list1)
3577                    list2 (cdr list2)))
3578             ((< (car list1) (car list2))
3579              (setcdr prev (cdr list1))
3580              (setq list1 (cdr list1)))
3581             (t
3582              (setq list2 (cdr list2)))))
3583     (setcdr prev nil)
3584     (cdr top)))
3585
3586 (defun gnus-compress-sequence (numbers &optional always-list)
3587   "Convert list of numbers to a list of ranges or a single range.
3588 If ALWAYS-LIST is non-nil, this function will always release a list of
3589 ranges."
3590   (let* ((first (car numbers))
3591          (last (car numbers))
3592          result)
3593     (if (null numbers)
3594         nil
3595       (if (not (listp (cdr numbers)))
3596           numbers
3597         (while numbers
3598           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3599                 ((= (1+ last) (car numbers)) ;Still in sequence
3600                  (setq last (car numbers)))
3601                 (t                      ;End of one sequence
3602                  (setq result 
3603                        (cons (if (= first last) first
3604                                (cons first last)) result))
3605                  (setq first (car numbers))
3606                  (setq last  (car numbers))))
3607           (setq numbers (cdr numbers)))
3608         (if (and (not always-list) (null result))
3609             (if (= first last) (list first) (cons first last))
3610           (nreverse (cons (if (= first last) first (cons first last))
3611                           result)))))))
3612
3613 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3614 (defun gnus-uncompress-range (ranges)
3615   "Expand a list of ranges into a list of numbers.
3616 RANGES is either a single range on the form `(num . num)' or a list of
3617 these ranges."
3618   (let (first last result)
3619     (cond 
3620      ((null ranges)
3621       nil)
3622      ((not (listp (cdr ranges)))
3623       (setq first (car ranges))
3624       (setq last (cdr ranges))
3625       (while (<= first last)
3626         (setq result (cons first result))
3627         (setq first (1+ first)))
3628       (nreverse result))
3629      (t
3630       (while ranges
3631         (if (atom (car ranges))
3632             (if (numberp (car ranges))
3633                 (setq result (cons (car ranges) result)))
3634           (setq first (car (car ranges)))
3635           (setq last  (cdr (car ranges)))
3636           (while (<= first last)
3637             (setq result (cons first result))
3638             (setq first (1+ first))))
3639         (setq ranges (cdr ranges)))
3640       (nreverse result)))))
3641
3642 (defun gnus-add-to-range (ranges list)
3643   "Return a list of ranges that has all articles from both RANGES and LIST.
3644 Note: LIST has to be sorted over `<'."
3645   (if (not ranges)
3646       (gnus-compress-sequence list t)
3647     (setq list (copy-sequence list))
3648     (or (listp (cdr ranges))
3649         (setq ranges (list ranges)))
3650     (let ((out ranges)
3651           ilist lowest highest temp)
3652       (while (and ranges list)
3653         (setq ilist list)
3654         (setq lowest (or (and (atom (car ranges)) (car ranges))
3655                          (car (car ranges))))
3656         (while (and list (cdr list) (< (car (cdr list)) lowest))
3657           (setq list (cdr list)))
3658         (if (< (car ilist) lowest)
3659             (progn
3660               (setq temp list)
3661               (setq list (cdr list))
3662               (setcdr temp nil)
3663               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3664         (setq highest (or (and (atom (car ranges)) (car ranges))
3665                           (cdr (car ranges))))
3666         (while (and list (<= (car list) highest))
3667           (setq list (cdr list)))
3668         (setq ranges (cdr ranges)))
3669       (if list
3670           (setq out (nconc (gnus-compress-sequence list t) out)))
3671       (setq out (sort out (lambda (r1 r2) 
3672                             (< (or (and (atom r1) r1) (car r1))
3673                                (or (and (atom r2) r2) (car r2))))))
3674       (setq ranges out)
3675       (while ranges
3676         (if (atom (car ranges))
3677             (if (cdr ranges)
3678                 (if (atom (car (cdr ranges)))
3679                     (if (= (1+ (car ranges)) (car (cdr ranges)))
3680                         (progn
3681                           (setcar ranges (cons (car ranges) 
3682                                                (car (cdr ranges))))
3683                           (setcdr ranges (cdr (cdr ranges)))))
3684                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
3685                       (progn
3686                         (setcar (car (cdr ranges)) (car ranges))
3687                         (setcar ranges (car (cdr ranges)))
3688                         (setcdr ranges (cdr (cdr ranges)))))))
3689           (if (cdr ranges)
3690               (if (atom (car (cdr ranges)))
3691                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
3692                       (progn
3693                         (setcdr (car ranges) (car (cdr ranges)))
3694                         (setcdr ranges (cdr (cdr ranges)))))
3695                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
3696                     (progn
3697                       (setcdr (car ranges) (cdr (car (cdr ranges))))
3698                       (setcdr ranges (cdr (cdr ranges))))))))
3699         (setq ranges (cdr ranges)))
3700       out)))
3701
3702 (defun gnus-remove-from-range (ranges list)
3703   "Return a list of ranges that has all articles from LIST removed from RANGES.
3704 Note: LIST has to be sorted over `<'."
3705   ;; !!! This function shouldn't look like this, but I've got a headache.
3706   (gnus-compress-sequence 
3707    (gnus-sorted-complement
3708     (gnus-uncompress-range ranges) list)))
3709
3710 (defun gnus-member-of-range (number ranges)
3711   (if (not (listp (cdr ranges)))
3712       (and (>= number (car ranges)) 
3713            (<= number (cdr ranges)))
3714     (let ((not-stop t))
3715       (while (and ranges 
3716                   (if (numberp (car ranges))
3717                       (>= number (car ranges))
3718                     (>= number (car (car ranges))))
3719                   not-stop)
3720         (if (if (numberp (car ranges))
3721                 (= number (car ranges))
3722               (and (>= number (car (car ranges)))
3723                    (<= number (cdr (car ranges)))))
3724             (setq not-stop nil))
3725         (setq ranges (cdr ranges)))
3726       (not not-stop))))
3727
3728 (defun gnus-range-length (range)
3729   "Return the length RANGE would have if uncompressed."
3730   (length (gnus-uncompress-range range)))
3731
3732 (defun gnus-sublist-p (list sublist)
3733   "Test whether all elements in SUBLIST are members of LIST."
3734   (let ((sublistp t))
3735     (while sublist
3736       (unless (memq (pop sublist) list)
3737         (setq sublistp nil
3738               sublist nil)))
3739     sublistp))
3740
3741 \f
3742 ;;;
3743 ;;; Gnus group mode
3744 ;;;
3745
3746 (defvar gnus-group-mode-map nil)
3747 (defvar gnus-group-group-map nil)
3748 (defvar gnus-group-mark-map nil)
3749 (defvar gnus-group-list-map nil)
3750 (defvar gnus-group-sort-map nil)
3751 (defvar gnus-group-soup-map nil)
3752 (defvar gnus-group-sub-map nil)
3753 (defvar gnus-group-help-map nil)
3754 (defvar gnus-group-score-map nil)
3755 (put 'gnus-group-mode 'mode-class 'special)
3756
3757 (if gnus-group-mode-map
3758     nil
3759   (setq gnus-group-mode-map (make-keymap))
3760   (suppress-keymap gnus-group-mode-map)
3761   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3762   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3763   (define-key gnus-group-mode-map "\M- " 'gnus-group-unhidden-select-group)
3764   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3765   (define-key gnus-group-mode-map "\M-\r" 'gnus-group-quick-select-group)
3766   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3767   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3768   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3769   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3770   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3771   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3772   (define-key gnus-group-mode-map
3773     "\M-n" 'gnus-group-next-unread-group-same-level)
3774   (define-key gnus-group-mode-map 
3775     "\M-p" 'gnus-group-prev-unread-group-same-level)
3776   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3777   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3778   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3779   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3780   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3781   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3782   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3783   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3784   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3785   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3786   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3787   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3788   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3789   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3790   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3791   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3792   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3793   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3794   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3795   (define-key gnus-group-mode-map "\C-c\M-\C-a" 'gnus-group-description-apropos)
3796   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3797   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3798   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3799   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3800   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3801   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3802   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3803   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3804   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3805   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3806   (define-key gnus-group-mode-map "V" 'gnus-version)
3807   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3808   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3809   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3810   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3811   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3812   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3813   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3814   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3815   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3816   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3817   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3818   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3819   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3820   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3821   (define-key gnus-group-mode-map "t" 'gnus-topic-mode)
3822   (define-key gnus-group-mode-map "\C-c\M-g" 'gnus-activate-all-groups)
3823
3824   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3825   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3826   (define-prefix-command 'gnus-group-mark-map)
3827   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3828   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3829   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3830   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3831   (define-key gnus-group-mark-map "r" 'gnus-group-mark-regexp)
3832
3833   (define-prefix-command 'gnus-group-group-map)
3834   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3835   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3836   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3837   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3838   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3839   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3840   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3841   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3842   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3843   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3844   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3845   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3846   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3847   (define-key gnus-group-group-map "r" 'gnus-group-rename-group)
3848   (define-key gnus-group-group-map "\177" 'gnus-group-delete-group)
3849
3850   (define-prefix-command 'gnus-group-soup-map)
3851   (define-key gnus-group-group-map "s" 'gnus-group-soup-map)
3852   (define-key gnus-group-soup-map "b" 'gnus-group-brew-soup)
3853   (define-key gnus-group-soup-map "w" 'gnus-soup-save-areas)
3854   (define-key gnus-group-soup-map "s" 'gnus-soup-send-replies)
3855   (define-key gnus-group-soup-map "p" 'gnus-soup-pack-packet)
3856   (define-key gnus-group-soup-map "r" 'nnsoup-pack-replies)
3857
3858   (define-prefix-command 'gnus-group-sort-map)
3859   (define-key gnus-group-group-map "S" 'gnus-group-sort-map)
3860   (define-key gnus-group-sort-map "s" 'gnus-group-sort-groups)
3861   (define-key gnus-group-sort-map "a" 'gnus-group-sort-groups-by-alphabet)
3862   (define-key gnus-group-sort-map "u" 'gnus-group-sort-groups-by-unread)
3863   (define-key gnus-group-sort-map "l" 'gnus-group-sort-groups-by-level)
3864   (define-key gnus-group-sort-map "v" 'gnus-group-sort-groups-by-score)
3865   (define-key gnus-group-sort-map "r" 'gnus-group-sort-groups-by-rank)
3866   (define-key gnus-group-sort-map "m" 'gnus-group-sort-groups-by-method)
3867
3868   (define-prefix-command 'gnus-group-list-map)
3869   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3870   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3871   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3872   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3873   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3874   (define-key gnus-group-list-map "A" 'gnus-group-list-active)
3875   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3876   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3877   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3878   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3879   (define-key gnus-group-list-map "l" 'gnus-group-list-level)
3880
3881   (define-prefix-command 'gnus-group-score-map)
3882   (define-key gnus-group-mode-map "W" 'gnus-group-score-map)
3883   (define-key gnus-group-score-map "f" 'gnus-score-flush-cache)
3884
3885   (define-prefix-command 'gnus-group-help-map)
3886   (define-key gnus-group-mode-map "H" 'gnus-group-help-map)
3887   (define-key gnus-group-help-map "f" 'gnus-group-fetch-faq)
3888
3889   (define-prefix-command 'gnus-group-sub-map)
3890   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3891   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3892   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3893   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3894   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3895   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3896   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3897   (define-key gnus-group-sub-map "\C-k" 'gnus-group-kill-level)
3898   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3899
3900 (defun gnus-group-mode ()
3901   "Major mode for reading news.
3902
3903 All normal editing commands are switched off.
3904 \\<gnus-group-mode-map>
3905 The group buffer lists (some of) the groups available.  For instance,
3906 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3907 lists all zombie groups. 
3908
3909 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3910 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3911
3912 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3913
3914 The following commands are available:
3915
3916 \\{gnus-group-mode-map}"
3917   (interactive)
3918   (when (and menu-bar-mode
3919              (gnus-visual-p 'group-menu 'menu))
3920     (gnus-group-make-menu-bar))
3921   (kill-all-local-variables)
3922   (gnus-simplify-mode-line)
3923   (setq major-mode 'gnus-group-mode)
3924   (setq mode-name "Group")
3925   (gnus-group-set-mode-line)
3926   (setq mode-line-process nil)
3927   (use-local-map gnus-group-mode-map)
3928   (buffer-disable-undo (current-buffer))
3929   (setq truncate-lines t)
3930   (setq buffer-read-only t)
3931   (run-hooks 'gnus-group-mode-hook))
3932
3933 (defun gnus-mouse-pick-group (e)
3934   "Enter the group under the mouse pointer."
3935   (interactive "e")
3936   (mouse-set-point e)
3937   (gnus-group-read-group nil))
3938
3939 ;; Look at LEVEL and find out what the level is really supposed to be.
3940 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3941 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3942 (defun gnus-group-default-level (&optional level number-or-nil)
3943   (cond  
3944    (gnus-group-use-permanent-levels
3945     (setq gnus-group-default-list-level 
3946           (or level gnus-group-default-list-level))
3947     (or gnus-group-default-list-level gnus-level-subscribed))
3948    (number-or-nil
3949     level)
3950    (t
3951     (or level gnus-group-default-list-level gnus-level-subscribed))))
3952   
3953 ;;;###autoload
3954 (defun gnus-slave-no-server (&optional arg)
3955   "Read network news as a slave, without connecting to local server"
3956   (interactive "P")
3957   (gnus-no-server arg t))
3958
3959 ;;;###autoload
3960 (defun gnus-no-server (&optional arg slave)
3961   "Read network news.
3962 If ARG is a positive number, Gnus will use that as the
3963 startup level.  If ARG is nil, Gnus will be started at level 2. 
3964 If ARG is non-nil and not a positive number, Gnus will
3965 prompt the user for the name of an NNTP server to use.
3966 As opposed to `gnus', this command will not connect to the local server."
3967   (interactive "P")
3968   (make-local-variable 'gnus-group-use-permanent-levels)
3969   (setq gnus-group-use-permanent-levels t)
3970   (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
3971
3972 ;;;###autoload
3973 (defun gnus-slave (&optional arg)
3974   "Read news as a slave."
3975   (interactive "P")
3976   (gnus arg nil 'slave))
3977
3978 ;;;###autoload
3979 (defun gnus (&optional arg dont-connect slave)
3980   "Read network news.
3981 If ARG is non-nil and a positive number, Gnus will use that as the
3982 startup level.  If ARG is non-nil and not a positive number, Gnus will
3983 prompt the user for the name of an NNTP server to use."
3984   (interactive "P")
3985   (if (get-buffer gnus-group-buffer)
3986       (progn
3987         (switch-to-buffer gnus-group-buffer)
3988         (gnus-group-get-new-news))
3989
3990     (gnus-clear-system)
3991     (nnheader-init-server-buffer)
3992     (gnus-read-init-file)
3993     (setq gnus-slave slave)
3994
3995     (gnus-group-setup-buffer)
3996     (let ((buffer-read-only nil))
3997       (erase-buffer)
3998       (if (not gnus-inhibit-startup-message)
3999           (progn
4000             (gnus-group-startup-message)
4001             (sit-for 0))))
4002     
4003     (let ((level (and arg (numberp arg) (> arg 0) arg))
4004           did-connect)
4005       (unwind-protect
4006           (progn
4007             (or dont-connect 
4008                 (setq did-connect
4009                       (gnus-start-news-server (and arg (not level))))))
4010         (if (and (not dont-connect) 
4011                  (not did-connect))
4012             (gnus-group-quit)
4013           (run-hooks 'gnus-startup-hook)
4014           ;; NNTP server is successfully open. 
4015
4016           ;; Find the current startup file name.
4017           (setq gnus-current-startup-file 
4018                 (gnus-make-newsrc-file gnus-startup-file))
4019
4020           ;; Read the dribble file.
4021           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4022
4023           (gnus-summary-make-display-table)
4024           ;; Do the actual startup.
4025           (gnus-setup-news nil level)
4026           ;; Generate the group buffer.
4027           (gnus-group-list-groups level)
4028           (gnus-configure-windows 'group)
4029           (gnus-group-set-mode-line))))))
4030
4031 (defun gnus-unload ()
4032   "Unload all Gnus features."
4033   (interactive)
4034   (or (boundp 'load-history)
4035       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4036   (let ((history load-history)
4037         feature)
4038     (while history
4039       (and (string-match "^gnus" (car (car history)))
4040            (setq feature (cdr (assq 'provide (car history))))
4041            (unload-feature feature 'force))
4042       (setq history (cdr history)))))
4043
4044 (defun gnus-compile ()
4045   "Byte-compile the user-defined format specs."
4046   (interactive)
4047   (let ((entries gnus-format-specs)
4048         entry gnus-tmp-func)
4049     (save-excursion
4050       (gnus-message 7 "Compiling format specs...")
4051
4052       (while entries
4053         (setq entry (pop entries))
4054         (if (eq (car entry) 'version)
4055             (setq gnus-format-specs (delq entry gnus-format-specs))
4056           (when (and (listp (caddr entry))
4057                      (not (eq 'byte-code (caaddr entry))))
4058             (fset 'gnus-tmp-func 
4059                   `(lambda () ,(caddr entry)))
4060             (byte-compile 'gnus-tmp-func)
4061             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4062
4063       (push (cons 'version emacs-version) gnus-format-specs)
4064
4065       (gnus-message 7 "Compiling user specs...done"))))
4066
4067 (defun gnus-indent-rigidly (start end arg)
4068   "Indent rigidly using only spaces and no tabs."
4069   (save-excursion
4070     (save-restriction
4071       (narrow-to-region start end)
4072       (indent-rigidly start end arg)
4073       (goto-char (point-min))
4074       (while (search-forward "\t" nil t)
4075         (replace-match "        " t t)))))
4076
4077 (defun gnus-group-startup-message (&optional x y)
4078   "Insert startup message in current buffer."
4079   ;; Insert the message.
4080   (erase-buffer)
4081   (insert
4082    (format "              %s
4083           _    ___ _             _      
4084           _ ___ __ ___  __    _ ___     
4085           __   _     ___    __  ___     
4086               _           ___     _     
4087              _  _ __             _      
4088              ___   __            _      
4089                    __           _       
4090                     _      _   _        
4091                    _      _    _        
4092                       _  _    _         
4093                   __  ___               
4094                  _   _ _     _          
4095                 _   _                   
4096               _    _                    
4097              _    _                     
4098             _                         
4099           __                             
4100
4101
4102            ""))
4103   ;; And then hack it.
4104   (gnus-indent-rigidly (point-min) (point-max) 
4105                        (/ (max (- (window-width) (or x 46)) 0) 2))
4106   (goto-char (point-min))
4107   (forward-line 1)
4108   (let* ((pheight (count-lines (point-min) (point-max)))
4109          (wheight (window-height))
4110          (rest (- wheight pheight)))
4111     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4112   ;; Fontify some.
4113   (goto-char (point-min))
4114   (and (search-forward "Praxis" nil t)
4115        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4116   (goto-char (point-min))
4117   (let* ((mode-string (gnus-group-set-mode-line)))
4118     (setq mode-line-buffer-identification 
4119           (list (concat gnus-version (substring (car mode-string) 4))))
4120     (set-buffer-modified-p t)))
4121
4122 (defun gnus-group-startup-message-old (&optional x y)
4123   "Insert startup message in current buffer."
4124   ;; Insert the message.
4125   (erase-buffer)
4126   (insert
4127    (format "
4128      %s
4129            A newsreader 
4130       for GNU Emacs
4131
4132         Based on GNUS 
4133              written by 
4134      Masanobu UMEDA
4135
4136        A Praxis Release
4137       larsi@ifi.uio.no
4138
4139            gnus-version))
4140   ;; And then hack it.
4141   ;; 18 is the longest line.
4142   (indent-rigidly (point-min) (point-max) 
4143                   (/ (max (- (window-width) (or x 28)) 0) 2))
4144   (goto-char (point-min))
4145   ;; +4 is fuzzy factor.
4146   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
4147
4148   ;; Fontify some.
4149   (goto-char (point-min))
4150   (search-forward "Praxis")
4151   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
4152   (goto-char (point-min)))
4153
4154 (defun gnus-group-setup-buffer ()
4155   (or (get-buffer gnus-group-buffer)
4156       (progn
4157         (switch-to-buffer gnus-group-buffer)
4158         (gnus-add-current-to-buffer-list)
4159         (gnus-group-mode)
4160         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4161
4162 (defun gnus-group-list-groups (&optional level unread lowest)
4163   "List newsgroups with level LEVEL or lower that have unread articles.
4164 Default is all subscribed groups.
4165 If argument UNREAD is non-nil, groups with no unread articles are also
4166 listed." 
4167   (interactive (list (if current-prefix-arg
4168                          (prefix-numeric-value current-prefix-arg)
4169                        (or
4170                         (gnus-group-default-level nil t)
4171                         gnus-group-default-list-level
4172                         gnus-level-subscribed))))
4173   (or level
4174       (setq level (car gnus-group-list-mode)
4175             unread (cdr gnus-group-list-mode)))
4176   (setq level (gnus-group-default-level level))
4177   (gnus-group-setup-buffer)             ;May call from out of group buffer
4178   (gnus-update-format-specifications)
4179   (let ((case-fold-search nil)
4180         (group (gnus-group-group-name)))
4181     (funcall gnus-group-prepare-function level unread lowest)
4182     (if (zerop (buffer-size))
4183         (gnus-message 5 gnus-no-groups-message)
4184       (goto-char (point-min))
4185       (if (not group)
4186           ;; Go to the first group with unread articles.
4187           (gnus-group-search-forward nil nil nil t)
4188         ;; Find the right group to put point on.  If the current group
4189         ;; has disapeared in the new listing, try to find the next
4190         ;; one.  If no next one can be found, just leave point at the
4191         ;; first newsgroup in the buffer.
4192         (if (not (gnus-goto-char
4193                   (text-property-any
4194                    (point-min) (point-max) 
4195                    'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4196             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
4197               (while (and newsrc
4198                           (not (gnus-goto-char 
4199                                 (text-property-any 
4200                                  (point-min) (point-max) 'gnus-group 
4201                                  (gnus-intern-safe 
4202                                   (car (car newsrc)) gnus-active-hashtb)))))
4203                 (setq newsrc (cdr newsrc)))
4204               (or newsrc (progn (goto-char (point-max))
4205                                 (forward-line -1))))))
4206       ;; Adjust cursor point.
4207       (gnus-group-position-point))))
4208
4209 (defun gnus-group-list-level (level &optional all)
4210   "List groups on LEVEL.
4211 If ALL (the prefix), also list groups that have no unread articles."
4212   (interactive "nList groups on level: \nP")
4213   (gnus-group-list-groups level all level))
4214
4215 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
4216   "List all newsgroups with unread articles of level LEVEL or lower.
4217 If ALL is non-nil, list groups that have no unread articles.
4218 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4219 If REGEXP, only list groups matching REGEXP."
4220   (set-buffer gnus-group-buffer)
4221   (setq gnus-topic-indentation "")
4222   (let ((buffer-read-only nil)
4223         (newsrc (cdr gnus-newsrc-alist))
4224         (lowest (or lowest 1))
4225         info clevel unread group params)
4226     (erase-buffer)
4227     (if (< lowest gnus-level-zombie)
4228         ;; List living groups.
4229         (while newsrc
4230           (setq info (car newsrc)
4231                 group (gnus-info-group info)
4232                 params (gnus-info-params info)
4233                 newsrc (cdr newsrc)
4234                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4235           (and unread                   ; This group might be bogus
4236                (or (not regexp)
4237                    (string-match regexp group))
4238                (<= (setq clevel (gnus-info-level info)) level) 
4239                (>= clevel lowest)
4240                (or all                  ; We list all groups?
4241                    (and gnus-group-list-inactive-groups
4242                         (eq unread t))  ; We list unactivated groups
4243                    (> unread 0)         ; We list groups with unread articles
4244                    (cdr (assq 'tick (gnus-info-marks info)))
4245                                         ; And groups with tickeds
4246                    ;; Check for permanent visibility.
4247                    (and gnus-permanently-visible-groups
4248                         (string-match gnus-permanently-visible-groups
4249                                       group))
4250                    (memq 'visible params)
4251                    (cdr (assq 'visible params)))
4252                (gnus-group-insert-group-line 
4253                 group (gnus-info-level info) 
4254                 (gnus-info-marks info) unread (gnus-info-method info)))))
4255       
4256     ;; List dead groups.
4257     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4258          (gnus-group-prepare-flat-list-dead 
4259           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
4260           gnus-level-zombie ?Z
4261           regexp))
4262     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4263          (gnus-group-prepare-flat-list-dead 
4264           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
4265           gnus-level-killed ?K regexp))
4266
4267     (gnus-group-set-mode-line)
4268     (setq gnus-group-list-mode (cons level all))
4269     (run-hooks 'gnus-group-prepare-hook)))
4270
4271 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4272   ;; List zombies and killed lists somehwat faster, which was
4273   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4274   ;; this by ignoring the group format specification altogether.
4275   (let (group beg)
4276     (if regexp
4277         ;; This loop is used when listing groups that match some
4278         ;; regexp. 
4279         (while groups
4280           (setq group (pop groups))
4281           (when (string-match regexp group)
4282             (add-text-properties 
4283              (point) (prog1 (1+ (point))
4284                        (insert " " mark "     *: " group "\n"))
4285              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4286                    'gnus-unread t
4287                    'gnus-level level))))
4288       ;; This loop is used when listing all groups.
4289       (while groups
4290         (add-text-properties 
4291          (point) (prog1 (1+ (point))
4292                    (insert " " mark "     *: " 
4293                            (setq group (pop groups)) "\n"))
4294          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4295                'gnus-unread t
4296                'gnus-level level))))))
4297
4298 (defmacro gnus-group-real-name (group)
4299   "Find the real name of a foreign newsgroup."
4300   `(let ((gname ,group))
4301      (if (string-match ":[^:]+$" gname)
4302          (substring gname (1+ (match-beginning 0)))
4303        gname)))
4304
4305 (defsubst gnus-server-add-address (method)
4306   (let ((method-name (symbol-name (car method))))
4307     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4308              (not (assq (intern (concat method-name "-address")) method)))
4309         (append method (list (list (intern (concat method-name "-address"))
4310                                    (nth 1 method))))
4311       method)))
4312
4313 (defsubst gnus-server-get-method (group method)
4314   ;; Input either a server name, and extended server name, or a
4315   ;; select method, and return a select method. 
4316   (cond ((stringp method)
4317          (gnus-server-to-method method))
4318         ((and (stringp (car method)) group)
4319          (gnus-server-extend-method group method))
4320         (t
4321          (gnus-server-add-address method))))
4322
4323 (defun gnus-server-to-method (server)
4324   "Map virtual server names to select methods."
4325   (or (and (equal server "native") gnus-select-method)
4326       (cdr (assoc server gnus-server-alist))))
4327
4328 (defun gnus-group-prefixed-name (group method)
4329   "Return the whole name from GROUP and METHOD."
4330   (and (stringp method) (setq method (gnus-server-to-method method)))
4331   (concat (format "%s" (car method))
4332           (if (and 
4333                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
4334                (not (string= (nth 1 method) "")))
4335               (concat "+" (nth 1 method)))
4336           ":" group))
4337
4338 (defun gnus-group-real-prefix (group)
4339   "Return the prefix of the current group name."
4340   (if (string-match "^[^:]+:" group)
4341       (substring group 0 (match-end 0))
4342     ""))
4343
4344 (defun gnus-group-method-name (group)
4345   "Return the method used for selecting GROUP."
4346   (let ((prefix (gnus-group-real-prefix group)))
4347     (if (equal prefix "")
4348         gnus-select-method
4349       (if (string-match "^[^\\+]+\\+" prefix)
4350           (list (intern (substring prefix 0 (1- (match-end 0))))
4351                 (substring prefix (match-end 0) (1- (length prefix))))
4352         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
4353
4354 (defsubst gnus-secondary-method-p (method)
4355   "Return whether METHOD is a secondary select method."
4356   (let ((methods gnus-secondary-select-methods)
4357         (gmethod (gnus-server-get-method nil method)))
4358     (while (and methods
4359                 (not (equal (gnus-server-get-method nil (car methods)) 
4360                             gmethod)))
4361       (setq methods (cdr methods)))
4362     methods))
4363
4364 (defun gnus-group-foreign-p (group)
4365   "Say whether a group is foreign or not."
4366   (and (not (gnus-group-native-p group))
4367        (not (gnus-group-secondary-p group))))
4368
4369 (defun gnus-group-native-p (group)
4370   "Say whether the group is native or not."
4371   (not (string-match ":" group)))
4372
4373 (defun gnus-group-secondary-p (group)
4374   "Say whether the group is secondary or not."
4375   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4376
4377 (defun gnus-group-get-parameter (group &optional symbol)
4378   "Returns the group parameters for GROUP.
4379 If SYMBOL, return the value of that symbol in the group parameters."
4380   (let ((params (gnus-info-params (gnus-get-info group))))
4381     (if symbol
4382         (gnus-group-parameter-value params symbol)
4383       params)))
4384
4385 (defun gnus-group-parameter-value (params symbol)
4386   "Return the value of SYMBOL in group PARAMS."
4387   (or (car (memq symbol params))        ; It's either a simple symbol
4388       (cdr (assq symbol params))))      ; or a cons.
4389
4390 (defun gnus-group-add-parameter (group param)
4391   "Add parameter PARAM to GROUP."
4392   (let ((info (gnus-get-info group)))
4393     (if (not info)
4394         () ; This is a dead group.  We just ignore it.
4395       ;; Cons the new param to the old one and update.
4396       (gnus-group-set-info (cons param (gnus-info-params info)) 
4397                            group 'params))))
4398
4399 (defun gnus-group-add-score (group &optional score)
4400   "Add SCORE to the GROUP score.  
4401 If SCORE is nil, add 1 to the score of GROUP."
4402   (let ((info (gnus-get-info group)))
4403     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4404
4405 (defun gnus-summary-bubble-group ()
4406   "Increase the score of the current group.
4407 This is a handy function to add to `gnus-summary-exit-hook' to
4408 increase the score of each group you read."
4409   (gnus-group-add-score gnus-newsgroup-name))
4410
4411 (defun gnus-group-set-info (info &optional method-only-group part)
4412   (let* ((entry (gnus-gethash
4413                  (or method-only-group (gnus-info-group info))
4414                  gnus-newsrc-hashtb))
4415          (part-info info)
4416          (info (if method-only-group (nth 2 entry) info)))
4417     (when method-only-group
4418       (unless entry
4419         (error "Trying to change non-existent group %s" method-only-group))
4420       ;; We have recevied parts of the actual group info - either the
4421       ;; select method or the group parameters.  We first check
4422       ;; whether we have to extend the info, and if so, do that.
4423       (let ((len (length info))
4424             (total (if (eq part 'method) 5 6)))
4425         (when (< len total)
4426           (setcdr (nthcdr (1- len) info)
4427                   (make-list (- total len) nil)))
4428         ;; Then we enter the new info.
4429         (setcar (nthcdr (1- total) info) part-info)))
4430     (unless entry
4431       ;; This is a new group, so we just create it.
4432       (save-excursion
4433         (set-buffer gnus-group-buffer)
4434         (if (gnus-info-method info)
4435             ;; It's a foreign group...
4436             (gnus-group-make-group 
4437              (gnus-group-real-name (gnus-info-group info))
4438              (prin1-to-string (car (gnus-info-method info)))
4439              (nth 1 (gnus-info-method info)))
4440           ;; It's a native group.
4441           (gnus-group-make-group (gnus-info-group info)))
4442         (gnus-message 6 "Note: New group created")
4443         (setq entry 
4444               (gnus-gethash (gnus-group-prefixed-name 
4445                              (gnus-group-real-name (gnus-info-group info))
4446                              (or (gnus-info-method info) gnus-select-method))
4447                             gnus-newsrc-hashtb))))
4448     ;; Whether it was a new group or not, we now have the entry, so we
4449     ;; can do the update.
4450     (if entry
4451         (progn
4452           (setcar (nthcdr 2 entry) info)
4453           (when (and (not (eq (car entry) t)) 
4454                      (gnus-active (gnus-info-group info)))
4455             (let ((marked (gnus-info-marks info)))
4456               (setcar entry (length (gnus-list-of-unread-articles 
4457                                      (car info)))))))
4458       (error "No such group: %s" (gnus-info-group info)))))
4459
4460 (defun gnus-group-set-method-info (group select-method)
4461   (gnus-group-set-info select-method group 'method))
4462
4463 (defun gnus-group-set-params-info (group params)
4464   (gnus-group-set-info params group 'params))
4465
4466 (defun gnus-group-update-group-line ()
4467   "Update the current line in the group buffer."
4468   (let* ((buffer-read-only nil)
4469          (group (gnus-group-group-name))
4470          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4471     (and entry 
4472          (not (gnus-ephemeral-group-p group))
4473          (gnus-dribble-enter 
4474           (concat "(gnus-group-set-info '" 
4475                   (prin1-to-string (nth 2 entry)) ")")))
4476     (gnus-delete-line)
4477     (gnus-group-insert-group-line-info group)
4478     (forward-line -1)
4479     (gnus-group-position-point)))
4480
4481 (defun gnus-group-insert-group-line-info (group)
4482   "Insert GROUP on the current line."
4483   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
4484         active info)
4485     (if entry
4486         (progn
4487           ;; (Un)subscribed group.
4488           (setq info (nth 2 entry))
4489           (gnus-group-insert-group-line 
4490            group (gnus-info-level info) (gnus-info-marks info)
4491            (or (car entry) t) (gnus-info-method info)))
4492       ;; This group is dead.
4493       (gnus-group-insert-group-line 
4494        group 
4495        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4496        nil 
4497        (if (setq active (gnus-active group))
4498            (- (1+ (cdr active)) (car active)) 0) 
4499        nil))))
4500
4501 ;; Dummy function redefined when running under XEmacs.
4502 (defalias 'gnus-group-remove-excess-properties 'ignore)
4503
4504 (defun gnus-group-insert-group-line 
4505   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4506                   gnus-tmp-method)
4507   "Insert a group line in the group buffer."
4508   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4509          (gnus-tmp-number-total 
4510           (if gnus-tmp-active 
4511               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4512             0))
4513          (gnus-tmp-number-of-unread 
4514           (if (numberp number) (int-to-string (max 0 number))
4515             "*"))
4516          (gnus-tmp-number-of-read
4517           (if (numberp number)
4518               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4519             "*"))
4520          (gnus-tmp-subscribed
4521           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4522                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4523                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4524                 (t ?K)))
4525          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4526          (gnus-tmp-newsgroup-description 
4527           (if gnus-description-hashtb
4528               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4529             ""))
4530          (gnus-tmp-moderated
4531           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4532          (gnus-tmp-moderated-string 
4533           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4534          (gnus-tmp-method
4535           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4536          (gnus-tmp-news-server (or (car (cdr gnus-tmp-method)) ""))
4537          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4538          (gnus-tmp-news-method-string 
4539           (if gnus-tmp-method
4540               (format "(%s:%s)" (car gnus-tmp-method)
4541                       (car (cdr gnus-tmp-method))) ""))
4542          (gnus-tmp-marked-mark 
4543           (if (and (numberp number) 
4544                    (zerop number)
4545                    (cdr (assq 'tick gnus-tmp-marked)))
4546               ?* ? ))
4547          (gnus-tmp-number
4548           (cond ((eq number t) "*" )
4549                 ((numberp number) (int-to-string number))
4550                 (t number)))
4551          (gnus-tmp-process-marked
4552           (if (member gnus-tmp-group gnus-group-marked)
4553               gnus-process-mark ? ))
4554          (buffer-read-only nil)
4555          header)                        ; passed as parameter to user-funcs.
4556     (beginning-of-line)
4557     (add-text-properties
4558      (point)
4559      (prog1 (1+ (point))
4560        ;; Insert the text.
4561        (eval gnus-group-line-format-spec))
4562      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4563        gnus-unread ,(if (numberp number)
4564                         (string-to-int gnus-tmp-number-of-unread)
4565                       t)
4566        gnus-marked ,gnus-tmp-marked-mark
4567        gnus-level ,gnus-tmp-level))
4568     ;; Allow XEmacs to remove front-sticky text properties.
4569     (gnus-group-remove-excess-properties)))
4570
4571 (defun gnus-group-update-group (group &optional visible-only)
4572   "Update all lines where GROUP appear.
4573 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4574 already." 
4575   (save-excursion
4576     (set-buffer gnus-group-buffer)
4577     ;; The buffer may be narrowed.
4578     (save-restriction
4579       (widen)
4580       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4581             (loc (point-min))
4582             found buffer-read-only visible)
4583         ;; Enter the current status into the dribble buffer.
4584         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4585           (if (and entry (not (gnus-ephemeral-group-p group)))
4586               (gnus-dribble-enter 
4587                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4588                        ")"))))
4589         ;; Find all group instances.  If topics are in use, each group
4590         ;; may be listed in more than once.
4591         (while (setq loc (text-property-any 
4592                           loc (point-max) 'gnus-group ident))
4593           (setq found t)
4594           (goto-char loc)
4595           (gnus-delete-line)
4596           (gnus-group-insert-group-line-info group)
4597           (setq loc (1+ loc)))
4598         (if (or found visible-only)
4599             ()
4600           ;; No such line in the buffer, find out where it's supposed to
4601           ;; go, and insert it there (or at the end of the buffer).
4602           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
4603           (let ((entry (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
4604             (while (and entry (car entry)
4605                         (not
4606                          (gnus-goto-char
4607                           (text-property-any
4608                            (point-min) (point-max) 
4609                            'gnus-group (gnus-intern-safe 
4610                                         (car (car entry)) 
4611                                         gnus-active-hashtb)))))
4612               (setq entry (cdr entry)))
4613             (or entry (goto-char (point-max))))
4614           ;; Finally insert the line.
4615           (gnus-group-insert-group-line-info group))
4616         (gnus-group-set-mode-line)))))
4617
4618 (defun gnus-group-set-mode-line ()
4619   (when (memq 'group gnus-updated-mode-lines)
4620     (let* ((gformat (or gnus-group-mode-line-format-spec
4621                         (setq gnus-group-mode-line-format-spec
4622                               (gnus-parse-format 
4623                                gnus-group-mode-line-format 
4624                                gnus-group-mode-line-format-alist))))
4625            (gnus-tmp-news-server (car (cdr gnus-select-method)))
4626            (gnus-tmp-news-method (car gnus-select-method))
4627            (max-len 60)
4628            header                       ;Dummy binding for user-defined formats
4629            ;; Get the resulting string.
4630            (mode-string (eval gformat)))
4631       ;; If the line is too long, we chop it off.
4632       (when (> (length mode-string) max-len) 
4633         (setq mode-string (substring mode-string 0 (- max-len 4))))
4634       (prog1
4635           (setq mode-line-buffer-identification (list mode-string))
4636         (set-buffer-modified-p t)))))
4637
4638 (defun gnus-group-group-name ()
4639   "Get the name of the newsgroup on the current line."
4640   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4641     (and group (symbol-name group))))
4642
4643 (defun gnus-group-group-level ()
4644   "Get the level of the newsgroup on the current line."
4645   (get-text-property (gnus-point-at-bol) 'gnus-level))
4646
4647 (defun gnus-group-group-unread ()
4648   "Get the number of unread articles of the newsgroup on the current line."
4649   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4650
4651 (defun gnus-group-search-forward (&optional backward all level first-too)
4652   "Find the next newsgroup with unread articles.
4653 If BACKWARD is non-nil, find the previous newsgroup instead.
4654 If ALL is non-nil, just find any newsgroup.
4655 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4656 group exists.
4657 If FIRST-TOO, the current line is also eligible as a target."
4658   (let ((way (if backward -1 1))
4659         (low gnus-level-killed)
4660         (beg (point))
4661         pos found lev)
4662     (if (and backward (progn (beginning-of-line)) (bobp))
4663         nil
4664       (or first-too (forward-line way))
4665       (while (and 
4666               (not (eobp))
4667               (not (setq 
4668                     found 
4669                     (and (or all
4670                              (and
4671                               (let ((unread 
4672                                      (get-text-property (point) 'gnus-unread)))
4673                                 (and (numberp unread) (> unread 0)))
4674                               (setq lev (get-text-property (point)
4675                                                            'gnus-level))
4676                               (<= lev gnus-level-subscribed)))
4677                          (or (not level)
4678                              (and (setq lev (get-text-property (point)
4679                                                                'gnus-level))
4680                                   (or (= lev level)
4681                                       (and (< lev low)
4682                                            (< level lev)
4683                                            (progn
4684                                              (setq low lev)
4685                                              (setq pos (point))
4686                                              nil))))))))
4687               (zerop (forward-line way)))))
4688     (if found 
4689         (progn (gnus-group-position-point) t)
4690       (goto-char (or pos beg))
4691       (and pos t))))
4692
4693 ;;; Gnus group mode commands
4694
4695 ;; Group marking.
4696
4697 (defun gnus-group-mark-group (n &optional unmark no-advance)
4698   "Mark the current group."
4699   (interactive "p")
4700   (let ((buffer-read-only nil)
4701         group)
4702     (while 
4703         (and (> n 0) 
4704              (setq group (gnus-group-group-name))
4705              (progn
4706                (beginning-of-line)
4707                (forward-char 
4708                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4709                (delete-char 1)
4710                (if unmark
4711                    (progn
4712                      (insert " ")
4713                      (setq gnus-group-marked (delete group gnus-group-marked)))
4714                  (insert "#")
4715                  (setq gnus-group-marked
4716                        (cons group (delete group gnus-group-marked))))
4717                t)
4718              (or no-advance (zerop (gnus-group-next-group 1))))
4719       (setq n (1- n)))
4720     (gnus-summary-position-point)
4721     n))
4722
4723 (defun gnus-group-unmark-group (n)
4724   "Remove the mark from the current group."
4725   (interactive "p")
4726   (gnus-group-mark-group n 'unmark))
4727
4728 (defun gnus-group-mark-region (unmark beg end)
4729   "Mark all groups between point and mark.
4730 If UNMARK, remove the mark instead."
4731   (interactive "P\nr")
4732   (let ((num (count-lines beg end)))
4733     (save-excursion
4734       (goto-char beg)
4735       (- num (gnus-group-mark-group num unmark)))))
4736
4737 (defun gnus-group-mark-regexp (regexp)
4738   "Mark all groups that match some regexp."
4739   (interactive "sMark (regexp): ")
4740   (let ((alist (cdr gnus-newsrc-alist))
4741         group)
4742     (while alist
4743       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
4744         (gnus-group-set-mark group)))))
4745
4746 (defun gnus-group-remove-mark (group)
4747   (if (gnus-group-goto-group group)
4748       (save-excursion
4749         (gnus-group-mark-group 1 'unmark t))
4750     (setq gnus-group-marked
4751           (delete group gnus-group-marked))))
4752                 
4753 (defun gnus-group-set-mark (group)
4754   "Set the process mark on GROUP."
4755   (if (gnus-group-goto-group group)
4756       (save-excursion
4757         (gnus-group-mark-group 1 nil t))
4758     (setq gnus-group-marked
4759           (cons group (delete group gnus-group-marked)))))
4760                 
4761 ;; Return a list of groups to work on.  Take into consideration N (the
4762 ;; prefix) and the list of marked groups.
4763 (defun gnus-group-process-prefix (n)
4764   (cond
4765    (n
4766     (setq n (prefix-numeric-value n))
4767     ;; There is a prefix, so we return a list of the N next
4768     ;; groups. 
4769     (let ((way (if (< n 0) -1 1))
4770           (n (abs n))
4771           group groups)
4772       (save-excursion
4773         (while (and (> n 0)
4774                     (setq group (gnus-group-group-name)))
4775           (setq groups (cons group groups))
4776           (setq n (1- n))
4777           (gnus-group-next-group way)))
4778       (nreverse groups)))
4779    ((and (boundp 'transient-mark-mode)
4780          transient-mark-mode
4781          mark-active)
4782     ;; Work on the region between point and mark.
4783     (let ((max (max (point) (mark)))
4784           groups)
4785       (save-excursion
4786         (goto-char (min (point) (mark)))
4787         (while 
4788             (and 
4789              (push (gnus-group-group-name) groups)
4790              (zerop (gnus-group-next-group 1))
4791              (< (point) max)))
4792         (nreverse groups))))
4793    (gnus-group-marked
4794     ;; No prefix, but a list of marked articles.
4795     (reverse gnus-group-marked))
4796    (t
4797     ;; Neither marked articles or a prefix, so we return the
4798     ;; current group.
4799     (let ((group (gnus-group-group-name)))
4800       (and group (list group))))))
4801
4802 ;; Selecting groups.
4803
4804 (defun gnus-group-read-group (&optional all no-article group)
4805   "Read news in this newsgroup.
4806 If the prefix argument ALL is non-nil, already read articles become
4807 readable.  IF ALL is a number, fetch this number of articles.  If the
4808 optional argument NO-ARTICLE is non-nil, no article will be
4809 auto-selected upon group entry.  If GROUP is non-nil, fetch that
4810 group."
4811   (interactive "P")
4812   (let ((group (or group (gnus-group-group-name)))
4813         number active marked entry)
4814     (or group (error "No group on current line"))
4815     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
4816                                             group gnus-newsrc-hashtb)))))
4817     ;; This group might be a dead group.  In that case we have to get
4818     ;; the number of unread articles from `gnus-active-hashtb'.
4819     (setq number
4820           (cond ((numberp all) all)
4821                 (entry (car entry))
4822                 ((setq active (gnus-active group))
4823                  (- (1+ (cdr active)) (car active)))))
4824     (gnus-summary-read-group 
4825      group (or all (and (numberp number) 
4826                         (zerop (+ number (length (cdr (assq 'tick marked)))
4827                                   (length (cdr (assq 'dormant marked)))))))
4828      no-article)))
4829
4830 (defun gnus-group-select-group (&optional all)
4831   "Select this newsgroup.
4832 No article is selected automatically.
4833 If ALL is non-nil, already read articles become readable.
4834 If ALL is a number, fetch this number of articles."
4835   (interactive "P")
4836   (gnus-group-read-group all t))
4837
4838 (defun gnus-group-quick-select-group (&optional all)
4839   "Select the current group \"quickly\". 
4840 This means that no highlighting or scoring will be performed."
4841   (interactive "P")
4842   (let (gnus-visual
4843         gnus-score-find-score-files-function
4844         gnus-apply-kill-hook
4845         gnus-summary-expunge-below)
4846     (gnus-group-read-group all t)))
4847
4848 (defun gnus-group-visible-select-group (&optional all)
4849   "Select the current group without hiding any articles."
4850   (interactive "P")
4851   (let ((gnus-inhibit-limiting t))
4852     (gnus-group-read-group all t)))
4853
4854 ;;;###autoload
4855 (defun gnus-fetch-group (group)
4856   "Start Gnus if necessary and enter GROUP.
4857 Returns whether the fetching was successful or not."
4858   (interactive "sGroup name: ")
4859   (or (get-buffer gnus-group-buffer)
4860       (gnus))
4861   (gnus-group-select-group))
4862
4863 ;; Enter a group that is not in the group buffer.  Non-nil is returned
4864 ;; if selection was successful.
4865 (defun gnus-group-read-ephemeral-group 
4866   (group method &optional activate quit-config)
4867   (let ((group (if (gnus-group-foreign-p group) group
4868                  (gnus-group-prefixed-name group method)))
4869         (cur (current-buffer)))
4870     (gnus-sethash 
4871      group
4872      (list t nil (list group gnus-level-default-subscribed nil nil 
4873                        (append method
4874                                (list
4875                                 (list 'quit-config 
4876                                       (if quit-config quit-config
4877                                         (cons (current-buffer) 'summary)))))))
4878      gnus-newsrc-hashtb)
4879     (set-buffer gnus-group-buffer)
4880     (or (gnus-check-server method)
4881         (error "Unable to contact server: %s" (gnus-status-message method)))
4882     (if activate (or (gnus-request-group group)
4883                      (error "Couldn't request group")))
4884     (condition-case ()
4885         (gnus-group-read-group t t group)
4886       (error nil)
4887       (quit nil))
4888     (not (equal (current-buffer) cur))))
4889   
4890 (defun gnus-group-jump-to-group (group)
4891   "Jump to newsgroup GROUP."
4892   (interactive 
4893    (list (completing-read 
4894           "Group: " gnus-active-hashtb nil 
4895           (memq gnus-select-method gnus-have-read-active-file))))
4896
4897   (if (equal group "")
4898       (error "Empty group name"))
4899
4900   (let ((b (text-property-any 
4901             (point-min) (point-max) 
4902             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4903     (if b
4904         ;; Either go to the line in the group buffer...
4905         (goto-char b)
4906       ;; ... or insert the line.
4907       (or
4908        (gnus-active group)
4909        (gnus-activate-group group)
4910        (error "%s error: %s" group (gnus-status-message group)))
4911
4912       (gnus-group-update-group group)
4913       (goto-char (text-property-any 
4914                   (point-min) (point-max)
4915                   'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
4916   ;; Adjust cursor point.
4917   (gnus-group-position-point))
4918
4919 (defun gnus-group-goto-group (group)
4920   "Goto to newsgroup GROUP."
4921   (when group
4922     (let ((b (text-property-any (point-min) (point-max) 
4923                                 'gnus-group (gnus-intern-safe
4924                                              group gnus-active-hashtb))))
4925       (and b (goto-char b)))))
4926
4927 (defun gnus-group-next-group (n)
4928   "Go to next N'th newsgroup.
4929 If N is negative, search backward instead.
4930 Returns the difference between N and the number of skips actually
4931 done."
4932   (interactive "p")
4933   (gnus-group-next-unread-group n t))
4934
4935 (defun gnus-group-next-unread-group (n &optional all level)
4936   "Go to next N'th unread newsgroup.
4937 If N is negative, search backward instead.
4938 If ALL is non-nil, choose any newsgroup, unread or not.
4939 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
4940 such group can be found, the next group with a level higher than
4941 LEVEL.
4942 Returns the difference between N and the number of skips actually
4943 made."
4944   (interactive "p")
4945   (let ((backward (< n 0))
4946         (n (abs n)))
4947     (while (and (> n 0)
4948                 (gnus-group-search-forward 
4949                  backward (or (not gnus-group-goto-unread) all) level))
4950       (setq n (1- n)))
4951     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
4952                                (if level " on this level or higher" "")))
4953     n))
4954
4955 (defun gnus-group-prev-group (n)
4956   "Go to previous N'th newsgroup.
4957 Returns the difference between N and the number of skips actually
4958 done."
4959   (interactive "p")
4960   (gnus-group-next-unread-group (- n) t))
4961
4962 (defun gnus-group-prev-unread-group (n)
4963   "Go to previous N'th unread newsgroup.
4964 Returns the difference between N and the number of skips actually
4965 done."  
4966   (interactive "p")
4967   (gnus-group-next-unread-group (- n)))
4968
4969 (defun gnus-group-next-unread-group-same-level (n)
4970   "Go to next N'th unread newsgroup on the same level.
4971 If N is negative, search backward instead.
4972 Returns the difference between N and the number of skips actually
4973 done."
4974   (interactive "p")
4975   (gnus-group-next-unread-group n t (gnus-group-group-level))
4976   (gnus-group-position-point))
4977
4978 (defun gnus-group-prev-unread-group-same-level (n)
4979   "Go to next N'th unread newsgroup on the same level.
4980 Returns the difference between N and the number of skips actually
4981 done."
4982   (interactive "p")
4983   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
4984   (gnus-group-position-point))
4985
4986 (defun gnus-group-best-unread-group (&optional exclude-group)
4987   "Go to the group with the highest level.
4988 If EXCLUDE-GROUP, do not go to that group."
4989   (interactive)
4990   (goto-char (point-min))
4991   (let ((best 100000)
4992         unread best-point)
4993     (while (setq unread (get-text-property (point) 'gnus-unread))
4994       (if (and (numberp unread) (> unread 0))
4995           (progn
4996             (if (and (< (get-text-property (point) 'gnus-level) best)
4997                      (or (not exclude-group)
4998                          (not (equal exclude-group (gnus-group-group-name)))))
4999                 (progn 
5000                   (setq best (get-text-property (point) 'gnus-level))
5001                   (setq best-point (point))))))
5002       (forward-line 1))
5003     (if best-point (goto-char best-point))
5004     (gnus-summary-position-point)
5005     (and best-point (gnus-group-group-name))))
5006
5007 (defun gnus-group-first-unread-group ()
5008   "Go to the first group with unread articles."
5009   (interactive)
5010   (prog1
5011       (let ((opoint (point))
5012             unread)
5013         (goto-char (point-min))
5014         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5015                 (and (numberp unread)   ; Not a topic.
5016                      (not (zerop unread))) ; Has unread articles.
5017                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5018             (point)                     ; Success.
5019           (goto-char opoint)
5020           nil))                         ; Not success.
5021     (gnus-group-position-point)))
5022
5023 (defun gnus-group-enter-server-mode ()
5024   "Jump to the server buffer."
5025   (interactive)
5026   (gnus-enter-server-buffer))
5027
5028 (defun gnus-group-make-group (name &optional method address)
5029   "Add a new newsgroup.
5030 The user will be prompted for a NAME, for a select METHOD, and an
5031 ADDRESS."
5032   (interactive
5033    (cons 
5034     (read-string "Group name: ")
5035     (let ((method
5036            (completing-read 
5037             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5038             nil t)))
5039       (if (assoc method gnus-valid-select-methods)
5040           (list method
5041                 (if (memq 'prompt-address
5042                           (assoc method gnus-valid-select-methods))
5043                     (read-string "Address: ")
5044                   ""))
5045         (list method nil)))))
5046   
5047   (save-excursion
5048     (set-buffer gnus-group-buffer)
5049     (let* ((meth (and method (if address (list (intern method) address) 
5050                                method)))
5051            (nname (if method (gnus-group-prefixed-name name meth) name))
5052            info)
5053       (and (gnus-gethash nname gnus-newsrc-hashtb)
5054            (error "Group %s already exists" nname))
5055       (gnus-group-change-level 
5056        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5057        gnus-level-default-subscribed gnus-level-killed 
5058        (and (gnus-group-group-name)
5059             (gnus-gethash (gnus-group-group-name)
5060                           gnus-newsrc-hashtb))
5061        t)
5062       (gnus-set-active nname (cons 1 0))
5063       (or (gnus-ephemeral-group-p name)
5064           (gnus-dribble-enter 
5065            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5066       (gnus-group-insert-group-line-info nname)
5067
5068       (if (assoc method gnus-valid-select-methods)
5069           (require (intern method)))
5070       (and (gnus-check-backend-function 'request-create-group nname)
5071            (gnus-request-create-group nname))
5072       t)))
5073
5074 (defun gnus-group-delete-group (group &optional force)
5075   "Delete the current group.
5076 If FORCE (the prefix) is non-nil, all the articles in the group will
5077 be deleted.  This is \"deleted\" as in \"removed forever from the face
5078 of the Earth\".  There is no undo."
5079   (interactive 
5080    (list (gnus-group-group-name)
5081          current-prefix-arg))
5082   (or group (error "No group to rename"))
5083   (or (gnus-check-backend-function 'request-delete-group group)
5084       (error "This backend does not support group deletion"))
5085   (prog1
5086       (if (not (gnus-yes-or-no-p
5087                 (format
5088                  "Do you really want to delete %s%s? " 
5089                  group (if force " and all its contents" ""))))
5090           () ; Whew!
5091         (gnus-message 6 "Deleting group %s..." group)
5092         (if (not (gnus-request-delete-group group force))
5093             (progn
5094               (gnus-message 3 "Couldn't delete group %s" group)
5095               (ding))
5096           (gnus-message 6 "Deleting group %s...done" group)
5097           (gnus-group-goto-group group)
5098           (gnus-group-kill-group 1 t)
5099           t))
5100     (gnus-group-position-point)))
5101
5102 (defun gnus-group-rename-group (group new-name)
5103   (interactive
5104    (list
5105     (gnus-group-group-name)
5106     (progn
5107       (or (gnus-check-backend-function 
5108            'request-rename-group (gnus-group-group-name))
5109           (error "This backend does not support renaming groups"))
5110       (read-string "New group name: "))))
5111
5112   (or (gnus-check-backend-function 'request-rename-group group)
5113       (error "This backend does not support renaming groups"))
5114
5115   (or group (error "No group to rename"))
5116   (and (string-match "^[ \t]*$" new-name) 
5117        (error "Not a valid group name"))
5118
5119   ;; We find the proper prefixed name.
5120   (setq new-name
5121         (gnus-group-prefixed-name 
5122          (gnus-group-real-name new-name)
5123          (gnus-info-method (gnus-get-info group))))
5124
5125   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5126   (prog1
5127       (if (not (gnus-request-rename-group group new-name))
5128           (progn
5129             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5130             (ding))
5131         ;; We rename the group internally by killing it...
5132         (gnus-group-goto-group group)
5133         (gnus-group-kill-group)
5134         ;; ... changing its name ...
5135         (setcar (cdr (car gnus-list-of-killed-groups))
5136                 new-name)
5137         ;; ... and then yanking it.  Magic!
5138         (gnus-group-yank-group) 
5139         (gnus-set-active new-name (gnus-active group))
5140         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5141         new-name)
5142     (gnus-group-position-point)))
5143
5144
5145 (defun gnus-group-edit-group (group &optional part)
5146   "Edit the group on the current line."
5147   (interactive (list (gnus-group-group-name)))
5148   (let ((done-func '(lambda () 
5149                       "Exit editing mode and update the information."
5150                       (interactive)
5151                       (gnus-group-edit-group-done 'part 'group)))
5152         (part (or part 'info))
5153         (winconf (current-window-configuration))
5154         info)
5155     (or group (error "No group on current line"))
5156     (or (setq info (gnus-get-info group))
5157         (error "Killed group; can't be edited"))
5158     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5159     (gnus-configure-windows 'edit-group)
5160     (gnus-add-current-to-buffer-list)
5161     (emacs-lisp-mode)
5162     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5163     (use-local-map (copy-keymap emacs-lisp-mode-map))
5164     (local-set-key "\C-c\C-c" done-func)
5165     (make-local-variable 'gnus-prev-winconf)
5166     (setq gnus-prev-winconf winconf)
5167     ;; We modify the func to let it know what part it is editing.
5168     (setcar (cdr (nth 4 done-func)) (list 'quote part))
5169     (setcar (cdr (cdr (nth 4 done-func))) group)
5170     (erase-buffer)
5171     (insert
5172      (cond 
5173       ((eq part 'method)
5174        ";; Type `C-c C-c' after editing the select method.\n\n")
5175       ((eq part 'params)
5176        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5177       ((eq part 'info)
5178        ";; Type `C-c C-c' after editing the group info.\n\n")))
5179     (insert 
5180      (pp-to-string
5181       (cond ((eq part 'method)
5182              (or (gnus-info-method info) "native"))
5183             ((eq part 'params)
5184              (gnus-info-params info))
5185             (t info)))
5186      "\n")))
5187
5188 (defun gnus-group-edit-group-method (group)
5189   "Edit the select method of GROUP."
5190   (interactive (list (gnus-group-group-name)))
5191   (gnus-group-edit-group group 'method))
5192
5193 (defun gnus-group-edit-group-parameters (group)
5194   "Edit the group parameters of GROUP."
5195   (interactive (list (gnus-group-group-name)))
5196   (gnus-group-edit-group group 'params))
5197
5198 (defun gnus-group-edit-group-done (part group)
5199   "Get info from buffer, update variables and jump to the group buffer."
5200   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5201   (goto-char (point-min))
5202   (let* ((form (read (current-buffer)))
5203          (winconf gnus-prev-winconf)
5204          (new-group (when (eq part 'info)
5205                       (if (or (not (nth 4 form))
5206                               (gnus-server-equal
5207                                gnus-select-method (nth 4 form)))
5208                           (gnus-group-real-name (car form))
5209                         (gnus-group-prefixed-name
5210                          (gnus-group-real-name (car form)) (nth 4 form))))))
5211     ;; Set the info.
5212     (if (eq part 'info) 
5213         (progn
5214           (when new-group (setcar form new-group))
5215           (gnus-group-set-info form))
5216       (gnus-group-set-info form group part))
5217     (kill-buffer (current-buffer))
5218     (and winconf (set-window-configuration winconf))
5219     (set-buffer gnus-group-buffer)
5220     (when (and new-group 
5221              (not (equal new-group group)))
5222       (when (gnus-group-goto-group group)
5223         (gnus-group-kill-group 1))
5224       (gnus-activate-group new-group))
5225     (gnus-group-update-group (or new-group group))
5226     (gnus-group-position-point)))
5227
5228 (defun gnus-group-make-help-group ()
5229   "Create the Gnus documentation group."
5230   (interactive)
5231   (let ((path load-path)
5232         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5233         file dir)
5234     (and (gnus-gethash name gnus-newsrc-hashtb)
5235          (error "Documentation group already exists"))
5236     (while path
5237       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5238             file nil)
5239       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5240                 (file-exists-p
5241                  (setq file (concat (file-name-directory 
5242                                      (directory-file-name dir))
5243                                     "etc/gnus-tut.txt"))))
5244         (setq path nil)))
5245     (if (not file)
5246         (message "Couldn't find doc group")
5247       (gnus-group-make-group 
5248        (gnus-group-real-name name)
5249        (list 'nndoc name
5250              (list 'nndoc-address file)
5251              (list 'nndoc-article-type 'mbox)))))
5252   (gnus-group-position-point))
5253
5254 (defun gnus-group-make-doc-group (file type)
5255   "Create a group that uses a single file as the source."
5256   (interactive 
5257    (list (read-file-name "File name: ") 
5258          (and current-prefix-arg 'ask)))
5259   (when (eq type 'ask)
5260     (let ((err "")
5261           char found)
5262       (while (not found)
5263         (message 
5264          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5265          err)
5266         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5267                           ((= char ?b) 'babyl)
5268                           ((= char ?d) 'digest)
5269                           ((= char ?f) 'forward)
5270                           ((= char ?a) 'mmfd)
5271                           (t (setq err (format "%c unknown. " char))
5272                              nil))))
5273       (setq type found)))
5274   (let* ((file (expand-file-name file))
5275          (name (gnus-generate-new-group-name
5276                 (gnus-group-prefixed-name
5277                  (file-name-nondirectory file) '(nndoc "")))))
5278     (gnus-group-make-group 
5279      (gnus-group-real-name name)
5280      (list 'nndoc name
5281            (list 'nndoc-address file)
5282            (list 'nndoc-article-type (or type 'guess))))
5283     (forward-line -1)
5284     (gnus-group-position-point)))
5285
5286 (defun gnus-group-make-archive-group (&optional all)
5287   "Create the (ding) Gnus archive group of the most recent articles.
5288 Given a prefix, create a full group."
5289   (interactive "P")
5290   (let ((group (gnus-group-prefixed-name 
5291                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5292     (and (gnus-gethash group gnus-newsrc-hashtb)
5293          (error "Archive group already exists"))
5294     (gnus-group-make-group
5295      (gnus-group-real-name group)
5296      (list 'nndir (if all "hpc" "edu")
5297            (list 'nndir-directory  
5298                  (if all gnus-group-archive-directory 
5299                    gnus-group-recent-archive-directory)))))
5300   (forward-line -1)
5301   (gnus-group-position-point))
5302
5303 (defun gnus-group-make-directory-group (dir)
5304   "Create an nndir group.
5305 The user will be prompted for a directory.  The contents of this
5306 directory will be used as a newsgroup.  The directory should contain
5307 mail messages or news articles in files that have numeric names."
5308   (interactive
5309    (list (read-file-name "Create group from directory: ")))
5310   (or (file-exists-p dir) (error "No such directory"))
5311   (or (file-directory-p dir) (error "Not a directory"))
5312   (let ((ext "")
5313         (i 0)
5314         group)
5315     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5316       (setq group
5317             (gnus-group-prefixed-name 
5318              (concat (file-name-as-directory (directory-file-name dir))
5319                      ext)
5320              '(nndir "")))
5321       (setq ext (format "<%d>" (setq i (1+ i)))))
5322     (gnus-group-make-group 
5323      (gnus-group-real-name group)
5324      (list 'nndir group (list 'nndir-directory dir))))
5325   (forward-line -1)
5326   (gnus-group-position-point))
5327
5328 (defun gnus-group-make-kiboze-group (group address scores)
5329   "Create an nnkiboze group.
5330 The user will be prompted for a name, a regexp to match groups, and
5331 score file entries for articles to include in the group."
5332   (interactive
5333    (list
5334     (read-string "nnkiboze group name: ")
5335     (read-string "Source groups (regexp): ")
5336     (let ((headers (mapcar (lambda (group) (list group))
5337                            '("subject" "from" "number" "date" "message-id"
5338                              "references" "chars" "lines" "xref"
5339                              "followup" "all" "body" "head")))
5340           scores header regexp regexps)
5341       (while (not (equal "" (setq header (completing-read 
5342                                           "Match on header: " headers nil t))))
5343         (setq regexps nil)
5344         (while (not (equal "" (setq regexp (read-string 
5345                                             (format "Match on %s (string): "
5346                                                     header)))))
5347           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5348         (setq scores (cons (cons header regexps) scores)))
5349       scores)))
5350   (gnus-group-make-group group "nnkiboze" address)
5351   (save-excursion
5352     (gnus-set-work-buffer)
5353     (let (emacs-lisp-mode-hook)
5354       (pp scores (current-buffer)))
5355     (write-region (point-min) (point-max) 
5356                   (gnus-score-file-name (concat "nnkiboze:" group))))
5357   (forward-line -1)
5358   (gnus-group-position-point))
5359
5360 (defun gnus-group-add-to-virtual (n vgroup)
5361   "Add the current group to a virtual group."
5362   (interactive
5363    (list current-prefix-arg
5364          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5365                           "nnvirtual:")))
5366   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5367       (error "%s is not an nnvirtual group" vgroup))
5368   (let* ((groups (gnus-group-process-prefix n))
5369          (method (gnus-info-method (gnus-get-info vgroup))))
5370     (setcar (cdr method)
5371             (concat 
5372              (nth 1 method) "\\|"
5373              (mapconcat 
5374               (lambda (s) 
5375                 (gnus-group-remove-mark s)
5376                 (concat "\\(^" (regexp-quote s) "$\\)"))
5377               groups "\\|"))))
5378   (gnus-group-position-point))
5379
5380 (defun gnus-group-make-empty-virtual (group)
5381   "Create a new, fresh, empty virtual group."
5382   (interactive "sCreate new, empty virtual group: ")
5383   (let* ((method (list 'nnvirtual "^$"))
5384          (pgroup (gnus-group-prefixed-name group method)))
5385     ;; Check whether it exists already.
5386     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5387          (error "Group %s already exists." pgroup))
5388     ;; Subscribe the new group after the group on the current line.
5389     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5390     (gnus-group-update-group pgroup)
5391     (forward-line -1)
5392     (gnus-group-position-point)))
5393
5394 (defun gnus-group-enter-directory (dir)
5395   "Enter an ephemeral nneething group."
5396   (interactive "DDirectory to read: ")
5397   (let* ((method (list 'nneething dir))
5398          (leaf (gnus-group-prefixed-name
5399                 (file-name-nondirectory (directory-file-name dir))
5400                 method))
5401          (name (gnus-generate-new-group-name leaf)))
5402     (let ((nneething-read-only t))
5403       (or (gnus-group-read-ephemeral-group 
5404            name method t
5405            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5406                                       'summary 'group)))
5407           (error "Couldn't enter %s" dir)))))
5408
5409 ;; Group sorting commands
5410 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5411
5412 (defun gnus-group-sort-groups (func &optional reverse)
5413   "Sort the group buffer according to FUNC.
5414 If REVERSE, reverse the sorting order."
5415   (interactive (list gnus-group-sort-function
5416                      current-prefix-arg))
5417   (unless (listp func)
5418     (setq func (list func)))
5419   ;; We peel off the dummy group from the alist.
5420   (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5421     (pop gnus-newsrc-alist))
5422   ;; Do the sorting.
5423   (while func
5424     (setq gnus-newsrc-alist 
5425           (sort gnus-newsrc-alist (pop func))))
5426   (when reverse
5427     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5428   ;; Regenerate the hash table.
5429   (gnus-make-hashtable-from-newsrc-alist)
5430   (gnus-group-list-groups))
5431
5432 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5433   "Sort the group buffer alphabetically by group name.
5434 If REVERSE, sort in reverse order."
5435   (interactive "P")
5436   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5437
5438 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5439   "Sort the group buffer by number of unread articles.
5440 If REVERSE, sort in reverse order."
5441   (interactive "P")
5442   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5443
5444 (defun gnus-group-sort-groups-by-level (&optional reverse)
5445   "Sort the group buffer by group level.
5446 If REVERSE, sort in reverse order."
5447   (interactive "P")
5448   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5449
5450 (defun gnus-group-sort-groups-by-score (&optional reverse)
5451   "Sort the group buffer by group score.
5452 If REVERSE, sort in reverse order."
5453   (interactive "P")
5454   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5455
5456 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5457   "Sort the group buffer by group rank.
5458 If REVERSE, sort in reverse order."
5459   (interactive "P")
5460   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5461
5462 (defun gnus-group-sort-groups-by-method (&optional reverse)
5463   "Sort the group buffer alphabetically by backend name.
5464 If REVERSE, sort in reverse order."
5465   (interactive "P")
5466   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5467
5468 (defun gnus-group-sort-by-alphabet (info1 info2)
5469   "Sort alphabetically."
5470   (string< (gnus-info-group info1) (gnus-info-group info2)))
5471
5472 (defun gnus-group-sort-by-unread (info1 info2)
5473   "Sort by number of unread articles."
5474   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5475         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5476     (< (or (and (numberp n1) n1) 0)
5477        (or (and (numberp n2) n2) 0))))
5478
5479 (defun gnus-group-sort-by-level (info1 info2)
5480   "Sort by level."
5481   (< (gnus-info-level info1) (gnus-info-level info2)))
5482
5483 (defun gnus-group-sort-by-method (info1 info2)
5484   "Sort alphabetically by backend name."
5485   (string< (symbol-name (car (gnus-find-method-for-group
5486                               (gnus-info-group info1) info1)))
5487            (symbol-name (car (gnus-find-method-for-group 
5488                               (gnus-info-group info2) info2)))))
5489
5490 (defun gnus-group-sort-by-score (info1 info2)
5491   "Sort by group score."
5492   (< (gnus-info-score info1) (gnus-info-score info2)))
5493
5494 (defun gnus-group-sort-by-rank (info1 info2)
5495   "Sort by level and score."
5496   (let ((level1 (gnus-info-level info1))
5497         (level2 (gnus-info-level info2)))
5498     (or (< level1 level2)
5499         (and (= level1 level2)
5500              (< (gnus-info-score info1) (gnus-info-score info2))))))
5501
5502 ;; Group catching up.
5503
5504 (defun gnus-group-catchup-current (&optional n all)
5505   "Mark all articles not marked as unread in current newsgroup as read.
5506 If prefix argument N is numeric, the ARG next newsgroups will be
5507 caught up.  If ALL is non-nil, marked articles will also be marked as
5508 read.  Cross references (Xref: header) of articles are ignored.
5509 The difference between N and actual number of newsgroups that were
5510 caught up is returned."
5511   (interactive "P")
5512   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5513                gnus-expert-user
5514                (gnus-y-or-n-p
5515                 (if all
5516                     "Do you really want to mark all articles as read? "
5517                   "Mark all unread articles as read? "))))
5518       n
5519     (let ((groups (gnus-group-process-prefix n))
5520           (ret 0))
5521       (while groups
5522         ;; Virtual groups have to be given special treatment. 
5523         (let ((method (gnus-find-method-for-group (car groups))))
5524           (if (eq 'nnvirtual (car method))
5525               (nnvirtual-catchup-group
5526                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5527         (gnus-group-remove-mark (car groups))
5528         (if (prog1
5529                 (gnus-group-goto-group (car groups))
5530               (gnus-group-catchup (car groups) all))
5531             (gnus-group-update-group-line)
5532           (setq ret (1+ ret)))
5533         (setq groups (cdr groups)))
5534       (gnus-group-next-unread-group 1)
5535       ret)))
5536
5537 (defun gnus-group-catchup-current-all (&optional n)
5538   "Mark all articles in current newsgroup as read.
5539 Cross references (Xref: header) of articles are ignored."
5540   (interactive "P")
5541   (gnus-group-catchup-current n 'all))
5542
5543 (defun gnus-group-catchup (group &optional all)
5544   "Mark all articles in GROUP as read.
5545 If ALL is non-nil, all articles are marked as read.
5546 The return value is the number of articles that were marked as read,
5547 or nil if no action could be taken."
5548   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5549          (num (car entry)))
5550     ;; Do the updating only if the newsgroup isn't killed.
5551     (if (not (numberp (car entry)))
5552         (gnus-message 1 "Can't catch up; non-active group")
5553       ;; Do auto-expirable marks if that's required.
5554       (when (gnus-group-auto-expirable-p group)
5555         (gnus-add-marked-articles 
5556          group 'expire (gnus-list-of-unread-articles group))
5557         (when all
5558           (let ((marks (nth 3 (nth 2 entry))))
5559             (gnus-add-marked-articles 
5560              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5561             (gnus-add-marked-articles 
5562              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5563       (when entry
5564         (gnus-update-read-articles group nil)
5565         ;; Also nix out the lists of marks and dormants. 
5566         (when all 
5567           (gnus-add-marked-articles group 'tick nil nil 'force)
5568           (gnus-add-marked-articles group 'dormant nil nil 'force))
5569         num))))
5570
5571 (defun gnus-group-expire-articles (&optional n)
5572   "Expire all expirable articles in the current newsgroup."
5573   (interactive "P")
5574   (let ((groups (gnus-group-process-prefix n))
5575         group)
5576     (unless groups
5577       (error "No groups to expire"))
5578     (while groups
5579       (setq group (pop groups))
5580       (gnus-group-remove-mark group)
5581       (when (gnus-check-backend-function 'request-expire-articles group)
5582         (gnus-message 6 "Expiring articles in %s..." group)
5583         (let* ((info (gnus-get-info group))
5584                (expirable (if (gnus-group-total-expirable-p group)
5585                               (cons nil (gnus-list-of-read-articles group))
5586                             (assq 'expire (gnus-info-marks info))))
5587                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5588           (when expirable 
5589             (setcdr expirable
5590                     (gnus-compress-sequence
5591                      (if expiry-wait
5592                          (let ((nnmail-expiry-wait-function nil)
5593                                (nnmail-expiry-wait expiry-wait))
5594                            (gnus-request-expire-articles 
5595                             (gnus-uncompress-sequence (cdr expirable)) group))
5596                        (gnus-request-expire-articles 
5597                         (gnus-uncompress-sequence (cdr expirable))
5598                         group)))))
5599           (gnus-message 6 "Expiring articles in %s...done" group)))
5600       (gnus-group-position-point))))
5601
5602
5603 (defun gnus-group-expire-all-groups ()
5604   "Expire all expirable articles in all newsgroups."
5605   (interactive)
5606   (save-excursion
5607     (gnus-message 5 "Expiring...")
5608     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5609                                      (cdr gnus-newsrc-alist))))
5610       (gnus-group-expire-articles nil)))
5611   (gnus-group-position-point)
5612   (gnus-message 5 "Expiring...done"))
5613
5614 (defun gnus-group-set-current-level (n level)
5615   "Set the level of the next N groups to LEVEL."
5616   (interactive 
5617    (list
5618     current-prefix-arg
5619     (string-to-int
5620      (let ((s (read-string 
5621                (format "Level (default %s): " (gnus-group-group-level)))))
5622        (if (string-match "^\\s-*$" s)
5623            (int-to-string (gnus-group-group-level))
5624          s)))))
5625   (or (and (>= level 1) (<= level gnus-level-killed))
5626       (error "Illegal level: %d" level))
5627   (let ((groups (gnus-group-process-prefix n))
5628         group)
5629     (while groups
5630       (setq group (car groups)
5631             groups (cdr groups))
5632       (gnus-group-remove-mark group)
5633       (gnus-message 6 "Changed level of %s from %d to %d" 
5634                     group (gnus-group-group-level) level)
5635       (gnus-group-change-level group level
5636                                (gnus-group-group-level))
5637       (gnus-group-update-group-line)))
5638   (gnus-group-position-point))
5639
5640 (defun gnus-group-unsubscribe-current-group (&optional n)
5641   "Toggle subscription of the current group.
5642 If given numerical prefix, toggle the N next groups."
5643   (interactive "P")
5644   (let ((groups (gnus-group-process-prefix n))
5645         group)
5646     (while groups
5647       (setq group (car groups)
5648             groups (cdr groups))
5649       (gnus-group-remove-mark group)
5650       (gnus-group-unsubscribe-group
5651        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5652                  gnus-level-default-unsubscribed
5653                gnus-level-default-subscribed) t)
5654       (gnus-group-update-group-line))
5655     (gnus-group-next-group 1)))
5656
5657 (defun gnus-group-unsubscribe-group (group &optional level silent)
5658   "Toggle subscription to GROUP.
5659 Killed newsgroups are subscribed.  If SILENT, don't try to update the
5660 group line."
5661   (interactive
5662    (list (completing-read
5663           "Group: " gnus-active-hashtb nil 
5664           (memq gnus-select-method gnus-have-read-active-file))))
5665   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
5666     (cond
5667      ((string-match "^[ \t]$" group)
5668       (error "Empty group name"))
5669      (newsrc
5670       ;; Toggle subscription flag.
5671       (gnus-group-change-level 
5672        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
5673                                       gnus-level-subscribed) 
5674                                   (1+ gnus-level-subscribed)
5675                                 gnus-level-default-subscribed)))
5676       (unless silent
5677         (gnus-group-update-group group)))
5678      ((and (stringp group)
5679            (or (not (memq gnus-select-method gnus-have-read-active-file))
5680                (gnus-active group)))
5681       ;; Add new newsgroup.
5682       (gnus-group-change-level 
5683        group 
5684        (if level level gnus-level-default-subscribed) 
5685        (or (and (member group gnus-zombie-list) 
5686                 gnus-level-zombie) 
5687            gnus-level-killed)
5688        (and (gnus-group-group-name)
5689             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
5690       (unless silent
5691         (gnus-group-update-group group)))
5692      (t (error "No such newsgroup: %s" group)))
5693     (gnus-group-position-point)))
5694
5695 (defun gnus-group-transpose-groups (n)
5696   "Move the current newsgroup up N places.
5697 If given a negative prefix, move down instead.  The difference between
5698 N and the number of steps taken is returned." 
5699   (interactive "p")
5700   (or (gnus-group-group-name)
5701       (error "No group on current line"))
5702   (gnus-group-kill-group 1)
5703   (prog1
5704       (forward-line (- n))
5705     (gnus-group-yank-group)
5706     (gnus-group-position-point)))
5707
5708 (defun gnus-group-kill-all-zombies ()
5709   "Kill all zombie newsgroups."
5710   (interactive)
5711   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
5712   (setq gnus-zombie-list nil)
5713   (gnus-group-list-groups))
5714
5715 (defun gnus-group-kill-region (begin end)
5716   "Kill newsgroups in current region (excluding current point).
5717 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
5718   (interactive "r")
5719   (let ((lines
5720          ;; Count lines.
5721          (save-excursion
5722            (count-lines
5723             (progn
5724               (goto-char begin)
5725               (beginning-of-line)
5726               (point))
5727             (progn
5728               (goto-char end)
5729               (beginning-of-line)
5730               (point))))))
5731     (goto-char begin)
5732     (beginning-of-line)                 ;Important when LINES < 1
5733     (gnus-group-kill-group lines)))
5734
5735 (defun gnus-group-kill-group (&optional n discard)
5736   "Kill the next N groups.
5737 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
5738 However, only groups that were alive can be yanked; already killed 
5739 groups or zombie groups can't be yanked.
5740 The return value is the name of the group that was killed, or a list
5741 of groups killed."
5742   (interactive "P")
5743   (let ((buffer-read-only nil)
5744         (groups (gnus-group-process-prefix n))
5745         group entry level out)
5746     (if (< (length groups) 10)
5747         ;; This is faster when there are few groups.
5748         (while groups
5749           (push (setq group (pop groups)) out)
5750           (gnus-group-remove-mark group)
5751           (setq level (gnus-group-group-level))
5752           (gnus-delete-line)
5753           (if (and (not discard)
5754                    (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
5755               (setq gnus-list-of-killed-groups 
5756                     (cons (cons (car entry) (nth 2 entry)) 
5757                           gnus-list-of-killed-groups)))
5758           (gnus-group-change-level 
5759            (if entry entry group) gnus-level-killed (if entry nil level)))
5760       ;; If there are lots and lots of groups to be killed, we use
5761       ;; this thing instead.
5762       (let (entry)
5763         (setq groups (nreverse groups))
5764         (while groups
5765           (gnus-group-remove-mark (car groups))
5766           (gnus-delete-line)
5767           (setq entry (gnus-gethash (pop groups) gnus-newsrc-hashtb))
5768           (push (cons (car entry) (nth 2 entry))
5769                 gnus-list-of-killed-groups)
5770           (setcdr (cdr entry) (cdr (cdr (cdr entry)))))
5771         (gnus-make-hashtable-from-newsrc-alist)))
5772     
5773     (gnus-group-position-point)
5774     (if (< (length out) 2) (car out) (nreverse out))))
5775
5776 (defun gnus-group-yank-group (&optional arg)
5777   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
5778 inserting it before the current newsgroup.  The numeric ARG specifies
5779 how many newsgroups are to be yanked.  The name of the newsgroup yanked
5780 is returned, or (if several groups are yanked) a list of yanked groups
5781 is returned."
5782   (interactive "p")
5783   (setq arg (or arg 1))
5784   (let (info group prev out)
5785     (while (>= (decf arg) 0)
5786       (if (not (setq info (pop gnus-list-of-killed-groups)))
5787           (error "No more newsgroups to yank"))
5788       (push (setq group (nth 1 info)) out)
5789       ;; Find which newsgroup to insert this one before - search
5790       ;; backward until something suitable is found.  If there are no
5791       ;; other newsgroups in this buffer, just make this newsgroup the
5792       ;; first newsgroup.
5793       (setq prev (gnus-group-group-name))
5794       (gnus-group-change-level 
5795        info (nth 2 info) gnus-level-killed 
5796        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
5797        t)
5798       (gnus-group-insert-group-line-info group))
5799     (forward-line -1)
5800     (gnus-group-position-point)
5801     (if (< (length out) 2) (car out) (nreverse out))))
5802
5803 (defun gnus-group-kill-level (level)
5804   "Kill all groups that is on a certain LEVEL."
5805   (interactive "nKill all groups on level: ")
5806   (cond 
5807    ((= level gnus-level-zombie)
5808     (setq gnus-killed-list
5809           (nconc gnus-zombie-list gnus-killed-list))
5810     (setq gnus-zombie-list nil))
5811    ((and (< level gnus-level-zombie)
5812          (> level 0)
5813          (or gnus-expert-user
5814              (gnus-yes-or-no-p
5815               (format 
5816                "Do you really want to kill all groups on level %d? "
5817                level))))
5818     (let* ((prev gnus-newsrc-alist)
5819            (alist (cdr prev)))
5820       (while alist
5821         (if (= (gnus-info-level level) level)
5822             (setcdr prev (cdr alist))
5823           (setq prev alist))
5824         (setq alist (cdr alist)))
5825       (gnus-make-hashtable-from-newsrc-alist)
5826       (gnus-group-list-groups)))
5827    (t
5828     (error "Can't kill; illegal level: %d" level))))
5829       
5830 (defun gnus-group-list-all-groups (&optional arg)
5831   "List all newsgroups with level ARG or lower.
5832 Default is gnus-level-unsubscribed, which lists all subscribed and most
5833 unsubscribed groups."
5834   (interactive "P")
5835   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
5836
5837 ;; Redefine this to list ALL killed groups if prefix arg used.
5838 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
5839 (defun gnus-group-list-killed (&optional arg)
5840   "List all killed newsgroups in the group buffer.
5841 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
5842 entail asking the server for the groups."
5843   (interactive "P")
5844   ;; Find all possible killed newsgroups if arg.
5845   (when arg
5846     ;; First make sure active file has been read.
5847     (unless gnus-have-read-active-file
5848       (let ((gnus-read-active-file t))
5849         (gnus-read-active-file)))
5850     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
5851     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
5852     (mapatoms
5853      (lambda (sym)
5854        (let ((groups 0)
5855              (group (symbol-name sym)))
5856          (if (or (null group)
5857                  (gnus-gethash group gnus-killed-hashtb)
5858                  (gnus-gethash group gnus-newsrc-hashtb))
5859              ()
5860            (let ((do-sub (gnus-matches-options-n group)))
5861              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
5862                  ()
5863                (setq groups (1+ groups))
5864                (setq gnus-killed-list 
5865                      (cons group gnus-killed-list))
5866                (gnus-sethash group group gnus-killed-hashtb))))))
5867      gnus-active-hashtb))
5868   (if (not gnus-killed-list)
5869       (gnus-message 6 "No killed groups")
5870     (let (gnus-group-list-mode)
5871       (funcall gnus-group-prepare-function 
5872                gnus-level-killed t gnus-level-killed))
5873     (goto-char (point-min)))
5874   (gnus-group-position-point))
5875
5876 (defun gnus-group-list-zombies ()
5877   "List all zombie newsgroups in the group buffer."
5878   (interactive)
5879   (if (not gnus-zombie-list)
5880       (gnus-message 6 "No zombie groups")
5881     (let (gnus-group-list-mode)
5882       (funcall gnus-group-prepare-function
5883                gnus-level-zombie t gnus-level-zombie))
5884     (goto-char (point-min)))
5885   (gnus-group-position-point))
5886
5887 (defun gnus-group-list-active ()
5888   "List all groups that are available from the server(s)."
5889   (interactive)
5890   ;; First we make sure that we have really read the active file. 
5891   (unless gnus-have-read-active-file
5892     (let ((gnus-read-active-file t))
5893       (gnus-read-active-file)))
5894   ;; Find all groups and sort them.
5895   (let ((groups 
5896          (sort 
5897           (let (list)
5898             (mapatoms
5899              (lambda (sym)
5900                (and (symbol-value sym)
5901                     (setq list (cons (symbol-name sym) list))))
5902              gnus-active-hashtb)
5903             list)
5904           'string<))
5905         (buffer-read-only nil))
5906     (erase-buffer)
5907     (while groups
5908       (gnus-group-insert-group-line-info (car groups))
5909       (setq groups (cdr groups)))
5910     (goto-char (point-min))))
5911
5912 (defun gnus-activate-all-groups (level)
5913   "Activate absolutely all groups."
5914   (interactive (list 7))
5915   (let ((gnus-activate-level level)
5916         (gnus-activate-foreign-newsgroups level))
5917     (gnus-group-get-new-news)))
5918
5919 (defun gnus-group-get-new-news (&optional arg)
5920   "Get newly arrived articles.
5921 If ARG is a number, it specifies which levels you are interested in
5922 re-scanning.  If ARG is non-nil and not a number, this will force
5923 \"hard\" re-reading of the active files from all servers."
5924   (interactive "P")
5925   (run-hooks 'gnus-get-new-news-hook)
5926   ;; We might read in new NoCeM messages here.
5927   (and gnus-use-nocem (gnus-nocem-scan-groups))
5928   ;; If ARG is not a number, then we read the active file.
5929   (and arg
5930        (not (numberp arg))
5931        (progn
5932          (let ((gnus-read-active-file t))
5933            (gnus-read-active-file))
5934          (setq arg nil)))
5935
5936   (setq arg (gnus-group-default-level arg t))
5937   (if (and gnus-read-active-file (not arg))
5938       (progn
5939         (gnus-read-active-file)
5940         (gnus-get-unread-articles arg))
5941     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
5942       (gnus-get-unread-articles arg)))
5943   (gnus-group-list-groups))
5944
5945 (defun gnus-group-get-new-news-this-group (&optional n)
5946   "Check for newly arrived news in the current group (and the N-1 next groups).
5947 The difference between N and the number of newsgroup checked is returned.
5948 If N is negative, this group and the N-1 previous groups will be checked."
5949   (interactive "P")
5950   (let* ((groups (gnus-group-process-prefix n))
5951          (ret (if (numberp n) (- n (length groups)) 0))
5952          group)
5953     (while groups
5954       (setq group (car groups)
5955             groups (cdr groups))
5956       (gnus-group-remove-mark group)
5957       (or (gnus-get-new-news-in-group group)
5958           (progn 
5959             (ding) 
5960             (gnus-message 3 "%s error: %s" group (gnus-status-message group))
5961             (sit-for 2))))
5962     (gnus-group-next-unread-group 1 t)
5963     (gnus-summary-position-point)
5964     ret))
5965
5966 (defun gnus-get-new-news-in-group (group)
5967   (when (and group (gnus-activate-group group 'scan))
5968     (gnus-get-unread-articles-in-group 
5969      (gnus-get-info group) (gnus-active group))
5970     (when (gnus-group-goto-group group)
5971       (gnus-group-update-group-line))
5972     t))
5973
5974 (defun gnus-group-fetch-faq (group &optional faq-dir)
5975   "Fetch the FAQ for the current group."
5976   (interactive 
5977    (list
5978     (gnus-group-real-name (gnus-group-group-name))
5979     (cond (current-prefix-arg
5980            (completing-read 
5981             "Faq dir: " (and (listp gnus-group-faq-directory) 
5982                              gnus-group-faq-directory))))))
5983   (or faq-dir
5984       (setq faq-dir (if (listp gnus-group-faq-directory)
5985                         (car gnus-group-faq-directory)
5986                       gnus-group-faq-directory)))
5987   (or group (error "No group name given"))
5988   (let ((file (concat (file-name-as-directory faq-dir)
5989                       (gnus-group-real-name group))))
5990     (if (not (file-exists-p file))
5991         (error "No such file: %s" file)
5992       (find-file file))))
5993   
5994 (defun gnus-group-describe-group (force &optional group)
5995   "Display a description of the current newsgroup."
5996   (interactive (list current-prefix-arg (gnus-group-group-name)))
5997   (and force (setq gnus-description-hashtb nil))
5998   (let ((method (gnus-find-method-for-group group))
5999         desc)
6000     (or group (error "No group name given"))
6001     (and (or (and gnus-description-hashtb
6002                   ;; We check whether this group's method has been
6003                   ;; queried for a description file.  
6004                   (gnus-gethash 
6005                    (gnus-group-prefixed-name "" method) 
6006                    gnus-description-hashtb))
6007              (setq desc (gnus-group-get-description group))
6008              (gnus-read-descriptions-file method))
6009          (message 
6010           (or desc (gnus-gethash group gnus-description-hashtb)
6011               "No description available")))))
6012
6013 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6014 (defun gnus-group-describe-all-groups (&optional force)
6015   "Pop up a buffer with descriptions of all newsgroups."
6016   (interactive "P")
6017   (and force (setq gnus-description-hashtb nil))
6018   (if (not (or gnus-description-hashtb
6019                (gnus-read-all-descriptions-files)))
6020       (error "Couldn't request descriptions file"))
6021   (let ((buffer-read-only nil)
6022         b)
6023     (erase-buffer)
6024     (mapatoms
6025      (lambda (group)
6026        (setq b (point))
6027        (insert (format "      *: %-20s %s\n" (symbol-name group)
6028                        (symbol-value group)))
6029        (add-text-properties 
6030         b (1+ b) (list 'gnus-group group
6031                        'gnus-unread t 'gnus-marked nil
6032                        'gnus-level (1+ gnus-level-subscribed))))
6033      gnus-description-hashtb)
6034     (goto-char (point-min))
6035     (gnus-group-position-point)))
6036
6037 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6038 (defun gnus-group-apropos (regexp &optional search-description)
6039   "List all newsgroups that have names that match a regexp."
6040   (interactive "sGnus apropos (regexp): ")
6041   (let ((prev "")
6042         (obuf (current-buffer))
6043         groups des)
6044     ;; Go through all newsgroups that are known to Gnus.
6045     (mapatoms 
6046      (lambda (group)
6047        (and (symbol-name group)
6048             (string-match regexp (symbol-name group))
6049             (setq groups (cons (symbol-name group) groups))))
6050      gnus-active-hashtb)
6051     ;; Go through all descriptions that are known to Gnus. 
6052     (if search-description
6053         (mapatoms 
6054          (lambda (group)
6055            (and (string-match regexp (symbol-value group))
6056                 (gnus-active (symbol-name group))
6057                 (setq groups (cons (symbol-name group) groups))))
6058          gnus-description-hashtb))
6059     (if (not groups)
6060         (gnus-message 3 "No groups matched \"%s\"." regexp)
6061       ;; Print out all the groups.
6062       (save-excursion
6063         (pop-to-buffer "*Gnus Help*")
6064         (buffer-disable-undo (current-buffer))
6065         (erase-buffer)
6066         (setq groups (sort groups 'string<))
6067         (while groups
6068           ;; Groups may be entered twice into the list of groups.
6069           (if (not (string= (car groups) prev))
6070               (progn
6071                 (insert (setq prev (car groups)) "\n")
6072                 (if (and gnus-description-hashtb
6073                          (setq des (gnus-gethash (car groups) 
6074                                                  gnus-description-hashtb)))
6075                     (insert "  " des "\n"))))
6076           (setq groups (cdr groups)))
6077         (goto-char (point-min))))
6078     (pop-to-buffer obuf)))
6079
6080 (defun gnus-group-description-apropos (regexp)
6081   "List all newsgroups that have names or descriptions that match a regexp."
6082   (interactive "sGnus description apropos (regexp): ")
6083   (if (not (or gnus-description-hashtb
6084                (gnus-read-all-descriptions-files)))
6085       (error "Couldn't request descriptions file"))
6086   (gnus-group-apropos regexp t))
6087
6088 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6089 (defun gnus-group-list-matching (level regexp &optional all lowest) 
6090   "List all groups with unread articles that match REGEXP.
6091 If the prefix LEVEL is non-nil, it should be a number that says which
6092 level to cut off listing groups. 
6093 If ALL, also list groups with no unread articles.
6094 If LOWEST, don't list groups with level lower than LOWEST."
6095   (interactive "P\nsList newsgroups matching: ")
6096   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6097                            all (or lowest 1) regexp)
6098   (goto-char (point-min))
6099   (gnus-group-position-point))
6100
6101 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
6102   "List all groups that match REGEXP.
6103 If the prefix LEVEL is non-nil, it should be a number that says which
6104 level to cut off listing groups. 
6105 If LOWEST, don't list groups with level lower than LOWEST."
6106   (interactive "P\nsList newsgroups matching: ")
6107   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6108
6109 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6110 (defun gnus-group-save-newsrc (&optional force)
6111   "Save the Gnus startup files.
6112 If FORCE, force saving whether it is necessary or not."
6113   (interactive "P")
6114   (gnus-save-newsrc-file force))
6115
6116 (defun gnus-group-restart (&optional arg)
6117   "Force Gnus to read the .newsrc file."
6118   (interactive "P")
6119   (gnus-save-newsrc-file)
6120   (gnus-setup-news 'force)
6121   (gnus-group-list-groups arg))
6122
6123 (defun gnus-group-read-init-file ()
6124   "Read the Gnus elisp init file."
6125   (interactive)
6126   (gnus-read-init-file))
6127
6128 (defun gnus-group-check-bogus-groups (&optional silent)
6129   "Check bogus newsgroups.
6130 If given a prefix, don't ask for confirmation before removing a bogus
6131 group."
6132   (interactive "P")
6133   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6134   (gnus-group-list-groups))
6135
6136 (defun gnus-group-edit-global-kill (&optional article group)
6137   "Edit the global kill file.
6138 If GROUP, edit that local kill file instead."
6139   (interactive "P")
6140   (setq gnus-current-kill-article article)
6141   (gnus-kill-file-edit-file group)
6142   (gnus-message 
6143    6
6144    (substitute-command-keys
6145     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6146             (if group "local" "global")))))
6147
6148 (defun gnus-group-edit-local-kill (article group)
6149   "Edit a local kill file."
6150   (interactive (list nil (gnus-group-group-name)))
6151   (gnus-group-edit-global-kill article group))
6152
6153 (defun gnus-group-force-update ()
6154   "Update `.newsrc' file."
6155   (interactive)
6156   (gnus-save-newsrc-file))
6157
6158 (defun gnus-group-suspend ()
6159   "Suspend the current Gnus session.
6160 In fact, cleanup buffers except for group mode buffer.
6161 The hook gnus-suspend-gnus-hook is called before actually suspending."
6162   (interactive)
6163   (run-hooks 'gnus-suspend-gnus-hook)
6164   ;; Kill Gnus buffers except for group mode buffer.
6165   (let ((group-buf (get-buffer gnus-group-buffer)))
6166     ;; Do this on a separate list in case the user does a ^G before we finish
6167     (let ((gnus-buffer-list
6168            (delq group-buf (delq gnus-dribble-buffer
6169                                  (append gnus-buffer-list nil)))))
6170       (while gnus-buffer-list
6171         (gnus-kill-buffer (car gnus-buffer-list))
6172         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6173     (if group-buf
6174         (progn
6175           (setq gnus-buffer-list (list group-buf))
6176           (bury-buffer group-buf)
6177           (delete-windows-on group-buf t)))))
6178
6179 (defun gnus-group-clear-dribble ()
6180   "Clear all information from the dribble buffer."
6181   (interactive)
6182   (gnus-dribble-clear)
6183   (gnus-message 7 "Cleared dribble buffer"))
6184
6185 (defun gnus-group-exit ()
6186   "Quit reading news after updating .newsrc.eld and .newsrc.
6187 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6188   (interactive)
6189   (if (or noninteractive                ;For gnus-batch-kill
6190           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6191           (not gnus-interactive-exit)   ;Without confirmation
6192           gnus-expert-user
6193           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6194       (progn
6195         (run-hooks 'gnus-exit-gnus-hook)
6196         ;; Offer to save data from non-quitted summary buffers.
6197         (gnus-offer-save-summaries)
6198         ;; Save the newsrc file(s).
6199         (gnus-save-newsrc-file)
6200         ;; Kill-em-all.
6201         (gnus-close-backends)
6202         ;; Shut down the cache.
6203         (when gnus-use-cache
6204           (gnus-cache-open))
6205         ;; Reset everything.
6206         (gnus-clear-system))))
6207
6208 (defun gnus-close-backends ()
6209   ;; Send a close request to all backends that support such a request. 
6210   (let ((methods gnus-valid-select-methods)
6211         func)
6212     (while methods
6213       (if (fboundp (setq func (intern (concat (car (car methods))
6214                                               "-request-close"))))
6215           (funcall func))
6216       (setq methods (cdr methods)))))
6217
6218 (defun gnus-group-quit ()
6219   "Quit reading news without updating .newsrc.eld or .newsrc.
6220 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6221   (interactive)
6222   (when (or noninteractive              ;For gnus-batch-kill
6223             (zerop (buffer-size))
6224             (not (gnus-server-opened gnus-select-method))
6225             gnus-expert-user
6226             (not gnus-current-startup-file)
6227             (gnus-yes-or-no-p
6228              (format "Quit reading news without saving %s? "
6229                      (file-name-nondirectory gnus-current-startup-file))))
6230     (run-hooks 'gnus-exit-gnus-hook)
6231     (if gnus-use-full-window
6232         (delete-other-windows)
6233       (gnus-remove-some-windows))
6234     (gnus-dribble-save)
6235     (gnus-close-backends)
6236     ;; Shut down the cache.
6237     (when gnus-use-cache
6238       (gnus-cache-open))
6239     (gnus-clear-system)))
6240
6241 (defun gnus-offer-save-summaries ()
6242   "Offer to save all active summary buffers."
6243   (save-excursion
6244     (let ((buflist (buffer-list)) 
6245           buffers bufname)
6246       ;; Go through all buffers and find all summaries.
6247       (while buflist
6248         (and (setq bufname (buffer-name (car buflist)))
6249              (string-match "Summary" bufname)
6250              (save-excursion
6251                (set-buffer bufname)
6252                ;; We check that this is, indeed, a summary buffer.
6253                (and (eq major-mode 'gnus-summary-mode)
6254                     ;; Also make sure this isn't bogus.
6255                     gnus-newsgroup-prepared))
6256              (push bufname buffers))
6257         (setq buflist (cdr buflist)))
6258       ;; Go through all these summary buffers and offer to save them.
6259       (when buffers
6260         (map-y-or-n-p 
6261          "Update summary buffer %s? "
6262          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6263          buffers)))))
6264
6265 (defun gnus-group-describe-briefly ()
6266   "Give a one line description of the group mode commands."
6267   (interactive)
6268   (gnus-message 7 (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")))
6269
6270 (defun gnus-group-browse-foreign-server (method)
6271   "Browse a foreign news server.
6272 If called interactively, this function will ask for a select method
6273  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
6274 If not, METHOD should be a list where the first element is the method
6275 and the second element is the address."
6276   (interactive
6277    (list (let ((how (completing-read 
6278                      "Which backend: "
6279                      (append gnus-valid-select-methods gnus-server-alist)
6280                      nil t "nntp")))
6281            ;; We either got a backend name or a virtual server name.
6282            ;; If the first, we also need an address.
6283            (if (assoc how gnus-valid-select-methods)
6284                (list (intern how)
6285                      ;; Suggested by mapjph@bath.ac.uk.
6286                      (completing-read 
6287                       "Address: " 
6288                       (mapcar (lambda (server) (list server))
6289                               gnus-secondary-servers)))
6290              ;; We got a server name, so we find the method.
6291              (gnus-server-to-method how)))))
6292   (gnus-browse-foreign-server method))
6293
6294 \f
6295 ;;;
6296 ;;; Browse Server Mode
6297 ;;;
6298
6299 (defvar gnus-browse-mode-hook nil)
6300 (defvar gnus-browse-mode-map nil)
6301 (put 'gnus-browse-mode 'mode-class 'special)
6302
6303 (if gnus-browse-mode-map
6304     nil
6305   (setq gnus-browse-mode-map (make-keymap))
6306   (suppress-keymap gnus-browse-mode-map)
6307   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
6308   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
6309   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
6310   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
6311   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
6312   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
6313   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
6314   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
6315   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
6316   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
6317   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
6318   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
6319   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
6320   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
6321   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
6322   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
6323   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
6324   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
6325   )
6326
6327 (defvar gnus-browse-current-method nil)
6328 (defvar gnus-browse-return-buffer nil)
6329
6330 (defvar gnus-browse-buffer "*Gnus Browse Server*")
6331
6332 (defun gnus-browse-foreign-server (method &optional return-buffer)
6333   "Browse the server METHOD."
6334   (setq gnus-browse-current-method method)
6335   (setq gnus-browse-return-buffer return-buffer)
6336   (let ((gnus-select-method method)
6337         groups group)
6338     (gnus-message 5 "Connecting to %s..." (nth 1 method))
6339     (cond 
6340      ((not (gnus-check-server method))
6341       (gnus-message 
6342        1 "Unable to contact server: %s" (gnus-status-message method))
6343       nil)
6344      ((not (gnus-request-list method))
6345       (gnus-message 
6346        1 "Couldn't request list: %s" (gnus-status-message method))
6347       nil)
6348      (t
6349       (get-buffer-create gnus-browse-buffer)
6350       (gnus-add-current-to-buffer-list)
6351       (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
6352       (gnus-configure-windows 'browse)
6353       (buffer-disable-undo (current-buffer))
6354       (let ((buffer-read-only nil))
6355         (erase-buffer))
6356       (gnus-browse-mode)
6357       (setq mode-line-buffer-identification
6358             (list
6359              (format
6360               "Gnus: %%b {%s:%s}" (car method) (car (cdr method)))))
6361       (save-excursion
6362         (set-buffer nntp-server-buffer)
6363         (let ((cur (current-buffer)))
6364           (goto-char (point-min))
6365           (or (string= gnus-ignored-newsgroups "")
6366               (delete-matching-lines gnus-ignored-newsgroups))
6367           (while (re-search-forward 
6368                   "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
6369             (goto-char (match-end 1))
6370             (setq groups (cons (cons (match-string 1)
6371                                      (max 0 (- (1+ (read cur)) (read cur))))
6372                                groups)))))
6373       (setq groups (sort groups 
6374                          (lambda (l1 l2)
6375                            (string< (car l1) (car l2)))))
6376       (let ((buffer-read-only nil))
6377         (while groups
6378           (setq group (car groups))
6379           (insert 
6380            (format "K%7d: %s\n" (cdr group) (car group)))
6381           (setq groups (cdr groups))))
6382       (switch-to-buffer (current-buffer))
6383       (goto-char (point-min))
6384       (gnus-group-position-point)
6385       t))))
6386
6387 (defun gnus-browse-mode ()
6388   "Major mode for browsing a foreign server.
6389
6390 All normal editing commands are switched off.
6391
6392 \\<gnus-browse-mode-map>
6393 The only things you can do in this buffer is
6394
6395 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
6396 The group will be inserted into the group buffer upon exit from this
6397 buffer.  
6398
6399 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
6400
6401 3) `\\[gnus-browse-exit]' to return to the group buffer."
6402   (interactive)
6403   (kill-all-local-variables)
6404   (when (and menu-bar-mode
6405              (gnus-visual-p 'browse-menu 'menu))
6406     (gnus-browse-make-menu-bar))
6407   (gnus-simplify-mode-line)
6408   (setq major-mode 'gnus-browse-mode)
6409   (setq mode-name "Browse Server")
6410   (setq mode-line-process nil)
6411   (use-local-map gnus-browse-mode-map)
6412   (buffer-disable-undo (current-buffer))
6413   (setq truncate-lines t)
6414   (setq buffer-read-only t)
6415   (run-hooks 'gnus-browse-mode-hook))
6416
6417 (defun gnus-browse-read-group (&optional no-article)
6418   "Enter the group at the current line."
6419   (interactive)
6420   (let ((group (gnus-browse-group-name)))
6421     (or (gnus-group-read-ephemeral-group 
6422          group gnus-browse-current-method nil
6423          (cons (current-buffer) 'browse))
6424         (error "Couldn't enter %s" group))))
6425
6426 (defun gnus-browse-select-group ()
6427   "Select the current group."
6428   (interactive)
6429   (gnus-browse-read-group 'no))
6430
6431 (defun gnus-browse-next-group (n)
6432   "Go to the next group."
6433   (interactive "p")
6434   (prog1
6435       (forward-line n)
6436     (gnus-group-position-point)))
6437
6438 (defun gnus-browse-prev-group (n)
6439   "Go to the next group."
6440   (interactive "p")
6441   (gnus-browse-next-group (- n)))
6442
6443 (defun gnus-browse-unsubscribe-current-group (arg)
6444   "(Un)subscribe to the next ARG groups."
6445   (interactive "p")
6446   (and (eobp)
6447        (error "No group at current line."))
6448   (let ((ward (if (< arg 0) -1 1))
6449         (arg (abs arg)))
6450     (while (and (> arg 0)
6451                 (not (eobp))
6452                 (gnus-browse-unsubscribe-group)
6453                 (zerop (gnus-browse-next-group ward)))
6454       (setq arg (1- arg)))
6455     (gnus-group-position-point)
6456     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
6457     arg))
6458
6459 (defun gnus-browse-group-name ()
6460   (save-excursion
6461     (beginning-of-line)
6462     (when (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t)
6463       (gnus-group-prefixed-name (match-string 1) gnus-browse-current-method))))
6464   
6465 (defun gnus-browse-unsubscribe-group ()
6466   "Toggle subscription of the current group in the browse buffer."
6467   (let ((sub nil)
6468         (buffer-read-only nil)
6469         group)
6470     (save-excursion
6471       (beginning-of-line)
6472       ;; If this group it killed, then we want to subscribe it.
6473       (if (= (following-char) ?K) (setq sub t))
6474       (setq group (gnus-browse-group-name))
6475       (delete-char 1)
6476       (if sub
6477           (progn
6478             (gnus-group-change-level 
6479              (list t group gnus-level-default-subscribed
6480                    nil nil gnus-browse-current-method) 
6481              gnus-level-default-subscribed gnus-level-killed
6482              (and (car (nth 1 gnus-newsrc-alist))
6483                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
6484                                 gnus-newsrc-hashtb))
6485              t)
6486             (insert ? ))
6487         (gnus-group-change-level 
6488          group gnus-level-killed gnus-level-default-subscribed)
6489         (insert ?K)))
6490     t))
6491
6492 (defun gnus-browse-exit ()
6493   "Quit browsing and return to the group buffer."
6494   (interactive)
6495   (if (eq major-mode 'gnus-browse-mode)
6496       (kill-buffer (current-buffer)))
6497   (if gnus-browse-return-buffer
6498       (gnus-configure-windows 'server 'force)
6499     (gnus-configure-windows 'group 'force)
6500     (gnus-group-list-groups nil)))
6501
6502 (defun gnus-browse-describe-briefly ()
6503   "Give a one line description of the group mode commands."
6504   (interactive)
6505   (gnus-message 6
6506                 (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")))
6507       
6508 \f
6509 ;;;
6510 ;;; Gnus summary mode
6511 ;;;
6512
6513 (defvar gnus-summary-mode-map nil)
6514 (defvar gnus-summary-mark-map nil)
6515 (defvar gnus-summary-mscore-map nil)
6516 (defvar gnus-summary-article-map nil)
6517 (defvar gnus-summary-thread-map nil)
6518 (defvar gnus-summary-goto-map nil)
6519 (defvar gnus-summary-exit-map nil)
6520 (defvar gnus-summary-interest-map nil)
6521 (defvar gnus-summary-sort-map nil)
6522 (defvar gnus-summary-backend-map nil)
6523 (defvar gnus-summary-save-map nil)
6524 (defvar gnus-summary-wash-map nil)
6525 (defvar gnus-summary-wash-hide-map nil)
6526 (defvar gnus-summary-wash-highlight-map nil)
6527 (defvar gnus-summary-wash-time-map nil)
6528 (defvar gnus-summary-help-map nil)
6529 (defvar gnus-summary-limit-map nil)
6530
6531 (put 'gnus-summary-mode 'mode-class 'special)
6532
6533 (if gnus-summary-mode-map
6534     nil
6535   (setq gnus-summary-mode-map (make-keymap))
6536   (suppress-keymap gnus-summary-mode-map)
6537
6538   ;; Non-orthogonal keys
6539
6540   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
6541   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
6542   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
6543   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
6544   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
6545   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
6546   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
6547   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
6548   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
6549   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
6550   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
6551   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
6552   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
6553   (define-key gnus-summary-mode-map 
6554     "\M-s" 'gnus-summary-search-article-forward)
6555   (define-key gnus-summary-mode-map 
6556     "\M-r" 'gnus-summary-search-article-backward)
6557   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
6558   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
6559   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-article)
6560   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
6561   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
6562   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
6563   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
6564   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
6565   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
6566   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
6567   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
6568   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
6569   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
6570   (define-key gnus-summary-mode-map 
6571     "k" 'gnus-summary-kill-same-subject-and-select)
6572   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
6573   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
6574   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
6575   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
6576   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
6577   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
6578   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
6579   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
6580   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
6581   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
6582   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
6583   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
6584   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
6585   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
6586   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
6587   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
6588   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
6589   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
6590   (define-key gnus-summary-mode-map 
6591     "\C-c\M-\C-s" 'gnus-summary-limit-include-expunged)
6592   (define-key gnus-summary-mode-map 
6593     "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
6594   (define-key gnus-summary-mode-map 
6595     "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
6596   (define-key gnus-summary-mode-map 
6597     "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
6598   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
6599   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
6600   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
6601   (define-key gnus-summary-mode-map 
6602     "\C-x\C-s" 'gnus-summary-reselect-current-group)
6603   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
6604   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
6605   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
6606   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
6607   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
6608   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
6609   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
6610   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
6611   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
6612   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
6613   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
6614   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
6615   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
6616   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
6617   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
6618   (define-key gnus-summary-mode-map "V" 'gnus-version)
6619   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
6620   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
6621   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
6622   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
6623   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
6624   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
6625   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
6626   (define-key gnus-summary-mode-map "x" 'gnus-summary-limit-to-unread)
6627   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
6628   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
6629   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
6630 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
6631   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
6632   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
6633   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
6634   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
6635   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
6636
6637   (define-key gnus-summary-mode-map "*" 'gnus-cache-enter-article)
6638   (define-key gnus-summary-mode-map "\M-*" 'gnus-cache-remove-article)
6639
6640   ;; Sort of orthogonal keymap
6641   (define-prefix-command 'gnus-summary-mark-map)
6642   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
6643   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
6644   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
6645   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
6646   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
6647   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
6648   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
6649   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
6650   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
6651   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
6652   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
6653   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
6654   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
6655   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
6656   (define-key gnus-summary-mark-map "S" 'gnus-summary-limit-include-expunged)
6657   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
6658   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
6659   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
6660   (define-key gnus-summary-mark-map 
6661     "k" 'gnus-summary-kill-same-subject-and-select)
6662   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
6663
6664   (define-prefix-command 'gnus-summary-mscore-map)
6665   (define-key gnus-summary-mark-map "V" 'gnus-summary-mscore-map)
6666   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
6667   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
6668   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
6669   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
6670
6671   (define-key gnus-summary-mark-map "P" 'gnus-uu-mark-map)
6672   
6673   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
6674
6675   (define-prefix-command 'gnus-summary-limit-map)
6676   (define-key gnus-summary-mode-map "/" 'gnus-summary-limit-map)
6677   (define-key gnus-summary-limit-map "/" 'gnus-summary-limit-to-subject)
6678   (define-key gnus-summary-limit-map "n" 'gnus-summary-limit-to-articles)
6679   (define-key gnus-summary-limit-map "w" 'gnus-summary-pop-limit)
6680   (define-key gnus-summary-limit-map "s" 'gnus-summary-limit-to-subject)
6681   (define-key gnus-summary-limit-map "u" 'gnus-summary-limit-to-unread)
6682   (define-key gnus-summary-limit-map "m" 'gnus-summary-limit-to-marks)
6683   (define-key gnus-summary-limit-map "v" 'gnus-summary-limit-to-score)
6684   (define-key gnus-summary-limit-map "D" 'gnus-summary-limit-include-dormant)
6685   (define-key gnus-summary-limit-map "d" 'gnus-summary-limit-exclude-dormant)
6686 ;  (define-key gnus-summary-limit-map "t" 'gnus-summary-limit-exclude-thread)
6687   (define-key gnus-summary-mark-map "E" 'gnus-summary-limit-include-expunged)
6688   (define-key gnus-summary-limit-map "c" 
6689     'gnus-summary-limit-exclude-childless-dormant)
6690
6691   (define-prefix-command 'gnus-summary-goto-map)
6692   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
6693   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
6694   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
6695   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
6696   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
6697   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
6698   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
6699   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
6700   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
6701   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
6702   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
6703   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
6704   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
6705   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
6706
6707
6708   (define-prefix-command 'gnus-summary-thread-map)
6709   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
6710   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
6711   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
6712   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
6713   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
6714   (define-key gnus-summary-thread-map "T" 'gnus-summary-rethread-current)
6715   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
6716   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
6717   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
6718   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
6719   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
6720   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
6721   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
6722   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
6723   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
6724   (define-key gnus-summary-thread-map "\M-#" 'gnus-uu-unmark-thread)
6725
6726   
6727   (define-prefix-command 'gnus-summary-exit-map)
6728   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
6729   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
6730   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
6731   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
6732   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
6733   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
6734   (define-key gnus-summary-exit-map 
6735     "n" 'gnus-summary-catchup-and-goto-next-group)
6736   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
6737   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
6738   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
6739   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
6740
6741
6742   (define-prefix-command 'gnus-summary-article-map)
6743   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
6744   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
6745   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
6746   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
6747   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
6748   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
6749   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
6750   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
6751   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
6752   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
6753   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
6754   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
6755   (define-key gnus-summary-article-map "R" 'gnus-summary-refer-references)
6756   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
6757   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
6758
6759
6760
6761   (define-prefix-command 'gnus-summary-wash-map)
6762   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
6763
6764   (define-prefix-command 'gnus-summary-wash-hide-map)
6765   (define-key gnus-summary-wash-map "W" 'gnus-summary-wash-hide-map)
6766   (define-key gnus-summary-wash-hide-map "a" 'gnus-article-hide)
6767   (define-key gnus-summary-wash-hide-map "h" 'gnus-article-hide-headers)
6768   (define-key gnus-summary-wash-hide-map "s" 'gnus-article-hide-signature)
6769   (define-key gnus-summary-wash-hide-map "c" 'gnus-article-hide-citation)
6770   (define-key gnus-summary-wash-hide-map "p" 'gnus-article-hide-pgp)
6771   (define-key gnus-summary-wash-hide-map 
6772     "\C-c" 'gnus-article-hide-citation-maybe)
6773
6774   (define-prefix-command 'gnus-summary-wash-highlight-map)
6775   (define-key gnus-summary-wash-map "H" 'gnus-summary-wash-highlight-map)
6776   (define-key gnus-summary-wash-highlight-map "a" 'gnus-article-highlight)
6777   (define-key gnus-summary-wash-highlight-map 
6778     "h" 'gnus-article-highlight-headers)
6779   (define-key gnus-summary-wash-highlight-map
6780     "c" 'gnus-article-highlight-citation)
6781   (define-key gnus-summary-wash-highlight-map
6782     "s" 'gnus-article-highlight-signature)
6783
6784   (define-prefix-command 'gnus-summary-wash-time-map)
6785   (define-key gnus-summary-wash-map "T" 'gnus-summary-wash-time-map)
6786   (define-key gnus-summary-wash-time-map "z" 'gnus-article-date-ut)
6787   (define-key gnus-summary-wash-time-map "u" 'gnus-article-date-ut)
6788   (define-key gnus-summary-wash-time-map "l" 'gnus-article-date-local)
6789   (define-key gnus-summary-wash-time-map "e" 'gnus-article-date-lapsed)
6790   (define-key gnus-summary-wash-time-map "o" 'gnus-article-date-original)
6791
6792   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
6793   (define-key gnus-summary-wash-map "B" 'gnus-article-add-buttons-to-head)
6794   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
6795   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
6796   (define-key gnus-summary-wash-map "c" 'gnus-article-remove-cr)
6797   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
6798   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
6799   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
6800   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
6801   (define-key gnus-summary-wash-map "t" 'gnus-summary-toggle-header)
6802   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
6803
6804
6805   (define-prefix-command 'gnus-summary-help-map)
6806   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
6807   (define-key gnus-summary-help-map "v" 'gnus-version)
6808   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
6809   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
6810   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
6811   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
6812
6813
6814   (define-prefix-command 'gnus-summary-backend-map)
6815   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
6816   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
6817   (define-key gnus-summary-backend-map "\M-\C-e" 
6818     'gnus-summary-expire-articles-now)
6819   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
6820   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
6821   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
6822   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
6823   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
6824   (define-key gnus-summary-backend-map "q" 'gnus-summary-respool-query)
6825   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
6826
6827
6828   (define-prefix-command 'gnus-summary-save-map)
6829   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
6830   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
6831   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
6832   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
6833   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
6834   (define-key gnus-summary-save-map "b" 'gnus-summary-save-article-body-file)
6835   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
6836   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
6837   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
6838   (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
6839
6840   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
6841
6842   (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
6843   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
6844
6845   (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
6846
6847   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
6848   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
6849   )
6850
6851
6852 \f
6853
6854 (defun gnus-summary-mode (&optional group)
6855   "Major mode for reading articles.
6856
6857 All normal editing commands are switched off.
6858 \\<gnus-summary-mode-map>
6859 Each line in this buffer represents one article.  To read an
6860 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6861 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
6862 respectively.
6863
6864 You can also post articles and send mail from this buffer.  To 
6865 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
6866 of an article, type `\\[gnus-summary-reply]'.
6867
6868 There are approx. one gazillion commands you can execute in this 
6869 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
6870
6871 The following commands are available:
6872
6873 \\{gnus-summary-mode-map}"
6874   (interactive)
6875   (when (and menu-bar-mode
6876              (gnus-visual-p 'summary-menu 'menu))
6877     (gnus-summary-make-menu-bar))
6878   (kill-all-local-variables)
6879   (let ((locals gnus-summary-local-variables))
6880     (while locals
6881       (if (consp (car locals))
6882           (progn
6883             (make-local-variable (car (car locals)))
6884             (set (car (car locals)) (eval (cdr (car locals)))))
6885         (make-local-variable (car locals))
6886         (set (car locals) nil))
6887       (setq locals (cdr locals))))
6888   (gnus-make-thread-indent-array)
6889   (gnus-simplify-mode-line)
6890   (setq major-mode 'gnus-summary-mode)
6891   (setq mode-name "Summary")
6892   (make-local-variable 'minor-mode-alist)
6893   (use-local-map gnus-summary-mode-map)
6894   (buffer-disable-undo (current-buffer))
6895   (setq buffer-read-only t)             ;Disable modification
6896   (setq truncate-lines t)
6897   (setq selective-display t)
6898   (setq selective-display-ellipses t)   ;Display `...'
6899   (setq buffer-display-table gnus-summary-display-table)
6900   (setq gnus-newsgroup-name group)
6901   (run-hooks 'gnus-summary-mode-hook))
6902
6903 (defun gnus-summary-make-display-table ()
6904   ;; Change the display table.  Odd characters have a tendency to mess
6905   ;; up nicely formatted displays - we make all possible glyphs
6906   ;; display only a single character.
6907
6908   ;; We start from the standard display table, if any.
6909   (setq gnus-summary-display-table 
6910         (or (copy-sequence standard-display-table)
6911             (make-display-table)))
6912   ;; Nix out all the control chars...
6913   (let ((i 32))
6914     (while (>= (setq i (1- i)) 0)
6915       (aset gnus-summary-display-table i [??])))
6916   ;; ... but not newline and cr, of course. (cr is necessary for the
6917   ;; selective display).  
6918   (aset gnus-summary-display-table ?\n nil)
6919   (aset gnus-summary-display-table ?\r nil)
6920   ;; We nix out any glyphs over 126 that are not set already.  
6921   (let ((i 256))
6922     (while (>= (setq i (1- i)) 127)
6923       ;; Only modify if the entry is nil.
6924       (or (aref gnus-summary-display-table i) 
6925           (aset gnus-summary-display-table i [??])))))
6926
6927 (defun gnus-summary-clear-local-variables ()
6928   (let ((locals gnus-summary-local-variables))
6929     (while locals
6930       (if (consp (car locals))
6931           (and (vectorp (car (car locals)))
6932                (set (car (car locals)) nil))
6933         (and (vectorp (car locals))
6934              (set (car locals) nil)))
6935       (setq locals (cdr locals)))))
6936
6937 ;; Summary data functions.
6938
6939 (defmacro gnus-data-number (data)
6940   `(car ,data))
6941
6942 (defmacro gnus-data-mark (data)
6943   `(nth 1 ,data))
6944
6945 (defmacro gnus-data-set-mark (data mark)
6946   `(setcar (nthcdr 1 ,data) ,mark))
6947
6948 (defmacro gnus-data-pos (data)
6949   `(nth 2 ,data))
6950
6951 (defmacro gnus-data-set-pos (data pos)
6952   `(setcar (nthcdr 2 ,data) ,pos))
6953
6954 (defmacro gnus-data-header (data)
6955   `(nth 3 ,data))
6956
6957 (defmacro gnus-data-level (data)
6958   `(nth 4 ,data))
6959
6960 (defmacro gnus-data-unread-p (data)
6961   `(= (nth 1 ,data) gnus-unread-mark))
6962
6963 (defmacro gnus-data-pseudo-p (data)
6964   `(consp (nth 3 ,data)))
6965
6966 (defmacro gnus-data-find (number)
6967   `(assq ,number gnus-newsgroup-data))
6968
6969 (defmacro gnus-data-find-list (number &optional data)
6970   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
6971      (memq (assq ,number bdata)
6972            bdata)))
6973
6974 (defmacro gnus-data-make (number mark pos header level)
6975   `(list ,number ,mark ,pos ,header ,level))
6976
6977 (defun gnus-data-enter (after-article number mark pos header level offset)
6978   (let ((data (gnus-data-find-list after-article)))
6979     (or data (error "No such article: %d" after-article))
6980     (setcdr data (cons (gnus-data-make number mark pos header level)
6981                        (cdr data)))
6982     (setq gnus-newsgroup-data-reverse nil)
6983     (gnus-data-update-list (cdr (cdr data)) offset)))
6984
6985 (defun gnus-data-enter-list (after-article list &optional offset)
6986   (when list
6987     (let ((data (and after-article (gnus-data-find-list after-article)))
6988           (ilist list))
6989       (or data (not after-article) (error "No such article: %d" after-article))
6990       ;; Find the last element in the list to be spliced into the main
6991       ;; list.  
6992       (while (cdr list)
6993         (setq list (cdr list)))
6994       (if (not data)
6995           (progn
6996             (setcdr list gnus-newsgroup-data)
6997             (setq gnus-newsgroup-data ilist)
6998             (and offset (gnus-data-update-list (cdr list) offset)))
6999         (setcdr list (cdr data))
7000         (setcdr data ilist)
7001         (and offset (gnus-data-update-list (cdr data) offset)))
7002       (setq gnus-newsgroup-data-reverse nil))))
7003
7004 (defun gnus-data-remove (article &optional offset)
7005   (let ((data gnus-newsgroup-data))
7006     (if (= (gnus-data-number (car data)) article)
7007         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7008               gnus-newsgroup-data-reverse nil)
7009       (while (cdr data)
7010         (and (= (gnus-data-number (car (cdr data))) article)
7011              (progn
7012                (setcdr data (cdr (cdr data)))
7013                (and offset (gnus-data-update-list (cdr data) offset))
7014                (setq data nil
7015                      gnus-newsgroup-data-reverse nil)))
7016         (setq data (cdr data))))))
7017
7018 (defmacro gnus-data-list (backward)
7019   `(if ,backward
7020        (or gnus-newsgroup-data-reverse
7021            (setq gnus-newsgroup-data-reverse
7022                  (reverse gnus-newsgroup-data)))
7023      gnus-newsgroup-data))
7024
7025 (defun gnus-data-update-list (data offset)
7026   "Add OFFSET to the POS of all data entries in DATA."
7027   (while data
7028     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7029     (setq data (cdr data))))
7030
7031 (defun gnus-data-compute-positions ()
7032   "Compute the positions of all articles."
7033   (let ((data gnus-newsgroup-data)
7034         pos)
7035     (while data
7036       (when (setq pos (text-property-any 
7037                        (point-min) (point-max)
7038                        'gnus-number (gnus-data-number (car data))))
7039         (gnus-data-set-pos (car data) (+ pos 3)))
7040       (setq data (cdr data)))))
7041
7042 (defun gnus-summary-article-pseudo-p (article)
7043   "Say whether this article is a pseudo article or not."
7044   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7045
7046 (defun gnus-article-parent-p (number)
7047   "Say whether this article is a parent or not."
7048   (let* ((data (gnus-data-find-list number)))
7049     (and (cdr data)                     ; There has to be an article after...
7050          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7051             (gnus-data-level (nth 1 data))))))
7052     
7053 (defmacro gnus-summary-skip-intangible ()
7054   "If the current article is intangible, then jump to a different article."
7055   '(let ((to (get-text-property (point) 'gnus-intangible)))
7056     (when to
7057       (gnus-summary-goto-subject to))))
7058
7059 (defmacro gnus-summary-article-intangible-p ()
7060   "Say whether this article is intangible or not."
7061   '(get-text-property (point) 'gnus-intangible))
7062
7063 ;; Some summary mode macros.
7064
7065 (defmacro gnus-summary-article-number ()
7066   "The article number of the article on the current line.
7067 If there isn's an article number here, then we return the current
7068 article number."
7069   '(progn
7070      (gnus-summary-skip-intangible)
7071      (or (get-text-property (point) 'gnus-number) 
7072          (gnus-summary-last-subject))))
7073
7074 (defmacro gnus-summary-article-header (&optional number)
7075   `(gnus-data-header (gnus-data-find
7076                       ,(or number '(gnus-summary-article-number)))))
7077
7078 (defmacro gnus-summary-thread-level (&optional number)
7079   `(gnus-data-level (gnus-data-find
7080                      ,(or number '(gnus-summary-article-number)))))
7081
7082 (defmacro gnus-summary-article-mark (&optional number)
7083   `(gnus-data-mark (gnus-data-find
7084                     ,(or number '(gnus-summary-article-number)))))
7085
7086 (defmacro gnus-summary-article-pos (&optional number)
7087   `(gnus-data-pos (gnus-data-find
7088                    ,(or number '(gnus-summary-article-number)))))
7089
7090 (defmacro gnus-summary-article-subject (&optional number)
7091   "Return current subject string or nil if nothing."
7092   `(let ((headers 
7093           ,(if number
7094                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7095              '(gnus-data-header (assq (gnus-summary-article-number)
7096                                       gnus-newsgroup-data)))))
7097      (and headers
7098           (vectorp headers)
7099           (mail-header-subject headers))))
7100
7101 (defmacro gnus-summary-article-score (&optional number)
7102   "Return current article score."
7103   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7104                   gnus-newsgroup-scored))
7105        gnus-summary-default-score 0))
7106
7107 (defun gnus-summary-article-children (&optional number)
7108   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7109          (level (gnus-data-level (car data)))
7110          l children)
7111     (while (and (setq data (cdr data))
7112                 (> (setq l (gnus-data-level (car data))) level))
7113       (and (= (1+ level) l)
7114            (setq children (cons (gnus-data-number (car data))
7115                                 children))))
7116     (nreverse children)))
7117
7118 (defun gnus-summary-article-parent (&optional number)
7119   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7120                                     (gnus-data-list t)))
7121          (level (gnus-data-level (car data)))
7122          l)
7123     (if (zerop level)
7124         () ; This is a root.
7125       ;; We search until we find an article with a level less than
7126       ;; this one.  That function has to be the parent.
7127       (while (and (setq data (cdr data))
7128                   (not (< (gnus-data-level (car data)) level))))
7129       (and data (gnus-data-number (car data))))))
7130
7131
7132 ;; Various summary mode internalish functions.
7133
7134 (defun gnus-mouse-pick-article (e)
7135   (interactive "e")
7136   (mouse-set-point e)
7137   (gnus-summary-next-page nil t))
7138
7139 (defun gnus-summary-setup-buffer (group)
7140   "Initialize summary buffer."
7141   (let ((buffer (concat "*Summary " group "*")))
7142     (if (get-buffer buffer)
7143         (progn
7144           (set-buffer buffer)
7145           (setq gnus-summary-buffer (current-buffer))
7146           (not gnus-newsgroup-prepared))
7147       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7148       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7149       (gnus-add-current-to-buffer-list)
7150       (gnus-summary-mode group)
7151       (when gnus-carpal 
7152         (gnus-carpal-setup-buffer 'summary))
7153       (unless gnus-single-article-buffer
7154         (make-local-variable 'gnus-article-buffer))
7155       (setq gnus-newsgroup-name group)
7156       t)))
7157
7158 (defun gnus-set-global-variables ()
7159   ;; Set the global equivalents of the summary buffer-local variables
7160   ;; to the latest values they had.  These reflect the summary buffer
7161   ;; that was in action when the last article was fetched.
7162   (when (eq major-mode 'gnus-summary-mode) 
7163     (setq gnus-summary-buffer (current-buffer))
7164     (let ((name gnus-newsgroup-name)
7165           (marked gnus-newsgroup-marked)
7166           (unread gnus-newsgroup-unreads)
7167           (headers gnus-current-headers)
7168           (data gnus-newsgroup-data)
7169           (article-buffer gnus-article-buffer)
7170           (score-file gnus-current-score-file))
7171       (save-excursion
7172         (set-buffer gnus-group-buffer)
7173         (setq gnus-newsgroup-name name)
7174         (setq gnus-newsgroup-marked marked)
7175         (setq gnus-newsgroup-unreads unread)
7176         (setq gnus-current-headers headers)
7177         (setq gnus-newsgroup-data data)
7178         (setq gnus-article-buffer article-buffer)
7179         (setq gnus-current-score-file score-file)))))
7180
7181 (defun gnus-summary-last-article-p (&optional article)
7182   "Return whether ARTICLE is the last article in the buffer."
7183   (if (not (setq article (or article (gnus-summary-article-number))))
7184       t ; All non-existant numbers are the last article. :-)
7185     (cdr (gnus-data-find-list article))))
7186     
7187 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7188   "Insert a dummy root in the summary buffer."
7189   (beginning-of-line)
7190   (add-text-properties
7191    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7192    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7193
7194 (defvar gnus-thread-indent-array nil)
7195 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7196 (defun gnus-make-thread-indent-array ()
7197   (let ((n 200))
7198     (if (and gnus-thread-indent-array
7199              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7200         nil
7201       (setq gnus-thread-indent-array (make-vector 201 "")
7202             gnus-thread-indent-array-level gnus-thread-indent-level)
7203       (while (>= n 0)
7204         (aset gnus-thread-indent-array n
7205               (make-string (* n gnus-thread-indent-level) ? ))
7206         (setq n (1- n))))))
7207
7208 (defun gnus-summary-insert-line 
7209   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread 
7210                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7211                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7212   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7213          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7214          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7215          (gnus-tmp-score-char
7216           (if (or (null gnus-summary-default-score)
7217                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7218                       gnus-summary-zcore-fuzz)) ? 
7219             (if (< gnus-tmp-score gnus-summary-default-score)
7220                 gnus-score-below-mark gnus-score-over-mark)))
7221          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7222                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7223                                   gnus-cached-mark)
7224                                  (gnus-tmp-replied gnus-replied-mark)
7225                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7226                                   gnus-saved-mark)
7227                                  (t gnus-unread-mark)))
7228          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7229          (gnus-tmp-name 
7230           (cond 
7231            ((string-match "(.+)" gnus-tmp-from)
7232             (substring gnus-tmp-from 
7233                        (1+ (match-beginning 0)) (1- (match-end 0))))
7234            ((string-match "<[^>]+> *$" gnus-tmp-from)
7235             (let ((beg (match-beginning 0)))
7236               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7237                        (substring gnus-tmp-from (1+ (match-beginning 0))
7238                                   (1- (match-end 0))))
7239                   (substring gnus-tmp-from 0 beg))))
7240            (t gnus-tmp-from)))
7241          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7242          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7243          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7244          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7245          (buffer-read-only nil))
7246     (when (string= gnus-tmp-name "")
7247       (setq gnus-tmp-name gnus-tmp-from))
7248     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7249     (put-text-property
7250      (point)
7251      (progn (eval gnus-summary-line-format-spec) (point))
7252      'gnus-number gnus-tmp-number)
7253     (when (gnus-visual-p 'summary-highlight 'highlight)
7254       (forward-line -1)
7255       (run-hooks 'gnus-summary-update-hook)
7256       (forward-line 1))))
7257
7258 (defun gnus-summary-update-line (&optional dont-update)
7259   ;; Update summary line after change.
7260   (when (and gnus-summary-default-score
7261              (not gnus-summary-inhibit-highlight))
7262     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7263            (article (gnus-summary-article-number))
7264            (score (gnus-summary-article-score article)))
7265       (unless dont-update
7266         (if (and gnus-summary-mark-below
7267                  (< (gnus-summary-article-score)
7268                     gnus-summary-mark-below))
7269             ;; This article has a low score, so we mark it as read.
7270             (when (memq article gnus-newsgroup-unreads)
7271               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7272           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7273             ;; This article was previously marked as read on account
7274             ;; of a low score, but now it has risen, so we mark it as
7275             ;; unread. 
7276             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7277         (gnus-summary-update-mark
7278          (if (or (null gnus-summary-default-score)
7279                  (<= (abs (- score gnus-summary-default-score))
7280                      gnus-summary-zcore-fuzz)) ? 
7281            (if (< score gnus-summary-default-score)
7282                gnus-score-below-mark gnus-score-over-mark)) 'score))
7283       ;; Do visual highlighting.
7284       (when (gnus-visual-p 'summary-highlight 'highlight)
7285         (run-hooks 'gnus-summary-update-hook)))))
7286
7287 (defvar gnus-tmp-new-adopts)
7288
7289 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7290   ;; Sum up all elements (and sub-elements) in a list.
7291   (let* ((number
7292           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7293           (cond ((and (consp thread) (cdr thread))
7294                  (apply
7295                   '+ 1 (mapcar
7296                         'gnus-summary-number-of-articles-in-thread 
7297                         (cdr thread))))
7298                 ((null thread)
7299                  1)
7300                 ((and level (zerop level) gnus-tmp-new-adopts)
7301                  (apply '+ 1 (mapcar 
7302                               'gnus-summary-number-of-articles-in-thread 
7303                               gnus-tmp-new-adopts)))
7304                 ((memq (mail-header-number (car thread))
7305                        gnus-newsgroup-limit)
7306                  1) 
7307                 (t 1))))
7308     (if char 
7309         (if (> number 1) gnus-not-empty-thread-mark
7310           gnus-empty-thread-mark)
7311       number)))
7312
7313 (defun gnus-summary-set-local-parameters (group)
7314  "Go through the local params of GROUP and set all variable specs in that list."
7315   (let ((params (gnus-info-params (gnus-get-info group)))
7316         elem)
7317     (while params
7318       (setq elem (car params)
7319             params (cdr params))
7320       (and (consp elem)                 ; Has to be a cons.
7321            (consp (cdr elem))           ; The cdr has to be a list.
7322            (symbolp (car elem))         ; Has to be a symbol in there.
7323            (progn                       ; So we set it.
7324              (make-local-variable (car elem))
7325              (set (car elem) (eval (nth 1 elem))))))))
7326
7327 (defun gnus-summary-read-group 
7328   (group &optional show-all no-article kill-buffer no-display)
7329   "Start reading news in newsgroup GROUP.
7330 If SHOW-ALL is non-nil, already read articles are also listed.
7331 If NO-ARTICLE is non-nil, no article is selected initially.
7332 If NO-DISPLAY, don't generate a summary buffer."
7333   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7334   (let* ((new-group (gnus-summary-setup-buffer group))
7335          (quit-config (gnus-group-quit-config group))
7336          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7337     (cond 
7338      ;; This summary buffer exists already, so we just select it. 
7339      ((not new-group)
7340       (gnus-set-global-variables)
7341       (gnus-kill-or-deaden-summary kill-buffer)
7342       (gnus-configure-windows 'summary 'force)
7343       (gnus-set-mode-line 'summary)
7344       (gnus-summary-position-point)
7345       (message "")
7346       t)
7347      ;; We couldn't select this group.
7348      ((null did-select) 
7349       (when (and (eq major-mode 'gnus-summary-mode)
7350                  (not (equal (current-buffer) kill-buffer)))
7351         (kill-buffer (current-buffer))
7352         (if (not quit-config)
7353             (progn
7354               (set-buffer gnus-group-buffer)
7355               (gnus-group-jump-to-group group)
7356               (gnus-group-next-unread-group 1))
7357           (if (not (buffer-name (car quit-config)))
7358               (gnus-configure-windows 'group 'force)
7359             (set-buffer (car quit-config))
7360             (and (eq major-mode 'gnus-summary-mode)
7361                  (gnus-set-global-variables))
7362             (gnus-configure-windows (cdr quit-config)))))
7363       (gnus-message 3 "Can't select group")
7364       nil)
7365      ;; The user did a `C-g' while prompting for number of articles,
7366      ;; so we exit this group.
7367      ((eq did-select 'quit)
7368       (and (eq major-mode 'gnus-summary-mode)
7369            (not (equal (current-buffer) kill-buffer))
7370            (kill-buffer (current-buffer)))
7371       (gnus-kill-or-deaden-summary kill-buffer)
7372       (if (not quit-config)
7373           (progn
7374             (set-buffer gnus-group-buffer)
7375             (gnus-group-jump-to-group group)
7376             (gnus-group-next-unread-group 1)
7377             (gnus-configure-windows 'group 'force))
7378         (if (not (buffer-name (car quit-config)))
7379             (gnus-configure-windows 'group 'force)
7380           (set-buffer (car quit-config))
7381           (and (eq major-mode 'gnus-summary-mode)
7382                (gnus-set-global-variables))
7383           (gnus-configure-windows (cdr quit-config))))
7384       ;; Finallt signal the quit.
7385       (signal 'quit nil))
7386      ;; The group was successfully selected.
7387      (t
7388       (gnus-set-global-variables)
7389       ;; Save the active value in effect when the group was entered.
7390       (setq gnus-newsgroup-active 
7391             (gnus-copy-sequence
7392              (gnus-active gnus-newsgroup-name)))
7393       ;; You can change the summary buffer in some way with this hook.
7394       (run-hooks 'gnus-select-group-hook)
7395       ;; Set any local variables in the group parameters.
7396       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7397       ;; Do score processing.
7398       (when gnus-use-scoring
7399         (gnus-possibly-score-headers))
7400       (gnus-update-format-specifications)
7401       ;; Find the initial limit.
7402       (gnus-summary-initial-limit)
7403       ;; Generate the summary buffer.
7404       (unless no-display
7405         (gnus-summary-prepare))
7406       ;; If the summary buffer is empty, but there are some low-scored
7407       ;; articles or some excluded dormants, we include these in the
7408       ;; buffer. 
7409       (when (zerop (buffer-size))
7410         (cond (gnus-newsgroup-dormant
7411                (gnus-summary-limit-include-dormant))
7412               ((and gnus-newsgroup-scored show-all)
7413                (gnus-summary-limit-include-expunged))))
7414       ;; Function `gnus-apply-kill-file' must be called in this hook.
7415       (run-hooks 'gnus-apply-kill-hook)
7416       (if (zerop (buffer-size))
7417           (progn
7418             ;; This newsgroup is empty.
7419             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7420             (gnus-message 6 "No unread news")
7421             (gnus-kill-or-deaden-summary kill-buffer)
7422             ;; Return nil from this function.
7423             nil)
7424         ;; Hide conversation thread subtrees.  We cannot do this in
7425         ;; gnus-summary-prepare-hook since kill processing may not
7426         ;; work with hidden articles.
7427         (and gnus-show-threads
7428              gnus-thread-hide-subtree
7429              (gnus-summary-hide-all-threads))
7430         ;; Show first unread article if requested.
7431         (if (and (not no-article)
7432                  gnus-newsgroup-unreads
7433                  gnus-auto-select-first)
7434             (progn
7435               (if (eq gnus-auto-select-first 'best)
7436                   (gnus-summary-best-unread-article)
7437                 (gnus-summary-first-unread-article)))
7438           ;; Don't select any articles, just move point to the first
7439           ;; article in the group.
7440           (goto-char (point-min))
7441           (gnus-summary-position-point)
7442           (gnus-set-mode-line 'summary)
7443           (gnus-configure-windows 'summary 'force))
7444         ;; If we are in async mode, we send some info to the backend.
7445         (when gnus-newsgroup-async
7446           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7447         (gnus-kill-or-deaden-summary kill-buffer)
7448         (when (get-buffer-window gnus-group-buffer)
7449           ;; Gotta use windows, because recenter does wierd stuff if
7450           ;; the current buffer ain't the displayed window.
7451           (let ((owin (selected-window))) 
7452             (select-window (get-buffer-window gnus-group-buffer))
7453             (when (gnus-group-goto-group group)
7454               (recenter))
7455             (select-window owin))))
7456       ;; Mark this buffer as "prepared".
7457       (setq gnus-newsgroup-prepared t)
7458       t))))
7459
7460 (defun gnus-summary-prepare ()
7461   "Generate the summary buffer."
7462   (let ((buffer-read-only nil))
7463     (erase-buffer)
7464     (setq gnus-newsgroup-data nil
7465           gnus-newsgroup-data-reverse nil)
7466     (run-hooks 'gnus-summary-generate-hook)
7467     ;; Generate the buffer, either with threads or without.
7468     (gnus-summary-prepare-threads 
7469      (if gnus-show-threads
7470          (gnus-gather-threads (gnus-sort-threads (gnus-make-threads)))
7471        gnus-newsgroup-headers))
7472     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7473     ;; Call hooks for modifying summary buffer.
7474     (goto-char (point-min))
7475     (run-hooks 'gnus-summary-prepare-hook)))
7476
7477 (defun gnus-gather-threads (threads)
7478   "Gather threads that have lost their roots."
7479   (if (not gnus-summary-make-false-root)
7480       threads 
7481     (let ((hashtb (gnus-make-hashtable 1023))
7482           (prev threads)
7483           (result threads)
7484           subject hthread whole-subject)
7485       (while threads
7486         (setq whole-subject 
7487               (setq subject (mail-header-subject (car (car threads)))))
7488         (if (and gnus-summary-gather-exclude-subject
7489                  (string-match gnus-summary-gather-exclude-subject
7490                                subject))
7491             () ; We don't want to do anything with this.
7492           (if gnus-summary-gather-subject-limit
7493               (or (and (numberp gnus-summary-gather-subject-limit)
7494                        (> (length subject) gnus-summary-gather-subject-limit)
7495                        (setq subject
7496                              (substring subject 0 
7497                                         gnus-summary-gather-subject-limit)))
7498                   (and (eq 'fuzzy gnus-summary-gather-subject-limit)
7499                        (setq subject (gnus-simplify-subject-fuzzy subject))))
7500             (setq subject (gnus-simplify-subject-re subject)))
7501           (if (setq hthread 
7502                     (gnus-gethash subject hashtb))
7503               (progn
7504                 (or (stringp (car (car hthread)))
7505                     (setcar hthread (list whole-subject (car hthread))))
7506                 (setcdr (car hthread) (nconc (cdr (car hthread)) 
7507                                              (list (car threads))))
7508                 (setcdr prev (cdr threads))
7509                 (setq threads prev))
7510             (gnus-sethash subject threads hashtb)))
7511         (setq prev threads)
7512         (setq threads (cdr threads)))
7513       result)))
7514
7515 (defun gnus-make-threads ()
7516   "Go through the dependency hashtb and find the roots.  Return all threads."
7517   ;; Then we find all the roots and return all the threads.
7518   (let (threads)
7519     (mapatoms
7520      (lambda (refs)
7521        (or (car (symbol-value refs))
7522            (setq threads (append (cdr (symbol-value refs)) threads))))
7523      gnus-newsgroup-dependencies)
7524     threads))
7525   
7526 (defun gnus-build-old-threads ()
7527   ;; Look at all the articles that refer back to old articles, and
7528   ;; fetch the headers for the articles that aren't there.  This will
7529   ;; build complete threads - if the roots haven't been expired by the
7530   ;; server, that is.
7531   (let (id heads)
7532     (mapatoms
7533      (lambda (refs)
7534        (when (not (car (symbol-value refs)))
7535          (setq heads (cdr (symbol-value refs)))
7536          (while heads
7537            (if (memq (mail-header-number (car (car heads)))
7538                      gnus-newsgroup-dormant)
7539                (setq heads (cdr heads))
7540              (setq id (symbol-name refs))
7541              (while (and (setq id (gnus-build-get-header id))
7542                          (not (car (gnus-gethash 
7543                                     id gnus-newsgroup-dependencies)))))
7544              (setq heads nil)))))
7545      gnus-newsgroup-dependencies)))
7546
7547 (defun gnus-build-get-header (id)
7548   ;; Look through the buffer of NOV lines and find the header to
7549   ;; ID.  Enter this line into the dependencies hash table, and return
7550   ;; the id of the parent article (if any).
7551   (let ((deps gnus-newsgroup-dependencies)
7552         found header)
7553     (prog1
7554         (save-excursion
7555           (set-buffer nntp-server-buffer)
7556           (goto-char (point-min))
7557           (while (and (not found) (search-forward id nil t))
7558             (beginning-of-line)
7559             (setq found (looking-at 
7560                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7561                                  (regexp-quote id))))
7562             (or found (beginning-of-line 2)))
7563           (when found
7564             (let (ref)
7565               (beginning-of-line)
7566               (and
7567                (setq header (gnus-nov-parse-line 
7568                              (read (current-buffer)) deps))
7569                (gnus-parent-id (mail-header-references header))))))
7570       (when header
7571         (let ((number (mail-header-number header)))
7572           (push number gnus-newsgroup-limit)
7573           (push header gnus-newsgroup-headers)
7574           (push number gnus-newsgroup-ancient))))))
7575
7576 (defun gnus-rebuild-thread (id)
7577   "Rebuild the thread containing ID."
7578   (let ((dep gnus-newsgroup-dependencies)
7579         (buffer-read-only nil)
7580         current headers refs thread art data)
7581     (if (not gnus-show-threads)
7582         (setq thread (list (car (gnus-gethash (downcase id) dep))))
7583       ;; Get the thread this article is part of.
7584       (setq thread (gnus-remove-thread id)))
7585     (setq current (save-excursion
7586                     (and (zerop (forward-line -1))
7587                          (gnus-summary-article-number))))
7588     ;; If this is a gathered thread, we have to go some re-gathering.
7589     (when (stringp (car thread))
7590       (let ((subject (car thread))
7591             roots thr)
7592         (setq thread (cdr thread))
7593         (while thread
7594           (unless (memq (setq thr (gnus-id-to-thread 
7595                                       (gnus-root-id
7596                                        (mail-header-id (car (car thread))))))
7597                         roots)
7598             (push thr roots))
7599           (setq thread (cdr thread)))
7600         ;; We now have all (unique) roots.
7601         (if (= (length roots) 1)
7602             ;; All the loose roots are now one solid root.
7603             (setq thread (car roots))
7604           (setq thread (cons subject (gnus-sort-threads roots))))))
7605     (let ((beg (point)) 
7606           threads)
7607       ;; We then insert this thread into the summary buffer.
7608       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7609         (gnus-summary-prepare-threads (list thread))
7610         (setq data (nreverse gnus-newsgroup-data))
7611         (setq threads gnus-newsgroup-threads))
7612       ;; We splice the new data into the data structure.
7613       (gnus-data-enter-list current data)
7614       (gnus-data-compute-positions)
7615       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7616
7617 (defun gnus-id-to-thread (id)
7618   "Return the (sub-)thread where ID appears."
7619   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7620
7621 (defun gnus-root-id (id)
7622   "Return the id of the root of the thread where ID appears."
7623   (let (last-id prev)
7624     (while (and id (setq prev (car (gnus-gethash 
7625                                     (downcase id)
7626                                     gnus-newsgroup-dependencies))))
7627       (setq last-id id
7628             id (gnus-parent-id (mail-header-references prev))))
7629     last-id))
7630
7631 (defun gnus-remove-thread (id)
7632   "Remove the thread that has ID in it."
7633   (let ((dep gnus-newsgroup-dependencies)
7634         headers thread prev last-id)
7635     ;; First go up in this thread until we find the root.
7636     (setq last-id (gnus-root-id id))
7637     (setq headers (list (car (gnus-id-to-thread last-id))
7638                         (car (car (cdr (gnus-id-to-thread last-id))))))
7639     ;; We have now found the real root of this thread.  It might have
7640     ;; been gathered into some loose thread, so we have to search
7641     ;; through the threads to find the thread we wanted.
7642     (let ((threads gnus-newsgroup-threads)
7643           sub)
7644       (while threads
7645         (setq sub (car threads))
7646         (if (stringp (car sub))
7647             ;; This is a gathered threads, so we look at the roots
7648             ;; below it to find whether this article in in this
7649             ;; gathered root.
7650             (progn
7651               (setq sub (cdr sub))
7652               (while sub
7653                 (when (member (car (car sub)) headers)
7654                   (setq thread (car threads)
7655                         threads nil
7656                         sub nil))
7657                 (setq sub (cdr sub))))
7658           ;; It's an ordinary thread, so we check it.
7659           (when (eq (car sub) (car headers))
7660             (setq thread sub
7661                   threads nil)))
7662         (setq threads (cdr threads)))
7663       ;; If this article is in no thread, then it's a root. 
7664       (if thread 
7665           (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads))
7666         (setq thread (gnus-gethash (downcase last-id) dep)))
7667       (when thread
7668         (prog1 
7669             thread ; We return this thread.
7670           (if (stringp (car thread))
7671               (progn
7672                 ;; If we use dummy roots, then we have to remove the
7673                 ;; dummy root as well.
7674                 (when (eq gnus-summary-make-false-root 'dummy)
7675                   ;; Uhm.
7676                   )
7677                 (setq thread (cdr thread))
7678                 (while thread
7679                   (gnus-remove-thread-1 (car thread))
7680                   (setq thread (cdr thread))))
7681             (gnus-remove-thread-1 thread)))))))
7682
7683 (defun gnus-remove-thread-1 (thread)
7684   "Remove the thread THREAD recursively."
7685   (let ((number (mail-header-number (car thread)))
7686         pos)
7687     (when (setq pos (text-property-any 
7688                      (point-min) (point-max) 'gnus-number number))
7689       (goto-char pos)
7690       (gnus-delete-line)
7691       (gnus-data-remove number))
7692     (setq thread (cdr thread))
7693     (while thread
7694       (gnus-remove-thread-1 (car thread))
7695       (setq thread (cdr thread)))))
7696
7697 (defun gnus-sort-threads (threads)
7698   "Sort THREADS as specified in `gnus-thread-sort-functions'."
7699   (let ((funs gnus-thread-sort-functions))
7700     (when funs
7701       (while funs
7702         (gnus-message 7 "Sorting with %S..." (car funs))
7703         (setq threads (sort threads (pop funs))))
7704       (gnus-message 7 "Sorting...done")))
7705   threads)
7706
7707 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
7708 (defmacro gnus-thread-header (thread)
7709   ;; Return header of first article in THREAD.
7710   ;; Note that THREAD must never, ever be anything else than a variable -
7711   ;; using some other form will lead to serious barfage.
7712   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
7713   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
7714   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
7715         (vector thread) 2))
7716
7717 (defsubst gnus-article-sort-by-number (h1 h2)
7718   "Sort articles by article number."
7719   (< (mail-header-number h1)
7720      (mail-header-number h2)))
7721
7722 (defun gnus-thread-sort-by-number (h1 h2)
7723   "Sort threads by root article number."
7724   (gnus-article-sort-by-number 
7725    (gnus-thread-header h1) (gnus-thread-header h2)))
7726
7727 (defsubst gnus-article-sort-by-author (h1 h2)
7728   "Sort articles by root author."
7729   (string-lessp
7730    (let ((extract (funcall 
7731                    gnus-extract-address-components
7732                    (mail-header-from h1))))
7733      (or (car extract) (cdr extract)))
7734    (let ((extract (funcall
7735                    gnus-extract-address-components 
7736                    (mail-header-from h2))))
7737      (or (car extract) (cdr extract)))))
7738
7739 (defun gnus-thread-sort-by-author (h1 h2)
7740   "Sort threads by root author."
7741   (gnus-article-sort-by-author
7742    (gnus-thread-header h1)  (gnus-thread-header h2)))
7743
7744 (defsubst gnus-article-sort-by-subject (h1 h2)
7745   "Sort articles by root subject."
7746   (string-lessp
7747    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
7748    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
7749
7750 (defun gnus-thread-sort-by-subject (h1 h2)
7751   "Sort threads by root subject."
7752   (gnus-article-sort-by-subject 
7753    (gnus-thread-header h1) (gnus-thread-header h2)))
7754
7755 (defsubst gnus-article-sort-by-date (h1 h2)
7756   "Sort articles by root article date."
7757   (string-lessp
7758    (gnus-sortable-date (mail-header-date h1))
7759    (gnus-sortable-date (mail-header-date h2))))
7760
7761 (defun gnus-thread-sort-by-date (h1 h2)
7762   "Sort threads by root article date."
7763   (gnus-article-sort-by-date 
7764    (gnus-thread-header h1) (gnus-thread-header h2)))
7765
7766 (defsubst gnus-article-sort-by-score (h1 h2)
7767   "Sort articles by root article score.
7768 Unscored articles will be counted as having a score of zero."
7769   (> (or (cdr (assq (mail-header-number h1)
7770                     gnus-newsgroup-scored))
7771          gnus-summary-default-score 0)
7772      (or (cdr (assq (mail-header-number h2)
7773                     gnus-newsgroup-scored))
7774          gnus-summary-default-score 0)))
7775
7776 (defun gnus-thread-sort-by-score (h1 h2)
7777   "Sort threads by root article score."
7778   (gnus-article-sort-by-score 
7779    (gnus-thread-header h1) (gnus-thread-header h2)))
7780
7781 (defun gnus-thread-sort-by-total-score (h1 h2)
7782   "Sort threads by the sum of all scores in the thread.
7783 Unscored articles will be counted as having a score of zero."
7784   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
7785
7786 (defun gnus-thread-total-score (thread)
7787   ;;  This function find the total score of THREAD.
7788   (if (consp thread)
7789       (if (stringp (car thread))
7790           (apply gnus-thread-score-function 0
7791                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
7792         (gnus-thread-total-score-1 thread))
7793     (gnus-thread-total-score-1 (list thread))))
7794
7795 (defun gnus-thread-total-score-1 (root)
7796   ;; This function find the total score of the thread below ROOT.
7797   (setq root (car root))
7798   (apply gnus-thread-score-function
7799          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
7800              gnus-summary-default-score 0)
7801          (mapcar 'gnus-thread-total-score
7802                  (cdr (gnus-gethash (downcase (mail-header-id root))
7803                                     gnus-newsgroup-dependencies)))))
7804
7805 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
7806 (defvar gnus-tmp-prev-subject nil)
7807 (defvar gnus-tmp-false-parent nil)
7808 (defvar gnus-tmp-root-expunged nil)
7809 (defvar gnus-tmp-dummy-line nil)
7810
7811 (defun gnus-summary-prepare-threads (threads)
7812   "Prepare summary buffer from THREADS and indentation LEVEL.  
7813 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
7814 or a straight list of headers."
7815   (gnus-message 7 "Generating summary...")
7816
7817   (setq gnus-newsgroup-threads threads)
7818   (beginning-of-line)
7819
7820   (let ((gnus-tmp-level 0)
7821         (default-score (or gnus-summary-default-score 0))
7822         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
7823         thread number subject stack state gnus-tmp-gathered beg-match
7824         new-roots gnus-tmp-new-adopts thread-end
7825         gnus-tmp-header gnus-tmp-unread
7826         gnus-tmp-replied gnus-tmp-subject-or-nil
7827         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
7828         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
7829         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
7830
7831     (setq gnus-tmp-prev-subject nil)
7832
7833     (if (vectorp (car threads))
7834         ;; If this is a straight (sic) list of headers, then a
7835         ;; threaded summary display isn't required, so we just create
7836         ;; an unthreaded one.
7837         (gnus-summary-prepare-unthreaded threads)
7838
7839       ;; Do the threaded display.
7840
7841       (while (or threads stack gnus-tmp-new-adopts new-roots)
7842
7843         (if (and (= gnus-tmp-level 0)
7844                  (not (setq gnus-tmp-dummy-line nil))
7845                  (or (not stack)
7846                      (= (car (car stack)) 0))
7847                  (not gnus-tmp-false-parent)
7848                  (or gnus-tmp-new-adopts new-roots))
7849             (if gnus-tmp-new-adopts
7850                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
7851                       thread (list (car gnus-tmp-new-adopts))
7852                       gnus-tmp-header (car (car thread))
7853                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
7854               (if new-roots
7855                   (setq thread (list (car new-roots))
7856                         gnus-tmp-header (car (car thread))
7857                         new-roots (cdr new-roots))))
7858
7859           (if threads
7860               ;; If there are some threads, we do them before the
7861               ;; threads on the stack.
7862               (setq thread threads
7863                     gnus-tmp-header (car (car thread)))
7864             ;; There were no current threads, so we pop something off
7865             ;; the stack. 
7866             (setq state (car stack)
7867                   gnus-tmp-level (car state)
7868                   thread (cdr state)
7869                   stack (cdr stack)
7870                   gnus-tmp-header (car (car thread)))))
7871
7872         (setq gnus-tmp-false-parent nil)
7873         (setq gnus-tmp-root-expunged nil)
7874         (setq thread-end nil)
7875
7876         (if (stringp gnus-tmp-header)
7877             ;; The header is a dummy root.
7878             (cond 
7879              ((eq gnus-summary-make-false-root 'adopt)
7880               ;; We let the first article adopt the rest.
7881               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
7882                                                (cdr (cdr (car thread)))))
7883               (setq gnus-tmp-gathered 
7884                     (nconc (mapcar
7885                             (lambda (h) (mail-header-number (car h)))
7886                             (cdr (cdr (car thread))))
7887                            gnus-tmp-gathered))
7888               (setq thread (cons (list (car (car thread))
7889                                        (car (cdr (car thread))))
7890                                  (cdr thread)))
7891               (setq gnus-tmp-level -1
7892                     gnus-tmp-false-parent t))
7893              ((eq gnus-summary-make-false-root 'empty)
7894               ;; We print adopted articles with empty subject fields.
7895               (setq gnus-tmp-gathered 
7896                     (nconc (mapcar
7897                             (lambda (h) (mail-header-number (car h)))
7898                             (cdr (cdr (car thread))))
7899                            gnus-tmp-gathered))
7900               (setq gnus-tmp-level -1))
7901              ((eq gnus-summary-make-false-root 'dummy)
7902               ;; We remember that we probably want to output a dummy
7903               ;; root.   
7904               (setq gnus-tmp-dummy-line gnus-tmp-header)
7905               (setq gnus-tmp-prev-subject gnus-tmp-header))
7906              (t
7907               ;; We do not make a root for the gathered
7908               ;; sub-threads at all.  
7909               (setq gnus-tmp-level -1)))
7910       
7911           (setq number (mail-header-number gnus-tmp-header)
7912                 subject (mail-header-subject gnus-tmp-header))
7913
7914           (cond 
7915            ;; If the thread has changed subject, we might want to make 
7916            ;; this subthread into a root.
7917            ((and (null gnus-thread-ignore-subject)
7918                  (not (zerop gnus-tmp-level))
7919                  gnus-tmp-prev-subject
7920                  (not (inline
7921                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
7922             (setq new-roots (nconc new-roots (list (car thread)))
7923                   thread-end t
7924                   gnus-tmp-header nil))
7925            ;; If the article lies outside the current limit,
7926            ;; then we do not display it.
7927            ((not (memq number gnus-newsgroup-limit))
7928             (setq gnus-tmp-gathered 
7929                   (nconc (mapcar
7930                           (lambda (h) (mail-header-number (car h)))
7931                           (cdr (car thread)))
7932                          gnus-tmp-gathered))
7933             (setq gnus-tmp-new-adopts (if (cdr (car thread))
7934                                           (append gnus-tmp-new-adopts 
7935                                                   (cdr (car thread)))
7936                                         gnus-tmp-new-adopts)
7937                   thread-end t
7938                   gnus-tmp-header nil)
7939             (when (zerop gnus-tmp-level)
7940               (setq gnus-tmp-root-expunged t)))
7941            ;; Perhaps this article is to be marked as read?
7942            ((and gnus-summary-mark-below
7943                  (< (or (cdr (assq number gnus-newsgroup-scored))
7944                         default-score)
7945                     gnus-summary-mark-below))
7946             (setq gnus-newsgroup-unreads 
7947                   (delq number gnus-newsgroup-unreads))
7948             (if gnus-newsgroup-auto-expire
7949                 (push number gnus-newsgroup-expirable)
7950               (push (cons number gnus-low-score-mark)
7951                     gnus-newsgroup-reads))))
7952           
7953           (when gnus-tmp-header
7954             ;; We may have an old dummy line to output before this
7955             ;; article.  
7956             (when gnus-tmp-dummy-line
7957               (gnus-summary-insert-dummy-line 
7958                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
7959               (setq gnus-tmp-dummy-line nil))
7960
7961             ;; Compute the mark.
7962             (setq 
7963              gnus-tmp-unread
7964              (cond 
7965               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
7966               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
7967               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
7968               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
7969               (t (or (cdr (assq number gnus-newsgroup-reads))
7970                      gnus-ancient-mark))))
7971
7972             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
7973                                   gnus-tmp-header gnus-tmp-level)
7974                   gnus-newsgroup-data)
7975
7976             ;; Actually insert the line.
7977             (setq 
7978              gnus-tmp-subject-or-nil
7979              (cond
7980               ((and gnus-thread-ignore-subject
7981                     gnus-tmp-prev-subject
7982                     (not (inline (gnus-subject-equal 
7983                                   gnus-tmp-prev-subject subject))))
7984                subject)
7985               ((zerop gnus-tmp-level)
7986                (if (and (eq gnus-summary-make-false-root 'empty)
7987                         (memq number gnus-tmp-gathered)
7988                         gnus-tmp-prev-subject
7989                         (inline (gnus-subject-equal
7990                                  gnus-tmp-prev-subject subject)))
7991                    gnus-summary-same-subject
7992                  subject))
7993               (t gnus-summary-same-subject)))
7994             (if (and (eq gnus-summary-make-false-root 'adopt)
7995                      (= gnus-tmp-level 1)
7996                      (memq number gnus-tmp-gathered))
7997                 (setq gnus-tmp-opening-bracket ?\<
7998                       gnus-tmp-closing-bracket ?\>)
7999               (setq gnus-tmp-opening-bracket ?\[
8000                     gnus-tmp-closing-bracket ?\]))
8001             (setq 
8002              gnus-tmp-indentation 
8003              (aref gnus-thread-indent-array gnus-tmp-level)
8004              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8005              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8006                                 gnus-summary-default-score 0)
8007              gnus-tmp-score-char
8008              (if (or (null gnus-summary-default-score)
8009                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8010                          gnus-summary-zcore-fuzz)) ? 
8011                (if (< gnus-tmp-score gnus-summary-default-score)
8012                    gnus-score-below-mark gnus-score-over-mark))
8013              gnus-tmp-replied
8014              (cond ((memq number gnus-newsgroup-processable)
8015                     gnus-process-mark)
8016                    ((memq number gnus-newsgroup-cached)
8017                     gnus-cached-mark)
8018                    ((memq number gnus-newsgroup-replied)
8019                     gnus-replied-mark)
8020                    (t gnus-unread-mark))
8021              gnus-tmp-from (mail-header-from gnus-tmp-header)
8022              gnus-tmp-name 
8023              (cond 
8024               ((string-match "(.+)" gnus-tmp-from)
8025                (substring gnus-tmp-from 
8026                           (1+ (match-beginning 0)) (1- (match-end 0))))
8027               ((string-match "<[^>]+> *$" gnus-tmp-from)
8028                (setq beg-match (match-beginning 0))
8029                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8030                         (substring gnus-tmp-from (1+ (match-beginning 0))
8031                                    (1- (match-end 0))))
8032                    (substring gnus-tmp-from 0 beg-match)))
8033               (t gnus-tmp-from)))
8034             (when (string= gnus-tmp-name "")
8035               (setq gnus-tmp-name gnus-tmp-from))
8036             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8037             (put-text-property
8038              (point)
8039              (progn (eval gnus-summary-line-format-spec) (point))
8040              'gnus-number number)
8041             (when gnus-visual-p
8042               (forward-line -1)
8043               (run-hooks 'gnus-summary-update-hook)
8044               (forward-line 1))
8045
8046             (setq gnus-tmp-prev-subject subject)))
8047
8048         (when (nth 1 thread) 
8049           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8050         (incf gnus-tmp-level)
8051         (setq threads (if thread-end nil (cdr (car thread))))
8052         (unless threads
8053           (setq gnus-tmp-level 0)))))
8054   (gnus-message 7 "Generating summary...done"))
8055
8056 (defun gnus-summary-prepare-unthreaded (headers)
8057   "Generate an unthreaded summary buffer based on HEADERS."
8058   (let (header number mark)
8059
8060     (while headers
8061       (setq header (car headers)
8062             headers (cdr headers)
8063             number (mail-header-number header))
8064
8065       ;; We may have to root out some bad articles...
8066       (when (memq number gnus-newsgroup-limit)
8067         (when (and gnus-summary-mark-below
8068                    (< (or (cdr (assq number gnus-newsgroup-scored))
8069                           gnus-summary-default-score 0)
8070                       gnus-summary-mark-below))
8071           (setq gnus-newsgroup-unreads 
8072                 (delq number gnus-newsgroup-unreads))
8073           (if gnus-newsgroup-auto-expire
8074               (push number gnus-newsgroup-expirable)
8075             (push (cons number gnus-low-score-mark)
8076                   gnus-newsgroup-reads)))
8077           
8078         (setq mark
8079               (cond 
8080                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8081                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8082                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8083                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8084                (t (or (cdr (assq number gnus-newsgroup-reads))
8085                       gnus-ancient-mark))))
8086         (setq gnus-newsgroup-data 
8087               (cons (gnus-data-make number mark (1+ (point)) header 0)
8088                     gnus-newsgroup-data))
8089         (gnus-summary-insert-line
8090          header 0 nil mark (memq number gnus-newsgroup-replied)
8091          (memq number gnus-newsgroup-expirable)
8092          (mail-header-subject header) nil
8093          (cdr (assq number gnus-newsgroup-scored))
8094          (memq number gnus-newsgroup-processable))))))
8095
8096 (defun gnus-select-newsgroup (group &optional read-all)
8097   "Select newsgroup GROUP.
8098 If READ-ALL is non-nil, all articles in the group are selected."
8099   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8100          (info (nth 2 entry))
8101          articles fetched-articles)
8102
8103     (or (gnus-check-server
8104          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8105         (error "Couldn't open server"))
8106     
8107     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8108         (gnus-activate-group group) ; Or we can activate it...
8109         (progn ; Or we bug out.
8110           (kill-buffer (current-buffer))
8111           (error "Couldn't request group %s: %s" 
8112                  group (gnus-status-message group))))
8113
8114     (setq gnus-newsgroup-name group)
8115     (setq gnus-newsgroup-unselected nil)
8116     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8117
8118     (and gnus-asynchronous
8119          (gnus-check-backend-function 
8120           'request-asynchronous gnus-newsgroup-name)
8121          (setq gnus-newsgroup-async
8122                (gnus-request-asynchronous gnus-newsgroup-name)))
8123
8124     ;; Adjust and set lists of article marks.
8125     (when info
8126       (gnus-adjust-marked-articles info))
8127
8128     (setq gnus-newsgroup-unreads 
8129           (gnus-set-difference
8130            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8131            gnus-newsgroup-dormant))
8132
8133     (setq gnus-newsgroup-processable nil)
8134     
8135     (setq articles (gnus-articles-to-read group read-all))
8136     
8137     (cond 
8138      ((null articles) 
8139       (gnus-message 3 "Couldn't select newsgroup")
8140       'quit)
8141      ((eq articles 0) nil)
8142      (t
8143       ;; Init the dependencies hash table.
8144       (setq gnus-newsgroup-dependencies 
8145             (gnus-make-hashtable (length articles)))
8146       ;; Retrieve the headers and read them in.
8147       (gnus-message 5 "Fetching headers...")
8148       (setq gnus-newsgroup-headers 
8149             (if (eq 'nov 
8150                     (setq gnus-headers-retrieved-by
8151                           (gnus-retrieve-headers 
8152                            articles gnus-newsgroup-name
8153                            ;; We might want to fetch old headers, but
8154                            ;; not if there is only 1 article.
8155                            (and gnus-fetch-old-headers
8156                                 (or (and 
8157                                      (not (eq gnus-fetch-old-headers 'some))
8158                                      (not (numberp gnus-fetch-old-headers)))
8159                                     (> (length articles) 1))))))
8160                 (gnus-get-newsgroup-headers-xover articles)
8161               (gnus-get-newsgroup-headers)))
8162       (gnus-message 5 "Fetching headers...done")      
8163       ;; Set the initial limit.
8164       (setq gnus-newsgroup-limit (copy-sequence articles))
8165       ;; Remove canceled articles from the list of unread articles.
8166       (setq gnus-newsgroup-unreads
8167             (gnus-set-sorted-intersection 
8168              gnus-newsgroup-unreads
8169              (setq fetched-articles
8170                    (mapcar (lambda (headers) (mail-header-number headers))
8171                            gnus-newsgroup-headers))))
8172       ;; Removed marked articles that do not exist.
8173       (gnus-update-missing-marks 
8174        (gnus-sorted-complement fetched-articles articles))
8175       ;; We might want to build some more threads first.
8176       (and gnus-fetch-old-headers
8177            (eq gnus-headers-retrieved-by 'nov)
8178            (gnus-build-old-threads))
8179       ;; Check whether auto-expire is to be done in this group.
8180       (setq gnus-newsgroup-auto-expire
8181             (gnus-group-auto-expirable-p group))
8182       ;; First and last article in this newsgroup.
8183       (and gnus-newsgroup-headers
8184            (setq gnus-newsgroup-begin 
8185                  (mail-header-number (car gnus-newsgroup-headers)))
8186            (setq gnus-newsgroup-end
8187                  (mail-header-number
8188                   (gnus-last-element gnus-newsgroup-headers))))
8189       (setq gnus-reffed-article-number -1)
8190       ;; GROUP is successfully selected.
8191       (or gnus-newsgroup-headers t)))))
8192
8193 (defun gnus-articles-to-read (group read-all)
8194   ;; Find out what articles the user wants to read.
8195   (let* ((articles
8196           ;; Select all articles if `read-all' is non-nil, or if there
8197           ;; are no unread articles.
8198           (if (or read-all
8199                   (and (zerop (length gnus-newsgroup-marked))
8200                        (zerop (length gnus-newsgroup-unreads))))
8201               (gnus-uncompress-range (gnus-active group))
8202             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked 
8203                           (copy-sequence gnus-newsgroup-unreads))
8204                   '<)))
8205          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8206          (scored (length scored-list))
8207          (number (length articles))
8208          (marked (+ (length gnus-newsgroup-marked)
8209                     (length gnus-newsgroup-dormant)))
8210          (select
8211           (cond 
8212            ((numberp read-all)
8213             read-all)
8214            (t
8215             (condition-case ()
8216                 (cond 
8217                  ((and (or (<= scored marked) (= scored number))
8218                        (numberp gnus-large-newsgroup)
8219                        (> number gnus-large-newsgroup))
8220                   (let ((input
8221                          (read-string
8222                           (format
8223                            "How many articles from %s (default %d): "
8224                            gnus-newsgroup-name number))))
8225                     (if (string-match "^[ \t]*$" input) number input)))
8226                  ((and (> scored marked) (< scored number))
8227                   (let ((input
8228                          (read-string
8229                           (format "%s %s (%d scored, %d total): "
8230                                   "How many articles from"
8231                                   group scored number))))
8232                     (if (string-match "^[ \t]*$" input)
8233                         number input)))
8234                  (t number))
8235               (quit nil))))))
8236     (setq select (if (stringp select) (string-to-number select) select))
8237     (if (or (null select) (zerop select))
8238         select
8239       (if (and (not (zerop scored)) (<= (abs select) scored))
8240           (progn
8241             (setq articles (sort scored-list '<))
8242             (setq number (length articles)))
8243         (setq articles (copy-sequence articles)))
8244
8245       (if (< (abs select) number)
8246           (if (< select 0) 
8247               ;; Select the N oldest articles.
8248               (setcdr (nthcdr (1- (abs select)) articles) nil)
8249             ;; Select the N most recent articles.
8250             (setq articles (nthcdr (- number select) articles))))
8251       (setq gnus-newsgroup-unselected
8252             (gnus-sorted-intersection
8253              gnus-newsgroup-unreads
8254              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8255       articles)))
8256
8257 (defun gnus-killed-articles (killed articles)
8258   (let (out)
8259     (while articles
8260       (if (inline (gnus-member-of-range (car articles) killed))
8261           (setq out (cons (car articles) out)))
8262       (setq articles (cdr articles)))
8263     out))
8264
8265 (defun gnus-adjust-marked-articles (info)
8266   "Set all article lists and remove all marks that are no longer legal."
8267   (let* ((marked-lists (gnus-info-marks info))
8268          (active (gnus-active (gnus-info-group info)))
8269          (min (car active))
8270          (max (cdr active))
8271          (types gnus-article-mark-lists)
8272          (uncompressed '(score bookmark))
8273          marks var articles article mark)
8274
8275     (while marked-lists
8276       (setq marks (pop marked-lists))
8277       (set (setq var (intern (format "gnus-newsgroup-%s" 
8278                                      (car (rassq (setq mark (car marks)) 
8279                                                  types)))))
8280            (if (memq (car marks) uncompressed) (cdr marks)
8281              (gnus-uncompress-range (cdr marks))))
8282
8283       (setq articles (symbol-value var))
8284
8285       ;; All articles have to be subsets of the active articles.  
8286       (cond 
8287        ;; Adjust "simple" lists.
8288        ((memq mark '(tick dormant expirable reply killed save))
8289         (while articles
8290           (when (or (< (setq article (pop articles)) min) (> article max))
8291             (set var (delq article (symbol-value var))))))
8292        ;; Adjust assocs.
8293        ((memq mark '(score bookmark))
8294         (while articles 
8295           (when (or (< (car (setq article (pop articles))) min) 
8296                     (> (car article) max))
8297             (set var (delq article (symbol-value var))))))))))
8298
8299 (defun gnus-update-missing-marks (missing)
8300   "Go through the list of MISSING articles and remove them mark lists."
8301   (when missing
8302     (let ((types gnus-article-mark-lists)
8303           var m)
8304       ;; Go through all types.
8305       (while types
8306         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8307         (when (symbol-value var)
8308           ;; This list has articles.  So we delete all missing articles 
8309           ;; from it.
8310           (setq m missing)
8311           (while m
8312             (set var (delq (pop m) (symbol-value var)))))))))
8313
8314 (defun gnus-update-marks ()
8315   "Enter the various lists of marked articles into the newsgroup info list."
8316   (let ((types gnus-article-mark-lists)
8317         (info (gnus-get-info gnus-newsgroup-name))
8318         (uncompressed '(score bookmark killed))
8319         var type list newmarked symbol)
8320     ;; Add all marks lists that are non-nil to the list of marks lists. 
8321     (while types
8322       (setq type (pop types))
8323       (when (setq list (symbol-value 
8324                         (setq symbol
8325                               (intern (format "gnus-newsgroup-%s" 
8326                                               (car type))))))
8327         (push (cons (cdr type) 
8328                     (if (memq (cdr type) uncompressed) list
8329                       (gnus-compress-sequence (set symbol (sort list '<)) t)))
8330               newmarked)))
8331
8332     ;; Enter these new marks into the info of the group.
8333     (if (nthcdr 3 info)
8334         (setcar (nthcdr 3 info) newmarked)
8335       ;; Add the marks lists to the end of the info.
8336       (when newmarked
8337         (setcdr (nthcdr 2 info) (list newmarked))))
8338
8339     ;; Cut off the end of the info if there's nothing else there. 
8340     (let ((i 5))
8341       (while (and (> i 2)
8342                   (not (nth i info)))
8343         (when (nthcdr (decf i) info)
8344           (setcdr (nthcdr i info) nil))))))
8345
8346 (defun gnus-add-marked-articles (group type articles &optional info force)
8347   ;; Add ARTICLES of TYPE to the info of GROUP.
8348   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8349   ;; add, but replace marked articles of TYPE with ARTICLES.
8350   (let ((info (or info (gnus-get-info group)))
8351         (uncompressed '(score bookmark killed))
8352         marked m)
8353     (or (not info)
8354         (and (not (setq marked (nthcdr 3 info)))
8355              (setcdr (nthcdr 2 info)
8356                      (list (list (cons type (gnus-compress-sequence
8357                                              articles t))))))
8358         (and (not (setq m (assq type (car marked))))
8359              (setcar marked 
8360                      (cons (cons type (gnus-compress-sequence articles t) )
8361                            (car marked))))
8362         (if force
8363             (setcdr m (gnus-compress-sequence articles t))
8364           (setcdr m (gnus-compress-sequence
8365                      (sort (nconc (gnus-uncompress-range m) 
8366                                   (copy-sequence articles)) '<) t))))))
8367          
8368 (defun gnus-set-mode-line (where)
8369   "This function sets the mode line of the article or summary buffers.
8370 If WHERE is `summary', the summary mode line format will be used."
8371   ;; Is this mode line one we keep updated?
8372   (when (memq where gnus-updated-mode-lines)
8373     (let (mode-string)
8374       (save-excursion
8375         ;; We evaluate this in the summary buffer since these
8376         ;; variables are buffer-local to that buffer.
8377         (set-buffer gnus-summary-buffer)
8378         ;; We bind all these variables that are used in the `eval' form
8379         ;; below. 
8380         (let* ((mformat (if (eq where 'article) 
8381                             gnus-article-mode-line-format-spec
8382                           gnus-summary-mode-line-format-spec))
8383                (gnus-tmp-group-name gnus-newsgroup-name)
8384                (gnus-tmp-article-number (or gnus-current-article 0))
8385                (gnus-tmp-unread gnus-newsgroup-unreads)
8386                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8387                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8388                (gnus-tmp-unread-and-unselected
8389                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8390                             (zerop gnus-tmp-unselected)) "")
8391                       ((zerop gnus-tmp-unselected) 
8392                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8393                       (t (format "{%d(+%d) more}"
8394                                  gnus-tmp-unread-and-unticked
8395                                  gnus-tmp-unselected))))
8396                (gnus-tmp-subject
8397                 (if (and gnus-current-headers
8398                          (vectorp gnus-current-headers))
8399                     (mail-header-subject gnus-current-headers) ""))
8400                max-len 
8401                header);; passed as argument to any user-format-funcs
8402           (setq mode-string (eval mformat))
8403           (setq max-len (max 4 (if gnus-mode-non-string-length
8404                                    (- (frame-width) 
8405                                       gnus-mode-non-string-length)
8406                                  (length mode-string))))
8407           ;; We might have to chop a bit of the string off...
8408           (when (> (length mode-string) max-len)
8409             (setq mode-string 
8410                   (concat (gnus-truncate-string mode-string (- max-len 3))
8411                           "...")))
8412           ;; Pad the mode string a bit.
8413           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8414       ;; Update the mode line.
8415       (setq mode-line-buffer-identification (list mode-string))
8416       (set-buffer-modified-p t))))
8417
8418 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8419   "Go through the HEADERS list and add all Xrefs to a hash table.
8420 The resulting hash table is returned, or nil if no Xrefs were found."
8421   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
8422          (virtual (memq 'virtual 
8423                         (assoc (symbol-name (car (gnus-find-method-for-group 
8424                                                   from-newsgroup)))
8425                                gnus-valid-select-methods)))     
8426          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8427          (xref-hashtb (make-vector 63 0))
8428          start group entry number xrefs header)
8429     (while headers
8430       (setq header (pop headers))
8431       (when (and (setq xrefs (mail-header-xref header))
8432                  (not (memq (setq number (mail-header-number header))
8433                             unreads)))
8434         (setq start 0)
8435         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8436           (setq start (match-end 0))
8437           (setq group (concat prefix (substring xrefs (match-beginning 1) 
8438                                                 (match-end 1))))
8439           (setq number 
8440                 (string-to-int (substring xrefs (match-beginning 2) 
8441                                           (match-end 2))))
8442           (if (setq entry (gnus-gethash group xref-hashtb))
8443               (setcdr entry (cons number (cdr entry)))
8444             (gnus-sethash group (cons number nil) xref-hashtb)))))
8445     (and start xref-hashtb)))
8446
8447 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8448   "Look through all the headers and mark the Xrefs as read."
8449   (let ((virtual (memq 'virtual 
8450                        (assoc (symbol-name (car (gnus-find-method-for-group 
8451                                                  from-newsgroup)))
8452                               gnus-valid-select-methods)))
8453         name entry info xref-hashtb idlist method
8454         nth4)
8455     (save-excursion
8456       (set-buffer gnus-group-buffer)
8457       (when (setq xref-hashtb 
8458                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8459         (mapatoms 
8460          (lambda (group)
8461            (unless (string= from-newsgroup (setq name (symbol-name group)))
8462              (setq idlist (symbol-value group))
8463              ;; Dead groups are not updated.
8464              (and (prog1 
8465                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8466                             info (nth 2 entry))
8467                     (if (stringp (setq nth4 (gnus-info-method info)))
8468                         (setq nth4 (gnus-server-to-method nth4))))
8469                   ;; Only do the xrefs if the group has the same
8470                   ;; select method as the group we have just read.
8471                   (or (gnus-methods-equal-p 
8472                        nth4 (gnus-find-method-for-group from-newsgroup))
8473                       virtual
8474                       (equal nth4 (setq method (gnus-find-method-for-group 
8475                                                 from-newsgroup)))
8476                       (and (equal (car nth4) (car method))
8477                            (equal (nth 1 nth4) (nth 1 method))))
8478                   gnus-use-cross-reference
8479                   (or (not (eq gnus-use-cross-reference t))
8480                       virtual
8481                       ;; Only do cross-references on subscribed
8482                       ;; groups, if that is what is wanted.  
8483                       (<= (gnus-info-level info) gnus-level-subscribed))
8484                   (gnus-group-make-articles-read name idlist))))
8485          xref-hashtb)))))
8486
8487 (defun gnus-group-make-articles-read (group articles)
8488   (let* ((num 0)
8489          (entry (gnus-gethash group gnus-newsrc-hashtb))
8490          (info (nth 2 entry))
8491          (active (gnus-active group))
8492          range)
8493     ;; First peel off all illegal article numbers.
8494     (if active
8495         (let ((ids articles)
8496               id first)
8497           (while ids
8498             (setq id (car ids))
8499             (if (and first (> id (cdr active)))
8500                 (progn
8501                   ;; We'll end up in this situation in one particular
8502                   ;; obscure situation.  If you re-scan a group and get
8503                   ;; a new article that is cross-posted to a different
8504                   ;; group that has not been re-scanned, you might get
8505                   ;; crossposted article that has a higher number than
8506                   ;; Gnus believes possible.  So we re-activate this
8507                   ;; group as well.  This might mean doing the
8508                   ;; crossposting thingie will *increase* the number
8509                   ;; of articles in some groups.  Tsk, tsk.
8510                   (setq active (or (gnus-activate-group group) active))))
8511             (if (or (> id (cdr active))
8512                     (< id (car active)))
8513                 (setq articles (delq id articles)))
8514             (setq ids (cdr ids)))))
8515     ;; If the read list is nil, we init it.
8516     (and active
8517          (null (gnus-info-read info))
8518          (> (car active) 1)
8519          (gnus-info-set-read info (cons 1 (1- (car active)))))
8520     ;; Then we add the read articles to the range.
8521     (gnus-info-set-read
8522      info
8523      (setq range
8524            (gnus-add-to-range 
8525             (gnus-info-read info) (setq articles (sort articles '<)))))
8526     ;; Then we have to re-compute how many unread
8527     ;; articles there are in this group.
8528     (if active
8529         (progn
8530           (cond 
8531            ((not range)
8532             (setq num (- (1+ (cdr active)) (car active))))
8533            ((not (listp (cdr range)))
8534             (setq num (- (cdr active) (- (1+ (cdr range)) 
8535                                          (car range)))))
8536            (t
8537             (while range
8538               (if (numberp (car range))
8539                   (setq num (1+ num))
8540                 (setq num (+ num (- (1+ (cdr (car range)))
8541                                     (car (car range))))))
8542               (setq range (cdr range)))
8543             (setq num (- (cdr active) num))))
8544           ;; Update the number of unread articles.
8545           (setcar entry num)
8546           ;; Update the group buffer.
8547           (gnus-group-update-group group t)))))
8548
8549 (defun gnus-methods-equal-p (m1 m2)
8550   (let ((m1 (or m1 gnus-select-method))
8551         (m2 (or m2 gnus-select-method)))
8552     (or (equal m1 m2)
8553         (and (eq (car m1) (car m2))
8554              (or (not (memq 'address (assoc (symbol-name (car m1))
8555                                             gnus-valid-select-methods)))
8556                  (equal (nth 1 m1) (nth 1 m2)))))))
8557
8558 (defsubst gnus-header-value ()
8559   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8560
8561 (defvar gnus-newsgroup-none-id 0)
8562
8563 (defun gnus-get-newsgroup-headers (&optional dependencies)
8564   (let ((cur nntp-server-buffer)
8565         (dependencies 
8566          (or dependencies
8567              (save-excursion (set-buffer gnus-summary-buffer)
8568                              gnus-newsgroup-dependencies)))
8569         headers id id-dep ref-dep end ref)
8570     (save-excursion
8571       (set-buffer nntp-server-buffer)
8572       (let ((case-fold-search t)
8573             in-reply-to header number p lines)
8574         (goto-char (point-min))
8575         ;; Search to the beginning of the next header.  Error messages
8576         ;; do not begin with 2 or 3.
8577         (while (re-search-forward "^[23][0-9]+ " nil t)
8578           (setq id nil
8579                 ref nil)
8580           ;; This implementation of this function, with nine
8581           ;; search-forwards instead of the one re-search-forward and
8582           ;; a case (which basically was the old function) is actually
8583           ;; about twice as fast, even though it looks messier.  You
8584           ;; can't have everything, I guess.  Speed and elegance
8585           ;; doesn't always go hand in hand.
8586           (setq 
8587            header
8588            (vector
8589             ;; Number.
8590             (prog1
8591                 (read cur)
8592               (end-of-line)
8593               (setq p (point))
8594               (narrow-to-region (point) 
8595                                 (or (and (search-forward "\n.\n" nil t)
8596                                          (- (point) 2))
8597                                     (point))))
8598             ;; Subject.
8599             (progn
8600               (goto-char p)
8601               (if (search-forward "\nsubject: " nil t)
8602                   (gnus-header-value) "(none)"))
8603             ;; From.
8604             (progn
8605               (goto-char p)
8606               (if (search-forward "\nfrom: " nil t)
8607                   (gnus-header-value) "(nobody)"))
8608             ;; Date.
8609             (progn
8610               (goto-char p)
8611               (if (search-forward "\ndate: " nil t)
8612                   (gnus-header-value) ""))
8613             ;; Message-ID.
8614             (progn
8615               (goto-char p)
8616               (if (search-forward "\nmessage-id: " nil t)
8617                   (setq id (gnus-header-value))
8618                 ;; If there was no message-id, we just fake one to make
8619                 ;; subsequent routines simpler.
8620                 (setq id (concat "none+" 
8621                                  (int-to-string 
8622                                   (setq gnus-newsgroup-none-id 
8623                                         (1+ gnus-newsgroup-none-id)))))))
8624             ;; References.
8625             (progn
8626               (goto-char p)
8627               (if (search-forward "\nreferences: " nil t)
8628                   (prog1
8629                       (gnus-header-value)
8630                     (setq end (match-end 0))
8631                     (save-excursion
8632                       (setq ref 
8633                             (downcase
8634                              (buffer-substring
8635                               (progn 
8636                                 (end-of-line)
8637                                 (search-backward ">" end t)
8638                                 (1+ (point)))
8639                               (progn
8640                                 (search-backward "<" end t)
8641                                 (point)))))))
8642                 ;; Get the references from the in-reply-to header if there
8643                 ;; were no references and the in-reply-to header looks
8644                 ;; promising. 
8645                 (if (and (search-forward "\nin-reply-to: " nil t)
8646                          (setq in-reply-to (gnus-header-value))
8647                          (string-match "<[^>]+>" in-reply-to))
8648                     (prog1
8649                         (setq ref (substring in-reply-to (match-beginning 0)
8650                                              (match-end 0)))
8651                       (setq ref (downcase ref))))
8652                 (setq ref "")))
8653             ;; Chars.
8654             0
8655             ;; Lines.
8656             (progn
8657               (goto-char p)
8658               (if (search-forward "\nlines: " nil t)
8659                   (if (numberp (setq lines (read cur)))
8660                       lines 0)
8661                 0))
8662             ;; Xref.
8663             (progn
8664               (goto-char p)
8665               (and (search-forward "\nxref: " nil t)
8666                    (gnus-header-value)))))
8667           (if (and gnus-nocem-hashtb
8668                    (gnus-gethash id gnus-nocem-hashtb))
8669               ;; Banned article.
8670               (setq header nil)
8671             ;; We do the threading while we read the headers.  The
8672             ;; message-id and the last reference are both entered into
8673             ;; the same hash table.  Some tippy-toeing around has to be
8674             ;; done in case an article has arrived before the article
8675             ;; which it refers to.
8676             (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8677                 (if (car (symbol-value id-dep))
8678                     ;; An article with this Message-ID has already
8679                     ;; been seen, so we ignore this one, except we add
8680                     ;; any additional Xrefs (in case the two articles
8681                     ;; came from different servers).
8682                     (progn
8683                       (mail-header-set-xref 
8684                        (car (symbol-value id-dep))
8685                        (concat (or (mail-header-xref 
8686                                     (car (symbol-value id-dep))) "")
8687                                (or (mail-header-xref header) "")))
8688                       (setq header nil))
8689                   (setcar (symbol-value id-dep) header))
8690               (set id-dep (list header))))
8691           (if header
8692               (progn
8693                 (if (boundp (setq ref-dep (intern ref dependencies)))
8694                     (setcdr (symbol-value ref-dep) 
8695                             (nconc (cdr (symbol-value ref-dep))
8696                                    (list (symbol-value id-dep))))
8697                   (set ref-dep (list nil (symbol-value id-dep))))
8698                 (setq headers (cons header headers))))
8699           (goto-char (point-max))
8700           (widen))
8701         (nreverse headers)))))
8702
8703 ;; The following macros and functions were written by Felix Lee
8704 ;; <flee@cse.psu.edu>. 
8705
8706 (defmacro gnus-nov-read-integer ()
8707   '(prog1
8708        (if (= (following-char) ?\t)
8709            0
8710          (let ((num (condition-case nil (read buffer) (error nil))))
8711            (if (numberp num) num 0)))
8712      (or (eobp) (forward-char 1))))
8713
8714 (defmacro gnus-nov-skip-field ()
8715   '(search-forward "\t" eol 'move))
8716
8717 (defmacro gnus-nov-field ()
8718   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
8719
8720 ;; Goes through the xover lines and returns a list of vectors
8721 (defun gnus-get-newsgroup-headers-xover (sequence)
8722   "Parse the news overview data in the server buffer, and return a
8723 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
8724   ;; Get the Xref when the users reads the articles since most/some
8725   ;; NNTP servers do not include Xrefs when using XOVER.
8726   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
8727   (let ((cur nntp-server-buffer)
8728         (dependencies gnus-newsgroup-dependencies)
8729         number headers header)
8730     (save-excursion
8731       (set-buffer nntp-server-buffer)
8732       ;; Allow the user to mangle the headers before parsing them.
8733       (run-hooks 'gnus-parse-headers-hook)
8734       ;; Allow the user to mangle the headers before parsing them.
8735       (run-hooks 'gnus-parse-headers-hook)
8736       (goto-char (point-min))
8737       (while (and sequence (not (eobp)))
8738         (setq number (read cur))
8739         (while (and sequence (< (car sequence) number))
8740           (setq sequence (cdr sequence)))
8741         (and sequence 
8742              (eq number (car sequence))
8743              (progn
8744                (setq sequence (cdr sequence))
8745                (if (setq header 
8746                          (inline (gnus-nov-parse-line number dependencies)))
8747                    (setq headers (cons header headers)))))
8748         (forward-line 1))
8749       (setq headers (nreverse headers)))
8750     headers))
8751
8752 ;; This function has to be called with point after the article number
8753 ;; on the beginning of the line.
8754 (defun gnus-nov-parse-line (number dependencies)
8755   (let ((none 0)
8756         (eol (gnus-point-at-eol)) 
8757         (buffer (current-buffer))
8758         header ref id id-dep ref-dep)
8759
8760     ;; overview: [num subject from date id refs chars lines misc]
8761     (narrow-to-region (point) eol)
8762     (or (eobp) (forward-char))
8763
8764     (condition-case nil
8765         (setq header
8766               (vector 
8767                number                   ; number
8768                (gnus-nov-field)         ; subject
8769                (gnus-nov-field)         ; from
8770                (gnus-nov-field)         ; date
8771                (setq id (or (gnus-nov-field)
8772                             (concat "none+"
8773                                     (int-to-string 
8774                                      (setq none (1+ none)))))) ; id
8775                (progn
8776                  (save-excursion
8777                    (let ((beg (point)))
8778                      (search-forward "\t" eol)
8779                      (if (search-backward ">" beg t)
8780                          (setq ref 
8781                                (downcase 
8782                                 (buffer-substring 
8783                                  (1+ (point))
8784                                  (progn
8785                                    (search-backward "<" beg t)
8786                                    (point)))))
8787                        (setq ref nil))))
8788                  (gnus-nov-field))      ; refs
8789                (gnus-nov-read-integer)  ; chars
8790                (gnus-nov-read-integer)  ; lines
8791                (if (= (following-char) ?\n)
8792                    nil
8793                  (gnus-nov-field))      ; misc
8794                ))
8795       (error (progn 
8796                (ding)
8797                (gnus-message 4 "Strange nov line")
8798                (setq header nil)
8799                (goto-char eol))))
8800
8801     (widen)
8802
8803     ;; We build the thread tree.
8804     (and header
8805          (if (and gnus-nocem-hashtb
8806                   (gnus-gethash id gnus-nocem-hashtb))
8807              ;; Banned article.
8808              (setq header nil)
8809            (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8810                (if (car (symbol-value id-dep))
8811                    ;; An article with this Message-ID has already been seen,
8812                    ;; so we ignore this one, except we add any additional
8813                    ;; Xrefs (in case the two articles came from different
8814                    ;; servers.
8815                    (progn
8816                      (mail-header-set-xref 
8817                       (car (symbol-value id-dep))
8818                       (concat (or (mail-header-xref 
8819                                    (car (symbol-value id-dep))) "")
8820                               (or (mail-header-xref header) "")))
8821                      (setq header nil))
8822                  (setcar (symbol-value id-dep) header))
8823              (set id-dep (list header)))))
8824     (if header
8825         (progn
8826           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
8827               (setcdr (symbol-value ref-dep) 
8828                       (nconc (cdr (symbol-value ref-dep))
8829                              (list (symbol-value id-dep))))
8830             (set ref-dep (list nil (symbol-value id-dep))))))
8831     header))
8832
8833 (defun gnus-article-get-xrefs ()
8834   "Fill in the Xref value in `gnus-current-headers', if necessary.
8835 This is meant to be called in `gnus-article-internal-prepare-hook'."
8836   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
8837                                  gnus-current-headers)))
8838     (or (not gnus-use-cross-reference)
8839         (not headers)
8840         (and (mail-header-xref headers)
8841              (not (string= (mail-header-xref headers) "")))
8842         (let ((case-fold-search t)
8843               xref)
8844           (save-restriction
8845             (gnus-narrow-to-headers)
8846             (goto-char (point-min))
8847             (if (or (and (eq (downcase (following-char)) ?x)
8848                          (looking-at "Xref:"))
8849                     (search-forward "\nXref:" nil t))
8850                 (progn
8851                   (goto-char (1+ (match-end 0)))
8852                   (setq xref (buffer-substring (point) 
8853                                                (progn (end-of-line) (point))))
8854                   (mail-header-set-xref headers xref))))))))
8855
8856 (defun gnus-summary-insert-subject (id)
8857   "Find article ID and insert the summary line for that article."
8858   (let ((header (gnus-read-header id))
8859         number)
8860     (when header
8861       ;; Rebuild the thread that this article is part of and go to the
8862       ;; article we have fetched.
8863       (gnus-rebuild-thread (mail-header-id header))
8864       (gnus-summary-goto-subject (setq number (mail-header-number header)))
8865       (when (> number 0)
8866         ;; We have to update the boundaries, possibly.
8867         (and (> number gnus-newsgroup-end)
8868              (setq gnus-newsgroup-end number))
8869         (and (< number gnus-newsgroup-begin)
8870              (setq gnus-newsgroup-begin number))
8871         (setq gnus-newsgroup-unselected
8872               (delq number gnus-newsgroup-unselected)))
8873       ;; Report back a success.
8874       number)))
8875
8876 (defun gnus-summary-work-articles (n)
8877   "Return a list of articles to be worked upon.  The prefix argument,
8878 the list of process marked articles, and the current article will be
8879 taken into consideration."
8880   (cond
8881    ((and n (numberp n))
8882     ;; A numerical prefix has been given.
8883     (let ((backward (< n 0))
8884           (n (abs n))
8885           articles article)
8886       (save-excursion
8887         (while 
8888             (and (> n 0)
8889                  (push (setq article (gnus-summary-article-number))
8890                        articles)
8891                  (if backward
8892                      (gnus-summary-find-prev nil article)
8893                    (gnus-summary-find-next nil article)))
8894           (decf n)))
8895       (nreverse articles)))
8896    ((and (boundp 'transient-mark-mode)
8897          transient-mark-mode
8898          mark-active)
8899     ;; Work on the region between point and mark.
8900     (let ((max (max (point) (mark)))
8901           articles article)
8902       (save-excursion
8903         (goto-char (min (point) (mark)))
8904         (while 
8905             (and 
8906              (push (setq article (gnus-summary-article-number)) articles)
8907              (gnus-summary-find-next nil article)
8908              (< (point) max)))
8909         (nreverse articles))))
8910    (gnus-newsgroup-processable
8911     ;; There are process-marked articles present.
8912     (reverse gnus-newsgroup-processable))
8913    (t
8914     ;; Just return the current article.
8915     (list (gnus-summary-article-number)))))
8916
8917 (defun gnus-summary-search-group (&optional backward use-level)
8918   "Search for next unread newsgroup.
8919 If optional argument BACKWARD is non-nil, search backward instead."
8920   (save-excursion
8921     (set-buffer gnus-group-buffer)
8922     (if (gnus-group-search-forward 
8923          backward nil (if use-level (gnus-group-group-level) nil))
8924         (gnus-group-group-name))))
8925
8926 (defun gnus-summary-best-group (&optional exclude-group)
8927   "Find the name of the best unread group.
8928 If EXCLUDE-GROUP, do not go to this group."
8929   (save-excursion
8930     (set-buffer gnus-group-buffer)
8931     (save-excursion
8932       (gnus-group-best-unread-group exclude-group))))
8933
8934 (defun gnus-summary-find-next (&optional unread article backward)
8935   (if backward (gnus-summary-find-prev)
8936     (let* ((article (or article (gnus-summary-article-number)))
8937            (arts (gnus-data-find-list article))
8938            result)
8939       (when (or (not gnus-summary-check-current)
8940                 (not unread)
8941                 (not (gnus-data-unread-p (car arts))))
8942         (setq arts (cdr arts)))
8943       (when (setq result
8944                   (if unread
8945                       (progn
8946                         (while arts
8947                           (when (gnus-data-unread-p (car arts))
8948                             (setq result (car arts)
8949                                   arts nil))
8950                           (setq arts (cdr arts)))
8951                         result)
8952                     (car arts)))
8953         (goto-char (gnus-data-pos result))
8954         (gnus-data-number result)))))
8955
8956 (defun gnus-summary-find-prev (&optional unread article)
8957   (let* ((article (or article (gnus-summary-article-number)))
8958          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
8959          result)
8960     (when (or (not gnus-summary-check-current)
8961               (not unread)
8962               (not (gnus-data-unread-p (car arts))))
8963       (setq arts (cdr arts)))
8964     (if (setq result
8965               (if unread
8966                   (progn
8967                     (while arts
8968                       (and (gnus-data-unread-p (car arts))
8969                            (setq result (car arts)
8970                                  arts nil))
8971                       (setq arts (cdr arts)))
8972                     result)
8973                 (car arts)))
8974         (progn
8975           (goto-char (gnus-data-pos result))
8976           (gnus-data-number result)))))
8977
8978 (defun gnus-summary-find-subject (subject &optional unread backward article)
8979   (let* ((simp-subject (gnus-simplify-subject-fully subject))
8980          (article (or article (gnus-summary-article-number)))
8981          (articles (gnus-data-list backward))
8982          (arts (gnus-data-find-list article articles))
8983          result)
8984     (when (or (not gnus-summary-check-current)
8985               (not unread)
8986               (not (gnus-data-unread-p (car arts))))
8987       (setq arts (cdr arts)))
8988     (while arts
8989       (and (or (not unread)
8990                (gnus-data-unread-p (car arts)))
8991            (vectorp (gnus-data-header (car arts)))
8992            (gnus-subject-equal 
8993             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
8994            (setq result (car arts)
8995                  arts nil))
8996       (setq arts (cdr arts)))
8997     (and result
8998          (goto-char (gnus-data-pos result))
8999          (gnus-data-number result))))
9000
9001 (defun gnus-summary-search-forward (&optional unread subject backward)
9002   (cond (subject
9003          (gnus-summary-find-subject subject unread backward))
9004         (backward
9005          (gnus-summary-find-prev unread))
9006         (t
9007          (gnus-summary-find-next unread))))
9008
9009 (defun gnus-summary-recenter ()
9010   "Center point in the summary window.
9011 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9012 displayed, no centering will be performed." 
9013   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9014   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9015   (let* ((top (cond ((< (window-height) 4) 0)
9016                     ((< (window-height) 7) 1)
9017                     (t 2)))
9018          (height (1- (window-height)))
9019          (bottom (save-excursion (goto-char (point-max))
9020                                  (forward-line (- height))
9021                                  (point)))
9022          (window (get-buffer-window (current-buffer))))
9023     (and 
9024      ;; The user has to want it,
9025      gnus-auto-center-summary 
9026      ;; the article buffer must be displayed,
9027      (get-buffer-window gnus-article-buffer)
9028      ;; Set the window start to either `bottom', which is the biggest
9029      ;; possible valid number, or the second line from the top,
9030      ;; whichever is the least.
9031      (set-window-start
9032       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
9033
9034 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9035 (defun gnus-short-group-name (group &optional levels)
9036   "Collapse GROUP name LEVELS."
9037   (let* ((name "") (foreign "") (depth -1) (skip 1)
9038          (levels (or levels
9039                      (progn
9040                        (while (string-match "\\." group skip)
9041                          (setq skip (match-end 0)
9042                                depth (+ depth 1)))
9043                        depth))))
9044     (if (string-match ":" group)
9045         (setq foreign (substring group 0 (match-end 0))
9046               group (substring group (match-end 0))))
9047     (while group
9048       (if (and (string-match "\\." group) (> levels 0))
9049           (setq name (concat name (substring group 0 1))
9050                 group (substring group (match-end 0))
9051                 levels (- levels 1)
9052                 name (concat name "."))
9053         (setq name (concat foreign name group)
9054               group nil)))
9055     name))
9056
9057 (defun gnus-summary-jump-to-group (newsgroup)
9058   "Move point to NEWSGROUP in group mode buffer."
9059   ;; Keep update point of group mode buffer if visible.
9060   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9061       (save-window-excursion
9062         ;; Take care of tree window mode.
9063         (if (get-buffer-window gnus-group-buffer)
9064             (pop-to-buffer gnus-group-buffer))
9065         (gnus-group-jump-to-group newsgroup))
9066     (save-excursion
9067       ;; Take care of tree window mode.
9068       (if (get-buffer-window gnus-group-buffer)
9069           (pop-to-buffer gnus-group-buffer)
9070         (set-buffer gnus-group-buffer))
9071       (gnus-group-jump-to-group newsgroup))))
9072
9073 ;; This function returns a list of article numbers based on the
9074 ;; difference between the ranges of read articles in this group and
9075 ;; the range of active articles.
9076 (defun gnus-list-of-unread-articles (group)
9077   (let* ((read (gnus-info-read (gnus-get-info group)))
9078          (active (gnus-active group))
9079          (last (cdr active))
9080          first nlast unread)
9081     ;; If none are read, then all are unread. 
9082     (if (not read)
9083         (setq first (car active))
9084       ;; If the range of read articles is a single range, then the
9085       ;; first unread article is the article after the last read
9086       ;; article.  Sounds logical, doesn't it?
9087       (if (not (listp (cdr read)))
9088           (setq first (1+ (cdr read)))
9089         ;; `read' is a list of ranges.
9090         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
9091                                 (car (car read)))) 1)
9092             (setq first 1))
9093         (while read
9094           (if first 
9095               (while (< first nlast)
9096                 (setq unread (cons first unread))
9097                 (setq first (1+ first))))
9098           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
9099           (setq nlast (if (atom (car (cdr read))) 
9100                           (car (cdr read))
9101                         (car (car (cdr read)))))
9102           (setq read (cdr read)))))
9103     ;; And add the last unread articles.
9104     (while (<= first last)
9105       (setq unread (cons first unread))
9106       (setq first (1+ first)))
9107     ;; Return the list of unread articles.
9108     (nreverse unread)))
9109
9110 (defun gnus-list-of-read-articles (group)
9111   "Return a list of unread, unticked and non-dormant articles."
9112   (let* ((info (gnus-get-info group))
9113          (marked (gnus-info-marks info))
9114          (active (gnus-active group)))
9115     (and info active
9116          (gnus-set-difference
9117           (gnus-sorted-complement 
9118            (gnus-uncompress-range active) 
9119            (gnus-list-of-unread-articles group))
9120           (append 
9121            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9122            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9123
9124 ;; Various summary commands
9125
9126 (defun gnus-summary-universal-argument ()
9127   "Perform any operation on all articles marked with the process mark."
9128   (interactive)
9129   (gnus-set-global-variables)
9130   (let ((articles (reverse gnus-newsgroup-processable))
9131         func)
9132     (or articles (error "No articles marked"))
9133     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
9134         (error "Undefined key"))
9135     (while articles
9136       (gnus-summary-goto-subject (car articles))
9137       (command-execute func)
9138       (gnus-summary-remove-process-mark (car articles))
9139       (setq articles (cdr articles)))))
9140
9141 (defun gnus-summary-toggle-truncation (&optional arg)
9142   "Toggle truncation of summary lines.
9143 With arg, turn line truncation on iff arg is positive."
9144   (interactive "P")
9145   (setq truncate-lines
9146         (if (null arg) (not truncate-lines)
9147           (> (prefix-numeric-value arg) 0)))
9148   (redraw-display))
9149
9150 (defun gnus-summary-reselect-current-group (&optional all)
9151   "Once exit and then reselect the current newsgroup.
9152 The prefix argument ALL means to select all articles."
9153   (interactive "P")
9154   (gnus-set-global-variables)
9155   (let ((current-subject (gnus-summary-article-number))
9156         (group gnus-newsgroup-name))
9157     (setq gnus-newsgroup-begin nil)
9158     (gnus-summary-exit)
9159     ;; We have to adjust the point of group mode buffer because the
9160     ;; current point was moved to the next unread newsgroup by
9161     ;; exiting.
9162     (gnus-summary-jump-to-group group)
9163     (gnus-group-read-group all t)
9164     (gnus-summary-goto-subject current-subject)))
9165
9166 (defun gnus-summary-rescan-group (&optional all)
9167   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9168   (interactive "P")
9169   (gnus-set-global-variables)
9170   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
9171   (let ((group gnus-newsgroup-name))
9172     (gnus-summary-exit)
9173     (gnus-summary-jump-to-group group)
9174     (save-excursion
9175       (set-buffer gnus-group-buffer)
9176       (gnus-group-get-new-news-this-group 1))
9177     (gnus-summary-jump-to-group group)
9178     (gnus-group-read-group all)))
9179
9180 (defun gnus-summary-update-info ()
9181   (let* ((group gnus-newsgroup-name))
9182     (when gnus-newsgroup-kill-headers
9183       (setq gnus-newsgroup-killed
9184             (gnus-compress-sequence
9185              (nconc
9186               (gnus-set-sorted-intersection
9187                (gnus-uncompress-range gnus-newsgroup-killed)
9188                (setq gnus-newsgroup-unselected
9189                      (sort gnus-newsgroup-unselected '<)))
9190               (setq gnus-newsgroup-unreads
9191                     (sort gnus-newsgroup-unreads '<))) t)))
9192     (unless (listp (cdr gnus-newsgroup-killed))
9193       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9194     (let ((headers gnus-newsgroup-headers))
9195       (gnus-close-group group)
9196       (run-hooks 'gnus-exit-group-hook)
9197       (unless gnus-save-score
9198         (setq gnus-newsgroup-scored nil))
9199       ;; Set the new ranges of read articles.
9200       (gnus-update-read-articles
9201        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9202       ;; Set the current article marks.
9203       (gnus-update-marks)
9204       ;; Do the cross-ref thing.
9205       (when gnus-use-cross-reference
9206         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9207       ;; Do adaptive scoring, and possibly save score files.
9208       (when gnus-newsgroup-adaptive
9209         (gnus-score-adaptive))
9210       (when gnus-use-scoring 
9211         (gnus-score-save))
9212       ;; Do not switch windows but change the buffer to work.
9213       (set-buffer gnus-group-buffer)
9214       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9215           (gnus-group-update-group group)))))
9216   
9217 (defun gnus-summary-exit (&optional temporary)
9218   "Exit reading current newsgroup, and then return to group selection mode.
9219 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9220   (interactive)
9221   (gnus-set-global-variables)
9222   (gnus-kill-save-kill-buffer)
9223   (let* ((group gnus-newsgroup-name)
9224          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9225          (mode major-mode)
9226          (buf (current-buffer)))
9227     (run-hooks 'gnus-summary-prepare-exit-hook)
9228     ;; Make all changes in this group permanent.
9229     (gnus-summary-update-info)          
9230     (set-buffer buf)
9231     (when gnus-use-cache
9232       (gnus-cache-possibly-remove-articles)
9233       (gnus-cache-save-buffers))
9234     ;; Make sure where I was, and go to next newsgroup.
9235     (set-buffer gnus-group-buffer)
9236     (or quit-config
9237         (progn
9238           (gnus-group-jump-to-group group)
9239           (gnus-group-next-unread-group 1)))
9240     (if temporary
9241         nil                             ;Nothing to do.
9242       ;; If we have several article buffers, we kill them at exit.
9243       (unless gnus-single-article-buffer
9244         (gnus-kill-buffer gnus-article-buffer))
9245       (if (not gnus-kill-summary-on-exit)
9246           (gnus-deaden-summary)
9247         ;; We set all buffer-local variables to nil.  It is unclear why
9248         ;; this is needed, but if we don't, buffer-local variables are
9249         ;; not garbage-collected, it seems.  This would the lead to en
9250         ;; ever-growing Emacs.
9251         (set-buffer buf)
9252         (gnus-summary-clear-local-variables)
9253         ;; We clear the global counterparts of the buffer-local
9254         ;; variables as well, just to be on the safe side.
9255         (gnus-configure-windows 'group 'force)
9256         (gnus-summary-clear-local-variables)
9257         ;; Return to group mode buffer. 
9258         (if (eq mode 'gnus-summary-mode)
9259             (gnus-kill-buffer buf)))
9260       (setq gnus-current-select-method gnus-select-method)
9261       (pop-to-buffer gnus-group-buffer)
9262       ;; Clear the current group name.
9263       (if (not quit-config)
9264           (progn
9265             (gnus-group-jump-to-group group)
9266             (gnus-group-next-unread-group 1))
9267         (if (not (buffer-name (car quit-config)))
9268             (gnus-configure-windows 'group 'force)
9269           (set-buffer (car quit-config))
9270           (and (eq major-mode 'gnus-summary-mode)
9271                (gnus-set-global-variables))
9272           (gnus-configure-windows (cdr quit-config))))
9273       (run-hooks 'gnus-summary-exit-hook)
9274       (unless quit-config
9275         (setq gnus-newsgroup-name nil)))))
9276
9277 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9278 (defun gnus-summary-exit-no-update (&optional no-questions)
9279   "Quit reading current newsgroup without updating read article info."
9280   (interactive)
9281   (gnus-set-global-variables)
9282   (let* ((group gnus-newsgroup-name)
9283          (quit-config (gnus-group-quit-config group)))
9284     (when (or no-questions
9285               gnus-expert-user
9286               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9287       ;; If we have several article buffers, we kill them at exit.
9288       (unless gnus-single-article-buffer
9289         (gnus-kill-buffer gnus-article-buffer))
9290       (if (not gnus-kill-summary-on-exit)
9291           (gnus-deaden-summary)
9292         (gnus-close-group group)
9293         (gnus-summary-clear-local-variables)
9294         (set-buffer gnus-group-buffer)
9295         (gnus-summary-clear-local-variables)
9296         (when (get-buffer gnus-summary-buffer)
9297           (kill-buffer gnus-summary-buffer)))
9298       ;; Return to the group buffer.
9299       (gnus-configure-windows 'group 'force)
9300       ;; Clear the current group name.
9301       (setq gnus-newsgroup-name nil)
9302       (when (equal (gnus-group-group-name) group)
9303         (gnus-group-next-unread-group 1))
9304       (when quit-config
9305         (if (not (buffer-name (car quit-config)))
9306             (gnus-configure-windows 'group 'force)
9307           (set-buffer (car quit-config))
9308           (when (eq major-mode 'gnus-summary-mode)
9309             (gnus-set-global-variables))
9310           (gnus-configure-windows (cdr quit-config)))))))
9311
9312 ;;; Dead summaries.
9313
9314 (defvar gnus-dead-summary-mode-map nil)
9315
9316 (if gnus-dead-summary-mode-map
9317     nil
9318   (setq gnus-dead-summary-map (make-keymap))
9319   (suppress-keymap gnus-dead-summary-map)
9320   (substitute-key-definition
9321    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-map)
9322   (let ((keys '("\C-d" "\r" "\177")))
9323     (while keys
9324       (define-key gnus-dead-summary-map
9325         (pop keys) 'gnus-summary-wake-up-the-dead))))
9326  
9327 (defvar gnus-dead-summary-mode nil
9328   "Minor mode for Gnus summary buffers.")
9329
9330 (defun gnus-dead-summary-mode (&optional arg)
9331   "Minor mode for Gnus summary buffers."
9332   (interactive "P")
9333   (when (eq major-mode 'gnus-summary-mode)
9334     (make-local-variable 'gnus-dead-summary-mode)
9335     (setq gnus-dead-summary-mode 
9336           (if (null arg) (not gnus-dead-summary-mode)
9337             (> (prefix-numeric-value arg) 0)))
9338     (when gnus-dead-summary-mode
9339       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9340         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9341       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9342         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9343               minor-mode-map-alist)))))
9344
9345 (defun gnus-deaden-summary ()
9346   "Make the current summary buffer into a dead summary buffer."
9347   ;; Kill any previous dead summary buffer.
9348   (when (and gnus-dead-summary
9349              (buffer-name gnus-dead-summary))
9350     (save-excursion
9351       (set-buffer gnus-dead-summary)
9352       (when gnus-dead-summary-mode
9353         (kill-buffer (current-buffer)))))
9354   ;; Make this the current dead summary.
9355   (setq gnus-dead-summary (current-buffer))
9356   (gnus-dead-summary-mode 1)
9357   (let ((name (buffer-name)))
9358     (when (string-match "Summary" name)
9359       (rename-buffer
9360        (concat (substring name 0 (match-beginning 0)) "Dead "
9361                (substring name (match-beginning 0))) t))))
9362
9363 (defun gnus-kill-or-deaden-summary (buffer)
9364   "Kill or deaden the summary BUFFER."
9365   (cond (gnus-kill-summary-on-exit
9366          (gnus-kill-buffer buffer))
9367         ((and (get-buffer buffer)
9368               (buffer-name (get-buffer buffer)))
9369          (save-excursion
9370            (set-buffer buffer)
9371            (gnus-deaden-summary)))))
9372
9373 (defun gnus-summary-wake-up-the-dead (&rest args)
9374   "Wake up the dead summary buffer."
9375   (interactive)
9376   (gnus-dead-summary-mode -1)
9377   (let ((name (buffer-name)))
9378     (when (string-match "Dead " name)
9379       (rename-buffer
9380        (concat (substring name 0 (match-beginning 0))
9381                (substring name (match-end 0))) t)))
9382   (gnus-message 3 "This dead summary is now alive again"))
9383
9384 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9385 (defun gnus-summary-fetch-faq (&optional faq-dir)
9386   "Fetch the FAQ for the current group.
9387 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9388 in."
9389   (interactive 
9390    (list
9391     (if current-prefix-arg
9392         (completing-read 
9393          "Faq dir: " (and (listp gnus-group-faq-directory)
9394                           gnus-group-faq-directory)))))
9395   (let (gnus-faq-buffer)
9396     (and (setq gnus-faq-buffer 
9397                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9398          (gnus-configure-windows 'summary-faq))))
9399
9400 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9401 (defun gnus-summary-describe-group (&optional force)
9402   "Describe the current newsgroup."
9403   (interactive "P")
9404   (gnus-group-describe-group force gnus-newsgroup-name))
9405
9406 (defun gnus-summary-describe-briefly ()
9407   "Describe summary mode commands briefly."
9408   (interactive)
9409   (gnus-message 6
9410                 (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")))
9411
9412 ;; Walking around group mode buffer from summary mode.
9413
9414 (defun gnus-summary-next-group (&optional no-article target-group backward)
9415   "Exit current newsgroup and then select next unread newsgroup.
9416 If prefix argument NO-ARTICLE is non-nil, no article is selected
9417 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9418 previous group instead."
9419   (interactive "P")
9420   (gnus-set-global-variables)
9421   (let ((current-group gnus-newsgroup-name)
9422         (current-buffer (current-buffer))
9423         entered)
9424     ;; First we semi-exit this group to update Xrefs and all variables.
9425     ;; We can't do a real exit, because the window conf must remain
9426     ;; the same in case the user is prompted for info, and we don't
9427     ;; want the window conf to change before that...
9428     (gnus-summary-exit t)
9429     (while (not entered)
9430       ;; Then we find what group we are supposed to enter.
9431       (set-buffer gnus-group-buffer)
9432       (gnus-group-jump-to-group current-group)
9433       (setq target-group 
9434             (or target-group        
9435                 (if (eq gnus-keep-same-level 'best) 
9436                     (gnus-summary-best-group gnus-newsgroup-name)
9437                   (gnus-summary-search-group backward gnus-keep-same-level))))
9438       (if (not target-group)
9439           ;; There are no further groups, so we return to the group
9440           ;; buffer.
9441           (progn
9442             (gnus-message 5 "Returning to the group buffer")
9443             (setq entered t)
9444             (set-buffer current-buffer)
9445             (gnus-summary-exit))
9446         ;; We try to enter the target group.
9447         (gnus-group-jump-to-group target-group)
9448         (let ((unreads (gnus-group-group-unread)))
9449           (if (and (or (eq t unreads)
9450                        (and unreads (not (zerop unreads))))
9451                    (gnus-summary-read-group
9452                     target-group nil no-article current-buffer))
9453               (setq entered t)
9454             (setq current-group target-group
9455                   target-group nil)))))))
9456
9457 (defun gnus-summary-prev-group (&optional no-article)
9458   "Exit current newsgroup and then select previous unread newsgroup.
9459 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9460   (interactive "P")
9461   (gnus-summary-next-group no-article nil t))
9462
9463 ;; Walking around summary lines.
9464
9465 (defun gnus-summary-first-subject (&optional unread)
9466   "Go to the first unread subject.
9467 If UNREAD is non-nil, go to the first unread article.
9468 Returns the article selected or nil if there are no unread articles."
9469   (interactive "P")
9470   (prog1
9471       (cond 
9472        ;; Empty summary.
9473        ((null gnus-newsgroup-data)
9474         (gnus-message 3 "No articles in the group")
9475         nil)
9476        ;; Pick the first article.
9477        ((not unread)
9478         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9479         (gnus-data-number (car gnus-newsgroup-data)))
9480        ;; No unread articles.
9481        ((null gnus-newsgroup-unreads)
9482         (gnus-message 3 "No more unread articles")
9483         nil)
9484        ;; Find the first unread article.
9485        (t
9486         (let ((data gnus-newsgroup-data))
9487           (while (and data
9488                       (not (gnus-data-unread-p (car data))))
9489             (setq data (cdr data)))
9490           (if data
9491               (progn
9492                 (goto-char (gnus-data-pos (car data)))
9493                 (gnus-data-number (car data)))))))
9494     (gnus-summary-position-point)))
9495
9496 (defun gnus-summary-next-subject (n &optional unread dont-display)
9497   "Go to next N'th summary line.
9498 If N is negative, go to the previous N'th subject line.
9499 If UNREAD is non-nil, only unread articles are selected.
9500 The difference between N and the actual number of steps taken is
9501 returned."
9502   (interactive "p")
9503   (let ((backward (< n 0))
9504         (n (abs n)))
9505     (while (and (> n 0)
9506                 (if backward
9507                     (gnus-summary-find-prev unread)
9508                   (gnus-summary-find-next unread)))
9509       (setq n (1- n)))
9510     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9511                                (if unread " unread" "")))
9512     (or dont-display
9513         (progn
9514           (gnus-summary-recenter)
9515           (gnus-summary-position-point)))
9516     n))
9517
9518 (defun gnus-summary-next-unread-subject (n)
9519   "Go to next N'th unread summary line."
9520   (interactive "p")
9521   (gnus-summary-next-subject n t))
9522
9523 (defun gnus-summary-prev-subject (n &optional unread)
9524   "Go to previous N'th summary line.
9525 If optional argument UNREAD is non-nil, only unread article is selected."
9526   (interactive "p")
9527   (gnus-summary-next-subject (- n) unread))
9528
9529 (defun gnus-summary-prev-unread-subject (n)
9530   "Go to previous N'th unread summary line."
9531   (interactive "p")
9532   (gnus-summary-next-subject (- n) t))
9533
9534 (defun gnus-summary-goto-subject (article &optional force silent)
9535   "Go the subject line of ARTICLE.
9536 If FORCE, also allow jumping to articles not currently shown."
9537   (let ((b (point))
9538         (data (gnus-data-find article)))
9539     ;; We read in the article if we have to.
9540     (and (not data) 
9541          force
9542          (gnus-summary-insert-subject article)
9543          (setq data (gnus-data-find article)))
9544     (goto-char b)
9545     (if (not data)
9546         (progn
9547           (unless silent
9548             (gnus-message 3 "Can't find article %d" article))
9549           nil)
9550       (goto-char (gnus-data-pos data))
9551       article)))
9552
9553 ;; Walking around summary lines with displaying articles.
9554
9555 (defun gnus-summary-expand-window (&optional arg)
9556   "Make the summary buffer take up the entire Emacs frame.
9557 Given a prefix, will force an `article' buffer configuration."
9558   (interactive "P")
9559   (gnus-set-global-variables)
9560   (if arg
9561       (gnus-configure-windows 'article 'force)
9562     (gnus-configure-windows 'summary 'force)))
9563
9564 (defun gnus-summary-display-article (article &optional all-header)
9565   "Display ARTICLE in article buffer."
9566   (gnus-set-global-variables)
9567   (if (null article)
9568       nil
9569     (prog1
9570         (gnus-article-prepare article all-header)
9571       (gnus-summary-show-thread)
9572       (run-hooks 'gnus-select-article-hook)
9573       (gnus-summary-recenter)
9574       (gnus-summary-goto-subject article)
9575       ;; Successfully display article.
9576       (gnus-summary-update-line)
9577       (gnus-article-set-window-start 
9578        (cdr (assq article gnus-newsgroup-bookmarks)))
9579       t)))
9580
9581 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9582   "Select the current article.
9583 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9584 non-nil, the article will be re-fetched even if it already present in
9585 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9586 be displayed."
9587   (let ((article (or article (gnus-summary-article-number)))
9588         (all-headers (not (not all-headers))) ;Must be T or NIL.
9589         did) 
9590     (and (not pseudo) 
9591          (gnus-summary-article-pseudo-p article)
9592          (error "This is a pseudo-article."))
9593     (prog1
9594         (save-excursion
9595           (set-buffer gnus-summary-buffer)
9596           (if (or (and gnus-single-article-buffer
9597                        (or (null gnus-current-article)
9598                            (null gnus-article-current)
9599                            (null (get-buffer gnus-article-buffer))
9600                            (not (eq article (cdr gnus-article-current)))
9601                            (not (equal (car gnus-article-current)
9602                                        gnus-newsgroup-name))))
9603                   (and (not gnus-single-article-buffer)
9604                        (null gnus-current-article))
9605                   force)
9606               ;; The requested article is different from the current article.
9607               (prog1
9608                   (gnus-summary-display-article article all-headers)
9609                 (setq did article))
9610             (if (or all-headers gnus-show-all-headers) 
9611                 (gnus-article-show-all-headers))
9612             t))
9613       (if did 
9614           (gnus-article-set-window-start 
9615            (cdr (assq article gnus-newsgroup-bookmarks)))))))
9616
9617 (defun gnus-summary-set-current-mark (&optional current-mark)
9618   "Obsolete function."
9619   nil)
9620
9621 (defun gnus-summary-next-article (&optional unread subject backward)
9622   "Select the next article.
9623 If UNREAD, only unread articles are selected.
9624 If SUBJECT, only articles with SUBJECT are selected.
9625 If BACKWARD, the previous article is selected instead of the next."
9626   (interactive "P")
9627   (gnus-set-global-variables)
9628   (let (header)
9629     (cond
9630      ;; Is there such an article?
9631      ((and (gnus-summary-search-forward unread subject backward)
9632            (or (gnus-summary-display-article (gnus-summary-article-number))
9633                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9634       (gnus-summary-position-point))
9635      ;; If not, we try the first unread, if that is wanted.
9636      ((and subject
9637            gnus-auto-select-same
9638            (or (gnus-summary-first-unread-article)
9639                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9640       (gnus-summary-position-point)
9641       (gnus-message 6 "Wrapped"))
9642      ;; Try to get next/previous article not displayed in this group.
9643      ((and gnus-auto-extend-newsgroup
9644            (not unread) (not subject))
9645       (gnus-summary-goto-article 
9646        (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
9647        nil t))
9648      ;; Go to next/previous group.
9649      (t
9650       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9651           (gnus-summary-jump-to-group gnus-newsgroup-name))
9652       (let ((cmd last-command-char)
9653             (group 
9654              (if (eq gnus-keep-same-level 'best) 
9655                  (gnus-summary-best-group gnus-newsgroup-name)
9656                (gnus-summary-search-group backward gnus-keep-same-level))))
9657         ;; For some reason, the group window gets selected.  We change
9658         ;; it back.  
9659         (select-window (get-buffer-window (current-buffer)))
9660         ;; Select next unread newsgroup automagically.
9661         (cond 
9662          ((not gnus-auto-select-next)
9663           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
9664          ((or (eq gnus-auto-select-next 'quietly)
9665               (and (eq gnus-auto-select-next 'almost-quietly)
9666                    (gnus-summary-last-article-p)))
9667           ;; Select quietly.
9668           (if (gnus-ephemeral-group-p gnus-newsgroup-name)
9669               (gnus-summary-exit)
9670             (gnus-message 7 "No more%s articles (%s)..."
9671                           (if unread " unread" "") 
9672                           (if group (concat "selecting " group)
9673                             "exiting"))
9674             (gnus-summary-next-group nil group backward)))
9675          (t
9676           (gnus-summary-walk-group-buffer 
9677            gnus-newsgroup-name cmd unread backward))))))))
9678
9679 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
9680   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
9681                       (?\C-p (gnus-group-prev-unread-group 1))))
9682         keve key group ended)
9683     (save-excursion
9684       (set-buffer gnus-group-buffer)
9685       (gnus-summary-jump-to-group from-group)
9686       (setq group (gnus-summary-search-group backward)))
9687     (while (not ended)
9688       (gnus-message 
9689        7 "No more%s articles%s" (if unread " unread" "")
9690        (if (and group 
9691                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
9692            (format " (Type %s for %s [%s])"
9693                    (single-key-description cmd) group
9694                    (car (gnus-gethash group gnus-newsrc-hashtb)))
9695          (format " (Type %s to exit %s)"
9696                  (single-key-description cmd)
9697                  gnus-newsgroup-name)))
9698       ;; Confirm auto selection.
9699       (setq key (car (setq keve (gnus-read-event-char))))
9700       (setq ended t)
9701       (cond 
9702        ((assq key keystrokes)
9703         (let ((obuf (current-buffer)))
9704           (switch-to-buffer gnus-group-buffer)
9705           (and group
9706                (gnus-group-jump-to-group group))
9707           (eval (car (cdr (assq key keystrokes))))
9708           (setq group (gnus-group-group-name))
9709           (switch-to-buffer obuf))
9710         (setq ended nil))
9711        ((equal key cmd)
9712         (if (or (not group)
9713                 (gnus-ephemeral-group-p gnus-newsgroup-name))
9714             (gnus-summary-exit)
9715           (gnus-summary-next-group nil group backward)))
9716        (t
9717         (push (cdr keve) unread-command-events))))))
9718
9719 (defun gnus-read-event-char ()
9720   "Get the next event."
9721   (let ((event (read-event)))
9722     (cons (and (numberp event) event) event)))
9723
9724 (defun gnus-summary-next-unread-article ()
9725   "Select unread article after current one."
9726   (interactive)
9727   (gnus-summary-next-article t (and gnus-auto-select-same
9728                                     (gnus-summary-article-subject))))
9729
9730 (defun gnus-summary-prev-article (&optional unread subject)
9731   "Select the article after the current one.
9732 If UNREAD is non-nil, only unread articles are selected."
9733   (interactive "P")
9734   (gnus-summary-next-article unread subject t))
9735
9736 (defun gnus-summary-prev-unread-article ()
9737   "Select unred article before current one."
9738   (interactive)
9739   (gnus-summary-prev-article t (and gnus-auto-select-same
9740                                     (gnus-summary-article-subject))))
9741
9742 (defun gnus-summary-next-page (&optional lines circular)
9743   "Show next page of selected article.
9744 If end of article, select next article.
9745 Argument LINES specifies lines to be scrolled up.
9746 If CIRCULAR is non-nil, go to the start of the article instead of 
9747 instead of selecting the next article when reaching the end of the
9748 current article." 
9749   (interactive "P")
9750   (setq gnus-summary-buffer (current-buffer))
9751   (gnus-set-global-variables)
9752   (let ((article (gnus-summary-article-number))
9753         (endp nil))
9754     (gnus-configure-windows 'article)
9755     (if (or (null gnus-current-article)
9756             (null gnus-article-current)
9757             (/= article (cdr gnus-article-current))
9758             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
9759         ;; Selected subject is different from current article's.
9760         (gnus-summary-display-article article)
9761       (gnus-eval-in-buffer-window
9762        gnus-article-buffer
9763        (setq endp (gnus-article-next-page lines)))
9764       (if endp
9765           (cond (circular
9766                  (gnus-summary-beginning-of-article))
9767                 (lines
9768                  (gnus-message 3 "End of message"))
9769                 ((null lines)
9770                  (gnus-summary-next-unread-article)))))
9771     (gnus-summary-recenter)
9772     (gnus-summary-position-point)))
9773
9774 (defun gnus-summary-prev-page (&optional lines)
9775   "Show previous page of selected article.
9776 Argument LINES specifies lines to be scrolled down."
9777   (interactive "P")
9778   (gnus-set-global-variables)
9779   (let ((article (gnus-summary-article-number)))
9780     (gnus-configure-windows 'article)
9781     (if (or (null gnus-current-article)
9782             (null gnus-article-current)
9783             (/= article (cdr gnus-article-current))
9784             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
9785         ;; Selected subject is different from current article's.
9786         (gnus-summary-display-article article)
9787       (gnus-summary-recenter)
9788       (gnus-eval-in-buffer-window gnus-article-buffer
9789                                   (gnus-article-prev-page lines))))
9790   (gnus-summary-position-point))
9791
9792 (defun gnus-summary-scroll-up (lines)
9793   "Scroll up (or down) one line current article.
9794 Argument LINES specifies lines to be scrolled up (or down if negative)."
9795   (interactive "p")
9796   (gnus-set-global-variables)
9797   (gnus-configure-windows 'article)
9798   (or (gnus-summary-select-article nil nil 'pseudo)
9799       (gnus-eval-in-buffer-window 
9800        gnus-article-buffer
9801        (cond ((> lines 0)
9802               (if (gnus-article-next-page lines)
9803                   (gnus-message 3 "End of message")))
9804              ((< lines 0)
9805               (gnus-article-prev-page (- lines))))))
9806   (gnus-summary-recenter)
9807   (gnus-summary-position-point))
9808
9809 (defun gnus-summary-next-same-subject ()
9810   "Select next article which has the same subject as current one."
9811   (interactive)
9812   (gnus-set-global-variables)
9813   (gnus-summary-next-article nil (gnus-summary-article-subject)))
9814
9815 (defun gnus-summary-prev-same-subject ()
9816   "Select previous article which has the same subject as current one."
9817   (interactive)
9818   (gnus-set-global-variables)
9819   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
9820
9821 (defun gnus-summary-next-unread-same-subject ()
9822   "Select next unread article which has the same subject as current one."
9823   (interactive)
9824   (gnus-set-global-variables)
9825   (gnus-summary-next-article t (gnus-summary-article-subject)))
9826
9827 (defun gnus-summary-prev-unread-same-subject ()
9828   "Select previous unread article which has the same subject as current one."
9829   (interactive)
9830   (gnus-set-global-variables)
9831   (gnus-summary-prev-article t (gnus-summary-article-subject)))
9832
9833 (defun gnus-summary-first-unread-article ()
9834   "Select the first unread article. 
9835 Return nil if there are no unread articles."
9836   (interactive)
9837   (gnus-set-global-variables)
9838   (prog1
9839       (if (gnus-summary-first-subject t)
9840           (progn
9841             (gnus-summary-show-thread)
9842             (gnus-summary-first-subject t)
9843             (gnus-summary-display-article (gnus-summary-article-number))))
9844     (gnus-summary-position-point)))
9845
9846 (defun gnus-summary-best-unread-article ()
9847   "Select the unread article with the highest score."
9848   (interactive)
9849   (gnus-set-global-variables)
9850   (let ((best -1000000)
9851         (data gnus-newsgroup-data)
9852         article score)
9853     (while data
9854       (and (gnus-data-unread-p (car data))
9855            (> (setq score 
9856                     (gnus-summary-article-score (gnus-data-number (car data))))
9857               best)
9858            (setq best score
9859                  article (gnus-data-number (car data))))
9860       (setq data (cdr data)))
9861     (if article
9862         (gnus-summary-goto-article article)
9863       (error "No unread articles"))
9864     (gnus-summary-position-point)))
9865
9866 (defun gnus-summary-last-subject ()
9867   "Go to the last displayed subject line in the group."
9868   (let ((article (gnus-data-number (car (gnus-data-list t)))))
9869     (when article
9870       (gnus-summary-goto-subject article))))
9871
9872 (defun gnus-summary-goto-article (article &optional all-headers force)
9873   "Fetch ARTICLE and display it if it exists.
9874 If ALL-HEADERS is non-nil, no header lines are hidden."
9875   (interactive
9876    (list
9877     (string-to-int
9878      (completing-read 
9879       "Article number: "
9880       (mapcar (lambda (number) (list (int-to-string number)))
9881               gnus-newsgroup-limit)))
9882     current-prefix-arg
9883     t))
9884   (prog1
9885       (if (gnus-summary-goto-subject article force)
9886           (gnus-summary-display-article article all-headers)
9887         (gnus-message 4 "Couldn't go to article %s" article) nil)
9888     (gnus-summary-position-point)))
9889
9890 (defun gnus-summary-goto-last-article ()
9891   "Go to the previously read article."
9892   (interactive)
9893   (prog1
9894       (and gnus-last-article
9895            (gnus-summary-goto-article gnus-last-article))
9896     (gnus-summary-position-point)))
9897
9898 (defun gnus-summary-pop-article (number)
9899   "Pop one article off the history and go to the previous.
9900 NUMBER articles will be popped off."
9901   (interactive "p")
9902   (let (to)
9903     (setq gnus-newsgroup-history
9904           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
9905     (if to
9906         (gnus-summary-goto-article (car to))
9907       (error "Article history empty")))
9908   (gnus-summary-position-point))
9909
9910 ;; Summary commands and functions for limiting the summary buffer.
9911
9912 (defun gnus-summary-limit-to-articles (n)
9913   "Limit the summary buffer to the next N articles.
9914 If not given a prefix, use the process marked articles instead."
9915   (interactive "P")
9916   (gnus-set-global-variables)
9917   (prog1
9918       (let ((articles (gnus-summary-work-articles n)))
9919         (setq gnus-newsgroup-processable nil)
9920         (gnus-summary-limit articles))
9921     (gnus-summary-position-point)))
9922
9923 (defun gnus-summary-pop-limit (&optional total)
9924   "Restore the previous limit.
9925 If given a prefix, remove all limits."
9926   (interactive "P")
9927   (gnus-set-global-variables)
9928   (prog2
9929       (if total (setq gnus-newsgroup-limits 
9930                       (list (mapcar (lambda (h) (mail-header-number h))
9931                                     gnus-newsgroup-headers))))
9932       (gnus-summary-limit nil 'pop)
9933     (gnus-summary-position-point)))
9934
9935 (defun gnus-summary-limit-to-subject (subject)
9936   "Limit the summary buffer to articles that have subjects that match a regexp."
9937   (interactive "sRegexp: ")
9938   (when (not (equal "" subject))
9939     (prog1
9940         (let ((articles (gnus-summary-find-matching "subject" subject 'all)))
9941           (or articles (error "Found no matches for \"%s\"" subject))
9942           (gnus-summary-limit articles))
9943       (gnus-summary-position-point))))
9944
9945 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
9946 (make-obsolete 
9947  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
9948
9949 (defun gnus-summary-limit-to-unread (&optional all)
9950   "Limit the summary buffer to articles that are not marked as read.
9951 If ALL is non-nil, limit strictly to unread articles."
9952   (interactive "P")
9953   (if all
9954       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
9955     (gnus-summary-limit-to-marks
9956      ;; Concat all the marks that say that an article is read and have
9957      ;; those removed.  
9958      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
9959            gnus-killed-mark gnus-kill-file-mark
9960            gnus-low-score-mark gnus-expirable-mark
9961            gnus-canceled-mark gnus-catchup-mark)
9962      'reverse)))
9963
9964 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
9965 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
9966
9967 (defun gnus-summary-limit-to-marks (marks &optional reverse)
9968   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
9969 If REVERSE, limit the summary buffer to articles that are not marked
9970 with MARKS.  MARKS can either be a string of marks or a list of marks. 
9971 Returns how many articles were removed."
9972   (interactive "sMarks: ")
9973   (gnus-set-global-variables)
9974   (prog1
9975       (let ((data gnus-newsgroup-data)
9976             (marks (if (listp marks) marks
9977                      (append marks nil))) ; Transform to list.
9978             articles)
9979         (while data
9980           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
9981                  (memq (gnus-data-mark (car data)) marks))
9982                (setq articles (cons (gnus-data-number (car data)) articles)))
9983           (setq data (cdr data)))
9984         (gnus-summary-limit articles))
9985     (gnus-summary-position-point)))
9986
9987 (defun gnus-summary-limit-to-score (&optional score)
9988   "Limit to articles with score at or above SCORE."
9989   (interactive "P")
9990   (gnus-set-global-variables)
9991   (setq score (if score
9992                   (prefix-numeric-value score)
9993                 (or gnus-summary-default-score 0)))
9994   (let ((data gnus-newsgroup-data)
9995         articles)
9996     (while data
9997       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
9998                 score)
9999         (push (gnus-data-number (car data)) articles))
10000       (setq data (cdr data)))
10001     (prog1
10002         (gnus-summary-limit articles)
10003       (gnus-summary-position-point))))
10004
10005 (defun gnus-summary-limit-include-dormant ()
10006   "Display all the hidden articles that are marked as dormant."
10007   (interactive)
10008   (gnus-set-global-variables)
10009   (or gnus-newsgroup-dormant 
10010       (error "There are no dormant articles in this group"))
10011   (prog1
10012       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10013     (gnus-summary-position-point)))
10014
10015 (defun gnus-summary-limit-exclude-dormant ()
10016   "Hide all dormant articles."
10017   (interactive)
10018   (gnus-set-global-variables)
10019   (prog1
10020       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10021     (gnus-summary-position-point)))
10022
10023 (defun gnus-summary-limit-exclude-childless-dormant ()
10024   "Hide all dormant articles that have no children."
10025   (interactive)
10026   (gnus-set-global-variables)
10027   (let ((data gnus-newsgroup-data)
10028         articles)
10029     ;; Find all articles that are either not dormant or have
10030     ;; children. 
10031     (while data
10032       (and (or (not (= (gnus-data-mark (car data)) gnus-dormant-mark))
10033                (gnus-article-parent-p (gnus-data-number (car data))))
10034            (setq articles (cons (gnus-data-number (car data))
10035                                 articles)))
10036       (setq data (cdr data)))
10037     ;; Do the limiting.
10038     (prog1
10039         (gnus-summary-limit articles)
10040       (gnus-summary-position-point))))
10041  
10042 (defun gnus-summary-limit (articles &optional pop)
10043   (if pop
10044       ;; We pop the previous limit off the stack and use that.
10045       (setq articles (car gnus-newsgroup-limits)
10046             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10047     ;; We use the new limit, so we push the old limit on the stack. 
10048     (setq gnus-newsgroup-limits 
10049           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10050   ;; Set the limit.
10051   (setq gnus-newsgroup-limit articles)
10052   (let ((total (length gnus-newsgroup-data))
10053         (data (gnus-data-find-list (gnus-summary-article-number)))
10054         found)
10055     ;; This will do all the work of generating the new summary buffer
10056     ;; according to the new limit.
10057     (gnus-summary-prepare)
10058     ;; Try to return to the article you were at, or on in the
10059     ;; neighborhood.  
10060     (if data
10061         ;; We try to find some article after the current one.
10062         (while data
10063           (and (gnus-summary-goto-subject 
10064                 (gnus-data-number (car data)) nil t)
10065                (setq data nil
10066                      found t))
10067           (setq data (cdr data))))
10068     (or found
10069         ;; If there is no data, that means that we were after the last
10070         ;; article.  The same goes when we can't find any articles
10071         ;; after the current one.
10072         (progn
10073           (goto-char (point-max))
10074           (gnus-summary-find-prev)))
10075     ;; We return how many articles were removed from the summary
10076     ;; buffer as a result of the new limit.
10077     (- total (length gnus-newsgroup-data))))
10078
10079 (defun gnus-summary-initial-limit ()
10080   "Figure out what the initial limit is supposed to be on group entry.
10081 This entails weeding out unwanted dormants, low-scored articles,
10082 fetch-old-headers verbiage, and so on."
10083   ;; Most groups have nothing to remove.
10084   (if (or gnus-inhibit-limiting
10085           (and (null gnus-newsgroup-dormant)
10086                (not (eq gnus-fetch-old-headers 'some))
10087                (null gnus-summary-expunge-below)
10088                (null gnus-thread-expunge-below)))
10089       () ; Do nothing.
10090     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10091     (setq gnus-newsgroup-limit nil)
10092     (mapatoms
10093      (lambda (node)
10094        (unless (car (symbol-value node))
10095          ;; These threads have no parents -- they are roots.
10096          (let ((nodes (cdr (symbol-value node))))
10097            (while nodes
10098              (if (and gnus-thread-expunge-below
10099                       (< (gnus-thread-total-score (car nodes))
10100                          gnus-thread-expunge-below))
10101                  (gnus-expunge-thread (pop nodes))
10102                (gnus-summary-limit-children (pop nodes)))))))
10103      gnus-newsgroup-dependencies)
10104     (when (not gnus-newsgroup-limit)
10105       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10106     gnus-newsgroup-limit))
10107
10108 (defun gnus-summary-limit-children (thread)
10109   "Return 1 if this subthread is visible and 0 if it is not."
10110   ;; First we get the number of visible children to this thread.  This
10111   ;; is done by recursing down the thread using this function, so this
10112   ;; will really go down to a leaf article first, before slowly
10113   ;; working its way up towards the root.
10114   (let ((children 
10115          (if (cdr thread)
10116              (apply '+ (mapcar 'gnus-summary-limit-children 
10117                                (cdr thread)))
10118            0))
10119         (number (mail-header-number (car thread)))
10120         score)
10121     (if (or 
10122          ;; If this article is dormant and has absolutely no visible
10123          ;; children, then this article isn't visible.
10124          (and (memq number gnus-newsgroup-dormant)
10125               (= children 0))
10126          ;; If this is a "fetch-old-headered" and there is only one
10127          ;; visible child (or less), then we don't want this article. 
10128          (and (eq gnus-fetch-old-headers 'some)
10129               (memq number gnus-newsgroup-ancient)
10130               (<= children 1))
10131          ;; If we use expunging, and this article is really
10132          ;; low-scored, then we don't want this article.
10133          (when (and gnus-summary-expunge-below
10134                     (< (setq score 
10135                              (or (cdr (assq number gnus-newsgroup-scored)) 
10136                                  gnus-summary-default-score))
10137                        gnus-summary-expunge-below))
10138            ;; We increase the expunge-tally here, but that has
10139            ;; nothing to do with the limits, really.
10140            (incf gnus-newsgroup-expunged-tally)
10141            ;; We also mark as read here, if that's wanted.
10142            (when (and gnus-summary-mark-below
10143                       (< score gnus-summary-mark-below))
10144              (setq gnus-newsgroup-unreads 
10145                    (delq number gnus-newsgroup-unreads))
10146              (if gnus-newsgroup-auto-expire
10147                  (push number gnus-newsgroup-expirable)
10148                (push (cons number gnus-low-score-mark)
10149                      gnus-newsgroup-reads)))
10150            t))
10151         ;; Nope, invisible article.
10152         0
10153       ;; Ok, this article is to be visible, so we add it to the limit
10154       ;; and return 1.
10155       (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10156       1)))
10157
10158 (defun gnus-expunge-thread (thread)
10159   "Mark all articles in THREAD as read."
10160   (let* ((number (mail-header-number (car thread))))
10161     (incf gnus-newsgroup-expunged-tally)
10162     ;; We also mark as read here, if that's wanted.
10163     (setq gnus-newsgroup-unreads 
10164           (delq number gnus-newsgroup-unreads))
10165     (if gnus-newsgroup-auto-expire
10166         (push number gnus-newsgroup-expirable)
10167       (push (cons number gnus-low-score-mark)
10168             gnus-newsgroup-reads)))
10169   ;; Go recursively through all subthreads.
10170   (mapcar 'gnus-expunge-thread (cdr thread)))
10171
10172 ;; Summary article oriented commands
10173
10174 (defun gnus-summary-refer-parent-article (n)
10175   "Refer parent article N times.
10176 The difference between N and the number of articles fetched is returned."
10177   (interactive "p")
10178   (gnus-set-global-variables)
10179   (while 
10180       (and 
10181        (> n 0)
10182        (let* ((header (gnus-summary-article-header))
10183               (ref 
10184                ;; If we try to find the parent of the currently
10185                ;; displayed article, then we take a look at the actual
10186                ;; References header, since this is slightly more
10187                ;; reliable than the References field we got from the
10188                ;; server. 
10189                (if (and (eq (mail-header-number header) 
10190                             (cdr gnus-article-current))
10191                         (equal gnus-newsgroup-name 
10192                                (car gnus-article-current)))
10193                    (save-excursion
10194                      (set-buffer gnus-original-article-buffer)
10195                      (gnus-narrow-to-headers)
10196                      (prog1
10197                          (mail-fetch-field "references")
10198                        (widen)))
10199                  ;; It's not the current article, so we take a bet on
10200                  ;; the value we got from the server. 
10201                  (mail-header-references header))))
10202          (if ref
10203              (or (gnus-summary-refer-article (gnus-parent-id ref))
10204                  (gnus-message 1 "Couldn't find parent"))
10205            (gnus-message 1 "No references in article %d"
10206                          (gnus-summary-article-number))
10207            nil)))
10208     (setq n (1- n)))
10209   (gnus-summary-position-point)
10210   n)
10211
10212 (defun gnus-summary-refer-references ()
10213   "Fetch all articles mentioned in the References header.
10214 Return how many articles were fetched."
10215   (interactive)
10216   (gnus-set-global-variables)
10217   (let ((ref (mail-header-references (gnus-summary-article-header)))
10218         (current (gnus-summary-article-number))
10219         (n 0))
10220     ;; For each Message-ID in the References header...
10221     (while (string-match "<[^>]*>" ref)
10222       (incf n)
10223       ;; ... fetch that article.
10224       (gnus-summary-refer-article 
10225        (prog1 (match-string 0 ref)
10226          (setq ref (substring ref (match-end 0))))))
10227     (gnus-summary-goto-subject current)
10228     (gnus-summary-position-point)
10229     n))
10230     
10231 (defun gnus-summary-refer-article (message-id)
10232   "Fetch an article specified by MESSAGE-ID."
10233   (interactive "sMessage-ID: ")
10234   (when (and (stringp message-id)
10235              (not (zerop (length message-id))))
10236     ;; Construct the correct Message-ID if necessary.
10237     ;; Suggested by tale@pawl.rpi.edu.
10238     (unless (string-match "^<" message-id)
10239       (setq message-id (concat "<" message-id)))
10240     (unless (string-match ">$" message-id)
10241       (setq message-id (concat message-id ">")))
10242     (let ((header (car (gnus-gethash (downcase message-id)
10243                                      gnus-newsgroup-dependencies))))
10244       (if header
10245           ;; The article is present in the buffer, to we just go to it.
10246           (gnus-summary-goto-article (mail-header-number header) nil t)
10247         ;; We fetch the article
10248         (let ((gnus-override-method gnus-refer-article-method)
10249               number)
10250           ;; Start the special refer-article method, if necessary.
10251           (when gnus-refer-article-method
10252             (gnus-check-server gnus-refer-article-method))
10253           ;; Fetch the header, and display the article.
10254           (when (setq number (gnus-summary-insert-subject message-id))
10255             (gnus-summary-select-article nil nil nil number)))))))
10256
10257 (defun gnus-summary-enter-digest-group (&optional force)
10258   "Enter a digest group based on the current article."
10259   (interactive "P")
10260   (gnus-set-global-variables)
10261   (gnus-summary-select-article)
10262   (let ((name (format "%s-%d" 
10263                       (gnus-group-prefixed-name 
10264                        gnus-newsgroup-name (list 'nndoc "")) 
10265                       gnus-current-article))
10266         (ogroup gnus-newsgroup-name)
10267         (buf (current-buffer)))
10268     (save-excursion
10269       (set-buffer gnus-original-article-buffer)
10270       (goto-char (point-min))
10271       (search-forward "\n\n" nil t)
10272       (narrow-to-region (point) (point-max)))
10273     (unwind-protect
10274         (if (gnus-group-read-ephemeral-group 
10275              name `(nndoc ,name (nndoc-address 
10276                                  ,(get-buffer gnus-original-article-buffer))
10277                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10278             ;; Make all postings to this group go to the parent group.
10279             (setcdr (nthcdr 4 (gnus-get-info name))
10280                     (list (list (cons 'to-group ogroup))))
10281           ;; Couldn't select this doc group.
10282           (switch-to-buffer buf)
10283           (gnus-set-global-variables)
10284           (gnus-configure-windows 'summary)
10285           (gnus-message 3 "Article couldn't be entered?"))
10286       (save-excursion
10287         (set-buffer gnus-original-article-buffer)
10288         (widen)))))
10289
10290 (defun gnus-summary-isearch-article (&optional regexp-p)
10291   "Do incremental search forward on the current article.
10292 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10293   (interactive "P")
10294   (gnus-set-global-variables)
10295   (gnus-summary-select-article)
10296   (gnus-eval-in-buffer-window 
10297    gnus-article-buffer
10298    (goto-char (point-min))
10299    (isearch-forward regexp-p)))
10300
10301 (defun gnus-summary-search-article-forward (regexp &optional backward)
10302   "Search for an article containing REGEXP forward.
10303 If BACKWARD, search backward instead."
10304   (interactive
10305    (list (read-string
10306           (format "Search article %s (regexp%s): "
10307                   (if current-prefix-arg "backward" "forward")
10308                   (if gnus-last-search-regexp
10309                       (concat ", default " gnus-last-search-regexp)
10310                     "")))
10311          current-prefix-arg))
10312   (gnus-set-global-variables)
10313   (if (string-equal regexp "")
10314       (setq regexp (or gnus-last-search-regexp ""))
10315     (setq gnus-last-search-regexp regexp))
10316   (if (gnus-summary-search-article regexp backward)
10317       (gnus-article-set-window-start 
10318        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10319     (error "Search failed: \"%s\"" regexp)))
10320
10321 (defun gnus-summary-search-article-backward (regexp)
10322   "Search for an article containing REGEXP backward."
10323   (interactive
10324    (list (read-string
10325           (format "Search article backward (regexp%s): "
10326                   (if gnus-last-search-regexp
10327                       (concat ", default " gnus-last-search-regexp)
10328                     "")))))
10329   (gnus-summary-search-article-forward regexp 'backward))
10330
10331 (defun gnus-summary-search-article (regexp &optional backward)
10332   "Search for an article containing REGEXP.
10333 Optional argument BACKWARD means do search for backward.
10334 gnus-select-article-hook is not called during the search."
10335   (let ((gnus-select-article-hook nil)  ;Disable hook.
10336         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10337         (re-search
10338          (if backward
10339              (function re-search-backward) (function re-search-forward)))
10340         (found nil)
10341         (last nil))
10342     ;; Hidden thread subtrees must be searched for ,too.
10343     (gnus-summary-show-all-threads)
10344     ;; First of all, search current article.
10345     ;; We don't want to read article again from NNTP server nor reset
10346     ;; current point.
10347     (gnus-summary-select-article)
10348     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10349     (setq last gnus-current-article)
10350     (gnus-eval-in-buffer-window
10351      gnus-article-buffer
10352      (save-restriction
10353        (widen)
10354        ;; Begin search from current point.
10355        (setq found (funcall re-search regexp nil t))))
10356     ;; Then search next articles.
10357     (while (and (not found)
10358                 (gnus-summary-display-article 
10359                  (if backward (gnus-summary-find-prev)
10360                    (gnus-summary-find-next))))
10361       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10362       (gnus-eval-in-buffer-window
10363        gnus-article-buffer
10364        (save-restriction
10365          (widen)
10366          (goto-char (if backward (point-max) (point-min)))
10367          (setq found (funcall re-search regexp nil t)))))
10368     (message "")
10369     ;; Adjust article pointer.
10370     (or (eq last gnus-current-article)
10371         (setq gnus-last-article last))
10372     ;; Return T if found such article.
10373     found))
10374
10375 (defun gnus-summary-find-matching (header regexp &optional backward unread
10376                                           not-case-fold)
10377   "Return a list of all articles that match REGEXP on HEADER.
10378 The search stars on the current article and goes forwards unless
10379 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10380 If UNREAD is non-nil, only unread articles will
10381 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10382 in the comparisons."
10383   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10384                 (gnus-data-find-list 
10385                  (gnus-summary-article-number) (gnus-data-list backward))))
10386         (func (intern (concat "gnus-header-" header)))
10387         (case-fold-search (not not-case-fold))
10388         articles d)
10389     (or (fboundp func) (error "%s is not a valid header" header))
10390     (while data
10391       (setq d (car data))
10392       (and (or (not unread)             ; We want all articles...
10393                (gnus-data-unread-p d))  ; Or just unreads.
10394            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10395            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10396            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10397       (setq data (cdr data)))
10398     (nreverse articles)))
10399     
10400 (defun gnus-summary-execute-command (header regexp command &optional backward)
10401   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10402 If HEADER is an empty string (or nil), the match is done on the entire
10403 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10404   (interactive
10405    (list (let ((completion-ignore-case t))
10406            (completing-read 
10407             "Header name: "
10408             (mapcar (lambda (string) (list string))
10409                     '("Number" "Subject" "From" "Lines" "Date"
10410                       "Message-ID" "Xref" "References"))
10411             nil 'require-match))
10412          (read-string "Regexp: ")
10413          (read-key-sequence "Command: ")
10414          current-prefix-arg))
10415   (gnus-set-global-variables)
10416   ;; Hidden thread subtrees must be searched as well.
10417   (gnus-summary-show-all-threads)
10418   ;; We don't want to change current point nor window configuration.
10419   (save-excursion
10420     (save-window-excursion
10421       (gnus-message 6 "Executing %s..." (key-description command))
10422       ;; We'd like to execute COMMAND interactively so as to give arguments.
10423       (gnus-execute header regexp
10424                     `(lambda () (call-interactively ',(key-binding command)))
10425                     backward)
10426       (gnus-message 6 "Executing %s...done" (key-description command)))))
10427
10428 (defun gnus-summary-beginning-of-article ()
10429   "Scroll the article back to the beginning."
10430   (interactive)
10431   (gnus-set-global-variables)
10432   (gnus-summary-select-article)
10433   (gnus-configure-windows 'article)
10434   (gnus-eval-in-buffer-window
10435    gnus-article-buffer
10436    (widen)
10437    (goto-char (point-min))
10438    (and gnus-break-pages (gnus-narrow-to-page))))
10439
10440 (defun gnus-summary-end-of-article ()
10441   "Scroll to the end of the article."
10442   (interactive)
10443   (gnus-set-global-variables)
10444   (gnus-summary-select-article)
10445   (gnus-configure-windows 'article)
10446   (gnus-eval-in-buffer-window 
10447    gnus-article-buffer
10448    (widen)
10449    (goto-char (point-max))
10450    (recenter -3)
10451    (and gnus-break-pages (gnus-narrow-to-page))))
10452
10453 (defun gnus-summary-show-article (&optional arg)
10454   "Force re-fetching of the current article.
10455 If ARG (the prefix) is non-nil, show the raw article without any
10456 article massaging functions being run."
10457   (interactive "P")
10458   (gnus-set-global-variables)
10459   (if (not arg)
10460       ;; Select the article the normal way.
10461       (gnus-summary-select-article nil 'force)
10462     ;; Bind the article treatment functions to nil.
10463     (let ((gnus-have-all-headers t)
10464           gnus-article-display-hook
10465           gnus-article-prepare-hook
10466           gnus-visual)
10467       (gnus-summary-select-article nil 'force)))
10468   (gnus-configure-windows 'article)
10469   (gnus-summary-position-point))
10470
10471 (defun gnus-summary-verbose-headers (&optional arg)
10472   "Toggle permanent full header display.
10473 If ARG is a positive number, turn header display on.
10474 If ARG is a negative number, turn header display off."
10475   (interactive "P")
10476   (gnus-set-global-variables)
10477   (gnus-summary-toggle-header arg)
10478   (setq gnus-show-all-headers
10479         (cond ((or (not (numberp arg))
10480                    (zerop arg))
10481                (not gnus-show-all-headers))
10482               ((natnump arg)
10483                t))))
10484
10485 (defun gnus-summary-toggle-header (&optional arg)
10486   "Show the headers if they are hidden, or hide them if they are shown.
10487 If ARG is a positive number, show the entire header.
10488 If ARG is a negative number, hide the unwanted header lines."
10489   (interactive "P")
10490   (gnus-set-global-variables)
10491   (save-excursion
10492     (set-buffer gnus-article-buffer)
10493     (let* ((buffer-read-only nil)
10494            (inhibit-point-motion-hooks t) 
10495            (hidden (text-property-any 
10496                     (goto-char (point-min)) (search-forward "\n\n")
10497                     'invisible t))
10498            e)
10499       (goto-char (point-min))
10500       (when (search-forward "\n\n" nil t)
10501         (delete-region (point-min) (1- (point))))
10502       (goto-char (point-min))
10503       (save-excursion 
10504         (set-buffer gnus-original-article-buffer)
10505         (goto-char (point-min))
10506         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
10507       (insert-buffer-substring gnus-original-article-buffer 1 e)
10508       (let ((gnus-inhibit-hiding t))
10509         (run-hooks 'gnus-article-display-hook))
10510       (if (or (not hidden) (and (numberp arg) (< arg 0)))
10511           (gnus-article-hide-headers)))))
10512
10513 (defun gnus-summary-show-all-headers ()
10514   "Make all header lines visible."
10515   (interactive)
10516   (gnus-set-global-variables)
10517   (gnus-article-show-all-headers))
10518
10519 (defun gnus-summary-toggle-mime (&optional arg)
10520   "Toggle MIME processing.
10521 If ARG is a positive number, turn MIME processing on."
10522   (interactive "P")
10523   (gnus-set-global-variables)
10524   (setq gnus-show-mime
10525         (if (null arg) (not gnus-show-mime)
10526           (> (prefix-numeric-value arg) 0)))
10527   (gnus-summary-select-article t 'force))
10528
10529 (defun gnus-summary-caesar-message (&optional arg)
10530   "Caesar rotate the current article by 13.
10531 The numerical prefix specifies how manu places to rotate each letter
10532 forward."
10533   (interactive "P")
10534   (gnus-set-global-variables)
10535   (gnus-summary-select-article)
10536   (let ((mail-header-separator ""))
10537     (gnus-eval-in-buffer-window 
10538      gnus-article-buffer
10539      (save-restriction
10540        (widen)
10541        (let ((start (window-start)))
10542          (news-caesar-buffer-body arg)
10543          (set-window-start (get-buffer-window (current-buffer)) start))))))
10544
10545 (defun gnus-summary-stop-page-breaking ()
10546   "Stop page breaking in the current article."
10547   (interactive)
10548   (gnus-set-global-variables)
10549   (gnus-summary-select-article)
10550   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
10551
10552 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
10553
10554 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
10555   "Move the current article to a different newsgroup.
10556 If N is a positive number, move the N next articles.
10557 If N is a negative number, move the N previous articles.
10558 If N is nil and any articles have been marked with the process mark,
10559 move those articles instead.
10560 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
10561 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
10562 re-spool using this method.
10563 For this function to work, both the current newsgroup and the
10564 newsgroup that you want to move to have to support the `request-move'
10565 and `request-accept' functions. (Ie. mail newsgroups at present.)"
10566   (interactive "P")
10567   (gnus-set-global-variables)
10568   (unless (gnus-check-backend-function
10569            'request-move-article gnus-newsgroup-name)
10570     (error "The current newsgroup does not support article moving"))
10571   (let ((articles (gnus-summary-work-articles n))
10572         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
10573         art-group to-method sel-met)
10574     (when (and (not to-newsgroup)
10575                (not select-method))
10576       (setq to-newsgroup 
10577             (gnus-read-move-group-name 
10578              "move" gnus-current-move-group articles prefix))
10579       (setq gnus-current-move-group to-newsgroup))
10580     (setq to-method (if select-method (list select-method "")
10581                       (gnus-find-method-for-group to-newsgroup)))
10582     (or (gnus-check-backend-function 'request-accept-article (car to-method))
10583         (error "%s does not support article copying" (car to-method)))
10584     (or (gnus-check-server to-method)
10585         (error "Can't open server %s" (car to-method)))
10586     (gnus-message 6 "Moving to %s: %s..." 
10587                   (or select-method to-newsgroup) articles)
10588     (while articles
10589       (if (setq art-group
10590                 (gnus-request-move-article 
10591                  (car articles)         ; Article to move
10592                  gnus-newsgroup-name    ; From newsgrouo
10593                  (nth 1 (gnus-find-method-for-group 
10594                          gnus-newsgroup-name)) ; Server
10595                  (list 'gnus-request-accept-article 
10596                        (if select-method
10597                            (list 'quote select-method)
10598                          to-newsgroup)
10599                        (not (cdr articles))) ; Accept form
10600                  (not (cdr articles)))) ; Only save nov last time
10601           (let* ((buffer-read-only nil)
10602                  (entry 
10603                   (or
10604                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
10605                    (gnus-gethash 
10606                     (gnus-group-prefixed-name 
10607                      (car art-group) 
10608                      (if select-method (list select-method "")
10609                        (gnus-find-method-for-group to-newsgroup)))
10610                     gnus-newsrc-hashtb)))
10611                  (info (nth 2 entry))
10612                  (article (car articles)))
10613             ;; Update the group that has been moved to.
10614             (if (not info)
10615                 ()                      ; This group does not exist yet.
10616               (unless (memq article gnus-newsgroup-unreads)
10617                 (gnus-info-set-read 
10618                  info (gnus-add-to-range (gnus-info-read info) 
10619                                          (list (cdr art-group)))))
10620
10621               ;; Copy any marks over to the new group.
10622               (let ((marks '((tick . gnus-newsgroup-marked)
10623                              (dormant . gnus-newsgroup-dormant)
10624                              (expire . gnus-newsgroup-expirable)
10625                              (bookmark . gnus-newsgroup-bookmarks)
10626                              (save . gnus-newsgroup-saved)
10627                              (reply . gnus-newsgroup-replied)))
10628                     (to-article (cdr art-group)))
10629
10630                 ;; See whether the article is to be put in the cache.
10631                 (when gnus-use-cache
10632                   (gnus-cache-possibly-enter-article 
10633                    (gnus-info-group info) to-article
10634                    (let ((header (copy-sequence
10635                                   (gnus-summary-article-header article))))
10636                      (mail-header-set-number header to-article)
10637                      header)
10638                    (memq article gnus-newsgroup-marked)
10639                    (memq article gnus-newsgroup-dormant)
10640                    (memq article gnus-newsgroup-unreads)))
10641
10642                 (while marks
10643                   (if (memq article (symbol-value (cdr (car marks))))
10644                       (gnus-add-marked-articles 
10645                        (gnus-info-group info) (car (car marks))
10646                        (list to-article) info))
10647                   (setq marks (cdr marks)))))
10648             (gnus-summary-goto-subject article)
10649             (gnus-summary-mark-article article gnus-canceled-mark))
10650         (gnus-message 1 "Couldn't move article %s" (car articles)))
10651       (gnus-summary-remove-process-mark (car articles))
10652       (setq articles (cdr articles)))
10653     (gnus-set-mode-line 'summary)))
10654
10655 (defun gnus-summary-respool-article (&optional n respool-method)
10656   "Respool the current article.
10657 The article will be squeezed through the mail spooling process again,
10658 which means that it will be put in some mail newsgroup or other
10659 depending on `nnmail-split-methods'.
10660 If N is a positive number, respool the N next articles.
10661 If N is a negative number, respool the N previous articles.
10662 If N is nil and any articles have been marked with the process mark,
10663 respool those articles instead.
10664
10665 Respooling can be done both from mail groups and \"real\" newsgroups.
10666 In the former case, the articles in question will be moved from the
10667 current group into whatever groups they are destined to.  In the
10668 latter case, they will be copied into the relevant groups."
10669   (interactive "P")
10670   (gnus-set-global-variables)
10671   (let ((respool-methods (gnus-methods-using 'respool))
10672         (methname 
10673          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
10674     (or respool-method
10675         (setq respool-method
10676               (completing-read
10677                "What method do you want to use when respooling? "
10678                respool-methods nil t methname)))
10679     (or (string= respool-method "")
10680         (if (assoc (symbol-name
10681                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
10682                    respool-methods)
10683             (gnus-summary-move-article n nil (intern respool-method))
10684           (gnus-summary-copy-article n nil (intern respool-method))))))
10685
10686 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
10687 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10688   "Move the current article to a different newsgroup.
10689 If N is a positive number, move the N next articles.
10690 If N is a negative number, move the N previous articles.
10691 If N is nil and any articles have been marked with the process mark,
10692 move those articles instead.
10693 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
10694 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
10695 re-spool using this method.
10696 For this function to work, the newsgroup that you want to move to have
10697 to support the `request-move' and `request-accept'
10698 functions. (Ie. mail newsgroups at present.)"
10699   (interactive "P")
10700   (gnus-set-global-variables)
10701   (let ((articles (gnus-summary-work-articles n))
10702         (copy-buf (get-buffer-create "*copy work*"))
10703         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
10704         art-group to-method)
10705     (buffer-disable-undo copy-buf)
10706     (when (and (not to-newsgroup)
10707                (not select-method))
10708       (setq to-newsgroup 
10709             (gnus-read-move-group-name
10710              "copy" gnus-current-move-group articles prefix))
10711       (setq gnus-current-move-group to-newsgroup))
10712     (setq to-method (if select-method (list select-method "")
10713                       (gnus-find-method-for-group to-newsgroup)))
10714     (or (gnus-check-backend-function 'request-accept-article (car to-method))
10715         (error "%s does not support article copying" (car to-method)))
10716     (or (gnus-check-server to-method)
10717         (error "Can't open server %s" (car to-method)))
10718     (while articles
10719       (gnus-message 6 "Copying to %s: %s..." 
10720                     (or select-method to-newsgroup) articles)
10721       (if (setq art-group
10722                 (save-excursion
10723                   (set-buffer copy-buf)
10724                   (gnus-request-article-this-buffer
10725                    (car articles) gnus-newsgroup-name)
10726                   (gnus-request-accept-article
10727                    (if select-method select-method to-newsgroup)
10728                    (not (cdr articles)))))
10729           (let* ((entry 
10730                   (or
10731                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
10732                    (gnus-gethash 
10733                     (gnus-group-prefixed-name 
10734                      (car art-group) 
10735                      (if select-method (list select-method "")
10736                        (gnus-find-method-for-group to-newsgroup)))
10737                     gnus-newsrc-hashtb)))
10738                  (info (nth 2 entry))
10739                  (article (car articles)))
10740             ;; We copy the info over to the new group.
10741             (if (not info)
10742                 ()                      ; This group does not exist (yet).
10743               (if (not (memq article gnus-newsgroup-unreads))
10744                   (gnus-info-set-read 
10745                    info (gnus-add-to-range (gnus-info-read info) 
10746                                            (list (cdr art-group)))))
10747
10748               ;; Copy any marks over to the new group.
10749               (let ((marks '((tick . gnus-newsgroup-marked)
10750                              (dormant . gnus-newsgroup-dormant)
10751                              (expire . gnus-newsgroup-expirable)
10752                              (bookmark . gnus-newsgroup-bookmarks)
10753                              (save . gnus-newsgroup-saved)
10754                              (reply . gnus-newsgroup-replied)))
10755                     (to-article (cdr art-group)))
10756
10757               ;; See whether the article is to be put in the cache.
10758               (when gnus-use-cache
10759                 (gnus-cache-possibly-enter-article 
10760                  (gnus-info-group info) to-article 
10761                  (let ((header (copy-sequence
10762                                 (gnus-summary-article-header article))))
10763                    (mail-header-set-number header to-article)
10764                    header)
10765                  (memq article gnus-newsgroup-marked)
10766                  (memq article gnus-newsgroup-dormant)
10767                  (memq article gnus-newsgroup-unreads)))
10768
10769               (while marks
10770                 (if (memq article (symbol-value (cdr (car marks))))
10771                     (gnus-add-marked-articles 
10772                      (gnus-info-group info) (car (car marks)) 
10773                      (list to-article) info))
10774                 (setq marks (cdr marks))))))
10775         (gnus-message 1 "Couldn't copy article %s" (car articles)))
10776       (gnus-summary-remove-process-mark (car articles))
10777       (setq articles (cdr articles)))
10778     (kill-buffer copy-buf)))
10779
10780 (defun gnus-summary-import-article (file)
10781   "Import a random file into a mail newsgroup."
10782   (interactive "fImport file: ")
10783   (gnus-set-global-variables)
10784   (let ((group gnus-newsgroup-name)
10785         atts lines)
10786     (or (gnus-check-backend-function 'request-accept-article group)
10787         (error "%s does not support article importing" group))
10788     (or (file-readable-p file)
10789         (not (file-regular-p file))
10790         (error "Can't read %s" file))
10791     (save-excursion
10792       (set-buffer (get-buffer-create " *import file*"))
10793       (buffer-disable-undo (current-buffer))
10794       (erase-buffer)
10795       (insert-file-contents file)
10796       (goto-char (point-min))
10797       (if (nnheader-article-p)
10798           ()
10799         (setq atts (file-attributes file)
10800               lines (count-lines (point-min) (point-max)))
10801         (insert "From: " (read-string "From: ") "\n"
10802                 "Subject: " (read-string "Subject: ") "\n"
10803                 "Date: " (current-time-string (nth 5 atts)) "\n"
10804                 "Message-ID: " (gnus-inews-message-id) "\n"
10805                 "Lines: " (int-to-string lines) "\n"
10806                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10807       (gnus-request-accept-article group t)
10808       (kill-buffer (current-buffer)))))
10809
10810 (defun gnus-summary-expire-articles ()
10811   "Expire all articles that are marked as expirable in the current group."
10812   (interactive)
10813   (gnus-set-global-variables)
10814   (when (gnus-check-backend-function 
10815          'request-expire-articles gnus-newsgroup-name)
10816     ;; This backend supports expiry.
10817     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10818            (expirable (if total
10819                           (gnus-list-of-read-articles gnus-newsgroup-name)
10820                         (setq gnus-newsgroup-expirable
10821                               (sort gnus-newsgroup-expirable '<))))
10822            (expiry-wait (gnus-group-get-parameter 
10823                          gnus-newsgroup-name 'expiry-wait))
10824            es)
10825       (when expirable
10826         ;; There are expirable articles in this group, so we run them
10827         ;; through the expiry process.
10828         (gnus-message 6 "Expiring articles...")
10829         ;; The list of articles that weren't expired is returned.
10830         (if expiry-wait
10831             (let ((nnmail-expiry-wait-function nil)
10832                   (nnmail-expiry-wait expiry-wait))
10833               (setq es (gnus-request-expire-articles
10834                         expirable gnus-newsgroup-name)))
10835           (setq es (gnus-request-expire-articles
10836                     expirable gnus-newsgroup-name)))
10837         (or total (setq gnus-newsgroup-expirable es))
10838         ;; We go through the old list of expirable, and mark all
10839         ;; really expired articles as non-existant.
10840         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
10841           (let ((gnus-use-cache nil))
10842             (while expirable
10843               (unless (memq (car expirable) es)
10844                 (when (gnus-data-find (car expirable))
10845                   (gnus-summary-mark-article
10846                    (car expirable) gnus-canceled-mark)))
10847               (setq expirable (cdr expirable)))))
10848         (gnus-message 6 "Expiring articles...done")))))
10849
10850 (defun gnus-summary-expire-articles-now ()
10851   "Expunge all expirable articles in the current group.
10852 This means that *all* articles that are marked as expirable will be
10853 deleted forever, right now."
10854   (interactive)
10855   (gnus-set-global-variables)
10856   (or gnus-expert-user
10857       (gnus-y-or-n-p
10858        "Are you really, really, really sure you want to expunge? ")
10859       (error "Phew!"))
10860   (let ((nnmail-expiry-wait 'immediate)
10861         (nnmail-expiry-wait-function nil))
10862     (gnus-summary-expire-articles)))
10863
10864 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10865 (defun gnus-summary-delete-article (&optional n)
10866   "Delete the N next (mail) articles.
10867 This command actually deletes articles.  This is not a marking
10868 command.  The article will disappear forever from your life, never to
10869 return. 
10870 If N is negative, delete backwards.
10871 If N is nil and articles have been marked with the process mark,
10872 delete these instead."
10873   (interactive "P")
10874   (gnus-set-global-variables)
10875   (or (gnus-check-backend-function 'request-expire-articles 
10876                                    gnus-newsgroup-name)
10877       (error "The current newsgroup does not support article deletion."))
10878   ;; Compute the list of articles to delete.
10879   (let ((articles (gnus-summary-work-articles n))
10880         not-deleted)
10881     (if (and gnus-novice-user
10882              (not (gnus-y-or-n-p 
10883                    (format "Do you really want to delete %s forever? "
10884                            (if (> (length articles) 1) "these articles"
10885                              "this article")))))
10886         ()
10887       ;; Delete the articles.
10888       (setq not-deleted (gnus-request-expire-articles 
10889                          articles gnus-newsgroup-name 'force))
10890       (while articles
10891         (gnus-summary-remove-process-mark (car articles))       
10892         ;; The backend might not have been able to delete the article
10893         ;; after all.  
10894         (or (memq (car articles) not-deleted)
10895             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10896         (setq articles (cdr articles))))
10897     (gnus-summary-position-point)
10898     (gnus-set-mode-line 'summary)
10899     not-deleted))
10900
10901 (defun gnus-summary-edit-article (&optional force)
10902   "Enter into a buffer and edit the current article.
10903 This will have permanent effect only in mail groups.
10904 If FORCE is non-nil, allow editing of articles even in read-only
10905 groups."
10906   (interactive "P")
10907   (gnus-set-global-variables)
10908   (when (and (not force)
10909              (gnus-group-read-only-p))
10910     (error "The current newsgroup does not support article editing."))
10911   (gnus-summary-select-article t nil t)
10912   (gnus-configure-windows 'article)
10913   (select-window (get-buffer-window gnus-article-buffer))
10914   (gnus-message 6 "C-c C-c to end edits")
10915   (setq buffer-read-only nil)
10916   (text-mode)
10917   (use-local-map (copy-keymap (current-local-map)))
10918   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
10919   (buffer-enable-undo)
10920   (widen)
10921   (goto-char (point-min))
10922   (search-forward "\n\n" nil t))
10923
10924 (defun gnus-summary-edit-article-done ()
10925   "Make edits to the current article permanent."
10926   (interactive)
10927   (if (gnus-group-read-only-p)
10928       (progn
10929         (gnus-summary-edit-article-postpone)
10930         (gnus-message 
10931          1 "The current newsgroup does not support article editing.")
10932         (ding))
10933     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
10934       (erase-buffer)
10935       (insert buf)
10936       (if (not (gnus-request-replace-article 
10937                 (cdr gnus-article-current) (car gnus-article-current) 
10938                 (current-buffer)))
10939           (error "Couldn't replace article.")
10940         (gnus-article-mode)
10941         (use-local-map gnus-article-mode-map)
10942         (setq buffer-read-only t)
10943         (buffer-disable-undo (current-buffer))
10944         (gnus-configure-windows 'summary))
10945       (and (gnus-visual-p 'summary-highlight 'highlight)
10946            (run-hooks 'gnus-visual-mark-article-hook)))))
10947
10948 (defun gnus-summary-edit-article-postpone ()
10949   "Postpone changes to the current article."
10950   (interactive)
10951   (gnus-article-mode)
10952   (use-local-map gnus-article-mode-map)
10953   (setq buffer-read-only t)
10954   (buffer-disable-undo (current-buffer))
10955   (gnus-configure-windows 'summary)
10956   (and (gnus-visual-p 'summary-highlight 'highlight)
10957        (run-hooks 'gnus-visual-mark-article-hook)))
10958
10959 (defun gnus-summary-respool-query ()
10960   "Query where the respool algorithm would put this article."
10961   (interactive)
10962   (gnus-set-global-variables)
10963   (gnus-summary-select-article)
10964   (save-excursion
10965     (set-buffer gnus-article-buffer)
10966     (save-restriction
10967       (goto-char (point-min))
10968       (search-forward "\n\n")
10969       (narrow-to-region (point-min) (point))
10970       (pp-eval-expression
10971        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
10972
10973 ;; Summary score commands.
10974
10975 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
10976
10977 (defun gnus-summary-raise-score (n)
10978   "Raise the score of the current article by N."
10979   (interactive "p")
10980   (gnus-set-global-variables)
10981   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
10982
10983 (defun gnus-summary-set-score (n)
10984   "Set the score of the current article to N."
10985   (interactive "p")
10986   (gnus-set-global-variables)
10987   (save-excursion
10988     (gnus-summary-show-thread)
10989     (let ((buffer-read-only nil))
10990       ;; Set score.
10991       (gnus-summary-update-mark
10992        (if (= n (or gnus-summary-default-score 0)) ? 
10993          (if (< n (or gnus-summary-default-score 0)) 
10994              gnus-score-below-mark gnus-score-over-mark)) 'score))
10995     (let* ((article (gnus-summary-article-number))
10996            (score (assq article gnus-newsgroup-scored)))
10997       (if score (setcdr score n)
10998         (setq gnus-newsgroup-scored 
10999               (cons (cons article n) gnus-newsgroup-scored))))
11000     (gnus-summary-update-line)))
11001
11002 (defun gnus-summary-current-score ()
11003   "Return the score of the current article."
11004   (interactive)
11005   (gnus-set-global-variables)
11006   (message "%s" (gnus-summary-article-score)))
11007
11008 ;; Summary marking commands.
11009
11010 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11011   "Mark articles which has the same subject as read, and then select the next.
11012 If UNMARK is positive, remove any kind of mark.
11013 If UNMARK is negative, tick articles."
11014   (interactive "P")
11015   (gnus-set-global-variables)
11016   (if unmark
11017       (setq unmark (prefix-numeric-value unmark)))
11018   (let ((count
11019          (gnus-summary-mark-same-subject
11020           (gnus-summary-article-subject) unmark)))
11021     ;; Select next unread article.  If auto-select-same mode, should
11022     ;; select the first unread article.
11023     (gnus-summary-next-article t (and gnus-auto-select-same
11024                                       (gnus-summary-article-subject)))
11025     (gnus-message 7 "%d article%s marked as %s"
11026                   count (if (= count 1) " is" "s are")
11027                   (if unmark "unread" "read"))))
11028
11029 (defun gnus-summary-kill-same-subject (&optional unmark)
11030   "Mark articles which has the same subject as read. 
11031 If UNMARK is positive, remove any kind of mark.
11032 If UNMARK is negative, tick articles."
11033   (interactive "P")
11034   (gnus-set-global-variables)
11035   (if unmark
11036       (setq unmark (prefix-numeric-value unmark)))
11037   (let ((count
11038          (gnus-summary-mark-same-subject
11039           (gnus-summary-article-subject) unmark)))
11040     ;; If marked as read, go to next unread subject.
11041     (if (null unmark)
11042         ;; Go to next unread subject.
11043         (gnus-summary-next-subject 1 t))
11044     (gnus-message 7 "%d articles are marked as %s"
11045                   count (if unmark "unread" "read"))))
11046
11047 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11048   "Mark articles with same SUBJECT as read, and return marked number.
11049 If optional argument UNMARK is positive, remove any kinds of marks.
11050 If optional argument UNMARK is negative, mark articles as unread instead."
11051   (let ((count 1))
11052     (save-excursion
11053       (cond 
11054        ((null unmark)                   ; Mark as read.
11055         (while (and 
11056                 (progn
11057                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11058                   (gnus-summary-show-thread) t)
11059                 (gnus-summary-find-subject subject))
11060           (setq count (1+ count))))
11061        ((> unmark 0)                    ; Tick.
11062         (while (and
11063                 (progn
11064                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11065                   (gnus-summary-show-thread) t)
11066                 (gnus-summary-find-subject subject))
11067           (setq count (1+ count))))
11068        (t                               ; Mark as unread.
11069         (while (and
11070                 (progn
11071                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11072                   (gnus-summary-show-thread) t)
11073                 (gnus-summary-find-subject subject))
11074           (setq count (1+ count)))))
11075       (gnus-set-mode-line 'summary)
11076       ;; Return the number of marked articles.
11077       count)))
11078
11079 (defun gnus-summary-mark-as-processable (n &optional unmark)
11080   "Set the process mark on the next N articles.
11081 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11082 the process mark instead.  The difference between N and the actual
11083 number of articles marked is returned."
11084   (interactive "p")
11085   (gnus-set-global-variables)
11086   (let ((backward (< n 0))
11087         (n (abs n)))
11088     (while (and 
11089             (> n 0)
11090             (if unmark
11091                 (gnus-summary-remove-process-mark
11092                  (gnus-summary-article-number))
11093               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11094             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11095       (setq n (1- n)))
11096     (if (/= 0 n) (gnus-message 7 "No more articles"))
11097     (gnus-summary-recenter)
11098     (gnus-summary-position-point)
11099     n))
11100
11101 (defun gnus-summary-unmark-as-processable (n)
11102   "Remove the process mark from the next N articles.
11103 If N is negative, mark backward instead.  The difference between N and
11104 the actual number of articles marked is returned."
11105   (interactive "p")
11106   (gnus-set-global-variables)
11107   (gnus-summary-mark-as-processable n t))
11108
11109 (defun gnus-summary-unmark-all-processable ()
11110   "Remove the process mark from all articles."
11111   (interactive)
11112   (gnus-set-global-variables)
11113   (save-excursion
11114     (while gnus-newsgroup-processable
11115       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11116   (gnus-summary-position-point))
11117
11118 (defun gnus-summary-mark-as-expirable (n)
11119   "Mark N articles forward as expirable.
11120 If N is negative, mark backward instead.  The difference between N and
11121 the actual number of articles marked is returned."
11122   (interactive "p")
11123   (gnus-set-global-variables)
11124   (gnus-summary-mark-forward n gnus-expirable-mark))
11125
11126 (defun gnus-summary-mark-article-as-replied (article)
11127   "Mark ARTICLE replied and update the summary line."
11128   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11129   (let ((buffer-read-only nil))
11130     (when (gnus-summary-goto-subject article)
11131       (gnus-summary-update-secondary-mark article))))
11132
11133 (defun gnus-summary-set-bookmark (article)
11134   "Set a bookmark in current article."
11135   (interactive (list (gnus-summary-article-number)))
11136   (gnus-set-global-variables)
11137   (if (or (not (get-buffer gnus-article-buffer))
11138           (not gnus-current-article)
11139           (not gnus-article-current)
11140           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11141       (error "No current article selected"))
11142   ;; Remove old bookmark, if one exists.
11143   (let ((old (assq article gnus-newsgroup-bookmarks)))
11144     (if old (setq gnus-newsgroup-bookmarks 
11145                   (delq old gnus-newsgroup-bookmarks))))
11146   ;; Set the new bookmark, which is on the form 
11147   ;; (article-number . line-number-in-body).
11148   (setq gnus-newsgroup-bookmarks 
11149         (cons 
11150          (cons article 
11151                (save-excursion
11152                  (set-buffer gnus-article-buffer)
11153                  (count-lines
11154                   (min (point)
11155                        (save-excursion
11156                          (goto-char (point-min))
11157                          (search-forward "\n\n" nil t)
11158                          (point)))
11159                   (point))))
11160          gnus-newsgroup-bookmarks))
11161   (gnus-message 6 "A bookmark has been added to the current article."))
11162
11163 (defun gnus-summary-remove-bookmark (article)
11164   "Remove the bookmark from the current article."
11165   (interactive (list (gnus-summary-article-number)))
11166   (gnus-set-global-variables)
11167   ;; Remove old bookmark, if one exists.
11168   (let ((old (assq article gnus-newsgroup-bookmarks)))
11169     (if old 
11170         (progn
11171           (setq gnus-newsgroup-bookmarks 
11172                 (delq old gnus-newsgroup-bookmarks))
11173           (gnus-message 6 "Removed bookmark."))
11174       (gnus-message 6 "No bookmark in current article."))))
11175
11176 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11177 (defun gnus-summary-mark-as-dormant (n)
11178   "Mark N articles forward as dormant.
11179 If N is negative, mark backward instead.  The difference between N and
11180 the actual number of articles marked is returned."
11181   (interactive "p")
11182   (gnus-set-global-variables)
11183   (gnus-summary-mark-forward n gnus-dormant-mark))
11184
11185 (defun gnus-summary-set-process-mark (article)
11186   "Set the process mark on ARTICLE and update the summary line."
11187   (setq gnus-newsgroup-processable 
11188         (cons article 
11189               (delq article gnus-newsgroup-processable)))
11190   (when (gnus-summary-goto-subject article)
11191     (gnus-summary-show-thread)
11192     (gnus-summary-update-secondary-mark article)))
11193
11194 (defun gnus-summary-remove-process-mark (article)
11195   "Remove the process mark from ARTICLE and update the summary line."
11196   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11197   (when (gnus-summary-goto-subject article)
11198     (gnus-summary-show-thread)
11199     (gnus-summary-update-secondary-mark article)))
11200
11201 (defun gnus-summary-set-saved-mark (article)
11202   "Set the process mark on ARTICLE and update the summary line."
11203   (push article gnus-newsgroup-saved)
11204   (when (gnus-summary-goto-subject article)
11205     (gnus-summary-update-secondary-mark article)))
11206
11207 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11208   "Mark N articles as read forwards.
11209 If N is negative, mark backwards instead.
11210 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11211 marked as unread. 
11212 The difference between N and the actual number of articles marked is
11213 returned."
11214   (interactive "p")
11215   (gnus-set-global-variables)
11216   (let ((backward (< n 0))
11217         (gnus-summary-goto-unread
11218          (and gnus-summary-goto-unread
11219               (not (memq mark (list gnus-unread-mark
11220                                     gnus-ticked-mark gnus-dormant-mark)))))
11221         (n (abs n))
11222         (mark (or mark gnus-del-mark)))
11223     (while (and (> n 0)
11224                 (gnus-summary-mark-article nil mark no-expire)
11225                 (zerop (gnus-summary-next-subject 
11226                         (if backward -1 1) gnus-summary-goto-unread t)))
11227       (setq n (1- n)))
11228     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11229     (gnus-summary-recenter)
11230     (gnus-summary-position-point)
11231     (gnus-set-mode-line 'summary)
11232     n))
11233
11234 (defun gnus-summary-mark-article-as-read (mark)
11235   "Mark the current article quickly as read with MARK."
11236   (let ((article (gnus-summary-article-number)))
11237     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11238     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11239     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11240     (setq gnus-newsgroup-reads
11241           (cons (cons article mark) gnus-newsgroup-reads))
11242     ;; Possibly remove from cache, if that is used. 
11243     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11244     (and gnus-newsgroup-auto-expire 
11245          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11246              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11247              (= mark gnus-read-mark) (= mark gnus-souped-mark))
11248          (progn
11249            (setq mark gnus-expirable-mark)
11250            (setq gnus-newsgroup-expirable 
11251                  (cons article gnus-newsgroup-expirable))))
11252     ;; Fix the mark.
11253     (gnus-summary-update-mark mark 'unread)
11254     t))
11255
11256 (defun gnus-summary-mark-article-as-unread (mark)
11257   "Mark the current article quickly as unread with MARK."
11258   (let ((article (gnus-summary-article-number)))
11259     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11260     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11261     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11262     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11263     (cond ((= mark gnus-ticked-mark)
11264            (push article gnus-newsgroup-marked))
11265           ((= mark gnus-dormant-mark)
11266            (push article gnus-newsgroup-dormant))
11267           (t     
11268            (push article gnus-newsgroup-unreads)))
11269     (setq gnus-newsgroup-reads
11270           (delq (assq article gnus-newsgroup-reads)
11271                 gnus-newsgroup-reads))
11272
11273     ;; See whether the article is to be put in the cache.
11274     (and gnus-use-cache
11275          (vectorp (gnus-summary-article-header article))
11276          (save-excursion
11277            (gnus-cache-possibly-enter-article 
11278             gnus-newsgroup-name article 
11279             (gnus-summary-article-header article)
11280             (= mark gnus-ticked-mark)
11281             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11282
11283     ;; Fix the mark.
11284     (gnus-summary-update-mark mark 'unread)
11285     t))
11286
11287 (defun gnus-summary-mark-article (&optional article mark no-expire)
11288   "Mark ARTICLE with MARK.  MARK can be any character.
11289 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), 
11290 `??' (dormant) and `?E' (expirable).
11291 If MARK is nil, then the default character `?D' is used.
11292 If ARTICLE is nil, then the article on the current line will be
11293 marked." 
11294   ;; The mark might be a string.
11295   (and (stringp mark)
11296        (setq mark (aref mark 0)))
11297   ;; If no mark is given, then we check auto-expiring.
11298   (and (not no-expire)
11299        gnus-newsgroup-auto-expire 
11300        (or (not mark)
11301            (and (numberp mark) 
11302                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11303                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11304                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11305        (setq mark gnus-expirable-mark))
11306   (let* ((mark (or mark gnus-del-mark))
11307          (article (or article (gnus-summary-article-number))))
11308     (or article (error "No article on current line"))
11309     (if (or (= mark gnus-unread-mark) 
11310             (= mark gnus-ticked-mark) 
11311             (= mark gnus-dormant-mark))
11312         (gnus-mark-article-as-unread article mark)
11313       (gnus-mark-article-as-read article mark))
11314
11315     ;; See whether the article is to be put in the cache.
11316     (and gnus-use-cache
11317          (not (= mark gnus-canceled-mark))
11318          (vectorp (gnus-summary-article-header article))
11319          (save-excursion
11320            (gnus-cache-possibly-enter-article 
11321             gnus-newsgroup-name article 
11322             (gnus-summary-article-header article)
11323             (= mark gnus-ticked-mark)
11324             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11325
11326     (if (gnus-summary-goto-subject article nil t)
11327         (let ((buffer-read-only nil))
11328           (gnus-summary-show-thread)
11329           ;; Fix the mark.
11330           (gnus-summary-update-mark mark 'unread)
11331           t))))
11332
11333 (defun gnus-summary-update-secondary-mark (article)
11334   "Update the secondary (read, process, cache) mark."
11335   (gnus-summary-update-mark
11336    (cond ((memq article gnus-newsgroup-processable)
11337           gnus-process-mark)
11338          ((memq article gnus-newsgroup-cached)
11339           gnus-cached-mark)
11340          ((memq article gnus-newsgroup-replied)
11341           gnus-replied-mark)
11342          ((memq article gnus-newsgroup-saved)
11343           gnus-saved-mark)
11344          (t gnus-unread-mark))
11345    'replied)
11346   (when (gnus-visual-p 'summary-highlight 'highlight)
11347     (run-hooks 'gnus-summary-update-hook))
11348   t)
11349
11350 (defun gnus-summary-update-mark (mark type)
11351   (beginning-of-line)
11352   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11353         (buffer-read-only nil))
11354     (when forward
11355       ;; Go to the right position on the line.
11356       (forward-char forward)
11357       ;; Replace the old mark with the new mark.
11358       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11359       ;; Optionally update the marks by some user rule.
11360       (when (eq type 'unread)
11361         (gnus-data-set-mark 
11362          (gnus-data-find (gnus-summary-article-number)) mark)
11363         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11364   
11365 (defun gnus-mark-article-as-read (article &optional mark)
11366   "Enter ARTICLE in the pertinent lists and remove it from others."
11367   ;; Make the article expirable.
11368   (let ((mark (or mark gnus-del-mark)))
11369     (if (= mark gnus-expirable-mark)
11370         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11371       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11372     ;; Remove from unread and marked lists.
11373     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11374     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11375     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11376     (push (cons article mark) gnus-newsgroup-reads)
11377     ;; Possibly remove from cache, if that is used. 
11378     (when gnus-use-cache 
11379       (gnus-cache-enter-remove-article article))))
11380
11381 (defun gnus-mark-article-as-unread (article &optional mark)
11382   "Enter ARTICLE in the pertinent lists and remove it from others."
11383   (let ((mark (or mark gnus-ticked-mark)))
11384     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11385     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11386     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11387     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11388     (cond ((= mark gnus-ticked-mark)
11389            (push article gnus-newsgroup-marked))
11390           ((= mark gnus-dormant-mark)
11391            (push article gnus-newsgroup-dormant))
11392           (t     
11393            (push article gnus-newsgroup-unreads)))
11394     (setq gnus-newsgroup-reads
11395           (delq (assq article gnus-newsgroup-reads)
11396                 gnus-newsgroup-reads))))
11397
11398 (defalias 'gnus-summary-mark-as-unread-forward 
11399   'gnus-summary-tick-article-forward)
11400 (make-obsolete 'gnus-summary-mark-as-unread-forward 
11401                'gnus-summary-tick-article-forward)
11402 (defun gnus-summary-tick-article-forward (n)
11403   "Tick N articles forwards.
11404 If N is negative, tick backwards instead.
11405 The difference between N and the number of articles ticked is returned."
11406   (interactive "p")
11407   (gnus-summary-mark-forward n gnus-ticked-mark))
11408
11409 (defalias 'gnus-summary-mark-as-unread-backward 
11410   'gnus-summary-tick-article-backward)
11411 (make-obsolete 'gnus-summary-mark-as-unread-backward 
11412                'gnus-summary-tick-article-backward)
11413 (defun gnus-summary-tick-article-backward (n)
11414   "Tick N articles backwards.
11415 The difference between N and the number of articles ticked is returned."
11416   (interactive "p")
11417   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11418
11419 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11420 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11421 (defun gnus-summary-tick-article (&optional article clear-mark)
11422   "Mark current article as unread.
11423 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11424 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11425   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11426                                        gnus-ticked-mark)))
11427
11428 (defun gnus-summary-mark-as-read-forward (n)
11429   "Mark N articles as read forwards.
11430 If N is negative, mark backwards instead.
11431 The difference between N and the actual number of articles marked is
11432 returned."
11433   (interactive "p")
11434   (gnus-summary-mark-forward n gnus-del-mark t))
11435
11436 (defun gnus-summary-mark-as-read-backward (n)
11437   "Mark the N articles as read backwards.
11438 The difference between N and the actual number of articles marked is
11439 returned."
11440   (interactive "p")
11441   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11442
11443 (defun gnus-summary-mark-as-read (&optional article mark)
11444   "Mark current article as read.
11445 ARTICLE specifies the article to be marked as read.
11446 MARK specifies a string to be inserted at the beginning of the line."
11447   (gnus-summary-mark-article article mark))
11448
11449 (defun gnus-summary-clear-mark-forward (n)
11450   "Clear marks from N articles forward.
11451 If N is negative, clear backward instead.
11452 The difference between N and the number of marks cleared is returned."
11453   (interactive "p")
11454   (gnus-summary-mark-forward n gnus-unread-mark))
11455
11456 (defun gnus-summary-clear-mark-backward (n)
11457   "Clear marks from N articles backward.
11458 The difference between N and the number of marks cleared is returned."
11459   (interactive "p")
11460   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11461
11462 (defun gnus-summary-mark-unread-as-read ()
11463   "Intended to be used by `gnus-summary-mark-article-hook'."
11464   (when (memq gnus-current-article gnus-newsgroup-unreads)
11465     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11466
11467 (defun gnus-summary-mark-region-as-read (point mark all)
11468   "Mark all unread articles between point and mark as read.
11469 If given a prefix, mark all articles between point and mark as read,
11470 even ticked and dormant ones."
11471   (interactive "r\nP")
11472   (save-excursion
11473     (let (article)
11474       (goto-char point)
11475       (beginning-of-line)
11476       (while (and 
11477               (< (point) mark)
11478               (progn
11479                 (when (or all 
11480                           (memq (setq article (gnus-summary-article-number))
11481                                 gnus-newsgroup-unreads))
11482                   (gnus-summary-mark-article article gnus-del-mark))
11483                 t)
11484               (gnus-summary-find-next))))))
11485
11486 (defun gnus-summary-mark-below (score mark)
11487   "Mark articles with score less than SCORE with MARK."
11488   (interactive "P\ncMark: ")
11489   (gnus-set-global-variables)
11490   (setq score (if score
11491                   (prefix-numeric-value score)
11492                 (or gnus-summary-default-score 0)))
11493   (save-excursion
11494     (set-buffer gnus-summary-buffer)
11495     (goto-char (point-min))
11496     (while (not (eobp))
11497       (and (< (gnus-summary-article-score) score)
11498            (gnus-summary-mark-article nil mark))
11499       (gnus-summary-find-next))))
11500
11501 (defun gnus-summary-kill-below (&optional score)
11502   "Mark articles with score below SCORE as read."
11503   (interactive "P")
11504   (gnus-set-global-variables)
11505   (gnus-summary-mark-below score gnus-killed-mark))
11506
11507 (defun gnus-summary-clear-above (&optional score)
11508   "Clear all marks from articles with score above SCORE."
11509   (interactive "P")
11510   (gnus-set-global-variables)
11511   (gnus-summary-mark-above score gnus-unread-mark))
11512
11513 (defun gnus-summary-tick-above (&optional score)
11514   "Tick all articles with score above SCORE."
11515   (interactive "P")
11516   (gnus-set-global-variables)
11517   (gnus-summary-mark-above score gnus-ticked-mark))
11518
11519 (defun gnus-summary-mark-above (score mark)
11520   "Mark articles with score over SCORE with MARK."
11521   (interactive "P\ncMark: ")
11522   (gnus-set-global-variables)
11523   (setq score (if score
11524                   (prefix-numeric-value score)
11525                 (or gnus-summary-default-score 0)))
11526   (save-excursion
11527     (set-buffer gnus-summary-buffer)
11528     (goto-char (point-min))
11529     (while (and (progn
11530                   (if (> (gnus-summary-article-score) score)
11531                       (gnus-summary-mark-article nil mark))
11532                   t)
11533                 (gnus-summary-find-next)))))
11534
11535 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
11536 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11537 (defun gnus-summary-limit-include-expunged ()
11538   "Display all the hidden articles that were expunged for low scores."
11539   (interactive)
11540   (gnus-set-global-variables)
11541   (let ((buffer-read-only nil))
11542     (let ((scored gnus-newsgroup-scored)
11543           headers h)
11544       (while scored
11545         (or (gnus-summary-goto-subject (car (car scored)))
11546             (and (setq h (gnus-summary-article-header (car (car scored))))
11547                  (< (cdr (car scored)) gnus-summary-expunge-below)
11548                  (setq headers (cons h headers))))
11549         (setq scored (cdr scored)))
11550       (or headers (error "No expunged articles hidden."))
11551       (goto-char (point-min))
11552       (gnus-summary-prepare-unthreaded (nreverse headers)))
11553     (goto-char (point-min))
11554     (gnus-summary-position-point)))
11555
11556 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
11557   "Mark all articles not marked as unread in this newsgroup as read.
11558 If prefix argument ALL is non-nil, all articles are marked as read.
11559 If QUIETLY is non-nil, no questions will be asked.
11560 If TO-HERE is non-nil, it should be a point in the buffer.  All
11561 articles before this point will be marked as read.
11562 The number of articles marked as read is returned."
11563   (interactive "P")
11564   (gnus-set-global-variables)
11565   (prog1
11566       (if (or quietly
11567               (not gnus-interactive-catchup) ;Without confirmation?
11568               gnus-expert-user
11569               (gnus-y-or-n-p
11570                (if all
11571                    "Mark absolutely all articles as read? "
11572                  "Mark all unread articles as read? ")))
11573           (if (and not-mark 
11574                    (not gnus-newsgroup-adaptive)
11575                    (not gnus-newsgroup-auto-expire))
11576               (progn
11577                 (when all
11578                   (setq gnus-newsgroup-marked nil
11579                         gnus-newsgroup-dormant nil))
11580                 (setq gnus-newsgroup-unreads nil))
11581             ;; We actually mark all articles as canceled, which we
11582             ;; have to do when using auto-expiry or adaptive scoring. 
11583             (gnus-summary-show-all-threads)
11584             (if (gnus-summary-first-subject (not all))
11585                 (while (and 
11586                         (if to-here (< (point) to-here) t)
11587                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11588                         (gnus-summary-find-next (not all)))))
11589             (unless to-here
11590               (setq gnus-newsgroup-unreads nil))
11591             (gnus-set-mode-line 'summary)))
11592     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
11593       (if (and (not to-here) (eq 'nnvirtual (car method)))
11594           (nnvirtual-catchup-group
11595            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
11596     (gnus-summary-position-point)))
11597
11598 (defun gnus-summary-catchup-to-here (&optional all)
11599   "Mark all unticked articles before the current one as read.
11600 If ALL is non-nil, also mark ticked and dormant articles as read."
11601   (interactive "P")
11602   (gnus-set-global-variables)
11603   (save-excursion
11604     (let ((beg (point)))
11605       ;; We check that there are unread articles.
11606       (when (or all (gnus-summary-find-prev))
11607         (gnus-summary-catchup all t beg))))
11608   (gnus-summary-position-point))
11609
11610 (defun gnus-summary-catchup-all (&optional quietly)
11611   "Mark all articles in this newsgroup as read."
11612   (interactive "P")
11613   (gnus-set-global-variables)
11614   (gnus-summary-catchup t quietly))
11615
11616 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11617   "Mark all articles not marked as unread in this newsgroup as read, then exit.
11618 If prefix argument ALL is non-nil, all articles are marked as read."
11619   (interactive "P")
11620   (gnus-set-global-variables)
11621   (gnus-summary-catchup all quietly nil 'fast)
11622   ;; Select next newsgroup or exit.
11623   (if (eq gnus-auto-select-next 'quietly)
11624       (gnus-summary-next-group nil)
11625     (gnus-summary-exit)))
11626
11627 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11628   "Mark all articles in this newsgroup as read, and then exit."
11629   (interactive "P")
11630   (gnus-set-global-variables)
11631   (gnus-summary-catchup-and-exit t quietly))
11632
11633 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
11634 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11635   "Mark all articles in this group as read and select the next group.
11636 If given a prefix, mark all articles, unread as well as ticked, as
11637 read." 
11638   (interactive "P")
11639   (gnus-set-global-variables)
11640   (save-excursion
11641     (gnus-summary-catchup all))
11642   (gnus-summary-next-article t))
11643
11644 ;; Thread-based commands.
11645
11646 (defun gnus-summary-articles-in-thread (&optional article)
11647   "Return a list of all articles in the current thread.
11648 If ARTICLE is non-nil, return all articles in the thread that starts
11649 with that article."
11650   (let* ((article (or article (gnus-summary-article-number)))
11651          (data (gnus-data-find-list article))
11652          (top-level (gnus-data-level (car data)))
11653          (top-subject 
11654           (cond ((null gnus-thread-operation-ignore-subject)
11655                  (gnus-simplify-subject-re
11656                   (mail-header-subject (gnus-data-header (car data)))))
11657                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11658                  (gnus-simplify-subject-fuzzy
11659                   (mail-header-subject (gnus-data-header (car data)))))
11660                 (t nil)))
11661          articles)
11662     (if (not data)
11663         ()                              ; This article doesn't exist.
11664       (while data
11665         (and (or (not top-subject)
11666                  (string= top-subject
11667                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11668                               (gnus-simplify-subject-fuzzy
11669                                (mail-header-subject 
11670                                 (gnus-data-header (car data))))
11671                             (gnus-simplify-subject-re
11672                              (mail-header-subject 
11673                               (gnus-data-header (car data)))))))
11674              (setq articles (cons (gnus-data-number (car data)) articles)))
11675         (if (and (setq data (cdr data))
11676                  (> (gnus-data-level (car data)) top-level))
11677             ()
11678           (setq data nil)))
11679       ;; Return the list of articles.
11680       (nreverse articles))))
11681
11682 (defun gnus-summary-rethread-current ()
11683   "Rethread the thread the current article is part of."
11684   (interactive)
11685   (gnus-set-global-variables)
11686   (let* ((gnus-show-threads t)
11687          (article (gnus-summary-article-number))
11688          (id (mail-header-id (gnus-summary-article-header)))
11689          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11690     (unless id
11691       (error "No article on the current line"))
11692     (gnus-rebuild-thread id)
11693     (gnus-summary-goto-subject article)))
11694
11695 (defun gnus-summary-toggle-threads (&optional arg)
11696   "Toggle showing conversation threads.
11697 If ARG is positive number, turn showing conversation threads on."
11698   (interactive "P")
11699   (gnus-set-global-variables)
11700   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11701     (setq gnus-show-threads
11702           (if (null arg) (not gnus-show-threads)
11703             (> (prefix-numeric-value arg) 0)))
11704     (gnus-summary-prepare)
11705     (gnus-summary-goto-subject current)
11706     (gnus-summary-position-point)))
11707
11708 (defun gnus-summary-show-all-threads ()
11709   "Show all threads."
11710   (interactive)
11711   (gnus-set-global-variables)
11712   (save-excursion
11713     (let ((buffer-read-only nil))
11714       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11715   (gnus-summary-position-point))
11716
11717 (defun gnus-summary-show-thread ()
11718   "Show thread subtrees.
11719 Returns nil if no thread was there to be shown."
11720   (interactive)
11721   (gnus-set-global-variables)
11722   (let ((buffer-read-only nil)
11723         (orig (point))
11724         ;; first goto end then to beg, to have point at beg after let
11725         (end (progn (end-of-line) (point)))
11726         (beg (progn (beginning-of-line) (point))))
11727     (prog1
11728         ;; Any hidden lines here?
11729         (search-forward "\r" end t)
11730       (subst-char-in-region beg end ?\^M ?\n t)
11731       (goto-char orig)
11732       (gnus-summary-position-point))))
11733
11734 (defun gnus-summary-hide-all-threads ()
11735   "Hide all thread subtrees."
11736   (interactive)
11737   (gnus-set-global-variables)
11738   (save-excursion
11739     (goto-char (point-min))
11740     (gnus-summary-hide-thread)
11741     (while (zerop (gnus-summary-next-thread 1 t))
11742       (gnus-summary-hide-thread)))
11743   (gnus-summary-position-point))
11744
11745 (defun gnus-summary-hide-thread ()
11746   "Hide thread subtrees.
11747 Returns nil if no threads were there to be hidden."
11748   (interactive)
11749   (gnus-set-global-variables)
11750   (let ((buffer-read-only nil)
11751         (start (point))
11752         (article (gnus-summary-article-number))
11753         end)
11754     ;; Go forward until either the buffer ends or the subthread
11755     ;; ends. 
11756     (when (and (not (eobp))
11757                (or (and (zerop (gnus-summary-next-thread 1 t))
11758                         (gnus-summary-find-prev))
11759                    (goto-char (gnus-data-pos (car (gnus-data-list 'rev))))))
11760       (setq end (point))
11761       (prog1
11762           (if (and (> (point) start)
11763                    (search-backward "\n" start t))
11764               (progn
11765                 (subst-char-in-region start end ?\n ?\^M)
11766                 (gnus-summary-goto-subject article))
11767             (goto-char start)
11768             nil)
11769         (gnus-summary-position-point)))))
11770
11771 (defun gnus-summary-go-to-next-thread (&optional previous)
11772   "Go to the same level (or less) next thread.
11773 If PREVIOUS is non-nil, go to previous thread instead.
11774 Return the article number moved to, or nil if moving was impossible."
11775   (let* ((level (gnus-summary-thread-level))
11776          (article (gnus-summary-article-number))
11777          (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
11778          oart)
11779     (while data
11780       (if (<= (gnus-data-level (car data)) level)
11781           (setq oart (gnus-data-number (car data))
11782                 data nil)
11783         (setq data (cdr data))))
11784     (and oart 
11785          (gnus-summary-goto-subject oart))))
11786
11787 (defun gnus-summary-next-thread (n &optional silent)
11788   "Go to the same level next N'th thread.
11789 If N is negative, search backward instead.
11790 Returns the difference between N and the number of skips actually
11791 done.
11792
11793 If SILENT, don't output messages."
11794   (interactive "p")
11795   (gnus-set-global-variables)
11796   (let ((backward (< n 0))
11797         (n (abs n)))
11798     (while (and (> n 0)
11799                 (gnus-summary-go-to-next-thread backward))
11800       (decf n))
11801     (gnus-summary-position-point)
11802     (when (and (not silent) (/= 0 n))
11803       (gnus-message 7 "No more threads"))
11804     n))
11805
11806 (defun gnus-summary-prev-thread (n)
11807   "Go to the same level previous N'th thread.
11808 Returns the difference between N and the number of skips actually
11809 done."
11810   (interactive "p")
11811   (gnus-set-global-variables)
11812   (gnus-summary-next-thread (- n)))
11813
11814 (defun gnus-summary-go-down-thread ()
11815   "Go down one level in the current thread."
11816   (let ((children (gnus-summary-article-children)))
11817     (and children
11818          (gnus-summary-goto-subject (car children)))))
11819
11820 (defun gnus-summary-go-up-thread ()
11821   "Go up one level in the current thread."
11822   (let ((parent (gnus-summary-article-parent)))
11823     (and parent
11824          (gnus-summary-goto-subject parent))))
11825
11826 (defun gnus-summary-down-thread (n)
11827   "Go down thread N steps.
11828 If N is negative, go up instead.
11829 Returns the difference between N and how many steps down that were
11830 taken."
11831   (interactive "p")
11832   (gnus-set-global-variables)
11833   (let ((up (< n 0))
11834         (n (abs n)))
11835     (while (and (> n 0)
11836                 (if up (gnus-summary-go-up-thread)
11837                   (gnus-summary-go-down-thread)))
11838       (setq n (1- n)))
11839     (gnus-summary-position-point)
11840     (if (/= 0 n) (gnus-message 7 "Can't go further"))
11841     n))
11842
11843 (defun gnus-summary-up-thread (n)
11844   "Go up thread N steps.
11845 If N is negative, go up instead.
11846 Returns the difference between N and how many steps down that were
11847 taken."
11848   (interactive "p")
11849   (gnus-set-global-variables)
11850   (gnus-summary-down-thread (- n)))
11851
11852 (defun gnus-summary-kill-thread (&optional unmark)
11853   "Mark articles under current thread as read.
11854 If the prefix argument is positive, remove any kinds of marks.
11855 If the prefix argument is negative, tick articles instead."
11856   (interactive "P")
11857   (gnus-set-global-variables)
11858   (if unmark
11859       (setq unmark (prefix-numeric-value unmark)))
11860   (let ((articles (gnus-summary-articles-in-thread)))
11861     (save-excursion
11862       ;; Expand the thread.
11863       (gnus-summary-show-thread)
11864       ;; Mark all the articles.
11865       (while articles
11866         (gnus-summary-goto-subject (car articles))
11867         (cond ((null unmark) 
11868                (gnus-summary-mark-article-as-read gnus-killed-mark))
11869               ((> unmark 0) 
11870                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11871               (t 
11872                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11873         (setq articles (cdr articles))))
11874     ;; Hide killed subtrees.
11875     (and (null unmark)
11876          gnus-thread-hide-killed
11877          (gnus-summary-hide-thread))
11878     ;; If marked as read, go to next unread subject.
11879     (if (null unmark)
11880         ;; Go to next unread subject.
11881         (gnus-summary-next-subject 1 t)))
11882   (gnus-set-mode-line 'summary))
11883
11884 ;; Summary sorting commands
11885
11886 (defun gnus-summary-sort-by-number (&optional reverse)
11887   "Sort summary buffer by article number.
11888 Argument REVERSE means reverse order."
11889   (interactive "P")
11890   (gnus-summary-sort 'number reverse))
11891
11892 (defun gnus-summary-sort-by-author (&optional reverse)
11893   "Sort summary buffer by author name alphabetically.
11894 If case-fold-search is non-nil, case of letters is ignored.
11895 Argument REVERSE means reverse order."
11896   (interactive "P")
11897   (gnus-summary-sort 'author reverse))
11898
11899 (defun gnus-summary-sort-by-subject (&optional reverse)
11900   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
11901 If case-fold-search is non-nil, case of letters is ignored.
11902 Argument REVERSE means reverse order."
11903   (interactive "P")
11904   (gnus-summary-sort 'subject reverse))
11905
11906 (defun gnus-summary-sort-by-date (&optional reverse)
11907   "Sort summary buffer by date.
11908 Argument REVERSE means reverse order."
11909   (interactive "P")
11910   (gnus-summary-sort 'date reverse))
11911
11912 (defun gnus-summary-sort-by-score (&optional reverse)
11913   "Sort summary buffer by score.
11914 Argument REVERSE means reverse order."
11915   (interactive "P")
11916   (gnus-summary-sort 'score reverse))
11917
11918 (defun gnus-summary-sort (predicate reverse)
11919   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11920   (gnus-set-global-variables)
11921   (let* ((gnus-thread-sort-functions 
11922           (list (intern (format "gnus-thread-sort-by-%s" predicate))))
11923          (gnus-article-sort-functions
11924           (list (intern (format "gnus-article-sort-by-%s" predicate))))
11925          (buffer-read-only)
11926          (gnus-summary-prepare-hook nil))
11927     ;; We do the sorting by regenerating the threads.
11928     (gnus-summary-prepare)
11929     ;; Hide subthreads if needed.
11930     (when (and gnus-show-threads gnus-thread-hide-subtree)
11931       (gnus-summary-hide-all-threads)))
11932   ;; If in async mode, we send some info to the backend.
11933   (when gnus-newsgroup-async
11934     (gnus-request-asynchronous 
11935      gnus-newsgroup-name gnus-newsgroup-data)))
11936   
11937 (defun gnus-sortable-date (date)
11938   "Make sortable string by string-lessp from DATE.
11939 Timezone package is used."
11940   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
11941          (year (aref date 0))
11942          (month (aref date 1))
11943          (day (aref date 2)))
11944     (timezone-make-sortable-date 
11945      year month day 
11946      (timezone-make-time-string
11947       (aref date 3) (aref date 4) (aref date 5)))))
11948
11949
11950 ;; Summary saving commands.
11951
11952 (defun gnus-summary-save-article (&optional n not-saved)
11953   "Save the current article using the default saver function.
11954 If N is a positive number, save the N next articles.
11955 If N is a negative number, save the N previous articles.
11956 If N is nil and any articles have been marked with the process mark,
11957 save those articles instead.
11958 The variable `gnus-default-article-saver' specifies the saver function."
11959   (interactive "P")
11960   (gnus-set-global-variables)
11961   (let ((articles (gnus-summary-work-articles n))
11962         file header article)
11963     (while articles
11964       (setq header (gnus-summary-article-header
11965                     (setq article (pop articles))))
11966       (if (not (vectorp header))
11967           ;; This is a pseudo-article.
11968           (if (assq 'name header)
11969               (gnus-copy-file (cdr (assq 'name header)))
11970             (gnus-message 1 "Article %d is unsaveable" article))
11971         ;; This is a real article.
11972         (save-window-excursion
11973           (gnus-summary-select-article t nil nil article))
11974         (unless gnus-save-all-headers
11975           ;; Remove headers accoring to `gnus-saved-headers'.
11976           (let ((gnus-visible-headers 
11977                  (or gnus-saved-headers gnus-visible-headers)))
11978             (gnus-article-hide-headers nil t)))
11979         ;; Remove any X-Gnus lines.
11980         (save-excursion
11981           (set-buffer gnus-article-buffer)
11982           (save-restriction
11983             (let ((buffer-read-only nil))
11984               (gnus-narrow-to-headers)
11985               (while (re-search-forward "^X-Gnus" nil t)
11986                 (gnus-delete-line)))))
11987         (save-window-excursion
11988           (if (not gnus-default-article-saver)
11989               (error "No default saver is defined.")
11990             (setq file (funcall
11991                         gnus-default-article-saver
11992                         (cond
11993                          ((not gnus-prompt-before-saving)
11994                           'default)
11995                          ((eq gnus-prompt-before-saving 'always)
11996                           nil)
11997                          (t file))))))
11998         (gnus-summary-remove-process-mark article)
11999         (unless not-saved
12000           (gnus-summary-set-saved-mark article))))
12001     (gnus-summary-position-point)
12002     n))
12003
12004 (defun gnus-summary-pipe-output (&optional arg)
12005   "Pipe the current article to a subprocess.
12006 If N is a positive number, pipe the N next articles.
12007 If N is a negative number, pipe the N previous articles.
12008 If N is nil and any articles have been marked with the process mark,
12009 pipe those articles instead."
12010   (interactive "P")
12011   (gnus-set-global-variables)
12012   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12013     (gnus-summary-save-article arg t))
12014   (gnus-configure-windows 'pipe))
12015
12016 (defun gnus-summary-save-article-mail (&optional arg)
12017   "Append the current article to an mail file.
12018 If N is a positive number, save the N next articles.
12019 If N is a negative number, save the N previous articles.
12020 If N is nil and any articles have been marked with the process mark,
12021 save those articles instead."
12022   (interactive "P")
12023   (gnus-set-global-variables)
12024   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12025     (gnus-summary-save-article arg)))
12026
12027 (defun gnus-summary-save-article-rmail (&optional arg)
12028   "Append the current article to an rmail file.
12029 If N is a positive number, save the N next articles.
12030 If N is a negative number, save the N previous articles.
12031 If N is nil and any articles have been marked with the process mark,
12032 save those articles instead."
12033   (interactive "P")
12034   (gnus-set-global-variables)
12035   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12036     (gnus-summary-save-article arg)))
12037
12038 (defun gnus-summary-save-article-file (&optional arg)
12039   "Append the current article to a file.
12040 If N is a positive number, save the N next articles.
12041 If N is a negative number, save the N previous articles.
12042 If N is nil and any articles have been marked with the process mark,
12043 save those articles instead."
12044   (interactive "P")
12045   (gnus-set-global-variables)
12046   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12047     (gnus-summary-save-article arg)))
12048
12049 (defun gnus-summary-save-article-body-file (&optional arg)
12050   "Append the current article body to a file.
12051 If N is a positive number, save the N next articles.
12052 If N is a negative number, save the N previous articles.
12053 If N is nil and any articles have been marked with the process mark,
12054 save those articles instead."
12055   (interactive "P")
12056   (gnus-set-global-variables)
12057   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12058     (gnus-summary-save-article arg)))
12059
12060 (defun gnus-get-split-value (methods)
12061   "Return a value based on the split METHODS."
12062   (let (split-name method result match)
12063     (when methods
12064       (save-excursion
12065         (set-buffer gnus-original-article-buffer)
12066         (save-restriction
12067           (gnus-narrow-to-headers)
12068           (while methods
12069             (goto-char (point-min))
12070             (setq method (pop methods))
12071             (setq match (pop method))
12072             (when (cond
12073                    ((stringp match)
12074                     ;; Regular expression.
12075                     (condition-case () 
12076                         (re-search-forward match nil t)
12077                       (error nil)))
12078                    ((gnus-functionp match)
12079                     (save-restriction
12080                       (widen)
12081                       (setq result (funcall match gnus-newsgroup-name))))
12082                    ((consp match)
12083                     (save-restriction
12084                       (widen)
12085                       (setq result (eval match)))))
12086               (setq split-name (append (cdr methods) split-name))
12087               (cond ((stringp result)
12088                      (push result split-name))
12089                     ((consp result)
12090                      (setq split-name (append result split-name)))))))))
12091     split-name))
12092
12093 (defun gnus-read-move-group-name (prompt default articles prefix)
12094   "Read a group name."
12095   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12096          (prom
12097           (format "Where do you want to %s %s? "
12098                   prompt
12099                   (if (> (length articles) 1)
12100                       (format "these %d articles" (length articles))
12101                     "this article")))
12102          (to-newsgroup
12103           (cond
12104            ((null split-name)
12105             (completing-read 
12106              (concat prom 
12107                      (if default
12108                          (format "(default %s) " default)
12109                        ""))
12110              gnus-active-hashtb nil nil prefix))
12111            ((= 1 (length split-name))
12112             (completing-read prom gnus-active-hashtb 
12113                              nil nil (cons (car split-name) 0)))
12114            (t
12115             (completing-read 
12116              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12117        
12118     (when to-newsgroup
12119       (if (or (string= to-newsgroup "") 
12120               (string= to-newsgroup prefix))
12121           (setq to-newsgroup (or default "")))
12122       (or (gnus-active to-newsgroup)
12123           (gnus-activate-group to-newsgroup)
12124           (error "No such group: %s" to-newsgroup)))
12125     to-newsgroup))
12126
12127 (defun gnus-read-save-file-name (prompt default-name)
12128   (let ((split-name (gnus-get-split-value gnus-split-methods)))
12129     ;; Let the split methods have their say.
12130     (cond
12131      ;; No split name was found.
12132      ((null split-name)
12133       (read-file-name
12134        (concat prompt " (default " (file-name-nondirectory default-name) ") ")
12135        (file-name-directory default-name)
12136        default-name))
12137      ;; A single split name was found
12138      ((= 1 (length split-name))
12139       (read-file-name
12140        (concat prompt " (default " (car split-name) ") ")
12141        gnus-article-save-directory
12142        (concat gnus-article-save-directory (car split-name))))
12143      ;; A list of splits was found.
12144      (t
12145       (setq split-name (mapcar (lambda (el) (list el)) (nreverse split-name)))
12146       (let ((result (completing-read (concat prompt " ") split-name nil nil)))
12147         (concat gnus-article-save-directory
12148                 (if (string= result "")
12149                     (car (car split-name))
12150                   result)))))))
12151
12152 (defun gnus-article-archive-name (group)
12153   "Return the first instance of an \"Archive-name\" in the current buffer."
12154   (let ((case-fold-search t))
12155     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12156       (match-string 1))))
12157
12158 (defun gnus-summary-save-in-rmail (&optional filename)
12159   "Append this article to Rmail file.
12160 Optional argument FILENAME specifies file name.
12161 Directory to save to is default to `gnus-article-save-directory' which
12162 is initialized from the SAVEDIR environment variable."
12163   (interactive)
12164   (gnus-set-global-variables)
12165   (let ((default-name
12166           (funcall gnus-rmail-save-name gnus-newsgroup-name
12167                    gnus-current-headers gnus-newsgroup-last-rmail)))
12168     (setq filename
12169           (cond ((eq filename 'default)
12170                  default-name)
12171                 (filename filename)
12172                 (t (gnus-read-save-file-name 
12173                     "Save in rmail file:" default-name))))
12174     (gnus-make-directory (file-name-directory filename))
12175     (gnus-eval-in-buffer-window 
12176      gnus-original-article-buffer
12177      (save-excursion
12178        (save-restriction
12179          (widen)
12180          (gnus-output-to-rmail filename))))
12181     ;; Remember the directory name to save articles
12182     (setq gnus-newsgroup-last-rmail filename)))
12183
12184 (defun gnus-summary-save-in-mail (&optional filename)
12185   "Append this article to Unix mail file.
12186 Optional argument FILENAME specifies file name.
12187 Directory to save to is default to `gnus-article-save-directory' which
12188 is initialized from the SAVEDIR environment variable."
12189   (interactive)
12190   (gnus-set-global-variables)
12191   (let ((default-name
12192           (funcall gnus-mail-save-name gnus-newsgroup-name
12193                    gnus-current-headers gnus-newsgroup-last-mail)))
12194     (setq filename
12195           (cond ((eq filename 'default)
12196                  default-name)
12197                 (filename filename)
12198                 (t (gnus-read-save-file-name 
12199                     "Save in Unix mail file:" default-name))))
12200     (setq filename
12201           (expand-file-name filename
12202                             (and default-name
12203                                  (file-name-directory default-name))))
12204     (gnus-make-directory (file-name-directory filename))
12205     (gnus-eval-in-buffer-window 
12206      gnus-original-article-buffer
12207      (save-excursion
12208        (save-restriction
12209          (widen)
12210          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12211              (gnus-output-to-rmail filename)
12212            (let ((mail-use-rfc822 t))
12213              (rmail-output filename 1 t t))))))
12214     ;; Remember the directory name to save articles.
12215     (setq gnus-newsgroup-last-mail filename)))
12216
12217 (defun gnus-summary-save-in-file (&optional filename)
12218   "Append this article to file.
12219 Optional argument FILENAME specifies file name.
12220 Directory to save to is default to `gnus-article-save-directory' which
12221 is initialized from the SAVEDIR environment variable."
12222   (interactive)
12223   (gnus-set-global-variables)
12224   (let ((default-name
12225           (funcall gnus-file-save-name gnus-newsgroup-name
12226                    gnus-current-headers gnus-newsgroup-last-file)))
12227     (setq filename
12228           (cond ((eq filename 'default)
12229                  default-name)
12230                 (filename filename)
12231                 (t (gnus-read-save-file-name 
12232                     "Save in file:" default-name))))
12233     (gnus-make-directory (file-name-directory filename))
12234     (gnus-eval-in-buffer-window 
12235      gnus-article-buffer
12236      (save-excursion
12237        (save-restriction
12238          (widen)
12239          (gnus-output-to-file filename))))
12240     ;; Remember the directory name to save articles.
12241     (setq gnus-newsgroup-last-file filename)))
12242
12243 (defun gnus-summary-save-body-in-file (&optional filename)
12244   "Append this article body to a file.
12245 Optional argument FILENAME specifies file name.
12246 The directory to save in defaults to `gnus-article-save-directory' which
12247 is initialized from the SAVEDIR environment variable."
12248   (interactive)
12249   (gnus-set-global-variables)
12250   (let ((default-name
12251           (funcall gnus-file-save-name gnus-newsgroup-name
12252                    gnus-current-headers gnus-newsgroup-last-file)))
12253     (setq filename
12254           (cond ((eq filename 'default)
12255                  default-name)
12256                 (filename filename)
12257                 (t (gnus-read-save-file-name 
12258                     "Save body in file:" default-name))))
12259     (gnus-make-directory (file-name-directory filename))
12260     (gnus-eval-in-buffer-window 
12261      gnus-article-buffer
12262      (save-excursion
12263        (save-restriction
12264          (widen)
12265          (goto-char (point-min))
12266          (and (search-forward "\n\n" nil t)
12267               (narrow-to-region (point) (point-max)))
12268          (gnus-output-to-file filename))))
12269     ;; Remember the directory name to save articles.
12270     (setq gnus-newsgroup-last-file filename)))
12271
12272 (defun gnus-summary-save-in-pipe (&optional command)
12273   "Pipe this article to subprocess."
12274   (interactive)
12275   (gnus-set-global-variables)
12276   (setq command
12277         (cond ((eq command 'default)
12278                gnus-last-shell-command)
12279               (command command)
12280               (t (read-string "Shell command on article: "
12281                               gnus-last-shell-command))))
12282   (if (string-equal command "")
12283       (setq command gnus-last-shell-command))
12284   (gnus-eval-in-buffer-window 
12285    gnus-article-buffer
12286    (save-restriction
12287      (widen)
12288      (shell-command-on-region (point-min) (point-max) command nil)))
12289   (setq gnus-last-shell-command command))
12290
12291 ;; Summary extract commands
12292
12293 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12294   (let ((buffer-read-only nil)
12295         (article (gnus-summary-article-number))
12296         after-article b e)
12297     (or (gnus-summary-goto-subject article)
12298         (error (format "No such article: %d" article)))
12299     (gnus-summary-position-point)
12300     ;; If all commands are to be bunched up on one line, we collect
12301     ;; them here.  
12302     (if gnus-view-pseudos-separately
12303         ()
12304       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12305             files action)
12306         (while ps
12307           (setq action (cdr (assq 'action (car ps))))
12308           (setq files (list (cdr (assq 'name (car ps)))))
12309           (while (and ps (cdr ps)
12310                       (string= (or action "1")
12311                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
12312             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
12313             (setcdr ps (cdr (cdr ps))))
12314           (if (not files)
12315               ()
12316             (if (not (string-match "%s" action))
12317                 (setq files (cons " " files)))
12318             (setq files (cons " " files))
12319             (and (assq 'execute (car ps))
12320                  (setcdr (assq 'execute (car ps))
12321                          (funcall (if (string-match "%s" action)
12322                                       'format 'concat)
12323                                   action 
12324                                   (mapconcat (lambda (f) f) files " ")))))
12325           (setq ps (cdr ps)))))
12326     (if (and gnus-view-pseudos (not not-view))
12327         (while pslist
12328           (and (assq 'execute (car pslist))
12329                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12330                                      (eq gnus-view-pseudos 'not-confirm)))
12331           (setq pslist (cdr pslist)))
12332       (save-excursion
12333         (while pslist
12334           (setq after-article (or (cdr (assq 'article (car pslist)))
12335                                   (gnus-summary-article-number)))
12336           (gnus-summary-goto-subject after-article)
12337           (forward-line 1)
12338           (setq b (point))
12339           (insert "          " (file-name-nondirectory
12340                                 (cdr (assq 'name (car pslist))))
12341                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12342           (setq e (point))
12343           (forward-line -1)             ; back to `b'
12344           (put-text-property b e 'gnus-number gnus-reffed-article-number)
12345           (gnus-data-enter after-article
12346                            gnus-reffed-article-number
12347                            gnus-unread-mark 
12348                            b
12349                            (car pslist) 
12350                            0 
12351                            (- e b))
12352           (setq gnus-newsgroup-unreads
12353                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
12354           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12355           (setq pslist (cdr pslist)))))))
12356
12357 (defun gnus-pseudos< (p1 p2)
12358   (let ((c1 (cdr (assq 'action p1)))
12359         (c2 (cdr (assq 'action p2))))
12360     (and c1 c2 (string< c1 c2))))
12361
12362 (defun gnus-request-pseudo-article (props)
12363   (cond ((assq 'execute props)
12364          (gnus-execute-command (cdr (assq 'execute props)))))
12365   (let ((gnus-current-article (gnus-summary-article-number)))
12366     (run-hooks 'gnus-mark-article-hook)))
12367
12368 (defun gnus-execute-command (command &optional automatic)
12369   (save-excursion
12370     (gnus-article-setup-buffer)
12371     (set-buffer gnus-article-buffer)
12372     (let ((command (if automatic command (read-string "Command: " command)))
12373           (buffer-read-only nil))
12374       (erase-buffer)
12375       (insert "$ " command "\n\n")
12376       (if gnus-view-pseudo-asynchronously
12377           (start-process "gnus-execute" nil "sh" "-c" command)
12378         (call-process "sh" nil t nil "-c" command)))))
12379
12380 (defun gnus-copy-file (file &optional to)
12381   "Copy FILE to TO."
12382   (interactive
12383    (list (read-file-name "Copy file: " default-directory)
12384          (read-file-name "Copy file to: " default-directory)))
12385   (gnus-set-global-variables)
12386   (or to (setq to (read-file-name "Copy file to: " default-directory)))
12387   (and (file-directory-p to) 
12388        (setq to (concat (file-name-as-directory to)
12389                         (file-name-nondirectory file))))
12390   (copy-file file to))
12391
12392 ;; Summary kill commands.
12393
12394 (defun gnus-summary-edit-global-kill (article)
12395   "Edit the \"global\" kill file."
12396   (interactive (list (gnus-summary-article-number)))
12397   (gnus-set-global-variables)
12398   (gnus-group-edit-global-kill article))
12399
12400 (defun gnus-summary-edit-local-kill ()
12401   "Edit a local kill file applied to the current newsgroup."
12402   (interactive)
12403   (gnus-set-global-variables)
12404   (setq gnus-current-headers (gnus-summary-article-header))
12405   (gnus-set-global-variables)
12406   (gnus-group-edit-local-kill 
12407    (gnus-summary-article-number) gnus-newsgroup-name))
12408
12409 \f
12410 ;;;
12411 ;;; Gnus article mode
12412 ;;;
12413
12414 (put 'gnus-article-mode 'mode-class 'special)
12415
12416 (if gnus-article-mode-map
12417     nil
12418   (setq gnus-article-mode-map (make-keymap))
12419   (suppress-keymap gnus-article-mode-map)
12420   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
12421   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
12422   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
12423   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
12424   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
12425   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
12426   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
12427   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
12428   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
12429   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
12430   (define-key gnus-article-mode-map "\M-\t" 'gnus-article-prev-button)
12431   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
12432   
12433   ;; Duplicate almost all summary keystrokes in the article mode map.
12434   (let ((commands 
12435          (list 
12436           "p" "N" "P" "\M-\C-n" "\M-\C-p"
12437           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
12438           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
12439           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
12440           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
12441           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
12442           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
12443           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
12444           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
12445           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
12446           "\C-c\C-v\C-v" "\C-d" "v" 
12447 ;;        "Mt" "M!" "Md" "Mr"
12448 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
12449 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
12450 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
12451 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
12452 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
12453 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
12454 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
12455 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
12456 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
12457 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
12458 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
12459           )))
12460     (while commands
12461       (define-key gnus-article-mode-map (car commands) 
12462         'gnus-article-summary-command)
12463       (setq commands (cdr commands))))
12464
12465   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
12466 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
12467                          "=" "n"  "^" "\M-^")))
12468     (while commands
12469       (define-key gnus-article-mode-map (car commands) 
12470         'gnus-article-summary-command-nosave)
12471       (setq commands (cdr commands)))))
12472
12473
12474 (defun gnus-article-mode ()
12475   "Major mode for displaying an article.
12476
12477 All normal editing commands are switched off.
12478
12479 The following commands are available:
12480
12481 \\<gnus-article-mode-map>
12482 \\[gnus-article-next-page]\t Scroll the article one page forwards
12483 \\[gnus-article-prev-page]\t Scroll the article one page backwards
12484 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
12485 \\[gnus-article-show-summary]\t Display the summary buffer
12486 \\[gnus-article-mail]\t Send a reply to the address near point
12487 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
12488 \\[gnus-info-find-node]\t Go to the Gnus info node"
12489   (interactive)
12490   (when (and menu-bar-mode
12491              (gnus-visual-p 'article-menu 'menu))
12492     (gnus-article-make-menu-bar))
12493   (kill-all-local-variables)
12494   (gnus-simplify-mode-line)
12495   (setq mode-name "Article")
12496   (setq major-mode 'gnus-article-mode)
12497   (make-local-variable 'minor-mode-alist)
12498   (or (assq 'gnus-show-mime minor-mode-alist)
12499       (setq minor-mode-alist
12500             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
12501   (use-local-map gnus-article-mode-map)
12502   (make-local-variable 'page-delimiter)
12503   (setq page-delimiter gnus-page-delimiter)
12504   (buffer-disable-undo (current-buffer))
12505   (setq buffer-read-only t)             ;Disable modification
12506   (run-hooks 'gnus-article-mode-hook))
12507
12508 (defun gnus-article-setup-buffer ()
12509   "Initialize the article buffer."
12510   ;; Returns the article buffer.
12511   (let ((name (if gnus-single-article-buffer "*Article*"
12512                 (concat "*Article " gnus-newsgroup-name "*"))))
12513     (setq gnus-article-buffer name)
12514     ;; This might be a variable local to the summary buffer.
12515     (unless gnus-single-article-buffer
12516       (save-excursion
12517         (set-buffer gnus-summary-buffer)
12518         (setq gnus-article-buffer name))
12519       (make-local-variable 'gnus-summary-buffer))
12520     (if (get-buffer name)
12521         (save-excursion
12522           (set-buffer name)
12523           (buffer-disable-undo (current-buffer))
12524           (setq buffer-read-only t)
12525           (gnus-add-current-to-buffer-list)
12526           (or (eq major-mode 'gnus-article-mode)
12527               (gnus-article-mode))
12528           (current-buffer))
12529       (save-excursion
12530         (set-buffer (get-buffer-create name))
12531         (gnus-add-current-to-buffer-list)
12532         (gnus-article-mode)
12533         (current-buffer)))))
12534
12535 ;; Set article window start at LINE, where LINE is the number of lines
12536 ;; from the head of the article.
12537 (defun gnus-article-set-window-start (&optional line)
12538   (set-window-start 
12539    (get-buffer-window gnus-article-buffer)
12540    (save-excursion
12541      (set-buffer gnus-article-buffer)
12542      (goto-char (point-min))
12543      (if (not line)
12544          (point-min)
12545        (gnus-message 6 "Moved to bookmark")
12546        (search-forward "\n\n" nil t)
12547        (forward-line line)
12548        (point)))))
12549
12550 (defun gnus-kill-all-overlays ()
12551   "Delete all overlays in the current buffer."
12552   (when (fboundp 'overlay-lists)
12553     (let* ((overlayss (overlay-lists))
12554            (buffer-read-only nil)
12555            (overlays (nconc (car overlayss) (cdr overlayss))))
12556       (while overlays
12557         (delete-overlay (pop overlays))))))
12558
12559 (defun gnus-request-article-this-buffer (article group)
12560   "Get an article and insert it into this buffer."
12561   (prog1
12562       (save-excursion
12563         (if (get-buffer gnus-original-article-buffer)
12564             (set-buffer (get-buffer gnus-original-article-buffer))
12565           (set-buffer (get-buffer-create gnus-original-article-buffer))
12566           (buffer-disable-undo (current-buffer))
12567           (setq major-mode 'gnus-original-article-mode)
12568           (setq buffer-read-only t)
12569           (gnus-add-current-to-buffer-list))
12570
12571         (setq group (or group gnus-newsgroup-name))
12572
12573         ;; Open server if it has closed.
12574         (gnus-check-server (gnus-find-method-for-group group))
12575
12576         ;; Using `gnus-request-article' directly will insert the article into
12577         ;; `nntp-server-buffer' - so we'll save some time by not having to
12578         ;; copy it from the server buffer into the article buffer.
12579
12580         ;; We only request an article by message-id when we do not have the
12581         ;; headers for it, so we'll have to get those.
12582         (and (stringp article) 
12583              (let ((gnus-override-method gnus-refer-article-method))
12584                (gnus-read-header article)))
12585
12586         ;; If the article number is negative, that means that this article
12587         ;; doesn't belong in this newsgroup (possibly), so we find its
12588         ;; message-id and request it by id instead of number.
12589         (if (not (numberp article))
12590             ()
12591           (save-excursion
12592             (set-buffer gnus-summary-buffer)
12593             (let ((header (gnus-summary-article-header article)))
12594               (if (< article 0)
12595                   (if (vectorp header)
12596                       ;; It's a real article.
12597                       (setq article (mail-header-id header))
12598                     ;; It is an extracted pseudo-article.
12599                     (setq article 'pseudo)
12600                     (gnus-request-pseudo-article header)))
12601
12602               (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12603                 (if (not (eq (car method) 'nneething))
12604                     ()
12605                   (let ((dir (concat (file-name-as-directory (nth 1 method))
12606                                      (mail-header-subject header))))
12607                     (if (file-directory-p dir)
12608                         (progn
12609                           (setq article 'nneething)
12610                           (gnus-group-enter-directory dir)))))))))
12611
12612         (cond 
12613          ;; We first check `gnus-original-article-buffer'.
12614          ((and (equal (car gnus-original-article) group)
12615                (eq (cdr gnus-original-article) article))
12616           ;; We don't have to do anything, since it's already where we
12617           ;; want it.  
12618           'article)
12619          ;; Check the backlog.
12620          ((and gnus-keep-backlog
12621                (gnus-backlog-request-article group article (current-buffer)))
12622           'article)
12623          ;; Check the cache.
12624          ((and gnus-use-cache
12625                (numberp article)
12626                (gnus-cache-request-article article group))
12627           'article)
12628          ;; Get the article and put into the article buffer.
12629          ((or (stringp article) (numberp article))
12630           (let ((gnus-override-method 
12631                  (and (stringp article) gnus-refer-article-method))
12632                 (buffer-read-only nil))
12633             (erase-buffer)
12634             (gnus-kill-all-overlays)
12635             (if (gnus-request-article article group (current-buffer))
12636                 (progn
12637                   (and gnus-keep-backlog 
12638                        (gnus-backlog-enter-article 
12639                         group article (current-buffer)))
12640                   'article))))
12641          ;; It was a pseudo.
12642          (t article)))
12643     (setq gnus-original-article (cons group article))
12644     (let (buffer-read-only)
12645       (erase-buffer)
12646       (gnus-kill-all-overlays)
12647       (insert-buffer-substring gnus-original-article-buffer))))
12648
12649 (defun gnus-read-header (id)
12650   "Read the headers of article ID and enter them into the Gnus system."
12651   (let ((group gnus-newsgroup-name)
12652         (headers gnus-newsgroup-headers)
12653         header where)
12654     ;; First we check to see whether the header in question is already
12655     ;; fetched. 
12656     (if (stringp id)
12657         ;; This is a Message-ID.
12658         (while headers
12659           (if (string= id (mail-header-id (car headers)))
12660               (setq header (car headers)
12661                     headers nil)
12662             (setq headers (cdr headers))))
12663       ;; This is an article number.
12664       (while headers
12665         (if (= id (mail-header-number (car headers)))
12666             (setq header (car headers)
12667                   headers nil)
12668           (setq headers (cdr headers)))))
12669     (if header
12670         ;; We have found the header.
12671         header
12672       ;; We have to really fetch the header to this article.
12673       (when (setq where
12674                   (if (gnus-check-backend-function 'request-head group)
12675                       (gnus-request-head id group)
12676                     (gnus-request-article id group)))
12677         (save-excursion
12678           (set-buffer nntp-server-buffer)
12679           (and (search-forward "\n\n" nil t)
12680                (delete-region (1- (point)) (point-max)))
12681           (goto-char (point-max))
12682           (insert ".\n")
12683           (goto-char (point-min))
12684           (insert "211 "
12685                   (int-to-string
12686                    (cond
12687                     ((numberp id)
12688                      id)
12689                     ((cdr where)
12690                      (cdr where))
12691                     (t
12692                      gnus-reffed-article-number)))
12693                   " Article retrieved.\n"))
12694         (if (not (setq header (car (gnus-get-newsgroup-headers))))
12695             () ; Malformed head.
12696           (if (and (stringp id)
12697                    (not (string= (gnus-group-real-name group)
12698                                  (car where))))
12699               ;; If we fetched by Message-ID and the article came
12700               ;; from a different group, we fudge some bogus article
12701               ;; numbers for this article.
12702               (mail-header-set-number header gnus-reffed-article-number))
12703           (decf gnus-reffed-article-number)
12704           (push header gnus-newsgroup-headers)
12705           (setq gnus-current-headers header)
12706           (push (mail-header-number header) gnus-newsgroup-limit)
12707           header)))))
12708
12709 (defun gnus-article-prepare (article &optional all-headers header)
12710   "Prepare ARTICLE in article mode buffer.
12711 ARTICLE should either be an article number or a Message-ID.
12712 If ARTICLE is an id, HEADER should be the article headers.
12713 If ALL-HEADERS is non-nil, no headers are hidden."
12714   (save-excursion
12715     ;; Make sure we start in a summary buffer.
12716     (unless (eq major-mode 'gnus-summary-mode)
12717       (set-buffer gnus-summary-buffer))
12718     (setq gnus-summary-buffer (current-buffer))
12719     ;; Make sure the connection to the server is alive.
12720     (unless (gnus-server-opened
12721              (gnus-find-method-for-group gnus-newsgroup-name))
12722       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
12723       (gnus-request-group gnus-newsgroup-name t))
12724     (let* ((article (if header (mail-header-number header) article))
12725            (summary-buffer (current-buffer))
12726            (internal-hook gnus-article-internal-prepare-hook)
12727            (group gnus-newsgroup-name)
12728            result)
12729       (save-excursion
12730         (gnus-article-setup-buffer)
12731         (set-buffer gnus-article-buffer)
12732         ;; Deactivate active regions.
12733         (when (and (boundp 'transient-mark-mode)
12734                    transient-mark-mode)
12735           (setq mark-active nil))
12736         (if (not (setq result (let ((buffer-read-only nil))
12737                                 (gnus-request-article-this-buffer 
12738                                  article group))))
12739             ;; There is no such article.
12740             (save-excursion
12741               (if (not (numberp article))
12742                   ()
12743                 (setq gnus-article-current 
12744                       (cons gnus-newsgroup-name article))
12745                 (set-buffer gnus-summary-buffer)
12746                 (setq gnus-current-article article)
12747                 (gnus-summary-mark-article article gnus-canceled-mark))
12748               (gnus-message 
12749                1 "No such article (may have expired or been canceled)")
12750               (ding)
12751               nil)
12752           (if (or (eq result 'pseudo) (eq result 'nneething))
12753               (progn
12754                 (save-excursion
12755                   (set-buffer summary-buffer)
12756                   (setq gnus-last-article gnus-current-article
12757                         gnus-newsgroup-history (cons gnus-current-article
12758                                                      gnus-newsgroup-history)
12759                         gnus-current-article 0
12760                         gnus-current-headers nil
12761                         gnus-article-current nil)
12762                   (if (eq result 'nneething)
12763                       (gnus-configure-windows 'summary)
12764                     (gnus-configure-windows 'article))
12765                   (gnus-set-global-variables))
12766                 (gnus-set-mode-line 'article))
12767             ;; The result from the `request' was an actual article -
12768             ;; or at least some text that is now displayed in the
12769             ;; article buffer.
12770             (if (and (numberp article)
12771                      (not (eq article gnus-current-article)))
12772                 ;; Seems like a new article has been selected.
12773                 ;; `gnus-current-article' must be an article number.
12774                 (save-excursion
12775                   (set-buffer summary-buffer)
12776                   (setq gnus-last-article gnus-current-article
12777                         gnus-newsgroup-history (cons gnus-current-article
12778                                                      gnus-newsgroup-history)
12779                         gnus-current-article article
12780                         gnus-current-headers 
12781                         (gnus-summary-article-header gnus-current-article)
12782                         gnus-article-current 
12783                         (cons gnus-newsgroup-name gnus-current-article))
12784                   (unless (vectorp gnus-current-headers)
12785                     (setq gnus-current-headers nil))
12786                   (gnus-summary-show-thread)
12787                   (run-hooks 'gnus-mark-article-hook)
12788                   (gnus-set-mode-line 'summary)
12789                   (and (gnus-visual-p 'article-highlight 'highlight)
12790                        (run-hooks 'gnus-visual-mark-article-hook))
12791                   ;; Set the global newsgroup variables here.
12792                   ;; Suggested by Jim Sisolak
12793                   ;; <sisolak@trans4.neep.wisc.edu>.
12794                   (gnus-set-global-variables)
12795                   (setq gnus-have-all-headers 
12796                         (or all-headers gnus-show-all-headers))
12797                   (and gnus-use-cache 
12798                        (vectorp (gnus-summary-article-header article))
12799                        (gnus-cache-possibly-enter-article
12800                         group article
12801                         (gnus-summary-article-header article)
12802                         (memq article gnus-newsgroup-marked)
12803                         (memq article gnus-newsgroup-dormant)
12804                         (memq article gnus-newsgroup-unreads)))))
12805             ;; Hooks for getting information from the article.
12806             ;; This hook must be called before being narrowed.
12807             (let (buffer-read-only)
12808               (run-hooks 'internal-hook)
12809               (run-hooks 'gnus-article-prepare-hook)
12810               ;; Decode MIME message.
12811               (if gnus-show-mime
12812                   (if (or (not gnus-strict-mime)
12813                           (gnus-fetch-field "Mime-Version"))
12814                       (funcall gnus-show-mime-method)
12815                     (funcall gnus-decode-encoded-word-method)))
12816               ;; Perform the article display hooks.
12817               (run-hooks 'gnus-article-display-hook))
12818             ;; Do page break.
12819             (goto-char (point-min))
12820             (and gnus-break-pages (gnus-narrow-to-page))
12821             (gnus-set-mode-line 'article)
12822             (gnus-configure-windows 'article)
12823             (goto-char (point-min))
12824             t))))))
12825
12826 (defun gnus-article-show-all-headers ()
12827   "Show all article headers in article mode buffer."
12828   (save-excursion 
12829     (gnus-article-setup-buffer)
12830     (set-buffer gnus-article-buffer)
12831     (let ((buffer-read-only nil))
12832       (remove-text-properties (point-min) (point-max) 
12833                               gnus-hidden-properties))))
12834
12835 (defun gnus-article-hide-headers-if-wanted ()
12836   "Hide unwanted headers if `gnus-have-all-headers' is nil.
12837 Provided for backwards compatability."
12838   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
12839       gnus-inhibit-hiding
12840       (gnus-article-hide-headers)))
12841
12842 (defun gnus-article-hide-headers (&optional show delete)
12843   "Hide unwanted headers and possibly sort them as well."
12844   (interactive "P")
12845   ;; This function might be inhibited.
12846   (if show
12847       (gnus-article-show-hidden-text 'headers)
12848     (unless gnus-inhibit-hiding
12849       (save-excursion
12850         (set-buffer gnus-article-buffer)
12851         (save-restriction
12852           (let ((buffer-read-only nil)
12853                 (ignored (when (not (stringp gnus-visible-headers))
12854                            (cond ((stringp gnus-ignored-headers)
12855                                   gnus-ignored-headers)
12856                                  ((listp gnus-ignored-headers)
12857                                   (mapconcat 'identity gnus-ignored-headers
12858                                              "\\|")))))
12859                 (visible (cond ((stringp gnus-visible-headers)
12860                                 gnus-visible-headers)
12861                                ((listp gnus-visible-headers)
12862                                 (mapconcat 'identity gnus-visible-headers
12863                                            "\\|"))))
12864                 want-list beg want-l)
12865             ;; First we narrow to just the headers.
12866             (widen)
12867             (goto-char (point-min))
12868             ;; Hide any "From " lines at the beginning of (mail) articles. 
12869             (while (looking-at "From ")
12870               (forward-line 1))
12871             (unless (bobp) 
12872               (add-text-properties 
12873                (point-min) (point)
12874                (nconc (list 'gnus-type 'headers) gnus-hidden-properties)))
12875             ;; Then treat the rest of the header lines.
12876             (narrow-to-region 
12877              (point) 
12878              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
12879             ;; Then we use the two regular expressions
12880             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
12881             ;; select which header lines is to remain visible in the
12882             ;; article buffer.
12883             (goto-char (point-min))
12884             (while (re-search-forward "^[^ \t]*:" nil t)
12885               (beginning-of-line)
12886               ;; We add the headers we want to keep to a list and delete
12887               ;; them from the buffer.
12888               (if (or (and visible (looking-at visible))
12889                       (and ignored (not (looking-at ignored))))
12890                   (progn
12891                     (push (buffer-substring
12892                            (setq beg (point))
12893                            (progn 
12894                              (forward-line 1)
12895                              ;; Be sure to get multi-line headers...
12896                              (re-search-forward "^[^ \t]*:" nil t)
12897                              (beginning-of-line) 
12898                              (point)))
12899                           want-list)
12900                     (delete-region beg (point)))
12901                 (forward-line 1)))
12902             ;; Sort the headers that we want to display.
12903             (setq want-list (sort want-list 'gnus-article-header-less))
12904             (goto-char (point-min))
12905             (while want-list
12906               (insert (pop want-list)))
12907             ;; We make the unwanted headers invisible.
12908             (if delete
12909                 (delete-region (point-min) (point-max))
12910               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
12911               (add-text-properties 
12912                (point) (point-max)
12913                (nconc (list 'gnus-type 'headers) 
12914                       gnus-hidden-properties)))))))))
12915
12916 (defsubst gnus-article-header-rank (header)
12917   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
12918   (let ((list gnus-sorted-header-list)
12919         (i 0))
12920     (while list
12921       (when (string-match (car list) header)
12922         (setq list nil))
12923       (setq list (cdr list))
12924       (incf i))
12925     i))
12926
12927 (defun gnus-article-header-less (h1 h2)
12928   "Say whether string H1 is \"less\" than string H2."
12929   (< (gnus-article-header-rank h1)
12930      (gnus-article-header-rank h2)))
12931
12932 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
12933 (defun gnus-article-treat-overstrike ()
12934   "Translate overstrikes into bold text."
12935   (interactive)
12936   (save-excursion
12937     (set-buffer gnus-article-buffer)
12938     (let ((buffer-read-only nil))
12939       (while (search-forward "\b" nil t)
12940         (let ((next (following-char))
12941               (previous (char-after (- (point) 2))))
12942           (cond ((eq next previous)
12943                  (put-text-property (- (point) 2) (point) 'invisible t)
12944                  (put-text-property (point) (1+ (point)) 'face 'bold))
12945                 ((eq next ?_)
12946                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
12947                  (put-text-property
12948                   (- (point) 2) (1- (point)) 'face 'underline))
12949                 ((eq previous ?_)
12950                  (put-text-property (- (point) 2) (point) 'invisible t)
12951                  (put-text-property 
12952                   (point) (1+ (point))  'face 'underline))))))))
12953
12954 (defun gnus-article-word-wrap ()
12955   "Format too long lines."
12956   (interactive)
12957   (save-excursion
12958     (set-buffer gnus-article-buffer)
12959     (let ((buffer-read-only nil)
12960           p)
12961       (widen)
12962       (goto-char (point-min))
12963       (search-forward "\n\n" nil t)
12964       (end-of-line 1)
12965       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
12966             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
12967             (adaptive-fill-mode t))
12968         (while (not (eobp))
12969           (and (>= (current-column) (min fill-column (window-width)))
12970                (/= (preceding-char) ?:)
12971                (fill-paragraph nil))
12972           (end-of-line 2))))))
12973
12974 (defun gnus-article-remove-cr ()
12975   "Remove carriage returns from an article."
12976   (interactive)
12977   (save-excursion
12978     (set-buffer gnus-article-buffer)
12979     (let ((buffer-read-only nil))
12980       (goto-char (point-min))
12981       (while (search-forward "\r" nil t)
12982         (replace-match "" t t)))))
12983
12984 (defun gnus-article-display-x-face (&optional force)
12985   "Look for an X-Face header and display it if present."
12986   (interactive (list 'force))
12987   (save-excursion
12988     (set-buffer gnus-article-buffer)
12989     ;; Delete the old process, if any.
12990     (when (process-status "gnus-x-face")
12991       (delete-process "gnus-x-face"))
12992     (let ((inhibit-point-motion-hooks t)
12993           (case-fold-search nil)
12994           from)
12995       (save-restriction
12996         (gnus-narrow-to-headers)
12997         (setq from (mail-fetch-field "from"))
12998         (goto-char (point-min))
12999         (when (and gnus-article-x-face-command 
13000                    (or force
13001                        ;; Check whether this face is censored.
13002                        (not gnus-article-x-face-too-ugly)
13003                        (and gnus-article-x-face-too-ugly from
13004                             (not (string-match gnus-article-x-face-too-ugly 
13005                                                from))))
13006                    ;; Has to be present.
13007                    (re-search-forward "^X-Face: " nil t))
13008           ;; We now have the area of the buffer where the X-Face is stored.
13009           (let ((beg (point))
13010                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13011             ;; We display the face.
13012             (if (symbolp gnus-article-x-face-command)
13013                 ;; The command is a lisp function, so we call it.
13014                 (if (gnus-functionp gnus-article-x-face-command)
13015                     (funcall gnus-article-x-face-command beg end)
13016                   (error "%s is not a function" gnus-article-x-face-command))
13017               ;; The command is a string, so we interpret the command
13018               ;; as a, well, command, and fork it off.
13019               (let ((process-connection-type nil))
13020                 (process-kill-without-query
13021                  (start-process 
13022                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13023                 (process-send-region "gnus-x-face" beg end)
13024                 (process-send-eof "gnus-x-face")))))))))
13025
13026 (defun gnus-headers-decode-quoted-printable ()
13027   "Hack to remove QP encoding from headers."
13028   (let ((case-fold-search t)
13029         (inhibit-point-motion-hooks t)
13030         string)
13031     (goto-char (point-min))
13032     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13033       (setq string (match-string 1))
13034       (narrow-to-region (match-beginning 0) (match-end 0))
13035       (delete-region (point-min) (point-max))
13036       (insert string)
13037       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13038       (subst-char-in-region (point-min) (point-max) ?_ ? )
13039       (widen)
13040       (goto-char (point-min)))))
13041        
13042 (defun gnus-article-de-quoted-unreadable (&optional force)
13043   "Do a naive translation of a quoted-printable-encoded article.
13044 This is in no way, shape or form meant as a replacement for real MIME
13045 processing, but is simply a stop-gap measure until MIME support is
13046 written.
13047 If FORCE, decode the article whether it is marked as quoted-printable
13048 or not." 
13049   (interactive (list 'force))
13050   (save-excursion
13051     (set-buffer gnus-article-buffer)
13052     (let ((case-fold-search t)
13053           (buffer-read-only nil)
13054           (type (gnus-fetch-field "content-transfer-encoding")))
13055       (when (or force
13056                 (and type (string-match "quoted-printable" type)))
13057         (goto-char (point-min))
13058         (search-forward "\n\n" nil 'move)
13059         (gnus-mime-decode-quoted-printable (point) (point-max))
13060         (gnus-headers-decode-quoted-printable)))))
13061
13062 (defun gnus-mime-decode-quoted-printable (from to)
13063   "Decode Quoted-Printable in the region between FROM and TO."
13064   (goto-char from)
13065   (while (search-forward "=" to t)
13066     (cond ((eq (following-char) ?\n)
13067            (delete-char -1)
13068            (delete-char 1))
13069           ((looking-at "[0-9A-F][0-9A-F]")
13070            (delete-char -1)
13071            (insert (hexl-hex-string-to-integer
13072                     (buffer-substring (point) (+ 2 (point)))))
13073            (delete-char 2))
13074           ((looking-at "=")
13075            (delete-char 1))
13076           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13077
13078 (defun gnus-article-hide-pgp (&optional show)
13079   "Hide any PGP headers and signatures in the current article.
13080 If given a prefix, show the hidden PGP gruft instead."
13081   (interactive)
13082   (if show
13083       (gnus-article-show-hidden-text 'pgp)
13084     (save-excursion
13085       (set-buffer gnus-article-buffer)
13086       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13087             buffer-read-only beg end)
13088         (widen)
13089         (goto-char (point-min))
13090         ;; Hide the "header".
13091         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13092              (add-text-properties (match-beginning 0) (match-end 0) props))
13093         (setq beg (point))
13094         ;; Hide the actual signature.
13095         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13096              (setq end (match-beginning 0))
13097              (add-text-properties 
13098               (match-beginning 0)
13099               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13100                   (match-end 0)
13101                 ;; Perhaps we shouldn't hide to the end of the buffer
13102                 ;; if there is no end to the signature?
13103                 (point-max))
13104               props))
13105         ;; Hide "- " PGP quotation markers.
13106         (when (and beg end)
13107           (narrow-to-region beg end)
13108           (goto-char (point-min))
13109           (while (re-search-forward "^- " nil t)
13110             (add-text-properties (match-beginning 0) (match-end 0) props))
13111           (widen))))))
13112
13113 (defun gnus-article-hide-signature (&optional show)
13114   "Hide the signature in the current article.
13115 If given a prefix, show the signature instead."
13116   (interactive "P")
13117   (if show
13118       (gnus-article-show-hidden-text 'signature)
13119     (save-excursion
13120       (set-buffer gnus-article-buffer)
13121       (let ((buffer-read-only nil))
13122         (goto-char (point-max))
13123         (and (re-search-backward gnus-signature-separator nil t)
13124              gnus-signature-face
13125              (add-text-properties 
13126               (match-end 0) (point-max)
13127               (nconc (list 'gnus-type 'signature)
13128                      gnus-hidden-properties)))))))
13129
13130 (defun gnus-article-hide (&optional show force)
13131   "Hide all the gruft in the current article.
13132 This means that PGP stuff, signatures, cited text and (some)
13133 headers will be hidden.
13134 If given a prefix, show the hidden text instead."
13135   (interactive (list current-prefix-arg 'force))
13136   (gnus-article-hide-headers show)
13137   (gnus-article-hide-pgp show)
13138   (gnus-article-hide-citation-maybe show force)
13139   (gnus-article-hide-signature show))
13140
13141 (defun gnus-article-show-hidden-text (type)
13142   (save-excursion
13143     (set-buffer gnus-article-buffer)
13144     (let ((buffer-read-only nil)
13145           (beg (point)))
13146       (while (gnus-goto-char (text-property-any
13147                               beg (point-max) 'gnus-type type))
13148         (remove-text-properties (point) (setq beg (1+ (point)))
13149                                 gnus-hidden-properties)))))
13150
13151 (defvar gnus-article-time-units
13152   `((year . ,(* 365.25 24 60 60))
13153     (week . ,(* 7 24 60 60))
13154     (day . ,(* 24 60 60))
13155     (hour . ,(* 60 60))
13156     (minute . 60)
13157     (second . 1))
13158   "Mapping from time units to seconds.")
13159
13160 (defun gnus-article-date-ut (&optional type highlight)
13161   "Convert DATE date to universal time in the current article.
13162 If TYPE is `local', convert to local time; if it is `lapsed', output
13163 how much time has lapsed since DATE."
13164   (interactive (list 'ut t))
13165   (let* ((header (or gnus-current-headers 
13166                      (gnus-summary-article-header) ""))
13167          (date (and (vectorp header) (mail-header-date header)))
13168          (date-regexp "^Date: \\|^X-Sent: ")
13169          (inhibit-point-motion-hooks t))
13170     (when (and date (not (string= date "")))
13171       (save-excursion
13172         (set-buffer gnus-article-buffer)
13173         (save-restriction
13174           (gnus-narrow-to-headers)
13175           (let ((buffer-read-only nil))
13176             ;; Delete any old Date headers.
13177             (if (zerop (nnheader-remove-header date-regexp t))
13178                 (beginning-of-line)
13179               (goto-char (point-max)))
13180             (insert
13181              (cond 
13182               ;; Convert to the local timezone.  We have to slap a
13183               ;; `condition-case' round the calls to the timezone
13184               ;; functions since they aren't particularly resistant to
13185               ;; buggy dates.
13186               ((eq type 'local)
13187                (concat "Date: " (condition-case ()
13188                                     (timezone-make-date-arpa-standard date)
13189                                   (error date))
13190                        "\n"))
13191               ;; Convert to Universal Time.
13192               ((eq type 'ut)
13193                (concat "Date: "
13194                        (condition-case ()
13195                            (timezone-make-date-arpa-standard date nil "UT")
13196                          (error date))
13197                        "\n"))
13198               ;; Get the original date from the article.
13199               ((eq type 'original)
13200                (concat "Date: " date "\n"))
13201               ;; Do an X-Sent lapsed format.
13202               ((eq type 'lapsed)
13203                ;; If the date is seriously mangled, the timezone
13204                ;; functions are liable to bug out, so we condition-case
13205                ;; the entire thing.  
13206                (let* ((real-sec (condition-case ()
13207                                     (- (gnus-seconds-since-epoch 
13208                                         (timezone-make-date-arpa-standard
13209                                          (current-time-string) 
13210                                          (current-time-zone) "UT"))
13211                                        (gnus-seconds-since-epoch 
13212                                         (timezone-make-date-arpa-standard 
13213                                          date nil "UT")))
13214                                   (error 0)))
13215                       (sec (abs real-sec))
13216                       num prev)
13217                  (if (zerop sec)
13218                      "X-Sent: Now\n"
13219                    (concat
13220                     "X-Sent: "
13221                     ;; This is a bit convoluted, but basically we go
13222                     ;; through the time units for years, weeks, etc,
13223                     ;; and divide things to see whether that results
13224                     ;; in positive answers.
13225                     (mapconcat 
13226                      (lambda (unit)
13227                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13228                            ;; The (remaining) seconds are too few to
13229                            ;; be divided into this time unit.
13230                            "" 
13231                          ;; It's big enough, so we output it.
13232                          (setq sec (- sec (* num (cdr unit))))
13233                          (prog1
13234                              (concat (if prev ", " "") (int-to-string 
13235                                                         (floor num))
13236                                      " " (symbol-name (car unit))
13237                                      (if (> num 1) "s" ""))
13238                            (setq prev t))))
13239                      gnus-article-time-units "")
13240                     ;; If dates are odd, then it might appear like the
13241                     ;; article was sent in the future.
13242                     (if (> real-sec 0)
13243                         " ago\n"
13244                       " in the future\n")))))
13245               (t
13246                (error "Unknown conversion type: %s" type)))))
13247           ;; Do highlighting.
13248           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13249             (gnus-article-highlight-headers)))))))
13250
13251 (defun gnus-article-date-local (&optional highlight)
13252   "Convert the current article date to the local timezone."
13253   (interactive (list t))
13254   (gnus-article-date-ut 'local highlight))
13255
13256 (defun gnus-article-date-original (&optional highlight)
13257   "Convert the current article date to what it was originally.
13258 This is only useful if you have used some other date conversion
13259 function and want to see what the date was before converting."
13260   (interactive (list t))
13261   (gnus-article-date-ut 'original highlight))
13262
13263 (defun gnus-article-date-lapsed (&optional highlight)
13264   "Convert the current article date to time lapsed since it was sent."
13265   (interactive (list t))
13266   (gnus-article-date-ut 'lapsed highlight))
13267
13268 (defun gnus-article-maybe-highlight ()
13269   "Do some article highlighting if `gnus-visual' is non-nil."
13270   (if (gnus-visual-p 'article-highlight 'highlight)
13271       (gnus-article-highlight-some)))
13272
13273 ;; Article savers.
13274
13275 (defun gnus-output-to-rmail (file-name)
13276   "Append the current article to an Rmail file named FILE-NAME."
13277   (require 'rmail)
13278   ;; Most of these codes are borrowed from rmailout.el.
13279   (setq file-name (expand-file-name file-name))
13280   (setq rmail-default-rmail-file file-name)
13281   (let ((artbuf (current-buffer))
13282         (tmpbuf (get-buffer-create " *Gnus-output*")))
13283     (save-excursion
13284       (or (get-file-buffer file-name)
13285           (file-exists-p file-name)
13286           (if (gnus-yes-or-no-p
13287                (concat "\"" file-name "\" does not exist, create it? "))
13288               (let ((file-buffer (create-file-buffer file-name)))
13289                 (save-excursion
13290                   (set-buffer file-buffer)
13291                   (rmail-insert-rmail-file-header)
13292                   (let ((require-final-newline nil))
13293                     (write-region (point-min) (point-max) file-name t 1)))
13294                 (kill-buffer file-buffer))
13295             (error "Output file does not exist")))
13296       (set-buffer tmpbuf)
13297       (buffer-disable-undo (current-buffer))
13298       (erase-buffer)
13299       (insert-buffer-substring artbuf)
13300       (gnus-convert-article-to-rmail)
13301       ;; Decide whether to append to a file or to an Emacs buffer.
13302       (let ((outbuf (get-file-buffer file-name)))
13303         (if (not outbuf)
13304             (append-to-file (point-min) (point-max) file-name)
13305           ;; File has been visited, in buffer OUTBUF.
13306           (set-buffer outbuf)
13307           (let ((buffer-read-only nil)
13308                 (msg (and (boundp 'rmail-current-message)
13309                           (symbol-value 'rmail-current-message))))
13310             ;; If MSG is non-nil, buffer is in RMAIL mode.
13311             (if msg
13312                 (progn (widen)
13313                        (narrow-to-region (point-max) (point-max))))
13314             (insert-buffer-substring tmpbuf)
13315             (if msg
13316                 (progn
13317                   (goto-char (point-min))
13318                   (widen)
13319                   (search-backward "\^_")
13320                   (narrow-to-region (point) (point-max))
13321                   (goto-char (1+ (point-min)))
13322                   (rmail-count-new-messages t)
13323                   (rmail-show-message msg)))))))
13324     (kill-buffer tmpbuf)))
13325
13326 (defun gnus-output-to-file (file-name)
13327   "Append the current article to a file named FILE-NAME."
13328   (setq file-name (expand-file-name file-name))
13329   (let ((artbuf (current-buffer))
13330         (tmpbuf (get-buffer-create " *Gnus-output*")))
13331     (save-excursion
13332       (set-buffer tmpbuf)
13333       (buffer-disable-undo (current-buffer))
13334       (erase-buffer)
13335       (insert-buffer-substring artbuf)
13336       ;; Append newline at end of the buffer as separator, and then
13337       ;; save it to file.
13338       (goto-char (point-max))
13339       (insert "\n")
13340       (append-to-file (point-min) (point-max) file-name))
13341     (kill-buffer tmpbuf)))
13342
13343 (defun gnus-convert-article-to-rmail ()
13344   "Convert article in current buffer to Rmail message format."
13345   (let ((buffer-read-only nil))
13346     ;; Convert article directly into Babyl format.
13347     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
13348     (goto-char (point-min))
13349     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
13350     (while (search-forward "\n\^_" nil t) ;single char
13351       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
13352     (goto-char (point-max))
13353     (insert "\^_")))
13354
13355 (defun gnus-narrow-to-page (&optional arg)
13356   "Narrow the article buffer to a page.
13357 If given a numerical ARG, move forward ARG pages."
13358   (interactive "P")
13359   (setq arg (if arg (prefix-numeric-value arg) 0))
13360   (save-excursion
13361     (set-buffer gnus-article-buffer)
13362     (goto-char (point-min))
13363     (widen)
13364     (when (gnus-visual-p 'page-marker)
13365       (let ((buffer-read-only nil))
13366         (gnus-remove-text-with-property 'gnus-prev)
13367         (gnus-remove-text-with-property 'gnus-next)))
13368     (when 
13369         (cond ((< arg 0)
13370                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
13371               ((> arg 0)
13372                (re-search-forward page-delimiter nil 'move arg)))
13373       (goto-char (match-end 0)))
13374     (narrow-to-region
13375      (point)
13376      (if (re-search-forward page-delimiter nil 'move)
13377          (match-beginning 0) 
13378        (point)))
13379     (when (and (gnus-visual-p 'page-marker)
13380                (not (= (point-min) 1)))
13381       (save-excursion
13382         (goto-char (point-min))
13383         (gnus-insert-prev-page-button)))
13384     (when (and (gnus-visual-p 'page-marker)
13385                (not (= (1- (point-max)) (buffer-size))))
13386       (save-excursion
13387         (goto-char (point-max))
13388         (gnus-insert-next-page-button)))))
13389
13390
13391 ;; Article mode commands
13392
13393 (defun gnus-article-next-page (&optional lines)
13394   "Show next page of current article.
13395 If end of article, return non-nil.  Otherwise return nil.
13396 Argument LINES specifies lines to be scrolled up."
13397   (interactive "P")
13398   (move-to-window-line -1)
13399   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
13400   (if (save-excursion
13401         (end-of-line)
13402         (and (pos-visible-in-window-p)  ;Not continuation line.
13403              (eobp)))
13404       ;; Nothing in this page.
13405       (if (or (not gnus-break-pages)
13406               (save-excursion
13407                 (save-restriction
13408                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
13409           t                             ;Nothing more.
13410         (gnus-narrow-to-page 1)         ;Go to next page.
13411         nil)
13412     ;; More in this page.
13413     (condition-case ()
13414         (scroll-up lines)
13415       (end-of-buffer
13416        ;; Long lines may cause an end-of-buffer error.
13417        (goto-char (point-max))))
13418     nil))
13419
13420 (defun gnus-article-prev-page (&optional lines)
13421   "Show previous page of current article.
13422 Argument LINES specifies lines to be scrolled down."
13423   (interactive "P")
13424   (move-to-window-line 0)
13425   (if (and gnus-break-pages
13426            (bobp)
13427            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
13428       (progn
13429         (gnus-narrow-to-page -1)        ;Go to previous page.
13430         (goto-char (point-max))
13431         (recenter -1))
13432     (condition-case ()
13433         (scroll-down lines)
13434       (error nil))))
13435
13436 (defun gnus-article-refer-article ()
13437   "Read article specified by message-id around point."
13438   (interactive)
13439   (search-forward ">" nil t)            ;Move point to end of "<....>".
13440   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
13441       (let ((message-id (match-string 1)))
13442         (set-buffer gnus-summary-buffer)
13443         (gnus-summary-refer-article message-id))
13444     (error "No references around point")))
13445
13446 (defun gnus-article-show-summary ()
13447   "Reconfigure windows to show summary buffer."
13448   (interactive)
13449   (gnus-configure-windows 'article)
13450   (gnus-summary-goto-subject gnus-current-article))
13451
13452 (defun gnus-article-describe-briefly ()
13453   "Describe article mode commands briefly."
13454   (interactive)
13455   (gnus-message 6
13456                 (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")))
13457
13458 (defun gnus-article-summary-command ()
13459   "Execute the last keystroke in the summary buffer."
13460   (interactive)
13461   (let ((obuf (current-buffer))
13462         (owin (current-window-configuration))
13463         func)
13464     (switch-to-buffer gnus-summary-buffer 'norecord)
13465     (setq func (lookup-key (current-local-map) (this-command-keys)))
13466     (call-interactively func)
13467     (set-buffer obuf)
13468     (set-window-configuration owin)
13469     (set-window-point (get-buffer-window (current-buffer)) (point))))
13470
13471 (defun gnus-article-summary-command-nosave ()
13472   "Execute the last keystroke in the summary buffer."
13473   (interactive)
13474   (let (func)
13475     (pop-to-buffer gnus-summary-buffer 'norecord)
13476     (setq func (lookup-key (current-local-map) (this-command-keys)))
13477     (call-interactively func)))
13478
13479 \f
13480 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
13481
13482 ;;;###autoload
13483 (defalias 'gnus-batch-kill 'gnus-batch-score)
13484 ;;;###autoload
13485 (defun gnus-batch-score ()
13486   "Run batched scoring.
13487 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
13488 Newsgroups is a list of strings in Bnews format.  If you want to score
13489 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
13490 score the alt hierarchy, you'd say \"!alt.all\"."
13491   (interactive)
13492   (let* ((yes-and-no
13493           (gnus-newsrc-parse-options
13494            (apply (function concat)
13495                   (mapcar (lambda (g) (concat g " "))
13496                           command-line-args-left))))
13497          (gnus-expert-user t)
13498          (nnmail-spool-file nil)
13499          (gnus-use-dribble-file nil)
13500          (yes (car yes-and-no))
13501          (no (cdr yes-and-no))
13502          group newsrc entry
13503          ;; Disable verbose message.
13504          gnus-novice-user gnus-large-newsgroup)
13505     ;; Eat all arguments.
13506     (setq command-line-args-left nil)
13507     ;; Start Gnus.
13508     (gnus)
13509     ;; Apply kills to specified newsgroups in command line arguments.
13510     (setq newsrc (cdr gnus-newsrc-alist))
13511     (while newsrc
13512       (setq group (car (car newsrc)))
13513       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
13514       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
13515                (and (car entry)
13516                     (or (eq (car entry) t)
13517                         (not (zerop (car entry)))))
13518                (if yes (string-match yes group) t)
13519                (or (null no) (not (string-match no group))))
13520           (progn
13521             (gnus-summary-read-group group nil t nil t)
13522             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
13523                  (gnus-summary-exit))))
13524       (setq newsrc (cdr newsrc)))
13525     ;; Exit Emacs.
13526     (switch-to-buffer gnus-group-buffer)
13527     (gnus-group-save-newsrc)))
13528
13529 (defun gnus-apply-kill-file ()
13530   "Apply a kill file to the current newsgroup.
13531 Returns the number of articles marked as read."
13532   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
13533           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
13534       (gnus-apply-kill-file-internal)
13535     0))
13536
13537 (defun gnus-kill-save-kill-buffer ()
13538   (save-excursion
13539     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
13540       (if (get-file-buffer file)
13541           (progn
13542             (set-buffer (get-file-buffer file))
13543             (and (buffer-modified-p) (save-buffer))
13544             (kill-buffer (current-buffer)))))))
13545
13546 (defvar gnus-kill-file-name "KILL"
13547   "Suffix of the kill files.")
13548
13549 (defun gnus-newsgroup-kill-file (newsgroup)
13550   "Return the name of a kill file name for NEWSGROUP.
13551 If NEWSGROUP is nil, return the global kill file name instead."
13552   (cond ((or (null newsgroup)
13553              (string-equal newsgroup ""))
13554          ;; The global KILL file is placed at top of the directory.
13555          (expand-file-name gnus-kill-file-name
13556                            (or gnus-kill-files-directory "~/News")))
13557         ((gnus-use-long-file-name 'not-kill)
13558          ;; Append ".KILL" to newsgroup name.
13559          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
13560                                    "." gnus-kill-file-name)
13561                            (or gnus-kill-files-directory "~/News")))
13562         (t
13563          ;; Place "KILL" under the hierarchical directory.
13564          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13565                                    "/" gnus-kill-file-name)
13566                            (or gnus-kill-files-directory "~/News")))))
13567
13568 \f
13569 ;;;
13570 ;;; Dribble file
13571 ;;;
13572
13573 (defvar gnus-dribble-ignore nil)
13574 (defvar gnus-dribble-eval-file nil)
13575
13576 (defun gnus-dribble-file-name ()
13577   (concat 
13578    (if gnus-dribble-directory
13579        (concat (file-name-as-directory gnus-dribble-directory)
13580                (file-name-nondirectory gnus-current-startup-file))
13581      gnus-current-startup-file)
13582    "-dribble"))
13583
13584 (defun gnus-dribble-enter (string)
13585   (if (and (not gnus-dribble-ignore)
13586            (or gnus-dribble-buffer
13587                gnus-slave)
13588            (buffer-name gnus-dribble-buffer))
13589       (let ((obuf (current-buffer)))
13590         (set-buffer gnus-dribble-buffer)
13591         (insert string "\n")
13592         (set-window-point (get-buffer-window (current-buffer)) (point-max))
13593         (set-buffer obuf))))
13594
13595 (defun gnus-dribble-read-file ()
13596   (let ((dribble-file (gnus-dribble-file-name)))
13597     (save-excursion 
13598       (set-buffer (setq gnus-dribble-buffer 
13599                         (get-buffer-create 
13600                          (file-name-nondirectory dribble-file))))
13601       (gnus-add-current-to-buffer-list)
13602       (erase-buffer)
13603       (setq buffer-file-name dribble-file)
13604       (auto-save-mode t)
13605       (buffer-disable-undo (current-buffer))
13606       (bury-buffer (current-buffer))
13607       (set-buffer-modified-p nil)
13608       (let ((auto (make-auto-save-file-name))
13609             (gnus-dribble-ignore t))
13610         (if (or (file-exists-p auto) (file-exists-p dribble-file))
13611             (progn
13612               (if (file-newer-than-file-p auto dribble-file)
13613                   (setq dribble-file auto))
13614               (insert-file-contents dribble-file)
13615               (if (not (zerop (buffer-size)))
13616                   (set-buffer-modified-p t))
13617               (if (gnus-y-or-n-p 
13618                    "Auto-save file exists.  Do you want to read it? ")
13619                   (setq gnus-dribble-eval-file t))))))))
13620
13621 (defun gnus-dribble-eval-file ()
13622   (if (not gnus-dribble-eval-file)
13623       ()
13624     (setq gnus-dribble-eval-file nil)
13625     (save-excursion
13626       (let ((gnus-dribble-ignore t))
13627         (set-buffer gnus-dribble-buffer)
13628         (eval-buffer (current-buffer))))))
13629
13630 (defun gnus-dribble-delete-file ()
13631   (if (file-exists-p (gnus-dribble-file-name))
13632       (delete-file (gnus-dribble-file-name)))
13633   (if gnus-dribble-buffer
13634       (save-excursion
13635         (set-buffer gnus-dribble-buffer)
13636         (let ((auto (make-auto-save-file-name)))
13637           (if (file-exists-p auto)
13638               (delete-file auto))
13639           (erase-buffer)
13640           (set-buffer-modified-p nil)))))
13641
13642 (defun gnus-dribble-save ()
13643   (if (and gnus-dribble-buffer
13644            (buffer-name gnus-dribble-buffer))
13645       (save-excursion
13646         (set-buffer gnus-dribble-buffer)
13647         (save-buffer))))
13648
13649 (defun gnus-dribble-clear ()
13650   (save-excursion
13651     (if (gnus-buffer-exists-p gnus-dribble-buffer)
13652         (progn
13653           (set-buffer gnus-dribble-buffer)
13654           (erase-buffer)
13655           (set-buffer-modified-p nil)
13656           (setq buffer-saved-size (buffer-size))))))
13657
13658 ;;;
13659 ;;; Server Communication
13660 ;;;
13661
13662 (defun gnus-start-news-server (&optional confirm)
13663   "Open a method for getting news.
13664 If CONFIRM is non-nil, the user will be asked for an NNTP server."
13665   (let (how)
13666     (if gnus-current-select-method
13667         ;; Stream is already opened.
13668         nil
13669       ;; Open NNTP server.
13670       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
13671       (if confirm
13672           (progn
13673             ;; Read server name with completion.
13674             (setq gnus-nntp-server
13675                   (completing-read "NNTP server: "
13676                                    (mapcar (lambda (server) (list server))
13677                                            (cons (list gnus-nntp-server)
13678                                                  gnus-secondary-servers))
13679                                    nil nil gnus-nntp-server))))
13680
13681       (if (and gnus-nntp-server 
13682                (stringp gnus-nntp-server)
13683                (not (string= gnus-nntp-server "")))
13684           (setq gnus-select-method
13685                 (cond ((or (string= gnus-nntp-server "")
13686                            (string= gnus-nntp-server "::"))
13687                        (list 'nnspool (system-name)))
13688                       ((string-match "^:" gnus-nntp-server)
13689                        (list 'nnmh gnus-nntp-server 
13690                              (list 'nnmh-directory 
13691                                    (file-name-as-directory
13692                                     (expand-file-name
13693                                      (concat "~/" (substring
13694                                                    gnus-nntp-server 1)))))
13695                              (list 'nnmh-get-new-mail nil)))
13696                       (t
13697                        (list 'nntp gnus-nntp-server)))))
13698
13699       (setq how (car gnus-select-method))
13700       (cond ((eq how 'nnspool)
13701              (require 'nnspool)
13702              (gnus-message 5 "Looking up local news spool..."))
13703             ((eq how 'nnmh)
13704              (require 'nnmh)
13705              (gnus-message 5 "Looking up mh spool..."))
13706             (t
13707              (require 'nntp)))
13708       (setq gnus-current-select-method gnus-select-method)
13709       (run-hooks 'gnus-open-server-hook)
13710       (or 
13711        ;; gnus-open-server-hook might have opened it
13712        (gnus-server-opened gnus-select-method)  
13713        (gnus-open-server gnus-select-method)
13714        (gnus-y-or-n-p
13715         (format
13716          "%s open error: '%s'.  Continue? "
13717          (nth 1 gnus-select-method)
13718          (gnus-status-message gnus-select-method)))
13719        (progn
13720          (gnus-message 1 "Couldn't open server on %s" 
13721                        (nth 1 gnus-select-method))
13722          (ding)
13723          nil)))))
13724
13725 (defun gnus-check-server (&optional method)
13726   "Check whether the connection to METHOD is down.
13727 If METHOD is nil, use `gnus-select-method'.
13728 If it is down, start it up (again)."
13729   (let ((method (or method gnus-select-method)))
13730     ;; Transform virtual server names into select methods.
13731     (when (stringp method)
13732       (setq method (gnus-server-to-method method)))
13733     (if (gnus-server-opened method)
13734         ;; The stream is already opened.
13735         t
13736       ;; Open the server.
13737       (gnus-message 5 "Opening %s server on %s..." (car method) (nth 1 method))
13738       (run-hooks 'gnus-open-server-hook)
13739       (prog1
13740           (gnus-open-server method)
13741         (message "")))))
13742
13743 (defun gnus-get-function (method function)
13744   "Return a function symbol based on METHOD and FUNCTION."
13745   ;; Translate server names into methods.
13746   (unless method
13747     (error "Attempted use of a nil select method"))
13748   (when (stringp method)
13749     (setq method (gnus-server-to-method method)))
13750   (let ((func (intern (format "%s-%s" (car method) function))))
13751     ;; If the functions isn't bound, we require the backend in
13752     ;; question.  
13753     (unless (fboundp func)
13754       (require (car method))
13755       (unless (fboundp func)
13756         ;; This backend doesn't implement this function.
13757         (error "No such function: %s" func)))
13758     func))
13759
13760 ;;; Interface functions to the backends.
13761
13762 (defun gnus-open-server (method)
13763   "Open a connection to METHOD."
13764   (let ((elem (assoc method gnus-opened-servers)))
13765     ;; If this method was previously denied, we just return nil.
13766     (if (eq (nth 1 elem) 'denied)
13767         (progn
13768           (gnus-message 1 "Denied server")
13769           nil)
13770       ;; Open the server.
13771       (let ((result
13772              (funcall (gnus-get-function method 'open-server)
13773                       (nth 1 method) (nthcdr 2 method))))
13774         ;; If this hasn't been opened before, we add it to the list.
13775         (unless elem 
13776           (setq elem (list method nil)
13777                 gnus-opened-servers (cons elem gnus-opened-servers)))
13778         ;; Set the status of this server.
13779         (setcar (cdr elem) (if result 'ok 'denied))
13780         ;; Return the result from the "open" call.
13781         result))))
13782
13783 (defun gnus-close-server (method)
13784   "Close the connection to METHOD."
13785   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
13786
13787 (defun gnus-request-list (method)
13788   "Request the active file from METHOD."
13789   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
13790
13791 (defun gnus-request-list-newsgroups (method)
13792   "Request the newsgroups file from METHOD."
13793   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
13794
13795 (defun gnus-request-newgroups (date method)
13796   "Request all new groups since DATE from METHOD."
13797   (funcall (gnus-get-function method 'request-newgroups) 
13798            date (nth 1 method)))
13799
13800 (defun gnus-server-opened (method)
13801   "Check whether a connection to METHOD has been opened."
13802   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
13803
13804 (defun gnus-status-message (method)
13805   "Return the status message from METHOD.
13806 If METHOD is a string, it is interpreted as a group name.   The method
13807 this group uses will be queried."
13808   (let ((method (if (stringp method) (gnus-find-method-for-group method)
13809                   method)))
13810     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
13811
13812 (defun gnus-request-group (group &optional dont-check)
13813   "Request GROUP.  If DONT-CHECK, no information is required."
13814   (let ((method (gnus-find-method-for-group group)))
13815     (funcall (gnus-get-function method 'request-group) 
13816              (gnus-group-real-name group) (nth 1 method) dont-check)))
13817
13818 (defun gnus-request-asynchronous (group &optional articles)
13819   "Request that GROUP behave asynchronously.
13820 ARTICLES is the `data' of the group."
13821   (let ((method (gnus-find-method-for-group group)))
13822     (funcall (gnus-get-function method 'request-asynchronous) 
13823              (gnus-group-real-name group) (nth 1 method) articles)))
13824
13825 (defun gnus-list-active-group (group)
13826   "Request active information on GROUP."
13827   (let ((method (gnus-find-method-for-group group))
13828         (func 'list-active-group))
13829     (when (gnus-check-backend-function func group)
13830       (funcall (gnus-get-function method func) 
13831                (gnus-group-real-name group) (nth 1 method)))))
13832
13833 (defun gnus-request-group-description (group)
13834   "Request a description of GROUP."
13835   (let ((method (gnus-find-method-for-group group))
13836         (func 'request-group-description))
13837     (when (gnus-check-backend-function func group)
13838       (funcall (gnus-get-function method func) 
13839                (gnus-group-real-name group) (nth 1 method)))))
13840
13841 (defun gnus-close-group (group)
13842   "Request the GROUP be closed."
13843   (let ((method (gnus-find-method-for-group group)))
13844     (funcall (gnus-get-function method 'close-group) 
13845              (gnus-group-real-name group) (nth 1 method))))
13846
13847 (defun gnus-retrieve-headers (articles group &optional fetch-old)
13848   "Request headers for ARTICLES in GROUP.
13849 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
13850   (let ((method (gnus-find-method-for-group group)))
13851     (if (and gnus-use-cache (numberp (car articles)))
13852         (gnus-cache-retrieve-headers articles group fetch-old)
13853       (funcall (gnus-get-function method 'retrieve-headers) 
13854                articles (gnus-group-real-name group) (nth 1 method)
13855                fetch-old))))
13856
13857 (defun gnus-retrieve-groups (groups method)
13858   "Request active information on GROUPS from METHOD."
13859   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
13860
13861 (defun gnus-request-type (group &optional article)
13862   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
13863   (let ((method (gnus-find-method-for-group group)))
13864     (if (not (gnus-check-backend-function 'request-type (car method)))
13865         'unknown
13866       (funcall (gnus-get-function method 'request-type)
13867                (gnus-group-real-name group) article))))
13868
13869 (defun gnus-request-article (article group &optional buffer)
13870   "Request the ARTICLE in GROUP.
13871 ARTICLE can either be an article number or an article Message-ID.
13872 If BUFFER, insert the article in that group."
13873   (let ((method (gnus-find-method-for-group group)))
13874     (funcall (gnus-get-function method 'request-article) 
13875              article (gnus-group-real-name group) (nth 1 method) buffer)))
13876
13877 (defun gnus-request-head (article group)
13878   "Request the head of ARTICLE in GROUP."
13879   (let ((method (gnus-find-method-for-group group)))
13880     (funcall (gnus-get-function method 'request-head) 
13881              article (gnus-group-real-name group) (nth 1 method))))
13882
13883 (defun gnus-request-body (article group)
13884   "Request the body of ARTICLE in GROUP."
13885   (let ((method (gnus-find-method-for-group group)))
13886     (funcall (gnus-get-function method 'request-body) 
13887              article (gnus-group-real-name group) (nth 1 method))))
13888
13889 (defun gnus-request-post (method)
13890   "Post the current buffer using METHOD."
13891   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
13892
13893 (defun gnus-request-scan (group method)
13894   "Request a SCAN being performed in GROUP from METHOD.
13895 If GROUP is nil, all groups on METHOD are scanned."
13896   (let ((method (if group (gnus-find-method-for-group group) method)))
13897     (funcall (gnus-get-function method 'request-scan) 
13898              (and group (gnus-group-real-name group)) (nth 1 method))))
13899
13900 (defun gnus-request-update-info (info method)
13901   "Request that METHOD update INFO."
13902   (when (gnus-check-backend-function 'request-update-info method)
13903     (funcall (gnus-get-function method 'request-update-info) 
13904              (gnus-group-real-name (gnus-info-group info)) 
13905              info (nth 1 method))))
13906
13907 (defun gnus-request-expire-articles (articles group &optional force)
13908   (let ((method (gnus-find-method-for-group group)))
13909     (funcall (gnus-get-function method 'request-expire-articles) 
13910              articles (gnus-group-real-name group) (nth 1 method)
13911              force)))
13912
13913 (defun gnus-request-move-article 
13914   (article group server accept-function &optional last)
13915   (let ((method (gnus-find-method-for-group group)))
13916     (funcall (gnus-get-function method 'request-move-article) 
13917              article (gnus-group-real-name group) 
13918              (nth 1 method) accept-function last)))
13919
13920 (defun gnus-request-accept-article (group &optional last)
13921   (let ((func (if (symbolp group) group
13922                 (car (gnus-find-method-for-group group)))))
13923     (funcall (intern (format "%s-request-accept-article" func))
13924              (if (stringp group) (gnus-group-real-name group) group)
13925              last)))
13926
13927 (defun gnus-request-replace-article (article group buffer)
13928   (let ((func (car (gnus-find-method-for-group group))))
13929     (funcall (intern (format "%s-request-replace-article" func))
13930              article (gnus-group-real-name group) buffer)))
13931
13932 (defun gnus-request-associate-buffer (group)
13933   (let ((method (gnus-find-method-for-group group)))
13934     (funcall (gnus-get-function method 'request-associate-buffer) 
13935              (gnus-group-real-name group))))
13936
13937 (defun gnus-request-restore-buffer (article group)
13938   "Request a new buffer restored to the state of ARTICLE."
13939   (let ((method (gnus-find-method-for-group group)))
13940     (funcall (gnus-get-function method 'request-restore-buffer) 
13941              article (gnus-group-real-name group) (nth 1 method))))
13942
13943 (defun gnus-request-create-group (group)
13944   (let ((method (gnus-find-method-for-group group)))
13945     (funcall (gnus-get-function method 'request-create-group) 
13946              (gnus-group-real-name group) (nth 1 method))))
13947
13948 (defun gnus-request-delete-group (group &optional force)
13949   (let ((method (gnus-find-method-for-group group)))
13950     (funcall (gnus-get-function method 'request-delete-group) 
13951              (gnus-group-real-name group) force (nth 1 method))))
13952
13953 (defun gnus-request-rename-group (group new-name)
13954   (let ((method (gnus-find-method-for-group group)))
13955     (funcall (gnus-get-function method 'request-rename-group) 
13956              (gnus-group-real-name group) 
13957              (gnus-group-real-name new-name) (nth 1 method))))
13958
13959 (defun gnus-post-method (group force-group-method)
13960   "Return the posting method based on GROUP and FORCE."
13961   (let ((group-method (if (stringp group)
13962                           (gnus-find-method-for-group group)
13963                         group)))
13964     (cond 
13965      ;; If the group-method is nil (which shouldn't happen) we use 
13966      ;; the default method.
13967      ((null group-method)
13968       gnus-select-method)
13969      ;; We want this group's method.
13970      (force-group-method group-method)
13971      ;; Override normal method.
13972      ((and gnus-post-method
13973            (or (gnus-method-option-p group-method 'post)
13974                (gnus-method-option-p group-method 'post-mail)))
13975       gnus-post-method)
13976      ;; Perhaps this is a mail group?
13977      ((and (not (gnus-member-of-valid 'post group))
13978            (not (gnus-method-option-p group-method 'post-mail)))
13979       group-method)
13980      ;; Use the normal select method.
13981      (t gnus-select-method))))
13982
13983 (defun gnus-member-of-valid (symbol group)
13984   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
13985   (memq symbol (assoc
13986                 (symbol-name (car (gnus-find-method-for-group group)))
13987                 gnus-valid-select-methods)))
13988
13989 (defun gnus-method-option-p (method option)
13990   "Return non-nil if select METHOD has OPTION as a parameter."
13991   (memq option (assoc (format "%s" (car method))
13992                       gnus-valid-select-methods)))
13993
13994 (defmacro gnus-server-equal (ss1 ss2)
13995   "Say whether two servers are equal."
13996   `(let ((s1 ,ss1)
13997          (s2 ,ss2))
13998      (or (equal s1 s2)
13999          (and (= (length s1) (length s2))
14000               (progn
14001                 (while (and s1 (member (car s1) s2))
14002                   (setq s1 (cdr s1)))
14003                 (null s1))))))
14004
14005 (defun gnus-server-extend-method (group method)
14006   ;; This function "extends" a virtual server.  If the server is
14007   ;; "hello", and the select method is ("hello" (my-var "something")) 
14008   ;; in the group "alt.alt", this will result in a new virtual server
14009   ;; called "helly+alt.alt".
14010   (let ((entry
14011          (gnus-copy-sequence 
14012           (if (equal (car method) "native") gnus-select-method
14013             (cdr (assoc (car method) gnus-server-alist))))))
14014     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14015     (nconc entry (cdr method))))
14016
14017 (defun gnus-find-method-for-group (group &optional info)
14018   "Find the select method that GROUP uses."
14019   (or gnus-override-method
14020       (and (not group)
14021            gnus-select-method)
14022       (let ((info (or info (gnus-get-info group)))
14023             method)
14024         (if (or (not info)
14025                 (not (setq method (gnus-info-method info))))
14026             (setq method gnus-select-method)
14027           (setq method
14028                 (cond ((stringp method)
14029                        (gnus-server-to-method method))
14030                       ((stringp (car method))
14031                        (gnus-server-extend-method group method))
14032                       (t
14033                        method))))
14034         (gnus-server-add-address method))))
14035
14036 (defun gnus-check-backend-function (func group)
14037   "Check whether GROUP supports function FUNC."
14038   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14039                   group)))
14040     (fboundp (intern (format "%s-%s" method func)))))
14041
14042 (defun gnus-methods-using (feature)
14043   "Find all methods that have FEATURE."
14044   (let ((valids gnus-valid-select-methods)
14045         outs)
14046     (while valids
14047       (if (memq feature (car valids)) 
14048           (setq outs (cons (car valids) outs)))
14049       (setq valids (cdr valids)))
14050     outs))
14051
14052 ;;; 
14053 ;;; Active & Newsrc File Handling
14054 ;;;
14055
14056 ;; Newsrc related functions.
14057 ;; Gnus internal format of gnus-newsrc-alist:
14058 ;; (("alt.general" 3 (1 . 1))
14059 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
14060 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
14061 ;; The first item is the group name; the second is the subscription
14062 ;; level; the third is either a range of a list of ranges of read
14063 ;; articles, the optional fourth element is a list of marked articles,
14064 ;; the optional fifth element is the select method.
14065 ;;
14066 ;; Gnus internal format of gnus-newsrc-hashtb:
14067 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
14068 ;; This is the entry for "alt.misc". The first element is the number
14069 ;; of unread articles in "alt.misc". The cdr of this entry is the
14070 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
14071 ;; trivial to remove or add new elements into gnus-newsrc-alist
14072 ;; without scanning the entire list.  So, to get the actual information
14073 ;; of "alt.misc", you'd say something like 
14074 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
14075 ;;
14076 ;; Gnus internal format of gnus-active-hashtb:
14077 ;; ((1 . 1))
14078 ;;  (5 . 10))
14079 ;;  (67 . 99)) ...)
14080 ;; The only element in each entry in this hash table is a range of
14081 ;; (possibly) available articles. (Articles in this range may have
14082 ;; been expired or canceled.)
14083 ;;
14084 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
14085 ;; ("alt.misc" "alt.test" "alt.general" ...)
14086
14087 (defun gnus-setup-news (&optional rawfile level)
14088   "Setup news information.
14089 If RAWFILE is non-nil, the .newsrc file will also be read.
14090 If LEVEL is non-nil, the news will be set up at level LEVEL."
14091   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14092     ;; Clear some variables to re-initialize news information.
14093     (if init (setq gnus-newsrc-alist nil 
14094                    gnus-active-hashtb nil))
14095
14096     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14097     (if init (gnus-read-newsrc-file rawfile))
14098
14099     ;; If we don't read the complete active file, we fill in the
14100     ;; hashtb here. 
14101     (if (or (null gnus-read-active-file)
14102             (eq gnus-read-active-file 'some))
14103         (gnus-update-active-hashtb-from-killed))
14104
14105     ;; Read the active file and create `gnus-active-hashtb'.
14106     ;; If `gnus-read-active-file' is nil, then we just create an empty
14107     ;; hash table.  The partial filling out of the hash table will be
14108     ;; done in `gnus-get-unread-articles'.
14109     (and gnus-read-active-file 
14110          (not level)
14111          (gnus-read-active-file))
14112
14113     (or gnus-active-hashtb
14114         (setq gnus-active-hashtb (make-vector 4095 0)))
14115
14116     ;; Initialize the cache.
14117     (when gnus-use-cache
14118       (gnus-cache-open))
14119
14120     ;; Possibly eval the dribble file.
14121     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14122
14123     (gnus-update-format-specifications)
14124
14125     ;; Find new newsgroups and treat them.
14126     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
14127              (gnus-check-server gnus-select-method))
14128         (gnus-find-new-newsgroups))
14129
14130     ;; Find the number of unread articles in each non-dead group.
14131     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14132       (gnus-get-unread-articles level))
14133
14134     (if (and init gnus-check-bogus-newsgroups 
14135              gnus-read-active-file (not level)
14136              (gnus-server-opened gnus-select-method))
14137         (gnus-check-bogus-newsgroups))))
14138
14139 (defun gnus-find-new-newsgroups ()
14140   "Search for new newsgroups and add them.
14141 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14142 The `-n' option line from .newsrc is respected."
14143   (interactive)
14144   (or (gnus-check-first-time-used)
14145       (if (or (consp gnus-check-new-newsgroups)
14146               (eq gnus-check-new-newsgroups 'ask-server))
14147           (gnus-ask-server-for-new-groups)
14148         (let ((groups 0)
14149               group new-newsgroups)
14150           (gnus-message 5 "Looking for new newsgroups...")
14151           (or gnus-have-read-active-file (gnus-read-active-file))
14152           (setq gnus-newsrc-last-checked-date (current-time-string))
14153           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
14154           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14155           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14156           (mapatoms
14157            (lambda (sym)
14158              (if (or (null (setq group (symbol-name sym)))
14159                      (not (boundp sym))
14160                      (null (symbol-value sym))
14161                      (gnus-gethash group gnus-killed-hashtb)
14162                      (gnus-gethash group gnus-newsrc-hashtb))
14163                  ()
14164                (let ((do-sub (gnus-matches-options-n group)))
14165                  (cond 
14166                   ((eq do-sub 'subscribe)
14167                    (setq groups (1+ groups))
14168                    (gnus-sethash group group gnus-killed-hashtb)
14169                    (funcall gnus-subscribe-options-newsgroup-method group))
14170                   ((eq do-sub 'ignore)
14171                    nil)
14172                   (t
14173                    (setq groups (1+ groups))
14174                    (gnus-sethash group group gnus-killed-hashtb)
14175                    (if gnus-subscribe-hierarchical-interactive
14176                        (setq new-newsgroups (cons group new-newsgroups))
14177                      (funcall gnus-subscribe-newsgroup-method group)))))))
14178            gnus-active-hashtb)
14179           (if new-newsgroups 
14180               (gnus-subscribe-hierarchical-interactive new-newsgroups))
14181           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14182           (if (> groups 0)
14183               (gnus-message 6 "%d new newsgroup%s arrived." 
14184                             groups (if (> groups 1) "s have" " has"))
14185             (gnus-message 6 "No new newsgroups."))))))
14186
14187 (defun gnus-matches-options-n (group)
14188   ;; Returns `subscribe' if the group is to be uncoditionally
14189   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14190   ;; no match for the group.
14191
14192   ;; First we check the two user variables.
14193   (cond
14194    ((and gnus-options-subscribe
14195          (string-match gnus-options-subscribe group))
14196     'subscribe)
14197    ((and gnus-auto-subscribed-groups 
14198          (string-match gnus-auto-subscribed-groups group))
14199     'subscribe)
14200    ((and gnus-options-not-subscribe
14201          (string-match gnus-options-not-subscribe group))
14202     'ignore)
14203    ;; Then we go through the list that was retrieved from the .newsrc
14204    ;; file.  This list has elements on the form 
14205    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14206    ;; is in the reverse order of the options line) is returned.
14207    (t
14208     (let ((regs gnus-newsrc-options-n))
14209       (while (and regs
14210                   (not (string-match (car (car regs)) group)))
14211         (setq regs (cdr regs)))
14212       (and regs (cdr (car regs)))))))
14213
14214 (defun gnus-ask-server-for-new-groups ()
14215   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
14216          (methods (cons gnus-select-method 
14217                         (append
14218                          (and (consp gnus-check-new-newsgroups)
14219                               gnus-check-new-newsgroups)
14220                          gnus-secondary-select-methods)))
14221          (groups 0)
14222          (new-date (current-time-string))
14223          (hashtb (gnus-make-hashtable 100))
14224          group new-newsgroups got-new method)
14225     ;; Go through both primary and secondary select methods and
14226     ;; request new newsgroups.  
14227     (while methods
14228       (setq method (gnus-server-get-method nil (car methods)))
14229       (and (gnus-check-server method)
14230            (gnus-request-newgroups date method)
14231            (save-excursion
14232              (setq got-new t)
14233              (set-buffer nntp-server-buffer)
14234              ;; Enter all the new groups in a hashtable.
14235              (gnus-active-to-gnus-format method hashtb 'ignore)))
14236       (setq methods (cdr methods)))
14237     (and got-new (setq gnus-newsrc-last-checked-date new-date))
14238     ;; Now all new groups from all select methods are in `hashtb'.
14239     (mapatoms
14240      (lambda (group-sym)
14241        (setq group (symbol-name group-sym))
14242        (if (or (null group)
14243                (null (symbol-value group-sym))
14244                (gnus-gethash group gnus-newsrc-hashtb)
14245                (member group gnus-zombie-list)
14246                (member group gnus-killed-list))
14247            ;; The group is already known.
14248            ()
14249          (and (symbol-value group-sym)
14250               (gnus-set-active group (symbol-value group-sym)))
14251          (let ((do-sub (gnus-matches-options-n group)))
14252            (cond ((eq do-sub 'subscribe)
14253                   (setq groups (1+ groups))
14254                   (gnus-sethash group group gnus-killed-hashtb)
14255                   (funcall 
14256                    gnus-subscribe-options-newsgroup-method group))
14257                  ((eq do-sub 'ignore)
14258                   nil)
14259                  (t
14260                   (setq groups (1+ groups))
14261                   (gnus-sethash group group gnus-killed-hashtb)
14262                   (if gnus-subscribe-hierarchical-interactive
14263                       (setq new-newsgroups (cons group new-newsgroups))
14264                     (funcall gnus-subscribe-newsgroup-method group)))))))
14265      hashtb)
14266     (if new-newsgroups 
14267         (gnus-subscribe-hierarchical-interactive new-newsgroups))
14268     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14269     (if (> groups 0)
14270         (gnus-message 6 "%d new newsgroup%s arrived." 
14271                       groups (if (> groups 1) "s have" " has")))
14272     got-new))
14273
14274 (defun gnus-check-first-time-used ()
14275   (if (or (> (length gnus-newsrc-alist) 1)
14276           (file-exists-p gnus-startup-file)
14277           (file-exists-p (concat gnus-startup-file ".el"))
14278           (file-exists-p (concat gnus-startup-file ".eld")))
14279       nil
14280     (gnus-message 6 "First time user; subscribing you to default groups")
14281     (or gnus-have-read-active-file (gnus-read-active-file))
14282     (setq gnus-newsrc-last-checked-date (current-time-string))
14283     (let ((groups gnus-default-subscribed-newsgroups)
14284           group)
14285       (if (eq groups t)
14286           nil
14287         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
14288         (mapatoms
14289          (lambda (sym)
14290            (if (null (setq group (symbol-name sym)))
14291                ()
14292              (let ((do-sub (gnus-matches-options-n group)))
14293                (cond 
14294                 ((eq do-sub 'subscribe)
14295                  (gnus-sethash group group gnus-killed-hashtb)
14296                  (funcall gnus-subscribe-options-newsgroup-method group))
14297                 ((eq do-sub 'ignore)
14298                  nil)
14299                 (t
14300                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
14301          gnus-active-hashtb)
14302         (while groups
14303           (if (gnus-active (car groups))
14304               (gnus-group-change-level 
14305                (car groups) gnus-level-default-subscribed gnus-level-killed))
14306           (setq groups (cdr groups)))
14307         (gnus-group-make-help-group)
14308         (and gnus-novice-user
14309              (gnus-message 7 "`A k' to list killed groups"))))))
14310
14311 (defun gnus-subscribe-group (group previous &optional method)
14312   (gnus-group-change-level 
14313    (if method
14314        (list t group gnus-level-default-subscribed nil nil method)
14315      group) 
14316    gnus-level-default-subscribed gnus-level-killed previous t))
14317
14318 ;; `gnus-group-change-level' is the fundamental function for changing
14319 ;; subscription levels of newsgroups.  This might mean just changing
14320 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
14321 ;; again, which subscribes/unsubscribes a group, which is equally
14322 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
14323 ;; from 8-9 to 1-7 means that you remove the group from the list of
14324 ;; killed (or zombie) groups and add them to the (kinda) subscribed
14325 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
14326 ;; which is trivial.
14327 ;; ENTRY can either be a string (newsgroup name) or a list (if
14328 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
14329 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
14330 ;; entries. 
14331 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
14332 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
14333 ;; after. 
14334 (defun gnus-group-change-level (entry level &optional oldlevel
14335                                       previous fromkilled)
14336   (let (group info active num)
14337     ;; Glean what info we can from the arguments
14338     (if (consp entry)
14339         (if fromkilled (setq group (nth 1 entry))
14340           (setq group (car (nth 2 entry))))
14341       (setq group entry))
14342     (if (and (stringp entry)
14343              oldlevel 
14344              (< oldlevel gnus-level-zombie))
14345         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
14346     (if (and (not oldlevel)
14347              (consp entry))
14348         (setq oldlevel (car (cdr (nth 2 entry)))))
14349     (if (stringp previous)
14350         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
14351
14352     (if (and (>= oldlevel gnus-level-zombie)
14353              (gnus-gethash group gnus-newsrc-hashtb))
14354         ;; We are trying to subscribe a group that is already
14355         ;; subscribed. 
14356         ()                              ; Do nothing. 
14357
14358       (or (gnus-ephemeral-group-p group)
14359           (gnus-dribble-enter
14360            (format "(gnus-group-change-level %S %S %S %S %S)" 
14361                    group level oldlevel (car (nth 2 previous)) fromkilled)))
14362     
14363       ;; Then we remove the newgroup from any old structures, if needed.
14364       ;; If the group was killed, we remove it from the killed or zombie
14365       ;; list.  If not, and it is in fact going to be killed, we remove
14366       ;; it from the newsrc hash table and assoc.
14367       (cond ((>= oldlevel gnus-level-zombie)
14368              (if (= oldlevel gnus-level-zombie)
14369                  (setq gnus-zombie-list (delete group gnus-zombie-list))
14370                (setq gnus-killed-list (delete group gnus-killed-list))))
14371             (t
14372              (if (and (>= level gnus-level-zombie)
14373                       entry)
14374                  (progn
14375                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
14376                    (if (nth 3 entry)
14377                        (setcdr (gnus-gethash (car (nth 3 entry))
14378                                              gnus-newsrc-hashtb)
14379                                (cdr entry)))
14380                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
14381
14382       ;; Finally we enter (if needed) the list where it is supposed to
14383       ;; go, and change the subscription level.  If it is to be killed,
14384       ;; we enter it into the killed or zombie list.
14385       (cond ((>= level gnus-level-zombie)
14386              ;; Remove from the hash table.
14387              (gnus-sethash group nil gnus-newsrc-hashtb)
14388              ;; We do not enter foreign groups into the list of dead
14389              ;; groups.  
14390              (unless (gnus-group-foreign-p group)
14391                (if (= level gnus-level-zombie)
14392                    (setq gnus-zombie-list (cons group gnus-zombie-list))
14393                  (setq gnus-killed-list (cons group gnus-killed-list)))))
14394             (t
14395              ;; If the list is to be entered into the newsrc assoc, and
14396              ;; it was killed, we have to create an entry in the newsrc
14397              ;; hashtb format and fix the pointers in the newsrc assoc.
14398              (if (>= oldlevel gnus-level-zombie)
14399                  (progn
14400                    (if (listp entry)
14401                        (progn
14402                          (setq info (cdr entry))
14403                          (setq num (car entry)))
14404                      (setq active (gnus-active group))
14405                      (setq num 
14406                            (if active (- (1+ (cdr active)) (car active)) t))
14407                      ;; Check whether the group is foreign.  If so, the
14408                      ;; foreign select method has to be entered into the
14409                      ;; info. 
14410                      (let ((method (gnus-group-method-name group)))
14411                        (if (eq method gnus-select-method)
14412                            (setq info (list group level nil))
14413                          (setq info (list group level nil nil method)))))
14414                    (or previous 
14415                        (setq previous 
14416                              (let ((p gnus-newsrc-alist))
14417                                (while (cdr (cdr p))
14418                                  (setq p (cdr p)))
14419                                p)))
14420                    (setq entry (cons info (cdr (cdr previous))))
14421                    (if (cdr previous)
14422                        (progn
14423                          (setcdr (cdr previous) entry)
14424                          (gnus-sethash group (cons num (cdr previous)) 
14425                                        gnus-newsrc-hashtb))
14426                      (setcdr previous entry)
14427                      (gnus-sethash group (cons num previous)
14428                                    gnus-newsrc-hashtb))
14429                    (if (cdr entry)
14430                        (setcdr (gnus-gethash (car (car (cdr entry)))
14431                                              gnus-newsrc-hashtb)
14432                                entry)))
14433                ;; It was alive, and it is going to stay alive, so we
14434                ;; just change the level and don't change any pointers or
14435                ;; hash table entries.
14436                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
14437
14438 (defun gnus-kill-newsgroup (newsgroup)
14439   "Obsolete function.  Kills a newsgroup."
14440   (gnus-group-change-level
14441    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
14442
14443 (defun gnus-check-bogus-newsgroups (&optional confirm)
14444   "Remove bogus newsgroups.
14445 If CONFIRM is non-nil, the user has to confirm the deletion of every
14446 newsgroup." 
14447   (let ((newsrc (cdr gnus-newsrc-alist))
14448         bogus group entry info)
14449     (gnus-message 5 "Checking bogus newsgroups...")
14450     (unless gnus-have-read-active-file 
14451       (gnus-read-active-file))
14452     (when (member gnus-select-method gnus-have-read-active-file)
14453       ;; Find all bogus newsgroup that are subscribed.
14454       (while newsrc
14455         (setq info (pop newsrc)
14456               group (gnus-info-group info))
14457         (unless (or (gnus-active group) ; Active
14458                     (gnus-info-method info) ; Foreign
14459                     (and confirm
14460                          (not (gnus-y-or-n-p
14461                                (format "Remove bogus newsgroup: %s " group)))))
14462           ;; Found a bogus newsgroup.
14463           (push group bogus)))
14464       ;; Remove all bogus subscribed groups by first killing them, and
14465       ;; then removing them from the list of killed groups.
14466       (while bogus
14467         (when (setq entry (gnus-gethash (setq group (pop bogus))
14468                                         gnus-newsrc-hashtb))
14469           (gnus-group-change-level entry gnus-level-killed)
14470           (setq gnus-killed-list (delete group gnus-killed-list))))
14471       ;; Then we remove all bogus groups from the list of killed and
14472       ;; zombie groups.  They are are removed without confirmation.
14473       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
14474             killed)
14475         (while dead-lists
14476           (setq killed (symbol-value (car dead-lists)))
14477           (while killed
14478             (unless (gnus-active (setq group (pop killed)))
14479               ;; The group is bogus.
14480               ;; !!!Slow as hell.
14481               (set (car dead-lists)
14482                    (delete group (symbol-value (car dead-lists))))))
14483           (setq dead-lists (cdr dead-lists))))
14484       (gnus-message 5 "Checking bogus newsgroups...done"))))
14485
14486 (defun gnus-check-duplicate-killed-groups ()
14487   "Remove duplicates from the list of killed groups."
14488   (interactive)
14489   (let ((killed gnus-killed-list))
14490     (while killed
14491       (gnus-message 9 "%d" (length killed))
14492       (setcdr killed (delete (car killed) (cdr killed)))
14493       (setq killed (cdr killed)))))
14494
14495 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
14496 ;; and compute how many unread articles there are in each group.
14497 (defun gnus-get-unread-articles (&optional level) 
14498   (let* ((newsrc (cdr gnus-newsrc-alist))
14499          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
14500          (foreign-level
14501           (min 
14502            (cond ((and gnus-activate-foreign-newsgroups 
14503                        (not (numberp gnus-activate-foreign-newsgroups)))
14504                   (1+ gnus-level-subscribed))
14505                  ((numberp gnus-activate-foreign-newsgroups)
14506                   gnus-activate-foreign-newsgroups)
14507                  (t 0))
14508            level))
14509          (update
14510           (fboundp (intern (format "%s-request-update-info"
14511                                    (car gnus-select-method)))))
14512          info group active virtuals method fmethod)
14513     (gnus-message 5 "Checking new news...")
14514
14515     (while newsrc
14516       (setq info (car newsrc)
14517             group (gnus-info-group info)
14518             active (gnus-active group))
14519
14520       ;; Check newsgroups.  If the user doesn't want to check them, or
14521       ;; they can't be checked (for instance, if the news server can't
14522       ;; be reached) we just set the number of unread articles in this
14523       ;; newsgroup to t.  This means that Gnus thinks that there are
14524       ;; unread articles, but it has no idea how many.
14525       (if (and (setq method (gnus-info-method info))
14526                (not (gnus-server-equal
14527                      gnus-select-method
14528                      (prog1
14529                          (setq fmethod (gnus-server-get-method nil method))
14530                        ;; We do this here because it would be awkward
14531                        ;; to do it anywhere else.  Hell, it's pretty
14532                        ;; awkward here as well, but at least it's
14533                        ;; reasonably efficient. 
14534                        (and (<= (gnus-info-level info) foreign-level)
14535                             (gnus-request-update-info info method)))))
14536                (not (gnus-secondary-method-p method)))
14537           ;; These groups are foreign.  Check the level.
14538           (if (<= (gnus-info-level info) foreign-level)
14539               (setq active (gnus-activate-group (gnus-info-group info) 'scan)))
14540
14541         ;; These groups are native or secondary. 
14542         (if (<= (gnus-info-level info) level)
14543             (progn
14544               (if (and update (not method))
14545                   (progn
14546                     ;; Allow updating of native groups as well, even
14547                     ;; though that's pretty unlikely.
14548                     (gnus-request-update-info info gnus-select-method)
14549                     (setq active (gnus-activate-group 
14550                                   (gnus-info-group info) 'scan)))
14551                 (or gnus-read-active-file
14552                     (setq active (gnus-activate-group 
14553                                   (gnus-info-group info) 'scan)))))))
14554       
14555       (if active
14556           (gnus-get-unread-articles-in-group info active)
14557         ;; The group couldn't be reached, so we nix out the number of
14558         ;; unread articles and stuff.
14559         (gnus-set-active group nil)
14560         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
14561       
14562       (setq newsrc (cdr newsrc)))
14563
14564     (gnus-message 5 "Checking new news...done")))
14565
14566 ;; Create a hash table out of the newsrc alist.  The `car's of the
14567 ;; alist elements are used as keys.
14568 (defun gnus-make-hashtable-from-newsrc-alist ()
14569   (let ((alist gnus-newsrc-alist)
14570         (ohashtb gnus-newsrc-hashtb)
14571         prev)
14572     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
14573     (setq alist 
14574           (setq prev (setq gnus-newsrc-alist 
14575                            (if (equal (car (car gnus-newsrc-alist))
14576                                       "dummy.group")
14577                                gnus-newsrc-alist
14578                              (cons (list "dummy.group" 0 nil) alist)))))
14579     (while alist
14580       (gnus-sethash 
14581        (car (car alist)) 
14582        (cons (and ohashtb (car (gnus-gethash (car (car alist)) ohashtb))) 
14583              prev)
14584        gnus-newsrc-hashtb)
14585       (setq prev alist
14586             alist (cdr alist)))))
14587
14588 (defun gnus-make-hashtable-from-killed ()
14589   "Create a hash table from the killed and zombie lists."
14590   (let ((lists '(gnus-killed-list gnus-zombie-list))
14591         list)
14592     (setq gnus-killed-hashtb 
14593           (gnus-make-hashtable 
14594            (+ (length gnus-killed-list) (length gnus-zombie-list))))
14595     (while lists
14596       (setq list (symbol-value (car lists)))
14597       (setq lists (cdr lists))
14598       (while list
14599         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
14600         (setq list (cdr list))))))
14601
14602 (defun gnus-get-unread-articles-in-group (info active)
14603   (let* ((range (gnus-info-read info))
14604          (num 0)
14605          (marked (gnus-info-marks info)))
14606     ;; If a cache is present, we may have to alter the active info.
14607     (and gnus-use-cache
14608          (gnus-cache-possibly-alter-active (gnus-info-group info) active))
14609     ;; Modify the list of read articles according to what articles 
14610     ;; are available; then tally the unread articles and add the
14611     ;; number to the group hash table entry.
14612     (cond 
14613      ((zerop (cdr active))
14614       (setq num 0))
14615      ((not range)
14616       (setq num (- (1+ (cdr active)) (car active))))
14617      ((not (listp (cdr range)))
14618       ;; Fix a single (num . num) range according to the
14619       ;; active hash table.
14620       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
14621       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
14622       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
14623       ;; Compute number of unread articles.
14624       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
14625      (t
14626       ;; The read list is a list of ranges.  Fix them according to
14627       ;; the active hash table.
14628       ;; First peel off any elements that are below the lower
14629       ;; active limit. 
14630       (while (and (cdr range) 
14631                   (>= (car active) 
14632                       (or (and (atom (car (cdr range))) (car (cdr range)))
14633                           (car (car (cdr range))))))
14634         (if (numberp (car range))
14635             (setcar range 
14636                     (cons (car range) 
14637                           (or (and (numberp (car (cdr range)))
14638                                    (car (cdr range))) 
14639                               (cdr (car (cdr range))))))
14640           (setcdr (car range) 
14641                   (or (and (numberp (nth 1 range)) (nth 1 range))
14642                       (cdr (car (cdr range))))))
14643         (setcdr range (cdr (cdr range))))
14644       ;; Adjust the first element to be the same as the lower limit. 
14645       (if (and (not (atom (car range))) 
14646                (< (cdr (car range)) (car active)))
14647           (setcdr (car range) (1- (car active))))
14648       ;; Then we want to peel off any elements that are higher
14649       ;; than the upper active limit.  
14650       (let ((srange range))
14651         ;; Go past all legal elements.
14652         (while (and (cdr srange) 
14653                     (<= (or (and (atom (car (cdr srange)))
14654                                  (car (cdr srange)))
14655                             (car (car (cdr srange)))) (cdr active)))
14656           (setq srange (cdr srange)))
14657         (if (cdr srange)
14658             ;; Nuke all remaining illegal elements.
14659             (setcdr srange nil))
14660
14661         ;; Adjust the final element.
14662         (if (and (not (atom (car srange)))
14663                  (> (cdr (car srange)) (cdr active)))
14664             (setcdr (car srange) (cdr active))))
14665       ;; Compute the number of unread articles.
14666       (while range
14667         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
14668                                     (cdr (car range))))
14669                             (or (and (atom (car range)) (car range))
14670                                 (car (car range))))))
14671         (setq range (cdr range)))
14672       (setq num (max 0 (- (cdr active) num)))))
14673     ;; Set the number of unread articles.
14674     (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num)
14675     num))
14676
14677 (defun gnus-activate-group (group &optional scan)
14678   ;; Check whether a group has been activated or not.
14679   ;; If SCAN, request a scan of that group as well.
14680   (let ((method (gnus-find-method-for-group group))
14681         active)
14682     (and (gnus-check-server method)
14683          ;; We escape all bugs and quit here to make it possible to
14684          ;; continue if a group is so out-there that it reports bugs
14685          ;; and stuff.
14686          (progn
14687            (and scan
14688                 (gnus-check-backend-function 'request-scan (car method))
14689                 (gnus-request-scan group method))
14690            t)
14691          (condition-case ()
14692              (gnus-request-group group)
14693         ;   (error nil)
14694            (quit nil))
14695          (save-excursion
14696            (set-buffer nntp-server-buffer)
14697            (goto-char (point-min))
14698            ;; Parse the result we got from `gnus-request-group'.
14699            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
14700                 (progn
14701                   (goto-char (match-beginning 1))
14702                   (gnus-set-active 
14703                    group (setq active (cons (read (current-buffer))
14704                                             (read (current-buffer)))))
14705                   ;; Return the new active info.
14706                   active))))))
14707
14708 (defun gnus-update-read-articles (group unread)
14709   "Update the list of read and ticked articles in GROUP using the
14710 UNREAD and TICKED lists.
14711 Note: UNSELECTED has to be sorted over `<'.
14712 Returns whether the updating was successful."
14713   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
14714          (entry (gnus-gethash group gnus-newsrc-hashtb))
14715          (info (nth 2 entry))
14716          (marked (gnus-info-marks info))
14717          (prev 1)
14718          (unread (sort (copy-sequence unread) '<))
14719          read)
14720     (if (or (not info) (not active))
14721         ;; There is no info on this group if it was, in fact,
14722         ;; killed.  Gnus stores no information on killed groups, so
14723         ;; there's nothing to be done. 
14724         ;; One could store the information somewhere temporarily,
14725         ;; perhaps...  Hmmm... 
14726         ()
14727       ;; Remove any negative articles numbers.
14728       (while (and unread (< (car unread) 0))
14729         (setq unread (cdr unread)))
14730       ;; Remove any expired article numbers
14731       (while (and unread (< (car unread) (car active)))
14732         (setq unread (cdr unread)))
14733       ;; Compute the ranges of read articles by looking at the list of
14734       ;; unread articles.  
14735       (while unread
14736         (if (/= (car unread) prev)
14737             (setq read (cons (if (= prev (1- (car unread))) prev
14738                                (cons prev (1- (car unread)))) read)))
14739         (setq prev (1+ (car unread)))
14740         (setq unread (cdr unread)))
14741       (when (<= prev (cdr active))
14742         (setq read (cons (cons prev (cdr active)) read)))
14743       ;; Enter this list into the group info.
14744       (gnus-info-set-read 
14745        info (if (> (length read) 1) (nreverse read) read))
14746       ;; Set the number of unread articles in gnus-newsrc-hashtb.
14747       (gnus-get-unread-articles-in-group info (gnus-active group))
14748       t)))
14749
14750 (defun gnus-make-articles-unread (group articles)
14751   "Mark ARTICLES in GROUP as unread."
14752   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
14753                           (gnus-gethash (gnus-group-real-name group)
14754                                         gnus-newsrc-hashtb))))
14755          (ranges (gnus-info-read info))
14756          news article)
14757     (while articles
14758       (when (gnus-member-of-range 
14759              (setq article (pop articles)) ranges)
14760         (setq news (cons article news))))
14761     (when news
14762       (gnus-info-set-read 
14763        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
14764       (gnus-group-update-group group t))))
14765
14766 ;; Enter all dead groups into the hashtb.
14767 (defun gnus-update-active-hashtb-from-killed ()
14768   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
14769         (lists (list gnus-killed-list gnus-zombie-list))
14770         killed)
14771     (while lists
14772       (setq killed (car lists))
14773       (while killed
14774         (gnus-sethash (car killed) nil hashtb)
14775         (setq killed (cdr killed)))
14776       (setq lists (cdr lists)))))
14777
14778 ;; Get the active file(s) from the backend(s).
14779 (defun gnus-read-active-file ()
14780   (gnus-group-set-mode-line)
14781   (let ((methods (if (gnus-check-server gnus-select-method)
14782                      ;; The native server is available.
14783                      (cons gnus-select-method gnus-secondary-select-methods)
14784                    ;; The native server is down, so we just do the
14785                    ;; secondary ones.   
14786                    gnus-secondary-select-methods))
14787         list-type)
14788     (setq gnus-have-read-active-file nil)
14789     (save-excursion
14790       (set-buffer nntp-server-buffer)
14791       (while methods
14792         (let* ((method (gnus-server-get-method nil (car methods)))
14793                (where (nth 1 method))
14794                (mesg (format "Reading active file%s via %s..."
14795                              (if (and where (not (zerop (length where))))
14796                                  (concat " from " where) "")
14797                              (car method))))
14798           (gnus-message 5 mesg)
14799           (if (not (gnus-check-server method))
14800               ()
14801             ;; Request that the backend scan its incoming messages.
14802             (and (gnus-check-backend-function 'request-scan (car method))
14803                  (gnus-request-scan nil method))
14804             (cond 
14805              ((and (eq gnus-read-active-file 'some)
14806                    (gnus-check-backend-function 'retrieve-groups (car method)))
14807               (let ((newsrc (cdr gnus-newsrc-alist))
14808                     (gmethod (gnus-server-get-method nil method))
14809                     groups)
14810                 (while newsrc
14811                   (and (gnus-server-equal 
14812                         (gnus-find-method-for-group 
14813                          (car (car newsrc)) (car newsrc))
14814                         gmethod)
14815                        (setq groups (cons (gnus-group-real-name 
14816                                            (car (car newsrc))) groups)))
14817                   (setq newsrc (cdr newsrc)))
14818                 (gnus-check-server method)
14819                 (setq list-type (gnus-retrieve-groups groups method))
14820                 (cond 
14821                  ((not list-type)
14822                   (gnus-message 
14823                    1 "Cannot read partial active file from %s server." 
14824                    (car method))
14825                   (ding)
14826                   (sit-for 2))
14827                  ((eq list-type 'active)
14828                   (gnus-active-to-gnus-format method gnus-active-hashtb))
14829                  (t
14830                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
14831              (t
14832               (if (not (gnus-request-list method))
14833                   (progn
14834                     (gnus-message 1 "Cannot read active file from %s server." 
14835                                   (car method))
14836                     (ding))
14837                 (gnus-active-to-gnus-format method)
14838                 ;; We mark this active file as read.
14839                 (setq gnus-have-read-active-file
14840                       (cons method gnus-have-read-active-file))
14841                 (gnus-message 5 "%sdone" mesg))))))
14842         (setq methods (cdr methods))))))
14843
14844 ;; Read an active file and place the results in `gnus-active-hashtb'.
14845 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
14846   (unless method
14847     (setq method gnus-select-method))
14848   (let ((cur (current-buffer))
14849         (hashtb (or hashtb 
14850                     (if (and gnus-active-hashtb 
14851                              (not (equal method gnus-select-method)))
14852                         gnus-active-hashtb
14853                       (setq gnus-active-hashtb
14854                             (if (equal method gnus-select-method)
14855                                 (gnus-make-hashtable 
14856                                  (count-lines (point-min) (point-max)))
14857                               (gnus-make-hashtable 4096))))))
14858         (flag-hashtb (gnus-make-hashtable 60)))
14859     ;; Delete unnecessary lines.
14860     (goto-char (point-min))
14861     (while (search-forward "\nto." nil t)
14862       (delete-region (1+ (match-beginning 0)) 
14863                      (progn (forward-line 1) (point))))
14864     (or (string= gnus-ignored-newsgroups "")
14865         (progn
14866           (goto-char (point-min))
14867           (delete-matching-lines gnus-ignored-newsgroups)))
14868     ;; Make the group names readable as a lisp expression even if they
14869     ;; contain special characters.
14870     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
14871     (goto-char (point-max))
14872     (while (re-search-backward "[][';?()#]" nil t)
14873       (insert ?\\))
14874     ;; If these are groups from a foreign select method, we insert the
14875     ;; group prefix in front of the group names. 
14876     (and method (not (gnus-server-equal
14877                       (gnus-server-get-method nil method)
14878                       (gnus-server-get-method nil gnus-select-method)))
14879          (let ((prefix (gnus-group-prefixed-name "" method)))
14880            (goto-char (point-min))
14881            (while (and (not (eobp))
14882                        (progn (insert prefix)
14883                               (zerop (forward-line 1)))))))
14884     ;; Store the active file in a hash table.
14885     (goto-char (point-min))
14886     (if (string-match "%[oO]" gnus-group-line-format)
14887         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
14888         ;; If we want information on moderated groups, we use this
14889         ;; loop...   
14890         (let* ((mod-hashtb (make-vector 7 0))
14891                (m (intern "m" mod-hashtb))
14892                group max min)
14893           (while (not (eobp))
14894             (condition-case nil
14895                 (progn
14896                   (narrow-to-region (point) (gnus-point-at-eol))
14897                   (setq group (let ((obarray hashtb)) (read cur)))
14898                   (if (and (numberp (setq max (read cur)))
14899                            (numberp (setq min (read cur)))
14900                            (progn 
14901                              (skip-chars-forward " \t")
14902                              (not
14903                               (or (= (following-char) ?=)
14904                                   (= (following-char) ?x)
14905                                   (= (following-char) ?j)))))
14906                       (set group (cons min max))
14907                     (set group nil))
14908                   ;; Enter moderated groups into a list.
14909                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
14910                       (setq gnus-moderated-list 
14911                             (cons (symbol-name group) gnus-moderated-list))))
14912               (error 
14913                (and group
14914                     (symbolp group)
14915                     (set group nil))))
14916             (widen)
14917             (forward-line 1)))
14918       ;; And if we do not care about moderation, we use this loop,
14919       ;; which is faster.
14920       (let (group max min)
14921         (while (not (eobp))
14922           (condition-case ()
14923               (progn
14924                 (narrow-to-region (point) (gnus-point-at-eol))
14925                 ;; group gets set to a symbol interned in the hash table
14926                 ;; (what a hack!!) - jwz
14927                 (setq group (let ((obarray hashtb)) (read cur)))
14928                 (if (and (numberp (setq max (read cur)))
14929                          (numberp (setq min (read cur)))
14930                          (progn 
14931                            (skip-chars-forward " \t")
14932                            (not
14933                             (or (= (following-char) ?=)
14934                                 (= (following-char) ?x)
14935                                 (= (following-char) ?j)))))
14936                     (set group (cons min max))
14937                   (set group nil)))
14938             (error 
14939              (progn 
14940                (and group
14941                     (symbolp group)
14942                     (set group nil))
14943                (or ignore-errors
14944                    (gnus-message 3 "Warning - illegal active: %s"
14945                                  (buffer-substring 
14946                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
14947           (widen)
14948           (forward-line 1))))))
14949
14950 (defun gnus-groups-to-gnus-format (method &optional hashtb)
14951   ;; Parse a "groups" active file.
14952   (let ((cur (current-buffer))
14953         (hashtb (or hashtb 
14954                     (if (and method gnus-active-hashtb)
14955                         gnus-active-hashtb
14956                       (setq gnus-active-hashtb
14957                             (gnus-make-hashtable 
14958                              (count-lines (point-min) (point-max)))))))
14959         (prefix (and method 
14960                      (not (gnus-server-equal
14961                            (gnus-server-get-method nil method)
14962                            (gnus-server-get-method nil gnus-select-method)))
14963                      (gnus-group-prefixed-name "" method))))
14964
14965     (goto-char (point-min))
14966     ;; We split this into to separate loops, one with the prefix
14967     ;; and one without to speed the reading up somewhat.
14968     (if prefix
14969         (let (min max opoint group)
14970           (while (not (eobp))
14971             (condition-case ()
14972                 (progn
14973                   (read cur) (read cur)
14974                   (setq min (read cur)
14975                         max (read cur)
14976                         opoint (point))
14977                   (skip-chars-forward " \t")
14978                   (insert prefix)
14979                   (goto-char opoint)
14980                   (set (let ((obarray hashtb)) (read cur)) 
14981                        (cons min max)))
14982               (error (and group (symbolp group) (set group nil))))
14983             (forward-line 1)))
14984       (let (min max group)
14985         (while (not (eobp))
14986           (condition-case ()
14987               (if (= (following-char) ?2)
14988                   (progn
14989                     (read cur) (read cur)
14990                     (setq min (read cur)
14991                           max (read cur))
14992                     (set (setq group (let ((obarray hashtb)) (read cur)))
14993                          (cons min max))))
14994             (error (and group (symbolp group) (set group nil))))
14995           (forward-line 1))))))
14996
14997 (defun gnus-read-newsrc-file (&optional force)
14998   "Read startup file.
14999 If FORCE is non-nil, the .newsrc file is read."
15000   ;; Reset variables that might be defined in the .newsrc.eld file.
15001   (let ((variables gnus-variable-list))
15002     (while variables
15003       (set (car variables) nil)
15004       (setq variables (cdr variables))))
15005   (let* ((newsrc-file gnus-current-startup-file)
15006          (quick-file (concat newsrc-file ".el")))
15007     (save-excursion
15008       ;; We always load the .newsrc.eld file.  If always contains
15009       ;; much information that can not be gotten from the .newsrc
15010       ;; file (ticked articles, killed groups, foreign methods, etc.)
15011       (gnus-read-newsrc-el-file quick-file)
15012  
15013       (if (or force
15014               (and (file-newer-than-file-p newsrc-file quick-file)
15015                    (file-newer-than-file-p newsrc-file 
15016                                            (concat quick-file "d")))
15017               (not gnus-newsrc-alist))
15018           ;; We read the .newsrc file.  Note that if there if a
15019           ;; .newsrc.eld file exists, it has already been read, and
15020           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15021           ;; the .newsrc file, Gnus will only use the information it
15022           ;; can find there for changing the data already read -
15023           ;; ie. reading the .newsrc file will not trash the data
15024           ;; already read (except for read articles).
15025           (save-excursion
15026             (gnus-message 5 "Reading %s..." newsrc-file)
15027             (set-buffer (find-file-noselect newsrc-file))
15028             (buffer-disable-undo (current-buffer))
15029             (gnus-newsrc-to-gnus-format)
15030             (kill-buffer (current-buffer))
15031             (gnus-message 5 "Reading %s...done" newsrc-file)))
15032
15033       ;; Read any slave files.
15034       (or gnus-slave
15035           (gnus-master-read-slave-newsrc)))))
15036
15037 (defun gnus-read-newsrc-el-file (file)
15038   (let ((ding-file (concat file "d")))
15039     ;; We always, always read the .eld file.
15040     (gnus-message 5 "Reading %s..." ding-file)
15041     (let (gnus-newsrc-assoc)
15042       (condition-case nil
15043           (load ding-file t t t)
15044         (error
15045          (gnus-message 1 "Error in %s" ding-file)
15046          (ding)))
15047       (when gnus-newsrc-assoc 
15048         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15049     (gnus-make-hashtable-from-newsrc-alist)
15050     (when (file-newer-than-file-p file ding-file)
15051       ;; Old format quick file
15052       (gnus-message 5 "Reading %s..." file)
15053       ;; The .el file is newer than the .eld file, so we read that one
15054       ;; as well. 
15055       (gnus-read-old-newsrc-el-file file))))
15056
15057 ;; Parse the old-style quick startup file
15058 (defun gnus-read-old-newsrc-el-file (file)
15059   (let (newsrc killed marked group m)
15060     (prog1
15061         (let ((gnus-killed-assoc nil)
15062               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15063           (prog1
15064               (condition-case nil
15065                   (load file t t t)
15066                 (error nil))
15067             (setq newsrc gnus-newsrc-assoc
15068                   killed gnus-killed-assoc
15069                   marked gnus-marked-assoc)))
15070       (setq gnus-newsrc-alist nil)
15071       (while newsrc
15072         (setq group (car newsrc))
15073         (let ((info (gnus-get-info (car group))))
15074           (if info
15075               (progn
15076                 (gnus-info-set-read info (cdr (cdr group)))
15077                 (gnus-info-set-level
15078                  info (if (nth 1 group) gnus-level-default-subscribed 
15079                         gnus-level-default-unsubscribed))
15080                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15081             (setq gnus-newsrc-alist
15082                   (cons 
15083                    (setq info
15084                          (list (car group)
15085                                (if (nth 1 group) gnus-level-default-subscribed
15086                                  gnus-level-default-unsubscribed) 
15087                                (cdr (cdr group))))
15088                    gnus-newsrc-alist)))
15089           (if (setq m (assoc (car group) marked))
15090               (gnus-info-set-marks 
15091                info (cons (list (cons 'tick (gnus-compress-sequence
15092                                              (sort (cdr m) '<) t)))
15093                           nil))))
15094         (setq newsrc (cdr newsrc)))
15095       (setq newsrc killed)
15096       (while newsrc
15097         (setcar newsrc (car (car newsrc)))
15098         (setq newsrc (cdr newsrc)))
15099       (setq gnus-killed-list killed))
15100     ;; The .el file version of this variable does not begin with
15101     ;; "options", while the .eld version does, so we just add it if it
15102     ;; isn't there.
15103     (and
15104      gnus-newsrc-options 
15105      (progn
15106        (and (not (string-match "^ *options" gnus-newsrc-options))
15107             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15108        (and (not (string-match "\n$" gnus-newsrc-options))
15109             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15110        ;; Finally, if we read some options lines, we parse them.
15111        (or (string= gnus-newsrc-options "")
15112            (gnus-newsrc-parse-options gnus-newsrc-options))))
15113
15114     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15115     (gnus-make-hashtable-from-newsrc-alist)))
15116       
15117 (defun gnus-make-newsrc-file (file)
15118   "Make server dependent file name by catenating FILE and server host name."
15119   (let* ((file (expand-file-name file nil))
15120          (real-file (concat file "-" (nth 1 gnus-select-method))))
15121     (if (or (file-exists-p real-file)
15122             (file-exists-p (concat real-file ".el"))
15123             (file-exists-p (concat real-file ".eld")))
15124         real-file file)))
15125
15126 (defun gnus-newsrc-to-gnus-format ()
15127   (setq gnus-newsrc-options "")
15128   (setq gnus-newsrc-options-n nil)
15129
15130   (or gnus-active-hashtb
15131       (setq gnus-active-hashtb (make-vector 4095 0)))
15132   (let ((buf (current-buffer))
15133         (already-read (> (length gnus-newsrc-alist) 1))
15134         group subscribed options-symbol newsrc Options-symbol
15135         symbol reads num1)
15136     (goto-char (point-min))
15137     ;; We intern the symbol `options' in the active hashtb so that we
15138     ;; can `eq' against it later.
15139     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15140     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15141   
15142     (while (not (eobp))
15143       ;; We first read the first word on the line by narrowing and
15144       ;; then reading into `gnus-active-hashtb'.  Most groups will
15145       ;; already exist in that hashtb, so this will save some string
15146       ;; space.
15147       (narrow-to-region
15148        (point)
15149        (progn (skip-chars-forward "^ \t!:\n") (point)))
15150       (goto-char (point-min))
15151       (setq symbol 
15152             (and (/= (point-min) (point-max))
15153                  (let ((obarray gnus-active-hashtb)) (read buf))))
15154       (widen)
15155       ;; Now, the symbol we have read is either `options' or a group
15156       ;; name.  If it is an options line, we just add it to a string. 
15157       (cond 
15158        ((or (eq symbol options-symbol)
15159             (eq symbol Options-symbol))
15160         (setq gnus-newsrc-options
15161               ;; This concatting is quite inefficient, but since our
15162               ;; thorough studies show that approx 99.37% of all
15163               ;; .newsrc files only contain a single options line, we
15164               ;; don't give a damn, frankly, my dear.
15165               (concat gnus-newsrc-options
15166                       (buffer-substring 
15167                        (gnus-point-at-bol)
15168                        ;; Options may continue on the next line.
15169                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15170                                 (progn (beginning-of-line) (point)))
15171                            (point)))))
15172         (forward-line -1))
15173        (symbol
15174         (or (boundp symbol) (set symbol nil))
15175         ;; It was a group name.
15176         (setq subscribed (= (following-char) ?:)
15177               group (symbol-name symbol)
15178               reads nil)
15179         (if (eolp)
15180             ;; If the line ends here, this is clearly a buggy line, so
15181             ;; we put point a the beginning of line and let the cond
15182             ;; below do the error handling.
15183             (beginning-of-line)
15184           ;; We skip to the beginning of the ranges.
15185           (skip-chars-forward "!: \t"))
15186         ;; We are now at the beginning of the list of read articles.
15187         ;; We read them range by range.
15188         (while
15189             (cond 
15190              ((looking-at "[0-9]+")
15191               ;; We narrow and read a number instead of buffer-substring/
15192               ;; string-to-int because it's faster.  narrow/widen is
15193               ;; faster than save-restriction/narrow, and save-restriction
15194               ;; produces a garbage object.
15195               (setq num1 (progn
15196                            (narrow-to-region (match-beginning 0) (match-end 0))
15197                            (read buf)))
15198               (widen)
15199               ;; If the next character is a dash, then this is a range.
15200               (if (= (following-char) ?-)
15201                   (progn
15202                     ;; We read the upper bound of the range.
15203                     (forward-char 1)
15204                     (if (not (looking-at "[0-9]+"))
15205                         ;; This is a buggy line, by we pretend that
15206                         ;; it's kinda OK.  Perhaps the user should be
15207                         ;; dinged? 
15208                         (setq reads (cons num1 reads))
15209                       (setq reads 
15210                             (cons 
15211                              (cons num1
15212                                    (progn
15213                                      (narrow-to-region (match-beginning 0) 
15214                                                        (match-end 0))
15215                                      (read buf)))
15216                              reads))
15217                       (widen)))
15218                 ;; It was just a simple number, so we add it to the
15219                 ;; list of ranges.
15220                 (setq reads (cons num1 reads)))
15221               ;; If the next char in ?\n, then we have reached the end
15222               ;; of the line and return nil.
15223               (/= (following-char) ?\n))
15224              ((= (following-char) ?\n)
15225               ;; End of line, so we end.
15226               nil)
15227              (t
15228               ;; Not numbers and not eol, so this might be a buggy
15229               ;; line... 
15230               (or (eobp)                
15231                   ;; If it was eob instead of ?\n, we allow it.
15232                   (progn
15233                     ;; The line was buggy.
15234                     (setq group nil)
15235                     (gnus-message 3 "Mangled line: %s" 
15236                                   (buffer-substring (gnus-point-at-bol) 
15237                                                     (gnus-point-at-eol)))
15238                     (ding)
15239                     (sit-for 1)))
15240               nil))
15241           ;; Skip past ", ".  Spaces are illegal in these ranges, but
15242           ;; we allow them, because it's a common mistake to put a
15243           ;; space after the comma.
15244           (skip-chars-forward ", "))
15245
15246         ;; We have already read .newsrc.eld, so we gently update the
15247         ;; data in the hash table with the information we have just
15248         ;; read. 
15249         (when group
15250           (let ((info (gnus-get-info group))
15251                 level)
15252             (if info
15253                 ;; There is an entry for this file in the alist.
15254                 (progn
15255                   (gnus-info-set-read info (nreverse reads))
15256                   ;; We update the level very gently.  In fact, we
15257                   ;; only change it if there's been a status change
15258                   ;; from subscribed to unsubscribed, or vice versa.
15259                   (setq level (gnus-info-level info))
15260                   (cond ((and (<= level gnus-level-subscribed)
15261                               (not subscribed))
15262                          (setq level (if reads
15263                                          gnus-level-default-unsubscribed 
15264                                        (1+ gnus-level-default-unsubscribed))))
15265                         ((and (> level gnus-level-subscribed) subscribed)
15266                          (setq level gnus-level-default-subscribed)))
15267                   (gnus-info-set-level info level))
15268               ;; This is a new group.
15269               (setq info (list group 
15270                                (if subscribed
15271                                    gnus-level-default-subscribed 
15272                                  (if reads
15273                                      (1+ gnus-level-subscribed)
15274                                    gnus-level-default-unsubscribed))
15275                                (nreverse reads))))
15276             (setq newsrc (cons info newsrc))))))
15277       (forward-line 1))
15278     
15279     (setq newsrc (nreverse newsrc))
15280
15281     (if (not already-read)
15282         ()
15283       ;; We now have two newsrc lists - `newsrc', which is what we
15284       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
15285       ;; what we've read from .newsrc.eld.  We have to merge these
15286       ;; lists.  We do this by "attaching" any (foreign) groups in the
15287       ;; gnus-newsrc-alist to the (native) group that precedes them. 
15288       (let ((rc (cdr gnus-newsrc-alist))
15289             (prev gnus-newsrc-alist)
15290             entry mentry)
15291         (while rc
15292           (or (null (nth 4 (car rc)))   ; It's a native group.
15293               (assoc (car (car rc)) newsrc) ; It's already in the alist.
15294               (if (setq entry (assoc (car (car prev)) newsrc))
15295                   (setcdr (setq mentry (memq entry newsrc))
15296                           (cons (car rc) (cdr mentry)))
15297                 (setq newsrc (cons (car rc) newsrc))))
15298           (setq prev rc
15299                 rc (cdr rc)))))
15300
15301     (setq gnus-newsrc-alist newsrc)
15302     ;; We make the newsrc hashtb.
15303     (gnus-make-hashtable-from-newsrc-alist)
15304
15305     ;; Finally, if we read some options lines, we parse them.
15306     (or (string= gnus-newsrc-options "")
15307         (gnus-newsrc-parse-options gnus-newsrc-options))))
15308
15309 ;; Parse options lines to find "options -n !all rec.all" and stuff.
15310 ;; The return value will be a list on the form
15311 ;; ((regexp1 . ignore)
15312 ;;  (regexp2 . subscribe)...)
15313 ;; When handling new newsgroups, groups that match a `ignore' regexp
15314 ;; will be ignored, and groups that match a `subscribe' regexp will be
15315 ;; subscribed.  A line like
15316 ;; options -n !all rec.all
15317 ;; will lead to a list that looks like
15318 ;; (("^rec\\..+" . subscribe) 
15319 ;;  ("^.+" . ignore))
15320 ;; So all "rec.*" groups will be subscribed, while all the other
15321 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
15322 ;; different from "options -n rec.all !all". 
15323 (defun gnus-newsrc-parse-options (options)
15324   (let (out eol)
15325     (save-excursion
15326       (gnus-set-work-buffer)
15327       (insert (regexp-quote options))
15328       ;; First we treat all continuation lines.
15329       (goto-char (point-min))
15330       (while (re-search-forward "\n[ \t]+" nil t)
15331         (replace-match " " t t))
15332       ;; Then we transform all "all"s into ".+"s.
15333       (goto-char (point-min))
15334       (while (re-search-forward "\\ball\\b" nil t)
15335         (replace-match ".+" t t))
15336       (goto-char (point-min))
15337       ;; We remove all other options than the "-n" ones.
15338       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
15339         (replace-match " ")
15340         (forward-char -1))
15341       (goto-char (point-min))
15342
15343       ;; We are only interested in "options -n" lines - we
15344       ;; ignore the other option lines.
15345       (while (re-search-forward "[ \t]-n" nil t)
15346         (setq eol 
15347               (or (save-excursion
15348                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
15349                          (- (point) 2)))
15350                   (gnus-point-at-eol)))
15351         ;; Search for all "words"...
15352         (while (re-search-forward "[^ \t,\n]+" eol t)
15353           (if (= (char-after (match-beginning 0)) ?!)
15354               ;; If the word begins with a bang (!), this is a "not"
15355               ;; spec.  We put this spec (minus the bang) and the
15356               ;; symbol `ignore' into the list.
15357               (setq out (cons (cons (concat 
15358                                      "^" (buffer-substring 
15359                                           (1+ (match-beginning 0))
15360                                           (match-end 0)))
15361                                     'ignore) out))
15362             ;; There was no bang, so this is a "yes" spec.
15363             (setq out (cons (cons (concat "^" (match-string 0))
15364                                   'subscribe) out)))))
15365     
15366       (setq gnus-newsrc-options-n out))))
15367
15368 (defun gnus-save-newsrc-file (&optional force)
15369   "Save .newsrc file."
15370   ;; Note: We cannot save .newsrc file if all newsgroups are removed
15371   ;; from the variable gnus-newsrc-alist.
15372   (when (and (or gnus-newsrc-alist gnus-killed-list)
15373              gnus-current-startup-file)
15374     (save-excursion
15375       (if (and (or gnus-use-dribble-file gnus-slave)
15376                (not force)
15377                (or (not gnus-dribble-buffer)
15378                    (not (buffer-name gnus-dribble-buffer))
15379                    (zerop (save-excursion
15380                             (set-buffer gnus-dribble-buffer)
15381                             (buffer-size)))))
15382           (gnus-message 4 "(No changes need to be saved)")
15383         (run-hooks 'gnus-save-newsrc-hook)
15384         (if gnus-slave
15385             (gnus-slave-save-newsrc)
15386           ;; Save .newsrc.
15387           (when gnus-save-newsrc-file
15388             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
15389             (gnus-gnus-to-newsrc-format)
15390             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
15391           ;; Save .newsrc.eld.
15392           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
15393           (make-local-variable 'version-control)
15394           (setq version-control 'never)
15395           (setq buffer-file-name 
15396                 (concat gnus-current-startup-file ".eld"))
15397           (gnus-add-current-to-buffer-list)
15398           (buffer-disable-undo (current-buffer))
15399           (erase-buffer)
15400           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
15401           (gnus-gnus-to-quick-newsrc-format)
15402           (run-hooks 'gnus-save-quick-newsrc-hook)
15403           (save-buffer)
15404           (kill-buffer (current-buffer))
15405           (gnus-message 
15406            5 "Saving %s.eld...done" gnus-current-startup-file))
15407         (gnus-dribble-delete-file)))))
15408
15409 (defun gnus-gnus-to-quick-newsrc-format ()
15410   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
15411   (insert ";; Gnus startup file.\n")
15412   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
15413   (insert ";; to read .newsrc.\n")
15414   (insert "(setq gnus-newsrc-file-version "
15415           (prin1-to-string gnus-version) ")\n")
15416   (let ((variables 
15417          (if gnus-save-killed-list gnus-variable-list
15418            ;; Remove the `gnus-killed-list' from the list of variables
15419            ;; to be saved, if required.
15420            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
15421         ;; Peel off the "dummy" group.
15422         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
15423         variable)
15424     ;; Insert the variables into the file.
15425     (while variables
15426       (when (and (boundp (setq variable (pop variables)))
15427                  (symbol-value variable))
15428         (insert "(setq " (symbol-name variable) " '"
15429                 (prin1-to-string (symbol-value variable)) ")\n")))))
15430
15431 (defun gnus-gnus-to-newsrc-format ()
15432   ;; Generate and save the .newsrc file.
15433   (let ((newsrc (cdr gnus-newsrc-alist))
15434         info ranges range)
15435     (save-excursion
15436       (set-buffer (create-file-buffer gnus-current-startup-file))
15437       (setq buffer-file-name gnus-current-startup-file)
15438       (buffer-disable-undo (current-buffer))
15439       (erase-buffer)
15440       ;; Write options.
15441       (if gnus-newsrc-options (insert gnus-newsrc-options))
15442       ;; Write subscribed and unsubscribed.
15443       (while newsrc
15444         (setq info (car newsrc))
15445         (if (not (gnus-info-method info))
15446             ;; Don't write foreign groups to .newsrc.
15447             (progn
15448               (insert (gnus-info-group info)
15449                       (if (> (gnus-info-level info) gnus-level-subscribed)
15450                           "!" ":"))
15451               (if (setq ranges (gnus-info-read info))
15452                   (progn
15453                     (insert " ")
15454                     (if (not (listp (cdr ranges)))
15455                         (if (= (car ranges) (cdr ranges))
15456                             (insert (int-to-string (car ranges)))
15457                           (insert (int-to-string (car ranges)) "-" 
15458                                   (int-to-string (cdr ranges))))
15459                       (while ranges
15460                         (setq range (car ranges)
15461                               ranges (cdr ranges))
15462                         (if (or (atom range) (= (car range) (cdr range)))
15463                             (insert (int-to-string 
15464                                      (or (and (atom range) range) 
15465                                          (car range))))
15466                           (insert (int-to-string (car range)) "-"
15467                                   (int-to-string (cdr range))))
15468                         (if ranges (insert ","))))))
15469               (insert "\n")))
15470         (setq newsrc (cdr newsrc)))
15471       (make-local-variable 'version-control)
15472       (setq version-control 'never)
15473       ;; It has been reported that sometime the modtime on the .newsrc
15474       ;; file seems to be off.  We really do want to overwrite it, so
15475       ;; we clear the modtime here before saving.  It's a bit odd,
15476       ;; though... 
15477       ;; sometimes the modtime clear isn't sufficient.  most brute force:
15478       ;; delete the silly thing entirely first.  but this fails to provide
15479       ;; such niceties as .newsrc~ creation.
15480       (if gnus-modtime-botch
15481           (delete-file gnus-startup-file)
15482         (clear-visited-file-modtime))
15483       (run-hooks 'gnus-save-standard-newsrc-hook)
15484       (save-buffer)
15485       (kill-buffer (current-buffer)))))
15486
15487
15488 ;;; Slave functions.
15489
15490 (defun gnus-slave-save-newsrc ()
15491   (save-excursion
15492     (set-buffer gnus-dribble-buffer)
15493     (let ((slave-name 
15494            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
15495       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
15496
15497 (defun gnus-master-read-slave-newsrc ()
15498   (let ((slave-files 
15499          (directory-files 
15500           (file-name-directory gnus-current-startup-file)
15501           t (concat 
15502              "^" (regexp-quote
15503                   (concat
15504                    (file-name-nondirectory gnus-current-startup-file)
15505                    "-slave-")))
15506           t))
15507         file)
15508     (if (not slave-files)
15509         ()                              ; There are no slave files to read.
15510       (gnus-message 7 "Reading slave newsrcs...")
15511       (save-excursion
15512         (set-buffer (get-buffer-create " *gnus slave*"))
15513         (buffer-disable-undo (current-buffer))
15514         (setq slave-files 
15515               (sort (mapcar (lambda (file) 
15516                               (list (nth 5 (file-attributes file)) file))
15517                             slave-files)
15518                     (lambda (f1 f2)
15519                       (or (< (car (car f1)) (car (car f2)))
15520                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
15521         (while slave-files
15522           (erase-buffer)
15523           (setq file (nth 1 (car slave-files)))
15524           (insert-file-contents file)
15525           (if (condition-case ()
15526                   (progn
15527                     (eval-buffer (current-buffer))
15528                     t)
15529                 (error 
15530                  (gnus-message 3 "Possible error in %s" file)
15531                  (ding)
15532                  (sit-for 2)
15533                  nil))
15534               (or gnus-slave ; Slaves shouldn't delete these files.
15535                   (condition-case ()
15536                       (delete-file file)
15537                     (error nil))))
15538           (setq slave-files (cdr slave-files))))
15539       (gnus-message 7 "Reading slave newsrcs...done"))))
15540
15541
15542 ;;; Group description.
15543
15544 (defun gnus-read-all-descriptions-files ()
15545   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
15546     (while methods
15547       (gnus-read-descriptions-file (car methods))
15548       (setq methods (cdr methods)))
15549     t))
15550
15551 (defun gnus-read-descriptions-file (&optional method)
15552   (let ((method (or method gnus-select-method)))
15553     ;; We create the hashtable whether we manage to read the desc file
15554     ;; to avoid trying to re-read after a failed read.
15555     (or gnus-description-hashtb
15556         (setq gnus-description-hashtb 
15557               (gnus-make-hashtable (length gnus-active-hashtb))))
15558     ;; Mark this method's desc file as read.
15559     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
15560                   gnus-description-hashtb)
15561
15562     (gnus-message 5 "Reading descriptions file via %s..." (car method))
15563     (cond 
15564      ((not (gnus-check-server method))
15565       (gnus-message 1 "Couldn't open server")
15566       nil)
15567      ((not (gnus-request-list-newsgroups method))
15568       (gnus-message 1 "Couldn't read newsgroups descriptions")
15569       nil)
15570      (t
15571       (let (group)
15572         (save-excursion
15573           (save-restriction
15574             (set-buffer nntp-server-buffer)
15575             (goto-char (point-min))
15576             (if (or (search-forward "\n.\n" nil t)
15577                     (goto-char (point-max)))
15578                 (progn
15579                   (beginning-of-line)
15580                   (narrow-to-region (point-min) (point))))
15581             (goto-char (point-min))
15582             (while (not (eobp))
15583               ;; If we get an error, we set group to 0, which is not a
15584               ;; symbol... 
15585               (setq group 
15586                     (condition-case ()
15587                         (let ((obarray gnus-description-hashtb))
15588                           ;; Group is set to a symbol interned in this
15589                           ;; hash table.
15590                           (read nntp-server-buffer))
15591                       (error 0)))
15592               (skip-chars-forward " \t")
15593               ;; ...  which leads to this line being effectively ignored.
15594               (and (symbolp group)
15595                    (set group (buffer-substring 
15596                                (point) (progn (end-of-line) (point)))))
15597               (forward-line 1))))
15598         (gnus-message 5 "Reading descriptions file...done")
15599         t)))))
15600
15601 (defun gnus-group-get-description (group)
15602   "Get the description of a group by sending XGTITLE to the server."
15603   (when (gnus-request-group-description group)
15604     (save-excursion
15605       (set-buffer nntp-server-buffer)
15606       (goto-char (point-min))
15607       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
15608         (match-string 1)))))
15609
15610 ;;;
15611 ;;; Buffering of read articles.
15612 ;;;
15613
15614 (defvar gnus-backlog-buffer " *Gnus Backlog*")
15615 (defvar gnus-backlog-articles nil)
15616 (defvar gnus-backlog-hashtb nil)
15617
15618 (defun gnus-backlog-buffer ()
15619   "Return the backlog buffer."
15620   (or (get-buffer gnus-backlog-buffer)
15621       (save-excursion
15622         (set-buffer (get-buffer-create gnus-backlog-buffer))
15623         (buffer-disable-undo (current-buffer))
15624         (setq buffer-read-only t)
15625         (gnus-add-current-to-buffer-list)
15626         (get-buffer gnus-backlog-buffer))))
15627
15628 (defun gnus-backlog-setup ()
15629   "Initialize backlog variables."
15630   (unless gnus-backlog-hashtb
15631     (setq gnus-backlog-hashtb (make-vector 1023 0))))
15632
15633 (defun gnus-backlog-shutdown ()
15634   "Clear all backlog variables and buffers."
15635   (when (get-buffer gnus-backlog-buffer)
15636     (kill-buffer gnus-backlog-buffer))
15637   (setq gnus-backlog-hashtb nil
15638         gnus-backlog-articles nil))
15639
15640 (defun gnus-backlog-enter-article (group number buffer)
15641   (gnus-backlog-setup)
15642   (let ((ident (intern (concat group ":" (int-to-string number))
15643                        gnus-backlog-hashtb))
15644         b)
15645     (if (memq ident gnus-backlog-articles)
15646         () ; It's already kept.
15647       ;; Remove the oldest article, if necessary.
15648       (and (numberp gnus-keep-backlog)
15649            (>= (length gnus-backlog-articles) gnus-keep-backlog)
15650            (gnus-backlog-remove-oldest-article))
15651       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
15652       ;; Insert the new article.
15653       (save-excursion
15654         (set-buffer (gnus-backlog-buffer))
15655         (let (buffer-read-only)
15656           (goto-char (point-max))
15657           (or (bolp) (insert "\n"))
15658           (setq b (point))
15659           (insert-buffer-substring buffer)
15660           ;; Tag the beginning of the article with the ident.
15661           (put-text-property b (1+ b) 'gnus-backlog ident))))))
15662
15663 (defun gnus-backlog-remove-oldest-article ()
15664   (save-excursion
15665     (set-buffer (gnus-backlog-buffer))
15666     (goto-char (point-min))
15667     (if (zerop (buffer-size))
15668         () ; The buffer is empty.
15669       (let ((ident (get-text-property (point) 'gnus-backlog))
15670             buffer-read-only)
15671         ;; Remove the ident from the list of articles.
15672         (when ident
15673           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
15674         ;; Delete the article itself.
15675         (delete-region 
15676          (point) (next-single-property-change
15677                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
15678
15679 (defun gnus-backlog-request-article (group number buffer)
15680   (gnus-backlog-setup)
15681   (let ((ident (intern (concat group ":" (int-to-string number))
15682                        gnus-backlog-hashtb))
15683         beg end)
15684     (when (memq ident gnus-backlog-articles)
15685       ;; It was in the backlog.
15686       (save-excursion
15687         (set-buffer (gnus-backlog-buffer))
15688         (if (not (setq beg (text-property-any 
15689                             (point-min) (point-max) 'gnus-backlog
15690                             ident)))
15691             ;; It wasn't in the backlog after all.
15692             (progn
15693               (setq gnus-backlog-articles (delq ident gnus-backlog-articles))
15694               nil)
15695           ;; Find the end (i. e., the beginning of the next article).
15696           (setq end
15697                 (next-single-property-change 
15698                  (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
15699       (let ((buffer-read-only nil))
15700         (erase-buffer)
15701         (insert-buffer-substring gnus-backlog-buffer beg end)
15702         t))))
15703
15704 ;; Allow redefinition of Gnus functions.
15705
15706 (gnus-ems-redefine)
15707
15708 (provide 'gnus)
15709
15710 ;;; gnus.el ends here