*** 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,96 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34 (require 'nnmail)
35 (require 'backquote)
36 (require 'nnoo)
37
38 (eval-when-compile (require 'cl))
39
40 (defvar gnus-directory (or (getenv "SAVEDIR") "~/News/")
41   "*Directory variable from which all other Gnus file variables are derived.")
42
43 ;; Site dependent variables.  These variables should be defined in
44 ;; paths.el.
45
46 (defvar gnus-default-nntp-server nil
47   "Specify a default NNTP server.
48 This variable should be defined in paths.el, and should never be set
49 by the user.
50 If you want to change servers, you should use `gnus-select-method'.
51 See the documentation to that variable.")
52
53 (defvar gnus-backup-default-subscribed-newsgroups
54   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
55   "Default default new newsgroups the first time Gnus is run.
56 Should be set in paths.el, and shouldn't be touched by the user.")
57
58 (defvar gnus-local-domain nil
59   "Local domain name without a host name.
60 The DOMAINNAME environment variable is used instead if it is defined.
61 If the `system-name' function returns the full Internet name, there is
62 no need to set this variable.")
63
64 (defvar gnus-local-organization nil
65   "String with a description of what organization (if any) the user belongs to.
66 The ORGANIZATION environment variable is used instead if it is defined.
67 If this variable contains a function, this function will be called
68 with the current newsgroup name as the argument.  The function should
69 return a string.
70
71 In any case, if the string (either in the variable, in the environment
72 variable, or returned by the function) is a file name, the contents of
73 this file will be used as the organization.")
74
75 ;; Customization variables
76
77 ;; Don't touch this variable.
78 (defvar gnus-nntp-service "nntp"
79   "*NNTP service name (\"nntp\" or 119).
80 This is an obsolete variable, which is scarcely used.  If you use an
81 nntp server for your newsgroup and want to change the port number
82 used to 899, you would say something along these lines:
83
84  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
85
86 (defvar gnus-nntpserver-file "/etc/nntpserver"
87   "*A file with only the name of the nntp server in it.")
88
89 ;; This function is used to check both the environment variable
90 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
91 ;; an nntp server name default.
92 (defun gnus-getenv-nntpserver ()
93   (or (getenv "NNTPSERVER")
94       (and (file-readable-p gnus-nntpserver-file)
95            (save-excursion
96              (set-buffer (get-buffer-create " *gnus nntp*"))
97              (buffer-disable-undo (current-buffer))
98              (insert-file-contents gnus-nntpserver-file)
99              (let ((name (buffer-string)))
100                (prog1
101                    (if (string-match "^[ \t\n]*$" name)
102                        nil
103                      name)
104                  (kill-buffer (current-buffer))))))))
105
106 (defvar gnus-select-method
107   (nconc
108    (list 'nntp (or (condition-case ()
109                        (gnus-getenv-nntpserver)
110                      (error nil))
111                    (if (and gnus-default-nntp-server
112                             (not (string= gnus-default-nntp-server "")))
113                        gnus-default-nntp-server)
114                    (system-name)))
115    (if (or (null gnus-nntp-service)
116            (equal gnus-nntp-service "nntp"))
117        nil
118      (list gnus-nntp-service)))
119   "*Default method for selecting a newsgroup.
120 This variable should be a list, where the first element is how the
121 news is to be fetched, the second is the address.
122
123 For instance, if you want to get your news via NNTP from
124 \"flab.flab.edu\", you could say:
125
126 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
127
128 If you want to use your local spool, say:
129
130 (setq gnus-select-method (list 'nnspool (system-name)))
131
132 If you use this variable, you must set `gnus-nntp-server' to nil.
133
134 There is a lot more to know about select methods and virtual servers -
135 see the manual for details.")
136
137 (defvar gnus-message-archive-method 
138   `(nnfolder
139     "archive"
140     (nnfolder-directory ,(nnheader-concat message-directory "archive"))
141     (nnfolder-active-file 
142      ,(nnheader-concat message-directory "archive/active"))
143     (nnfolder-get-new-mail nil)
144     (nnfolder-inhibit-expiry t))
145   "*Method used for archiving messages you've sent.
146 This should be a mail method.
147
148 It's probably not a very effective to change this variable once you've
149 run Gnus once.  After doing that, you must edit this server from the
150 server buffer.")
151
152 (defvar gnus-message-archive-group nil
153   "*Name of the group in which to save the messages you've written.
154 This can either be a string, a list of strings; or an alist
155 of regexps/functions/forms to be evaluated to return a string (or a list
156 of strings).  The functions are called with the name of the current
157 group (or nil) as a parameter.
158
159 Normally the group names returned by this variable should be
160 unprefixed -- which implictly means \"store on the archive server\".
161 However, you may wish to store the message on some other server.  In
162 that case, just return a fully prefixed name of the group --
163 \"nnml+private:mail.misc\", for instance.")
164
165 (defvar gnus-refer-article-method nil
166   "*Preferred method for fetching an article by Message-ID.
167 If you are reading news from the local spool (with nnspool), fetching
168 articles by Message-ID is painfully slow.  By setting this method to an
169 nntp method, you might get acceptable results.
170
171 The value of this variable must be a valid select method as discussed
172 in the documentation of `gnus-select-method'.")
173
174 (defvar gnus-secondary-select-methods nil
175   "*A list of secondary methods that will be used for reading news.
176 This is a list where each element is a complete select method (see
177 `gnus-select-method').
178
179 If, for instance, you want to read your mail with the nnml backend,
180 you could set this variable:
181
182 (setq gnus-secondary-select-methods '((nnml \"\")))")
183
184 (defvar gnus-secondary-servers nil
185   "*List of NNTP servers that the user can choose between interactively.
186 To make Gnus query you for a server, you have to give `gnus' a
187 non-numeric prefix - `C-u M-x gnus', in short.")
188
189 (defvar gnus-nntp-server nil
190   "*The name of the host running the NNTP server.
191 This variable is semi-obsolete.  Use the `gnus-select-method'
192 variable instead.")
193
194 (defvar gnus-startup-file "~/.newsrc"
195   "*Your `.newsrc' file.
196 `.newsrc-SERVER' will be used instead if that exists.")
197
198 (defvar gnus-init-file "~/.gnus"
199   "*Your Gnus elisp startup file.
200 If a file with the .el or .elc suffixes exist, it will be read
201 instead.")
202
203 (defvar gnus-group-faq-directory
204   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
205     "/ftp@sunsite.auc.dk:/pub/usenet/"
206     "/ftp@sunsite.doc.ic.ac.uk:/pub/usenet/news-faqs/"
207     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
208     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
209     "/ftp@rtfm.mit.edu:/pub/usenet/"
210     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
211     "/ftp@ftp.sunet.se:/pub/usenet/"
212     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
213     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
214     "/ftp@ftp.hk.super.net:/mirror/faqs/")
215   "*Directory where the group FAQs are stored.
216 This will most commonly be on a remote machine, and the file will be
217 fetched by ange-ftp.
218
219 This variable can also be a list of directories.  In that case, the
220 first element in the list will be used by default, and the others will
221 be used as backup sites.
222
223 Note that Gnus uses an aol machine as the default directory.  If this
224 feels fundamentally unclean, just think of it as a way to finally get
225 something of value back from them.
226
227 If the default site is too slow, try one of these:
228
229    North America: mirrors.aol.com                /pub/rtfm/usenet
230                   ftp.seas.gwu.edu               /pub/rtfm
231                   rtfm.mit.edu                   /pub/usenet
232    Europe:        ftp.uni-paderborn.de           /pub/FAQ
233                   src.doc.ic.ac.uk               /usenet/news-FAQS
234                   ftp.sunet.se                   /pub/usenet
235                   sunsite.auc.dk                 /pub/usenet
236    Asia:          nctuccca.edu.tw                /USENET/FAQ
237                   hwarang.postech.ac.kr          /pub/usenet
238                   ftp.hk.super.net               /mirror/faqs")
239
240 (defvar gnus-group-archive-directory
241   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
242   "*The address of the (ding) archives.")
243
244 (defvar gnus-group-recent-archive-directory
245   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
246   "*The address of the most recent (ding) articles.")
247
248 (defvar gnus-default-subscribed-newsgroups nil
249   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
250 It should be a list of strings.
251 If it is `t', Gnus will not do anything special the first time it is
252 started; it'll just use the normal newsgroups subscription methods.")
253
254 (defvar gnus-use-cross-reference t
255   "*Non-nil means that cross referenced articles will be marked as read.
256 If nil, ignore cross references.  If t, mark articles as read in
257 subscribed newsgroups.  If neither t nor nil, mark as read in all
258 newsgroups.")
259
260 (defvar gnus-single-article-buffer t
261   "*If non-nil, display all articles in the same buffer.
262 If nil, each group will get its own article buffer.")
263
264 (defvar gnus-use-dribble-file t
265   "*Non-nil means that Gnus will use a dribble file to store user updates.
266 If Emacs should crash without saving the .newsrc files, complete
267 information can be restored from the dribble file.")
268
269 (defvar gnus-dribble-directory nil
270   "*The directory where dribble files will be saved.
271 If this variable is nil, the directory where the .newsrc files are
272 saved will be used.")
273
274 (defvar gnus-asynchronous nil
275   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
276
277 (defvar gnus-kill-summary-on-exit t
278   "*If non-nil, kill the summary buffer when you exit from it.
279 If nil, the summary will become a \"*Dead Summary*\" buffer, and
280 it will be killed sometime later.")
281
282 (defvar gnus-large-newsgroup 200
283   "*The number of articles which indicates a large newsgroup.
284 If the number of articles in a newsgroup is greater than this value,
285 confirmation is required for selecting the newsgroup.")
286
287 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
288 (defvar gnus-no-groups-message "No news is horrible news"
289   "*Message displayed by Gnus when no groups are available.")
290
291 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
292   "*Non-nil means that the default name of a file to save articles in is the group name.
293 If it's nil, the directory form of the group name is used instead.
294
295 If this variable is a list, and the list contains the element
296 `not-score', long file names will not be used for score files; if it
297 contains the element `not-save', long file names will not be used for
298 saving; and if it contains the element `not-kill', long file names
299 will not be used for kill files.
300
301 Note that the default for this variable varies according to what system
302 type you're using.  On `usg-unix-v' and `xenix' this variable defaults
303 to nil while on all other systems it defaults to t.")
304
305 (defvar gnus-article-save-directory gnus-directory
306   "*Name of the directory articles will be saved in (default \"~/News\").")
307
308 (defvar gnus-kill-files-directory gnus-directory
309   "*Name of the directory where kill files will be stored (default \"~/News\").")
310
311 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
312   "*A function to save articles in your favorite format.
313 The function must be interactively callable (in other words, it must
314 be an Emacs command).
315
316 Gnus provides the following functions:
317
318 * gnus-summary-save-in-rmail (Rmail format)
319 * gnus-summary-save-in-mail (Unix mail format)
320 * gnus-summary-save-in-folder (MH folder)
321 * gnus-summary-save-in-file (article format).
322 * gnus-summary-save-in-vm (use VM's folder format).")
323
324 (defvar gnus-prompt-before-saving 'always
325   "*This variable says how much prompting is to be done when saving articles.
326 If it is nil, no prompting will be done, and the articles will be
327 saved to the default files.  If this variable is `always', each and
328 every article that is saved will be preceded by a prompt, even when
329 saving large batches of articles.  If this variable is neither nil not
330 `always', there the user will be prompted once for a file name for
331 each invocation of the saving commands.")
332
333 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
334   "*A function generating a file name to save articles in Rmail format.
335 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
336
337 (defvar gnus-mail-save-name (function gnus-plain-save-name)
338   "*A function generating a file name to save articles in Unix mail format.
339 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
340
341 (defvar gnus-folder-save-name (function gnus-folder-save-name)
342   "*A function generating a file name to save articles in MH folder.
343 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
344
345 (defvar gnus-file-save-name (function gnus-numeric-save-name)
346   "*A function generating a file name to save articles in article format.
347 The function is called with NEWSGROUP, HEADERS, and optional
348 LAST-FILE.")
349
350 (defvar gnus-split-methods
351   '((gnus-article-archive-name))
352   "*Variable used to suggest where articles are to be saved.
353 For instance, if you would like to save articles related to Gnus in
354 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
355 you could set this variable to something like:
356
357  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
358    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
359
360 This variable is an alist where the where the key is the match and the
361 value is a list of possible files to save in if the match is non-nil.
362
363 If the match is a string, it is used as a regexp match on the
364 article.  If the match is a symbol, that symbol will be funcalled
365 from the buffer of the article to be saved with the newsgroup as the
366 parameter.  If it is a list, it will be evaled in the same buffer.
367
368 If this form or function returns a string, this string will be used as
369 a possible file name; and if it returns a non-nil list, that list will
370 be used as possible file names.")
371
372 (defvar gnus-move-split-methods nil
373   "*Variable used to suggest where articles are to be moved to.
374 It uses the same syntax as the `gnus-split-methods' variable.")
375
376 (defvar gnus-save-score nil
377   "*If non-nil, save group scoring info.")
378
379 (defvar gnus-use-adaptive-scoring nil
380   "*If non-nil, use some adaptive scoring scheme.")
381
382 (defvar gnus-use-cache 'passive
383   "*If nil, Gnus will ignore the article cache.
384 If `passive', it will allow entering (and reading) articles
385 explicitly entered into the cache.  If anything else, use the
386 cache to the full extent of the law.")
387
388 (defvar gnus-use-trees nil
389   "*If non-nil, display a thread tree buffer.")
390
391 (defvar gnus-use-grouplens nil
392   "*If non-nil, use GroupLens ratings.")
393
394 (defvar gnus-keep-backlog nil
395   "*If non-nil, Gnus will keep read articles for later re-retrieval.
396 If it is a number N, then Gnus will only keep the last N articles
397 read.  If it is neither nil nor a number, Gnus will keep all read
398 articles.  This is not a good idea.")
399
400 (defvar gnus-use-nocem nil
401   "*If non-nil, Gnus will read NoCeM cancel messages.")
402
403 (defvar gnus-use-demon nil
404   "If non-nil, Gnus might use some demons.")
405
406 (defvar gnus-use-scoring t
407   "*If non-nil, enable scoring.")
408
409 (defvar gnus-use-picons nil
410   "*If non-nil, display picons.")
411
412 (defvar gnus-fetch-old-headers nil
413   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
414 If an unread article in the group refers to an older, already read (or
415 just marked as read) article, the old article will not normally be
416 displayed in the Summary buffer.  If this variable is non-nil, Gnus
417 will attempt to grab the headers to the old articles, and thereby
418 build complete threads.  If it has the value `some', only enough
419 headers to connect otherwise loose threads will be displayed.
420 This variable can also be a number.  In that case, no more than that
421 number of old headers will be fetched.
422
423 The server has to support NOV for any of this to work.")
424
425 ;see gnus-cus.el
426 ;(defvar gnus-visual t
427 ;  "*If non-nil, will do various highlighting.
428 ;If nil, no mouse highlights (or any other highlights) will be
429 ;performed.  This might speed up Gnus some when generating large group
430 ;and summary buffers.")
431
432 (defvar gnus-novice-user t
433   "*Non-nil means that you are a usenet novice.
434 If non-nil, verbose messages may be displayed and confirmations may be
435 required.")
436
437 (defvar gnus-expert-user nil
438   "*Non-nil means that you will never be asked for confirmation about anything.
439 And that means *anything*.")
440
441 (defvar gnus-verbose 7
442   "*Integer that says how verbose Gnus should be.
443 The higher the number, the more messages Gnus will flash to say what
444 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
445 display most important messages; and at ten, Gnus will keep on
446 jabbering all the time.")
447
448 (defvar gnus-keep-same-level nil
449   "*Non-nil means that the next newsgroup after the current will be on the same level.
450 When you type, for instance, `n' after reading the last article in the
451 current newsgroup, you will go to the next newsgroup.  If this variable
452 is nil, the next newsgroup will be the next from the group
453 buffer.
454 If this variable is non-nil, Gnus will either put you in the
455 next newsgroup with the same level, or, if no such newsgroup is
456 available, the next newsgroup with the lowest possible level higher
457 than the current level.
458 If this variable is `best', Gnus will make the next newsgroup the one
459 with the best level.")
460
461 (defvar gnus-summary-make-false-root 'adopt
462   "*nil means that Gnus won't gather loose threads.
463 If the root of a thread has expired or been read in a previous
464 session, the information necessary to build a complete thread has been
465 lost.  Instead of having many small sub-threads from this original thread
466 scattered all over the summary buffer, Gnus can gather them.
467
468 If non-nil, Gnus will try to gather all loose sub-threads from an
469 original thread into one large thread.
470
471 If this variable is non-nil, it should be one of `none', `adopt',
472 `dummy' or `empty'.
473
474 If this variable is `none', Gnus will not make a false root, but just
475 present the sub-threads after another.
476 If this variable is `dummy', Gnus will create a dummy root that will
477 have all the sub-threads as children.
478 If this variable is `adopt', Gnus will make one of the \"children\"
479 the parent and mark all the step-children as such.
480 If this variable is `empty', the \"children\" are printed with empty
481 subject fields.  (Or rather, they will be printed with a string
482 given by the `gnus-summary-same-subject' variable.)")
483
484 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
485   "*A regexp to match subjects to be excluded from loose thread gathering.
486 As loose thread gathering is done on subjects only, that means that
487 there can be many false gatherings performed.  By rooting out certain
488 common subjects, gathering might become saner.")
489
490 (defvar gnus-summary-gather-subject-limit nil
491   "*Maximum length of subject comparisons when gathering loose threads.
492 Use nil to compare full subjects.  Setting this variable to a low
493 number will help gather threads that have been corrupted by
494 newsreaders chopping off subject lines, but it might also mean that
495 unrelated articles that have subject that happen to begin with the
496 same few characters will be incorrectly gathered.
497
498 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
499 comparing subjects.")
500
501 (defvar gnus-simplify-ignored-prefixes nil
502   "*Regexp, matches for which are removed from subject lines when simplifying.")
503
504 (defvar gnus-build-sparse-threads nil
505   "*If non-nil, fill in the gaps in threads.
506 If `some', only fill in the gaps that are needed to tie loose threads
507 together.  If `more', fill in all leaf nodes that Gnus can find.  If
508 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
509
510 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
511   "Function used for gathering loose threads.
512 There are two pre-defined functions: `gnus-gather-threads-by-subject',
513 which only takes Subjects into consideration; and
514 `gnus-gather-threads-by-references', which compared the References
515 headers of the articles to find matches.")
516
517 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
518 (defvar gnus-summary-same-subject ""
519   "*String indicating that the current article has the same subject as the previous.
520 This variable will only be used if the value of
521 `gnus-summary-make-false-root' is `empty'.")
522
523 (defvar gnus-summary-goto-unread t
524   "*If non-nil, marking commands will go to the next unread article.
525 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
526 whether it is read or not.")
527
528 (defvar gnus-group-goto-unread t
529   "*If non-nil, movement commands will go to the next unread and subscribed group.")
530
531 (defvar gnus-goto-next-group-when-activating t
532   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
533
534 (defvar gnus-check-new-newsgroups t
535   "*Non-nil means that Gnus will add new newsgroups at startup.
536 If this variable is `ask-server', Gnus will ask the server for new
537 groups since the last time it checked.  This means that the killed list
538 is no longer necessary, so you could set `gnus-save-killed-list' to
539 nil.
540
541 A variant is to have this variable be a list of select methods.  Gnus
542 will then use the `ask-server' method on all these select methods to
543 query for new groups from all those servers.
544
545 Eg.
546   (setq gnus-check-new-newsgroups
547         '((nntp \"some.server\") (nntp \"other.server\")))
548
549 If this variable is nil, then you have to tell Gnus explicitly to
550 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
551
552 (defvar gnus-check-bogus-newsgroups nil
553   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
554 If this variable is nil, then you have to tell Gnus explicitly to
555 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
556
557 (defvar gnus-read-active-file t
558   "*Non-nil means that Gnus will read the entire active file at startup.
559 If this variable is nil, Gnus will only know about the groups in your
560 `.newsrc' file.
561
562 If this variable is `some', Gnus will try to only read the relevant
563 parts of the active file from the server.  Not all servers support
564 this, and it might be quite slow with other servers, but this should
565 generally be faster than both the t and nil value.
566
567 If you set this variable to nil or `some', you probably still want to
568 be told about new newsgroups that arrive.  To do that, set
569 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
570 properly with all servers.")
571
572 (defvar gnus-level-subscribed 5
573   "*Groups with levels less than or equal to this variable are subscribed.")
574
575 (defvar gnus-level-unsubscribed 7
576   "*Groups with levels less than or equal to this variable are unsubscribed.
577 Groups with levels less than `gnus-level-subscribed', which should be
578 less than this variable, are subscribed.")
579
580 (defvar gnus-level-zombie 8
581   "*Groups with this level are zombie groups.")
582
583 (defvar gnus-level-killed 9
584   "*Groups with this level are killed.")
585
586 (defvar gnus-level-default-subscribed 3
587   "*New subscribed groups will be subscribed at this level.")
588
589 (defvar gnus-level-default-unsubscribed 6
590   "*New unsubscribed groups will be unsubscribed at this level.")
591
592 (defvar gnus-activate-level (1+ gnus-level-subscribed)
593   "*Groups higher than this level won't be activated on startup.
594 Setting this variable to something log might save lots of time when
595 you have many groups that you aren't interested in.")
596
597 (defvar gnus-activate-foreign-newsgroups 4
598   "*If nil, Gnus will not check foreign newsgroups at startup.
599 If it is non-nil, it should be a number between one and nine.  Foreign
600 newsgroups that have a level lower or equal to this number will be
601 activated on startup.  For instance, if you want to active all
602 subscribed newsgroups, but not the rest, you'd set this variable to
603 `gnus-level-subscribed'.
604
605 If you subscribe to lots of newsgroups from different servers, startup
606 might take a while.  By setting this variable to nil, you'll save time,
607 but you won't be told how many unread articles there are in the
608 groups.")
609
610 (defvar gnus-save-newsrc-file t
611   "*Non-nil means that Gnus will save the `.newsrc' file.
612 Gnus always saves its own startup file, which is called
613 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
614 be readily understood by other newsreaders.  If you don't plan on
615 using other newsreaders, set this variable to nil to save some time on
616 exit.")
617
618 (defvar gnus-save-killed-list t
619   "*If non-nil, save the list of killed groups to the startup file.
620 If you set this variable to nil, you'll save both time (when starting
621 and quitting) and space (both memory and disk), but it will also mean
622 that Gnus has no record of which groups are new and which are old, so
623 the automatic new newsgroups subscription methods become meaningless.
624
625 You should always set `gnus-check-new-newsgroups' to `ask-server' or
626 nil if you set this variable to nil.")
627
628 (defvar gnus-interactive-catchup t
629   "*If non-nil, require your confirmation when catching up a group.")
630
631 (defvar gnus-interactive-exit t
632   "*If non-nil, require your confirmation when exiting Gnus.")
633
634 (defvar gnus-kill-killed t
635   "*If non-nil, Gnus will apply kill files to already killed articles.
636 If it is nil, Gnus will never apply kill files to articles that have
637 already been through the scoring process, which might very well save lots
638 of time.")
639
640 (defvar gnus-extract-address-components 'gnus-extract-address-components
641   "*Function for extracting address components from a From header.
642 Two pre-defined function exist: `gnus-extract-address-components',
643 which is the default, quite fast, and too simplistic solution, and
644 `mail-extract-address-components', which works much better, but is
645 slower.")
646
647 (defvar gnus-summary-default-score 0
648   "*Default article score level.
649 If this variable is nil, scoring will be disabled.")
650
651 (defvar gnus-summary-zcore-fuzz 0
652   "*Fuzziness factor for the zcore in the summary buffer.
653 Articles with scores closer than this to `gnus-summary-default-score'
654 will not be marked.")
655
656 (defvar gnus-simplify-subject-fuzzy-regexp nil
657   "*Strings to be removed when doing fuzzy matches.
658 This can either be a regular expression or list of regular expressions
659 that will be removed from subject strings if fuzzy subject
660 simplification is selected.")
661
662 (defvar gnus-permanently-visible-groups nil
663   "*Regexp to match groups that should always be listed in the group buffer.
664 This means that they will still be listed when there are no unread
665 articles in the groups.")
666
667 (defvar gnus-list-groups-with-ticked-articles t
668   "*If non-nil, list groups that have only ticked articles.
669 If nil, only list groups that have unread articles.")
670
671 (defvar gnus-group-default-list-level gnus-level-subscribed
672   "*Default listing level.
673 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
674
675 (defvar gnus-group-use-permanent-levels nil
676   "*If non-nil, once you set a level, Gnus will use this level.")
677
678 (defvar gnus-group-list-inactive-groups t
679   "*If non-nil, inactive groups will be listed.")
680
681 (defvar gnus-show-mime nil
682   "*If non-nil, do mime processing of articles.
683 The articles will simply be fed to the function given by
684 `gnus-show-mime-method'.")
685
686 (defvar gnus-strict-mime t
687   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
688
689 (defvar gnus-show-mime-method 'metamail-buffer
690   "*Function to process a MIME message.
691 The function is called from the article buffer.")
692
693 (defvar gnus-decode-encoded-word-method (lambda ())
694   "*Function to decode a MIME encoded-words.
695 The function is called from the article buffer.")
696
697 (defvar gnus-show-threads t
698   "*If non-nil, display threads in summary mode.")
699
700 (defvar gnus-thread-hide-subtree nil
701   "*If non-nil, hide all threads initially.
702 If threads are hidden, you have to run the command
703 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
704 to expose hidden threads.")
705
706 (defvar gnus-thread-hide-killed t
707   "*If non-nil, hide killed threads automatically.")
708
709 (defvar gnus-thread-ignore-subject nil
710   "*If non-nil, ignore subjects and do all threading based on the Reference header.
711 If nil, which is the default, articles that have different subjects
712 from their parents will start separate threads.")
713
714 (defvar gnus-thread-operation-ignore-subject t
715   "*If non-nil, subjects will be ignored when doing thread commands.
716 This affects commands like `gnus-summary-kill-thread' and
717 `gnus-summary-lower-thread'.
718
719 If this variable is nil, articles in the same thread with different
720 subjects will not be included in the operation in question.  If this
721 variable is `fuzzy', only articles that have subjects that are fuzzily
722 equal will be included.")
723
724 (defvar gnus-thread-indent-level 4
725   "*Number that says how much each sub-thread should be indented.")
726
727 (defvar gnus-ignored-newsgroups
728   (purecopy (mapconcat 'identity
729                        '("^to\\."       ; not "real" groups
730                          "^[0-9. \t]+ " ; all digits in name
731                          "[][\"#'()]"   ; bogus characters
732                          )
733                        "\\|"))
734   "*A regexp to match uninteresting newsgroups in the active file.
735 Any lines in the active file matching this regular expression are
736 removed from the newsgroup list before anything else is done to it,
737 thus making them effectively non-existent.")
738
739 (defvar gnus-ignored-headers
740   "^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:"
741   "*All headers that match this regexp will be hidden.
742 This variable can also be a list of regexps of headers to be ignored.
743 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
744
745 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
746   "*All headers that do not match this regexp will be hidden.
747 This variable can also be a list of regexp of headers to remain visible.
748 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
749
750 (defvar gnus-sorted-header-list
751   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
752     "^Cc:" "^Date:" "^Organization:")
753   "*This variable is a list of regular expressions.
754 If it is non-nil, headers that match the regular expressions will
755 be placed first in the article buffer in the sequence specified by
756 this list.")
757
758 (defvar gnus-boring-article-headers
759   '(empty followup-to reply-to)
760   "*Headers that are only to be displayed if they have interesting data.
761 Possible values in this list are `empty', `newsgroups', `followup-to',
762 `reply-to', and `date'.")
763
764 (defvar gnus-show-all-headers nil
765   "*If non-nil, don't hide any headers.")
766
767 (defvar gnus-save-all-headers t
768   "*If non-nil, don't remove any headers before saving.")
769
770 (defvar gnus-saved-headers gnus-visible-headers
771   "*Headers to keep if `gnus-save-all-headers' is nil.
772 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
773 If that variable is nil, however, all headers that match this regexp
774 will be kept while the rest will be deleted before saving.")
775
776 (defvar gnus-inhibit-startup-message nil
777   "*If non-nil, the startup message will not be displayed.")
778
779 (defvar gnus-signature-separator "^-- *$"
780   "Regexp matching signature separator.")
781
782 (defvar gnus-signature-limit nil
783   "Provide a limit to what is considered a signature.
784 If it is a number, no signature may not be longer (in characters) than
785 that number.  If it is a function, the function will be called without
786 any parameters, and if it returns nil, there is no signature in the
787 buffer.  If it is a string, it will be used as a regexp.  If it
788 matches, the text in question is not a signature.")
789
790 (defvar gnus-auto-extend-newsgroup t
791   "*If non-nil, extend newsgroup forward and backward when requested.")
792
793 (defvar gnus-auto-select-first t
794   "*If nil, don't select the first unread article when entering a group.
795 If this variable is `best', select the highest-scored unread article
796 in the group.  If neither nil nor `best', select the first unread
797 article.
798
799 If you want to prevent automatic selection of the first unread article
800 in some newsgroups, set the variable to nil in
801 `gnus-select-group-hook'.")
802
803 (defvar gnus-auto-select-next t
804   "*If non-nil, offer to go to the next group from the end of the previous.
805 If the value is t and the next newsgroup is empty, Gnus will exit
806 summary mode and go back to group mode.  If the value is neither nil
807 nor t, Gnus will select the following unread newsgroup.  In
808 particular, if the value is the symbol `quietly', the next unread
809 newsgroup will be selected without any confirmation, and if it is
810 `almost-quietly', the next group will be selected without any
811 confirmation if you are located on the last article in the group.
812 Finally, if this variable is `slightly-quietly', the `Z n' command
813 will go to the next group without confirmation.")
814
815 (defvar gnus-auto-select-same nil
816   "*If non-nil, select the next article with the same subject.")
817
818 (defvar gnus-summary-check-current nil
819   "*If non-nil, consider the current article when moving.
820 The \"unread\" movement commands will stay on the same line if the
821 current article is unread.")
822
823 (defvar gnus-auto-center-summary t
824   "*If non-nil, always center the current summary buffer.
825 In particular, if `vertical' do only vertical recentering.  If non-nil
826 and non-`vertical', do both horizontal and vertical recentering.")
827
828 (defvar gnus-break-pages t
829   "*If non-nil, do page breaking on articles.
830 The page delimiter is specified by the `gnus-page-delimiter'
831 variable.")
832
833 (defvar gnus-page-delimiter "^\^L"
834   "*Regexp describing what to use as article page delimiters.
835 The default value is \"^\^L\", which is a form linefeed at the
836 beginning of a line.")
837
838 (defvar gnus-use-full-window t
839   "*If non-nil, use the entire Emacs screen.")
840
841 (defvar gnus-window-configuration nil
842   "Obsolete variable.  See `gnus-buffer-configuration'.")
843
844 (defvar gnus-window-min-width 2
845   "*Minimum width of Gnus buffers.")
846
847 (defvar gnus-window-min-height 1
848   "*Minimum height of Gnus buffers.")
849
850 (defvar gnus-buffer-configuration
851   '((group
852      (vertical 1.0
853                (group 1.0 point)
854                (if gnus-carpal '(group-carpal 4))))
855     (summary
856      (vertical 1.0
857                (summary 1.0 point)
858                (if gnus-carpal '(summary-carpal 4))))
859     (article
860      (cond 
861       (gnus-use-picons
862        '(frame 1.0
863                (vertical 1.0
864                          (summary 0.25 point)
865                          (if gnus-carpal '(summary-carpal 4))
866                          (article 1.0))
867                (vertical ((height . 5) (width . 15)
868                           (user-position . t)
869                           (left . -1) (top . 1))
870                          (picons 1.0))))
871       (gnus-use-trees
872        '(vertical 1.0
873                   (summary 0.25 point)
874                   (tree 0.25)
875                   (article 1.0)))
876       (t
877        '(vertical 1.0
878                  (summary 0.25 point)
879                  (if gnus-carpal '(summary-carpal 4))
880                  (article 1.0)))))
881     (server
882      (vertical 1.0
883                (server 1.0 point)
884                (if gnus-carpal '(server-carpal 2))))
885     (browse
886      (vertical 1.0
887                (browse 1.0 point)
888                (if gnus-carpal '(browse-carpal 2))))
889     (message
890      (vertical 1.0
891                (message 1.0 point)))
892     (pick
893      (vertical 1.0
894                (article 1.0 point)))
895     (info
896      (vertical 1.0
897                (info 1.0 point)))
898     (summary-faq
899      (vertical 1.0
900                (summary 0.25)
901                (faq 1.0 point)))
902     (edit-group
903      (vertical 1.0
904                (group 0.5)
905                (edit-group 1.0 point)))
906     (edit-server
907      (vertical 1.0
908                (server 0.5)
909                (edit-server 1.0 point)))
910     (edit-score
911      (vertical 1.0
912                (summary 0.25)
913                (edit-score 1.0 point)))
914     (post
915      (vertical 1.0
916                (post 1.0 point)))
917     (reply
918      (vertical 1.0
919                (article-copy 0.5)
920                (message 1.0 point)))
921     (forward
922      (vertical 1.0
923                (message 1.0 point)))
924     (reply-yank
925      (vertical 1.0
926                (message 1.0 point)))
927     (mail-bounce
928      (vertical 1.0
929                (article 0.5)
930                (message 1.0 point)))
931     (draft
932      (vertical 1.0
933                (draft 1.0 point)))
934     (pipe
935      (vertical 1.0
936                (summary 0.25 point)
937                (if gnus-carpal '(summary-carpal 4))
938                ("*Shell Command Output*" 1.0)))
939     (bug
940      (vertical 1.0
941                ("*Gnus Help Bug*" 0.5)
942                ("*Gnus Bug*" 1.0 point)))
943     (compose-bounce
944      (vertical 1.0
945                (article 0.5)
946                (message 1.0 point))))
947   "Window configuration for all possible Gnus buffers.
948 This variable is a list of lists.  Each of these lists has a NAME and
949 a RULE.  The NAMEs are commonsense names like `group', which names a
950 rule used when displaying the group buffer; `summary', which names a
951 rule for what happens when you enter a group and do not display an
952 article buffer; and so on.  See the value of this variable for a
953 complete list of NAMEs.
954
955 Each RULE is a list of vectors.  The first element in this vector is
956 the name of the buffer to be displayed; the second element is the
957 percentage of the screen this buffer is to occupy (a number in the
958 0.0-0.99 range); the optional third element is `point', which should
959 be present to denote which buffer point is to go to after making this
960 buffer configuration.")
961
962 (defvar gnus-window-to-buffer
963   '((group . gnus-group-buffer)
964     (summary . gnus-summary-buffer)
965     (article . gnus-article-buffer)
966     (server . gnus-server-buffer)
967     (browse . "*Gnus Browse Server*")
968     (edit-group . gnus-group-edit-buffer)
969     (edit-server . gnus-server-edit-buffer)
970     (group-carpal . gnus-carpal-group-buffer)
971     (summary-carpal . gnus-carpal-summary-buffer)
972     (server-carpal . gnus-carpal-server-buffer)
973     (browse-carpal . gnus-carpal-browse-buffer)
974     (edit-score . gnus-score-edit-buffer)
975     (message . gnus-message-buffer)
976     (mail . gnus-message-buffer)
977     (post-news . gnus-message-buffer)
978     (faq . gnus-faq-buffer)
979     (picons . "*Picons*")
980     (tree . gnus-tree-buffer)
981     (info . gnus-info-buffer)
982     (article-copy . gnus-article-copy)
983     (draft . gnus-draft-buffer))
984   "Mapping from short symbols to buffer names or buffer variables.")
985
986 (defvar gnus-carpal nil
987   "*If non-nil, display clickable icons.")
988
989 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
990   "*Function called with a group name when new group is detected.
991 A few pre-made functions are supplied: `gnus-subscribe-randomly'
992 inserts new groups at the beginning of the list of groups;
993 `gnus-subscribe-alphabetically' inserts new groups in strict
994 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
995 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
996 for your decision; `gnus-subscribe-killed' kills all new groups;
997 `gnus-subscribe-zombies' will make all new groups into zombies.")
998
999 ;; Suggested by a bug report by Hallvard B Furuseth.
1000 ;; <h.b.furuseth@usit.uio.no>.
1001 (defvar gnus-subscribe-options-newsgroup-method
1002   (function gnus-subscribe-alphabetically)
1003   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
1004 If, for instance, you want to subscribe to all newsgroups in the
1005 \"no\" and \"alt\" hierarchies, you'd put the following in your
1006 .newsrc file:
1007
1008 options -n no.all alt.all
1009
1010 Gnus will the subscribe all new newsgroups in these hierarchies with
1011 the subscription method in this variable.")
1012
1013 (defvar gnus-subscribe-hierarchical-interactive nil
1014   "*If non-nil, Gnus will offer to subscribe hierarchically.
1015 When a new hierarchy appears, Gnus will ask the user:
1016
1017 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1018
1019 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1020 subscribe to all newsgroups in the hierarchy and `s' will skip this
1021 hierarchy in its entirety.")
1022
1023 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1024   "*Function used for sorting the group buffer.
1025 This function will be called with group info entries as the arguments
1026 for the groups to be sorted.  Pre-made functions include
1027 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1028 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1029 `gnus-group-sort-by-rank'.
1030
1031 This variable can also be a list of sorting functions.  In that case,
1032 the most significant sort function should be the last function in the
1033 list.")
1034
1035 ;; Mark variables suggested by Thomas Michanek
1036 ;; <Thomas.Michanek@telelogic.se>.
1037 (defvar gnus-unread-mark ? 
1038   "*Mark used for unread articles.")
1039 (defvar gnus-ticked-mark ?!
1040   "*Mark used for ticked articles.")
1041 (defvar gnus-dormant-mark ??
1042   "*Mark used for dormant articles.")
1043 (defvar gnus-del-mark ?r
1044   "*Mark used for del'd articles.")
1045 (defvar gnus-read-mark ?R
1046   "*Mark used for read articles.")
1047 (defvar gnus-expirable-mark ?E
1048   "*Mark used for expirable articles.")
1049 (defvar gnus-killed-mark ?K
1050   "*Mark used for killed articles.")
1051 (defvar gnus-souped-mark ?F
1052   "*Mark used for killed articles.")
1053 (defvar gnus-kill-file-mark ?X
1054   "*Mark used for articles killed by kill files.")
1055 (defvar gnus-low-score-mark ?Y
1056   "*Mark used for articles with a low score.")
1057 (defvar gnus-catchup-mark ?C
1058   "*Mark used for articles that are caught up.")
1059 (defvar gnus-replied-mark ?A
1060   "*Mark used for articles that have been replied to.")
1061 (defvar gnus-cached-mark ?*
1062   "*Mark used for articles that are in the cache.")
1063 (defvar gnus-saved-mark ?S
1064   "*Mark used for articles that have been saved to.")
1065 (defvar gnus-process-mark ?#
1066   "*Process mark.")
1067 (defvar gnus-ancient-mark ?O
1068   "*Mark used for ancient articles.")
1069 (defvar gnus-sparse-mark ?Q
1070   "*Mark used for sparsely reffed articles.")
1071 (defvar gnus-canceled-mark ?G
1072   "*Mark used for canceled articles.")
1073 (defvar gnus-score-over-mark ?+
1074   "*Score mark used for articles with high scores.")
1075 (defvar gnus-score-below-mark ?-
1076   "*Score mark used for articles with low scores.")
1077 (defvar gnus-empty-thread-mark ? 
1078   "*There is no thread under the article.")
1079 (defvar gnus-not-empty-thread-mark ?=
1080   "*There is a thread under the article.")
1081
1082 (defvar gnus-view-pseudo-asynchronously nil
1083   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1084
1085 (defvar gnus-view-pseudos nil
1086   "*If `automatic', pseudo-articles will be viewed automatically.
1087 If `not-confirm', pseudos will be viewed automatically, and the user
1088 will not be asked to confirm the command.")
1089
1090 (defvar gnus-view-pseudos-separately t
1091   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1092 If nil, all files that use the same viewing command will be given as a
1093 list of parameters to that command.")
1094
1095 (defvar gnus-insert-pseudo-articles t
1096   "*If non-nil, insert pseudo-articles when decoding articles.")
1097
1098 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1099   "*Format of group lines.
1100 It works along the same lines as a normal formatting string,
1101 with some simple extensions.
1102
1103 %M    Only marked articles (character, \"*\" or \" \")
1104 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1105 %L    Level of subscribedness (integer)
1106 %N    Number of unread articles (integer)
1107 %I    Number of dormant articles (integer)
1108 %i    Number of ticked and dormant (integer)
1109 %T    Number of ticked articles (integer)
1110 %R    Number of read articles (integer)
1111 %t    Total number of articles (integer)
1112 %y    Number of unread, unticked articles (integer)
1113 %G    Group name (string)
1114 %g    Qualified group name (string)
1115 %D    Group description (string)
1116 %s    Select method (string)
1117 %o    Moderated group (char, \"m\")
1118 %p    Process mark (char)
1119 %O    Moderated group (string, \"(m)\" or \"\")
1120 %P    Topic indentation (string)
1121 %l    Whether there are GroupLens predictions for this group (string)
1122 %n    Select from where (string)
1123 %z    A string that look like `<%s:%n>' if a foreign select method is used
1124 %u    User defined specifier.  The next character in the format string should
1125       be a letter.  Gnus will call the function gnus-user-format-function-X,
1126       where X is the letter following %u.  The function will be passed the
1127       current header as argument.  The function should return a string, which
1128       will be inserted into the buffer just like information from any other
1129       group specifier.
1130
1131 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1132 the mouse point move inside the area.  There can only be one such area.
1133
1134 Note that this format specification is not always respected.  For
1135 reasons of efficiency, when listing killed groups, this specification
1136 is ignored altogether.  If the spec is changed considerably, your
1137 output may end up looking strange when listing both alive and killed
1138 groups.
1139
1140 If you use %o or %O, reading the active file will be slower and quite
1141 a bit of extra memory will be used. %D will also worsen performance.
1142 Also note that if you change the format specification to include any
1143 of these specs, you must probably re-start Gnus to see them go into
1144 effect.")
1145
1146 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1147   "*The format specification of the lines in the summary buffer.
1148
1149 It works along the same lines as a normal formatting string,
1150 with some simple extensions.
1151
1152 %N   Article number, left padded with spaces (string)
1153 %S   Subject (string)
1154 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1155 %n   Name of the poster (string)
1156 %a   Extracted name of the poster (string)
1157 %A   Extracted address of the poster (string)
1158 %F   Contents of the From: header (string)
1159 %x   Contents of the Xref: header (string)
1160 %D   Date of the article (string)
1161 %d   Date of the article (string) in DD-MMM format
1162 %M   Message-id of the article (string)
1163 %r   References of the article (string)
1164 %c   Number of characters in the article (integer)
1165 %L   Number of lines in the article (integer)
1166 %I   Indentation based on thread level (a string of spaces)
1167 %T   A string with two possible values: 80 spaces if the article
1168      is on thread level two or larger and 0 spaces on level one
1169 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1170 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1171 %[   Opening bracket (character, \"[\" or \"<\")
1172 %]   Closing bracket (character, \"]\" or \">\")
1173 %>   Spaces of length thread-level (string)
1174 %<   Spaces of length (- 20 thread-level) (string)
1175 %i   Article score (number)
1176 %z   Article zcore (character)
1177 %t   Number of articles under the current thread (number).
1178 %e   Whether the thread is empty or not (character).
1179 %l   GroupLens score (string).
1180 %u   User defined specifier.  The next character in the format string should
1181      be a letter.  Gnus will call the function gnus-user-format-function-X,
1182      where X is the letter following %u.  The function will be passed the
1183      current header as argument.  The function should return a string, which
1184      will be inserted into the summary just like information from any other
1185      summary specifier.
1186
1187 Text between %( and %) will be highlighted with `gnus-mouse-face'
1188 when the mouse point is placed inside the area.  There can only be one
1189 such area.
1190
1191 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1192 with care.  For reasons of efficiency, Gnus will compute what column
1193 these characters will end up in, and \"hard-code\" that.  This means that
1194 it is illegal to have these specs after a variable-length spec.  Well,
1195 you might not be arrested, but your summary buffer will look strange,
1196 which is bad enough.
1197
1198 The smart choice is to have these specs as for to the left as
1199 possible.
1200
1201 This restriction may disappear in later versions of Gnus.")
1202
1203 (defvar gnus-summary-dummy-line-format
1204   "*  %(:                          :%) %S\n"
1205   "*The format specification for the dummy roots in the summary buffer.
1206 It works along the same lines as a normal formatting string,
1207 with some simple extensions.
1208
1209 %S  The subject")
1210
1211 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1212   "*The format specification for the summary mode line.
1213 It works along the same lines as a normal formatting string,
1214 with some simple extensions:
1215
1216 %G  Group name
1217 %p  Unprefixed group name
1218 %A  Current article number
1219 %V  Gnus version
1220 %U  Number of unread articles in the group
1221 %e  Number of unselected articles in the group
1222 %Z  A string with unread/unselected article counts
1223 %g  Shortish group name
1224 %S  Subject of the current article
1225 %u  User-defined spec
1226 %s  Current score file name
1227 %d  Number of dormant articles
1228 %r  Number of articles that have been marked as read in this session
1229 %E  Number of articles expunged by the score files")
1230
1231 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1232   "*The format specification for the article mode line.
1233 See `gnus-summary-mode-line-format' for a closer description.")
1234
1235 (defvar gnus-group-mode-line-format "Gnus: %%b {%M%:%S}"
1236   "*The format specification for the group mode line.
1237 It works along the same lines as a normal formatting string,
1238 with some simple extensions:
1239
1240 %S   The native news server.
1241 %M   The native select method.
1242 %:   \":\" if %S isn't \"\".")
1243
1244 (defvar gnus-valid-select-methods
1245   '(("nntp" post address prompt-address)
1246     ("nnspool" post address)
1247     ("nnvirtual" post-mail virtual prompt-address)
1248     ("nnmbox" mail respool address)
1249     ("nnml" mail respool address)
1250     ("nnmh" mail respool address)
1251     ("nndir" post-mail prompt-address address)
1252     ("nneething" none address prompt-address)
1253     ("nndoc" none address prompt-address)
1254     ("nnbabyl" mail address respool)
1255     ("nnkiboze" post virtual)
1256     ("nnsoup" post-mail address)
1257     ("nndraft" post-mail)
1258     ("nnfolder" mail respool address))
1259   "An alist of valid select methods.
1260 The first element of each list lists should be a string with the name
1261 of the select method.  The other elements may be be the category of
1262 this method (ie. `post', `mail', `none' or whatever) or other
1263 properties that this method has (like being respoolable).
1264 If you implement a new select method, all you should have to change is
1265 this variable.  I think.")
1266
1267 (defvar gnus-updated-mode-lines '(group article summary tree)
1268   "*List of buffers that should update their mode lines.
1269 The list may contain the symbols `group', `article' and `summary'.  If
1270 the corresponding symbol is present, Gnus will keep that mode line
1271 updated with information that may be pertinent.
1272 If this variable is nil, screen refresh may be quicker.")
1273
1274 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1275 (defvar gnus-mode-non-string-length nil
1276   "*Max length of mode-line non-string contents.
1277 If this is nil, Gnus will take space as is needed, leaving the rest
1278 of the modeline intact.")
1279
1280 ;see gnus-cus.el
1281 ;(defvar gnus-mouse-face 'highlight
1282 ;  "*Face used for mouse highlighting in Gnus.
1283 ;No mouse highlights will be done if `gnus-visual' is nil.")
1284
1285 (defvar gnus-summary-mark-below 0
1286   "*Mark all articles with a score below this variable as read.
1287 This variable is local to each summary buffer and usually set by the
1288 score file.")
1289
1290 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1291   "*List of functions used for sorting articles in the summary buffer.
1292 This variable is only used when not using a threaded display.")
1293
1294 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1295   "*List of functions used for sorting threads in the summary buffer.
1296 By default, threads are sorted by article number.
1297
1298 Each function takes two threads and return non-nil if the first thread
1299 should be sorted before the other.  If you use more than one function,
1300 the primary sort function should be the last.  You should probably
1301 always include `gnus-thread-sort-by-number' in the list of sorting
1302 functions -- preferably first.
1303
1304 Ready-mady functions include `gnus-thread-sort-by-number',
1305 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1306 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1307 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1308
1309 (defvar gnus-thread-score-function '+
1310   "*Function used for calculating the total score of a thread.
1311
1312 The function is called with the scores of the article and each
1313 subthread and should then return the score of the thread.
1314
1315 Some functions you can use are `+', `max', or `min'.")
1316
1317 (defvar gnus-summary-expunge-below nil
1318   "All articles that have a score less than this variable will be expunged.")
1319
1320 (defvar gnus-thread-expunge-below nil
1321   "All threads that have a total score less than this variable will be expunged.
1322 See `gnus-thread-score-function' for en explanation of what a
1323 \"thread score\" is.")
1324
1325 (defvar gnus-auto-subscribed-groups
1326   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1327   "*All new groups that match this regexp will be subscribed automatically.
1328 Note that this variable only deals with new groups.  It has no effect
1329 whatsoever on old groups.
1330
1331 New groups that match this regexp will not be handled by
1332 `gnus-subscribe-newsgroup-method'.  Instead, they will
1333 be subscribed using `gnus-subscribe-options-newsgroup-method'.")
1334
1335 (defvar gnus-options-subscribe nil
1336   "*All new groups matching this regexp will be subscribed unconditionally.
1337 Note that this variable deals only with new newsgroups.  This variable
1338 does not affect old newsgroups.
1339
1340 New groups that match this regexp will not be handled by
1341 `gnus-subscribe-newsgroup-method'.  Instead, they will
1342 be subscribed using `gnus-subscribe-options-newsgroup-method'.")
1343
1344 (defvar gnus-options-not-subscribe nil
1345   "*All new groups matching this regexp will be ignored.
1346 Note that this variable deals only with new newsgroups.  This variable
1347 does not affect old (already subscribed) newsgroups.")
1348
1349 (defvar gnus-auto-expirable-newsgroups nil
1350   "*Groups in which to automatically mark read articles as expirable.
1351 If non-nil, this should be a regexp that should match all groups in
1352 which to perform auto-expiry.  This only makes sense for mail groups.")
1353
1354 (defvar gnus-total-expirable-newsgroups nil
1355   "*Groups in which to perform expiry of all read articles.
1356 Use with extreme caution.  All groups that match this regexp will be
1357 expiring - which means that all read articles will be deleted after
1358 (say) one week.  (This only goes for mail groups and the like, of
1359 course.)")
1360
1361 (defvar gnus-group-uncollapsed-levels 1
1362   "Number of group name elements to leave alone when making a short group name.")
1363
1364 (defvar gnus-hidden-properties '(invisible t intangible t)
1365   "Property list to use for hiding text.")
1366
1367 (defvar gnus-modtime-botch nil
1368   "*Non-nil means .newsrc should be deleted prior to save.  
1369 Its use is due to the bogus appearance that .newsrc was modified on
1370 disc.")
1371
1372 ;; Hooks.
1373
1374 (defvar gnus-group-mode-hook nil
1375   "*A hook for Gnus group mode.")
1376
1377 (defvar gnus-summary-mode-hook nil
1378   "*A hook for Gnus summary mode.
1379 This hook is run before any variables are set in the summary buffer.")
1380
1381 (defvar gnus-article-mode-hook nil
1382   "*A hook for Gnus article mode.")
1383
1384 (defvar gnus-summary-prepare-exit-hook nil
1385   "*A hook called when preparing to exit from the summary buffer.
1386 It calls `gnus-summary-expire-articles' by default.")
1387 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1388
1389 (defvar gnus-summary-exit-hook nil
1390   "*A hook called on exit from the summary buffer.")
1391
1392 (defvar gnus-group-catchup-group-hook nil
1393   "*A hook run when catching up a group from the group buffer.")
1394
1395 (defvar gnus-group-update-group-hook nil
1396   "*A hook called when updating group lines.")
1397
1398 (defvar gnus-open-server-hook nil
1399   "*A hook called just before opening connection to the news server.")
1400
1401 (defvar gnus-load-hook nil
1402   "*A hook run while Gnus is loaded.")
1403
1404 (defvar gnus-startup-hook nil
1405   "*A hook called at startup.
1406 This hook is called after Gnus is connected to the NNTP server.")
1407
1408 (defvar gnus-get-new-news-hook nil
1409   "*A hook run just before Gnus checks for new news.")
1410
1411 (defvar gnus-after-getting-new-news-hook nil
1412   "*A hook run after Gnus checks for new news.")
1413
1414 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1415   "*A function that is called to generate the group buffer.
1416 The function is called with three arguments: The first is a number;
1417 all group with a level less or equal to that number should be listed,
1418 if the second is non-nil, empty groups should also be displayed.  If
1419 the third is non-nil, it is a number.  No groups with a level lower
1420 than this number should be displayed.
1421
1422 The only current function implemented is `gnus-group-prepare-flat'.")
1423
1424 (defvar gnus-group-prepare-hook nil
1425   "*A hook called after the group buffer has been generated.
1426 If you want to modify the group buffer, you can use this hook.")
1427
1428 (defvar gnus-summary-prepare-hook nil
1429   "*A hook called after the summary buffer has been generated.
1430 If you want to modify the summary buffer, you can use this hook.")
1431
1432 (defvar gnus-summary-generate-hook nil
1433   "*A hook run just before generating the summary buffer.
1434 This hook is commonly used to customize threading variables and the
1435 like.")
1436
1437 (defvar gnus-article-prepare-hook nil
1438   "*A hook called after an article has been prepared in the article buffer.
1439 If you want to run a special decoding program like nkf, use this hook.")
1440
1441 ;(defvar gnus-article-display-hook nil
1442 ;  "*A hook called after the article is displayed in the article buffer.
1443 ;The hook is designed to change the contents of the article
1444 ;buffer.  Typical functions that this hook may contain are
1445 ;`gnus-article-hide-headers' (hide selected headers),
1446 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1447 ;`gnus-article-hide-signature' (hide signature) and
1448 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1449 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1450 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1451 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1452
1453 (defvar gnus-article-x-face-too-ugly nil
1454   "Regexp matching posters whose face shouldn't be shown automatically.")
1455
1456 (defvar gnus-select-group-hook nil
1457   "*A hook called when a newsgroup is selected.
1458
1459 If you'd like to simplify subjects like the
1460 `gnus-summary-next-same-subject' command does, you can use the
1461 following hook:
1462
1463  (setq gnus-select-group-hook
1464       (list
1465         (lambda ()
1466           (mapcar (lambda (header)
1467                      (mail-header-set-subject
1468                       header
1469                       (gnus-simplify-subject
1470                        (mail-header-subject header) 're-only)))
1471                   gnus-newsgroup-headers))))")
1472
1473 (defvar gnus-select-article-hook nil
1474   "*A hook called when an article is selected.")
1475
1476 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1477   "*A hook called to apply kill files to a group.
1478 This hook is intended to apply a kill file to the selected newsgroup.
1479 The function `gnus-apply-kill-file' is called by default.
1480
1481 Since a general kill file is too heavy to use only for a few
1482 newsgroups, I recommend you to use a lighter hook function.  For
1483 example, if you'd like to apply a kill file to articles which contains
1484 a string `rmgroup' in subject in newsgroup `control', you can use the
1485 following hook:
1486
1487  (setq gnus-apply-kill-hook
1488       (list
1489         (lambda ()
1490           (cond ((string-match \"control\" gnus-newsgroup-name)
1491                  (gnus-kill \"Subject\" \"rmgroup\")
1492                  (gnus-expunge \"X\"))))))")
1493
1494 (defvar gnus-visual-mark-article-hook
1495   (list 'gnus-highlight-selected-summary)
1496   "*Hook run after selecting an article in the summary buffer.
1497 It is meant to be used for highlighting the article in some way.  It
1498 is not run if `gnus-visual' is nil.")
1499
1500 (defvar gnus-parse-headers-hook nil
1501   "*A hook called before parsing the headers.")
1502 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1503
1504 (defvar gnus-exit-group-hook nil
1505   "*A hook called when exiting (not quitting) summary mode.")
1506
1507 (defvar gnus-suspend-gnus-hook nil
1508   "*A hook called when suspending (not exiting) Gnus.")
1509
1510 (defvar gnus-exit-gnus-hook nil
1511   "*A hook called when exiting Gnus.")
1512
1513 (defvar gnus-after-exiting-gnus-hook nil
1514   "*A hook called after exiting Gnus.")
1515
1516 (defvar gnus-save-newsrc-hook nil
1517   "*A hook called before saving any of the newsrc files.")
1518
1519 (defvar gnus-save-quick-newsrc-hook nil
1520   "*A hook called just before saving the quick newsrc file.
1521 Can be used to turn version control on or off.")
1522
1523 (defvar gnus-save-standard-newsrc-hook nil
1524   "*A hook called just before saving the standard newsrc file.
1525 Can be used to turn version control on or off.")
1526
1527 (defvar gnus-summary-update-hook
1528   (list 'gnus-summary-highlight-line)
1529   "*A hook called when a summary line is changed.
1530 The hook will not be called if `gnus-visual' is nil.
1531
1532 The default function `gnus-summary-highlight-line' will
1533 highlight the line according to the `gnus-summary-highlight'
1534 variable.")
1535
1536 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1537   "*A hook called when a group line is changed.
1538 The hook will not be called if `gnus-visual' is nil.
1539
1540 The default function `gnus-group-highlight-line' will
1541 highlight the line according to the `gnus-group-highlight'
1542 variable.")
1543
1544 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1545   "*A hook called when an article is selected for the first time.
1546 The hook is intended to mark an article as read (or unread)
1547 automatically when it is selected.")
1548
1549 (defvar gnus-group-change-level-function nil
1550   "Function run when a group level is changed.
1551 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1552
1553 ;; Remove any hilit infestation.
1554 (add-hook 'gnus-startup-hook
1555           (lambda ()
1556             (remove-hook 'gnus-summary-prepare-hook
1557                          'hilit-rehighlight-buffer-quietly)
1558             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1559             (setq gnus-mark-article-hook
1560                   '(gnus-summary-mark-read-and-unread-as-read))
1561             (remove-hook 'gnus-article-prepare-hook
1562                          'hilit-rehighlight-buffer-quietly)))
1563
1564 \f
1565 ;; Internal variables
1566
1567 (defvar gnus-tree-buffer "*Tree*"
1568   "Buffer where Gnus thread trees are displayed.")
1569
1570 ;; Dummy variable.
1571 (defvar gnus-use-generic-from nil)
1572
1573 (defvar gnus-thread-indent-array nil)
1574 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1575
1576 (defvar gnus-newsrc-file-version nil)
1577
1578 (defvar gnus-method-history nil)
1579 ;; Variable holding the user answers to all method prompts.
1580
1581 (defvar gnus-group-history nil)
1582 ;; Variable holding the user answers to all group prompts.
1583
1584 (defvar gnus-server-alist nil
1585   "List of available servers.")
1586
1587 (defvar gnus-group-indentation-function nil)
1588
1589 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1590
1591 (defvar gnus-goto-missing-group-function nil)
1592
1593 (defvar gnus-override-subscribe-method nil)
1594
1595 (defvar gnus-group-goto-next-group-function nil
1596   "Function to override finding the next group after listing groups.")
1597
1598 (defconst gnus-article-mark-lists
1599   '((marked . tick) (replied . reply)
1600     (expirable . expire) (killed . killed)
1601     (bookmarks . bookmark) (dormant . dormant)
1602     (scored . score) (saved . save)
1603     (cached . cache)
1604     ))
1605
1606 ;; Avoid highlighting in kill files.
1607 (defvar gnus-summary-inhibit-highlight nil)
1608 (defvar gnus-newsgroup-selected-overlay nil)
1609
1610 (defvar gnus-inhibit-hiding nil)
1611 (defvar gnus-group-indentation "")
1612 (defvar gnus-inhibit-limiting nil)
1613 (defvar gnus-created-frames nil)
1614
1615 (defvar gnus-article-mode-map nil)
1616 (defvar gnus-dribble-buffer nil)
1617 (defvar gnus-headers-retrieved-by nil)
1618 (defvar gnus-article-reply nil)
1619 (defvar gnus-override-method nil)
1620 (defvar gnus-article-check-size nil)
1621
1622 (defvar gnus-current-score-file nil)
1623 (defvar gnus-newsgroup-adaptive-score-file nil)
1624 (defvar gnus-scores-exclude-files nil)
1625
1626 (defvar gnus-opened-servers nil)
1627
1628 (defvar gnus-current-move-group nil)
1629 (defvar gnus-current-copy-group nil)
1630 (defvar gnus-current-crosspost-group nil)
1631
1632 (defvar gnus-newsgroup-dependencies nil)
1633 (defvar gnus-newsgroup-async nil)
1634 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1635
1636 (defvar gnus-newsgroup-adaptive nil)
1637
1638 (defvar gnus-summary-display-table nil)
1639 (defvar gnus-summary-display-article-function nil)
1640
1641 (defvar gnus-summary-highlight-line-function nil
1642   "Function called after highlighting a summary line.")
1643
1644 (defvar gnus-group-line-format-alist
1645   `((?M gnus-tmp-marked-mark ?c)
1646     (?S gnus-tmp-subscribed ?c)
1647     (?L gnus-tmp-level ?d)
1648     (?N (cond ((eq number t) "*" )
1649               ((numberp number) 
1650                (int-to-string
1651                 (+ number
1652                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1653                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1654               (t number)) ?s)
1655     (?R gnus-tmp-number-of-read ?s)
1656     (?t gnus-tmp-number-total ?d)
1657     (?y gnus-tmp-number-of-unread ?s)
1658     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1659     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1660     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1661            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1662     (?g gnus-tmp-group ?s)
1663     (?G gnus-tmp-qualified-group ?s)
1664     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1665     (?D gnus-tmp-newsgroup-description ?s)
1666     (?o gnus-tmp-moderated ?c)
1667     (?O gnus-tmp-moderated-string ?s)
1668     (?p gnus-tmp-process-marked ?c)
1669     (?s gnus-tmp-news-server ?s)
1670     (?n gnus-tmp-news-method ?s)
1671     (?P gnus-group-indentation ?s)
1672     (?l gnus-tmp-grouplens ?s)
1673     (?z gnus-tmp-news-method-string ?s)
1674     (?u gnus-tmp-user-defined ?s)))
1675
1676 (defvar gnus-summary-line-format-alist
1677   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1678     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1679     (?s gnus-tmp-subject-or-nil ?s)
1680     (?n gnus-tmp-name ?s)
1681     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1682         ?s)
1683     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1684             gnus-tmp-from) ?s)
1685     (?F gnus-tmp-from ?s)
1686     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1687     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1688     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1689     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1690     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1691     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1692     (?L gnus-tmp-lines ?d)
1693     (?I gnus-tmp-indentation ?s)
1694     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1695     (?R gnus-tmp-replied ?c)
1696     (?\[ gnus-tmp-opening-bracket ?c)
1697     (?\] gnus-tmp-closing-bracket ?c)
1698     (?\> (make-string gnus-tmp-level ? ) ?s)
1699     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1700     (?i gnus-tmp-score ?d)
1701     (?z gnus-tmp-score-char ?c)
1702     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1703     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1704     (?U gnus-tmp-unread ?c)
1705     (?t (gnus-summary-number-of-articles-in-thread
1706          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1707         ?d)
1708     (?e (gnus-summary-number-of-articles-in-thread
1709          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1710         ?c)
1711     (?u gnus-tmp-user-defined ?s))
1712   "An alist of format specifications that can appear in summary lines,
1713 and what variables they correspond with, along with the type of the
1714 variable (string, integer, character, etc).")
1715
1716 (defvar gnus-summary-dummy-line-format-alist
1717   `((?S gnus-tmp-subject ?s)
1718     (?N gnus-tmp-number ?d)
1719     (?u gnus-tmp-user-defined ?s)))
1720
1721 (defvar gnus-summary-mode-line-format-alist
1722   `((?G gnus-tmp-group-name ?s)
1723     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1724     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1725     (?A gnus-tmp-article-number ?d)
1726     (?Z gnus-tmp-unread-and-unselected ?s)
1727     (?V gnus-version ?s)
1728     (?U gnus-tmp-unread-and-unticked ?d)
1729     (?S gnus-tmp-subject ?s)
1730     (?e gnus-tmp-unselected ?d)
1731     (?u gnus-tmp-user-defined ?s)
1732     (?d (length gnus-newsgroup-dormant) ?d)
1733     (?t (length gnus-newsgroup-marked) ?d)
1734     (?r (length gnus-newsgroup-reads) ?d)
1735     (?E gnus-newsgroup-expunged-tally ?d)
1736     (?s (gnus-current-score-file-nondirectory) ?s)))
1737
1738 (defvar gnus-article-mode-line-format-alist
1739   gnus-summary-mode-line-format-alist)
1740
1741 (defvar gnus-group-mode-line-format-alist
1742   `((?S gnus-tmp-news-server ?s)
1743     (?M gnus-tmp-news-method ?s)
1744     (?u gnus-tmp-user-defined ?s)
1745     (?: gnus-tmp-colon ?s)))
1746
1747 (defvar gnus-have-read-active-file nil)
1748
1749 (defconst gnus-maintainer
1750   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1751   "The mail address of the Gnus maintainers.")
1752
1753 (defconst gnus-version-number "5.2.29"
1754   "Version number for this version of Gnus.")
1755
1756 (defconst gnus-version (format "Gnus v%s" gnus-version-number)
1757   "Version string for this version of Gnus.")
1758
1759 (defvar gnus-info-nodes
1760   '((gnus-group-mode "(gnus)The Group Buffer")
1761     (gnus-summary-mode "(gnus)The Summary Buffer")
1762     (gnus-article-mode "(gnus)The Article Buffer")
1763     (gnus-server-mode "(gnus)The Server Buffer")
1764     (gnus-browse-mode "(gnus)Browse Foreign Server")
1765     (gnus-tree-mode "(gnus)Tree Display")
1766     )
1767   "Alist of major modes and related Info nodes.")
1768
1769 (defvar gnus-group-buffer "*Group*")
1770 (defvar gnus-summary-buffer "*Summary*")
1771 (defvar gnus-article-buffer "*Article*")
1772 (defvar gnus-server-buffer "*Server*")
1773
1774 (defvar gnus-work-buffer " *gnus work*")
1775
1776 (defvar gnus-original-article-buffer " *Original Article*")
1777 (defvar gnus-original-article nil)
1778
1779 (defvar gnus-buffer-list nil
1780   "Gnus buffers that should be killed on exit.")
1781
1782 (defvar gnus-slave nil
1783   "Whether this Gnus is a slave or not.")
1784
1785 (defvar gnus-variable-list
1786   '(gnus-newsrc-options gnus-newsrc-options-n
1787     gnus-newsrc-last-checked-date
1788     gnus-newsrc-alist gnus-server-alist
1789     gnus-killed-list gnus-zombie-list
1790     gnus-topic-topology gnus-topic-alist
1791     gnus-format-specs)
1792   "Gnus variables saved in the quick startup file.")
1793
1794 (defvar gnus-newsrc-options nil
1795   "Options line in the .newsrc file.")
1796
1797 (defvar gnus-newsrc-options-n nil
1798   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1799
1800 (defvar gnus-newsrc-last-checked-date nil
1801   "Date Gnus last asked server for new newsgroups.")
1802
1803 (defvar gnus-topic-topology nil
1804   "The complete topic hierarchy.")
1805
1806 (defvar gnus-topic-alist nil
1807   "The complete topic-group alist.")
1808
1809 (defvar gnus-newsrc-alist nil
1810   "Assoc list of read articles.
1811 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1812
1813 (defvar gnus-newsrc-hashtb nil
1814   "Hashtable of gnus-newsrc-alist.")
1815
1816 (defvar gnus-killed-list nil
1817   "List of killed newsgroups.")
1818
1819 (defvar gnus-killed-hashtb nil
1820   "Hash table equivalent of gnus-killed-list.")
1821
1822 (defvar gnus-zombie-list nil
1823   "List of almost dead newsgroups.")
1824
1825 (defvar gnus-description-hashtb nil
1826   "Descriptions of newsgroups.")
1827
1828 (defvar gnus-list-of-killed-groups nil
1829   "List of newsgroups that have recently been killed by the user.")
1830
1831 (defvar gnus-active-hashtb nil
1832   "Hashtable of active articles.")
1833
1834 (defvar gnus-moderated-list nil
1835   "List of moderated newsgroups.")
1836
1837 (defvar gnus-group-marked nil)
1838
1839 (defvar gnus-current-startup-file nil
1840   "Startup file for the current host.")
1841
1842 (defvar gnus-last-search-regexp nil
1843   "Default regexp for article search command.")
1844
1845 (defvar gnus-last-shell-command nil
1846   "Default shell command on article.")
1847
1848 (defvar gnus-current-select-method nil
1849   "The current method for selecting a newsgroup.")
1850
1851 (defvar gnus-group-list-mode nil)
1852
1853 (defvar gnus-article-internal-prepare-hook nil)
1854
1855 (defvar gnus-newsgroup-name nil)
1856 (defvar gnus-newsgroup-begin nil)
1857 (defvar gnus-newsgroup-end nil)
1858 (defvar gnus-newsgroup-last-rmail nil)
1859 (defvar gnus-newsgroup-last-mail nil)
1860 (defvar gnus-newsgroup-last-folder nil)
1861 (defvar gnus-newsgroup-last-file nil)
1862 (defvar gnus-newsgroup-auto-expire nil)
1863 (defvar gnus-newsgroup-active nil)
1864
1865 (defvar gnus-newsgroup-data nil)
1866 (defvar gnus-newsgroup-data-reverse nil)
1867 (defvar gnus-newsgroup-limit nil)
1868 (defvar gnus-newsgroup-limits nil)
1869
1870 (defvar gnus-newsgroup-unreads nil
1871   "List of unread articles in the current newsgroup.")
1872
1873 (defvar gnus-newsgroup-unselected nil
1874   "List of unselected unread articles in the current newsgroup.")
1875
1876 (defvar gnus-newsgroup-reads nil
1877   "Alist of read articles and article marks in the current newsgroup.")
1878
1879 (defvar gnus-newsgroup-expunged-tally nil)
1880
1881 (defvar gnus-newsgroup-marked nil
1882   "List of ticked articles in the current newsgroup (a subset of unread art).")
1883
1884 (defvar gnus-newsgroup-killed nil
1885   "List of ranges of articles that have been through the scoring process.")
1886
1887 (defvar gnus-newsgroup-cached nil
1888   "List of articles that come from the article cache.")
1889
1890 (defvar gnus-newsgroup-saved nil
1891   "List of articles that have been saved.")
1892
1893 (defvar gnus-newsgroup-kill-headers nil)
1894
1895 (defvar gnus-newsgroup-replied nil
1896   "List of articles that have been replied to in the current newsgroup.")
1897
1898 (defvar gnus-newsgroup-expirable nil
1899   "List of articles in the current newsgroup that can be expired.")
1900
1901 (defvar gnus-newsgroup-processable nil
1902   "List of articles in the current newsgroup that can be processed.")
1903
1904 (defvar gnus-newsgroup-bookmarks nil
1905   "List of articles in the current newsgroup that have bookmarks.")
1906
1907 (defvar gnus-newsgroup-dormant nil
1908   "List of dormant articles in the current newsgroup.")
1909
1910 (defvar gnus-newsgroup-scored nil
1911   "List of scored articles in the current newsgroup.")
1912
1913 (defvar gnus-newsgroup-headers nil
1914   "List of article headers in the current newsgroup.")
1915
1916 (defvar gnus-newsgroup-threads nil)
1917
1918 (defvar gnus-newsgroup-prepared nil
1919   "Whether the current group has been prepared properly.")
1920
1921 (defvar gnus-newsgroup-ancient nil
1922   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1923
1924 (defvar gnus-newsgroup-sparse nil)
1925
1926 (defvar gnus-current-article nil)
1927 (defvar gnus-article-current nil)
1928 (defvar gnus-current-headers nil)
1929 (defvar gnus-have-all-headers nil)
1930 (defvar gnus-last-article nil)
1931 (defvar gnus-newsgroup-history nil)
1932 (defvar gnus-current-kill-article nil)
1933
1934 ;; Save window configuration.
1935 (defvar gnus-prev-winconf nil)
1936
1937 (defvar gnus-summary-mark-positions nil)
1938 (defvar gnus-group-mark-positions nil)
1939
1940 (defvar gnus-reffed-article-number nil)
1941
1942 ;;; Let the byte-compiler know that we know about this variable.
1943 (defvar rmail-default-rmail-file)
1944
1945 (defvar gnus-cache-removable-articles nil)
1946
1947 (defvar gnus-dead-summary nil)
1948
1949 (defconst gnus-summary-local-variables
1950   '(gnus-newsgroup-name
1951     gnus-newsgroup-begin gnus-newsgroup-end
1952     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1953     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1954     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1955     gnus-newsgroup-unselected gnus-newsgroup-marked
1956     gnus-newsgroup-reads gnus-newsgroup-saved
1957     gnus-newsgroup-replied gnus-newsgroup-expirable
1958     gnus-newsgroup-processable gnus-newsgroup-killed
1959     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1960     gnus-newsgroup-headers gnus-newsgroup-threads
1961     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1962     gnus-current-article gnus-current-headers gnus-have-all-headers
1963     gnus-last-article gnus-article-internal-prepare-hook
1964     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1965     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1966     gnus-newsgroup-async gnus-thread-expunge-below
1967     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1968     (gnus-summary-mark-below . global)
1969     gnus-newsgroup-active gnus-scores-exclude-files
1970     gnus-newsgroup-history gnus-newsgroup-ancient
1971     gnus-newsgroup-sparse
1972     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1973     gnus-newsgroup-adaptive-score-file
1974     (gnus-newsgroup-expunged-tally . 0)
1975     gnus-cache-removable-articles gnus-newsgroup-cached
1976     gnus-newsgroup-data gnus-newsgroup-data-reverse
1977     gnus-newsgroup-limit gnus-newsgroup-limits)
1978   "Variables that are buffer-local to the summary buffers.")
1979
1980 (defconst gnus-bug-message
1981   "Sending a bug report to the Gnus Towers.
1982 ========================================
1983
1984 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1985 be sent to the Gnus Bug Exterminators.
1986
1987 At the bottom of the buffer you'll see lots of variable settings.
1988 Please do not delete those.  They will tell the Bug People what your
1989 environment is, so that it will be easier to locate the bugs.
1990
1991 If you have found a bug that makes Emacs go \"beep\", set
1992 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1993 and include the backtrace in your bug report.
1994
1995 Please describe the bug in annoying, painstaking detail.
1996
1997 Thank you for your help in stamping out bugs.
1998 ")
1999
2000 ;;; End of variables.
2001
2002 ;; Define some autoload functions Gnus might use.
2003 (eval-and-compile
2004
2005   ;; This little mapcar goes through the list below and marks the
2006   ;; symbols in question as autoloaded functions.
2007   (mapcar
2008    (lambda (package)
2009      (let ((interactive (nth 1 (memq ':interactive package))))
2010        (mapcar
2011         (lambda (function)
2012           (let (keymap)
2013             (when (consp function)
2014               (setq keymap (car (memq 'keymap function)))
2015               (setq function (car function)))
2016             (autoload function (car package) nil interactive keymap)))
2017         (if (eq (nth 1 package) ':interactive)
2018             (cdddr package)
2019           (cdr package)))))
2020    '(("metamail" metamail-buffer)
2021      ("info" Info-goto-node)
2022      ("hexl" hexl-hex-string-to-integer)
2023      ("pp" pp pp-to-string pp-eval-expression)
2024      ("mail-extr" mail-extract-address-components)
2025      ("nnmail" nnmail-split-fancy nnmail-article-group)
2026      ("nnvirtual" nnvirtual-catchup-group)
2027      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
2028       timezone-make-sortable-date timezone-make-time-string)
2029      ("rmailout" rmail-output)
2030      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2031       rmail-show-message)
2032      ("gnus-soup" :interactive t
2033       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2034       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2035      ("nnsoup" nnsoup-pack-replies)
2036      ("gnus-scomo" :interactive t gnus-score-mode)
2037      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2038       gnus-Folder-save-name gnus-folder-save-name)
2039      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2040      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2041       gnus-server-make-menu-bar gnus-article-make-menu-bar
2042       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2043       gnus-summary-highlight-line gnus-carpal-setup-buffer
2044       gnus-group-highlight-line
2045       gnus-article-add-button gnus-insert-next-page-button
2046       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2047      ("gnus-vis" :interactive t
2048       gnus-article-push-button gnus-article-press-button
2049       gnus-article-highlight gnus-article-highlight-some
2050       gnus-article-highlight-headers gnus-article-highlight-signature
2051       gnus-article-add-buttons gnus-article-add-buttons-to-head
2052       gnus-article-next-button gnus-article-prev-button)
2053      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2054       gnus-demon-add-disconnection gnus-demon-add-handler
2055       gnus-demon-remove-handler)
2056      ("gnus-demon" :interactive t
2057       gnus-demon-init gnus-demon-cancel)
2058      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2059       gnus-tree-open gnus-tree-close)
2060      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2061       gnus-nocem-unwanted-article-p)
2062      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2063      ("gnus-srvr" gnus-browse-foreign-server)
2064      ("gnus-cite" :interactive t
2065       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2066       gnus-article-hide-citation gnus-article-fill-cited-article
2067       gnus-article-hide-citation-in-followups)
2068      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2069       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2070       gnus-execute gnus-expunge)
2071      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2072       gnus-cache-possibly-remove-articles gnus-cache-request-article
2073       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2074       gnus-cache-enter-remove-article gnus-cached-article-p
2075       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2076      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2077       gnus-cache-remove-article)
2078      ("gnus-score" :interactive t
2079       gnus-summary-increase-score gnus-summary-lower-score
2080       gnus-score-flush-cache gnus-score-close
2081       gnus-score-raise-same-subject-and-select
2082       gnus-score-raise-same-subject gnus-score-default
2083       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2084       gnus-score-lower-same-subject gnus-score-lower-thread
2085       gnus-possibly-score-headers gnus-summary-raise-score 
2086       gnus-summary-set-score gnus-summary-current-score)
2087      ("gnus-score"
2088       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2089       gnus-current-score-file-nondirectory gnus-score-adaptive
2090       gnus-score-find-trace gnus-score-file-name)
2091      ("gnus-edit" :interactive t gnus-score-customize)
2092      ("gnus-topic" :interactive t gnus-topic-mode)
2093      ("gnus-topic" gnus-topic-remove-group)
2094      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2095      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2096      ("gnus-uu" :interactive t
2097       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2098       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2099       gnus-uu-mark-by-regexp gnus-uu-mark-all
2100       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2101       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2102       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2103       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2104       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2105       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2106       gnus-uu-decode-binhex-view)
2107      ("gnus-msg" (gnus-summary-send-map keymap)
2108       gnus-mail-yank-original gnus-mail-send-and-exit
2109       gnus-article-mail gnus-new-mail gnus-mail-reply)
2110      ("gnus-msg" :interactive t
2111       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2112       gnus-summary-followup gnus-summary-followup-with-original
2113       gnus-summary-cancel-article gnus-summary-supersede-article
2114       gnus-post-news gnus-inews-news 
2115       gnus-summary-reply gnus-summary-reply-with-original
2116       gnus-summary-mail-forward gnus-summary-mail-other-window
2117       gnus-bug)
2118      ("gnus-picon" :interactive t gnus-article-display-picons
2119       gnus-group-display-picons gnus-picons-article-display-x-face
2120       gnus-picons-display-x-face)
2121      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2122       gnus-grouplens-mode)
2123      ("smiley" :interactive t gnus-smiley-display)
2124      ("gnus-vm" gnus-vm-mail-setup)
2125      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2126       gnus-summary-save-article-vm))))
2127
2128 \f
2129
2130 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2131 ;; If you want the cursor to go somewhere else, set these two
2132 ;; functions in some startup hook to whatever you want.
2133 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2134 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2135
2136 ;;; Various macros and substs.
2137
2138 (defun gnus-header-from (header)
2139   (mail-header-from header))
2140
2141 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2142   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2143   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2144         (w (make-symbol "w"))
2145         (buf (make-symbol "buf")))
2146     `(let* ((,tempvar (selected-window))
2147             (,buf ,buffer)
2148             (,w (get-buffer-window ,buf 'visible)))
2149        (unwind-protect
2150            (progn
2151              (if ,w
2152                  (select-window ,w)
2153                (pop-to-buffer ,buf))
2154              ,@forms)
2155          (select-window ,tempvar)))))
2156
2157 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
2158 (put 'gnus-eval-in-buffer-window 'lisp-indent-hook 1)
2159 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
2160
2161 (defmacro gnus-gethash (string hashtable)
2162   "Get hash value of STRING in HASHTABLE."
2163   `(symbol-value (intern-soft ,string ,hashtable)))
2164
2165 (defmacro gnus-sethash (string value hashtable)
2166   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2167   `(set (intern ,string ,hashtable) ,value))
2168
2169 (defmacro gnus-intern-safe (string hashtable)
2170   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2171   `(let ((symbol (intern ,string ,hashtable)))
2172      (or (boundp symbol)
2173          (set symbol nil))
2174      symbol))
2175
2176 (defmacro gnus-group-unread (group)
2177   "Get the currently computed number of unread articles in GROUP."
2178   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2179
2180 (defmacro gnus-group-entry (group)
2181   "Get the newsrc entry for GROUP."
2182   `(gnus-gethash ,group gnus-newsrc-hashtb))
2183
2184 (defmacro gnus-active (group)
2185   "Get active info on GROUP."
2186   `(gnus-gethash ,group gnus-active-hashtb))
2187
2188 (defmacro gnus-set-active (group active)
2189   "Set GROUP's active info."
2190   `(gnus-sethash ,group ,active gnus-active-hashtb))
2191
2192 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2193 ;;   function `substring' might cut on a middle of multi-octet
2194 ;;   character.
2195 (defun gnus-truncate-string (str width)
2196   (substring str 0 width))
2197
2198 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2199 ;; to limit the length of a string.  This function is necessary since
2200 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2201 (defsubst gnus-limit-string (str width)
2202   (if (> (length str) width)
2203       (substring str 0 width)
2204     str))
2205
2206 (defsubst gnus-simplify-subject-re (subject)
2207   "Remove \"Re:\" from subject lines."
2208   (if (string-match "^[Rr][Ee]: *" subject)
2209       (substring subject (match-end 0))
2210     subject))
2211
2212 (defsubst gnus-functionp (form)
2213   "Return non-nil if FORM is funcallable."
2214   (or (and (symbolp form) (fboundp form))
2215       (and (listp form) (eq (car form) 'lambda))))
2216
2217 (defsubst gnus-goto-char (point)
2218   (and point (goto-char point)))
2219
2220 (defmacro gnus-buffer-exists-p (buffer)
2221   `(let ((buffer ,buffer))
2222      (and buffer
2223           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2224                    buffer))))
2225
2226 (defmacro gnus-kill-buffer (buffer)
2227   `(let ((buf ,buffer))
2228      (if (gnus-buffer-exists-p buf)
2229          (kill-buffer buf))))
2230
2231 (defsubst gnus-point-at-bol ()
2232   "Return point at the beginning of the line."
2233   (let ((p (point)))
2234     (beginning-of-line)
2235     (prog1
2236         (point)
2237       (goto-char p))))
2238
2239 (defsubst gnus-point-at-eol ()
2240   "Return point at the end of the line."
2241   (let ((p (point)))
2242     (end-of-line)
2243     (prog1
2244         (point)
2245       (goto-char p))))
2246
2247 (defun gnus-alive-p ()
2248   "Say whether Gnus is running or not."
2249   (and gnus-group-buffer
2250        (get-buffer gnus-group-buffer)))
2251
2252 (defun gnus-delete-first (elt list)
2253   "Delete by side effect the first occurrence of ELT as a member of LIST."
2254   (if (equal (car list) elt)
2255       (cdr list)
2256     (let ((total list))
2257       (while (and (cdr list)
2258                   (not (equal (cadr list) elt)))
2259         (setq list (cdr list)))
2260       (when (cdr list)
2261         (setcdr list (cddr list)))
2262       total)))
2263
2264 ;; Delete the current line (and the next N lines.);
2265 (defmacro gnus-delete-line (&optional n)
2266   `(delete-region (progn (beginning-of-line) (point))
2267                   (progn (forward-line ,(or n 1)) (point))))
2268
2269 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2270 (defvar gnus-init-inhibit nil)
2271 (defun gnus-read-init-file (&optional inhibit-next)
2272   (if gnus-init-inhibit
2273       (setq gnus-init-inhibit nil)
2274     (setq gnus-init-inhibit inhibit-next)
2275     (and gnus-init-file
2276          (or (and (file-exists-p gnus-init-file)
2277                   ;; Don't try to load a directory.
2278                   (not (file-directory-p gnus-init-file)))
2279              (file-exists-p (concat gnus-init-file ".el"))
2280              (file-exists-p (concat gnus-init-file ".elc")))
2281          (condition-case var
2282              (load gnus-init-file nil t)
2283            (error
2284             (error "Error in %s: %s" gnus-init-file var))))))
2285
2286 ;; Info access macros.
2287
2288 (defmacro gnus-info-group (info)
2289   `(nth 0 ,info))
2290 (defmacro gnus-info-rank (info)
2291   `(nth 1 ,info))
2292 (defmacro gnus-info-read (info)
2293   `(nth 2 ,info))
2294 (defmacro gnus-info-marks (info)
2295   `(nth 3 ,info))
2296 (defmacro gnus-info-method (info)
2297   `(nth 4 ,info))
2298 (defmacro gnus-info-params (info)
2299   `(nth 5 ,info))
2300
2301 (defmacro gnus-info-level (info)
2302   `(let ((rank (gnus-info-rank ,info)))
2303      (if (consp rank)
2304          (car rank)
2305        rank)))
2306 (defmacro gnus-info-score (info)
2307   `(let ((rank (gnus-info-rank ,info)))
2308      (or (and (consp rank) (cdr rank)) 0)))
2309
2310 (defmacro gnus-info-set-group (info group)
2311   `(setcar ,info ,group))
2312 (defmacro gnus-info-set-rank (info rank)
2313   `(setcar (nthcdr 1 ,info) ,rank))
2314 (defmacro gnus-info-set-read (info read)
2315   `(setcar (nthcdr 2 ,info) ,read))
2316 (defmacro gnus-info-set-marks (info marks)
2317   `(setcar (nthcdr 3 ,info) ,marks))
2318 (defmacro gnus-info-set-method (info method)
2319   `(setcar (nthcdr 4 ,info) ,method))
2320 (defmacro gnus-info-set-params (info params)
2321   `(setcar (nthcdr 5 ,info) ,params))
2322
2323 (defmacro gnus-info-set-level (info level)
2324   `(let ((rank (cdr ,info)))
2325      (if (consp (car rank))
2326          (setcar (car rank) ,level)
2327        (setcar rank ,level))))
2328 (defmacro gnus-info-set-score (info score)
2329   `(let ((rank (cdr ,info)))
2330      (if (consp (car rank))
2331          (setcdr (car rank) ,score)
2332        (setcar rank (cons (car rank) ,score)))))
2333
2334 (defmacro gnus-get-info (group)
2335   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2336
2337 (defun gnus-byte-code (func)
2338   "Return a form that can be `eval'ed based on FUNC."
2339   (let ((fval (symbol-function func)))
2340     (if (byte-code-function-p fval)
2341         (let ((flist (append fval nil)))
2342           (setcar flist 'byte-code)
2343           flist)
2344       (cons 'progn (cddr fval)))))
2345
2346 ;; Find out whether the gnus-visual TYPE is wanted.
2347 (defun gnus-visual-p (&optional type class)
2348   (and gnus-visual                      ; Has to be non-nil, at least.
2349        (if (not type)                   ; We don't care about type.
2350            gnus-visual
2351          (if (listp gnus-visual)        ; It's a list, so we check it.
2352              (or (memq type gnus-visual)
2353                  (memq class gnus-visual))
2354            t))))
2355
2356 ;;; Load the compatability functions.
2357
2358 (require 'gnus-cus)
2359 (require 'gnus-ems)
2360
2361 \f
2362 ;;;
2363 ;;; Shutdown
2364 ;;;
2365
2366 (defvar gnus-shutdown-alist nil)
2367
2368 (defun gnus-add-shutdown (function &rest symbols)
2369   "Run FUNCTION whenever one of SYMBOLS is shut down."
2370   (push (cons function symbols) gnus-shutdown-alist))
2371
2372 (defun gnus-shutdown (symbol)
2373   "Shut down everything that waits for SYMBOL."
2374   (let ((alist gnus-shutdown-alist)
2375         entry)
2376     (while (setq entry (pop alist))
2377       (when (memq symbol (cdr entry))
2378         (funcall (car entry))))))
2379
2380 \f
2381
2382 ;; Format specs.  The chunks below are the machine-generated forms
2383 ;; that are to be evaled as the result of the default format strings.
2384 ;; We write them in here to get them byte-compiled.  That way the
2385 ;; default actions will be quite fast, while still retaining the full
2386 ;; flexibility of the user-defined format specs.
2387
2388 ;; First we have lots of dummy defvars to let the compiler know these
2389 ;; are really dynamic variables.
2390
2391 (defvar gnus-tmp-unread)
2392 (defvar gnus-tmp-replied)
2393 (defvar gnus-tmp-score-char)
2394 (defvar gnus-tmp-indentation)
2395 (defvar gnus-tmp-opening-bracket)
2396 (defvar gnus-tmp-lines)
2397 (defvar gnus-tmp-name)
2398 (defvar gnus-tmp-closing-bracket)
2399 (defvar gnus-tmp-subject-or-nil)
2400 (defvar gnus-tmp-subject)
2401 (defvar gnus-tmp-marked)
2402 (defvar gnus-tmp-marked-mark)
2403 (defvar gnus-tmp-subscribed)
2404 (defvar gnus-tmp-process-marked)
2405 (defvar gnus-tmp-number-of-unread)
2406 (defvar gnus-tmp-group-name)
2407 (defvar gnus-tmp-group)
2408 (defvar gnus-tmp-article-number)
2409 (defvar gnus-tmp-unread-and-unselected)
2410 (defvar gnus-tmp-news-method)
2411 (defvar gnus-tmp-news-server)
2412 (defvar gnus-tmp-article-number)
2413 (defvar gnus-mouse-face)
2414 (defvar gnus-mouse-face-prop)
2415
2416 (defun gnus-summary-line-format-spec ()
2417   (insert gnus-tmp-unread gnus-tmp-replied
2418           gnus-tmp-score-char gnus-tmp-indentation)
2419   (gnus-put-text-property
2420    (point)
2421    (progn
2422      (insert
2423       gnus-tmp-opening-bracket
2424       (format "%4d: %-20s"
2425               gnus-tmp-lines
2426               (if (> (length gnus-tmp-name) 20)
2427                   (substring gnus-tmp-name 0 20)
2428                 gnus-tmp-name))
2429       gnus-tmp-closing-bracket)
2430      (point))
2431    gnus-mouse-face-prop gnus-mouse-face)
2432   (insert " " gnus-tmp-subject-or-nil "\n"))
2433
2434 (defvar gnus-summary-line-format-spec
2435   (gnus-byte-code 'gnus-summary-line-format-spec))
2436
2437 (defun gnus-summary-dummy-line-format-spec ()
2438   (insert "*  ")
2439   (gnus-put-text-property
2440    (point)
2441    (progn
2442      (insert ":                          :")
2443      (point))
2444    gnus-mouse-face-prop gnus-mouse-face)
2445   (insert " " gnus-tmp-subject "\n"))
2446
2447 (defvar gnus-summary-dummy-line-format-spec
2448   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2449
2450 (defun gnus-group-line-format-spec ()
2451   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2452           gnus-tmp-process-marked
2453           gnus-group-indentation
2454           (format "%5s: " gnus-tmp-number-of-unread))
2455   (gnus-put-text-property
2456    (point)
2457    (progn
2458      (insert gnus-tmp-group "\n")
2459      (1- (point)))
2460    gnus-mouse-face-prop gnus-mouse-face))
2461 (defvar gnus-group-line-format-spec
2462   (gnus-byte-code 'gnus-group-line-format-spec))
2463
2464 (defvar gnus-format-specs
2465   `((version . ,emacs-version)
2466     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2467     (summary-dummy ,gnus-summary-dummy-line-format
2468                    ,gnus-summary-dummy-line-format-spec)
2469     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2470
2471 (defvar gnus-article-mode-line-format-spec nil)
2472 (defvar gnus-summary-mode-line-format-spec nil)
2473 (defvar gnus-group-mode-line-format-spec nil)
2474
2475 ;;; Phew.  All that gruft is over, fortunately.
2476
2477 \f
2478 ;;;
2479 ;;; Gnus Utility Functions
2480 ;;;
2481
2482 (defun gnus-extract-address-components (from)
2483   (let (name address)
2484     ;; First find the address - the thing with the @ in it.  This may
2485     ;; not be accurate in mail addresses, but does the trick most of
2486     ;; the time in news messages.
2487     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2488         (setq address (substring from (match-beginning 0) (match-end 0))))
2489     ;; Then we check whether the "name <address>" format is used.
2490     (and address
2491          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2492          ;; Linear white space is not required.
2493          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2494          (and (setq name (substring from 0 (match-beginning 0)))
2495               ;; Strip any quotes from the name.
2496               (string-match "\".*\"" name)
2497               (setq name (substring name 1 (1- (match-end 0))))))
2498     ;; If not, then "address (name)" is used.
2499     (or name
2500         (and (string-match "(.+)" from)
2501              (setq name (substring from (1+ (match-beginning 0))
2502                                    (1- (match-end 0)))))
2503         (and (string-match "()" from)
2504              (setq name address))
2505         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2506         ;; XOVER might not support folded From headers.
2507         (and (string-match "(.*" from)
2508              (setq name (substring from (1+ (match-beginning 0))
2509                                    (match-end 0)))))
2510     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2511     (list (or name from) (or address from))))
2512
2513 (defun gnus-fetch-field (field)
2514   "Return the value of the header FIELD of current article."
2515   (save-excursion
2516     (save-restriction
2517       (let ((case-fold-search t)
2518             (inhibit-point-motion-hooks t))
2519         (nnheader-narrow-to-headers)
2520         (message-fetch-field field)))))
2521
2522 (defun gnus-goto-colon ()
2523   (beginning-of-line)
2524   (search-forward ":" (gnus-point-at-eol) t))
2525
2526 ;;;###autoload
2527 (defun gnus-update-format (var)
2528   "Update the format specification near point."
2529   (interactive
2530    (list
2531     (save-excursion
2532       (eval-defun nil)
2533       ;; Find the end of the current word.
2534       (re-search-forward "[ \t\n]" nil t)
2535       ;; Search backward.
2536       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2537         (match-string 1)))))
2538   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2539                               (match-string 1 var))))
2540          (entry (assq type gnus-format-specs))
2541          value spec)
2542     (when entry
2543       (setq gnus-format-specs (delq entry gnus-format-specs)))
2544     (set
2545      (intern (format "%s-spec" var))
2546      (gnus-parse-format (setq value (symbol-value (intern var)))
2547                         (symbol-value (intern (format "%s-alist" var)))
2548                         (not (string-match "mode" var))))
2549     (setq spec (symbol-value (intern (format "%s-spec" var))))
2550     (push (list type value spec) gnus-format-specs)
2551
2552     (pop-to-buffer "*Gnus Format*")
2553     (erase-buffer)
2554     (lisp-interaction-mode)
2555     (insert (pp-to-string spec))))
2556
2557 (defun gnus-update-format-specifications (&optional force)
2558   "Update all (necessary) format specifications."
2559   ;; Make the indentation array.
2560   (gnus-make-thread-indent-array)
2561
2562   ;; See whether all the stored info needs to be flushed.
2563   (when (or force
2564             (not (equal emacs-version
2565                         (cdr (assq 'version gnus-format-specs)))))
2566     (setq gnus-format-specs nil))
2567
2568   ;; Go through all the formats and see whether they need updating.
2569   (let ((types '(summary summary-dummy group
2570                          summary-mode group-mode article-mode))
2571         new-format entry type val)
2572     (while (setq type (pop types))
2573       ;; Jump to the proper buffer to find out the value of
2574       ;; the variable, if possible.  (It may be buffer-local.)
2575       (save-excursion
2576         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2577               val)
2578           (when (and (boundp buffer)
2579                      (setq val (symbol-value buffer))
2580                      (get-buffer val)
2581                      (buffer-name (get-buffer val)))
2582             (set-buffer (get-buffer val)))
2583           (setq new-format (symbol-value
2584                             (intern (format "gnus-%s-line-format" type))))))
2585       (setq entry (cdr (assq type gnus-format-specs)))
2586       (if (and entry
2587                (equal (car entry) new-format))
2588           ;; Use the old format.
2589           (set (intern (format "gnus-%s-line-format-spec" type))
2590                (cadr entry))
2591         ;; This is a new format.
2592         (setq val
2593               (if (not (stringp new-format))
2594                   ;; This is a function call or something.
2595                   new-format
2596                 ;; This is a "real" format.
2597                 (gnus-parse-format
2598                  new-format
2599                  (symbol-value
2600                   (intern (format "gnus-%s-line-format-alist"
2601                                   (if (eq type 'article-mode)
2602                                       'summary-mode type))))
2603                  (not (string-match "mode$" (symbol-name type))))))
2604         ;; Enter the new format spec into the list.
2605         (if entry
2606             (progn
2607               (setcar (cdr entry) val)
2608               (setcar entry new-format))
2609           (push (list type new-format val) gnus-format-specs))
2610         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2611
2612   (unless (assq 'version gnus-format-specs)
2613     (push (cons 'version emacs-version) gnus-format-specs))
2614
2615   (gnus-update-group-mark-positions)
2616   (gnus-update-summary-mark-positions))
2617
2618 (defun gnus-update-summary-mark-positions ()
2619   "Compute where the summary marks are to go."
2620   (save-excursion
2621     (when (and gnus-summary-buffer
2622                (get-buffer gnus-summary-buffer)
2623                (buffer-name (get-buffer gnus-summary-buffer)))
2624       (set-buffer gnus-summary-buffer))
2625     (let ((gnus-replied-mark 129)
2626           (gnus-score-below-mark 130)
2627           (gnus-score-over-mark 130)
2628           (thread nil)
2629           (gnus-visual nil)
2630           (spec gnus-summary-line-format-spec)
2631           pos)
2632       (save-excursion
2633         (gnus-set-work-buffer)
2634         (let ((gnus-summary-line-format-spec spec))
2635           (gnus-summary-insert-line
2636            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2637           (goto-char (point-min))
2638           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2639                                              (- (point) 2)))))
2640           (goto-char (point-min))
2641           (push (cons 'replied (and (search-forward "\201" nil t) 
2642                                     (- (point) 2)))
2643                 pos)
2644           (goto-char (point-min))
2645           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2646                 pos)))
2647       (setq gnus-summary-mark-positions pos))))
2648
2649 (defun gnus-update-group-mark-positions ()
2650   (save-excursion
2651     (let ((gnus-process-mark 128)
2652           (gnus-group-marked '("dummy.group"))
2653           (gnus-active-hashtb (make-vector 10 0)))
2654       (gnus-set-active "dummy.group" '(0 . 0))
2655       (gnus-set-work-buffer)
2656       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2657       (goto-char (point-min))
2658       (setq gnus-group-mark-positions
2659             (list (cons 'process (and (search-forward "\200" nil t)
2660                                       (- (point) 2))))))))
2661
2662 (defvar gnus-mouse-face-0 'highlight)
2663 (defvar gnus-mouse-face-1 'highlight)
2664 (defvar gnus-mouse-face-2 'highlight)
2665 (defvar gnus-mouse-face-3 'highlight)
2666 (defvar gnus-mouse-face-4 'highlight)
2667
2668 (defun gnus-mouse-face-function (form type)
2669   `(gnus-put-text-property
2670     (point) (progn ,@form (point))
2671     gnus-mouse-face-prop
2672     ,(if (equal type 0)
2673          'gnus-mouse-face
2674        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2675
2676 (defvar gnus-face-0 'bold)
2677 (defvar gnus-face-1 'italic)
2678 (defvar gnus-face-2 'bold-italic)
2679 (defvar gnus-face-3 'bold)
2680 (defvar gnus-face-4 'bold)
2681
2682 (defun gnus-face-face-function (form type)
2683   `(gnus-put-text-property
2684     (point) (progn ,@form (point))
2685     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2686
2687 (defun gnus-max-width-function (el max-width)
2688   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2689   (if (symbolp el)
2690       `(if (> (length ,el) ,max-width)
2691            (substring ,el 0 ,max-width)
2692          ,el)
2693     `(let ((val (eval ,el)))
2694        (if (numberp val)
2695            (setq val (int-to-string val)))
2696        (if (> (length val) ,max-width)
2697            (substring val 0 ,max-width)
2698          val))))
2699
2700 (defun gnus-parse-format (format spec-alist &optional insert)
2701   ;; This function parses the FORMAT string with the help of the
2702   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2703   ;; string.  If the FORMAT string contains the specifiers %( and %)
2704   ;; the text between them will have the mouse-face text property.
2705   (if (string-match
2706        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2707        format)
2708       (gnus-parse-complex-format format spec-alist)
2709     ;; This is a simple format.
2710     (gnus-parse-simple-format format spec-alist insert)))
2711
2712 (defun gnus-parse-complex-format (format spec-alist)
2713   (save-excursion
2714     (gnus-set-work-buffer)
2715     (insert format)
2716     (goto-char (point-min))
2717     (while (re-search-forward "\"" nil t)
2718       (replace-match "\\\"" nil t))
2719     (goto-char (point-min))
2720     (insert "(\"")
2721     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2722       (let ((number (if (match-beginning 1)
2723                         (match-string 1) "0"))
2724             (delim (aref (match-string 2) 0)))
2725         (if (or (= delim ?\() (= delim ?\{))
2726             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2727                                    " " number " \""))
2728           (replace-match "\")\""))))
2729     (goto-char (point-max))
2730     (insert "\")")
2731     (goto-char (point-min))
2732     (let ((form (read (current-buffer))))
2733       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2734
2735 (defun gnus-complex-form-to-spec (form spec-alist)
2736   (delq nil
2737         (mapcar
2738          (lambda (sform)
2739            (if (stringp sform)
2740                (gnus-parse-simple-format sform spec-alist t)
2741              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2742                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2743                       (nth 1 sform))))
2744          form)))
2745
2746 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2747   ;; This function parses the FORMAT string with the help of the
2748   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2749   ;; string.
2750   (let ((max-width 0)
2751         spec flist fstring newspec elem beg result dontinsert)
2752     (save-excursion
2753       (gnus-set-work-buffer)
2754       (insert format)
2755       (goto-char (point-min))
2756       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2757                                 nil t)
2758         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2759               (setq newspec "%"
2760                     beg (1+ (match-beginning 0)))
2761           ;; First check if there are any specs that look anything like
2762           ;; "%12,12A", ie. with a "max width specification".  These have
2763           ;; to be treated specially.
2764           (if (setq beg (match-beginning 1))
2765               (setq max-width
2766                     (string-to-int
2767                      (buffer-substring
2768                       (1+ (match-beginning 1)) (match-end 1))))
2769             (setq max-width 0)
2770             (setq beg (match-beginning 2)))
2771           ;; Find the specification from `spec-alist'.
2772           (unless (setq elem (cdr (assq spec spec-alist)))
2773             (setq elem '("*" ?s)))
2774           ;; Treat user defined format specifiers specially.
2775           (when (eq (car elem) 'gnus-tmp-user-defined)
2776             (setq elem
2777                   (list
2778                    (list (intern (concat "gnus-user-format-function-"
2779                                          (match-string 3)))
2780                          'gnus-tmp-header) ?s))
2781             (delete-region (match-beginning 3) (match-end 3)))
2782           (if (not (zerop max-width))
2783               (let ((el (car elem)))
2784                 (cond ((= (cadr elem) ?c)
2785                        (setq el (list 'char-to-string el)))
2786                       ((= (cadr elem) ?d)
2787                        (setq el (list 'int-to-string el))))
2788                 (setq flist (cons (gnus-max-width-function el max-width)
2789                                   flist))
2790                 (setq newspec ?s))
2791             (progn
2792               (setq flist (cons (car elem) flist))
2793               (setq newspec (cadr elem)))))
2794         ;; Remove the old specification (and possibly a ",12" string).
2795         (delete-region beg (match-end 2))
2796         ;; Insert the new specification.
2797         (goto-char beg)
2798         (insert newspec))
2799       (setq fstring (buffer-substring 1 (point-max))))
2800     ;; Do some postprocessing to increase efficiency.
2801     (setq
2802      result
2803      (cond
2804       ;; Emptyness.
2805       ((string= fstring "")
2806        nil)
2807       ;; Not a format string.
2808       ((not (string-match "%" fstring))
2809        (list fstring))
2810       ;; A format string with just a single string spec.
2811       ((string= fstring "%s")
2812        (list (car flist)))
2813       ;; A single character.
2814       ((string= fstring "%c")
2815        (list (car flist)))
2816       ;; A single number.
2817       ((string= fstring "%d")
2818        (setq dontinsert)
2819        (if insert
2820            (list `(princ ,(car flist)))
2821          (list `(int-to-string ,(car flist)))))
2822       ;; Just lots of chars and strings.
2823       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2824        (nreverse flist))
2825       ;; A single string spec at the beginning of the spec.
2826       ((string-match "\\`%[sc][^%]+\\'" fstring)
2827        (list (car flist) (substring fstring 2)))
2828       ;; A single string spec in the middle of the spec.
2829       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2830        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2831       ;; A single string spec in the end of the spec.
2832       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2833        (list (match-string 1 fstring) (car flist)))
2834       ;; A more complex spec.
2835       (t
2836        (list (cons 'format (cons fstring (nreverse flist)))))))
2837
2838     (if insert
2839         (when result
2840           (if dontinsert
2841               result
2842             (cons 'insert result)))
2843       (cond ((stringp result)
2844              result)
2845             ((consp result)
2846              (cons 'concat result))
2847             (t "")))))
2848
2849 (defun gnus-eval-format (format &optional alist props)
2850   "Eval the format variable FORMAT, using ALIST.
2851 If PROPS, insert the result."
2852   (let ((form (gnus-parse-format format alist props)))
2853     (if props
2854         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2855       (eval form))))
2856
2857 (defun gnus-remove-text-with-property (prop)
2858   "Delete all text in the current buffer with text property PROP."
2859   (save-excursion
2860     (goto-char (point-min))
2861     (while (not (eobp))
2862       (while (get-text-property (point) prop)
2863         (delete-char 1))
2864       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2865
2866 (defun gnus-set-work-buffer ()
2867   (if (get-buffer gnus-work-buffer)
2868       (progn
2869         (set-buffer gnus-work-buffer)
2870         (erase-buffer))
2871     (set-buffer (get-buffer-create gnus-work-buffer))
2872     (kill-all-local-variables)
2873     (buffer-disable-undo (current-buffer))
2874     (gnus-add-current-to-buffer-list)))
2875
2876 ;; Article file names when saving.
2877
2878 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2879   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2880 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2881 Otherwise, it is like ~/News/news/group/num."
2882   (let ((default
2883           (expand-file-name
2884            (concat (if (gnus-use-long-file-name 'not-save)
2885                        (gnus-capitalize-newsgroup newsgroup)
2886                      (gnus-newsgroup-directory-form newsgroup))
2887                    "/" (int-to-string (mail-header-number headers)))
2888            gnus-article-save-directory)))
2889     (if (and last-file
2890              (string-equal (file-name-directory default)
2891                            (file-name-directory last-file))
2892              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2893         default
2894       (or last-file default))))
2895
2896 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2897   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2898 If variable `gnus-use-long-file-name' is non-nil, it is
2899 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2900   (let ((default
2901           (expand-file-name
2902            (concat (if (gnus-use-long-file-name 'not-save)
2903                        newsgroup
2904                      (gnus-newsgroup-directory-form newsgroup))
2905                    "/" (int-to-string (mail-header-number headers)))
2906            gnus-article-save-directory)))
2907     (if (and last-file
2908              (string-equal (file-name-directory default)
2909                            (file-name-directory last-file))
2910              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2911         default
2912       (or last-file default))))
2913
2914 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2915   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2916 If variable `gnus-use-long-file-name' is non-nil, it is
2917 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2918   (or last-file
2919       (expand-file-name
2920        (if (gnus-use-long-file-name 'not-save)
2921            (gnus-capitalize-newsgroup newsgroup)
2922          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2923        gnus-article-save-directory)))
2924
2925 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2926   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2927 If variable `gnus-use-long-file-name' is non-nil, it is
2928 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2929   (or last-file
2930       (expand-file-name
2931        (if (gnus-use-long-file-name 'not-save)
2932            newsgroup
2933          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2934        gnus-article-save-directory)))
2935
2936 ;; For subscribing new newsgroup
2937
2938 (defun gnus-subscribe-hierarchical-interactive (groups)
2939   (let ((groups (sort groups 'string<))
2940         prefixes prefix start ans group starts)
2941     (while groups
2942       (setq prefixes (list "^"))
2943       (while (and groups prefixes)
2944         (while (not (string-match (car prefixes) (car groups)))
2945           (setq prefixes (cdr prefixes)))
2946         (setq prefix (car prefixes))
2947         (setq start (1- (length prefix)))
2948         (if (and (string-match "[^\\.]\\." (car groups) start)
2949                  (cdr groups)
2950                  (setq prefix
2951                        (concat "^" (substring (car groups) 0 (match-end 0))))
2952                  (string-match prefix (cadr groups)))
2953             (progn
2954               (setq prefixes (cons prefix prefixes))
2955               (message "Descend hierarchy %s? ([y]nsq): "
2956                        (substring prefix 1 (1- (length prefix))))
2957               (while (not (memq (setq ans (read-char)) '(?y ?\n ?n ?s ?q)))
2958                 (ding)
2959                 (message "Descend hierarchy %s? ([y]nsq): "
2960                          (substring prefix 1 (1- (length prefix)))))
2961               (cond ((= ans ?n)
2962                      (while (and groups
2963                                  (string-match prefix
2964                                                (setq group (car groups))))
2965                        (setq gnus-killed-list
2966                              (cons group gnus-killed-list))
2967                        (gnus-sethash group group gnus-killed-hashtb)
2968                        (setq groups (cdr groups)))
2969                      (setq starts (cdr starts)))
2970                     ((= ans ?s)
2971                      (while (and groups
2972                                  (string-match prefix
2973                                                (setq group (car groups))))
2974                        (gnus-sethash group group gnus-killed-hashtb)
2975                        (gnus-subscribe-alphabetically (car groups))
2976                        (setq groups (cdr groups)))
2977                      (setq starts (cdr starts)))
2978                     ((= ans ?q)
2979                      (while groups
2980                        (setq group (car groups))
2981                        (setq gnus-killed-list (cons group gnus-killed-list))
2982                        (gnus-sethash group group gnus-killed-hashtb)
2983                        (setq groups (cdr groups))))
2984                     (t nil)))
2985           (message "Subscribe %s? ([n]yq)" (car groups))
2986           (while (not (memq (setq ans (read-char)) '(?y ?\n ?q ?n)))
2987             (ding)
2988             (message "Subscribe %s? ([n]yq)" (car groups)))
2989           (setq group (car groups))
2990           (cond ((= ans ?y)
2991                  (gnus-subscribe-alphabetically (car groups))
2992                  (gnus-sethash group group gnus-killed-hashtb))
2993                 ((= ans ?q)
2994                  (while groups
2995                    (setq group (car groups))
2996                    (setq gnus-killed-list (cons group gnus-killed-list))
2997                    (gnus-sethash group group gnus-killed-hashtb)
2998                    (setq groups (cdr groups))))
2999                 (t
3000                  (setq gnus-killed-list (cons group gnus-killed-list))
3001                  (gnus-sethash group group gnus-killed-hashtb)))
3002           (setq groups (cdr groups)))))))
3003
3004 (defun gnus-subscribe-randomly (newsgroup)
3005   "Subscribe new NEWSGROUP by making it the first newsgroup."
3006   (gnus-subscribe-newsgroup newsgroup))
3007
3008 (defun gnus-subscribe-alphabetically (newgroup)
3009   "Subscribe new NEWSGROUP and insert it in alphabetical order."
3010   (let ((groups (cdr gnus-newsrc-alist))
3011         before)
3012     (while (and (not before) groups)
3013       (if (string< newgroup (caar groups))
3014           (setq before (caar groups))
3015         (setq groups (cdr groups))))
3016     (gnus-subscribe-newsgroup newgroup before)))
3017
3018 (defun gnus-subscribe-hierarchically (newgroup)
3019   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
3020   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
3021   (save-excursion
3022     (set-buffer (find-file-noselect gnus-current-startup-file))
3023     (let ((groupkey newgroup)
3024           before)
3025       (while (and (not before) groupkey)
3026         (goto-char (point-min))
3027         (let ((groupkey-re
3028                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
3029           (while (and (re-search-forward groupkey-re nil t)
3030                       (progn
3031                         (setq before (match-string 1))
3032                         (string< before newgroup)))))
3033         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
3034         (setq groupkey
3035               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
3036                   (substring groupkey (match-beginning 1) (match-end 1)))))
3037       (gnus-subscribe-newsgroup newgroup before))
3038     (kill-buffer (current-buffer))))
3039
3040 (defun gnus-subscribe-interactively (group)
3041   "Subscribe the new GROUP interactively.
3042 It is inserted in hierarchical newsgroup order if subscribed.  If not,
3043 it is killed."
3044   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
3045       (gnus-subscribe-hierarchically group)
3046     (push group gnus-killed-list)))
3047
3048 (defun gnus-subscribe-zombies (group)
3049   "Make the new GROUP into a zombie group."
3050   (push group gnus-zombie-list))
3051
3052 (defun gnus-subscribe-killed (group)
3053   "Make the new GROUP a killed group."
3054   (push group gnus-killed-list))
3055
3056 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
3057   "Subscribe new NEWSGROUP.
3058 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
3059 the first newsgroup."
3060   ;; We subscribe the group by changing its level to `subscribed'.
3061   (gnus-group-change-level
3062    newsgroup gnus-level-default-subscribed
3063    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3064   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3065
3066 ;; For directories
3067
3068 (defun gnus-newsgroup-directory-form (newsgroup)
3069   "Make hierarchical directory name from NEWSGROUP name."
3070   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3071         (len (length newsgroup))
3072         idx)
3073     ;; If this is a foreign group, we don't want to translate the
3074     ;; entire name.
3075     (if (setq idx (string-match ":" newsgroup))
3076         (aset newsgroup idx ?/)
3077       (setq idx 0))
3078     ;; Replace all occurrences of `.' with `/'.
3079     (while (< idx len)
3080       (if (= (aref newsgroup idx) ?.)
3081           (aset newsgroup idx ?/))
3082       (setq idx (1+ idx)))
3083     newsgroup))
3084
3085 (defun gnus-newsgroup-savable-name (group)
3086   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3087   ;; with dots.
3088   (nnheader-replace-chars-in-string group ?/ ?.))
3089
3090 (defun gnus-make-directory (dir)
3091   "Make DIRECTORY recursively."
3092   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3093   ;; of the many mysteries of the universe.
3094   (let* ((dir (expand-file-name dir default-directory))
3095          dirs err)
3096     (if (string-match "/$" dir)
3097         (setq dir (substring dir 0 (match-beginning 0))))
3098     ;; First go down the path until we find a directory that exists.
3099     (while (not (file-exists-p dir))
3100       (setq dirs (cons dir dirs))
3101       (string-match "/[^/]+$" dir)
3102       (setq dir (substring dir 0 (match-beginning 0))))
3103     ;; Then create all the subdirs.
3104     (while (and dirs (not err))
3105       (condition-case ()
3106           (make-directory (car dirs))
3107         (error (setq err t)))
3108       (setq dirs (cdr dirs)))
3109     ;; We return whether we were successful or not.
3110     (not dirs)))
3111
3112 (defun gnus-capitalize-newsgroup (newsgroup)
3113   "Capitalize NEWSGROUP name."
3114   (and (not (zerop (length newsgroup)))
3115        (concat (char-to-string (upcase (aref newsgroup 0)))
3116                (substring newsgroup 1))))
3117
3118 ;; Various... things.
3119
3120 (defun gnus-simplify-subject (subject &optional re-only)
3121   "Remove `Re:' and words in parentheses.
3122 If RE-ONLY is non-nil, strip leading `Re:'s only."
3123   (let ((case-fold-search t))           ;Ignore case.
3124     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3125     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3126       (setq subject (substring subject (match-end 0))))
3127     ;; Remove uninteresting prefixes.
3128     (if (and (not re-only)
3129              gnus-simplify-ignored-prefixes
3130              (string-match gnus-simplify-ignored-prefixes subject))
3131         (setq subject (substring subject (match-end 0))))
3132     ;; Remove words in parentheses from end.
3133     (unless re-only
3134       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3135         (setq subject (substring subject 0 (match-beginning 0)))))
3136     ;; Return subject string.
3137     subject))
3138
3139 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3140 ;; all whitespace.
3141 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3142 (defun gnus-simplify-buffer-fuzzy ()
3143   (let ((case-fold-search t))
3144     (goto-char (point-min))
3145     (while (search-forward "\t" nil t)
3146       (replace-match " " t t))
3147     (goto-char (point-min))
3148     (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3149     (goto-char (match-beginning 0))
3150     (while (or
3151             (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3152             (looking-at "^[[].*: .*[]]$"))
3153       (goto-char (point-min))
3154       (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3155                                 nil t)
3156         (replace-match "" t t))
3157       (goto-char (point-min))
3158       (while (re-search-forward "^[[].*: .*[]]$" nil t)
3159         (goto-char (match-end 0))
3160         (delete-char -1)
3161         (delete-region
3162          (progn (goto-char (match-beginning 0)))
3163          (re-search-forward ":"))))
3164     (goto-char (point-min))
3165     (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3166       (replace-match "" t t))
3167     (goto-char (point-min))
3168     (while (re-search-forward "  +" nil t)
3169       (replace-match " " t t))
3170     (goto-char (point-min))
3171     (while (re-search-forward " $" nil t)
3172       (replace-match "" t t))
3173     (goto-char (point-min))
3174     (while (re-search-forward "^ +" nil t)
3175       (replace-match "" t t))
3176     (goto-char (point-min))
3177     (when gnus-simplify-subject-fuzzy-regexp
3178       (if (listp gnus-simplify-subject-fuzzy-regexp)
3179           (let ((list gnus-simplify-subject-fuzzy-regexp))
3180             (while list
3181               (goto-char (point-min))
3182               (while (re-search-forward (car list) nil t)
3183                 (replace-match "" t t))
3184               (setq list (cdr list))))
3185         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3186           (replace-match "" t t))))))
3187
3188 (defun gnus-simplify-subject-fuzzy (subject)
3189   "Siplify a subject string fuzzily."
3190   (save-excursion
3191     (gnus-set-work-buffer)
3192     (let ((case-fold-search t))
3193       (insert subject)
3194       (inline (gnus-simplify-buffer-fuzzy))
3195       (buffer-string))))
3196
3197 ;; Add the current buffer to the list of buffers to be killed on exit.
3198 (defun gnus-add-current-to-buffer-list ()
3199   (or (memq (current-buffer) gnus-buffer-list)
3200       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3201
3202 (defun gnus-string> (s1 s2)
3203   (not (or (string< s1 s2)
3204            (string= s1 s2))))
3205
3206 (defun gnus-read-active-file-p ()
3207   "Say whether the active file has been read from `gnus-select-method'."
3208   (memq gnus-select-method gnus-have-read-active-file))
3209
3210 ;;; General various misc type functions.
3211
3212 (defun gnus-clear-system ()
3213   "Clear all variables and buffers."
3214   ;; Clear Gnus variables.
3215   (let ((variables gnus-variable-list))
3216     (while variables
3217       (set (car variables) nil)
3218       (setq variables (cdr variables))))
3219   ;; Clear other internal variables.
3220   (setq gnus-list-of-killed-groups nil
3221         gnus-have-read-active-file nil
3222         gnus-newsrc-alist nil
3223         gnus-newsrc-hashtb nil
3224         gnus-killed-list nil
3225         gnus-zombie-list nil
3226         gnus-killed-hashtb nil
3227         gnus-active-hashtb nil
3228         gnus-moderated-list nil
3229         gnus-description-hashtb nil
3230         gnus-current-headers nil
3231         gnus-thread-indent-array nil
3232         gnus-newsgroup-headers nil
3233         gnus-newsgroup-name nil
3234         gnus-server-alist nil
3235         gnus-group-list-mode nil
3236         gnus-opened-servers nil
3237         gnus-group-mark-positions nil
3238         gnus-newsgroup-data nil
3239         gnus-newsgroup-unreads nil
3240         nnoo-state-alist nil
3241         gnus-current-select-method nil)
3242   (gnus-shutdown 'gnus)
3243   ;; Kill the startup file.
3244   (and gnus-current-startup-file
3245        (get-file-buffer gnus-current-startup-file)
3246        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3247   ;; Clear the dribble buffer.
3248   (gnus-dribble-clear)
3249   ;; Kill global KILL file buffer.
3250   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3251     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3252   (gnus-kill-buffer nntp-server-buffer)
3253   ;; Kill Gnus buffers.
3254   (while gnus-buffer-list
3255     (gnus-kill-buffer (pop gnus-buffer-list)))
3256   ;; Remove Gnus frames.
3257   (gnus-kill-gnus-frames))
3258
3259 (defun gnus-kill-gnus-frames ()
3260   "Kill all frames Gnus has created."
3261   (while gnus-created-frames
3262     (when (frame-live-p (car gnus-created-frames))
3263       ;; We slap a condition-case around this `delete-frame' to ensure 
3264       ;; against errors if we try do delete the single frame that's left.
3265       (condition-case ()
3266           (delete-frame (car gnus-created-frames))
3267         (error nil)))
3268     (pop gnus-created-frames)))
3269
3270 (defun gnus-windows-old-to-new (setting)
3271   ;; First we take care of the really, really old Gnus 3 actions.
3272   (when (symbolp setting)
3273     (setq setting
3274           ;; Take care of ooold GNUS 3.x values.
3275           (cond ((eq setting 'SelectArticle) 'article)
3276                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3277                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3278                 (t setting))))
3279   (if (or (listp setting)
3280           (not (and gnus-window-configuration
3281                     (memq setting '(group summary article)))))
3282       setting
3283     (let* ((setting (if (eq setting 'group)
3284                         (if (assq 'newsgroup gnus-window-configuration)
3285                             'newsgroup
3286                           'newsgroups) setting))
3287            (elem (cadr (assq setting gnus-window-configuration)))
3288            (total (apply '+ elem))
3289            (types '(group summary article))
3290            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3291            (i 0)
3292            perc
3293            out)
3294       (while (< i 3)
3295         (or (not (numberp (nth i elem)))
3296             (zerop (nth i elem))
3297             (progn
3298               (setq perc (if (= i 2)
3299                              1.0
3300                            (/ (float (nth 0 elem)) total)))
3301               (setq out (cons (if (eq pbuf (nth i types))
3302                                   (list (nth i types) perc 'point)
3303                                 (list (nth i types) perc))
3304                               out))))
3305         (setq i (1+ i)))
3306       `(vertical 1.0 ,@(nreverse out)))))
3307
3308 ;;;###autoload
3309 (defun gnus-add-configuration (conf)
3310   "Add the window configuration CONF to `gnus-buffer-configuration'."
3311   (setq gnus-buffer-configuration
3312         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3313                          gnus-buffer-configuration))))
3314
3315 (defvar gnus-frame-list nil)
3316
3317 (defun gnus-configure-frame (split &optional window)
3318   "Split WINDOW according to SPLIT."
3319   (unless window
3320     (setq window (get-buffer-window (current-buffer))))
3321   (select-window window)
3322   ;; This might be an old-stylee buffer config.
3323   (when (vectorp split)
3324     (setq split (append split nil)))
3325   (when (or (consp (car split))
3326             (vectorp (car split)))
3327     (push 1.0 split)
3328     (push 'vertical split))
3329   ;; The SPLIT might be something that is to be evaled to
3330   ;; return a new SPLIT.
3331   (while (and (not (assq (car split) gnus-window-to-buffer))
3332               (gnus-functionp (car split)))
3333     (setq split (eval split)))
3334   (let* ((type (car split))
3335          (subs (cddr split))
3336          (len (if (eq type 'horizontal) (window-width) (window-height)))
3337          (total 0)
3338          (window-min-width (or gnus-window-min-width window-min-width))
3339          (window-min-height (or gnus-window-min-height window-min-height))
3340          s result new-win rest comp-subs size sub)
3341     (cond
3342      ;; Nothing to do here.
3343      ((null split))
3344      ;; Don't switch buffers.
3345      ((null type)
3346       (and (memq 'point split) window))
3347      ;; This is a buffer to be selected.
3348      ((not (memq type '(frame horizontal vertical)))
3349       (let ((buffer (cond ((stringp type) type)
3350                           (t (cdr (assq type gnus-window-to-buffer)))))
3351             buf)
3352         (unless buffer
3353           (error "Illegal buffer type: %s" type))
3354         (unless (setq buf (get-buffer (if (symbolp buffer)
3355                                           (symbol-value buffer) buffer)))
3356           (setq buf (get-buffer-create (if (symbolp buffer)
3357                                            (symbol-value buffer) buffer))))
3358         (switch-to-buffer buf)
3359         ;; We return the window if it has the `point' spec.
3360         (and (memq 'point split) window)))
3361      ;; This is a frame split.
3362      ((eq type 'frame)
3363       (unless gnus-frame-list
3364         (setq gnus-frame-list (list (window-frame
3365                                      (get-buffer-window (current-buffer))))))
3366       (let ((i 0)
3367             params frame fresult)
3368         (while (< i (length subs))
3369           ;; Frame parameter is gotten from the sub-split.
3370           (setq params (cadr (elt subs i)))
3371           ;; It should be a list.
3372           (unless (listp params)
3373             (setq params nil))
3374           ;; Create a new frame?
3375           (unless (setq frame (elt gnus-frame-list i))
3376             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3377             (push frame gnus-created-frames))
3378           ;; Is the old frame still alive?
3379           (unless (frame-live-p frame)
3380             (setcar (nthcdr i gnus-frame-list)
3381                     (setq frame (make-frame params))))
3382           ;; Select the frame in question and do more splits there.
3383           (select-frame frame)
3384           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3385           (incf i))
3386         ;; Select the frame that has the selected buffer.
3387         (when fresult
3388           (select-frame (window-frame fresult)))))
3389      ;; This is a normal split.
3390      (t
3391       (when (> (length subs) 0)
3392         ;; First we have to compute the sizes of all new windows.
3393         (while subs
3394           (setq sub (append (pop subs) nil))
3395           (while (and (not (assq (car sub) gnus-window-to-buffer))
3396                       (gnus-functionp (car sub)))
3397             (setq sub (eval sub)))
3398           (when sub
3399             (push sub comp-subs)
3400             (setq size (cadar comp-subs))
3401             (cond ((equal size 1.0)
3402                    (setq rest (car comp-subs))
3403                    (setq s 0))
3404                   ((floatp size)
3405                    (setq s (floor (* size len))))
3406                   ((integerp size)
3407                    (setq s size))
3408                   (t
3409                    (error "Illegal size: %s" size)))
3410             ;; Try to make sure that we are inside the safe limits.
3411             (cond ((zerop s))
3412                   ((eq type 'horizontal)
3413                    (setq s (max s window-min-width)))
3414                   ((eq type 'vertical)
3415                    (setq s (max s window-min-height))))
3416             (setcar (cdar comp-subs) s)
3417             (incf total s)))
3418         ;; Take care of the "1.0" spec.
3419         (if rest
3420             (setcar (cdr rest) (- len total))
3421           (error "No 1.0 specs in %s" split))
3422         ;; The we do the actual splitting in a nice recursive
3423         ;; fashion.
3424         (setq comp-subs (nreverse comp-subs))
3425         (while comp-subs
3426           (if (null (cdr comp-subs))
3427               (setq new-win window)
3428             (setq new-win
3429                   (split-window window (cadar comp-subs)
3430                                 (eq type 'horizontal))))
3431           (setq result (or (gnus-configure-frame
3432                             (car comp-subs) window) result))
3433           (select-window new-win)
3434           (setq window new-win)
3435           (setq comp-subs (cdr comp-subs))))
3436       ;; Return the proper window, if any.
3437       (when result
3438         (select-window result))))))
3439
3440 (defvar gnus-frame-split-p nil)
3441
3442 (defun gnus-configure-windows (setting &optional force)
3443   (setq setting (gnus-windows-old-to-new setting))
3444   (let ((split (if (symbolp setting)
3445                    (cadr (assq setting gnus-buffer-configuration))
3446                  setting))
3447         all-visible)
3448
3449     (setq gnus-frame-split-p nil)
3450
3451     (unless split
3452       (error "No such setting: %s" setting))
3453
3454     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3455              (not force))
3456         ;; All the windows mentioned are already visible, so we just
3457         ;; put point in the assigned buffer, and do not touch the
3458         ;; winconf.
3459         (select-window all-visible)
3460
3461       ;; Either remove all windows or just remove all Gnus windows.
3462       (let ((frame (selected-frame)))
3463         (unwind-protect
3464             (if gnus-use-full-window
3465                 ;; We want to remove all other windows.
3466                 (if (not gnus-frame-split-p)
3467                     ;; This is not a `frame' split, so we ignore the
3468                     ;; other frames.  
3469                     (delete-other-windows)
3470                   ;; This is a `frame' split, so we delete all windows
3471                   ;; on all frames.
3472                   (mapcar 
3473                    (lambda (frame)
3474                      (unless (eq (cdr (assq 'minibuffer
3475                                             (frame-parameters frame)))
3476                                  'only)
3477                        (select-frame frame)
3478                        (delete-other-windows)))
3479                    (frame-list)))
3480               ;; Just remove some windows.
3481               (gnus-remove-some-windows)
3482               (switch-to-buffer nntp-server-buffer))
3483           (select-frame frame)))
3484
3485       (switch-to-buffer nntp-server-buffer)
3486       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3487
3488 (defun gnus-all-windows-visible-p (split)
3489   "Say whether all buffers in SPLIT are currently visible.
3490 In particular, the value returned will be the window that
3491 should have point."
3492   (let ((stack (list split))
3493         (all-visible t)
3494         type buffer win buf)
3495     (while (and (setq split (pop stack))
3496                 all-visible)
3497       ;; Be backwards compatible.
3498       (when (vectorp split)
3499         (setq split (append split nil)))
3500       (when (or (consp (car split))
3501                 (vectorp (car split)))
3502         (push 1.0 split)
3503         (push 'vertical split))
3504       ;; The SPLIT might be something that is to be evaled to
3505       ;; return a new SPLIT.
3506       (while (and (not (assq (car split) gnus-window-to-buffer))
3507                   (gnus-functionp (car split)))
3508         (setq split (eval split)))
3509
3510       (setq type (elt split 0))
3511       (cond
3512        ;; Nothing here.
3513        ((null split) t)
3514        ;; A buffer.
3515        ((not (memq type '(horizontal vertical frame)))
3516         (setq buffer (cond ((stringp type) type)
3517                            (t (cdr (assq type gnus-window-to-buffer)))))
3518         (unless buffer
3519           (error "Illegal buffer type: %s" type))
3520         (when (setq buf (get-buffer (if (symbolp buffer)
3521                                         (symbol-value buffer)
3522                                       buffer)))
3523           (setq win (get-buffer-window buf t)))
3524         (if win
3525             (when (memq 'point split)
3526                 (setq all-visible win))
3527           (setq all-visible nil)))
3528        (t
3529         (when (eq type 'frame)
3530           (setq gnus-frame-split-p t))
3531         (setq stack (append (cddr split) stack)))))
3532     (unless (eq all-visible t)
3533       all-visible)))
3534
3535 (defun gnus-window-top-edge (&optional window)
3536   (nth 1 (window-edges window)))
3537
3538 (defun gnus-remove-some-windows ()
3539   (let ((buffers gnus-window-to-buffer)
3540         buf bufs lowest-buf lowest)
3541     (save-excursion
3542       ;; Remove windows on all known Gnus buffers.
3543       (while buffers
3544         (setq buf (cdar buffers))
3545         (if (symbolp buf)
3546             (setq buf (and (boundp buf) (symbol-value buf))))
3547         (and buf
3548              (get-buffer-window buf)
3549              (progn
3550                (setq bufs (cons buf bufs))
3551                (pop-to-buffer buf)
3552                (if (or (not lowest)
3553                        (< (gnus-window-top-edge) lowest))
3554                    (progn
3555                      (setq lowest (gnus-window-top-edge))
3556                      (setq lowest-buf buf)))))
3557         (setq buffers (cdr buffers)))
3558       ;; Remove windows on *all* summary buffers.
3559       (walk-windows
3560        (lambda (win)
3561          (let ((buf (window-buffer win)))
3562            (if (string-match    "^\\*Summary" (buffer-name buf))
3563                (progn
3564                  (setq bufs (cons buf bufs))
3565                  (pop-to-buffer buf)
3566                  (if (or (not lowest)
3567                          (< (gnus-window-top-edge) lowest))
3568                      (progn
3569                        (setq lowest-buf buf)
3570                        (setq lowest (gnus-window-top-edge)))))))))
3571       (and lowest-buf
3572            (progn
3573              (pop-to-buffer lowest-buf)
3574              (switch-to-buffer nntp-server-buffer)))
3575       (while bufs
3576         (and (not (eq (car bufs) lowest-buf))
3577              (delete-windows-on (car bufs)))
3578         (setq bufs (cdr bufs))))))
3579
3580 (defun gnus-version (&optional arg)
3581   "Version number of this version of Gnus.
3582 If ARG, insert string at point."
3583   (interactive "P")
3584   (let ((methods gnus-valid-select-methods)
3585         (mess gnus-version)
3586         meth)
3587     ;; Go through all the legal select methods and add their version
3588     ;; numbers to the total version string.  Only the backends that are
3589     ;; currently in use will have their message numbers taken into
3590     ;; consideration.
3591     (while methods
3592       (setq meth (intern (concat (caar methods) "-version")))
3593       (and (boundp meth)
3594            (stringp (symbol-value meth))
3595            (setq mess (concat mess "; " (symbol-value meth))))
3596       (setq methods (cdr methods)))
3597     (if arg
3598         (insert (message mess))
3599       (message mess))))
3600
3601 (defun gnus-info-find-node ()
3602   "Find Info documentation of Gnus."
3603   (interactive)
3604   ;; Enlarge info window if needed.
3605   (let ((mode major-mode)
3606         gnus-info-buffer)
3607     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3608     (setq gnus-info-buffer (current-buffer))
3609     (gnus-configure-windows 'info)))
3610
3611 (defun gnus-days-between (date1 date2)
3612   ;; Return the number of days between date1 and date2.
3613   (- (gnus-day-number date1) (gnus-day-number date2)))
3614
3615 (defun gnus-day-number (date)
3616   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3617                      (timezone-parse-date date))))
3618     (timezone-absolute-from-gregorian
3619      (nth 1 dat) (nth 2 dat) (car dat))))
3620
3621 (defun gnus-encode-date (date)
3622   "Convert DATE to internal time."
3623   (let* ((parse (timezone-parse-date date))
3624          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3625          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3626     (encode-time (caddr time) (cadr time) (car time)
3627                  (caddr date) (cadr date) (car date) (nth 4 date))))
3628
3629 (defun gnus-time-minus (t1 t2)
3630   "Subtract two internal times."
3631   (let ((borrow (< (cadr t1) (cadr t2))))
3632     (list (- (car t1) (car t2) (if borrow 1 0))
3633           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3634
3635 (defun gnus-file-newer-than (file date)
3636   (let ((fdate (nth 5 (file-attributes file))))
3637     (or (> (car fdate) (car date))
3638         (and (= (car fdate) (car date))
3639              (> (nth 1 fdate) (nth 1 date))))))
3640
3641 (defmacro gnus-local-set-keys (&rest plist)
3642   "Set the keys in PLIST in the current keymap."
3643   `(gnus-define-keys-1 (current-local-map) ',plist))
3644
3645 (defmacro gnus-define-keys (keymap &rest plist)
3646   "Define all keys in PLIST in KEYMAP."
3647   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3648
3649 (put 'gnus-define-keys 'lisp-indent-function 1)
3650 (put 'gnus-define-keys 'lisp-indent-hook 1)
3651 (put 'gnus-define-keymap 'lisp-indent-function 1)
3652 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3653
3654 (defmacro gnus-define-keymap (keymap &rest plist)
3655   "Define all keys in PLIST in KEYMAP."
3656   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3657
3658 (defun gnus-define-keys-1 (keymap plist)
3659   (when (null keymap)
3660     (error "Can't set keys in a null keymap"))
3661   (cond ((symbolp keymap)
3662          (setq keymap (symbol-value keymap)))
3663         ((keymapp keymap))
3664         ((listp keymap)
3665          (set (car keymap) nil)
3666          (define-prefix-command (car keymap))
3667          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3668          (setq keymap (symbol-value (car keymap)))))
3669   (let (key)
3670     (while plist
3671       (when (symbolp (setq key (pop plist)))
3672         (setq key (symbol-value key)))
3673       (define-key keymap key (pop plist)))))
3674
3675 (defun gnus-group-read-only-p (&optional group)
3676   "Check whether GROUP supports editing or not.
3677 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3678 that that variable is buffer-local to the summary buffers."
3679   (let ((group (or group gnus-newsgroup-name)))
3680     (not (gnus-check-backend-function 'request-replace-article group))))
3681
3682 (defun gnus-group-total-expirable-p (group)
3683   "Check whether GROUP is total-expirable or not."
3684   (let ((params (gnus-info-params (gnus-get-info group))))
3685     (or (memq 'total-expire params)
3686         (cdr (assq 'total-expire params)) ; (total-expire . t)
3687         (and gnus-total-expirable-newsgroups ; Check var.
3688              (string-match gnus-total-expirable-newsgroups group)))))
3689
3690 (defun gnus-group-auto-expirable-p (group)
3691   "Check whether GROUP is total-expirable or not."
3692   (let ((params (gnus-info-params (gnus-get-info group))))
3693     (or (memq 'auto-expire params)
3694         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3695         (and gnus-auto-expirable-newsgroups ; Check var.
3696              (string-match gnus-auto-expirable-newsgroups group)))))
3697
3698 (defun gnus-virtual-group-p (group)
3699   "Say whether GROUP is virtual or not."
3700   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3701                         gnus-valid-select-methods)))
3702
3703 (defun gnus-news-group-p (group &optional article)
3704   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3705   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3706       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3707            (eq (gnus-request-type group article) 'news))))
3708
3709 (defsubst gnus-simplify-subject-fully (subject)
3710   "Simplify a subject string according to the user's wishes."
3711   (cond
3712    ((null gnus-summary-gather-subject-limit)
3713     (gnus-simplify-subject-re subject))
3714    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3715     (gnus-simplify-subject-fuzzy subject))
3716    ((numberp gnus-summary-gather-subject-limit)
3717     (gnus-limit-string (gnus-simplify-subject-re subject)
3718                        gnus-summary-gather-subject-limit))
3719    (t
3720     subject)))
3721
3722 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3723   "Check whether two subjects are equal.  If optional argument
3724 simple-first is t, first argument is already simplified."
3725   (cond
3726    ((null simple-first)
3727     (equal (gnus-simplify-subject-fully s1)
3728            (gnus-simplify-subject-fully s2)))
3729    (t
3730     (equal s1
3731            (gnus-simplify-subject-fully s2)))))
3732
3733 ;; Returns a list of writable groups.
3734 (defun gnus-writable-groups ()
3735   (let ((alist gnus-newsrc-alist)
3736         groups group)
3737     (while (setq group (car (pop alist)))
3738       (unless (gnus-group-read-only-p group)
3739         (push group groups)))
3740     (nreverse groups)))
3741
3742 (defun gnus-completing-read (default prompt &rest args)
3743   ;; Like `completing-read', except that DEFAULT is the default argument.
3744   (let* ((prompt (if default 
3745                      (concat prompt " (default " default ") ")
3746                    (concat prompt " ")))
3747          (answer (apply 'completing-read prompt args)))
3748     (if (or (null answer) (zerop (length answer)))
3749         default
3750       answer)))
3751
3752 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3753 ;; the echo area.
3754 (defun gnus-y-or-n-p (prompt)
3755   (prog1
3756       (y-or-n-p prompt)
3757     (message "")))
3758
3759 (defun gnus-yes-or-no-p (prompt)
3760   (prog1
3761       (yes-or-no-p prompt)
3762     (message "")))
3763
3764 ;; Check whether to use long file names.
3765 (defun gnus-use-long-file-name (symbol)
3766   ;; The variable has to be set...
3767   (and gnus-use-long-file-name
3768        ;; If it isn't a list, then we return t.
3769        (or (not (listp gnus-use-long-file-name))
3770            ;; If it is a list, and the list contains `symbol', we
3771            ;; return nil.
3772            (not (memq symbol gnus-use-long-file-name)))))
3773
3774 ;; I suspect there's a better way, but I haven't taken the time to do
3775 ;; it yet. -erik selberg@cs.washington.edu
3776 (defun gnus-dd-mmm (messy-date)
3777   "Return a string like DD-MMM from a big messy string"
3778   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3779                    (error nil))))
3780     (if (not datevec)
3781         "??-???"
3782       (format "%2s-%s"
3783               (condition-case ()
3784                   ;; Make sure leading zeroes are stripped.
3785                   (number-to-string (string-to-number (aref datevec 2)))
3786                 (error "??"))
3787               (capitalize
3788                (or (car
3789                     (nth (1- (string-to-number (aref datevec 1)))
3790                          timezone-months-assoc))
3791                    "???"))))))
3792
3793 (defun gnus-mode-string-quote (string)
3794   "Quote all \"%\" in STRING."
3795   (save-excursion
3796     (gnus-set-work-buffer)
3797     (insert string)
3798     (goto-char (point-min))
3799     (while (search-forward "%" nil t)
3800       (insert "%"))
3801     (buffer-string)))
3802
3803 ;; Make a hash table (default and minimum size is 255).
3804 ;; Optional argument HASHSIZE specifies the table size.
3805 (defun gnus-make-hashtable (&optional hashsize)
3806   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3807
3808 ;; Make a number that is suitable for hashing; bigger than MIN and one
3809 ;; less than 2^x.
3810 (defun gnus-create-hash-size (min)
3811   (let ((i 1))
3812     (while (< i min)
3813       (setq i (* 2 i)))
3814     (1- i)))
3815
3816 ;; Show message if message has a lower level than `gnus-verbose'.
3817 ;; Guideline for numbers:
3818 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3819 ;; for things that take a long time, 7 - not very important messages
3820 ;; on stuff, 9 - messages inside loops.
3821 (defun gnus-message (level &rest args)
3822   (if (<= level gnus-verbose)
3823       (apply 'message args)
3824     ;; We have to do this format thingy here even if the result isn't
3825     ;; shown - the return value has to be the same as the return value
3826     ;; from `message'.
3827     (apply 'format args)))
3828
3829 (defun gnus-error (level &rest args)
3830   "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
3831   (when (<= (floor level) gnus-verbose)
3832     (apply 'message args)
3833     (ding)
3834     (let (duration)
3835       (when (and (floatp level)
3836                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3837         (sit-for duration))))
3838   nil)
3839
3840 ;; Generate a unique new group name.
3841 (defun gnus-generate-new-group-name (leaf)
3842   (let ((name leaf)
3843         (num 0))
3844     (while (gnus-gethash name gnus-newsrc-hashtb)
3845       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3846     name))
3847
3848 (defsubst gnus-hide-text (b e props)
3849   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3850   (gnus-add-text-properties b e props)
3851   (when (memq 'intangible props)
3852     (gnus-put-text-property (max (1- b) (point-min))
3853                        b 'intangible (cddr (memq 'intangible props)))))
3854
3855 (defsubst gnus-unhide-text (b e)
3856   "Remove hidden text properties from region between B and E."
3857   (remove-text-properties b e gnus-hidden-properties)
3858   (when (memq 'intangible gnus-hidden-properties)
3859     (gnus-put-text-property (max (1- b) (point-min))
3860                             b 'intangible nil)))
3861
3862 (defun gnus-hide-text-type (b e type)
3863   "Hide text of TYPE between B and E."
3864   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3865
3866 (defun gnus-parent-headers (headers &optional generation)
3867   "Return the headers of the GENERATIONeth parent of HEADERS."
3868   (unless generation 
3869     (setq generation 1))
3870   (let (references parent)
3871     (while (and headers (not (zerop generation)))
3872       (setq references (mail-header-references headers))
3873       (when (and references
3874                  (setq parent (gnus-parent-id references))
3875                  (setq headers (car (gnus-id-to-thread parent))))
3876         (decf generation)))
3877     headers))
3878
3879 (defun gnus-parent-id (references)
3880   "Return the last Message-ID in REFERENCES."
3881   (when (and references
3882              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3883     (substring references (match-beginning 1) (match-end 1))))
3884
3885 (defun gnus-split-references (references)
3886   "Return a list of Message-IDs in REFERENCES."
3887   (let ((beg 0)
3888         ids)
3889     (while (string-match "<[^>]+>" references beg)
3890       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3891             ids))
3892     (nreverse ids)))
3893
3894 (defun gnus-buffer-live-p (buffer)
3895   "Say whether BUFFER is alive or not."
3896   (and buffer
3897        (get-buffer buffer)
3898        (buffer-name (get-buffer buffer))))
3899
3900 (defun gnus-ephemeral-group-p (group)
3901   "Say whether GROUP is ephemeral or not."
3902   (gnus-group-get-parameter group 'quit-config))
3903
3904 (defun gnus-group-quit-config (group)
3905   "Return the quit-config of GROUP."
3906   (gnus-group-get-parameter group 'quit-config))
3907
3908 (defun gnus-simplify-mode-line ()
3909   "Make mode lines a bit simpler."
3910   (setq mode-line-modified "-- ")
3911   (when (listp mode-line-format)
3912     (make-local-variable 'mode-line-format)
3913     (setq mode-line-format (copy-sequence mode-line-format))
3914     (when (equal (nth 3 mode-line-format) "   ")
3915       (setcar (nthcdr 3 mode-line-format) " "))))
3916
3917 ;;; List and range functions
3918
3919 (defun gnus-last-element (list)
3920   "Return last element of LIST."
3921   (while (cdr list)
3922     (setq list (cdr list)))
3923   (car list))
3924
3925 (defun gnus-copy-sequence (list)
3926   "Do a complete, total copy of a list."
3927   (if (and (consp list) (not (consp (cdr list))))
3928       (cons (car list) (cdr list))
3929     (mapcar (lambda (elem) (if (consp elem)
3930                                (if (consp (cdr elem))
3931                                    (gnus-copy-sequence elem)
3932                                  (cons (car elem) (cdr elem)))
3933                              elem))
3934             list)))
3935
3936 (defun gnus-set-difference (list1 list2)
3937   "Return a list of elements of LIST1 that do not appear in LIST2."
3938   (let ((list1 (copy-sequence list1)))
3939     (while list2
3940       (setq list1 (delq (car list2) list1))
3941       (setq list2 (cdr list2)))
3942     list1))
3943
3944 (defun gnus-sorted-complement (list1 list2)
3945   "Return a list of elements of LIST1 that do not appear in LIST2.
3946 Both lists have to be sorted over <."
3947   (let (out)
3948     (if (or (null list1) (null list2))
3949         (or list1 list2)
3950       (while (and list1 list2)
3951         (cond ((= (car list1) (car list2))
3952                (setq list1 (cdr list1)
3953                      list2 (cdr list2)))
3954               ((< (car list1) (car list2))
3955                (setq out (cons (car list1) out))
3956                (setq list1 (cdr list1)))
3957               (t
3958                (setq out (cons (car list2) out))
3959                (setq list2 (cdr list2)))))
3960       (nconc (nreverse out) (or list1 list2)))))
3961
3962 (defun gnus-intersection (list1 list2)
3963   (let ((result nil))
3964     (while list2
3965       (if (memq (car list2) list1)
3966           (setq result (cons (car list2) result)))
3967       (setq list2 (cdr list2)))
3968     result))
3969
3970 (defun gnus-sorted-intersection (list1 list2)
3971   ;; LIST1 and LIST2 have to be sorted over <.
3972   (let (out)
3973     (while (and list1 list2)
3974       (cond ((= (car list1) (car list2))
3975              (setq out (cons (car list1) out)
3976                    list1 (cdr list1)
3977                    list2 (cdr list2)))
3978             ((< (car list1) (car list2))
3979              (setq list1 (cdr list1)))
3980             (t
3981              (setq list2 (cdr list2)))))
3982     (nreverse out)))
3983
3984 (defun gnus-set-sorted-intersection (list1 list2)
3985   ;; LIST1 and LIST2 have to be sorted over <.
3986   ;; This function modifies LIST1.
3987   (let* ((top (cons nil list1))
3988          (prev top))
3989     (while (and list1 list2)
3990       (cond ((= (car list1) (car list2))
3991              (setq prev list1
3992                    list1 (cdr list1)
3993                    list2 (cdr list2)))
3994             ((< (car list1) (car list2))
3995              (setcdr prev (cdr list1))
3996              (setq list1 (cdr list1)))
3997             (t
3998              (setq list2 (cdr list2)))))
3999     (setcdr prev nil)
4000     (cdr top)))
4001
4002 (defun gnus-compress-sequence (numbers &optional always-list)
4003   "Convert list of numbers to a list of ranges or a single range.
4004 If ALWAYS-LIST is non-nil, this function will always release a list of
4005 ranges."
4006   (let* ((first (car numbers))
4007          (last (car numbers))
4008          result)
4009     (if (null numbers)
4010         nil
4011       (if (not (listp (cdr numbers)))
4012           numbers
4013         (while numbers
4014           (cond ((= last (car numbers)) nil) ;Omit duplicated number
4015                 ((= (1+ last) (car numbers)) ;Still in sequence
4016                  (setq last (car numbers)))
4017                 (t                      ;End of one sequence
4018                  (setq result
4019                        (cons (if (= first last) first
4020                                (cons first last)) result))
4021                  (setq first (car numbers))
4022                  (setq last  (car numbers))))
4023           (setq numbers (cdr numbers)))
4024         (if (and (not always-list) (null result))
4025             (if (= first last) (list first) (cons first last))
4026           (nreverse (cons (if (= first last) first (cons first last))
4027                           result)))))))
4028
4029 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
4030 (defun gnus-uncompress-range (ranges)
4031   "Expand a list of ranges into a list of numbers.
4032 RANGES is either a single range on the form `(num . num)' or a list of
4033 these ranges."
4034   (let (first last result)
4035     (cond
4036      ((null ranges)
4037       nil)
4038      ((not (listp (cdr ranges)))
4039       (setq first (car ranges))
4040       (setq last (cdr ranges))
4041       (while (<= first last)
4042         (setq result (cons first result))
4043         (setq first (1+ first)))
4044       (nreverse result))
4045      (t
4046       (while ranges
4047         (if (atom (car ranges))
4048             (if (numberp (car ranges))
4049                 (setq result (cons (car ranges) result)))
4050           (setq first (caar ranges))
4051           (setq last  (cdar ranges))
4052           (while (<= first last)
4053             (setq result (cons first result))
4054             (setq first (1+ first))))
4055         (setq ranges (cdr ranges)))
4056       (nreverse result)))))
4057
4058 (defun gnus-add-to-range (ranges list)
4059   "Return a list of ranges that has all articles from both RANGES and LIST.
4060 Note: LIST has to be sorted over `<'."
4061   (if (not ranges)
4062       (gnus-compress-sequence list t)
4063     (setq list (copy-sequence list))
4064     (or (listp (cdr ranges))
4065         (setq ranges (list ranges)))
4066     (let ((out ranges)
4067           ilist lowest highest temp)
4068       (while (and ranges list)
4069         (setq ilist list)
4070         (setq lowest (or (and (atom (car ranges)) (car ranges))
4071                          (caar ranges)))
4072         (while (and list (cdr list) (< (cadr list) lowest))
4073           (setq list (cdr list)))
4074         (if (< (car ilist) lowest)
4075             (progn
4076               (setq temp list)
4077               (setq list (cdr list))
4078               (setcdr temp nil)
4079               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4080         (setq highest (or (and (atom (car ranges)) (car ranges))
4081                           (cdar ranges)))
4082         (while (and list (<= (car list) highest))
4083           (setq list (cdr list)))
4084         (setq ranges (cdr ranges)))
4085       (if list
4086           (setq out (nconc (gnus-compress-sequence list t) out)))
4087       (setq out (sort out (lambda (r1 r2)
4088                             (< (or (and (atom r1) r1) (car r1))
4089                                (or (and (atom r2) r2) (car r2))))))
4090       (setq ranges out)
4091       (while ranges
4092         (if (atom (car ranges))
4093             (if (cdr ranges)
4094                 (if (atom (cadr ranges))
4095                     (if (= (1+ (car ranges)) (cadr ranges))
4096                         (progn
4097                           (setcar ranges (cons (car ranges)
4098                                                (cadr ranges)))
4099                           (setcdr ranges (cddr ranges))))
4100                   (if (= (1+ (car ranges)) (caadr ranges))
4101                       (progn
4102                         (setcar (cadr ranges) (car ranges))
4103                         (setcar ranges (cadr ranges))
4104                         (setcdr ranges (cddr ranges))))))
4105           (if (cdr ranges)
4106               (if (atom (cadr ranges))
4107                   (if (= (1+ (cdar ranges)) (cadr ranges))
4108                       (progn
4109                         (setcdr (car ranges) (cadr ranges))
4110                         (setcdr ranges (cddr ranges))))
4111                 (if (= (1+ (cdar ranges)) (caadr ranges))
4112                     (progn
4113                       (setcdr (car ranges) (cdadr ranges))
4114                       (setcdr ranges (cddr ranges)))))))
4115         (setq ranges (cdr ranges)))
4116       out)))
4117
4118 (defun gnus-remove-from-range (ranges list)
4119   "Return a list of ranges that has all articles from LIST removed from RANGES.
4120 Note: LIST has to be sorted over `<'."
4121   ;; !!! This function shouldn't look like this, but I've got a headache.
4122   (gnus-compress-sequence
4123    (gnus-sorted-complement
4124     (gnus-uncompress-range ranges) list)))
4125
4126 (defun gnus-member-of-range (number ranges)
4127   (if (not (listp (cdr ranges)))
4128       (and (>= number (car ranges))
4129            (<= number (cdr ranges)))
4130     (let ((not-stop t))
4131       (while (and ranges
4132                   (if (numberp (car ranges))
4133                       (>= number (car ranges))
4134                     (>= number (caar ranges)))
4135                   not-stop)
4136         (if (if (numberp (car ranges))
4137                 (= number (car ranges))
4138               (and (>= number (caar ranges))
4139                    (<= number (cdar ranges))))
4140             (setq not-stop nil))
4141         (setq ranges (cdr ranges)))
4142       (not not-stop))))
4143
4144 (defun gnus-range-length (range)
4145   "Return the length RANGE would have if uncompressed."
4146   (length (gnus-uncompress-range range)))
4147
4148 (defun gnus-sublist-p (list sublist)
4149   "Test whether all elements in SUBLIST are members of LIST."
4150   (let ((sublistp t))
4151     (while sublist
4152       (unless (memq (pop sublist) list)
4153         (setq sublistp nil
4154               sublist nil)))
4155     sublistp))
4156
4157 \f
4158 ;;;
4159 ;;; Gnus group mode
4160 ;;;
4161
4162 (defvar gnus-group-mode-map nil)
4163 (put 'gnus-group-mode 'mode-class 'special)
4164
4165 (unless gnus-group-mode-map
4166   (setq gnus-group-mode-map (make-keymap))
4167   (suppress-keymap gnus-group-mode-map)
4168
4169   (gnus-define-keys gnus-group-mode-map
4170     " " gnus-group-read-group
4171     "=" gnus-group-select-group
4172     "\r" gnus-group-select-group
4173     "\M-\r" gnus-group-quick-select-group
4174     "j" gnus-group-jump-to-group
4175     "n" gnus-group-next-unread-group
4176     "p" gnus-group-prev-unread-group
4177     "\177" gnus-group-prev-unread-group
4178     [delete] gnus-group-prev-unread-group
4179     "N" gnus-group-next-group
4180     "P" gnus-group-prev-group
4181     "\M-n" gnus-group-next-unread-group-same-level
4182     "\M-p" gnus-group-prev-unread-group-same-level
4183     "," gnus-group-best-unread-group
4184     "." gnus-group-first-unread-group
4185     "u" gnus-group-unsubscribe-current-group
4186     "U" gnus-group-unsubscribe-group
4187     "c" gnus-group-catchup-current
4188     "C" gnus-group-catchup-current-all
4189     "l" gnus-group-list-groups
4190     "L" gnus-group-list-all-groups
4191     "m" gnus-group-mail
4192     "g" gnus-group-get-new-news
4193     "\M-g" gnus-group-get-new-news-this-group
4194     "R" gnus-group-restart
4195     "r" gnus-group-read-init-file
4196     "B" gnus-group-browse-foreign-server
4197     "b" gnus-group-check-bogus-groups
4198     "F" gnus-find-new-newsgroups
4199     "\C-c\C-d" gnus-group-describe-group
4200     "\M-d" gnus-group-describe-all-groups
4201     "\C-c\C-a" gnus-group-apropos
4202     "\C-c\M-\C-a" gnus-group-description-apropos
4203     "a" gnus-group-post-news
4204     "\ek" gnus-group-edit-local-kill
4205     "\eK" gnus-group-edit-global-kill
4206     "\C-k" gnus-group-kill-group
4207     "\C-y" gnus-group-yank-group
4208     "\C-w" gnus-group-kill-region
4209     "\C-x\C-t" gnus-group-transpose-groups
4210     "\C-c\C-l" gnus-group-list-killed
4211     "\C-c\C-x" gnus-group-expire-articles
4212     "\C-c\M-\C-x" gnus-group-expire-all-groups
4213     "V" gnus-version
4214     "s" gnus-group-save-newsrc
4215     "z" gnus-group-suspend
4216 ;    "Z" gnus-group-clear-dribble
4217     "q" gnus-group-exit
4218     "Q" gnus-group-quit
4219     "?" gnus-group-describe-briefly
4220     "\C-c\C-i" gnus-info-find-node
4221     "\M-e" gnus-group-edit-group-method
4222     "^" gnus-group-enter-server-mode
4223     gnus-mouse-2 gnus-mouse-pick-group
4224     "<" beginning-of-buffer
4225     ">" end-of-buffer
4226     "\C-c\C-b" gnus-bug
4227     "\C-c\C-s" gnus-group-sort-groups
4228     "t" gnus-topic-mode
4229     "\C-c\M-g" gnus-activate-all-groups
4230     "\M-&" gnus-group-universal-argument
4231     "#" gnus-group-mark-group
4232     "\M-#" gnus-group-unmark-group)
4233
4234   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4235     "m" gnus-group-mark-group
4236     "u" gnus-group-unmark-group
4237     "w" gnus-group-mark-region
4238     "m" gnus-group-mark-buffer
4239     "r" gnus-group-mark-regexp
4240     "U" gnus-group-unmark-all-groups)
4241
4242   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4243     "d" gnus-group-make-directory-group
4244     "h" gnus-group-make-help-group
4245     "a" gnus-group-make-archive-group
4246     "k" gnus-group-make-kiboze-group
4247     "m" gnus-group-make-group
4248     "E" gnus-group-edit-group
4249     "e" gnus-group-edit-group-method
4250     "p" gnus-group-edit-group-parameters
4251     "v" gnus-group-add-to-virtual
4252     "V" gnus-group-make-empty-virtual
4253     "D" gnus-group-enter-directory
4254     "f" gnus-group-make-doc-group
4255     "r" gnus-group-rename-group
4256     "\177" gnus-group-delete-group
4257     [delete] gnus-group-delete-group)
4258
4259    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4260      "b" gnus-group-brew-soup
4261      "w" gnus-soup-save-areas
4262      "s" gnus-soup-send-replies
4263      "p" gnus-soup-pack-packet
4264      "r" nnsoup-pack-replies)
4265
4266    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4267      "s" gnus-group-sort-groups
4268      "a" gnus-group-sort-groups-by-alphabet
4269      "u" gnus-group-sort-groups-by-unread
4270      "l" gnus-group-sort-groups-by-level
4271      "v" gnus-group-sort-groups-by-score
4272      "r" gnus-group-sort-groups-by-rank
4273      "m" gnus-group-sort-groups-by-method)
4274
4275    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4276      "k" gnus-group-list-killed
4277      "z" gnus-group-list-zombies
4278      "s" gnus-group-list-groups
4279      "u" gnus-group-list-all-groups
4280      "A" gnus-group-list-active
4281      "a" gnus-group-apropos
4282      "d" gnus-group-description-apropos
4283      "m" gnus-group-list-matching
4284      "M" gnus-group-list-all-matching
4285      "l" gnus-group-list-level)
4286
4287    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4288      "f" gnus-score-flush-cache)
4289
4290    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4291      "f" gnus-group-fetch-faq)
4292
4293    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4294      "l" gnus-group-set-current-level
4295      "t" gnus-group-unsubscribe-current-group
4296      "s" gnus-group-unsubscribe-group
4297      "k" gnus-group-kill-group
4298      "y" gnus-group-yank-group
4299      "w" gnus-group-kill-region
4300      "\C-k" gnus-group-kill-level
4301      "z" gnus-group-kill-all-zombies))
4302
4303 (defun gnus-group-mode ()
4304   "Major mode for reading news.
4305
4306 All normal editing commands are switched off.
4307 \\<gnus-group-mode-map>
4308 The group buffer lists (some of) the groups available.  For instance,
4309 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4310 lists all zombie groups.
4311
4312 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4313 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4314
4315 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4316
4317 The following commands are available:
4318
4319 \\{gnus-group-mode-map}"
4320   (interactive)
4321   (when (and menu-bar-mode
4322              (gnus-visual-p 'group-menu 'menu))
4323     (gnus-group-make-menu-bar))
4324   (kill-all-local-variables)
4325   (gnus-simplify-mode-line)
4326   (setq major-mode 'gnus-group-mode)
4327   (setq mode-name "Group")
4328   (gnus-group-set-mode-line)
4329   (setq mode-line-process nil)
4330   (use-local-map gnus-group-mode-map)
4331   (buffer-disable-undo (current-buffer))
4332   (setq truncate-lines t)
4333   (setq buffer-read-only t)
4334   (gnus-make-local-hook 'post-command-hook)
4335   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
4336   (run-hooks 'gnus-group-mode-hook))
4337
4338 (defun gnus-clear-inboxes-moved ()
4339   (setq nnmail-moved-inboxes nil))
4340
4341 (defun gnus-mouse-pick-group (e)
4342   "Enter the group under the mouse pointer."
4343   (interactive "e")
4344   (mouse-set-point e)
4345   (gnus-group-read-group nil))
4346
4347 ;; Look at LEVEL and find out what the level is really supposed to be.
4348 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4349 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4350 (defun gnus-group-default-level (&optional level number-or-nil)
4351   (cond
4352    (gnus-group-use-permanent-levels
4353     (or (setq gnus-group-use-permanent-levels
4354               (or level (if (numberp gnus-group-use-permanent-levels)
4355                             gnus-group-use-permanent-levels
4356                           (or gnus-group-default-list-level
4357                               gnus-level-subscribed))))
4358         gnus-group-default-list-level gnus-level-subscribed))
4359    (number-or-nil
4360     level)
4361    (t
4362     (or level gnus-group-default-list-level gnus-level-subscribed))))
4363
4364 ;;;###autoload
4365 (defun gnus-slave-no-server (&optional arg)
4366   "Read network news as a slave, without connecting to local server"
4367   (interactive "P")
4368   (gnus-no-server arg t))
4369
4370 ;;;###autoload
4371 (defun gnus-no-server (&optional arg slave)
4372   "Read network news.
4373 If ARG is a positive number, Gnus will use that as the
4374 startup level.  If ARG is nil, Gnus will be started at level 2.
4375 If ARG is non-nil and not a positive number, Gnus will
4376 prompt the user for the name of an NNTP server to use.
4377 As opposed to `gnus', this command will not connect to the local server."
4378   (interactive "P")
4379   (let ((val (or arg (1- gnus-level-default-subscribed))))
4380     (gnus val t slave)
4381     (make-local-variable 'gnus-group-use-permanent-levels)
4382     (setq gnus-group-use-permanent-levels val)))
4383
4384 ;;;###autoload
4385 (defun gnus-slave (&optional arg)
4386   "Read news as a slave."
4387   (interactive "P")
4388   (gnus arg nil 'slave))
4389
4390 ;;;###autoload
4391 (defun gnus-other-frame (&optional arg)
4392   "Pop up a frame to read news."
4393   (interactive "P")
4394   (if (get-buffer gnus-group-buffer)
4395       (let ((pop-up-frames t))
4396         (gnus arg))
4397     (select-frame (make-frame))
4398     (gnus arg)))
4399
4400 ;;;###autoload
4401 (defun gnus (&optional arg dont-connect slave)
4402   "Read network news.
4403 If ARG is non-nil and a positive number, Gnus will use that as the
4404 startup level.  If ARG is non-nil and not a positive number, Gnus will
4405 prompt the user for the name of an NNTP server to use."
4406   (interactive "P")
4407
4408   (if (get-buffer gnus-group-buffer)
4409       (progn
4410         (switch-to-buffer gnus-group-buffer)
4411         (gnus-group-get-new-news))
4412
4413     (gnus-clear-system)
4414     (nnheader-init-server-buffer)
4415     (gnus-read-init-file)
4416     (setq gnus-slave slave)
4417
4418     (gnus-group-setup-buffer)
4419     (let ((buffer-read-only nil))
4420       (erase-buffer)
4421       (if (not gnus-inhibit-startup-message)
4422           (progn
4423             (gnus-group-startup-message)
4424             (sit-for 0))))
4425
4426     (let ((level (and (numberp arg) (> arg 0) arg))
4427           did-connect)
4428       (unwind-protect
4429           (progn
4430             (or dont-connect
4431                 (setq did-connect
4432                       (gnus-start-news-server (and arg (not level))))))
4433         (if (and (not dont-connect)
4434                  (not did-connect))
4435             (gnus-group-quit)
4436           (run-hooks 'gnus-startup-hook)
4437           ;; NNTP server is successfully open.
4438
4439           ;; Find the current startup file name.
4440           (setq gnus-current-startup-file
4441                 (gnus-make-newsrc-file gnus-startup-file))
4442
4443           ;; Read the dribble file.
4444           (when (or gnus-slave gnus-use-dribble-file)
4445             (gnus-dribble-read-file))
4446
4447           ;; Allow using GroupLens predictions.
4448           (when gnus-use-grouplens
4449             (bbb-login)
4450             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4451
4452           (gnus-summary-make-display-table)
4453           ;; Do the actual startup.
4454           (gnus-setup-news nil level dont-connect)
4455           ;; Generate the group buffer.
4456           (gnus-group-list-groups level)
4457           (gnus-group-first-unread-group)
4458           (gnus-configure-windows 'group)
4459           (gnus-group-set-mode-line))))))
4460
4461 (defun gnus-unload ()
4462   "Unload all Gnus features."
4463   (interactive)
4464   (or (boundp 'load-history)
4465       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4466   (let ((history load-history)
4467         feature)
4468     (while history
4469       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4470            (setq feature (cdr (assq 'provide (car history))))
4471            (unload-feature feature 'force))
4472       (setq history (cdr history)))))
4473
4474 (defun gnus-compile ()
4475   "Byte-compile the user-defined format specs."
4476   (interactive)
4477   (let ((entries gnus-format-specs)
4478         entry gnus-tmp-func)
4479     (save-excursion
4480       (gnus-message 7 "Compiling format specs...")
4481
4482       (while entries
4483         (setq entry (pop entries))
4484         (if (eq (car entry) 'version)
4485             (setq gnus-format-specs (delq entry gnus-format-specs))
4486           (when (and (listp (caddr entry))
4487                      (not (eq 'byte-code (caaddr entry))))
4488             (fset 'gnus-tmp-func
4489                   `(lambda () ,(caddr entry)))
4490             (byte-compile 'gnus-tmp-func)
4491             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4492
4493       (push (cons 'version emacs-version) gnus-format-specs)
4494
4495       (gnus-message 7 "Compiling user specs...done"))))
4496
4497 (defun gnus-indent-rigidly (start end arg)
4498   "Indent rigidly using only spaces and no tabs."
4499   (save-excursion
4500     (save-restriction
4501       (narrow-to-region start end)
4502       (indent-rigidly start end arg)
4503       (goto-char (point-min))
4504       (while (search-forward "\t" nil t)
4505         (replace-match "        " t t)))))
4506
4507 (defun gnus-group-startup-message (&optional x y)
4508   "Insert startup message in current buffer."
4509   ;; Insert the message.
4510   (erase-buffer)
4511   (insert
4512    (format "              %s
4513           _    ___ _             _
4514           _ ___ __ ___  __    _ ___
4515           __   _     ___    __  ___
4516               _           ___     _
4517              _  _ __             _
4518              ___   __            _
4519                    __           _
4520                     _      _   _
4521                    _      _    _
4522                       _  _    _
4523                   __  ___
4524                  _   _ _     _
4525                 _   _
4526               _    _
4527              _    _
4528             _
4529           __
4530
4531 "
4532            ""))
4533   ;; And then hack it.
4534   (gnus-indent-rigidly (point-min) (point-max)
4535                        (/ (max (- (window-width) (or x 46)) 0) 2))
4536   (goto-char (point-min))
4537   (forward-line 1)
4538   (let* ((pheight (count-lines (point-min) (point-max)))
4539          (wheight (window-height))
4540          (rest (- wheight pheight)))
4541     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4542   ;; Fontify some.
4543   (goto-char (point-min))
4544   (and (search-forward "Praxis" nil t)
4545        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4546   (goto-char (point-min))
4547   (let* ((mode-string (gnus-group-set-mode-line)))
4548     (setq mode-line-buffer-identification
4549           (list (concat gnus-version (substring (car mode-string) 4))))
4550     (set-buffer-modified-p t)))
4551
4552 (defun gnus-group-setup-buffer ()
4553   (or (get-buffer gnus-group-buffer)
4554       (progn
4555         (switch-to-buffer gnus-group-buffer)
4556         (gnus-add-current-to-buffer-list)
4557         (gnus-group-mode)
4558         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4559
4560 (defun gnus-group-list-groups (&optional level unread lowest)
4561   "List newsgroups with level LEVEL or lower that have unread articles.
4562 Default is all subscribed groups.
4563 If argument UNREAD is non-nil, groups with no unread articles are also
4564 listed."
4565   (interactive (list (if current-prefix-arg
4566                          (prefix-numeric-value current-prefix-arg)
4567                        (or
4568                         (gnus-group-default-level nil t)
4569                         gnus-group-default-list-level
4570                         gnus-level-subscribed))))
4571   (or level
4572       (setq level (car gnus-group-list-mode)
4573             unread (cdr gnus-group-list-mode)))
4574   (setq level (gnus-group-default-level level))
4575   (gnus-group-setup-buffer)             ;May call from out of group buffer
4576   (gnus-update-format-specifications)
4577   (let ((case-fold-search nil)
4578         (props (text-properties-at (gnus-point-at-bol)))
4579         (group (gnus-group-group-name)))
4580     (set-buffer gnus-group-buffer)
4581     (funcall gnus-group-prepare-function level unread lowest)
4582     (if (zerop (buffer-size))
4583         (gnus-message 5 gnus-no-groups-message)
4584       (goto-char (point-max))
4585       (when (or (not gnus-group-goto-next-group-function)
4586                 (not (funcall gnus-group-goto-next-group-function 
4587                               group props)))
4588         (if (not group)
4589             ;; Go to the first group with unread articles.
4590             (gnus-group-search-forward t)
4591           ;; Find the right group to put point on.  If the current group
4592           ;; has disappeared in the new listing, try to find the next
4593           ;; one.        If no next one can be found, just leave point at the
4594           ;; first newsgroup in the buffer.
4595           (if (not (gnus-goto-char
4596                     (text-property-any
4597                      (point-min) (point-max)
4598                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4599               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4600                 (while (and newsrc
4601                             (not (gnus-goto-char
4602                                   (text-property-any
4603                                    (point-min) (point-max) 'gnus-group
4604                                    (gnus-intern-safe
4605                                     (caar newsrc) gnus-active-hashtb)))))
4606                   (setq newsrc (cdr newsrc)))
4607                 (or newsrc (progn (goto-char (point-max))
4608                                   (forward-line -1)))))))
4609       ;; Adjust cursor point.
4610       (gnus-group-position-point))))
4611
4612 (defun gnus-group-list-level (level &optional all)
4613   "List groups on LEVEL.
4614 If ALL (the prefix), also list groups that have no unread articles."
4615   (interactive "nList groups on level: \nP")
4616   (gnus-group-list-groups level all level))
4617
4618 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4619   "List all newsgroups with unread articles of level LEVEL or lower.
4620 If ALL is non-nil, list groups that have no unread articles.
4621 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4622 If REGEXP, only list groups matching REGEXP."
4623   (set-buffer gnus-group-buffer)
4624   (let ((buffer-read-only nil)
4625         (newsrc (cdr gnus-newsrc-alist))
4626         (lowest (or lowest 1))
4627         info clevel unread group params)
4628     (erase-buffer)
4629     (if (< lowest gnus-level-zombie)
4630         ;; List living groups.
4631         (while newsrc
4632           (setq info (car newsrc)
4633                 group (gnus-info-group info)
4634                 params (gnus-info-params info)
4635                 newsrc (cdr newsrc)
4636                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4637           (and unread                   ; This group might be bogus
4638                (or (not regexp)
4639                    (string-match regexp group))
4640                (<= (setq clevel (gnus-info-level info)) level)
4641                (>= clevel lowest)
4642                (or all                  ; We list all groups?
4643                    (if (eq unread t)    ; Unactivated?
4644                        gnus-group-list-inactive-groups ; We list unactivated 
4645                      (> unread 0))      ; We list groups with unread articles
4646                    (and gnus-list-groups-with-ticked-articles
4647                         (cdr (assq 'tick (gnus-info-marks info))))
4648                                         ; And groups with tickeds
4649                    ;; Check for permanent visibility.
4650                    (and gnus-permanently-visible-groups
4651                         (string-match gnus-permanently-visible-groups
4652                                       group))
4653                    (memq 'visible params)
4654                    (cdr (assq 'visible params)))
4655                (gnus-group-insert-group-line
4656                 group (gnus-info-level info)
4657                 (gnus-info-marks info) unread (gnus-info-method info)))))
4658
4659     ;; List dead groups.
4660     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4661          (gnus-group-prepare-flat-list-dead
4662           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4663           gnus-level-zombie ?Z
4664           regexp))
4665     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4666          (gnus-group-prepare-flat-list-dead
4667           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4668           gnus-level-killed ?K regexp))
4669
4670     (gnus-group-set-mode-line)
4671     (setq gnus-group-list-mode (cons level all))
4672     (run-hooks 'gnus-group-prepare-hook)))
4673
4674 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4675   ;; List zombies and killed lists somewhat faster, which was
4676   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4677   ;; this by ignoring the group format specification altogether.
4678   (let (group)
4679     (if regexp
4680         ;; This loop is used when listing groups that match some
4681         ;; regexp.
4682         (while groups
4683           (setq group (pop groups))
4684           (when (string-match regexp group)
4685             (gnus-add-text-properties
4686              (point) (prog1 (1+ (point))
4687                        (insert " " mark "     *: " group "\n"))
4688              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4689                    'gnus-unread t
4690                    'gnus-level level))))
4691       ;; This loop is used when listing all groups.
4692       (while groups
4693         (gnus-add-text-properties
4694          (point) (prog1 (1+ (point))
4695                    (insert " " mark "     *: "
4696                            (setq group (pop groups)) "\n"))
4697          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4698                'gnus-unread t
4699                'gnus-level level))))))
4700
4701 (defmacro gnus-group-real-name (group)
4702   "Find the real name of a foreign newsgroup."
4703   `(let ((gname ,group))
4704      (if (string-match ":[^:]+$" gname)
4705          (substring gname (1+ (match-beginning 0)))
4706        gname)))
4707
4708 (defsubst gnus-server-add-address (method)
4709   (let ((method-name (symbol-name (car method))))
4710     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4711              (not (assq (intern (concat method-name "-address")) method)))
4712         (append method (list (list (intern (concat method-name "-address"))
4713                                    (nth 1 method))))
4714       method)))
4715
4716 (defsubst gnus-server-get-method (group method)
4717   ;; Input either a server name, and extended server name, or a
4718   ;; select method, and return a select method.
4719   (cond ((stringp method)
4720          (gnus-server-to-method method))
4721         ((equal method gnus-select-method)
4722          gnus-select-method)
4723         ((and (stringp (car method)) group)
4724          (gnus-server-extend-method group method))
4725         ((and method (not group)
4726               (equal (cadr method) ""))
4727          method)
4728         (t
4729          (gnus-server-add-address method))))
4730
4731 (defun gnus-server-to-method (server)
4732   "Map virtual server names to select methods."
4733   (or 
4734    ;; Is this a method, perhaps?
4735    (and server (listp server) server)
4736    ;; Perhaps this is the native server?
4737    (and (equal server "native") gnus-select-method)
4738    ;; It should be in the server alist.
4739    (cdr (assoc server gnus-server-alist))
4740    ;; If not, we look through all the opened server
4741    ;; to see whether we can find it there.
4742    (let ((opened gnus-opened-servers))
4743      (while (and opened
4744                  (not (equal server (format "%s:%s" (caaar opened)
4745                                             (cadaar opened)))))
4746        (pop opened))
4747      (caar opened))))
4748
4749 (defmacro gnus-method-equal (ss1 ss2)
4750   "Say whether two servers are equal."
4751   `(let ((s1 ,ss1)
4752          (s2 ,ss2))
4753      (or (equal s1 s2)
4754          (and (= (length s1) (length s2))
4755               (progn
4756                 (while (and s1 (member (car s1) s2))
4757                   (setq s1 (cdr s1)))
4758                 (null s1))))))
4759
4760 (defun gnus-server-equal (m1 m2)
4761   "Say whether two methods are equal."
4762   (let ((m1 (cond ((null m1) gnus-select-method)
4763                   ((stringp m1) (gnus-server-to-method m1))
4764                   (t m1)))
4765         (m2 (cond ((null m2) gnus-select-method)
4766                   ((stringp m2) (gnus-server-to-method m2))
4767                   (t m2))))
4768     (gnus-method-equal m1 m2)))
4769
4770 (defun gnus-servers-using-backend (backend)
4771   "Return a list of known servers using BACKEND."
4772   (let ((opened gnus-opened-servers)
4773         out)
4774     (while opened
4775       (when (eq backend (caaar opened))
4776         (push (caar opened) out))
4777       (pop opened))
4778     out))
4779
4780 (defun gnus-archive-server-wanted-p ()
4781   "Say whether the user wants to use the archive server."
4782   (cond 
4783    ((or (not gnus-message-archive-method)
4784         (not gnus-message-archive-group))
4785     nil)
4786    ((and gnus-message-archive-method gnus-message-archive-group)
4787     t)
4788    (t
4789     (let ((active (cadr (assq 'nnfolder-active-file
4790                               gnus-message-archive-method))))
4791       (and active
4792            (file-exists-p active))))))
4793
4794 (defun gnus-group-prefixed-name (group method)
4795   "Return the whole name from GROUP and METHOD."
4796   (and (stringp method) (setq method (gnus-server-to-method method)))
4797   (concat (format "%s" (car method))
4798           (if (and
4799                (or (assoc (format "%s" (car method)) 
4800                           (gnus-methods-using 'address))
4801                    (gnus-server-equal method gnus-message-archive-method))
4802                (nth 1 method)
4803                (not (string= (nth 1 method) "")))
4804               (concat "+" (nth 1 method)))
4805           ":" group))
4806
4807 (defun gnus-group-real-prefix (group)
4808   "Return the prefix of the current group name."
4809   (if (string-match "^[^:]+:" group)
4810       (substring group 0 (match-end 0))
4811     ""))
4812
4813 (defun gnus-group-method (group)
4814   "Return the server or method used for selecting GROUP."
4815   (let ((prefix (gnus-group-real-prefix group)))
4816     (if (equal prefix "")
4817         gnus-select-method
4818       (let ((servers gnus-opened-servers)
4819             (server "")
4820             backend possible found)
4821         (if (string-match "^[^\\+]+\\+" prefix)
4822             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4823                   server (substring prefix (match-end 0) (1- (length prefix))))
4824           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4825         (while servers
4826           (when (eq (caaar servers) backend)
4827             (setq possible (caar servers))
4828             (when (equal (cadaar servers) server)
4829               (setq found (caar servers))))
4830           (pop servers))
4831         (or (car (rassoc found gnus-server-alist))
4832             found
4833             (car (rassoc possible gnus-server-alist))
4834             possible
4835             (list backend server))))))
4836
4837 (defsubst gnus-secondary-method-p (method)
4838   "Return whether METHOD is a secondary select method."
4839   (let ((methods gnus-secondary-select-methods)
4840         (gmethod (gnus-server-get-method nil method)))
4841     (while (and methods
4842                 (not (equal (gnus-server-get-method nil (car methods))
4843                             gmethod)))
4844       (setq methods (cdr methods)))
4845     methods))
4846
4847 (defun gnus-group-foreign-p (group)
4848   "Say whether a group is foreign or not."
4849   (and (not (gnus-group-native-p group))
4850        (not (gnus-group-secondary-p group))))
4851
4852 (defun gnus-group-native-p (group)
4853   "Say whether the group is native or not."
4854   (not (string-match ":" group)))
4855
4856 (defun gnus-group-secondary-p (group)
4857   "Say whether the group is secondary or not."
4858   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4859
4860 (defun gnus-group-get-parameter (group &optional symbol)
4861   "Returns the group parameters for GROUP.
4862 If SYMBOL, return the value of that symbol in the group parameters."
4863   (let ((params (gnus-info-params (gnus-get-info group))))
4864     (if symbol
4865         (gnus-group-parameter-value params symbol)
4866       params)))
4867
4868 (defun gnus-group-parameter-value (params symbol)
4869   "Return the value of SYMBOL in group PARAMS."
4870   (or (car (memq symbol params))        ; It's either a simple symbol
4871       (cdr (assq symbol params))))      ; or a cons.
4872
4873 (defun gnus-group-add-parameter (group param)
4874   "Add parameter PARAM to GROUP."
4875   (let ((info (gnus-get-info group)))
4876     (if (not info)
4877         () ; This is a dead group.  We just ignore it.
4878       ;; Cons the new param to the old one and update.
4879       (gnus-group-set-info (cons param (gnus-info-params info))
4880                            group 'params))))
4881
4882 (defun gnus-group-set-parameter (group name value)
4883   "Set parameter NAME to VALUE in GROUP."
4884   (let ((info (gnus-get-info group)))
4885     (if (not info)
4886         () ; This is a dead group.  We just ignore it.
4887       (let ((old-params (gnus-info-params info))
4888             (new-params (list (cons name value))))
4889         (while old-params
4890           (if (or (not (listp (car old-params)))
4891                   (not (eq (caar old-params) name)))
4892               (setq new-params (append new-params (list (car old-params)))))
4893           (setq old-params (cdr old-params)))
4894         (gnus-group-set-info new-params group 'params)))))
4895
4896 (defun gnus-group-add-score (group &optional score)
4897   "Add SCORE to the GROUP score.
4898 If SCORE is nil, add 1 to the score of GROUP."
4899   (let ((info (gnus-get-info group)))
4900     (when info
4901       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4902
4903 (defun gnus-summary-bubble-group ()
4904   "Increase the score of the current group.
4905 This is a handy function to add to `gnus-summary-exit-hook' to
4906 increase the score of each group you read."
4907   (gnus-group-add-score gnus-newsgroup-name))
4908
4909 (defun gnus-group-set-info (info &optional method-only-group part)
4910   (let* ((entry (gnus-gethash
4911                  (or method-only-group (gnus-info-group info))
4912                  gnus-newsrc-hashtb))
4913          (part-info info)
4914          (info (if method-only-group (nth 2 entry) info))
4915          method)
4916     (when method-only-group
4917       (unless entry
4918         (error "Trying to change non-existent group %s" method-only-group))
4919       ;; We have received parts of the actual group info - either the
4920       ;; select method or the group parameters.  We first check
4921       ;; whether we have to extend the info, and if so, do that.
4922       (let ((len (length info))
4923             (total (if (eq part 'method) 5 6)))
4924         (when (< len total)
4925           (setcdr (nthcdr (1- len) info)
4926                   (make-list (- total len) nil)))
4927         ;; Then we enter the new info.
4928         (setcar (nthcdr (1- total) info) part-info)))
4929     (unless entry
4930       ;; This is a new group, so we just create it.
4931       (save-excursion
4932         (set-buffer gnus-group-buffer)
4933         (setq method (gnus-info-method info))
4934         (when (gnus-server-equal method "native")
4935           (setq method nil))
4936         (save-excursion
4937           (set-buffer gnus-group-buffer)
4938           (if method
4939               ;; It's a foreign group...
4940               (gnus-group-make-group
4941                (gnus-group-real-name (gnus-info-group info))
4942                (if (stringp method) method
4943                  (prin1-to-string (car method)))
4944                (and (consp method)
4945                     (nth 1 (gnus-info-method info))))
4946             ;; It's a native group.
4947             (gnus-group-make-group (gnus-info-group info))))
4948         (gnus-message 6 "Note: New group created")
4949         (setq entry
4950               (gnus-gethash (gnus-group-prefixed-name
4951                              (gnus-group-real-name (gnus-info-group info))
4952                              (or (gnus-info-method info) gnus-select-method))
4953                             gnus-newsrc-hashtb))))
4954     ;; Whether it was a new group or not, we now have the entry, so we
4955     ;; can do the update.
4956     (if entry
4957         (progn
4958           (setcar (nthcdr 2 entry) info)
4959           (when (and (not (eq (car entry) t))
4960                      (gnus-active (gnus-info-group info)))
4961             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4962       (error "No such group: %s" (gnus-info-group info)))))
4963
4964 (defun gnus-group-set-method-info (group select-method)
4965   (gnus-group-set-info select-method group 'method))
4966
4967 (defun gnus-group-set-params-info (group params)
4968   (gnus-group-set-info params group 'params))
4969
4970 (defun gnus-group-update-group-line ()
4971   "Update the current line in the group buffer."
4972   (let* ((buffer-read-only nil)
4973          (group (gnus-group-group-name))
4974          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4975          gnus-group-indentation)
4976     (when group
4977       (and entry
4978            (not (gnus-ephemeral-group-p group))
4979            (gnus-dribble-enter
4980             (concat "(gnus-group-set-info '"
4981                     (prin1-to-string (nth 2 entry)) ")")))
4982       (setq gnus-group-indentation (gnus-group-group-indentation))
4983       (gnus-delete-line)
4984       (gnus-group-insert-group-line-info group)
4985       (forward-line -1)
4986       (gnus-group-position-point))))
4987
4988 (defun gnus-group-insert-group-line-info (group)
4989   "Insert GROUP on the current line."
4990   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4991         active info)
4992     (if entry
4993         (progn
4994           ;; (Un)subscribed group.
4995           (setq info (nth 2 entry))
4996           (gnus-group-insert-group-line
4997            group (gnus-info-level info) (gnus-info-marks info)
4998            (or (car entry) t) (gnus-info-method info)))
4999       ;; This group is dead.
5000       (gnus-group-insert-group-line
5001        group
5002        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
5003        nil
5004        (if (setq active (gnus-active group))
5005            (- (1+ (cdr active)) (car active)) 0)
5006        nil))))
5007
5008 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
5009                                                     gnus-tmp-marked number
5010                                                     gnus-tmp-method)
5011   "Insert a group line in the group buffer."
5012   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
5013          (gnus-tmp-number-total
5014           (if gnus-tmp-active
5015               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
5016             0))
5017          (gnus-tmp-number-of-unread
5018           (if (numberp number) (int-to-string (max 0 number))
5019             "*"))
5020          (gnus-tmp-number-of-read
5021           (if (numberp number)
5022               (int-to-string (max 0 (- gnus-tmp-number-total number)))
5023             "*"))
5024          (gnus-tmp-subscribed
5025           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
5026                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
5027                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
5028                 (t ?K)))
5029          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
5030          (gnus-tmp-newsgroup-description
5031           (if gnus-description-hashtb
5032               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
5033             ""))
5034          (gnus-tmp-moderated
5035           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
5036          (gnus-tmp-moderated-string
5037           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
5038          (gnus-tmp-method
5039           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
5040          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
5041          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
5042          (gnus-tmp-news-method-string
5043           (if gnus-tmp-method
5044               (format "(%s:%s)" (car gnus-tmp-method)
5045                       (cadr gnus-tmp-method)) ""))
5046          (gnus-tmp-marked-mark
5047           (if (and (numberp number)
5048                    (zerop number)
5049                    (cdr (assq 'tick gnus-tmp-marked)))
5050               ?* ? ))
5051          (gnus-tmp-process-marked
5052           (if (member gnus-tmp-group gnus-group-marked)
5053               gnus-process-mark ? ))
5054          (gnus-tmp-grouplens
5055           (or (and gnus-use-grouplens
5056                    (bbb-grouplens-group-p gnus-tmp-group))
5057               ""))
5058          (buffer-read-only nil)
5059          header gnus-tmp-header)        ; passed as parameter to user-funcs.
5060     (beginning-of-line)
5061     (gnus-add-text-properties
5062      (point)
5063      (prog1 (1+ (point))
5064        ;; Insert the text.
5065        (eval gnus-group-line-format-spec))
5066      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
5067        gnus-unread ,(if (numberp number)
5068                         (string-to-int gnus-tmp-number-of-unread)
5069                       t)
5070        gnus-marked ,gnus-tmp-marked-mark
5071        gnus-indentation ,gnus-group-indentation
5072        gnus-level ,gnus-tmp-level))
5073     (when (inline (gnus-visual-p 'group-highlight 'highlight))
5074       (forward-line -1)
5075       (run-hooks 'gnus-group-update-hook)
5076       (forward-line))
5077     ;; Allow XEmacs to remove front-sticky text properties.
5078     (gnus-group-remove-excess-properties)))
5079
5080 (defun gnus-group-update-group (group &optional visible-only)
5081   "Update all lines where GROUP appear.
5082 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5083 already."
5084   (save-excursion
5085     (set-buffer gnus-group-buffer)
5086     ;; The buffer may be narrowed.
5087     (save-restriction
5088       (widen)
5089       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5090             (loc (point-min))
5091             found buffer-read-only)
5092         ;; Enter the current status into the dribble buffer.
5093         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5094           (if (and entry (not (gnus-ephemeral-group-p group)))
5095               (gnus-dribble-enter
5096                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5097                        ")"))))
5098         ;; Find all group instances.  If topics are in use, each group
5099         ;; may be listed in more than once.
5100         (while (setq loc (text-property-any
5101                           loc (point-max) 'gnus-group ident))
5102           (setq found t)
5103           (goto-char loc)
5104           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5105             (gnus-delete-line)
5106             (gnus-group-insert-group-line-info group)
5107             (save-excursion
5108               (forward-line -1)
5109               (run-hooks 'gnus-group-update-group-hook)))
5110           (setq loc (1+ loc)))
5111         (unless (or found visible-only)
5112           ;; No such line in the buffer, find out where it's supposed to
5113           ;; go, and insert it there (or at the end of the buffer).
5114           (if gnus-goto-missing-group-function
5115               (funcall gnus-goto-missing-group-function group)
5116             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5117               (while (and entry (car entry)
5118                           (not
5119                            (gnus-goto-char
5120                             (text-property-any
5121                              (point-min) (point-max)
5122                              'gnus-group (gnus-intern-safe
5123                                           (caar entry) gnus-active-hashtb)))))
5124                 (setq entry (cdr entry)))
5125               (or entry (goto-char (point-max)))))
5126           ;; Finally insert the line.
5127           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5128             (gnus-group-insert-group-line-info group)
5129             (save-excursion
5130               (forward-line -1)
5131               (run-hooks 'gnus-group-update-group-hook))))
5132         (gnus-group-set-mode-line)))))
5133
5134 (defun gnus-group-set-mode-line ()
5135   "Update the mode line in the group buffer."
5136   (when (memq 'group gnus-updated-mode-lines)
5137     ;; Yes, we want to keep this mode line updated.
5138     (save-excursion
5139       (set-buffer gnus-group-buffer)
5140       (let* ((gformat (or gnus-group-mode-line-format-spec
5141                           (setq gnus-group-mode-line-format-spec
5142                                 (gnus-parse-format
5143                                  gnus-group-mode-line-format
5144                                  gnus-group-mode-line-format-alist))))
5145              (gnus-tmp-news-server (cadr gnus-select-method))
5146              (gnus-tmp-news-method (car gnus-select-method))
5147              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5148              (max-len 60)
5149              gnus-tmp-header            ;Dummy binding for user-defined formats
5150              ;; Get the resulting string.
5151              (modified 
5152               (and gnus-dribble-buffer
5153                    (buffer-name gnus-dribble-buffer)
5154                    (buffer-modified-p gnus-dribble-buffer)
5155                    (save-excursion
5156                      (set-buffer gnus-dribble-buffer)
5157                      (not (zerop (buffer-size))))))
5158              (mode-string (eval gformat)))
5159         ;; Say whether the dribble buffer has been modified.
5160         (setq mode-line-modified
5161               (if modified "---*- " "----- "))
5162         ;; If the line is too long, we chop it off.
5163         (when (> (length mode-string) max-len)
5164           (setq mode-string (substring mode-string 0 (- max-len 4))))
5165         (prog1
5166             (setq mode-line-buffer-identification 
5167                   (gnus-mode-line-buffer-identification
5168                    (list mode-string)))
5169           (set-buffer-modified-p modified))))))
5170
5171 (defun gnus-group-group-name ()
5172   "Get the name of the newsgroup on the current line."
5173   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5174     (and group (symbol-name group))))
5175
5176 (defun gnus-group-group-level ()
5177   "Get the level of the newsgroup on the current line."
5178   (get-text-property (gnus-point-at-bol) 'gnus-level))
5179
5180 (defun gnus-group-group-indentation ()
5181   "Get the indentation of the newsgroup on the current line."
5182   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5183       (and gnus-group-indentation-function
5184            (funcall gnus-group-indentation-function))
5185       ""))
5186
5187 (defun gnus-group-group-unread ()
5188   "Get the number of unread articles of the newsgroup on the current line."
5189   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5190
5191 (defun gnus-group-search-forward (&optional backward all level first-too)
5192   "Find the next newsgroup with unread articles.
5193 If BACKWARD is non-nil, find the previous newsgroup instead.
5194 If ALL is non-nil, just find any newsgroup.
5195 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5196 group exists.
5197 If FIRST-TOO, the current line is also eligible as a target."
5198   (let ((way (if backward -1 1))
5199         (low gnus-level-killed)
5200         (beg (point))
5201         pos found lev)
5202     (if (and backward (progn (beginning-of-line)) (bobp))
5203         nil
5204       (or first-too (forward-line way))
5205       (while (and
5206               (not (eobp))
5207               (not (setq
5208                     found
5209                     (and (or all
5210                              (and
5211                               (let ((unread
5212                                      (get-text-property (point) 'gnus-unread)))
5213                                 (and (numberp unread) (> unread 0)))
5214                               (setq lev (get-text-property (point)
5215                                                            'gnus-level))
5216                               (<= lev gnus-level-subscribed)))
5217                          (or (not level)
5218                              (and (setq lev (get-text-property (point)
5219                                                                'gnus-level))
5220                                   (or (= lev level)
5221                                       (and (< lev low)
5222                                            (< level lev)
5223                                            (progn
5224                                              (setq low lev)
5225                                              (setq pos (point))
5226                                              nil))))))))
5227               (zerop (forward-line way)))))
5228     (if found
5229         (progn (gnus-group-position-point) t)
5230       (goto-char (or pos beg))
5231       (and pos t))))
5232
5233 ;;; Gnus group mode commands
5234
5235 ;; Group marking.
5236
5237 (defun gnus-group-mark-group (n &optional unmark no-advance)
5238   "Mark the current group."
5239   (interactive "p")
5240   (let ((buffer-read-only nil)
5241         group)
5242     (while (and (> n 0)
5243                 (not (eobp)))
5244       (when (setq group (gnus-group-group-name))
5245         ;; Update the mark.
5246         (beginning-of-line)
5247         (forward-char
5248          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5249         (delete-char 1)
5250         (if unmark
5251             (progn
5252               (insert " ")
5253               (setq gnus-group-marked (delete group gnus-group-marked)))
5254           (insert "#")
5255           (setq gnus-group-marked
5256                 (cons group (delete group gnus-group-marked)))))
5257       (or no-advance (gnus-group-next-group 1))
5258       (decf n))
5259     (gnus-summary-position-point)
5260     n))
5261
5262 (defun gnus-group-unmark-group (n)
5263   "Remove the mark from the current group."
5264   (interactive "p")
5265   (gnus-group-mark-group n 'unmark)
5266   (gnus-group-position-point))
5267
5268 (defun gnus-group-unmark-all-groups ()
5269   "Unmark all groups."
5270   (interactive)
5271   (let ((groups gnus-group-marked))
5272     (save-excursion
5273       (while groups
5274         (gnus-group-remove-mark (pop groups)))))
5275   (gnus-group-position-point))
5276
5277 (defun gnus-group-mark-region (unmark beg end)
5278   "Mark all groups between point and mark.
5279 If UNMARK, remove the mark instead."
5280   (interactive "P\nr")
5281   (let ((num (count-lines beg end)))
5282     (save-excursion
5283       (goto-char beg)
5284       (- num (gnus-group-mark-group num unmark)))))
5285
5286 (defun gnus-group-mark-buffer (&optional unmark)
5287   "Mark all groups in the buffer.
5288 If UNMARK, remove the mark instead."
5289   (interactive "P")
5290   (gnus-group-mark-region unmark (point-min) (point-max)))
5291
5292 (defun gnus-group-mark-regexp (regexp)
5293   "Mark all groups that match some regexp."
5294   (interactive "sMark (regexp): ")
5295   (let ((alist (cdr gnus-newsrc-alist))
5296         group)
5297     (while alist
5298       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5299         (gnus-group-set-mark group))))
5300   (gnus-group-position-point))
5301
5302 (defun gnus-group-remove-mark (group)
5303   "Remove the process mark from GROUP and move point there.
5304 Return nil if the group isn't displayed."
5305   (if (gnus-group-goto-group group)
5306       (save-excursion
5307         (gnus-group-mark-group 1 'unmark t)
5308         t)
5309     (setq gnus-group-marked
5310           (delete group gnus-group-marked))
5311     nil))
5312
5313 (defun gnus-group-set-mark (group)
5314   "Set the process mark on GROUP."
5315   (if (gnus-group-goto-group group) 
5316       (save-excursion
5317         (gnus-group-mark-group 1 nil t))
5318     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5319
5320 (defun gnus-group-universal-argument (arg &optional groups func)
5321   "Perform any command on all groups accoring to the process/prefix convention."
5322   (interactive "P")
5323   (let ((groups (or groups (gnus-group-process-prefix arg)))
5324         group func)
5325     (if (eq (setq func (or func
5326                            (key-binding
5327                             (read-key-sequence
5328                              (substitute-command-keys
5329                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5330             'undefined)
5331         (gnus-error 1 "Undefined key")
5332       (while groups
5333         (gnus-group-remove-mark (setq group (pop groups)))
5334         (command-execute func))))
5335   (gnus-group-position-point))
5336
5337 (defun gnus-group-process-prefix (n)
5338   "Return a list of groups to work on.
5339 Take into consideration N (the prefix) and the list of marked groups."
5340   (cond
5341    (n
5342     (setq n (prefix-numeric-value n))
5343     ;; There is a prefix, so we return a list of the N next
5344     ;; groups.
5345     (let ((way (if (< n 0) -1 1))
5346           (n (abs n))
5347           group groups)
5348       (save-excursion
5349         (while (and (> n 0)
5350                     (setq group (gnus-group-group-name)))
5351           (setq groups (cons group groups))
5352           (setq n (1- n))
5353           (gnus-group-next-group way)))
5354       (nreverse groups)))
5355    ((and (boundp 'transient-mark-mode)
5356          transient-mark-mode
5357          (boundp 'mark-active)
5358          mark-active)
5359     ;; Work on the region between point and mark.
5360     (let ((max (max (point) (mark)))
5361           groups)
5362       (save-excursion
5363         (goto-char (min (point) (mark)))
5364         (while
5365             (and
5366              (push (gnus-group-group-name) groups)
5367              (zerop (gnus-group-next-group 1))
5368              (< (point) max)))
5369         (nreverse groups))))
5370    (gnus-group-marked
5371     ;; No prefix, but a list of marked articles.
5372     (reverse gnus-group-marked))
5373    (t
5374     ;; Neither marked articles or a prefix, so we return the
5375     ;; current group.
5376     (let ((group (gnus-group-group-name)))
5377       (and group (list group))))))
5378
5379 ;; Selecting groups.
5380
5381 (defun gnus-group-read-group (&optional all no-article group)
5382   "Read news in this newsgroup.
5383 If the prefix argument ALL is non-nil, already read articles become
5384 readable.  IF ALL is a number, fetch this number of articles.  If the
5385 optional argument NO-ARTICLE is non-nil, no article will be
5386 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5387 group."
5388   (interactive "P")
5389   (let ((group (or group (gnus-group-group-name)))
5390         number active marked entry)
5391     (or group (error "No group on current line"))
5392     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5393                                             group gnus-newsrc-hashtb)))))
5394     ;; This group might be a dead group.  In that case we have to get
5395     ;; the number of unread articles from `gnus-active-hashtb'.
5396     (setq number
5397           (cond ((numberp all) all)
5398                 (entry (car entry))
5399                 ((setq active (gnus-active group))
5400                  (- (1+ (cdr active)) (car active)))))
5401     (gnus-summary-read-group
5402      group (or all (and (numberp number)
5403                         (zerop (+ number (length (cdr (assq 'tick marked)))
5404                                   (length (cdr (assq 'dormant marked)))))))
5405      no-article)))
5406
5407 (defun gnus-group-select-group (&optional all)
5408   "Select this newsgroup.
5409 No article is selected automatically.
5410 If ALL is non-nil, already read articles become readable.
5411 If ALL is a number, fetch this number of articles."
5412   (interactive "P")
5413   (gnus-group-read-group all t))
5414
5415 (defun gnus-group-quick-select-group (&optional all)
5416   "Select the current group \"quickly\".
5417 This means that no highlighting or scoring will be performed."
5418   (interactive "P")
5419   (let (gnus-visual
5420         gnus-score-find-score-files-function
5421         gnus-apply-kill-hook
5422         gnus-summary-expunge-below)
5423     (gnus-group-read-group all t)))
5424
5425 (defun gnus-group-visible-select-group (&optional all)
5426   "Select the current group without hiding any articles."
5427   (interactive "P")
5428   (let ((gnus-inhibit-limiting t))
5429     (gnus-group-read-group all t)))
5430
5431 ;;;###autoload
5432 (defun gnus-fetch-group (group)
5433   "Start Gnus if necessary and enter GROUP.
5434 Returns whether the fetching was successful or not."
5435   (interactive "sGroup name: ")
5436   (or (get-buffer gnus-group-buffer)
5437       (gnus))
5438   (gnus-group-read-group nil nil group))
5439
5440 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5441 ;; if selection was successful.
5442 (defun gnus-group-read-ephemeral-group
5443   (group method &optional activate quit-config)
5444   (let ((group (if (gnus-group-foreign-p group) group
5445                  (gnus-group-prefixed-name group method))))
5446     (gnus-sethash
5447      group
5448      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5449                      ((quit-config . ,(if quit-config quit-config
5450                                         (cons (current-buffer) 'summary))))))
5451      gnus-newsrc-hashtb)
5452     (set-buffer gnus-group-buffer)
5453     (or (gnus-check-server method)
5454         (error "Unable to contact server: %s" (gnus-status-message method)))
5455     (if activate (or (gnus-request-group group)
5456                      (error "Couldn't request group")))
5457     (condition-case ()
5458         (gnus-group-read-group t t group)
5459       (error nil)
5460       (quit nil))))
5461
5462 (defun gnus-group-jump-to-group (group)
5463   "Jump to newsgroup GROUP."
5464   (interactive
5465    (list (completing-read
5466           "Group: " gnus-active-hashtb nil
5467           (gnus-read-active-file-p)
5468           nil
5469           'gnus-group-history)))
5470
5471   (when (equal group "")
5472     (error "Empty group name"))
5473
5474   (when (string-match "[\000-\032]" group)
5475     (error "Control characters in group: %s" group))
5476
5477   (let ((b (text-property-any
5478             (point-min) (point-max)
5479             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5480     (unless (gnus-ephemeral-group-p group)
5481       (if b
5482           ;; Either go to the line in the group buffer...
5483           (goto-char b)
5484         ;; ... or insert the line.
5485         (or
5486          (gnus-active group)
5487          (gnus-activate-group group)
5488          (error "%s error: %s" group (gnus-status-message group)))
5489
5490         (gnus-group-update-group group)
5491         (goto-char (text-property-any
5492                     (point-min) (point-max)
5493                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5494     ;; Adjust cursor point.
5495     (gnus-group-position-point)))
5496
5497 (defun gnus-group-goto-group (group)
5498   "Goto to newsgroup GROUP."
5499   (when group
5500     (let ((b (text-property-any (point-min) (point-max)
5501                                 'gnus-group (gnus-intern-safe
5502                                              group gnus-active-hashtb))))
5503       (and b (goto-char b)))))
5504
5505 (defun gnus-group-next-group (n)
5506   "Go to next N'th newsgroup.
5507 If N is negative, search backward instead.
5508 Returns the difference between N and the number of skips actually
5509 done."
5510   (interactive "p")
5511   (gnus-group-next-unread-group n t))
5512
5513 (defun gnus-group-next-unread-group (n &optional all level)
5514   "Go to next N'th unread newsgroup.
5515 If N is negative, search backward instead.
5516 If ALL is non-nil, choose any newsgroup, unread or not.
5517 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5518 such group can be found, the next group with a level higher than
5519 LEVEL.
5520 Returns the difference between N and the number of skips actually
5521 made."
5522   (interactive "p")
5523   (let ((backward (< n 0))
5524         (n (abs n)))
5525     (while (and (> n 0)
5526                 (gnus-group-search-forward
5527                  backward (or (not gnus-group-goto-unread) all) level))
5528       (setq n (1- n)))
5529     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5530                                (if level " on this level or higher" "")))
5531     n))
5532
5533 (defun gnus-group-prev-group (n)
5534   "Go to previous N'th newsgroup.
5535 Returns the difference between N and the number of skips actually
5536 done."
5537   (interactive "p")
5538   (gnus-group-next-unread-group (- n) t))
5539
5540 (defun gnus-group-prev-unread-group (n)
5541   "Go to previous N'th unread newsgroup.
5542 Returns the difference between N and the number of skips actually
5543 done."
5544   (interactive "p")
5545   (gnus-group-next-unread-group (- n)))
5546
5547 (defun gnus-group-next-unread-group-same-level (n)
5548   "Go to next N'th unread newsgroup on the same level.
5549 If N is negative, search backward instead.
5550 Returns the difference between N and the number of skips actually
5551 done."
5552   (interactive "p")
5553   (gnus-group-next-unread-group n t (gnus-group-group-level))
5554   (gnus-group-position-point))
5555
5556 (defun gnus-group-prev-unread-group-same-level (n)
5557   "Go to next N'th unread newsgroup on the same level.
5558 Returns the difference between N and the number of skips actually
5559 done."
5560   (interactive "p")
5561   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5562   (gnus-group-position-point))
5563
5564 (defun gnus-group-best-unread-group (&optional exclude-group)
5565   "Go to the group with the highest level.
5566 If EXCLUDE-GROUP, do not go to that group."
5567   (interactive)
5568   (goto-char (point-min))
5569   (let ((best 100000)
5570         unread best-point)
5571     (while (not (eobp))
5572       (setq unread (get-text-property (point) 'gnus-unread))
5573       (if (and (numberp unread) (> unread 0))
5574           (progn
5575             (if (and (get-text-property (point) 'gnus-level)
5576                      (< (get-text-property (point) 'gnus-level) best)
5577                      (or (not exclude-group)
5578                          (not (equal exclude-group (gnus-group-group-name)))))
5579                 (progn
5580                   (setq best (get-text-property (point) 'gnus-level))
5581                   (setq best-point (point))))))
5582       (forward-line 1))
5583     (if best-point (goto-char best-point))
5584     (gnus-summary-position-point)
5585     (and best-point (gnus-group-group-name))))
5586
5587 (defun gnus-group-first-unread-group ()
5588   "Go to the first group with unread articles."
5589   (interactive)
5590   (prog1
5591       (let ((opoint (point))
5592             unread)
5593         (goto-char (point-min))
5594         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5595                 (and (numberp unread)   ; Not a topic.
5596                      (not (zerop unread))) ; Has unread articles.
5597                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5598             (point)                     ; Success.
5599           (goto-char opoint)
5600           nil))                         ; Not success.
5601     (gnus-group-position-point)))
5602
5603 (defun gnus-group-enter-server-mode ()
5604   "Jump to the server buffer."
5605   (interactive)
5606   (gnus-enter-server-buffer))
5607
5608 (defun gnus-group-make-group (name &optional method address)
5609   "Add a new newsgroup.
5610 The user will be prompted for a NAME, for a select METHOD, and an
5611 ADDRESS."
5612   (interactive
5613    (cons
5614     (read-string "Group name: ")
5615     (let ((method
5616            (completing-read
5617             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5618             nil t nil 'gnus-method-history)))
5619       (cond ((assoc method gnus-valid-select-methods)
5620              (list method
5621                    (if (memq 'prompt-address
5622                              (assoc method gnus-valid-select-methods))
5623                        (read-string "Address: ")
5624                      "")))
5625             ((assoc method gnus-server-alist)
5626              (list method))
5627             (t
5628              (list method ""))))))
5629
5630   (let* ((meth (and method (if address (list (intern method) address)
5631                              method)))
5632          (nname (if method (gnus-group-prefixed-name name meth) name))
5633          backend info)
5634     (when (gnus-gethash nname gnus-newsrc-hashtb)
5635       (error "Group %s already exists" nname))
5636     ;; Subscribe to the new group.
5637     (gnus-group-change-level
5638      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5639      gnus-level-default-subscribed gnus-level-killed
5640      (and (gnus-group-group-name)
5641           (gnus-gethash (gnus-group-group-name)
5642                         gnus-newsrc-hashtb))
5643      t)
5644     ;; Make it active.
5645     (gnus-set-active nname (cons 1 0))
5646     (or (gnus-ephemeral-group-p name)
5647         (gnus-dribble-enter
5648          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5649     ;; Insert the line.
5650     (gnus-group-insert-group-line-info nname)
5651     (forward-line -1)
5652     (gnus-group-position-point)
5653
5654     ;; Load the backend and try to make the backend create
5655     ;; the group as well.
5656     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5657                                                   nil meth))))
5658                  gnus-valid-select-methods)
5659       (require backend))
5660     (gnus-check-server meth)
5661     (and (gnus-check-backend-function 'request-create-group nname)
5662          (gnus-request-create-group nname))
5663     t))
5664
5665 (defun gnus-group-delete-group (group &optional force)
5666   "Delete the current group.  Only meaningful with mail groups.
5667 If FORCE (the prefix) is non-nil, all the articles in the group will
5668 be deleted.  This is \"deleted\" as in \"removed forever from the face
5669 of the Earth\".  There is no undo.  The user will be prompted before
5670 doing the deletion."
5671   (interactive
5672    (list (gnus-group-group-name)
5673          current-prefix-arg))
5674   (or group (error "No group to rename"))
5675   (or (gnus-check-backend-function 'request-delete-group group)
5676       (error "This backend does not support group deletion"))
5677   (prog1
5678       (if (not (gnus-yes-or-no-p
5679                 (format
5680                  "Do you really want to delete %s%s? "
5681                  group (if force " and all its contents" ""))))
5682           () ; Whew!
5683         (gnus-message 6 "Deleting group %s..." group)
5684         (if (not (gnus-request-delete-group group force))
5685             (gnus-error 3 "Couldn't delete group %s" group)
5686           (gnus-message 6 "Deleting group %s...done" group)
5687           (gnus-group-goto-group group)
5688           (gnus-group-kill-group 1 t)
5689           (gnus-sethash group nil gnus-active-hashtb)
5690           t))
5691     (gnus-group-position-point)))
5692
5693 (defun gnus-group-rename-group (group new-name)
5694   (interactive
5695    (list
5696     (gnus-group-group-name)
5697     (progn
5698       (or (gnus-check-backend-function
5699            'request-rename-group (gnus-group-group-name))
5700           (error "This backend does not support renaming groups"))
5701       (read-string "New group name: "))))
5702
5703   (or (gnus-check-backend-function 'request-rename-group group)
5704       (error "This backend does not support renaming groups"))
5705
5706   (or group (error "No group to rename"))
5707   (and (string-match "^[ \t]*$" new-name)
5708        (error "Not a valid group name"))
5709
5710   ;; We find the proper prefixed name.
5711   (setq new-name
5712         (gnus-group-prefixed-name
5713          (gnus-group-real-name new-name)
5714          (gnus-info-method (gnus-get-info group))))
5715
5716   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5717   (prog1
5718       (if (not (gnus-request-rename-group group new-name))
5719           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5720         ;; We rename the group internally by killing it...
5721         (gnus-group-goto-group group)
5722         (gnus-group-kill-group)
5723         ;; ... changing its name ...
5724         (setcar (cdar gnus-list-of-killed-groups) new-name)
5725         ;; ... and then yanking it.  Magic!
5726         (gnus-group-yank-group)
5727         (gnus-set-active new-name (gnus-active group))
5728         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5729         new-name)
5730     (gnus-group-position-point)))
5731
5732 (defun gnus-group-edit-group (group &optional part)
5733   "Edit the group on the current line."
5734   (interactive (list (gnus-group-group-name)))
5735   (let* ((part (or part 'info))
5736          (done-func `(lambda ()
5737                        "Exit editing mode and update the information."
5738                        (interactive)
5739                        (gnus-group-edit-group-done ',part ,group)))
5740          (winconf (current-window-configuration))
5741          info)
5742     (or group (error "No group on current line"))
5743     (or (setq info (gnus-get-info group))
5744         (error "Killed group; can't be edited"))
5745     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5746     (gnus-configure-windows 'edit-group)
5747     (gnus-add-current-to-buffer-list)
5748     (emacs-lisp-mode)
5749     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5750     (use-local-map (copy-keymap emacs-lisp-mode-map))
5751     (local-set-key "\C-c\C-c" done-func)
5752     (make-local-variable 'gnus-prev-winconf)
5753     (setq gnus-prev-winconf winconf)
5754     (erase-buffer)
5755     (insert
5756      (cond
5757       ((eq part 'method)
5758        ";; Type `C-c C-c' after editing the select method.\n\n")
5759       ((eq part 'params)
5760        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5761       ((eq part 'info)
5762        ";; Type `C-c C-c' after editing the group info.\n\n")))
5763     (insert
5764      (pp-to-string
5765       (cond ((eq part 'method)
5766              (or (gnus-info-method info) "native"))
5767             ((eq part 'params)
5768              (gnus-info-params info))
5769             (t info)))
5770      "\n")))
5771
5772 (defun gnus-group-edit-group-method (group)
5773   "Edit the select method of GROUP."
5774   (interactive (list (gnus-group-group-name)))
5775   (gnus-group-edit-group group 'method))
5776
5777 (defun gnus-group-edit-group-parameters (group)
5778   "Edit the group parameters of GROUP."
5779   (interactive (list (gnus-group-group-name)))
5780   (gnus-group-edit-group group 'params))
5781
5782 (defun gnus-group-edit-group-done (part group)
5783   "Get info from buffer, update variables and jump to the group buffer."
5784   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5785   (goto-char (point-min))
5786   (let* ((form (read (current-buffer)))
5787          (winconf gnus-prev-winconf)
5788          (method (cond ((eq part 'info) (nth 4 form))
5789                        ((eq part 'method) form)
5790                        (t nil)))
5791          (info (cond ((eq part 'info) form)
5792                      ((eq part 'method) (gnus-get-info group))
5793                      (t nil)))
5794          (new-group (if info
5795                       (if (or (not method)
5796                               (gnus-server-equal
5797                                gnus-select-method method))
5798                           (gnus-group-real-name (car info))
5799                         (gnus-group-prefixed-name
5800                          (gnus-group-real-name (car info)) method))
5801                       nil)))
5802     (when (and new-group
5803                (not (equal new-group group)))
5804       (when (gnus-group-goto-group group)
5805         (gnus-group-kill-group 1))
5806       (gnus-activate-group new-group))
5807     ;; Set the info.
5808     (if (and info new-group)
5809         (progn
5810           (setq info (gnus-copy-sequence info))
5811           (setcar info new-group)
5812           (unless (gnus-server-equal method "native")
5813             (unless (nthcdr 3 info)
5814               (nconc info (list nil nil)))
5815             (unless (nthcdr 4 info)
5816               (nconc info (list nil)))
5817             (gnus-info-set-method info method))
5818           (gnus-group-set-info info))
5819       (gnus-group-set-info form (or new-group group) part))
5820     (kill-buffer (current-buffer))
5821     (and winconf (set-window-configuration winconf))
5822     (set-buffer gnus-group-buffer)
5823     (gnus-group-update-group (or new-group group))
5824     (gnus-group-position-point)))
5825
5826 (defun gnus-group-make-help-group ()
5827   "Create the Gnus documentation group."
5828   (interactive)
5829   (let ((path load-path)
5830         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5831         file dir)
5832     (and (gnus-gethash name gnus-newsrc-hashtb)
5833          (error "Documentation group already exists"))
5834     (while path
5835       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5836             file nil)
5837       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5838                 (file-exists-p
5839                  (setq file (concat (file-name-directory
5840                                      (directory-file-name dir))
5841                                     "etc/gnus-tut.txt"))))
5842         (setq path nil)))
5843     (if (not file)
5844         (gnus-message 1 "Couldn't find doc group")
5845       (gnus-group-make-group
5846        (gnus-group-real-name name)
5847        (list 'nndoc "gnus-help"
5848              (list 'nndoc-address file)
5849              (list 'nndoc-article-type 'mbox)))))
5850   (gnus-group-position-point))
5851
5852 (defun gnus-group-make-doc-group (file type)
5853   "Create a group that uses a single file as the source."
5854   (interactive
5855    (list (read-file-name "File name: ")
5856          (and current-prefix-arg 'ask)))
5857   (when (eq type 'ask)
5858     (let ((err "")
5859           char found)
5860       (while (not found)
5861         (message
5862          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5863          err)
5864         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5865                           ((= char ?b) 'babyl)
5866                           ((= char ?d) 'digest)
5867                           ((= char ?f) 'forward)
5868                           ((= char ?a) 'mmfd)
5869                           (t (setq err (format "%c unknown. " char))
5870                              nil))))
5871       (setq type found)))
5872   (let* ((file (expand-file-name file))
5873          (name (gnus-generate-new-group-name
5874                 (gnus-group-prefixed-name
5875                  (file-name-nondirectory file) '(nndoc "")))))
5876     (gnus-group-make-group
5877      (gnus-group-real-name name)
5878      (list 'nndoc (file-name-nondirectory file)
5879            (list 'nndoc-address file)
5880            (list 'nndoc-article-type (or type 'guess))))))
5881
5882 (defun gnus-group-make-archive-group (&optional all)
5883   "Create the (ding) Gnus archive group of the most recent articles.
5884 Given a prefix, create a full group."
5885   (interactive "P")
5886   (let ((group (gnus-group-prefixed-name
5887                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5888     (and (gnus-gethash group gnus-newsrc-hashtb)
5889          (error "Archive group already exists"))
5890     (gnus-group-make-group
5891      (gnus-group-real-name group)
5892      (list 'nndir (if all "hpc" "edu")
5893            (list 'nndir-directory
5894                  (if all gnus-group-archive-directory
5895                    gnus-group-recent-archive-directory))))))
5896
5897 (defun gnus-group-make-directory-group (dir)
5898   "Create an nndir group.
5899 The user will be prompted for a directory.  The contents of this
5900 directory will be used as a newsgroup.  The directory should contain
5901 mail messages or news articles in files that have numeric names."
5902   (interactive
5903    (list (read-file-name "Create group from directory: ")))
5904   (or (file-exists-p dir) (error "No such directory"))
5905   (or (file-directory-p dir) (error "Not a directory"))
5906   (let ((ext "")
5907         (i 0)
5908         group)
5909     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5910       (setq group
5911             (gnus-group-prefixed-name
5912              (concat (file-name-as-directory (directory-file-name dir))
5913                      ext)
5914              '(nndir "")))
5915       (setq ext (format "<%d>" (setq i (1+ i)))))
5916     (gnus-group-make-group
5917      (gnus-group-real-name group)
5918      (list 'nndir group (list 'nndir-directory dir)))))
5919
5920 (defun gnus-group-make-kiboze-group (group address scores)
5921   "Create an nnkiboze group.
5922 The user will be prompted for a name, a regexp to match groups, and
5923 score file entries for articles to include in the group."
5924   (interactive
5925    (list
5926     (read-string "nnkiboze group name: ")
5927     (read-string "Source groups (regexp): ")
5928     (let ((headers (mapcar (lambda (group) (list group))
5929                            '("subject" "from" "number" "date" "message-id"
5930                              "references" "chars" "lines" "xref"
5931                              "followup" "all" "body" "head")))
5932           scores header regexp regexps)
5933       (while (not (equal "" (setq header (completing-read
5934                                           "Match on header: " headers nil t))))
5935         (setq regexps nil)
5936         (while (not (equal "" (setq regexp (read-string
5937                                             (format "Match on %s (string): "
5938                                                     header)))))
5939           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5940         (setq scores (cons (cons header regexps) scores)))
5941       scores)))
5942   (gnus-group-make-group group "nnkiboze" address)
5943   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5944     (let (emacs-lisp-mode-hook)
5945       (pp scores (current-buffer)))))
5946
5947 (defun gnus-group-add-to-virtual (n vgroup)
5948   "Add the current group to a virtual group."
5949   (interactive
5950    (list current-prefix-arg
5951          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5952                           "nnvirtual:")))
5953   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5954       (error "%s is not an nnvirtual group" vgroup))
5955   (let* ((groups (gnus-group-process-prefix n))
5956          (method (gnus-info-method (gnus-get-info vgroup))))
5957     (setcar (cdr method)
5958             (concat
5959              (nth 1 method) "\\|"
5960              (mapconcat
5961               (lambda (s)
5962                 (gnus-group-remove-mark s)
5963                 (concat "\\(^" (regexp-quote s) "$\\)"))
5964               groups "\\|"))))
5965   (gnus-group-position-point))
5966
5967 (defun gnus-group-make-empty-virtual (group)
5968   "Create a new, fresh, empty virtual group."
5969   (interactive "sCreate new, empty virtual group: ")
5970   (let* ((method (list 'nnvirtual "^$"))
5971          (pgroup (gnus-group-prefixed-name group method)))
5972     ;; Check whether it exists already.
5973     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5974          (error "Group %s already exists." pgroup))
5975     ;; Subscribe the new group after the group on the current line.
5976     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5977     (gnus-group-update-group pgroup)
5978     (forward-line -1)
5979     (gnus-group-position-point)))
5980
5981 (defun gnus-group-enter-directory (dir)
5982   "Enter an ephemeral nneething group."
5983   (interactive "DDirectory to read: ")
5984   (let* ((method (list 'nneething dir))
5985          (leaf (gnus-group-prefixed-name
5986                 (file-name-nondirectory (directory-file-name dir))
5987                 method))
5988          (name (gnus-generate-new-group-name leaf)))
5989     (let ((nneething-read-only t))
5990       (or (gnus-group-read-ephemeral-group
5991            name method t
5992            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5993                                       'summary 'group)))
5994           (error "Couldn't enter %s" dir)))))
5995
5996 ;; Group sorting commands
5997 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5998
5999 (defun gnus-group-sort-groups (func &optional reverse)
6000   "Sort the group buffer according to FUNC.
6001 If REVERSE, reverse the sorting order."
6002   (interactive (list gnus-group-sort-function
6003                      current-prefix-arg))
6004   (let ((func (cond 
6005                ((not (listp func)) func)
6006                ((null func) func)
6007                ((= 1 (length func)) (car func))
6008                (t `(lambda (t1 t2)
6009                      ,(gnus-make-sort-function 
6010                        (reverse func)))))))
6011     ;; We peel off the dummy group from the alist.
6012     (when func
6013       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
6014         (pop gnus-newsrc-alist))
6015       ;; Do the sorting.
6016       (setq gnus-newsrc-alist
6017             (sort gnus-newsrc-alist func))
6018       (when reverse
6019         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
6020       ;; Regenerate the hash table.
6021       (gnus-make-hashtable-from-newsrc-alist)
6022       (gnus-group-list-groups))))
6023
6024 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
6025   "Sort the group buffer alphabetically by group name.
6026 If REVERSE, sort in reverse order."
6027   (interactive "P")
6028   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
6029
6030 (defun gnus-group-sort-groups-by-unread (&optional reverse)
6031   "Sort the group buffer by number of unread articles.
6032 If REVERSE, sort in reverse order."
6033   (interactive "P")
6034   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
6035
6036 (defun gnus-group-sort-groups-by-level (&optional reverse)
6037   "Sort the group buffer by group level.
6038 If REVERSE, sort in reverse order."
6039   (interactive "P")
6040   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
6041
6042 (defun gnus-group-sort-groups-by-score (&optional reverse)
6043   "Sort the group buffer by group score.
6044 If REVERSE, sort in reverse order."
6045   (interactive "P")
6046   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
6047
6048 (defun gnus-group-sort-groups-by-rank (&optional reverse)
6049   "Sort the group buffer by group rank.
6050 If REVERSE, sort in reverse order."
6051   (interactive "P")
6052   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
6053
6054 (defun gnus-group-sort-groups-by-method (&optional reverse)
6055   "Sort the group buffer alphabetically by backend name.
6056 If REVERSE, sort in reverse order."
6057   (interactive "P")
6058   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
6059
6060 (defun gnus-group-sort-by-alphabet (info1 info2)
6061   "Sort alphabetically."
6062   (string< (gnus-info-group info1) (gnus-info-group info2)))
6063
6064 (defun gnus-group-sort-by-unread (info1 info2)
6065   "Sort by number of unread articles."
6066   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
6067         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
6068     (< (or (and (numberp n1) n1) 0)
6069        (or (and (numberp n2) n2) 0))))
6070
6071 (defun gnus-group-sort-by-level (info1 info2)
6072   "Sort by level."
6073   (< (gnus-info-level info1) (gnus-info-level info2)))
6074
6075 (defun gnus-group-sort-by-method (info1 info2)
6076   "Sort alphabetically by backend name."
6077   (string< (symbol-name (car (gnus-find-method-for-group
6078                               (gnus-info-group info1) info1)))
6079            (symbol-name (car (gnus-find-method-for-group
6080                               (gnus-info-group info2) info2)))))
6081
6082 (defun gnus-group-sort-by-score (info1 info2)
6083   "Sort by group score."
6084   (< (gnus-info-score info1) (gnus-info-score info2)))
6085
6086 (defun gnus-group-sort-by-rank (info1 info2)
6087   "Sort by level and score."
6088   (let ((level1 (gnus-info-level info1))
6089         (level2 (gnus-info-level info2)))
6090     (or (< level1 level2)
6091         (and (= level1 level2)
6092              (> (gnus-info-score info1) (gnus-info-score info2))))))
6093
6094 ;; Group catching up.
6095
6096 (defun gnus-group-clear-data (n)
6097   "Clear all marks and read ranges from the current group."
6098   (interactive "P")
6099   (let ((groups (gnus-group-process-prefix n))
6100         group info)
6101     (while (setq group (pop groups))
6102       (setq info (gnus-get-info group))
6103       (gnus-info-set-read info nil)
6104       (when (gnus-info-marks info)
6105         (gnus-info-set-marks info nil))
6106       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6107       (when (gnus-group-goto-group group)
6108         (gnus-group-remove-mark group)
6109         (gnus-group-update-group-line)))))
6110
6111 (defun gnus-group-catchup-current (&optional n all)
6112   "Mark all articles not marked as unread in current newsgroup as read.
6113 If prefix argument N is numeric, the ARG next newsgroups will be
6114 caught up.  If ALL is non-nil, marked articles will also be marked as
6115 read.  Cross references (Xref: header) of articles are ignored.
6116 The difference between N and actual number of newsgroups that were
6117 caught up is returned."
6118   (interactive "P")
6119   (unless (gnus-group-group-name)
6120     (error "No group on the current line"))
6121   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6122                gnus-expert-user
6123                (gnus-y-or-n-p
6124                 (if all
6125                     "Do you really want to mark all articles as read? "
6126                   "Mark all unread articles as read? "))))
6127       n
6128     (let ((groups (gnus-group-process-prefix n))
6129           (ret 0))
6130       (while groups
6131         ;; Virtual groups have to be given special treatment.
6132         (let ((method (gnus-find-method-for-group (car groups))))
6133           (if (eq 'nnvirtual (car method))
6134               (nnvirtual-catchup-group
6135                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6136         (gnus-group-remove-mark (car groups))
6137         (if (>= (gnus-group-group-level) gnus-level-zombie)
6138             (gnus-message 2 "Dead groups can't be caught up")
6139           (if (prog1
6140                   (gnus-group-goto-group (car groups))
6141                 (gnus-group-catchup (car groups) all))
6142               (gnus-group-update-group-line)
6143             (setq ret (1+ ret))))
6144         (setq groups (cdr groups)))
6145       (gnus-group-next-unread-group 1)
6146       ret)))
6147
6148 (defun gnus-group-catchup-current-all (&optional n)
6149   "Mark all articles in current newsgroup as read.
6150 Cross references (Xref: header) of articles are ignored."
6151   (interactive "P")
6152   (gnus-group-catchup-current n 'all))
6153
6154 (defun gnus-group-catchup (group &optional all)
6155   "Mark all articles in GROUP as read.
6156 If ALL is non-nil, all articles are marked as read.
6157 The return value is the number of articles that were marked as read,
6158 or nil if no action could be taken."
6159   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6160          (num (car entry)))
6161     ;; Do the updating only if the newsgroup isn't killed.
6162     (if (not (numberp (car entry)))
6163         (gnus-message 1 "Can't catch up; non-active group")
6164       ;; Do auto-expirable marks if that's required.
6165       (when (gnus-group-auto-expirable-p group)
6166         (gnus-add-marked-articles
6167          group 'expire (gnus-list-of-unread-articles group))
6168         (when all
6169           (let ((marks (nth 3 (nth 2 entry))))
6170             (gnus-add-marked-articles
6171              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6172             (gnus-add-marked-articles
6173              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6174       (when entry
6175         (gnus-update-read-articles group nil)
6176         ;; Also nix out the lists of marks and dormants.
6177         (when all
6178           (gnus-add-marked-articles group 'tick nil nil 'force)
6179           (gnus-add-marked-articles group 'dormant nil nil 'force))
6180         (run-hooks 'gnus-group-catchup-group-hook)
6181         num))))
6182
6183 (defun gnus-group-expire-articles (&optional n)
6184   "Expire all expirable articles in the current newsgroup."
6185   (interactive "P")
6186   (let ((groups (gnus-group-process-prefix n))
6187         group)
6188     (unless groups
6189       (error "No groups to expire"))
6190     (while (setq group (pop groups))
6191       (gnus-group-remove-mark group)
6192       (when (gnus-check-backend-function 'request-expire-articles group)
6193         (gnus-message 6 "Expiring articles in %s..." group)
6194         (let* ((info (gnus-get-info group))
6195                (expirable (if (gnus-group-total-expirable-p group)
6196                               (cons nil (gnus-list-of-read-articles group))
6197                             (assq 'expire (gnus-info-marks info))))
6198                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6199           (when expirable
6200             (setcdr
6201              expirable
6202              (gnus-compress-sequence
6203               (if expiry-wait
6204                   ;; We set the expiry variables to the groupp
6205                   ;; parameter. 
6206                   (let ((nnmail-expiry-wait-function nil)
6207                         (nnmail-expiry-wait expiry-wait))
6208                     (gnus-request-expire-articles
6209                      (gnus-uncompress-sequence (cdr expirable)) group))
6210                 ;; Just expire using the normal expiry values.
6211                 (gnus-request-expire-articles
6212                  (gnus-uncompress-sequence (cdr expirable)) group))))
6213             (gnus-close-group group))
6214           (gnus-message 6 "Expiring articles in %s...done" group)))
6215       (gnus-group-position-point))))
6216
6217 (defun gnus-group-expire-all-groups ()
6218   "Expire all expirable articles in all newsgroups."
6219   (interactive)
6220   (save-excursion
6221     (gnus-message 5 "Expiring...")
6222     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6223                                      (cdr gnus-newsrc-alist))))
6224       (gnus-group-expire-articles nil)))
6225   (gnus-group-position-point)
6226   (gnus-message 5 "Expiring...done"))
6227
6228 (defun gnus-group-set-current-level (n level)
6229   "Set the level of the next N groups to LEVEL."
6230   (interactive
6231    (list
6232     current-prefix-arg
6233     (string-to-int
6234      (let ((s (read-string
6235                (format "Level (default %s): "
6236                        (or (gnus-group-group-level) 
6237                            gnus-level-default-subscribed)))))
6238        (if (string-match "^\\s-*$" s)
6239            (int-to-string (or (gnus-group-group-level) 
6240                               gnus-level-default-subscribed))
6241          s)))))
6242   (or (and (>= level 1) (<= level gnus-level-killed))
6243       (error "Illegal level: %d" level))
6244   (let ((groups (gnus-group-process-prefix n))
6245         group)
6246     (while (setq group (pop groups))
6247       (gnus-group-remove-mark group)
6248       (gnus-message 6 "Changed level of %s from %d to %d"
6249                     group (or (gnus-group-group-level) gnus-level-killed)
6250                     level)
6251       (gnus-group-change-level
6252        group level (or (gnus-group-group-level) gnus-level-killed))
6253       (gnus-group-update-group-line)))
6254   (gnus-group-position-point))
6255
6256 (defun gnus-group-unsubscribe-current-group (&optional n)
6257   "Toggle subscription of the current group.
6258 If given numerical prefix, toggle the N next groups."
6259   (interactive "P")
6260   (let ((groups (gnus-group-process-prefix n))
6261         group)
6262     (while groups
6263       (setq group (car groups)
6264             groups (cdr groups))
6265       (gnus-group-remove-mark group)
6266       (gnus-group-unsubscribe-group
6267        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6268                  gnus-level-default-unsubscribed
6269                gnus-level-default-subscribed) t)
6270       (gnus-group-update-group-line))
6271     (gnus-group-next-group 1)))
6272
6273 (defun gnus-group-unsubscribe-group (group &optional level silent)
6274   "Toggle subscription to GROUP.
6275 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6276 group line."
6277   (interactive
6278    (list (completing-read
6279           "Group: " gnus-active-hashtb nil
6280           (gnus-read-active-file-p)
6281           nil 
6282           'gnus-group-history)))
6283   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6284     (cond
6285      ((string-match "^[ \t]$" group)
6286       (error "Empty group name"))
6287      (newsrc
6288       ;; Toggle subscription flag.
6289       (gnus-group-change-level
6290        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6291                                       gnus-level-subscribed)
6292                                   (1+ gnus-level-subscribed)
6293                                 gnus-level-default-subscribed)))
6294       (unless silent
6295         (gnus-group-update-group group)))
6296      ((and (stringp group)
6297            (or (not (gnus-read-active-file-p))
6298                (gnus-active group)))
6299       ;; Add new newsgroup.
6300       (gnus-group-change-level
6301        group
6302        (if level level gnus-level-default-subscribed)
6303        (or (and (member group gnus-zombie-list)
6304                 gnus-level-zombie)
6305            gnus-level-killed)
6306        (and (gnus-group-group-name)
6307             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6308       (unless silent
6309         (gnus-group-update-group group)))
6310      (t (error "No such newsgroup: %s" group)))
6311     (gnus-group-position-point)))
6312
6313 (defun gnus-group-transpose-groups (n)
6314   "Move the current newsgroup up N places.
6315 If given a negative prefix, move down instead.  The difference between
6316 N and the number of steps taken is returned."
6317   (interactive "p")
6318   (or (gnus-group-group-name)
6319       (error "No group on current line"))
6320   (gnus-group-kill-group 1)
6321   (prog1
6322       (forward-line (- n))
6323     (gnus-group-yank-group)
6324     (gnus-group-position-point)))
6325
6326 (defun gnus-group-kill-all-zombies ()
6327   "Kill all zombie newsgroups."
6328   (interactive)
6329   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6330   (setq gnus-zombie-list nil)
6331   (gnus-group-list-groups))
6332
6333 (defun gnus-group-kill-region (begin end)
6334   "Kill newsgroups in current region (excluding current point).
6335 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6336   (interactive "r")
6337   (let ((lines
6338          ;; Count lines.
6339          (save-excursion
6340            (count-lines
6341             (progn
6342               (goto-char begin)
6343               (beginning-of-line)
6344               (point))
6345             (progn
6346               (goto-char end)
6347               (beginning-of-line)
6348               (point))))))
6349     (goto-char begin)
6350     (beginning-of-line)                 ;Important when LINES < 1
6351     (gnus-group-kill-group lines)))
6352
6353 (defun gnus-group-kill-group (&optional n discard)
6354   "Kill the next N groups.
6355 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6356 However, only groups that were alive can be yanked; already killed
6357 groups or zombie groups can't be yanked.
6358 The return value is the name of the group that was killed, or a list
6359 of groups killed."
6360   (interactive "P")
6361   (let ((buffer-read-only nil)
6362         (groups (gnus-group-process-prefix n))
6363         group entry level out)
6364     (if (< (length groups) 10)
6365         ;; This is faster when there are few groups.
6366         (while groups
6367           (push (setq group (pop groups)) out)
6368           (gnus-group-remove-mark group)
6369           (setq level (gnus-group-group-level))
6370           (gnus-delete-line)
6371           (when (and (not discard)
6372                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6373             (push (cons (car entry) (nth 2 entry))
6374                   gnus-list-of-killed-groups))
6375           (gnus-group-change-level
6376            (if entry entry group) gnus-level-killed (if entry nil level)))
6377       ;; If there are lots and lots of groups to be killed, we use
6378       ;; this thing instead.
6379       (let (entry)
6380         (setq groups (nreverse groups))
6381         (while groups
6382           (gnus-group-remove-mark (setq group (pop groups)))
6383           (gnus-delete-line)
6384           (push group gnus-killed-list)
6385           (setq gnus-newsrc-alist
6386                 (delq (assoc group gnus-newsrc-alist)
6387                       gnus-newsrc-alist))
6388           (when gnus-group-change-level-function
6389             (funcall gnus-group-change-level-function group 9 3))
6390           (cond
6391            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6392             (push (cons (car entry) (nth 2 entry))
6393                   gnus-list-of-killed-groups)
6394             (setcdr (cdr entry) (cdddr entry)))
6395            ((member group gnus-zombie-list)
6396             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6397         (gnus-make-hashtable-from-newsrc-alist)))
6398
6399     (gnus-group-position-point)
6400     (if (< (length out) 2) (car out) (nreverse out))))
6401
6402 (defun gnus-group-yank-group (&optional arg)
6403   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6404 inserting it before the current newsgroup.  The numeric ARG specifies
6405 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6406 is returned, or (if several groups are yanked) a list of yanked groups
6407 is returned."
6408   (interactive "p")
6409   (setq arg (or arg 1))
6410   (let (info group prev out)
6411     (while (>= (decf arg) 0)
6412       (if (not (setq info (pop gnus-list-of-killed-groups)))
6413           (error "No more newsgroups to yank"))
6414       (push (setq group (nth 1 info)) out)
6415       ;; Find which newsgroup to insert this one before - search
6416       ;; backward until something suitable is found.  If there are no
6417       ;; other newsgroups in this buffer, just make this newsgroup the
6418       ;; first newsgroup.
6419       (setq prev (gnus-group-group-name))
6420       (gnus-group-change-level
6421        info (gnus-info-level (cdr info)) gnus-level-killed
6422        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6423        t)
6424       (gnus-group-insert-group-line-info group))
6425     (forward-line -1)
6426     (gnus-group-position-point)
6427     (if (< (length out) 2) (car out) (nreverse out))))
6428
6429 (defun gnus-group-kill-level (level)
6430   "Kill all groups that is on a certain LEVEL."
6431   (interactive "nKill all groups on level: ")
6432   (cond
6433    ((= level gnus-level-zombie)
6434     (setq gnus-killed-list
6435           (nconc gnus-zombie-list gnus-killed-list))
6436     (setq gnus-zombie-list nil))
6437    ((and (< level gnus-level-zombie)
6438          (> level 0)
6439          (or gnus-expert-user
6440              (gnus-yes-or-no-p
6441               (format
6442                "Do you really want to kill all groups on level %d? "
6443                level))))
6444     (let* ((prev gnus-newsrc-alist)
6445            (alist (cdr prev)))
6446       (while alist
6447         (if (= (gnus-info-level (car alist)) level)
6448             (setcdr prev (cdr alist))
6449           (setq prev alist))
6450         (setq alist (cdr alist)))
6451       (gnus-make-hashtable-from-newsrc-alist)
6452       (gnus-group-list-groups)))
6453    (t
6454     (error "Can't kill; illegal level: %d" level))))
6455
6456 (defun gnus-group-list-all-groups (&optional arg)
6457   "List all newsgroups with level ARG or lower.
6458 Default is gnus-level-unsubscribed, which lists all subscribed and most
6459 unsubscribed groups."
6460   (interactive "P")
6461   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6462
6463 ;; Redefine this to list ALL killed groups if prefix arg used.
6464 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6465 (defun gnus-group-list-killed (&optional arg)
6466   "List all killed newsgroups in the group buffer.
6467 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6468 entail asking the server for the groups."
6469   (interactive "P")
6470   ;; Find all possible killed newsgroups if arg.
6471   (when arg
6472     (gnus-get-killed-groups))
6473   (if (not gnus-killed-list)
6474       (gnus-message 6 "No killed groups")
6475     (let (gnus-group-list-mode)
6476       (funcall gnus-group-prepare-function
6477                gnus-level-killed t gnus-level-killed))
6478     (goto-char (point-min)))
6479   (gnus-group-position-point))
6480
6481 (defun gnus-group-list-zombies ()
6482   "List all zombie newsgroups in the group buffer."
6483   (interactive)
6484   (if (not gnus-zombie-list)
6485       (gnus-message 6 "No zombie groups")
6486     (let (gnus-group-list-mode)
6487       (funcall gnus-group-prepare-function
6488                gnus-level-zombie t gnus-level-zombie))
6489     (goto-char (point-min)))
6490   (gnus-group-position-point))
6491
6492 (defun gnus-group-list-active ()
6493   "List all groups that are available from the server(s)."
6494   (interactive)
6495   ;; First we make sure that we have really read the active file.
6496   (unless (gnus-read-active-file-p)
6497     (let ((gnus-read-active-file t))
6498       (gnus-read-active-file)))
6499   ;; Find all groups and sort them.
6500   (let ((groups
6501          (sort
6502           (let (list)
6503             (mapatoms
6504              (lambda (sym)
6505                (and (boundp sym)
6506                     (symbol-value sym)
6507                     (setq list (cons (symbol-name sym) list))))
6508              gnus-active-hashtb)
6509             list)
6510           'string<))
6511         (buffer-read-only nil))
6512     (erase-buffer)
6513     (while groups
6514       (gnus-group-insert-group-line-info (pop groups)))
6515     (goto-char (point-min))))
6516
6517 (defun gnus-activate-all-groups (level)
6518   "Activate absolutely all groups."
6519   (interactive (list 7))
6520   (let ((gnus-activate-level level)
6521         (gnus-activate-foreign-newsgroups level))
6522     (gnus-group-get-new-news)))
6523
6524 (defun gnus-group-get-new-news (&optional arg)
6525   "Get newly arrived articles.
6526 If ARG is a number, it specifies which levels you are interested in
6527 re-scanning.  If ARG is non-nil and not a number, this will force
6528 \"hard\" re-reading of the active files from all servers."
6529   (interactive "P")
6530   (run-hooks 'gnus-get-new-news-hook)
6531   ;; We might read in new NoCeM messages here.
6532   (when (and gnus-use-nocem 
6533              (null arg))
6534     (gnus-nocem-scan-groups))
6535   ;; If ARG is not a number, then we read the active file.
6536   (when (and arg (not (numberp arg)))
6537     (let ((gnus-read-active-file t))
6538       (gnus-read-active-file))
6539     (setq arg nil))
6540
6541   (setq arg (gnus-group-default-level arg t))
6542   (if (and gnus-read-active-file (not arg))
6543       (progn
6544         (gnus-read-active-file)
6545         (gnus-get-unread-articles arg))
6546     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6547       (gnus-get-unread-articles arg)))
6548   (run-hooks 'gnus-after-getting-new-news-hook)
6549   (gnus-group-list-groups))
6550
6551 (defun gnus-group-get-new-news-this-group (&optional n)
6552   "Check for newly arrived news in the current group (and the N-1 next groups).
6553 The difference between N and the number of newsgroup checked is returned.
6554 If N is negative, this group and the N-1 previous groups will be checked."
6555   (interactive "P")
6556   (let* ((groups (gnus-group-process-prefix n))
6557          (ret (if (numberp n) (- n (length groups)) 0))
6558          (beg (unless n (point)))
6559          group)
6560     (while (setq group (pop groups))
6561       (gnus-group-remove-mark group)
6562       (if (gnus-activate-group group 'scan)
6563           (progn
6564             (gnus-get-unread-articles-in-group
6565              (gnus-get-info group) (gnus-active group) t)
6566             (unless (gnus-virtual-group-p group)
6567               (gnus-close-group group))
6568             (gnus-group-update-group group))
6569         (if (eq (gnus-server-status (gnus-find-method-for-group group))
6570                 'denied)
6571             (gnus-error "Server denied access")
6572           (gnus-error 3 "%s error: %s" group (gnus-status-message group)))))
6573     (when beg (goto-char beg))
6574     (when gnus-goto-next-group-when-activating
6575       (gnus-group-next-unread-group 1 t))
6576     (gnus-summary-position-point)
6577     ret))
6578
6579 (defun gnus-group-fetch-faq (group &optional faq-dir)
6580   "Fetch the FAQ for the current group."
6581   (interactive
6582    (list
6583     (and (gnus-group-group-name)
6584          (gnus-group-real-name (gnus-group-group-name)))
6585     (cond (current-prefix-arg
6586            (completing-read
6587             "Faq dir: " (and (listp gnus-group-faq-directory)
6588                              (mapcar (lambda (file) (list file))
6589                                      gnus-group-faq-directory)))))))
6590   (or faq-dir
6591       (setq faq-dir (if (listp gnus-group-faq-directory)
6592                         (car gnus-group-faq-directory)
6593                       gnus-group-faq-directory)))
6594   (or group (error "No group name given"))
6595   (let ((file (concat (file-name-as-directory faq-dir)
6596                       (gnus-group-real-name group))))
6597     (if (not (file-exists-p file))
6598         (error "No such file: %s" file)
6599       (find-file file))))
6600
6601 (defun gnus-group-describe-group (force &optional group)
6602   "Display a description of the current newsgroup."
6603   (interactive (list current-prefix-arg (gnus-group-group-name)))
6604   (when (and force
6605              gnus-description-hashtb)
6606     (gnus-sethash group nil gnus-description-hashtb))
6607   (let ((method (gnus-find-method-for-group group))
6608         desc)
6609     (or group (error "No group name given"))
6610     (and (or (and gnus-description-hashtb
6611                   ;; We check whether this group's method has been
6612                   ;; queried for a description file.
6613                   (gnus-gethash
6614                    (gnus-group-prefixed-name "" method)
6615                    gnus-description-hashtb))
6616              (setq desc (gnus-group-get-description group))
6617              (gnus-read-descriptions-file method))
6618          (gnus-message 1
6619           (or desc (gnus-gethash group gnus-description-hashtb)
6620               "No description available")))))
6621
6622 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6623 (defun gnus-group-describe-all-groups (&optional force)
6624   "Pop up a buffer with descriptions of all newsgroups."
6625   (interactive "P")
6626   (and force (setq gnus-description-hashtb nil))
6627   (if (not (or gnus-description-hashtb
6628                (gnus-read-all-descriptions-files)))
6629       (error "Couldn't request descriptions file"))
6630   (let ((buffer-read-only nil)
6631         b)
6632     (erase-buffer)
6633     (mapatoms
6634      (lambda (group)
6635        (setq b (point))
6636        (insert (format "      *: %-20s %s\n" (symbol-name group)
6637                        (symbol-value group)))
6638        (gnus-add-text-properties
6639         b (1+ b) (list 'gnus-group group
6640                        'gnus-unread t 'gnus-marked nil
6641                        'gnus-level (1+ gnus-level-subscribed))))
6642      gnus-description-hashtb)
6643     (goto-char (point-min))
6644     (gnus-group-position-point)))
6645
6646 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6647 (defun gnus-group-apropos (regexp &optional search-description)
6648   "List all newsgroups that have names that match a regexp."
6649   (interactive "sGnus apropos (regexp): ")
6650   (let ((prev "")
6651         (obuf (current-buffer))
6652         groups des)
6653     ;; Go through all newsgroups that are known to Gnus.
6654     (mapatoms
6655      (lambda (group)
6656        (and (symbol-name group)
6657             (string-match regexp (symbol-name group))
6658             (setq groups (cons (symbol-name group) groups))))
6659      gnus-active-hashtb)
6660     ;; Also go through all descriptions that are known to Gnus.
6661     (when search-description
6662       (mapatoms
6663        (lambda (group)
6664          (and (string-match regexp (symbol-value group))
6665               (gnus-active (symbol-name group))
6666               (setq groups (cons (symbol-name group) groups))))
6667        gnus-description-hashtb))
6668     (if (not groups)
6669         (gnus-message 3 "No groups matched \"%s\"." regexp)
6670       ;; Print out all the groups.
6671       (save-excursion
6672         (pop-to-buffer "*Gnus Help*")
6673         (buffer-disable-undo (current-buffer))
6674         (erase-buffer)
6675         (setq groups (sort groups 'string<))
6676         (while groups
6677           ;; Groups may be entered twice into the list of groups.
6678           (if (not (string= (car groups) prev))
6679               (progn
6680                 (insert (setq prev (car groups)) "\n")
6681                 (if (and gnus-description-hashtb
6682                          (setq des (gnus-gethash (car groups)
6683                                                  gnus-description-hashtb)))
6684                     (insert "  " des "\n"))))
6685           (setq groups (cdr groups)))
6686         (goto-char (point-min))))
6687     (pop-to-buffer obuf)))
6688
6689 (defun gnus-group-description-apropos (regexp)
6690   "List all newsgroups that have names or descriptions that match a regexp."
6691   (interactive "sGnus description apropos (regexp): ")
6692   (if (not (or gnus-description-hashtb
6693                (gnus-read-all-descriptions-files)))
6694       (error "Couldn't request descriptions file"))
6695   (gnus-group-apropos regexp t))
6696
6697 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6698 (defun gnus-group-list-matching (level regexp &optional all lowest)
6699   "List all groups with unread articles that match REGEXP.
6700 If the prefix LEVEL is non-nil, it should be a number that says which
6701 level to cut off listing groups.
6702 If ALL, also list groups with no unread articles.
6703 If LOWEST, don't list groups with level lower than LOWEST.
6704
6705 This command may read the active file."
6706   (interactive "P\nsList newsgroups matching: ")
6707   ;; First make sure active file has been read.
6708   (when (and level
6709              (> (prefix-numeric-value level) gnus-level-killed))
6710     (gnus-get-killed-groups))
6711   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6712                            all (or lowest 1) regexp)
6713   (goto-char (point-min))
6714   (gnus-group-position-point))
6715
6716 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6717   "List all groups that match REGEXP.
6718 If the prefix LEVEL is non-nil, it should be a number that says which
6719 level to cut off listing groups.
6720 If LOWEST, don't list groups with level lower than LOWEST."
6721   (interactive "P\nsList newsgroups matching: ")
6722   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6723
6724 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6725 (defun gnus-group-save-newsrc (&optional force)
6726   "Save the Gnus startup files.
6727 If FORCE, force saving whether it is necessary or not."
6728   (interactive "P")
6729   (gnus-save-newsrc-file force))
6730
6731 (defun gnus-group-restart (&optional arg)
6732   "Force Gnus to read the .newsrc file."
6733   (interactive "P")
6734   (when (gnus-yes-or-no-p
6735          (format "Are you sure you want to read %s? "
6736                  gnus-current-startup-file))
6737     (gnus-save-newsrc-file)
6738     (gnus-setup-news 'force)
6739     (gnus-group-list-groups arg)))
6740
6741 (defun gnus-group-read-init-file ()
6742   "Read the Gnus elisp init file."
6743   (interactive)
6744   (gnus-read-init-file))
6745
6746 (defun gnus-group-check-bogus-groups (&optional silent)
6747   "Check bogus newsgroups.
6748 If given a prefix, don't ask for confirmation before removing a bogus
6749 group."
6750   (interactive "P")
6751   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6752   (gnus-group-list-groups))
6753
6754 (defun gnus-group-edit-global-kill (&optional article group)
6755   "Edit the global kill file.
6756 If GROUP, edit that local kill file instead."
6757   (interactive "P")
6758   (setq gnus-current-kill-article article)
6759   (gnus-kill-file-edit-file group)
6760   (gnus-message
6761    6
6762    (substitute-command-keys
6763     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6764             (if group "local" "global")))))
6765
6766 (defun gnus-group-edit-local-kill (article group)
6767   "Edit a local kill file."
6768   (interactive (list nil (gnus-group-group-name)))
6769   (gnus-group-edit-global-kill article group))
6770
6771 (defun gnus-group-force-update ()
6772   "Update `.newsrc' file."
6773   (interactive)
6774   (gnus-save-newsrc-file))
6775
6776 (defun gnus-group-suspend ()
6777   "Suspend the current Gnus session.
6778 In fact, cleanup buffers except for group mode buffer.
6779 The hook gnus-suspend-gnus-hook is called before actually suspending."
6780   (interactive)
6781   (run-hooks 'gnus-suspend-gnus-hook)
6782   ;; Kill Gnus buffers except for group mode buffer.
6783   (let* ((group-buf (get-buffer gnus-group-buffer))
6784          ;; Do this on a separate list in case the user does a ^G before we finish
6785          (gnus-buffer-list
6786           (delete group-buf (delete gnus-dribble-buffer
6787                                     (append gnus-buffer-list nil)))))
6788     (while gnus-buffer-list
6789       (gnus-kill-buffer (pop gnus-buffer-list)))
6790     (gnus-kill-gnus-frames)
6791     (when group-buf
6792       (setq gnus-buffer-list (list group-buf))
6793       (bury-buffer group-buf)
6794       (delete-windows-on group-buf t))))
6795
6796 (defun gnus-group-clear-dribble ()
6797   "Clear all information from the dribble buffer."
6798   (interactive)
6799   (gnus-dribble-clear)
6800   (gnus-message 7 "Cleared dribble buffer"))
6801
6802 (defun gnus-group-exit ()
6803   "Quit reading news after updating .newsrc.eld and .newsrc.
6804 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6805   (interactive)
6806   (when 
6807       (or noninteractive                ;For gnus-batch-kill
6808           (not gnus-interactive-exit)   ;Without confirmation
6809           gnus-expert-user
6810           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6811     (run-hooks 'gnus-exit-gnus-hook)
6812     ;; Offer to save data from non-quitted summary buffers.
6813     (gnus-offer-save-summaries)
6814     ;; Save the newsrc file(s).
6815     (gnus-save-newsrc-file)
6816     ;; Kill-em-all.
6817     (gnus-close-backends)
6818     ;; Reset everything.
6819     (gnus-clear-system)
6820     ;; Allow the user to do things after cleaning up.
6821     (run-hooks 'gnus-after-exiting-gnus-hook)))
6822
6823 (defun gnus-close-backends ()
6824   ;; Send a close request to all backends that support such a request.
6825   (let ((methods gnus-valid-select-methods)
6826         func)
6827     (while methods
6828       (if (fboundp (setq func (intern (concat (caar methods)
6829                                               "-request-close"))))
6830           (funcall func))
6831       (setq methods (cdr methods)))))
6832
6833 (defun gnus-group-quit ()
6834   "Quit reading news without updating .newsrc.eld or .newsrc.
6835 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6836   (interactive)
6837   (when (or noninteractive              ;For gnus-batch-kill
6838             (zerop (buffer-size))
6839             (not (gnus-server-opened gnus-select-method))
6840             gnus-expert-user
6841             (not gnus-current-startup-file)
6842             (gnus-yes-or-no-p
6843              (format "Quit reading news without saving %s? "
6844                      (file-name-nondirectory gnus-current-startup-file))))
6845     (run-hooks 'gnus-exit-gnus-hook)
6846     (if gnus-use-full-window
6847         (delete-other-windows)
6848       (gnus-remove-some-windows))
6849     (gnus-dribble-save)
6850     (gnus-close-backends)
6851     (gnus-clear-system)
6852     ;; Allow the user to do things after cleaning up.
6853     (run-hooks 'gnus-after-exiting-gnus-hook)))
6854
6855 (defun gnus-offer-save-summaries ()
6856   "Offer to save all active summary buffers."
6857   (save-excursion
6858     (let ((buflist (buffer-list))
6859           buffers bufname)
6860       ;; Go through all buffers and find all summaries.
6861       (while buflist
6862         (and (setq bufname (buffer-name (car buflist)))
6863              (string-match "Summary" bufname)
6864              (save-excursion
6865                (set-buffer bufname)
6866                ;; We check that this is, indeed, a summary buffer.
6867                (and (eq major-mode 'gnus-summary-mode)
6868                     ;; Also make sure this isn't bogus.
6869                     gnus-newsgroup-prepared))
6870              (push bufname buffers))
6871         (setq buflist (cdr buflist)))
6872       ;; Go through all these summary buffers and offer to save them.
6873       (when buffers
6874         (map-y-or-n-p
6875          "Update summary buffer %s? "
6876          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6877          buffers)))))
6878
6879 (defun gnus-group-describe-briefly ()
6880   "Give a one line description of the group mode commands."
6881   (interactive)
6882   (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")))
6883
6884 (defun gnus-group-browse-foreign-server (method)
6885   "Browse a foreign news server.
6886 If called interactively, this function will ask for a select method
6887  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6888 If not, METHOD should be a list where the first element is the method
6889 and the second element is the address."
6890   (interactive
6891    (list (let ((how (completing-read
6892                      "Which backend: "
6893                      (append gnus-valid-select-methods gnus-server-alist)
6894                      nil t (cons "nntp" 0) 'gnus-method-history)))
6895            ;; We either got a backend name or a virtual server name.
6896            ;; If the first, we also need an address.
6897            (if (assoc how gnus-valid-select-methods)
6898                (list (intern how)
6899                      ;; Suggested by mapjph@bath.ac.uk.
6900                      (completing-read
6901                       "Address: "
6902                       (mapcar (lambda (server) (list server))
6903                               gnus-secondary-servers)))
6904              ;; We got a server name, so we find the method.
6905              (gnus-server-to-method how)))))
6906   (gnus-browse-foreign-server method))
6907
6908 \f
6909 ;;;
6910 ;;; Gnus summary mode
6911 ;;;
6912
6913 (defvar gnus-summary-mode-map nil)
6914
6915 (put 'gnus-summary-mode 'mode-class 'special)
6916
6917 (unless gnus-summary-mode-map
6918   (setq gnus-summary-mode-map (make-keymap))
6919   (suppress-keymap gnus-summary-mode-map)
6920
6921   ;; Non-orthogonal keys
6922
6923   (gnus-define-keys gnus-summary-mode-map
6924     " " gnus-summary-next-page
6925     "\177" gnus-summary-prev-page
6926     [delete] gnus-summary-prev-page
6927     "\r" gnus-summary-scroll-up
6928     "n" gnus-summary-next-unread-article
6929     "p" gnus-summary-prev-unread-article
6930     "N" gnus-summary-next-article
6931     "P" gnus-summary-prev-article
6932     "\M-\C-n" gnus-summary-next-same-subject
6933     "\M-\C-p" gnus-summary-prev-same-subject
6934     "\M-n" gnus-summary-next-unread-subject
6935     "\M-p" gnus-summary-prev-unread-subject
6936     "." gnus-summary-first-unread-article
6937     "," gnus-summary-best-unread-article
6938     "\M-s" gnus-summary-search-article-forward
6939     "\M-r" gnus-summary-search-article-backward
6940     "<" gnus-summary-beginning-of-article
6941     ">" gnus-summary-end-of-article
6942     "j" gnus-summary-goto-article
6943     "^" gnus-summary-refer-parent-article
6944     "\M-^" gnus-summary-refer-article
6945     "u" gnus-summary-tick-article-forward
6946     "!" gnus-summary-tick-article-forward
6947     "U" gnus-summary-tick-article-backward
6948     "d" gnus-summary-mark-as-read-forward
6949     "D" gnus-summary-mark-as-read-backward
6950     "E" gnus-summary-mark-as-expirable
6951     "\M-u" gnus-summary-clear-mark-forward
6952     "\M-U" gnus-summary-clear-mark-backward
6953     "k" gnus-summary-kill-same-subject-and-select
6954     "\C-k" gnus-summary-kill-same-subject
6955     "\M-\C-k" gnus-summary-kill-thread
6956     "\M-\C-l" gnus-summary-lower-thread
6957     "e" gnus-summary-edit-article
6958     "#" gnus-summary-mark-as-processable
6959     "\M-#" gnus-summary-unmark-as-processable
6960     "\M-\C-t" gnus-summary-toggle-threads
6961     "\M-\C-s" gnus-summary-show-thread
6962     "\M-\C-h" gnus-summary-hide-thread
6963     "\M-\C-f" gnus-summary-next-thread
6964     "\M-\C-b" gnus-summary-prev-thread
6965     "\M-\C-u" gnus-summary-up-thread
6966     "\M-\C-d" gnus-summary-down-thread
6967     "&" gnus-summary-execute-command
6968     "c" gnus-summary-catchup-and-exit
6969     "\C-w" gnus-summary-mark-region-as-read
6970     "\C-t" gnus-summary-toggle-truncation
6971     "?" gnus-summary-mark-as-dormant
6972     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6973     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6974     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6975     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6976     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6977     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6978     "=" gnus-summary-expand-window
6979     "\C-x\C-s" gnus-summary-reselect-current-group
6980     "\M-g" gnus-summary-rescan-group
6981     "w" gnus-summary-stop-page-breaking
6982     "\C-c\C-r" gnus-summary-caesar-message
6983     "\M-t" gnus-summary-toggle-mime
6984     "f" gnus-summary-followup
6985     "F" gnus-summary-followup-with-original
6986     "C" gnus-summary-cancel-article
6987     "r" gnus-summary-reply
6988     "R" gnus-summary-reply-with-original
6989     "\C-c\C-f" gnus-summary-mail-forward
6990     "o" gnus-summary-save-article
6991     "\C-o" gnus-summary-save-article-mail
6992     "|" gnus-summary-pipe-output
6993     "\M-k" gnus-summary-edit-local-kill
6994     "\M-K" gnus-summary-edit-global-kill
6995     "V" gnus-version
6996     "\C-c\C-d" gnus-summary-describe-group
6997     "q" gnus-summary-exit
6998     "Q" gnus-summary-exit-no-update
6999     "\C-c\C-i" gnus-info-find-node
7000     gnus-mouse-2 gnus-mouse-pick-article
7001     "m" gnus-summary-mail-other-window
7002     "a" gnus-summary-post-news
7003     "x" gnus-summary-limit-to-unread
7004     "s" gnus-summary-isearch-article
7005     "t" gnus-article-hide-headers
7006     "g" gnus-summary-show-article
7007     "l" gnus-summary-goto-last-article
7008     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
7009     "\C-d" gnus-summary-enter-digest-group
7010     "\C-c\C-b" gnus-bug
7011     "*" gnus-cache-enter-article
7012     "\M-*" gnus-cache-remove-article
7013     "\M-&" gnus-summary-universal-argument
7014     "\C-l" gnus-recenter
7015     "I" gnus-summary-increase-score
7016     "L" gnus-summary-lower-score
7017
7018     "V" gnus-summary-score-map
7019     "X" gnus-uu-extract-map
7020     "S" gnus-summary-send-map)
7021
7022   ;; Sort of orthogonal keymap
7023   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
7024     "t" gnus-summary-tick-article-forward
7025     "!" gnus-summary-tick-article-forward
7026     "d" gnus-summary-mark-as-read-forward
7027     "r" gnus-summary-mark-as-read-forward
7028     "c" gnus-summary-clear-mark-forward
7029     " " gnus-summary-clear-mark-forward
7030     "e" gnus-summary-mark-as-expirable
7031     "x" gnus-summary-mark-as-expirable
7032     "?" gnus-summary-mark-as-dormant
7033     "b" gnus-summary-set-bookmark
7034     "B" gnus-summary-remove-bookmark
7035     "#" gnus-summary-mark-as-processable
7036     "\M-#" gnus-summary-unmark-as-processable
7037     "S" gnus-summary-limit-include-expunged
7038     "C" gnus-summary-catchup
7039     "H" gnus-summary-catchup-to-here
7040     "\C-c" gnus-summary-catchup-all
7041     "k" gnus-summary-kill-same-subject-and-select
7042     "K" gnus-summary-kill-same-subject
7043     "P" gnus-uu-mark-map)
7044
7045   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
7046     "c" gnus-summary-clear-above
7047     "u" gnus-summary-tick-above
7048     "m" gnus-summary-mark-above
7049     "k" gnus-summary-kill-below)
7050
7051   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
7052     "/" gnus-summary-limit-to-subject
7053     "n" gnus-summary-limit-to-articles
7054     "w" gnus-summary-pop-limit
7055     "s" gnus-summary-limit-to-subject
7056     "a" gnus-summary-limit-to-author
7057     "u" gnus-summary-limit-to-unread
7058     "m" gnus-summary-limit-to-marks
7059     "v" gnus-summary-limit-to-score
7060     "D" gnus-summary-limit-include-dormant
7061     "d" gnus-summary-limit-exclude-dormant
7062     ;;  "t" gnus-summary-limit-exclude-thread
7063     "E" gnus-summary-limit-include-expunged
7064     "c" gnus-summary-limit-exclude-childless-dormant
7065     "C" gnus-summary-limit-mark-excluded-as-read)
7066
7067   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
7068     "n" gnus-summary-next-unread-article
7069     "p" gnus-summary-prev-unread-article
7070     "N" gnus-summary-next-article
7071     "P" gnus-summary-prev-article
7072     "\C-n" gnus-summary-next-same-subject
7073     "\C-p" gnus-summary-prev-same-subject
7074     "\M-n" gnus-summary-next-unread-subject
7075     "\M-p" gnus-summary-prev-unread-subject
7076     "f" gnus-summary-first-unread-article
7077     "b" gnus-summary-best-unread-article
7078     "j" gnus-summary-goto-article
7079     "g" gnus-summary-goto-subject
7080     "l" gnus-summary-goto-last-article
7081     "p" gnus-summary-pop-article)
7082
7083   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
7084     "k" gnus-summary-kill-thread
7085     "l" gnus-summary-lower-thread
7086     "i" gnus-summary-raise-thread
7087     "T" gnus-summary-toggle-threads
7088     "t" gnus-summary-rethread-current
7089     "^" gnus-summary-reparent-thread
7090     "s" gnus-summary-show-thread
7091     "S" gnus-summary-show-all-threads
7092     "h" gnus-summary-hide-thread
7093     "H" gnus-summary-hide-all-threads
7094     "n" gnus-summary-next-thread
7095     "p" gnus-summary-prev-thread
7096     "u" gnus-summary-up-thread
7097     "o" gnus-summary-top-thread
7098     "d" gnus-summary-down-thread
7099     "#" gnus-uu-mark-thread
7100     "\M-#" gnus-uu-unmark-thread)
7101
7102   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
7103     "c" gnus-summary-catchup-and-exit
7104     "C" gnus-summary-catchup-all-and-exit
7105     "E" gnus-summary-exit-no-update
7106     "Q" gnus-summary-exit
7107     "Z" gnus-summary-exit
7108     "n" gnus-summary-catchup-and-goto-next-group
7109     "R" gnus-summary-reselect-current-group
7110     "G" gnus-summary-rescan-group
7111     "N" gnus-summary-next-group
7112     "P" gnus-summary-prev-group)
7113
7114   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7115     " " gnus-summary-next-page
7116     "n" gnus-summary-next-page
7117     "\177" gnus-summary-prev-page
7118     [delete] gnus-summary-prev-page
7119     "p" gnus-summary-prev-page
7120     "\r" gnus-summary-scroll-up
7121     "<" gnus-summary-beginning-of-article
7122     ">" gnus-summary-end-of-article
7123     "b" gnus-summary-beginning-of-article
7124     "e" gnus-summary-end-of-article
7125     "^" gnus-summary-refer-parent-article
7126     "r" gnus-summary-refer-parent-article
7127     "R" gnus-summary-refer-references
7128     "g" gnus-summary-show-article
7129     "s" gnus-summary-isearch-article)
7130
7131   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7132     "b" gnus-article-add-buttons
7133     "B" gnus-article-add-buttons-to-head
7134     "o" gnus-article-treat-overstrike
7135     ;;  "w" gnus-article-word-wrap
7136     "w" gnus-article-fill-cited-article
7137     "c" gnus-article-remove-cr
7138     "L" gnus-article-remove-trailing-blank-lines
7139     "q" gnus-article-de-quoted-unreadable
7140     "f" gnus-article-display-x-face
7141     "l" gnus-summary-stop-page-breaking
7142     "r" gnus-summary-caesar-message
7143     "t" gnus-article-hide-headers
7144     "v" gnus-summary-verbose-headers
7145     "m" gnus-summary-toggle-mime)
7146
7147   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7148     "a" gnus-article-hide
7149     "h" gnus-article-hide-headers
7150     "b" gnus-article-hide-boring-headers
7151     "s" gnus-article-hide-signature
7152     "c" gnus-article-hide-citation
7153     "p" gnus-article-hide-pgp
7154     "\C-c" gnus-article-hide-citation-maybe)
7155
7156   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7157     "a" gnus-article-highlight
7158     "h" gnus-article-highlight-headers
7159     "c" gnus-article-highlight-citation
7160     "s" gnus-article-highlight-signature)
7161
7162   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7163     "z" gnus-article-date-ut
7164     "u" gnus-article-date-ut
7165     "l" gnus-article-date-local
7166     "e" gnus-article-date-lapsed
7167     "o" gnus-article-date-original)
7168
7169   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7170     "v" gnus-version
7171     "f" gnus-summary-fetch-faq
7172     "d" gnus-summary-describe-group
7173     "h" gnus-summary-describe-briefly
7174     "i" gnus-info-find-node)
7175
7176   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7177     "e" gnus-summary-expire-articles
7178     "\M-\C-e" gnus-summary-expire-articles-now
7179     "\177" gnus-summary-delete-article
7180     [delete] gnus-summary-delete-article
7181     "m" gnus-summary-move-article
7182     "r" gnus-summary-respool-article
7183     "w" gnus-summary-edit-article
7184     "c" gnus-summary-copy-article
7185     "B" gnus-summary-crosspost-article
7186     "q" gnus-summary-respool-query
7187     "i" gnus-summary-import-article)
7188
7189   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7190     "o" gnus-summary-save-article
7191     "m" gnus-summary-save-article-mail
7192     "r" gnus-summary-save-article-rmail
7193     "f" gnus-summary-save-article-file
7194     "b" gnus-summary-save-article-body-file
7195     "h" gnus-summary-save-article-folder
7196     "v" gnus-summary-save-article-vm
7197     "p" gnus-summary-pipe-output
7198     "s" gnus-soup-add-article)
7199   )
7200
7201 \f
7202
7203 (defun gnus-summary-mode (&optional group)
7204   "Major mode for reading articles.
7205
7206 All normal editing commands are switched off.
7207 \\<gnus-summary-mode-map>
7208 Each line in this buffer represents one article.  To read an
7209 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7210 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7211 respectively.
7212
7213 You can also post articles and send mail from this buffer.  To
7214 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7215 of an article, type `\\[gnus-summary-reply]'.
7216
7217 There are approx. one gazillion commands you can execute in this
7218 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7219
7220 The following commands are available:
7221
7222 \\{gnus-summary-mode-map}"
7223   (interactive)
7224   (when (and menu-bar-mode
7225              (gnus-visual-p 'summary-menu 'menu))
7226     (gnus-summary-make-menu-bar))
7227   (kill-all-local-variables)
7228   (gnus-summary-make-local-variables)
7229   (gnus-make-thread-indent-array)
7230   (gnus-simplify-mode-line)
7231   (setq major-mode 'gnus-summary-mode)
7232   (setq mode-name "Summary")
7233   (make-local-variable 'minor-mode-alist)
7234   (use-local-map gnus-summary-mode-map)
7235   (buffer-disable-undo (current-buffer))
7236   (setq buffer-read-only t)             ;Disable modification
7237   (setq truncate-lines t)
7238   (setq selective-display t)
7239   (setq selective-display-ellipses t)   ;Display `...'
7240   (setq buffer-display-table gnus-summary-display-table)
7241   (setq gnus-newsgroup-name group)
7242   (make-local-variable 'gnus-summary-line-format)
7243   (make-local-variable 'gnus-summary-line-format-spec)
7244   (make-local-variable 'gnus-summary-mark-positions)
7245   (run-hooks 'gnus-summary-mode-hook))
7246
7247 (defun gnus-summary-make-local-variables ()
7248   "Make all the local summary buffer variables."
7249   (let ((locals gnus-summary-local-variables)
7250         global local)
7251     (while (setq local (pop locals))
7252       (if (consp local)
7253           (progn
7254             (if (eq (cdr local) 'global)
7255                 ;; Copy the global value of the variable.
7256                 (setq global (symbol-value (car local)))
7257               ;; Use the value from the list.
7258               (setq global (eval (cdr local))))
7259             (make-local-variable (car local))
7260             (set (car local) global))
7261         ;; Simple nil-valued local variable.
7262         (make-local-variable local)
7263         (set local nil)))))
7264
7265 (defun gnus-summary-make-display-table ()
7266   ;; Change the display table.  Odd characters have a tendency to mess
7267   ;; up nicely formatted displays - we make all possible glyphs
7268   ;; display only a single character.
7269
7270   ;; We start from the standard display table, if any.
7271   (setq gnus-summary-display-table
7272         (or (copy-sequence standard-display-table)
7273             (make-display-table)))
7274   ;; Nix out all the control chars...
7275   (let ((i 32))
7276     (while (>= (setq i (1- i)) 0)
7277       (aset gnus-summary-display-table i [??])))
7278   ;; ... but not newline and cr, of course. (cr is necessary for the
7279   ;; selective display).
7280   (aset gnus-summary-display-table ?\n nil)
7281   (aset gnus-summary-display-table ?\r nil)
7282   ;; We nix out any glyphs over 126 that are not set already.
7283   (let ((i 256))
7284     (while (>= (setq i (1- i)) 127)
7285       ;; Only modify if the entry is nil.
7286       (or (aref gnus-summary-display-table i)
7287           (aset gnus-summary-display-table i [??])))))
7288
7289 (defun gnus-summary-clear-local-variables ()
7290   (let ((locals gnus-summary-local-variables))
7291     (while locals
7292       (if (consp (car locals))
7293           (and (vectorp (caar locals))
7294                (set (caar locals) nil))
7295         (and (vectorp (car locals))
7296              (set (car locals) nil)))
7297       (setq locals (cdr locals)))))
7298
7299 ;; Summary data functions.
7300
7301 (defmacro gnus-data-number (data)
7302   `(car ,data))
7303
7304 (defmacro gnus-data-set-number (data number)
7305   `(setcar ,data ,number))
7306
7307 (defmacro gnus-data-mark (data)
7308   `(nth 1 ,data))
7309
7310 (defmacro gnus-data-set-mark (data mark)
7311   `(setcar (nthcdr 1 ,data) ,mark))
7312
7313 (defmacro gnus-data-pos (data)
7314   `(nth 2 ,data))
7315
7316 (defmacro gnus-data-set-pos (data pos)
7317   `(setcar (nthcdr 2 ,data) ,pos))
7318
7319 (defmacro gnus-data-header (data)
7320   `(nth 3 ,data))
7321
7322 (defmacro gnus-data-level (data)
7323   `(nth 4 ,data))
7324
7325 (defmacro gnus-data-unread-p (data)
7326   `(= (nth 1 ,data) gnus-unread-mark))
7327
7328 (defmacro gnus-data-pseudo-p (data)
7329   `(consp (nth 3 ,data)))
7330
7331 (defmacro gnus-data-find (number)
7332   `(assq ,number gnus-newsgroup-data))
7333
7334 (defmacro gnus-data-find-list (number &optional data)
7335   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7336      (memq (assq ,number bdata)
7337            bdata)))
7338
7339 (defmacro gnus-data-make (number mark pos header level)
7340   `(list ,number ,mark ,pos ,header ,level))
7341
7342 (defun gnus-data-enter (after-article number mark pos header level offset)
7343   (let ((data (gnus-data-find-list after-article)))
7344     (or data (error "No such article: %d" after-article))
7345     (setcdr data (cons (gnus-data-make number mark pos header level)
7346                        (cdr data)))
7347     (setq gnus-newsgroup-data-reverse nil)
7348     (gnus-data-update-list (cddr data) offset)))
7349
7350 (defun gnus-data-enter-list (after-article list &optional offset)
7351   (when list
7352     (let ((data (and after-article (gnus-data-find-list after-article)))
7353           (ilist list))
7354       (or data (not after-article) (error "No such article: %d" after-article))
7355       ;; Find the last element in the list to be spliced into the main
7356       ;; list.
7357       (while (cdr list)
7358         (setq list (cdr list)))
7359       (if (not data)
7360           (progn
7361             (setcdr list gnus-newsgroup-data)
7362             (setq gnus-newsgroup-data ilist)
7363             (and offset (gnus-data-update-list (cdr list) offset)))
7364         (setcdr list (cdr data))
7365         (setcdr data ilist)
7366         (and offset (gnus-data-update-list (cdr data) offset)))
7367       (setq gnus-newsgroup-data-reverse nil))))
7368
7369 (defun gnus-data-remove (article &optional offset)
7370   (let ((data gnus-newsgroup-data))
7371     (if (= (gnus-data-number (car data)) article)
7372         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7373               gnus-newsgroup-data-reverse nil)
7374       (while (cdr data)
7375         (and (= (gnus-data-number (cadr data)) article)
7376              (progn
7377                (setcdr data (cddr data))
7378                (and offset (gnus-data-update-list (cdr data) offset))
7379                (setq data nil
7380                      gnus-newsgroup-data-reverse nil)))
7381         (setq data (cdr data))))))
7382
7383 (defmacro gnus-data-list (backward)
7384   `(if ,backward
7385        (or gnus-newsgroup-data-reverse
7386            (setq gnus-newsgroup-data-reverse
7387                  (reverse gnus-newsgroup-data)))
7388      gnus-newsgroup-data))
7389
7390 (defun gnus-data-update-list (data offset)
7391   "Add OFFSET to the POS of all data entries in DATA."
7392   (while data
7393     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7394     (setq data (cdr data))))
7395
7396 (defun gnus-data-compute-positions ()
7397   "Compute the positions of all articles."
7398   (let ((data gnus-newsgroup-data)
7399         pos)
7400     (while data
7401       (when (setq pos (text-property-any
7402                        (point-min) (point-max)
7403                        'gnus-number (gnus-data-number (car data))))
7404         (gnus-data-set-pos (car data) (+ pos 3)))
7405       (setq data (cdr data)))))
7406
7407 (defun gnus-summary-article-pseudo-p (article)
7408   "Say whether this article is a pseudo article or not."
7409   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7410
7411 (defun gnus-article-parent-p (number)
7412   "Say whether this article is a parent or not."
7413   (let ((data (gnus-data-find-list number)))
7414     (and (cdr data)                     ; There has to be an article after...
7415          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7416             (gnus-data-level (nth 1 data))))))
7417
7418 (defun gnus-article-children (number)
7419   "Return a list of all children to NUMBER."
7420   (let* ((data (gnus-data-find-list number))
7421          (level (gnus-data-level (car data)))
7422          children)
7423     (setq data (cdr data))
7424     (while (and data            
7425                 (= (gnus-data-level (car data)) (1+ level)))
7426       (push (gnus-data-number (car data)) children)
7427       (setq data (cdr data)))
7428     children))
7429
7430 (defmacro gnus-summary-skip-intangible ()
7431   "If the current article is intangible, then jump to a different article."
7432   '(let ((to (get-text-property (point) 'gnus-intangible)))
7433     (and to (gnus-summary-goto-subject to))))
7434
7435 (defmacro gnus-summary-article-intangible-p ()
7436   "Say whether this article is intangible or not."
7437   '(get-text-property (point) 'gnus-intangible))
7438
7439 ;; Some summary mode macros.
7440
7441 (defmacro gnus-summary-article-number ()
7442   "The article number of the article on the current line.
7443 If there isn's an article number here, then we return the current
7444 article number."
7445   '(progn
7446      (gnus-summary-skip-intangible)
7447      (or (get-text-property (point) 'gnus-number)
7448          (gnus-summary-last-subject))))
7449
7450 (defmacro gnus-summary-article-header (&optional number)
7451   `(gnus-data-header (gnus-data-find
7452                       ,(or number '(gnus-summary-article-number)))))
7453
7454 (defmacro gnus-summary-thread-level (&optional number)
7455   `(if (and (eq gnus-summary-make-false-root 'dummy)
7456             (get-text-property (point) 'gnus-intangible))
7457        0
7458      (gnus-data-level (gnus-data-find
7459                        ,(or number '(gnus-summary-article-number))))))
7460
7461 (defmacro gnus-summary-article-mark (&optional number)
7462   `(gnus-data-mark (gnus-data-find
7463                     ,(or number '(gnus-summary-article-number)))))
7464
7465 (defmacro gnus-summary-article-pos (&optional number)
7466   `(gnus-data-pos (gnus-data-find
7467                    ,(or number '(gnus-summary-article-number)))))
7468
7469 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7470 (defmacro gnus-summary-article-subject (&optional number)
7471   "Return current subject string or nil if nothing."
7472   `(let ((headers
7473           ,(if number
7474                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7475              '(gnus-data-header (assq (gnus-summary-article-number)
7476                                       gnus-newsgroup-data)))))
7477      (and headers
7478           (vectorp headers)
7479           (mail-header-subject headers))))
7480
7481 (defmacro gnus-summary-article-score (&optional number)
7482   "Return current article score."
7483   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7484                   gnus-newsgroup-scored))
7485        gnus-summary-default-score 0))
7486
7487 (defun gnus-summary-article-children (&optional number)
7488   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7489          (level (gnus-data-level (car data)))
7490          l children)
7491     (while (and (setq data (cdr data))
7492                 (> (setq l (gnus-data-level (car data))) level))
7493       (and (= (1+ level) l)
7494            (setq children (cons (gnus-data-number (car data))
7495                                 children))))
7496     (nreverse children)))
7497
7498 (defun gnus-summary-article-parent (&optional number)
7499   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7500                                     (gnus-data-list t)))
7501          (level (gnus-data-level (car data))))
7502     (if (zerop level)
7503         () ; This is a root.
7504       ;; We search until we find an article with a level less than
7505       ;; this one.  That function has to be the parent.
7506       (while (and (setq data (cdr data))
7507                   (not (< (gnus-data-level (car data)) level))))
7508       (and data (gnus-data-number (car data))))))
7509
7510 (defun gnus-unread-mark-p (mark)
7511   "Say whether MARK is the unread mark."
7512   (= mark gnus-unread-mark))
7513
7514 (defun gnus-read-mark-p (mark)
7515   "Say whether MARK is one of the marks that mark as read.
7516 This is all marks except unread, ticked, dormant, and expirable."
7517   (not (or (= mark gnus-unread-mark)
7518            (= mark gnus-ticked-mark)
7519            (= mark gnus-dormant-mark)
7520            (= mark gnus-expirable-mark))))
7521
7522 ;; Saving hidden threads.
7523
7524 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7525 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7526 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
7527
7528 (defmacro gnus-save-hidden-threads (&rest forms)
7529   "Save hidden threads, eval FORMS, and restore the hidden threads."
7530   (let ((config (make-symbol "config")))
7531     `(let ((,config (gnus-hidden-threads-configuration)))
7532        (unwind-protect
7533            (progn
7534              ,@forms)
7535          (gnus-restore-hidden-threads-configuration ,config)))))
7536
7537 (defun gnus-hidden-threads-configuration ()
7538   "Return the current hidden threads configuration."
7539   (save-excursion
7540     (let (config)
7541       (goto-char (point-min))
7542       (while (search-forward "\r" nil t)
7543         (push (1- (point)) config))
7544       config)))
7545
7546 (defun gnus-restore-hidden-threads-configuration (config)
7547   "Restore hidden threads configuration from CONFIG."
7548   (let (point buffer-read-only)
7549     (while (setq point (pop config))
7550       (when (and (< point (point-max))
7551                  (goto-char point)
7552                  (= (following-char) ?\n))
7553         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7554
7555 ;; Various summary mode internalish functions.
7556
7557 (defun gnus-mouse-pick-article (e)
7558   (interactive "e")
7559   (mouse-set-point e)
7560   (gnus-summary-next-page nil t))
7561
7562 (defun gnus-summary-setup-buffer (group)
7563   "Initialize summary buffer."
7564   (let ((buffer (concat "*Summary " group "*")))
7565     (if (get-buffer buffer)
7566         (progn
7567           (set-buffer buffer)
7568           (setq gnus-summary-buffer (current-buffer))
7569           (not gnus-newsgroup-prepared))
7570       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7571       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7572       (gnus-add-current-to-buffer-list)
7573       (gnus-summary-mode group)
7574       (when gnus-carpal
7575         (gnus-carpal-setup-buffer 'summary))
7576       (unless gnus-single-article-buffer
7577         (make-local-variable 'gnus-article-buffer)
7578         (make-local-variable 'gnus-article-current)
7579         (make-local-variable 'gnus-original-article-buffer))
7580       (setq gnus-newsgroup-name group)
7581       t)))
7582
7583 (defun gnus-set-global-variables ()
7584   ;; Set the global equivalents of the summary buffer-local variables
7585   ;; to the latest values they had.  These reflect the summary buffer
7586   ;; that was in action when the last article was fetched.
7587   (when (eq major-mode 'gnus-summary-mode)
7588     (setq gnus-summary-buffer (current-buffer))
7589     (let ((name gnus-newsgroup-name)
7590           (marked gnus-newsgroup-marked)
7591           (unread gnus-newsgroup-unreads)
7592           (headers gnus-current-headers)
7593           (data gnus-newsgroup-data)
7594           (summary gnus-summary-buffer)
7595           (article-buffer gnus-article-buffer)
7596           (original gnus-original-article-buffer)
7597           (gac gnus-article-current)
7598           (score-file gnus-current-score-file))
7599       (save-excursion
7600         (set-buffer gnus-group-buffer)
7601         (setq gnus-newsgroup-name name)
7602         (setq gnus-newsgroup-marked marked)
7603         (setq gnus-newsgroup-unreads unread)
7604         (setq gnus-current-headers headers)
7605         (setq gnus-newsgroup-data data)
7606         (setq gnus-article-current gac)
7607         (setq gnus-summary-buffer summary)
7608         (setq gnus-article-buffer article-buffer)
7609         (setq gnus-original-article-buffer original)
7610         (setq gnus-current-score-file score-file)))))
7611
7612 (defun gnus-summary-last-article-p (&optional article)
7613   "Return whether ARTICLE is the last article in the buffer."
7614   (if (not (setq article (or article (gnus-summary-article-number))))
7615       t ; All non-existant numbers are the last article. :-)
7616     (not (cdr (gnus-data-find-list article)))))
7617
7618 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7619   "Insert a dummy root in the summary buffer."
7620   (beginning-of-line)
7621   (gnus-add-text-properties
7622    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7623    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7624
7625 (defun gnus-make-thread-indent-array ()
7626   (let ((n 200))
7627     (unless (and gnus-thread-indent-array
7628                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7629       (setq gnus-thread-indent-array (make-vector 201 "")
7630             gnus-thread-indent-array-level gnus-thread-indent-level)
7631       (while (>= n 0)
7632         (aset gnus-thread-indent-array n
7633               (make-string (* n gnus-thread-indent-level) ? ))
7634         (setq n (1- n))))))
7635
7636 (defun gnus-summary-insert-line
7637   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7638                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7639                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7640   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7641          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7642          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7643          (gnus-tmp-score-char
7644           (if (or (null gnus-summary-default-score)
7645                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7646                       gnus-summary-zcore-fuzz)) ? 
7647             (if (< gnus-tmp-score gnus-summary-default-score)
7648                 gnus-score-below-mark gnus-score-over-mark)))
7649          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7650                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7651                                   gnus-cached-mark)
7652                                  (gnus-tmp-replied gnus-replied-mark)
7653                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7654                                   gnus-saved-mark)
7655                                  (t gnus-unread-mark)))
7656          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7657          (gnus-tmp-name
7658           (cond
7659            ((string-match "(.+)" gnus-tmp-from)
7660             (substring gnus-tmp-from
7661                        (1+ (match-beginning 0)) (1- (match-end 0))))
7662            ((string-match "<[^>]+> *$" gnus-tmp-from)
7663             (let ((beg (match-beginning 0)))
7664               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7665                        (substring gnus-tmp-from (1+ (match-beginning 0))
7666                                   (1- (match-end 0))))
7667                   (substring gnus-tmp-from 0 beg))))
7668            (t gnus-tmp-from)))
7669          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7670          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7671          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7672          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7673          (buffer-read-only nil))
7674     (when (string= gnus-tmp-name "")
7675       (setq gnus-tmp-name gnus-tmp-from))
7676     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7677     (gnus-put-text-property
7678      (point)
7679      (progn (eval gnus-summary-line-format-spec) (point))
7680      'gnus-number gnus-tmp-number)
7681     (when (gnus-visual-p 'summary-highlight 'highlight)
7682       (forward-line -1)
7683       (run-hooks 'gnus-summary-update-hook)
7684       (forward-line 1))))
7685
7686 (defun gnus-summary-update-line (&optional dont-update)
7687   ;; Update summary line after change.
7688   (when (and gnus-summary-default-score
7689              (not gnus-summary-inhibit-highlight))
7690     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7691            (article (gnus-summary-article-number))
7692            (score (gnus-summary-article-score article)))
7693       (unless dont-update
7694         (if (and gnus-summary-mark-below
7695                  (< (gnus-summary-article-score)
7696                     gnus-summary-mark-below))
7697             ;; This article has a low score, so we mark it as read.
7698             (when (memq article gnus-newsgroup-unreads)
7699               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7700           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7701             ;; This article was previously marked as read on account
7702             ;; of a low score, but now it has risen, so we mark it as
7703             ;; unread.
7704             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7705         (gnus-summary-update-mark
7706          (if (or (null gnus-summary-default-score)
7707                  (<= (abs (- score gnus-summary-default-score))
7708                      gnus-summary-zcore-fuzz)) ? 
7709            (if (< score gnus-summary-default-score)
7710                gnus-score-below-mark gnus-score-over-mark)) 'score))
7711       ;; Do visual highlighting.
7712       (when (gnus-visual-p 'summary-highlight 'highlight)
7713         (run-hooks 'gnus-summary-update-hook)))))
7714
7715 (defvar gnus-tmp-new-adopts nil)
7716
7717 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7718   ;; Sum up all elements (and sub-elements) in a list.
7719   (let* ((number
7720           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7721           (cond
7722            ((and (consp thread) (cdr thread))
7723             (apply
7724              '+ 1 (mapcar
7725                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7726            ((null thread)
7727             1)
7728            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7729             1)
7730            (t 0))))
7731     (when (and level (zerop level) gnus-tmp-new-adopts)
7732       (incf number
7733             (apply '+ (mapcar
7734                        'gnus-summary-number-of-articles-in-thread
7735                        gnus-tmp-new-adopts))))
7736     (if char
7737         (if (> number 1) gnus-not-empty-thread-mark
7738           gnus-empty-thread-mark)
7739       number)))
7740
7741 (defun gnus-summary-set-local-parameters (group)
7742  "Go through the local params of GROUP and set all variable specs in that list."
7743   (let ((params (gnus-info-params (gnus-get-info group)))
7744         elem)
7745     (while params
7746       (setq elem (car params)
7747             params (cdr params))
7748       (and (consp elem)                 ; Has to be a cons.
7749            (consp (cdr elem))           ; The cdr has to be a list.
7750            (symbolp (car elem))         ; Has to be a symbol in there.
7751            (not (memq (car elem) 
7752                       '(quit-config to-address to-list to-group)))
7753            (progn                       ; So we set it.
7754              (make-local-variable (car elem))
7755              (set (car elem) (eval (nth 1 elem))))))))
7756
7757 (defun gnus-summary-read-group (group &optional show-all no-article
7758                                       kill-buffer no-display)
7759   "Start reading news in newsgroup GROUP.
7760 If SHOW-ALL is non-nil, already read articles are also listed.
7761 If NO-ARTICLE is non-nil, no article is selected initially.
7762 If NO-DISPLAY, don't generate a summary buffer."
7763   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7764   (let* ((new-group (gnus-summary-setup-buffer group))
7765          (quit-config (gnus-group-quit-config group))
7766          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7767     (cond
7768      ;; This summary buffer exists already, so we just select it.
7769      ((not new-group)
7770       (gnus-set-global-variables)
7771       (when kill-buffer
7772         (gnus-kill-or-deaden-summary kill-buffer))
7773       (gnus-configure-windows 'summary 'force)
7774       (gnus-set-mode-line 'summary)
7775       (gnus-summary-position-point)
7776       (message "")
7777       t)
7778      ;; We couldn't select this group.
7779      ((null did-select)
7780       (when (and (eq major-mode 'gnus-summary-mode)
7781                  (not (equal (current-buffer) kill-buffer)))
7782         (kill-buffer (current-buffer))
7783         (if (not quit-config)
7784             (progn
7785               (set-buffer gnus-group-buffer)
7786               (gnus-group-jump-to-group group)
7787               (gnus-group-next-unread-group 1))
7788           (if (not (buffer-name (car quit-config)))
7789               (gnus-configure-windows 'group 'force)
7790             (set-buffer (car quit-config))
7791             (and (eq major-mode 'gnus-summary-mode)
7792                  (gnus-set-global-variables))
7793             (gnus-configure-windows (cdr quit-config)))))
7794       (gnus-message 3 "Can't select group")
7795       nil)
7796      ;; The user did a `C-g' while prompting for number of articles,
7797      ;; so we exit this group.
7798      ((eq did-select 'quit)
7799       (and (eq major-mode 'gnus-summary-mode)
7800            (not (equal (current-buffer) kill-buffer))
7801            (kill-buffer (current-buffer)))
7802       (when kill-buffer
7803         (gnus-kill-or-deaden-summary kill-buffer))
7804       (if (not quit-config)
7805           (progn
7806             (set-buffer gnus-group-buffer)
7807             (gnus-group-jump-to-group group)
7808             (gnus-group-next-unread-group 1)
7809             (gnus-configure-windows 'group 'force))
7810         (if (not (buffer-name (car quit-config)))
7811             (gnus-configure-windows 'group 'force)
7812           (set-buffer (car quit-config))
7813           (and (eq major-mode 'gnus-summary-mode)
7814                (gnus-set-global-variables))
7815           (gnus-configure-windows (cdr quit-config))))
7816       ;; Finally signal the quit.
7817       (signal 'quit nil))
7818      ;; The group was successfully selected.
7819      (t
7820       (gnus-set-global-variables)
7821       ;; Save the active value in effect when the group was entered.
7822       (setq gnus-newsgroup-active
7823             (gnus-copy-sequence
7824              (gnus-active gnus-newsgroup-name)))
7825       ;; You can change the summary buffer in some way with this hook.
7826       (run-hooks 'gnus-select-group-hook)
7827       ;; Set any local variables in the group parameters.
7828       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7829       (gnus-update-format-specifications)
7830       ;; Do score processing.
7831       (when gnus-use-scoring
7832         (gnus-possibly-score-headers))
7833       ;; Check whether to fill in the gaps in the threads.
7834       (when gnus-build-sparse-threads
7835         (gnus-build-sparse-threads))
7836       ;; Find the initial limit.
7837       (if gnus-show-threads
7838           (if show-all
7839               (let ((gnus-newsgroup-dormant nil))
7840                 (gnus-summary-initial-limit show-all))
7841             (gnus-summary-initial-limit show-all))
7842         (setq gnus-newsgroup-limit 
7843               (mapcar 
7844                (lambda (header) (mail-header-number header))
7845                gnus-newsgroup-headers)))
7846       ;; Generate the summary buffer.
7847       (unless no-display
7848         (gnus-summary-prepare))
7849       (when gnus-use-trees
7850         (gnus-tree-open group)
7851         (setq gnus-summary-highlight-line-function
7852               'gnus-tree-highlight-article))
7853       ;; If the summary buffer is empty, but there are some low-scored
7854       ;; articles or some excluded dormants, we include these in the
7855       ;; buffer.
7856       (when (and (zerop (buffer-size))
7857                  (not no-display))
7858         (cond (gnus-newsgroup-dormant
7859                (gnus-summary-limit-include-dormant))
7860               ((and gnus-newsgroup-scored show-all)
7861                (gnus-summary-limit-include-expunged))))
7862       ;; Function `gnus-apply-kill-file' must be called in this hook.
7863       (run-hooks 'gnus-apply-kill-hook)
7864       (if (and (zerop (buffer-size))
7865                (not no-display))
7866           (progn
7867             ;; This newsgroup is empty.
7868             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7869             (gnus-message 6 "No unread news")
7870             (when kill-buffer
7871               (gnus-kill-or-deaden-summary kill-buffer))
7872             ;; Return nil from this function.
7873             nil)
7874         ;; Hide conversation thread subtrees.  We cannot do this in
7875         ;; gnus-summary-prepare-hook since kill processing may not
7876         ;; work with hidden articles.
7877         (and gnus-show-threads
7878              gnus-thread-hide-subtree
7879              (gnus-summary-hide-all-threads))
7880         ;; Show first unread article if requested.
7881         (if (and (not no-article)
7882                  (not no-display)
7883                  gnus-newsgroup-unreads
7884                  gnus-auto-select-first)
7885             (unless (if (eq gnus-auto-select-first 'best)
7886                         (gnus-summary-best-unread-article)
7887                       (gnus-summary-first-unread-article))
7888               (gnus-configure-windows 'summary))
7889           ;; Don't select any articles, just move point to the first
7890           ;; article in the group.
7891           (goto-char (point-min))
7892           (gnus-summary-position-point)
7893           (gnus-set-mode-line 'summary)
7894           (gnus-configure-windows 'summary 'force))
7895         ;; If we are in async mode, we send some info to the backend.
7896         (when gnus-newsgroup-async
7897           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7898         (when kill-buffer
7899           (gnus-kill-or-deaden-summary kill-buffer))
7900         (when (get-buffer-window gnus-group-buffer t)
7901           ;; Gotta use windows, because recenter does wierd stuff if
7902           ;; the current buffer ain't the displayed window.
7903           (let ((owin (selected-window)))
7904             (select-window (get-buffer-window gnus-group-buffer t))
7905             (when (gnus-group-goto-group group)
7906               (recenter))
7907             (select-window owin))))
7908       ;; Mark this buffer as "prepared".
7909       (setq gnus-newsgroup-prepared t)
7910       t))))
7911
7912 (defun gnus-summary-prepare ()
7913   "Generate the summary buffer."
7914   (let ((buffer-read-only nil))
7915     (erase-buffer)
7916     (setq gnus-newsgroup-data nil
7917           gnus-newsgroup-data-reverse nil)
7918     (run-hooks 'gnus-summary-generate-hook)
7919     ;; Generate the buffer, either with threads or without.
7920     (when gnus-newsgroup-headers
7921       (gnus-summary-prepare-threads
7922        (if gnus-show-threads
7923            (gnus-sort-gathered-threads
7924             (funcall gnus-summary-thread-gathering-function
7925                      (gnus-sort-threads
7926                       (gnus-cut-threads (gnus-make-threads)))))
7927          ;; Unthreaded display.
7928          (gnus-sort-articles gnus-newsgroup-headers))))
7929     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7930     ;; Call hooks for modifying summary buffer.
7931     (goto-char (point-min))
7932     (run-hooks 'gnus-summary-prepare-hook)))
7933
7934 (defun gnus-gather-threads-by-subject (threads)
7935   "Gather threads by looking at Subject headers."
7936   (if (not gnus-summary-make-false-root)
7937       threads
7938     (let ((hashtb (gnus-make-hashtable 1023))
7939           (prev threads)
7940           (result threads)
7941           subject hthread whole-subject)
7942       (while threads
7943         (setq whole-subject (mail-header-subject (caar threads)))
7944         (setq subject
7945               (cond
7946                ;; Truncate the subject.
7947                ((numberp gnus-summary-gather-subject-limit)
7948                 (setq subject (gnus-simplify-subject-re whole-subject))
7949                 (if (> (length subject) gnus-summary-gather-subject-limit)
7950                     (substring subject 0 gnus-summary-gather-subject-limit)
7951                   subject))
7952                ;; Fuzzily simplify it.
7953                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7954                 (gnus-simplify-subject-fuzzy whole-subject))
7955                ;; Just remove the leading "Re:".
7956                (t
7957                 (gnus-simplify-subject-re whole-subject))))
7958
7959         (if (and gnus-summary-gather-exclude-subject
7960                  (string-match gnus-summary-gather-exclude-subject
7961                                subject))
7962             ()          ; We don't want to do anything with this article.
7963           ;; We simplify the subject before looking it up in the
7964           ;; hash table.
7965
7966           (if (setq hthread (gnus-gethash subject hashtb))
7967               (progn
7968                 ;; We enter a dummy root into the thread, if we
7969                 ;; haven't done that already.
7970                 (unless (stringp (caar hthread))
7971                   (setcar hthread (list whole-subject (car hthread))))
7972                 ;; We add this new gathered thread to this gathered
7973                 ;; thread.
7974                 (setcdr (car hthread)
7975                         (nconc (cdar hthread) (list (car threads))))
7976                 ;; Remove it from the list of threads.
7977                 (setcdr prev (cdr threads))
7978                 (setq threads prev))
7979             ;; Enter this thread into the hash table.
7980             (gnus-sethash subject threads hashtb)))
7981         (setq prev threads)
7982         (setq threads (cdr threads)))
7983       result)))
7984
7985 (defun gnus-gather-threads-by-references (threads)
7986   "Gather threads by looking at References headers."
7987   (let ((idhashtb (gnus-make-hashtable 1023))
7988         (thhashtb (gnus-make-hashtable 1023))
7989         (prev threads)
7990         (result threads)
7991         ids references id gthread gid entered)
7992     (while threads
7993       (when (setq references (mail-header-references (caar threads)))
7994         (setq id (mail-header-id (caar threads)))
7995         (setq ids (gnus-split-references references))
7996         (setq entered nil)
7997         (while ids
7998           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7999               (progn
8000                 (gnus-sethash (car ids) id idhashtb)
8001                 (gnus-sethash id threads thhashtb))
8002             (setq gthread (gnus-gethash gid thhashtb))
8003             (unless entered
8004               ;; We enter a dummy root into the thread, if we
8005               ;; haven't done that already.
8006               (unless (stringp (caar gthread))
8007                 (setcar gthread (list (mail-header-subject (caar gthread))
8008                                       (car gthread))))
8009               ;; We add this new gathered thread to this gathered
8010               ;; thread.
8011               (setcdr (car gthread)
8012                       (nconc (cdar gthread) (list (car threads)))))
8013             ;; Add it into the thread hash table.
8014             (gnus-sethash id gthread thhashtb)
8015             (setq entered t)
8016             ;; Remove it from the list of threads.
8017             (setcdr prev (cdr threads))
8018             (setq threads prev))
8019           (setq ids (cdr ids))))
8020       (setq prev threads)
8021       (setq threads (cdr threads)))
8022     result))
8023
8024 (defun gnus-sort-gathered-threads (threads)
8025   "Sort subtreads inside each gathered thread by article number."
8026   (let ((result threads))
8027     (while threads
8028       (when (stringp (caar threads))
8029         (setcdr (car threads)
8030                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
8031       (setq threads (cdr threads)))
8032     result))
8033
8034 (defun gnus-make-threads ()
8035   "Go through the dependency hashtb and find the roots.  Return all threads."
8036   (let (threads)
8037     (mapatoms
8038      (lambda (refs)
8039        (unless (car (symbol-value refs))
8040          ;; These threads do not refer back to any other articles,
8041          ;; so they're roots.
8042          (setq threads (append (cdr (symbol-value refs)) threads))))
8043      gnus-newsgroup-dependencies)
8044     threads))
8045
8046 (defun gnus-build-sparse-threads ()
8047   (let ((headers gnus-newsgroup-headers)
8048         (deps gnus-newsgroup-dependencies)
8049         header references generation relations 
8050         cthread subject child end pthread relation)
8051     ;; First we create an alist of generations/relations, where 
8052     ;; generations is how much we trust the ralation, and the relation
8053     ;; is parent/child.
8054     (gnus-message 7 "Making sparse threads...")
8055     (save-excursion
8056       (nnheader-set-temp-buffer " *gnus sparse threads*")
8057       (while (setq header (pop headers))
8058         (when (and (setq references (mail-header-references header))
8059                    (not (string= references "")))
8060           (insert references)
8061           (setq child (mail-header-id header)
8062                 subject (mail-header-subject header))
8063           (setq generation 0)
8064           (while (search-backward ">" nil t)
8065             (setq end (1+ (point)))
8066             (when (search-backward "<" nil t)
8067               (push (list (incf generation) 
8068                           child (setq child (buffer-substring (point) end))
8069                           subject)
8070                     relations)))
8071           (push (list (1+ generation) child nil subject) relations)
8072           (erase-buffer)))
8073       (kill-buffer (current-buffer)))
8074     ;; Sort over trustworthiness.
8075     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
8076     (while (setq relation (pop relations))
8077       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
8078                 (unless (car (symbol-value cthread))
8079                   ;; Make this article the parent of these threads.
8080                   (setcar (symbol-value cthread)
8081                           (vector gnus-reffed-article-number 
8082                                   (cadddr relation) 
8083                                   "" ""
8084                                   (cadr relation) 
8085                                   (or (caddr relation) "") 0 0 "")))
8086               (set cthread (list (vector gnus-reffed-article-number
8087                                          (cadddr relation) 
8088                                          "" "" (cadr relation) 
8089                                          (or (caddr relation) "") 0 0 ""))))
8090         (push gnus-reffed-article-number gnus-newsgroup-limit)
8091         (push gnus-reffed-article-number gnus-newsgroup-sparse)
8092         (push (cons gnus-reffed-article-number gnus-sparse-mark)
8093               gnus-newsgroup-reads)
8094         (decf gnus-reffed-article-number)
8095         ;; Make this new thread the child of its parent.
8096         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
8097             (setcdr (symbol-value pthread)
8098                     (nconc (cdr (symbol-value pthread))
8099                            (list (symbol-value cthread))))
8100           (set pthread (list nil (symbol-value cthread))))))
8101     (gnus-message 7 "Making sparse threads...done")))
8102
8103 (defun gnus-build-old-threads ()
8104   ;; Look at all the articles that refer back to old articles, and
8105   ;; fetch the headers for the articles that aren't there.  This will
8106   ;; build complete threads - if the roots haven't been expired by the
8107   ;; server, that is.
8108   (let (id heads)
8109     (mapatoms
8110      (lambda (refs)
8111        (when (not (car (symbol-value refs)))
8112          (setq heads (cdr (symbol-value refs)))
8113          (while heads
8114            (if (memq (mail-header-number (caar heads))
8115                      gnus-newsgroup-dormant)
8116                (setq heads (cdr heads))
8117              (setq id (symbol-name refs))
8118              (while (and (setq id (gnus-build-get-header id))
8119                          (not (car (gnus-gethash
8120                                     id gnus-newsgroup-dependencies)))))
8121              (setq heads nil)))))
8122      gnus-newsgroup-dependencies)))
8123
8124 (defun gnus-build-get-header (id)
8125   ;; Look through the buffer of NOV lines and find the header to
8126   ;; ID.  Enter this line into the dependencies hash table, and return
8127   ;; the id of the parent article (if any).
8128   (let ((deps gnus-newsgroup-dependencies)
8129         found header)
8130     (prog1
8131         (save-excursion
8132           (set-buffer nntp-server-buffer)
8133           (goto-char (point-min))
8134           (while (and (not found) (search-forward id nil t))
8135             (beginning-of-line)
8136             (setq found (looking-at
8137                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8138                                  (regexp-quote id))))
8139             (or found (beginning-of-line 2)))
8140           (when found
8141             (beginning-of-line)
8142             (and
8143              (setq header (gnus-nov-parse-line
8144                            (read (current-buffer)) deps))
8145              (gnus-parent-id (mail-header-references header)))))
8146       (when header
8147         (let ((number (mail-header-number header)))
8148           (push number gnus-newsgroup-limit)
8149           (push header gnus-newsgroup-headers)
8150           (if (memq number gnus-newsgroup-unselected)
8151               (progn
8152                 (push number gnus-newsgroup-unreads)
8153                 (setq gnus-newsgroup-unselected
8154                       (delq number gnus-newsgroup-unselected)))
8155             (push number gnus-newsgroup-ancient)))))))
8156
8157 (defun gnus-summary-update-article (article &optional iheader)
8158   "Update ARTICLE in the summary buffer."
8159   (set-buffer gnus-summary-buffer)
8160   (let* ((header (or iheader (gnus-summary-article-header article)))
8161          (id (mail-header-id header))
8162          (data (gnus-data-find article))
8163          (thread (gnus-id-to-thread id))
8164          (references (mail-header-references header))
8165          (parent
8166           (gnus-id-to-thread
8167            (or (gnus-parent-id 
8168                 (if (and references
8169                          (not (equal "" references)))
8170                     references))
8171                "none")))
8172          (buffer-read-only nil)
8173          (old (car thread))
8174          (number (mail-header-number header))
8175          pos)
8176     (when thread
8177       ;; !!! Should this be in or not?
8178       (unless iheader
8179         (setcar thread nil))
8180       (when parent
8181         (delq thread parent))
8182       (if (gnus-summary-insert-subject id header iheader)
8183           ;; Set the (possibly) new article number in the data structure.
8184           (gnus-data-set-number data (gnus-id-to-article id))
8185         (setcar thread old)
8186         nil))))
8187
8188 (defun gnus-rebuild-thread (id)
8189   "Rebuild the thread containing ID."
8190   (let ((buffer-read-only nil)
8191         current thread data)
8192     (if (not gnus-show-threads)
8193         (setq thread (list (car (gnus-id-to-thread id))))
8194       ;; Get the thread this article is part of.
8195       (setq thread (gnus-remove-thread id)))
8196     (setq current (save-excursion
8197                     (and (zerop (forward-line -1))
8198                          (gnus-summary-article-number))))
8199     ;; If this is a gathered thread, we have to go some re-gathering.
8200     (when (stringp (car thread))
8201       (let ((subject (car thread))
8202             roots thr)
8203         (setq thread (cdr thread))
8204         (while thread
8205           (unless (memq (setq thr (gnus-id-to-thread
8206                                       (gnus-root-id
8207                                        (mail-header-id (caar thread)))))
8208                         roots)
8209             (push thr roots))
8210           (setq thread (cdr thread)))
8211         ;; We now have all (unique) roots.
8212         (if (= (length roots) 1)
8213             ;; All the loose roots are now one solid root.
8214             (setq thread (car roots))
8215           (setq thread (cons subject (gnus-sort-threads roots))))))
8216     (let (threads)
8217       ;; We then insert this thread into the summary buffer.
8218       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8219         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8220         (setq data (nreverse gnus-newsgroup-data))
8221         (setq threads gnus-newsgroup-threads))
8222       ;; We splice the new data into the data structure.
8223       (gnus-data-enter-list current data)
8224       (gnus-data-compute-positions)
8225       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8226
8227 (defun gnus-number-to-header (number)
8228   "Return the header for article NUMBER."
8229   (let ((headers gnus-newsgroup-headers))
8230     (while (and headers
8231                 (not (= number (mail-header-number (car headers)))))
8232       (pop headers))
8233     (when headers
8234       (car headers))))
8235
8236 (defun gnus-id-to-thread (id)
8237   "Return the (sub-)thread where ID appears."
8238   (gnus-gethash id gnus-newsgroup-dependencies))
8239
8240 (defun gnus-id-to-article (id)
8241   "Return the article number of ID."
8242   (let ((thread (gnus-id-to-thread id)))
8243     (when (and thread
8244                (car thread))
8245       (mail-header-number (car thread)))))
8246
8247 (defun gnus-id-to-header (id)
8248   "Return the article headers of ID."
8249   (car (gnus-id-to-thread id)))
8250
8251 (defun gnus-article-displayed-root-p (article)
8252   "Say whether ARTICLE is a root(ish) article."
8253   (let ((level (gnus-summary-thread-level article))
8254         (refs (mail-header-references  (gnus-summary-article-header article)))
8255         particle)
8256     (cond 
8257      ((null level) nil)
8258      ((zerop level) t)
8259      ((null refs) t)
8260      ((null (gnus-parent-id refs)) t)
8261      ((and (= 1 level)
8262            (null (setq particle (gnus-id-to-article
8263                                  (gnus-parent-id refs))))
8264            (null (gnus-summary-thread-level particle)))))))
8265
8266 (defun gnus-root-id (id)
8267   "Return the id of the root of the thread where ID appears."
8268   (let (last-id prev)
8269     (while (and id (setq prev (car (gnus-gethash 
8270                                     id gnus-newsgroup-dependencies))))
8271       (setq last-id id
8272             id (gnus-parent-id (mail-header-references prev))))
8273     last-id))
8274
8275 (defun gnus-remove-thread (id &optional dont-remove)
8276   "Remove the thread that has ID in it."
8277   (let ((dep gnus-newsgroup-dependencies)
8278         headers thread last-id)
8279     ;; First go up in this thread until we find the root.
8280     (setq last-id (gnus-root-id id))
8281     (setq headers (list (car (gnus-id-to-thread last-id))
8282                         (caadr (gnus-id-to-thread last-id))))
8283     ;; We have now found the real root of this thread.  It might have
8284     ;; been gathered into some loose thread, so we have to search
8285     ;; through the threads to find the thread we wanted.
8286     (let ((threads gnus-newsgroup-threads)
8287           sub)
8288       (while threads
8289         (setq sub (car threads))
8290         (if (stringp (car sub))
8291             ;; This is a gathered threads, so we look at the roots
8292             ;; below it to find whether this article in in this
8293             ;; gathered root.
8294             (progn
8295               (setq sub (cdr sub))
8296               (while sub
8297                 (when (member (caar sub) headers)
8298                   (setq thread (car threads)
8299                         threads nil
8300                         sub nil))
8301                 (setq sub (cdr sub))))
8302           ;; It's an ordinary thread, so we check it.
8303           (when (eq (car sub) (car headers))
8304             (setq thread sub
8305                   threads nil)))
8306         (setq threads (cdr threads)))
8307       ;; If this article is in no thread, then it's a root.
8308       (if thread
8309           (unless dont-remove
8310             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8311         (setq thread (gnus-gethash last-id dep)))
8312       (when thread
8313         (prog1
8314             thread ; We return this thread.
8315           (unless dont-remove
8316             (if (stringp (car thread))
8317                 (progn
8318                   ;; If we use dummy roots, then we have to remove the
8319                   ;; dummy root as well.
8320                   (when (eq gnus-summary-make-false-root 'dummy)
8321                     ;; Uhm.
8322                     )
8323                   (setq thread (cdr thread))
8324                   (while thread
8325                     (gnus-remove-thread-1 (car thread))
8326                     (setq thread (cdr thread))))
8327               (gnus-remove-thread-1 thread))))))))
8328
8329 (defun gnus-remove-thread-1 (thread)
8330   "Remove the thread THREAD recursively."
8331   (let ((number (mail-header-number (car thread)))
8332         pos)
8333     (when (setq pos (text-property-any
8334                      (point-min) (point-max) 'gnus-number number))
8335       (goto-char pos)
8336       (gnus-delete-line)
8337       (gnus-data-remove number))
8338     (setq thread (cdr thread))
8339     (while thread
8340       (gnus-remove-thread-1 (pop thread)))))
8341
8342 (defun gnus-sort-threads (threads)
8343   "Sort THREADS."
8344   (if (not gnus-thread-sort-functions)
8345       threads
8346     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8347                     (car gnus-thread-sort-functions)
8348                   `(lambda (t1 t2)
8349                      ,(gnus-make-sort-function 
8350                        (reverse gnus-thread-sort-functions))))))
8351       (gnus-message 7 "Sorting threads...")
8352       (prog1
8353           (sort threads func)
8354         (gnus-message 7 "Sorting threads...done")))))
8355
8356 (defun gnus-sort-articles (articles)
8357   "Sort ARTICLES."
8358   (when gnus-article-sort-functions
8359     (let ((func (if (= 1 (length gnus-article-sort-functions))
8360                     (car gnus-article-sort-functions)
8361                   `(lambda (t1 t2)
8362                      ,(gnus-make-sort-function 
8363                        (reverse gnus-article-sort-functions))))))
8364       (gnus-message 7 "Sorting articles...")
8365       (prog1
8366           (setq gnus-newsgroup-headers (sort articles func))
8367         (gnus-message 7 "Sorting articles...done")))))
8368
8369 (defun gnus-make-sort-function (funs)
8370   "Return a composite sort condition based on the functions in FUNC."
8371   (if (cdr funs)
8372       `(or (,(car funs) t1 t2)
8373            (and (not (,(car funs) t2 t1))
8374                 ,(gnus-make-sort-function (cdr funs))))
8375     `(,(car funs) t1 t2)))
8376                  
8377 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8378 (defmacro gnus-thread-header (thread)
8379   ;; Return header of first article in THREAD.
8380   ;; Note that THREAD must never, ever be anything else than a variable -
8381   ;; using some other form will lead to serious barfage.
8382   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8383   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8384   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8385         (vector thread) 2))
8386
8387 (defsubst gnus-article-sort-by-number (h1 h2)
8388   "Sort articles by article number."
8389   (< (mail-header-number h1)
8390      (mail-header-number h2)))
8391
8392 (defun gnus-thread-sort-by-number (h1 h2)
8393   "Sort threads by root article number."
8394   (gnus-article-sort-by-number
8395    (gnus-thread-header h1) (gnus-thread-header h2)))
8396
8397 (defsubst gnus-article-sort-by-author (h1 h2)
8398   "Sort articles by root author."
8399   (string-lessp
8400    (let ((extract (funcall
8401                    gnus-extract-address-components
8402                    (mail-header-from h1))))
8403      (or (car extract) (cdr extract)))
8404    (let ((extract (funcall
8405                    gnus-extract-address-components
8406                    (mail-header-from h2))))
8407      (or (car extract) (cdr extract)))))
8408
8409 (defun gnus-thread-sort-by-author (h1 h2)
8410   "Sort threads by root author."
8411   (gnus-article-sort-by-author
8412    (gnus-thread-header h1)  (gnus-thread-header h2)))
8413
8414 (defsubst gnus-article-sort-by-subject (h1 h2)
8415   "Sort articles by root subject."
8416   (string-lessp
8417    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8418    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8419
8420 (defun gnus-thread-sort-by-subject (h1 h2)
8421   "Sort threads by root subject."
8422   (gnus-article-sort-by-subject
8423    (gnus-thread-header h1) (gnus-thread-header h2)))
8424
8425 (defsubst gnus-article-sort-by-date (h1 h2)
8426   "Sort articles by root article date."
8427   (string-lessp
8428    (inline (gnus-sortable-date (mail-header-date h1)))
8429    (inline (gnus-sortable-date (mail-header-date h2)))))
8430
8431 (defun gnus-thread-sort-by-date (h1 h2)
8432   "Sort threads by root article date."
8433   (gnus-article-sort-by-date
8434    (gnus-thread-header h1) (gnus-thread-header h2)))
8435
8436 (defsubst gnus-article-sort-by-score (h1 h2)
8437   "Sort articles by root article score.
8438 Unscored articles will be counted as having a score of zero."
8439   (> (or (cdr (assq (mail-header-number h1)
8440                     gnus-newsgroup-scored))
8441          gnus-summary-default-score 0)
8442      (or (cdr (assq (mail-header-number h2)
8443                     gnus-newsgroup-scored))
8444          gnus-summary-default-score 0)))
8445
8446 (defun gnus-thread-sort-by-score (h1 h2)
8447   "Sort threads by root article score."
8448   (gnus-article-sort-by-score
8449    (gnus-thread-header h1) (gnus-thread-header h2)))
8450
8451 (defun gnus-thread-sort-by-total-score (h1 h2)
8452   "Sort threads by the sum of all scores in the thread.
8453 Unscored articles will be counted as having a score of zero."
8454   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8455
8456 (defun gnus-thread-total-score (thread)
8457   ;;  This function find the total score of THREAD.
8458   (cond ((null thread)
8459          0)
8460         ((consp thread)
8461          (if (stringp (car thread))
8462              (apply gnus-thread-score-function 0
8463                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8464            (gnus-thread-total-score-1 thread)))
8465         (t
8466          (gnus-thread-total-score-1 (list thread)))))
8467
8468 (defun gnus-thread-total-score-1 (root)
8469   ;; This function find the total score of the thread below ROOT.
8470   (setq root (car root))
8471   (apply gnus-thread-score-function
8472          (or (append
8473               (mapcar 'gnus-thread-total-score
8474                       (cdr (gnus-gethash (mail-header-id root)
8475                                          gnus-newsgroup-dependencies)))
8476                  (if (> (mail-header-number root) 0)
8477                      (list (or (cdr (assq (mail-header-number root) 
8478                                           gnus-newsgroup-scored))
8479                                gnus-summary-default-score 0))))
8480              (list gnus-summary-default-score)
8481              '(0))))
8482
8483 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8484 (defvar gnus-tmp-prev-subject nil)
8485 (defvar gnus-tmp-false-parent nil)
8486 (defvar gnus-tmp-root-expunged nil)
8487 (defvar gnus-tmp-dummy-line nil)
8488
8489 (defun gnus-summary-prepare-threads (threads)
8490   "Prepare summary buffer from THREADS and indentation LEVEL.
8491 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8492 or a straight list of headers."
8493   (gnus-message 7 "Generating summary...")
8494
8495   (setq gnus-newsgroup-threads threads)
8496   (beginning-of-line)
8497
8498   (let ((gnus-tmp-level 0)
8499         (default-score (or gnus-summary-default-score 0))
8500         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8501         thread number subject stack state gnus-tmp-gathered beg-match
8502         new-roots gnus-tmp-new-adopts thread-end
8503         gnus-tmp-header gnus-tmp-unread
8504         gnus-tmp-replied gnus-tmp-subject-or-nil
8505         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8506         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8507         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8508
8509     (setq gnus-tmp-prev-subject nil)
8510
8511     (if (vectorp (car threads))
8512         ;; If this is a straight (sic) list of headers, then a
8513         ;; threaded summary display isn't required, so we just create
8514         ;; an unthreaded one.
8515         (gnus-summary-prepare-unthreaded threads)
8516
8517       ;; Do the threaded display.
8518
8519       (while (or threads stack gnus-tmp-new-adopts new-roots)
8520
8521         (if (and (= gnus-tmp-level 0)
8522                  (not (setq gnus-tmp-dummy-line nil))
8523                  (or (not stack)
8524                      (= (caar stack) 0))
8525                  (not gnus-tmp-false-parent)
8526                  (or gnus-tmp-new-adopts new-roots))
8527             (if gnus-tmp-new-adopts
8528                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8529                       thread (list (car gnus-tmp-new-adopts))
8530                       gnus-tmp-header (caar thread)
8531                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8532               (if new-roots
8533                   (setq thread (list (car new-roots))
8534                         gnus-tmp-header (caar thread)
8535                         new-roots (cdr new-roots))))
8536
8537           (if threads
8538               ;; If there are some threads, we do them before the
8539               ;; threads on the stack.
8540               (setq thread threads
8541                     gnus-tmp-header (caar thread))
8542             ;; There were no current threads, so we pop something off
8543             ;; the stack.
8544             (setq state (car stack)
8545                   gnus-tmp-level (car state)
8546                   thread (cdr state)
8547                   stack (cdr stack)
8548                   gnus-tmp-header (caar thread))))
8549
8550         (setq gnus-tmp-false-parent nil)
8551         (setq gnus-tmp-root-expunged nil)
8552         (setq thread-end nil)
8553
8554         (if (stringp gnus-tmp-header)
8555             ;; The header is a dummy root.
8556             (cond
8557              ((eq gnus-summary-make-false-root 'adopt)
8558               ;; We let the first article adopt the rest.
8559               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8560                                                (cddar thread)))
8561               (setq gnus-tmp-gathered
8562                     (nconc (mapcar
8563                             (lambda (h) (mail-header-number (car h)))
8564                             (cddar thread))
8565                            gnus-tmp-gathered))
8566               (setq thread (cons (list (caar thread)
8567                                        (cadar thread))
8568                                  (cdr thread)))
8569               (setq gnus-tmp-level -1
8570                     gnus-tmp-false-parent t))
8571              ((eq gnus-summary-make-false-root 'empty)
8572               ;; We print adopted articles with empty subject fields.
8573               (setq gnus-tmp-gathered
8574                     (nconc (mapcar
8575                             (lambda (h) (mail-header-number (car h)))
8576                             (cddar thread))
8577                            gnus-tmp-gathered))
8578               (setq gnus-tmp-level -1))
8579              ((eq gnus-summary-make-false-root 'dummy)
8580               ;; We remember that we probably want to output a dummy
8581               ;; root.
8582               (setq gnus-tmp-dummy-line gnus-tmp-header)
8583               (setq gnus-tmp-prev-subject gnus-tmp-header))
8584              (t
8585               ;; We do not make a root for the gathered
8586               ;; sub-threads at all.
8587               (setq gnus-tmp-level -1)))
8588
8589           (setq number (mail-header-number gnus-tmp-header)
8590                 subject (mail-header-subject gnus-tmp-header))
8591
8592           (cond
8593            ;; If the thread has changed subject, we might want to make
8594            ;; this subthread into a root.
8595            ((and (null gnus-thread-ignore-subject)
8596                  (not (zerop gnus-tmp-level))
8597                  gnus-tmp-prev-subject
8598                  (not (inline
8599                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8600             (setq new-roots (nconc new-roots (list (car thread)))
8601                   thread-end t
8602                   gnus-tmp-header nil))
8603            ;; If the article lies outside the current limit,
8604            ;; then we do not display it.
8605            ((and (not (memq number gnus-newsgroup-limit))
8606                  (not gnus-tmp-dummy-line))
8607             (setq gnus-tmp-gathered
8608                   (nconc (mapcar
8609                           (lambda (h) (mail-header-number (car h)))
8610                           (cdar thread))
8611                          gnus-tmp-gathered))
8612             (setq gnus-tmp-new-adopts (if (cdar thread)
8613                                           (append gnus-tmp-new-adopts
8614                                                   (cdar thread))
8615                                         gnus-tmp-new-adopts)
8616                   thread-end t
8617                   gnus-tmp-header nil)
8618             (when (zerop gnus-tmp-level)
8619               (setq gnus-tmp-root-expunged t)))
8620            ;; Perhaps this article is to be marked as read?
8621            ((and gnus-summary-mark-below
8622                  (< (or (cdr (assq number gnus-newsgroup-scored))
8623                         default-score)
8624                     gnus-summary-mark-below)
8625                  ;; Don't touch sparse articles.
8626                  (not (memq number gnus-newsgroup-sparse))
8627                  (not (memq number gnus-newsgroup-ancient)))
8628             (setq gnus-newsgroup-unreads
8629                   (delq number gnus-newsgroup-unreads))
8630             (if gnus-newsgroup-auto-expire
8631                 (push number gnus-newsgroup-expirable)
8632               (push (cons number gnus-low-score-mark)
8633                     gnus-newsgroup-reads))))
8634
8635           (when gnus-tmp-header
8636             ;; We may have an old dummy line to output before this
8637             ;; article.
8638             (when gnus-tmp-dummy-line
8639               (gnus-summary-insert-dummy-line
8640                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8641               (setq gnus-tmp-dummy-line nil))
8642
8643             ;; Compute the mark.
8644             (setq
8645              gnus-tmp-unread
8646              (cond
8647               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8648               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8649               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8650               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8651               (t (or (cdr (assq number gnus-newsgroup-reads))
8652                      gnus-ancient-mark))))
8653
8654             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8655                                   gnus-tmp-header gnus-tmp-level)
8656                   gnus-newsgroup-data)
8657
8658             ;; Actually insert the line.
8659             (setq
8660              gnus-tmp-subject-or-nil
8661              (cond
8662               ((and gnus-thread-ignore-subject
8663                     gnus-tmp-prev-subject
8664                     (not (inline (gnus-subject-equal
8665                                   gnus-tmp-prev-subject subject))))
8666                subject)
8667               ((zerop gnus-tmp-level)
8668                (if (and (eq gnus-summary-make-false-root 'empty)
8669                         (memq number gnus-tmp-gathered)
8670                         gnus-tmp-prev-subject
8671                         (inline (gnus-subject-equal
8672                                  gnus-tmp-prev-subject subject)))
8673                    gnus-summary-same-subject
8674                  subject))
8675               (t gnus-summary-same-subject)))
8676             (if (and (eq gnus-summary-make-false-root 'adopt)
8677                      (= gnus-tmp-level 1)
8678                      (memq number gnus-tmp-gathered))
8679                 (setq gnus-tmp-opening-bracket ?\<
8680                       gnus-tmp-closing-bracket ?\>)
8681               (setq gnus-tmp-opening-bracket ?\[
8682                     gnus-tmp-closing-bracket ?\]))
8683             (setq
8684              gnus-tmp-indentation
8685              (aref gnus-thread-indent-array gnus-tmp-level)
8686              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8687              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8688                                 gnus-summary-default-score 0)
8689              gnus-tmp-score-char
8690              (if (or (null gnus-summary-default-score)
8691                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8692                          gnus-summary-zcore-fuzz)) ? 
8693                (if (< gnus-tmp-score gnus-summary-default-score)
8694                    gnus-score-below-mark gnus-score-over-mark))
8695              gnus-tmp-replied
8696              (cond ((memq number gnus-newsgroup-processable)
8697                     gnus-process-mark)
8698                    ((memq number gnus-newsgroup-cached)
8699                     gnus-cached-mark)
8700                    ((memq number gnus-newsgroup-replied)
8701                     gnus-replied-mark)
8702                    ((memq number gnus-newsgroup-saved)
8703                     gnus-saved-mark)
8704                    (t gnus-unread-mark))
8705              gnus-tmp-from (mail-header-from gnus-tmp-header)
8706              gnus-tmp-name
8707              (cond
8708               ((string-match "(.+)" gnus-tmp-from)
8709                (substring gnus-tmp-from
8710                           (1+ (match-beginning 0)) (1- (match-end 0))))
8711               ((string-match "<[^>]+> *$" gnus-tmp-from)
8712                (setq beg-match (match-beginning 0))
8713                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8714                         (substring gnus-tmp-from (1+ (match-beginning 0))
8715                                    (1- (match-end 0))))
8716                    (substring gnus-tmp-from 0 beg-match)))
8717               (t gnus-tmp-from)))
8718             (when (string= gnus-tmp-name "")
8719               (setq gnus-tmp-name gnus-tmp-from))
8720             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8721             (gnus-put-text-property
8722              (point)
8723              (progn (eval gnus-summary-line-format-spec) (point))
8724              'gnus-number number)
8725             (when gnus-visual-p
8726               (forward-line -1)
8727               (run-hooks 'gnus-summary-update-hook)
8728               (forward-line 1))
8729
8730             (setq gnus-tmp-prev-subject subject)))
8731
8732         (when (nth 1 thread)
8733           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8734         (incf gnus-tmp-level)
8735         (setq threads (if thread-end nil (cdar thread)))
8736         (unless threads
8737           (setq gnus-tmp-level 0)))))
8738   (gnus-message 7 "Generating summary...done"))
8739
8740 (defun gnus-summary-prepare-unthreaded (headers)
8741   "Generate an unthreaded summary buffer based on HEADERS."
8742   (let (header number mark)
8743
8744     (while headers
8745       ;; We may have to root out some bad articles...
8746       (when (memq (setq number (mail-header-number
8747                                 (setq header (pop headers))))
8748                   gnus-newsgroup-limit)
8749         ;; Mark article as read when it has a low score.
8750         (when (and gnus-summary-mark-below
8751                    (< (or (cdr (assq number gnus-newsgroup-scored))
8752                           gnus-summary-default-score 0)
8753                       gnus-summary-mark-below)
8754                    (not (memq number gnus-newsgroup-ancient)))
8755           (setq gnus-newsgroup-unreads
8756                 (delq number gnus-newsgroup-unreads))
8757           (if gnus-newsgroup-auto-expire
8758               (push number gnus-newsgroup-expirable)
8759             (push (cons number gnus-low-score-mark)
8760                   gnus-newsgroup-reads)))
8761
8762         (setq mark
8763               (cond
8764                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8765                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8766                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8767                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8768                (t (or (cdr (assq number gnus-newsgroup-reads))
8769                       gnus-ancient-mark))))
8770         (setq gnus-newsgroup-data
8771               (cons (gnus-data-make number mark (1+ (point)) header 0)
8772                     gnus-newsgroup-data))
8773         (gnus-summary-insert-line
8774          header 0 nil mark (memq number gnus-newsgroup-replied)
8775          (memq number gnus-newsgroup-expirable)
8776          (mail-header-subject header) nil
8777          (cdr (assq number gnus-newsgroup-scored))
8778          (memq number gnus-newsgroup-processable))))))
8779
8780 (defun gnus-select-newsgroup (group &optional read-all)
8781   "Select newsgroup GROUP.
8782 If READ-ALL is non-nil, all articles in the group are selected."
8783   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8784          (info (nth 2 entry))
8785          articles fetched-articles cached)
8786
8787     (or (gnus-check-server
8788          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8789         (error "Couldn't open server"))
8790
8791     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8792         (gnus-activate-group group)     ; Or we can activate it...
8793         (progn                          ; Or we bug out.
8794           (when (equal major-mode 'gnus-summary-mode)
8795             (kill-buffer (current-buffer)))
8796           (error "Couldn't request group %s: %s"
8797                  group (gnus-status-message group))))
8798
8799     (unless (gnus-request-group group t)
8800       (when (equal major-mode 'gnus-summary-mode)
8801         (kill-buffer (current-buffer)))
8802       (error "Couldn't request group %s: %s"
8803              group (gnus-status-message group)))      
8804
8805     (setq gnus-newsgroup-name group)
8806     (setq gnus-newsgroup-unselected nil)
8807     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8808
8809     (and gnus-asynchronous
8810          (gnus-check-backend-function
8811           'request-asynchronous gnus-newsgroup-name)
8812          (setq gnus-newsgroup-async
8813                (gnus-request-asynchronous gnus-newsgroup-name)))
8814
8815     ;; Adjust and set lists of article marks.
8816     (when info
8817       (gnus-adjust-marked-articles info))
8818
8819     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8820     (when (gnus-virtual-group-p group)
8821       (setq cached gnus-newsgroup-cached))
8822
8823     (setq gnus-newsgroup-unreads
8824           (gnus-set-difference
8825            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8826            gnus-newsgroup-dormant))
8827
8828     (setq gnus-newsgroup-processable nil)
8829
8830     (setq articles (gnus-articles-to-read group read-all))
8831
8832     (cond
8833      ((null articles)
8834       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8835       'quit)
8836      ((eq articles 0) nil)
8837      (t
8838       ;; Init the dependencies hash table.
8839       (setq gnus-newsgroup-dependencies
8840             (gnus-make-hashtable (length articles)))
8841       ;; Retrieve the headers and read them in.
8842       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8843       (setq gnus-newsgroup-headers
8844             (if (eq 'nov
8845                     (setq gnus-headers-retrieved-by
8846                           (gnus-retrieve-headers
8847                            articles gnus-newsgroup-name
8848                            ;; We might want to fetch old headers, but
8849                            ;; not if there is only 1 article.
8850                            (and gnus-fetch-old-headers
8851                                 (or (and
8852                                      (not (eq gnus-fetch-old-headers 'some))
8853                                      (not (numberp gnus-fetch-old-headers)))
8854                                     (> (length articles) 1))))))
8855                 (gnus-get-newsgroup-headers-xover articles)
8856               (gnus-get-newsgroup-headers)))
8857       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8858
8859       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8860       (when cached
8861         (setq gnus-newsgroup-cached cached))
8862
8863       ;; Set the initial limit.
8864       (setq gnus-newsgroup-limit (copy-sequence articles))
8865       ;; Remove canceled articles from the list of unread articles.
8866       (setq gnus-newsgroup-unreads
8867             (gnus-set-sorted-intersection
8868              gnus-newsgroup-unreads
8869              (setq fetched-articles
8870                    (mapcar (lambda (headers) (mail-header-number headers))
8871                            gnus-newsgroup-headers))))
8872       ;; Removed marked articles that do not exist.
8873       (gnus-update-missing-marks
8874        (gnus-sorted-complement fetched-articles articles))
8875       ;; We might want to build some more threads first.
8876       (and gnus-fetch-old-headers
8877            (eq gnus-headers-retrieved-by 'nov)
8878            (gnus-build-old-threads))
8879       ;; Check whether auto-expire is to be done in this group.
8880       (setq gnus-newsgroup-auto-expire
8881             (gnus-group-auto-expirable-p group))
8882       ;; Set up the article buffer now, if necessary.
8883       (unless gnus-single-article-buffer
8884         (gnus-article-setup-buffer))
8885       ;; First and last article in this newsgroup.
8886       (when gnus-newsgroup-headers
8887         (setq gnus-newsgroup-begin
8888               (mail-header-number (car gnus-newsgroup-headers))
8889               gnus-newsgroup-end
8890               (mail-header-number
8891                (gnus-last-element gnus-newsgroup-headers))))
8892       (setq gnus-reffed-article-number -1)
8893       ;; GROUP is successfully selected.
8894       (or gnus-newsgroup-headers t)))))
8895
8896 (defun gnus-articles-to-read (group read-all)
8897   ;; Find out what articles the user wants to read.
8898   (let* ((articles
8899           ;; Select all articles if `read-all' is non-nil, or if there
8900           ;; are no unread articles.
8901           (if (or read-all
8902                   (and (zerop (length gnus-newsgroup-marked))
8903                        (zerop (length gnus-newsgroup-unreads))))
8904               (gnus-uncompress-range (gnus-active group))
8905             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8906                           (copy-sequence gnus-newsgroup-unreads))
8907                   '<)))
8908          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8909          (scored (length scored-list))
8910          (number (length articles))
8911          (marked (+ (length gnus-newsgroup-marked)
8912                     (length gnus-newsgroup-dormant)))
8913          (select
8914           (cond
8915            ((numberp read-all)
8916             read-all)
8917            (t
8918             (condition-case ()
8919                 (cond
8920                  ((and (or (<= scored marked) (= scored number))
8921                        (numberp gnus-large-newsgroup)
8922                        (> number gnus-large-newsgroup))
8923                   (let ((input
8924                          (read-string
8925                           (format
8926                            "How many articles from %s (default %d): "
8927                            gnus-newsgroup-name number))))
8928                     (if (string-match "^[ \t]*$" input) number input)))
8929                  ((and (> scored marked) (< scored number)
8930                        (> (- scored number) 20))
8931                   (let ((input
8932                          (read-string
8933                           (format "%s %s (%d scored, %d total): "
8934                                   "How many articles from"
8935                                   group scored number))))
8936                     (if (string-match "^[ \t]*$" input)
8937                         number input)))
8938                  (t number))
8939               (quit nil))))))
8940     (setq select (if (stringp select) (string-to-number select) select))
8941     (if (or (null select) (zerop select))
8942         select
8943       (if (and (not (zerop scored)) (<= (abs select) scored))
8944           (progn
8945             (setq articles (sort scored-list '<))
8946             (setq number (length articles)))
8947         (setq articles (copy-sequence articles)))
8948
8949       (if (< (abs select) number)
8950           (if (< select 0)
8951               ;; Select the N oldest articles.
8952               (setcdr (nthcdr (1- (abs select)) articles) nil)
8953             ;; Select the N most recent articles.
8954             (setq articles (nthcdr (- number select) articles))))
8955       (setq gnus-newsgroup-unselected
8956             (gnus-sorted-intersection
8957              gnus-newsgroup-unreads
8958              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8959       articles)))
8960
8961 (defun gnus-killed-articles (killed articles)
8962   (let (out)
8963     (while articles
8964       (if (inline (gnus-member-of-range (car articles) killed))
8965           (setq out (cons (car articles) out)))
8966       (setq articles (cdr articles)))
8967     out))
8968
8969 (defun gnus-uncompress-marks (marks)
8970   "Uncompress the mark ranges in MARKS."
8971   (let ((uncompressed '(score bookmark))
8972         out)
8973     (while marks
8974       (if (memq (caar marks) uncompressed)
8975           (push (car marks) out)
8976         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8977       (setq marks (cdr marks)))
8978     out))
8979
8980 (defun gnus-adjust-marked-articles (info)
8981   "Set all article lists and remove all marks that are no longer legal."
8982   (let* ((marked-lists (gnus-info-marks info))
8983          (active (gnus-active (gnus-info-group info)))
8984          (min (car active))
8985          (max (cdr active))
8986          (types gnus-article-mark-lists)
8987          (uncompressed '(score bookmark killed))
8988          marks var articles article mark)
8989
8990     (while marked-lists
8991       (setq marks (pop marked-lists))
8992       (set (setq var (intern (format "gnus-newsgroup-%s"
8993                                      (car (rassq (setq mark (car marks))
8994                                                  types)))))
8995            (if (memq (car marks) uncompressed) (cdr marks)
8996              (gnus-uncompress-range (cdr marks))))
8997
8998       (setq articles (symbol-value var))
8999
9000       ;; All articles have to be subsets of the active articles.
9001       (cond
9002        ;; Adjust "simple" lists.
9003        ((memq mark '(tick dormant expirable reply save))
9004         (while articles
9005           (when (or (< (setq article (pop articles)) min) (> article max))
9006             (set var (delq article (symbol-value var))))))
9007        ;; Adjust assocs.
9008        ((memq mark uncompressed)
9009         (while articles
9010           (when (or (not (consp (setq article (pop articles))))
9011                     (< (car article) min)
9012                     (> (car article) max))
9013             (set var (delq article (symbol-value var))))))))))
9014
9015 (defun gnus-update-missing-marks (missing)
9016   "Go through the list of MISSING articles and remove them mark lists."
9017   (when missing
9018     (let ((types gnus-article-mark-lists)
9019           var m)
9020       ;; Go through all types.
9021       (while types
9022         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
9023         (when (symbol-value var)
9024           ;; This list has articles.  So we delete all missing articles
9025           ;; from it.
9026           (setq m missing)
9027           (while m
9028             (set var (delq (pop m) (symbol-value var)))))))))
9029
9030 (defun gnus-update-marks ()
9031   "Enter the various lists of marked articles into the newsgroup info list."
9032   (let ((types gnus-article-mark-lists)
9033         (info (gnus-get-info gnus-newsgroup-name))
9034         (uncompressed '(score bookmark killed))
9035         type list newmarked symbol)
9036     (when info
9037       ;; Add all marks lists that are non-nil to the list of marks lists.
9038       (while types
9039         (setq type (pop types))
9040         (when (setq list (symbol-value
9041                           (setq symbol
9042                                 (intern (format "gnus-newsgroup-%s"
9043                                                 (car type))))))
9044           (push (cons (cdr type)
9045                       (if (memq (cdr type) uncompressed) list
9046                         (gnus-compress-sequence 
9047                          (set symbol (sort list '<)) t)))
9048                 newmarked)))
9049
9050       ;; Enter these new marks into the info of the group.
9051       (if (nthcdr 3 info)
9052           (setcar (nthcdr 3 info) newmarked)
9053         ;; Add the marks lists to the end of the info.
9054         (when newmarked
9055           (setcdr (nthcdr 2 info) (list newmarked))))
9056
9057       ;; Cut off the end of the info if there's nothing else there.
9058       (let ((i 5))
9059         (while (and (> i 2)
9060                     (not (nth i info)))
9061           (when (nthcdr (decf i) info)
9062             (setcdr (nthcdr i info) nil)))))))
9063
9064 (defun gnus-add-marked-articles (group type articles &optional info force)
9065   ;; Add ARTICLES of TYPE to the info of GROUP.
9066   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
9067   ;; add, but replace marked articles of TYPE with ARTICLES.
9068   (let ((info (or info (gnus-get-info group)))
9069         (uncompressed '(score bookmark killed))
9070         marked m)
9071     (or (not info)
9072         (and (not (setq marked (nthcdr 3 info)))
9073              (or (null articles)
9074                  (setcdr (nthcdr 2 info)
9075                          (list (list (cons type (gnus-compress-sequence
9076                                                  articles t)))))))
9077         (and (not (setq m (assq type (car marked))))
9078              (or (null articles)
9079                  (setcar marked
9080                          (cons (cons type (gnus-compress-sequence articles t) )
9081                                (car marked)))))
9082         (if force
9083             (if (null articles)
9084                 (setcar (nthcdr 3 info)
9085                         (delq (assq type (car marked)) (car marked)))
9086               (setcdr m (gnus-compress-sequence articles t)))
9087           (setcdr m (gnus-compress-sequence
9088                      (sort (nconc (gnus-uncompress-range (cdr m))
9089                                   (copy-sequence articles)) '<) t))))))
9090
9091 (defun gnus-set-mode-line (where)
9092   "This function sets the mode line of the article or summary buffers.
9093 If WHERE is `summary', the summary mode line format will be used."
9094   ;; Is this mode line one we keep updated?
9095   (when (memq where gnus-updated-mode-lines)
9096     (let (mode-string)
9097       (save-excursion
9098         ;; We evaluate this in the summary buffer since these
9099         ;; variables are buffer-local to that buffer.
9100         (set-buffer gnus-summary-buffer)
9101         ;; We bind all these variables that are used in the `eval' form
9102         ;; below.
9103         (let* ((mformat (symbol-value
9104                          (intern
9105                           (format "gnus-%s-mode-line-format-spec" where))))
9106                (gnus-tmp-group-name gnus-newsgroup-name)
9107                (gnus-tmp-article-number (or gnus-current-article 0))
9108                (gnus-tmp-unread gnus-newsgroup-unreads)
9109                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
9110                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
9111                (gnus-tmp-unread-and-unselected
9112                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
9113                             (zerop gnus-tmp-unselected)) "")
9114                       ((zerop gnus-tmp-unselected)
9115                        (format "{%d more}" gnus-tmp-unread-and-unticked))
9116                       (t (format "{%d(+%d) more}"
9117                                  gnus-tmp-unread-and-unticked
9118                                  gnus-tmp-unselected))))
9119                (gnus-tmp-subject
9120                 (if (and gnus-current-headers
9121                          (vectorp gnus-current-headers))
9122                     (gnus-mode-string-quote
9123                      (mail-header-subject gnus-current-headers)) ""))
9124                max-len
9125                gnus-tmp-header);; passed as argument to any user-format-funcs
9126           (setq mode-string (eval mformat))
9127           (setq max-len (max 4 (if gnus-mode-non-string-length
9128                                    (- (window-width)
9129                                       gnus-mode-non-string-length)
9130                                  (length mode-string))))
9131           ;; We might have to chop a bit of the string off...
9132           (when (> (length mode-string) max-len)
9133             (setq mode-string
9134                   (concat (gnus-truncate-string mode-string (- max-len 3))
9135                           "...")))
9136           ;; Pad the mode string a bit.
9137           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
9138       ;; Update the mode line.
9139       (setq mode-line-buffer-identification 
9140             (gnus-mode-line-buffer-identification
9141              (list mode-string)))
9142       (set-buffer-modified-p t))))
9143
9144 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
9145   "Go through the HEADERS list and add all Xrefs to a hash table.
9146 The resulting hash table is returned, or nil if no Xrefs were found."
9147   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9148          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9149          (xref-hashtb (make-vector 63 0))
9150          start group entry number xrefs header)
9151     (while headers
9152       (setq header (pop headers))
9153       (when (and (setq xrefs (mail-header-xref header))
9154                  (not (memq (setq number (mail-header-number header))
9155                             unreads)))
9156         (setq start 0)
9157         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9158           (setq start (match-end 0))
9159           (setq group (if prefix
9160                           (concat prefix (substring xrefs (match-beginning 1)
9161                                                     (match-end 1)))
9162                         (substring xrefs (match-beginning 1) (match-end 1))))
9163           (setq number
9164                 (string-to-int (substring xrefs (match-beginning 2)
9165                                           (match-end 2))))
9166           (if (setq entry (gnus-gethash group xref-hashtb))
9167               (setcdr entry (cons number (cdr entry)))
9168             (gnus-sethash group (cons number nil) xref-hashtb)))))
9169     (and start xref-hashtb)))
9170
9171 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9172   "Look through all the headers and mark the Xrefs as read."
9173   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9174         name entry info xref-hashtb idlist method nth4)
9175     (save-excursion
9176       (set-buffer gnus-group-buffer)
9177       (when (setq xref-hashtb
9178                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9179         (mapatoms
9180          (lambda (group)
9181            (unless (string= from-newsgroup (setq name (symbol-name group)))
9182              (setq idlist (symbol-value group))
9183              ;; Dead groups are not updated.
9184              (and (prog1
9185                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9186                             info (nth 2 entry))
9187                     (if (stringp (setq nth4 (gnus-info-method info)))
9188                         (setq nth4 (gnus-server-to-method nth4))))
9189                   ;; Only do the xrefs if the group has the same
9190                   ;; select method as the group we have just read.
9191                   (or (gnus-methods-equal-p
9192                        nth4 (gnus-find-method-for-group from-newsgroup))
9193                       virtual
9194                       (equal nth4 (setq method (gnus-find-method-for-group
9195                                                 from-newsgroup)))
9196                       (and (equal (car nth4) (car method))
9197                            (equal (nth 1 nth4) (nth 1 method))))
9198                   gnus-use-cross-reference
9199                   (or (not (eq gnus-use-cross-reference t))
9200                       virtual
9201                       ;; Only do cross-references on subscribed
9202                       ;; groups, if that is what is wanted.
9203                       (<= (gnus-info-level info) gnus-level-subscribed))
9204                   (gnus-group-make-articles-read name idlist))))
9205          xref-hashtb)))))
9206
9207 (defun gnus-group-make-articles-read (group articles)
9208   (let* ((num 0)
9209          (entry (gnus-gethash group gnus-newsrc-hashtb))
9210          (info (nth 2 entry))
9211          (active (gnus-active group))
9212          range)
9213     ;; First peel off all illegal article numbers.
9214     (if active
9215         (let ((ids articles)
9216               id first)
9217           (while ids
9218             (setq id (car ids))
9219             (if (and first (> id (cdr active)))
9220                 (progn
9221                   ;; We'll end up in this situation in one particular
9222                   ;; obscure situation.  If you re-scan a group and get
9223                   ;; a new article that is cross-posted to a different
9224                   ;; group that has not been re-scanned, you might get
9225                   ;; crossposted article that has a higher number than
9226                   ;; Gnus believes possible.  So we re-activate this
9227                   ;; group as well.  This might mean doing the
9228                   ;; crossposting thingy will *increase* the number
9229                   ;; of articles in some groups.  Tsk, tsk.
9230                   (setq active (or (gnus-activate-group group) active))))
9231             (if (or (> id (cdr active))
9232                     (< id (car active)))
9233                 (setq articles (delq id articles)))
9234             (setq ids (cdr ids)))))
9235     ;; If the read list is nil, we init it.
9236     (and active
9237          (null (gnus-info-read info))
9238          (> (car active) 1)
9239          (gnus-info-set-read info (cons 1 (1- (car active)))))
9240     ;; Then we add the read articles to the range.
9241     (gnus-info-set-read
9242      info
9243      (setq range
9244            (gnus-add-to-range
9245             (gnus-info-read info) (setq articles (sort articles '<)))))
9246     ;; Then we have to re-compute how many unread
9247     ;; articles there are in this group.
9248     (if active
9249         (progn
9250           (cond
9251            ((not range)
9252             (setq num (- (1+ (cdr active)) (car active))))
9253            ((not (listp (cdr range)))
9254             (setq num (- (cdr active) (- (1+ (cdr range))
9255                                          (car range)))))
9256            (t
9257             (while range
9258               (if (numberp (car range))
9259                   (setq num (1+ num))
9260                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9261               (setq range (cdr range)))
9262             (setq num (- (cdr active) num))))
9263           ;; Update the number of unread articles.
9264           (setcar entry num)
9265           ;; Update the group buffer.
9266           (gnus-group-update-group group t)))))
9267
9268 (defun gnus-methods-equal-p (m1 m2)
9269   (let ((m1 (or m1 gnus-select-method))
9270         (m2 (or m2 gnus-select-method)))
9271     (or (equal m1 m2)
9272         (and (eq (car m1) (car m2))
9273              (or (not (memq 'address (assoc (symbol-name (car m1))
9274                                             gnus-valid-select-methods)))
9275                  (equal (nth 1 m1) (nth 1 m2)))))))
9276
9277 (defsubst gnus-header-value ()
9278   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9279
9280 (defvar gnus-newsgroup-none-id 0)
9281
9282 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9283   (let ((cur nntp-server-buffer)
9284         (dependencies
9285          (or dependencies
9286              (save-excursion (set-buffer gnus-summary-buffer)
9287                              gnus-newsgroup-dependencies)))
9288         headers id id-dep ref-dep end ref)
9289     (save-excursion
9290       (set-buffer nntp-server-buffer)
9291       (run-hooks 'gnus-parse-headers-hook)
9292       (let ((case-fold-search t)
9293             in-reply-to header p lines)
9294         (goto-char (point-min))
9295         ;; Search to the beginning of the next header.  Error messages
9296         ;; do not begin with 2 or 3.
9297         (while (re-search-forward "^[23][0-9]+ " nil t)
9298           (setq id nil
9299                 ref nil)
9300           ;; This implementation of this function, with nine
9301           ;; search-forwards instead of the one re-search-forward and
9302           ;; a case (which basically was the old function) is actually
9303           ;; about twice as fast, even though it looks messier.  You
9304           ;; can't have everything, I guess.  Speed and elegance
9305           ;; doesn't always go hand in hand.
9306           (setq
9307            header
9308            (vector
9309             ;; Number.
9310             (prog1
9311                 (read cur)
9312               (end-of-line)
9313               (setq p (point))
9314               (narrow-to-region (point)
9315                                 (or (and (search-forward "\n.\n" nil t)
9316                                          (- (point) 2))
9317                                     (point))))
9318             ;; Subject.
9319             (progn
9320               (goto-char p)
9321               (if (search-forward "\nsubject: " nil t)
9322                   (gnus-header-value) "(none)"))
9323             ;; From.
9324             (progn
9325               (goto-char p)
9326               (if (search-forward "\nfrom: " nil t)
9327                   (gnus-header-value) "(nobody)"))
9328             ;; Date.
9329             (progn
9330               (goto-char p)
9331               (if (search-forward "\ndate: " nil t)
9332                   (gnus-header-value) ""))
9333             ;; Message-ID.
9334             (progn
9335               (goto-char p)
9336               (if (search-forward "\nmessage-id: " nil t)
9337                   (setq id (gnus-header-value))
9338                 ;; If there was no message-id, we just fake one to make
9339                 ;; subsequent routines simpler.
9340                 (setq id (concat "none+"
9341                                  (int-to-string
9342                                   (setq gnus-newsgroup-none-id
9343                                         (1+ gnus-newsgroup-none-id)))))))
9344             ;; References.
9345             (progn
9346               (goto-char p)
9347               (if (search-forward "\nreferences: " nil t)
9348                   (progn
9349                     (setq end (point))
9350                     (prog1
9351                         (gnus-header-value)
9352                       (setq ref
9353                             (buffer-substring
9354                              (progn
9355                                (end-of-line)
9356                                (search-backward ">" end t)
9357                                (1+ (point)))
9358                              (progn
9359                                (search-backward "<" end t)
9360                                (point))))))
9361                 ;; Get the references from the in-reply-to header if there
9362                 ;; were no references and the in-reply-to header looks
9363                 ;; promising.
9364                 (if (and (search-forward "\nin-reply-to: " nil t)
9365                          (setq in-reply-to (gnus-header-value))
9366                          (string-match "<[^>]+>" in-reply-to))
9367                     (setq ref (substring in-reply-to (match-beginning 0)
9368                                          (match-end 0)))
9369                   (setq ref ""))))
9370             ;; Chars.
9371             0
9372             ;; Lines.
9373             (progn
9374               (goto-char p)
9375               (if (search-forward "\nlines: " nil t)
9376                   (if (numberp (setq lines (read cur)))
9377                       lines 0)
9378                 0))
9379             ;; Xref.
9380             (progn
9381               (goto-char p)
9382               (and (search-forward "\nxref: " nil t)
9383                    (gnus-header-value)))))
9384           ;; We do the threading while we read the headers.  The
9385           ;; message-id and the last reference are both entered into
9386           ;; the same hash table.  Some tippy-toeing around has to be
9387           ;; done in case an article has arrived before the article
9388           ;; which it refers to.
9389           (if (boundp (setq id-dep (intern id dependencies)))
9390               (if (and (car (symbol-value id-dep))
9391                        (not force-new))
9392                   ;; An article with this Message-ID has already
9393                   ;; been seen, so we ignore this one, except we add
9394                   ;; any additional Xrefs (in case the two articles
9395                   ;; came from different servers).
9396                   (progn
9397                     (mail-header-set-xref
9398                      (car (symbol-value id-dep))
9399                      (concat (or (mail-header-xref
9400                                   (car (symbol-value id-dep))) "")
9401                              (or (mail-header-xref header) "")))
9402                     (setq header nil))
9403                 (setcar (symbol-value id-dep) header))
9404             (set id-dep (list header)))
9405           (when header
9406             (if (boundp (setq ref-dep (intern ref dependencies)))
9407                 (setcdr (symbol-value ref-dep)
9408                         (nconc (cdr (symbol-value ref-dep))
9409                                (list (symbol-value id-dep))))
9410               (set ref-dep (list nil (symbol-value id-dep))))
9411             (setq headers (cons header headers)))
9412           (goto-char (point-max))
9413           (widen))
9414         (nreverse headers)))))
9415
9416 ;; The following macros and functions were written by Felix Lee
9417 ;; <flee@cse.psu.edu>.
9418
9419 (defmacro gnus-nov-read-integer ()
9420   '(prog1
9421        (if (= (following-char) ?\t)
9422            0
9423          (let ((num (condition-case nil (read buffer) (error nil))))
9424            (if (numberp num) num 0)))
9425      (or (eobp) (forward-char 1))))
9426
9427 (defmacro gnus-nov-skip-field ()
9428   '(search-forward "\t" eol 'move))
9429
9430 (defmacro gnus-nov-field ()
9431   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9432
9433 ;; Goes through the xover lines and returns a list of vectors
9434 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9435                                                   force-new dependencies)
9436   "Parse the news overview data in the server buffer, and return a
9437 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9438   ;; Get the Xref when the users reads the articles since most/some
9439   ;; NNTP servers do not include Xrefs when using XOVER.
9440   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9441   (let ((cur nntp-server-buffer)
9442         (dependencies (or dependencies gnus-newsgroup-dependencies))
9443         number headers header)
9444     (save-excursion
9445       (set-buffer nntp-server-buffer)
9446       ;; Allow the user to mangle the headers before parsing them.
9447       (run-hooks 'gnus-parse-headers-hook)
9448       (goto-char (point-min))
9449       (while (and sequence (not (eobp)))
9450         (setq number (read cur))
9451         (while (and sequence (< (car sequence) number))
9452           (setq sequence (cdr sequence)))
9453         (and sequence
9454              (eq number (car sequence))
9455              (progn
9456                (setq sequence (cdr sequence))
9457                (if (setq header
9458                          (inline (gnus-nov-parse-line
9459                                   number dependencies force-new)))
9460                    (setq headers (cons header headers)))))
9461         (forward-line 1))
9462       (setq headers (nreverse headers)))
9463     headers))
9464
9465 ;; This function has to be called with point after the article number
9466 ;; on the beginning of the line.
9467 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9468   (let ((none 0)
9469         (eol (gnus-point-at-eol))
9470         (buffer (current-buffer))
9471         header ref id id-dep ref-dep)
9472
9473     ;; overview: [num subject from date id refs chars lines misc]
9474     (narrow-to-region (point) eol)
9475     (or (eobp) (forward-char))
9476
9477     (condition-case nil
9478         (setq header
9479               (vector
9480                number                   ; number
9481                (gnus-nov-field)         ; subject
9482                (gnus-nov-field)         ; from
9483                (gnus-nov-field)         ; date
9484                (setq id (or (gnus-nov-field)
9485                             (concat "none+"
9486                                     (int-to-string
9487                                      (setq none (1+ none)))))) ; id
9488                (progn
9489                  (save-excursion
9490                    (let ((beg (point)))
9491                      (search-forward "\t" eol)
9492                      (if (search-backward ">" beg t)
9493                          (setq ref
9494                                (buffer-substring
9495                                 (1+ (point))
9496                                 (search-backward "<" beg t)))
9497                        (setq ref nil))))
9498                  (gnus-nov-field))      ; refs
9499                (gnus-nov-read-integer)  ; chars
9500                (gnus-nov-read-integer)  ; lines
9501                (if (= (following-char) ?\n)
9502                    nil
9503                  (gnus-nov-field))      ; misc
9504                ))
9505       (error (progn
9506                (gnus-error 4 "Strange nov line")
9507                (setq header nil)
9508                (goto-char eol))))
9509
9510     (widen)
9511
9512     ;; We build the thread tree.
9513     (when header
9514       (if (boundp (setq id-dep (intern id dependencies)))
9515           (if (and (car (symbol-value id-dep))
9516                    (not force-new))
9517               ;; An article with this Message-ID has already been seen,
9518               ;; so we ignore this one, except we add any additional
9519               ;; Xrefs (in case the two articles came from different
9520               ;; servers.
9521               (progn
9522                 (mail-header-set-xref
9523                  (car (symbol-value id-dep))
9524                  (concat (or (mail-header-xref
9525                               (car (symbol-value id-dep))) "")
9526                          (or (mail-header-xref header) "")))
9527                 (setq header nil))
9528             (setcar (symbol-value id-dep) header))
9529         (set id-dep (list header))))
9530     (when header
9531       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9532           (setcdr (symbol-value ref-dep)
9533                   (nconc (cdr (symbol-value ref-dep))
9534                          (list (symbol-value id-dep))))
9535         (set ref-dep (list nil (symbol-value id-dep)))))
9536     header))
9537
9538 (defun gnus-article-get-xrefs ()
9539   "Fill in the Xref value in `gnus-current-headers', if necessary.
9540 This is meant to be called in `gnus-article-internal-prepare-hook'."
9541   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9542                                  gnus-current-headers)))
9543     (or (not gnus-use-cross-reference)
9544         (not headers)
9545         (and (mail-header-xref headers)
9546              (not (string= (mail-header-xref headers) "")))
9547         (let ((case-fold-search t)
9548               xref)
9549           (save-restriction
9550             (nnheader-narrow-to-headers)
9551             (goto-char (point-min))
9552             (if (or (and (eq (downcase (following-char)) ?x)
9553                          (looking-at "Xref:"))
9554                     (search-forward "\nXref:" nil t))
9555                 (progn
9556                   (goto-char (1+ (match-end 0)))
9557                   (setq xref (buffer-substring (point)
9558                                                (progn (end-of-line) (point))))
9559                   (mail-header-set-xref headers xref))))))))
9560
9561 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
9562   "Find article ID and insert the summary line for that article."
9563   (let ((header (if (and old-header use-old-header)
9564                     old-header (gnus-read-header id)))
9565         (number (and (numberp id) id))
9566         pos)
9567     (when header
9568       ;; Rebuild the thread that this article is part of and go to the
9569       ;; article we have fetched.
9570       (when (and (not gnus-show-threads)
9571                  old-header)
9572         (when (setq pos (text-property-any
9573                          (point-min) (point-max) 'gnus-number 
9574                          (mail-header-number old-header)))
9575           (goto-char pos)
9576           (gnus-delete-line)
9577           (gnus-data-remove (mail-header-number old-header))))
9578       (when old-header
9579         (mail-header-set-number header (mail-header-number old-header)))
9580       (setq gnus-newsgroup-sparse
9581             (delq (setq number (mail-header-number header)) 
9582                   gnus-newsgroup-sparse))
9583       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
9584       (gnus-rebuild-thread (mail-header-id header))
9585       (gnus-summary-goto-subject number nil t))
9586     (when (and (numberp number)
9587                (> number 0))
9588       ;; We have to update the boundaries even if we can't fetch the
9589       ;; article if ID is a number -- so that the next `P' or `N'
9590       ;; command will fetch the previous (or next) article even
9591       ;; if the one we tried to fetch this time has been canceled.
9592       (and (> number gnus-newsgroup-end)
9593            (setq gnus-newsgroup-end number))
9594       (and (< number gnus-newsgroup-begin)
9595            (setq gnus-newsgroup-begin number))
9596       (setq gnus-newsgroup-unselected
9597             (delq number gnus-newsgroup-unselected)))
9598     ;; Report back a success?
9599     (and header (mail-header-number header))))
9600
9601 (defun gnus-summary-work-articles (n)
9602   "Return a list of articles to be worked upon.  The prefix argument,
9603 the list of process marked articles, and the current article will be
9604 taken into consideration."
9605   (cond
9606    (n
9607     ;; A numerical prefix has been given.
9608     (let ((backward (< n 0))
9609           (n (abs (prefix-numeric-value n)))
9610           articles article)
9611       (save-excursion
9612         (while
9613             (and (> n 0)
9614                  (push (setq article (gnus-summary-article-number))
9615                        articles)
9616                  (if backward
9617                      (gnus-summary-find-prev nil article)
9618                    (gnus-summary-find-next nil article)))
9619           (decf n)))
9620       (nreverse articles)))
9621    ((and (boundp 'transient-mark-mode)
9622          transient-mark-mode
9623          mark-active)
9624     ;; Work on the region between point and mark.
9625     (let ((max (max (point) (mark)))
9626           articles article)
9627       (save-excursion
9628         (goto-char (min (point) (mark)))
9629         (while
9630             (and
9631              (push (setq article (gnus-summary-article-number)) articles)
9632              (gnus-summary-find-next nil article)
9633              (< (point) max)))
9634         (nreverse articles))))
9635    (gnus-newsgroup-processable
9636     ;; There are process-marked articles present.
9637     (reverse gnus-newsgroup-processable))
9638    (t
9639     ;; Just return the current article.
9640     (list (gnus-summary-article-number)))))
9641
9642 (defun gnus-summary-search-group (&optional backward use-level)
9643   "Search for next unread newsgroup.
9644 If optional argument BACKWARD is non-nil, search backward instead."
9645   (save-excursion
9646     (set-buffer gnus-group-buffer)
9647     (if (gnus-group-search-forward
9648          backward nil (if use-level (gnus-group-group-level) nil))
9649         (gnus-group-group-name))))
9650
9651 (defun gnus-summary-best-group (&optional exclude-group)
9652   "Find the name of the best unread group.
9653 If EXCLUDE-GROUP, do not go to this group."
9654   (save-excursion
9655     (set-buffer gnus-group-buffer)
9656     (save-excursion
9657       (gnus-group-best-unread-group exclude-group))))
9658
9659 (defun gnus-summary-find-next (&optional unread article backward)
9660   (if backward (gnus-summary-find-prev)
9661     (let* ((dummy (gnus-summary-article-intangible-p))
9662            (article (or article (gnus-summary-article-number)))
9663            (arts (gnus-data-find-list article))
9664            result)
9665       (when (and (not dummy)
9666                  (or (not gnus-summary-check-current)
9667                      (not unread)
9668                      (not (gnus-data-unread-p (car arts)))))
9669         (setq arts (cdr arts)))
9670       (when (setq result
9671                   (if unread
9672                       (progn
9673                         (while arts
9674                           (when (gnus-data-unread-p (car arts))
9675                             (setq result (car arts)
9676                                   arts nil))
9677                           (setq arts (cdr arts)))
9678                         result)
9679                     (car arts)))
9680         (goto-char (gnus-data-pos result))
9681         (gnus-data-number result)))))
9682
9683 (defun gnus-summary-find-prev (&optional unread article)
9684   (let* ((eobp (eobp))
9685          (article (or article (gnus-summary-article-number)))
9686          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9687          result)
9688     (when (and (not eobp)
9689                (or (not gnus-summary-check-current)
9690                    (not unread)
9691                    (not (gnus-data-unread-p (car arts)))))
9692       (setq arts (cdr arts)))
9693     (if (setq result
9694               (if unread
9695                   (progn
9696                     (while arts
9697                       (and (gnus-data-unread-p (car arts))
9698                            (setq result (car arts)
9699                                  arts nil))
9700                       (setq arts (cdr arts)))
9701                     result)
9702                 (car arts)))
9703         (progn
9704           (goto-char (gnus-data-pos result))
9705           (gnus-data-number result)))))
9706
9707 (defun gnus-summary-find-subject (subject &optional unread backward article)
9708   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9709          (article (or article (gnus-summary-article-number)))
9710          (articles (gnus-data-list backward))
9711          (arts (gnus-data-find-list article articles))
9712          result)
9713     (when (or (not gnus-summary-check-current)
9714               (not unread)
9715               (not (gnus-data-unread-p (car arts))))
9716       (setq arts (cdr arts)))
9717     (while arts
9718       (and (or (not unread)
9719                (gnus-data-unread-p (car arts)))
9720            (vectorp (gnus-data-header (car arts)))
9721            (gnus-subject-equal
9722             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9723            (setq result (car arts)
9724                  arts nil))
9725       (setq arts (cdr arts)))
9726     (and result
9727          (goto-char (gnus-data-pos result))
9728          (gnus-data-number result))))
9729
9730 (defun gnus-summary-search-forward (&optional unread subject backward)
9731   "Search forward for an article.
9732 If UNREAD, look for unread articles.  If SUBJECT, look for
9733 articles with that subject.  If BACKWARD, search backward instead."
9734   (cond (subject (gnus-summary-find-subject subject unread backward))
9735         (backward (gnus-summary-find-prev unread))
9736         (t (gnus-summary-find-next unread))))
9737
9738 (defun gnus-recenter (&optional n)
9739   "Center point in window and redisplay frame.
9740 Also do horizontal recentering."
9741   (interactive "P")
9742   (when (and gnus-auto-center-summary
9743              (not (eq gnus-auto-center-summary 'vertical)))
9744     (gnus-horizontal-recenter))
9745   (recenter n))
9746
9747 (defun gnus-summary-recenter ()
9748   "Center point in the summary window.
9749 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9750 displayed, no centering will be performed."
9751   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9752   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9753   (let* ((top (cond ((< (window-height) 4) 0)
9754                     ((< (window-height) 7) 1)
9755                     (t 2)))
9756          (height (1- (window-height)))
9757          (bottom (save-excursion (goto-char (point-max))
9758                                  (forward-line (- height))
9759                                  (point)))
9760          (window (get-buffer-window (current-buffer))))
9761     ;; The user has to want it.
9762     (when gnus-auto-center-summary
9763       (when (get-buffer-window gnus-article-buffer)
9764        ;; Only do recentering when the article buffer is displayed,
9765        ;; Set the window start to either `bottom', which is the biggest
9766        ;; possible valid number, or the second line from the top,
9767        ;; whichever is the least.
9768        (set-window-start
9769         window (min bottom (save-excursion 
9770                              (forward-line (- top)) (point)))))
9771       ;; Do horizontal recentering while we're at it.
9772       (when (and (get-buffer-window (current-buffer) t)
9773                  (not (eq gnus-auto-center-summary 'vertical)))
9774         (let ((selected (selected-window)))
9775           (select-window (get-buffer-window (current-buffer) t))
9776           (gnus-summary-position-point)
9777           (gnus-horizontal-recenter)
9778           (select-window selected))))))
9779
9780 (defun gnus-horizontal-recenter ()
9781   "Recenter the current buffer horizontally."
9782   (if (< (current-column) (/ (window-width) 2))
9783       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9784     (let* ((orig (point))
9785            (end (window-end (get-buffer-window (current-buffer) t)))
9786            (max 0))
9787       ;; Find the longest line currently displayed in the window.
9788       (goto-char (window-start))
9789       (while (and (not (eobp)) 
9790                   (< (point) end))
9791         (end-of-line)
9792         (setq max (max max (current-column)))
9793         (forward-line 1))
9794       (goto-char orig)
9795       ;; Scroll horizontally to center (sort of) the point.
9796       (if (> max (window-width))
9797           (set-window-hscroll 
9798            (get-buffer-window (current-buffer) t)
9799            (min (- (current-column) (/ (window-width) 3))
9800                 (+ 2 (- max (window-width)))))
9801         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9802       max)))
9803
9804 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9805 (defun gnus-short-group-name (group &optional levels)
9806   "Collapse GROUP name LEVELS."
9807   (let* ((name "") 
9808          (foreign "")
9809          (depth 0) 
9810          (skip 1)
9811          (levels (or levels
9812                      (progn
9813                        (while (string-match "\\." group skip)
9814                          (setq skip (match-end 0)
9815                                depth (+ depth 1)))
9816                        depth))))
9817     (if (string-match ":" group)
9818         (setq foreign (substring group 0 (match-end 0))
9819               group (substring group (match-end 0))))
9820     (while group
9821       (if (and (string-match "\\." group)
9822                (> levels (- gnus-group-uncollapsed-levels 1)))
9823           (setq name (concat name (substring group 0 1))
9824                 group (substring group (match-end 0))
9825                 levels (- levels 1)
9826                 name (concat name "."))
9827         (setq name (concat foreign name group)
9828               group nil)))
9829     name))
9830
9831 (defun gnus-summary-jump-to-group (newsgroup)
9832   "Move point to NEWSGROUP in group mode buffer."
9833   ;; Keep update point of group mode buffer if visible.
9834   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9835       (save-window-excursion
9836         ;; Take care of tree window mode.
9837         (if (get-buffer-window gnus-group-buffer)
9838             (pop-to-buffer gnus-group-buffer))
9839         (gnus-group-jump-to-group newsgroup))
9840     (save-excursion
9841       ;; Take care of tree window mode.
9842       (if (get-buffer-window gnus-group-buffer)
9843           (pop-to-buffer gnus-group-buffer)
9844         (set-buffer gnus-group-buffer))
9845       (gnus-group-jump-to-group newsgroup))))
9846
9847 ;; This function returns a list of article numbers based on the
9848 ;; difference between the ranges of read articles in this group and
9849 ;; the range of active articles.
9850 (defun gnus-list-of-unread-articles (group)
9851   (let* ((read (gnus-info-read (gnus-get-info group)))
9852          (active (gnus-active group))
9853          (last (cdr active))
9854          first nlast unread)
9855     ;; If none are read, then all are unread.
9856     (if (not read)
9857         (setq first (car active))
9858       ;; If the range of read articles is a single range, then the
9859       ;; first unread article is the article after the last read
9860       ;; article.  Sounds logical, doesn't it?
9861       (if (not (listp (cdr read)))
9862           (setq first (1+ (cdr read)))
9863         ;; `read' is a list of ranges.
9864         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9865                                 (caar read))) 1)
9866             (setq first 1))
9867         (while read
9868           (if first
9869               (while (< first nlast)
9870                 (setq unread (cons first unread))
9871                 (setq first (1+ first))))
9872           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9873           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9874           (setq read (cdr read)))))
9875     ;; And add the last unread articles.
9876     (while (<= first last)
9877       (setq unread (cons first unread))
9878       (setq first (1+ first)))
9879     ;; Return the list of unread articles.
9880     (nreverse unread)))
9881
9882 (defun gnus-list-of-read-articles (group)
9883   "Return a list of unread, unticked and non-dormant articles."
9884   (let* ((info (gnus-get-info group))
9885          (marked (gnus-info-marks info))
9886          (active (gnus-active group)))
9887     (and info active
9888          (gnus-set-difference
9889           (gnus-sorted-complement
9890            (gnus-uncompress-range active)
9891            (gnus-list-of-unread-articles group))
9892           (append
9893            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9894            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9895
9896 ;; Various summary commands
9897
9898 (defun gnus-summary-universal-argument (arg)
9899   "Perform any operation on all articles that are process/prefixed."
9900   (interactive "P")
9901   (gnus-set-global-variables)
9902   (let ((articles (gnus-summary-work-articles arg))
9903         func article)
9904     (if (eq
9905          (setq
9906           func
9907           (key-binding
9908            (read-key-sequence
9909             (substitute-command-keys
9910              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9911              ))))
9912          'undefined)
9913         (gnus-error 1 "Undefined key")
9914       (save-excursion
9915         (while articles
9916           (gnus-summary-goto-subject (setq article (pop articles)))
9917           (command-execute func)
9918           (gnus-summary-remove-process-mark article)))))
9919   (gnus-summary-position-point))
9920
9921 (defun gnus-summary-toggle-truncation (&optional arg)
9922   "Toggle truncation of summary lines.
9923 With arg, turn line truncation on iff arg is positive."
9924   (interactive "P")
9925   (setq truncate-lines
9926         (if (null arg) (not truncate-lines)
9927           (> (prefix-numeric-value arg) 0)))
9928   (redraw-display))
9929
9930 (defun gnus-summary-reselect-current-group (&optional all rescan)
9931   "Exit and then reselect the current newsgroup.
9932 The prefix argument ALL means to select all articles."
9933   (interactive "P")
9934   (gnus-set-global-variables)
9935   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
9936     (error "Ephemeral groups can't be reselected"))
9937   (let ((current-subject (gnus-summary-article-number))
9938         (group gnus-newsgroup-name))
9939     (setq gnus-newsgroup-begin nil)
9940     (gnus-summary-exit)
9941     ;; We have to adjust the point of group mode buffer because the
9942     ;; current point was moved to the next unread newsgroup by
9943     ;; exiting.
9944     (gnus-summary-jump-to-group group)
9945     (when rescan
9946       (save-excursion
9947         (gnus-group-get-new-news-this-group 1)))
9948     (gnus-group-read-group all t)
9949     (gnus-summary-goto-subject current-subject nil t)))
9950
9951 (defun gnus-summary-rescan-group (&optional all)
9952   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9953   (interactive "P")
9954   (gnus-summary-reselect-current-group all t))
9955
9956 (defun gnus-summary-update-info ()
9957   (let* ((group gnus-newsgroup-name))
9958     (when gnus-newsgroup-kill-headers
9959       (setq gnus-newsgroup-killed
9960             (gnus-compress-sequence
9961              (nconc
9962               (gnus-set-sorted-intersection
9963                (gnus-uncompress-range gnus-newsgroup-killed)
9964                (setq gnus-newsgroup-unselected
9965                      (sort gnus-newsgroup-unselected '<)))
9966               (setq gnus-newsgroup-unreads
9967                     (sort gnus-newsgroup-unreads '<))) t)))
9968     (unless (listp (cdr gnus-newsgroup-killed))
9969       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9970     (let ((headers gnus-newsgroup-headers))
9971       (run-hooks 'gnus-exit-group-hook)
9972       (unless gnus-save-score
9973         (setq gnus-newsgroup-scored nil))
9974       ;; Set the new ranges of read articles.
9975       (gnus-update-read-articles
9976        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9977       ;; Set the current article marks.
9978       (gnus-update-marks)
9979       ;; Do the cross-ref thing.
9980       (when gnus-use-cross-reference
9981         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9982       ;; Do adaptive scoring, and possibly save score files.
9983       (when gnus-newsgroup-adaptive
9984         (gnus-score-adaptive))
9985       (when gnus-use-scoring
9986         (gnus-score-save))
9987       ;; Do not switch windows but change the buffer to work.
9988       (set-buffer gnus-group-buffer)
9989       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9990           (gnus-group-update-group group)))))
9991
9992 (defun gnus-summary-exit (&optional temporary)
9993   "Exit reading current newsgroup, and then return to group selection mode.
9994 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9995   (interactive)
9996   (gnus-set-global-variables)
9997   (gnus-kill-save-kill-buffer)
9998   (let* ((group gnus-newsgroup-name)
9999          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
10000          (mode major-mode)
10001          (buf (current-buffer)))
10002     (run-hooks 'gnus-summary-prepare-exit-hook)
10003     ;; If we have several article buffers, we kill them at exit.
10004     (unless gnus-single-article-buffer
10005       (gnus-kill-buffer gnus-original-article-buffer)
10006       (setq gnus-article-current nil))
10007     (when gnus-use-cache
10008       (gnus-cache-possibly-remove-articles)
10009       (gnus-cache-save-buffers))
10010     (when gnus-use-trees
10011       (gnus-tree-close group))
10012     ;; Make all changes in this group permanent.
10013     (unless quit-config
10014       (gnus-summary-update-info))
10015     (gnus-close-group group)
10016     ;; Make sure where I was, and go to next newsgroup.
10017     (set-buffer gnus-group-buffer)
10018     (unless quit-config
10019       (gnus-group-jump-to-group group))
10020     (run-hooks 'gnus-summary-exit-hook)
10021     (unless quit-config
10022       (gnus-group-next-unread-group 1))
10023     (if temporary
10024         nil                             ;Nothing to do.
10025       ;; If we have several article buffers, we kill them at exit.
10026       (unless gnus-single-article-buffer
10027         (gnus-kill-buffer gnus-article-buffer)
10028         (gnus-kill-buffer gnus-original-article-buffer)
10029         (setq gnus-article-current nil))
10030       (set-buffer buf)
10031       (if (not gnus-kill-summary-on-exit)
10032           (gnus-deaden-summary)
10033         ;; We set all buffer-local variables to nil.  It is unclear why
10034         ;; this is needed, but if we don't, buffer-local variables are
10035         ;; not garbage-collected, it seems.  This would the lead to en
10036         ;; ever-growing Emacs.
10037         (gnus-summary-clear-local-variables)
10038         (when (get-buffer gnus-article-buffer)
10039           (bury-buffer gnus-article-buffer))
10040         ;; We clear the global counterparts of the buffer-local
10041         ;; variables as well, just to be on the safe side.
10042         (gnus-configure-windows 'group 'force)
10043         (gnus-summary-clear-local-variables)
10044         ;; Return to group mode buffer.
10045         (if (eq mode 'gnus-summary-mode)
10046             (gnus-kill-buffer buf)))
10047       (setq gnus-current-select-method gnus-select-method)
10048       (pop-to-buffer gnus-group-buffer)
10049       ;; Clear the current group name.
10050       (if (not quit-config)
10051           (progn
10052             (gnus-group-jump-to-group group)
10053             (gnus-group-next-unread-group 1)
10054             (gnus-configure-windows 'group 'force))
10055         (if (not (buffer-name (car quit-config)))
10056             (gnus-configure-windows 'group 'force)
10057           (set-buffer (car quit-config))
10058           (and (eq major-mode 'gnus-summary-mode)
10059                (gnus-set-global-variables))
10060           (gnus-configure-windows (cdr quit-config))))
10061       (unless quit-config
10062         (setq gnus-newsgroup-name nil)))))
10063
10064 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
10065 (defun gnus-summary-exit-no-update (&optional no-questions)
10066   "Quit reading current newsgroup without updating read article info."
10067   (interactive)
10068   (gnus-set-global-variables)
10069   (let* ((group gnus-newsgroup-name)
10070          (quit-config (gnus-group-quit-config group)))
10071     (when (or no-questions
10072               gnus-expert-user
10073               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
10074       ;; If we have several article buffers, we kill them at exit.
10075       (unless gnus-single-article-buffer
10076         (gnus-kill-buffer gnus-article-buffer)
10077         (gnus-kill-buffer gnus-original-article-buffer)
10078         (setq gnus-article-current nil))
10079       (if (not gnus-kill-summary-on-exit)
10080           (gnus-deaden-summary)
10081         (gnus-close-group group)
10082         (gnus-summary-clear-local-variables)
10083         (set-buffer gnus-group-buffer)
10084         (gnus-summary-clear-local-variables)
10085         (when (get-buffer gnus-summary-buffer)
10086           (kill-buffer gnus-summary-buffer)))
10087       (unless gnus-single-article-buffer
10088         (setq gnus-article-current nil))
10089       (when gnus-use-trees
10090         (gnus-tree-close group))
10091       (when (get-buffer gnus-article-buffer)
10092         (bury-buffer gnus-article-buffer))
10093       ;; Return to the group buffer.
10094       (gnus-configure-windows 'group 'force)
10095       ;; Clear the current group name.
10096       (setq gnus-newsgroup-name nil)
10097       (when (equal (gnus-group-group-name) group)
10098         (gnus-group-next-unread-group 1))
10099       (when quit-config
10100         (if (not (buffer-name (car quit-config)))
10101             (gnus-configure-windows 'group 'force)
10102           (set-buffer (car quit-config))
10103           (when (eq major-mode 'gnus-summary-mode)
10104             (gnus-set-global-variables))
10105           (gnus-configure-windows (cdr quit-config)))))))
10106
10107 ;;; Dead summaries.
10108
10109 (defvar gnus-dead-summary-mode-map nil)
10110
10111 (if gnus-dead-summary-mode-map
10112     nil
10113   (setq gnus-dead-summary-mode-map (make-keymap))
10114   (suppress-keymap gnus-dead-summary-mode-map)
10115   (substitute-key-definition
10116    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
10117   (let ((keys '("\C-d" "\r" "\177")))
10118     (while keys
10119       (define-key gnus-dead-summary-mode-map
10120         (pop keys) 'gnus-summary-wake-up-the-dead))))
10121
10122 (defvar gnus-dead-summary-mode nil
10123   "Minor mode for Gnus summary buffers.")
10124
10125 (defun gnus-dead-summary-mode (&optional arg)
10126   "Minor mode for Gnus summary buffers."
10127   (interactive "P")
10128   (when (eq major-mode 'gnus-summary-mode)
10129     (make-local-variable 'gnus-dead-summary-mode)
10130     (setq gnus-dead-summary-mode
10131           (if (null arg) (not gnus-dead-summary-mode)
10132             (> (prefix-numeric-value arg) 0)))
10133     (when gnus-dead-summary-mode
10134       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
10135         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
10136       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
10137         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
10138               minor-mode-map-alist)))))
10139
10140 (defun gnus-deaden-summary ()
10141   "Make the current summary buffer into a dead summary buffer."
10142   ;; Kill any previous dead summary buffer.
10143   (when (and gnus-dead-summary
10144              (buffer-name gnus-dead-summary))
10145     (save-excursion
10146       (set-buffer gnus-dead-summary)
10147       (when gnus-dead-summary-mode
10148         (kill-buffer (current-buffer)))))
10149   ;; Make this the current dead summary.
10150   (setq gnus-dead-summary (current-buffer))
10151   (gnus-dead-summary-mode 1)
10152   (let ((name (buffer-name)))
10153     (when (string-match "Summary" name)
10154       (rename-buffer
10155        (concat (substring name 0 (match-beginning 0)) "Dead "
10156                (substring name (match-beginning 0))) t))))
10157
10158 (defun gnus-kill-or-deaden-summary (buffer)
10159   "Kill or deaden the summary BUFFER."
10160   (when (and (buffer-name buffer)
10161              (not gnus-single-article-buffer))
10162     (save-excursion
10163       (set-buffer buffer)
10164       (gnus-kill-buffer gnus-article-buffer)
10165       (gnus-kill-buffer gnus-original-article-buffer)))
10166   (cond (gnus-kill-summary-on-exit
10167          (when (and gnus-use-trees
10168                     (and (get-buffer buffer)
10169                          (buffer-name (get-buffer buffer))))
10170            (save-excursion
10171              (set-buffer (get-buffer buffer))
10172              (gnus-tree-close gnus-newsgroup-name)))
10173          (gnus-kill-buffer buffer))
10174         ((and (get-buffer buffer)
10175               (buffer-name (get-buffer buffer)))
10176          (save-excursion
10177            (set-buffer buffer)
10178            (gnus-deaden-summary)))))
10179
10180 (defun gnus-summary-wake-up-the-dead (&rest args)
10181   "Wake up the dead summary buffer."
10182   (interactive)
10183   (gnus-dead-summary-mode -1)
10184   (let ((name (buffer-name)))
10185     (when (string-match "Dead " name)
10186       (rename-buffer
10187        (concat (substring name 0 (match-beginning 0))
10188                (substring name (match-end 0))) t)))
10189   (gnus-message 3 "This dead summary is now alive again"))
10190
10191 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10192 (defun gnus-summary-fetch-faq (&optional faq-dir)
10193   "Fetch the FAQ for the current group.
10194 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10195 in."
10196   (interactive
10197    (list
10198     (if current-prefix-arg
10199         (completing-read
10200          "Faq dir: " (and (listp gnus-group-faq-directory)
10201                           gnus-group-faq-directory)))))
10202   (let (gnus-faq-buffer)
10203     (and (setq gnus-faq-buffer
10204                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10205          (gnus-configure-windows 'summary-faq))))
10206
10207 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10208 (defun gnus-summary-describe-group (&optional force)
10209   "Describe the current newsgroup."
10210   (interactive "P")
10211   (gnus-group-describe-group force gnus-newsgroup-name))
10212
10213 (defun gnus-summary-describe-briefly ()
10214   "Describe summary mode commands briefly."
10215   (interactive)
10216   (gnus-message 6
10217                 (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")))
10218
10219 ;; Walking around group mode buffer from summary mode.
10220
10221 (defun gnus-summary-next-group (&optional no-article target-group backward)
10222   "Exit current newsgroup and then select next unread newsgroup.
10223 If prefix argument NO-ARTICLE is non-nil, no article is selected
10224 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10225 previous group instead."
10226   (interactive "P")
10227   (gnus-set-global-variables)
10228   (let ((current-group gnus-newsgroup-name)
10229         (current-buffer (current-buffer))
10230         entered)
10231     ;; First we semi-exit this group to update Xrefs and all variables.
10232     ;; We can't do a real exit, because the window conf must remain
10233     ;; the same in case the user is prompted for info, and we don't
10234     ;; want the window conf to change before that...
10235     (gnus-summary-exit t)
10236     (while (not entered)
10237       ;; Then we find what group we are supposed to enter.
10238       (set-buffer gnus-group-buffer)
10239       (gnus-group-jump-to-group current-group)
10240       (setq target-group
10241             (or target-group
10242                 (if (eq gnus-keep-same-level 'best)
10243                     (gnus-summary-best-group gnus-newsgroup-name)
10244                   (gnus-summary-search-group backward gnus-keep-same-level))))
10245       (if (not target-group)
10246           ;; There are no further groups, so we return to the group
10247           ;; buffer.
10248           (progn
10249             (gnus-message 5 "Returning to the group buffer")
10250             (setq entered t)
10251             (set-buffer current-buffer)
10252             (gnus-summary-exit))
10253         ;; We try to enter the target group.
10254         (gnus-group-jump-to-group target-group)
10255         (let ((unreads (gnus-group-group-unread)))
10256           (if (and (or (eq t unreads)
10257                        (and unreads (not (zerop unreads))))
10258                    (gnus-summary-read-group
10259                     target-group nil no-article current-buffer))
10260               (setq entered t)
10261             (setq current-group target-group
10262                   target-group nil)))))))
10263
10264 (defun gnus-summary-prev-group (&optional no-article)
10265   "Exit current newsgroup and then select previous unread newsgroup.
10266 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10267   (interactive "P")
10268   (gnus-summary-next-group no-article nil t))
10269
10270 ;; Walking around summary lines.
10271
10272 (defun gnus-summary-first-subject (&optional unread)
10273   "Go to the first unread subject.
10274 If UNREAD is non-nil, go to the first unread article.
10275 Returns the article selected or nil if there are no unread articles."
10276   (interactive "P")
10277   (prog1
10278       (cond
10279        ;; Empty summary.
10280        ((null gnus-newsgroup-data)
10281         (gnus-message 3 "No articles in the group")
10282         nil)
10283        ;; Pick the first article.
10284        ((not unread)
10285         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10286         (gnus-data-number (car gnus-newsgroup-data)))
10287        ;; No unread articles.
10288        ((null gnus-newsgroup-unreads)
10289         (gnus-message 3 "No more unread articles")
10290         nil)
10291        ;; Find the first unread article.
10292        (t
10293         (let ((data gnus-newsgroup-data))
10294           (while (and data
10295                       (not (gnus-data-unread-p (car data))))
10296             (setq data (cdr data)))
10297           (if data
10298               (progn
10299                 (goto-char (gnus-data-pos (car data)))
10300                 (gnus-data-number (car data)))))))
10301     (gnus-summary-position-point)))
10302
10303 (defun gnus-summary-next-subject (n &optional unread dont-display)
10304   "Go to next N'th summary line.
10305 If N is negative, go to the previous N'th subject line.
10306 If UNREAD is non-nil, only unread articles are selected.
10307 The difference between N and the actual number of steps taken is
10308 returned."
10309   (interactive "p")
10310   (let ((backward (< n 0))
10311         (n (abs n)))
10312     (while (and (> n 0)
10313                 (if backward
10314                     (gnus-summary-find-prev unread)
10315                   (gnus-summary-find-next unread)))
10316       (setq n (1- n)))
10317     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10318                                (if unread " unread" "")))
10319     (unless dont-display
10320       (gnus-summary-recenter)
10321       (gnus-summary-position-point))
10322     n))
10323
10324 (defun gnus-summary-next-unread-subject (n)
10325   "Go to next N'th unread summary line."
10326   (interactive "p")
10327   (gnus-summary-next-subject n t))
10328
10329 (defun gnus-summary-prev-subject (n &optional unread)
10330   "Go to previous N'th summary line.
10331 If optional argument UNREAD is non-nil, only unread article is selected."
10332   (interactive "p")
10333   (gnus-summary-next-subject (- n) unread))
10334
10335 (defun gnus-summary-prev-unread-subject (n)
10336   "Go to previous N'th unread summary line."
10337   (interactive "p")
10338   (gnus-summary-next-subject (- n) t))
10339
10340 (defun gnus-summary-goto-subject (article &optional force silent)
10341   "Go the subject line of ARTICLE.
10342 If FORCE, also allow jumping to articles not currently shown."
10343   (let ((b (point))
10344         (data (gnus-data-find article)))
10345     ;; We read in the article if we have to.
10346     (and (not data)
10347          force
10348          (gnus-summary-insert-subject article (and (vectorp force) force) t)
10349          (setq data (gnus-data-find article)))
10350     (goto-char b)
10351     (if (not data)
10352         (progn
10353           (unless silent
10354             (gnus-message 3 "Can't find article %d" article))
10355           nil)
10356       (goto-char (gnus-data-pos data))
10357       article)))
10358
10359 ;; Walking around summary lines with displaying articles.
10360
10361 (defun gnus-summary-expand-window (&optional arg)
10362   "Make the summary buffer take up the entire Emacs frame.
10363 Given a prefix, will force an `article' buffer configuration."
10364   (interactive "P")
10365   (gnus-set-global-variables)
10366   (if arg
10367       (gnus-configure-windows 'article 'force)
10368     (gnus-configure-windows 'summary 'force)))
10369
10370 (defun gnus-summary-display-article (article &optional all-header)
10371   "Display ARTICLE in article buffer."
10372   (gnus-set-global-variables)
10373   (if (null article)
10374       nil
10375     (prog1
10376         (if gnus-summary-display-article-function
10377             (funcall gnus-summary-display-article-function article all-header)
10378           (gnus-article-prepare article all-header))
10379       (run-hooks 'gnus-select-article-hook)
10380       (unless (zerop gnus-current-article)
10381         (gnus-summary-goto-subject gnus-current-article))
10382       (gnus-summary-recenter)
10383       (when gnus-use-trees
10384         (gnus-possibly-generate-tree article)
10385         (gnus-highlight-selected-tree article))
10386       ;; Successfully display article.
10387       (gnus-article-set-window-start
10388        (cdr (assq article gnus-newsgroup-bookmarks))))))
10389
10390 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10391   "Select the current article.
10392 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10393 non-nil, the article will be re-fetched even if it already present in
10394 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10395 be displayed."
10396   ;; Make sure we are in the summary buffer to work around bbdb bug.
10397   (unless (eq major-mode 'gnus-summary-mode)
10398     (set-buffer gnus-summary-buffer))
10399   (let ((article (or article (gnus-summary-article-number)))
10400         (all-headers (not (not all-headers))) ;Must be T or NIL.
10401         gnus-summary-display-article-function
10402         did)
10403     (and (not pseudo)
10404          (gnus-summary-article-pseudo-p article)
10405          (error "This is a pseudo-article."))
10406     (prog1
10407         (save-excursion
10408           (set-buffer gnus-summary-buffer)
10409           (if (or (and gnus-single-article-buffer
10410                        (or (null gnus-current-article)
10411                            (null gnus-article-current)
10412                            (null (get-buffer gnus-article-buffer))
10413                            (not (eq article (cdr gnus-article-current)))
10414                            (not (equal (car gnus-article-current)
10415                                        gnus-newsgroup-name))))
10416                   (and (not gnus-single-article-buffer)
10417                        (or (null gnus-current-article)
10418                            (not (eq gnus-current-article article))))
10419                   force)
10420               ;; The requested article is different from the current article.
10421               (prog1
10422                   (gnus-summary-display-article article all-headers)
10423                 (setq did article))
10424             (if (or all-headers gnus-show-all-headers)
10425                 (gnus-article-show-all-headers))
10426             'old))
10427       (if did
10428           (gnus-article-set-window-start
10429            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10430
10431 (defun gnus-summary-set-current-mark (&optional current-mark)
10432   "Obsolete function."
10433   nil)
10434
10435 (defun gnus-summary-next-article (&optional unread subject backward push)
10436   "Select the next article.
10437 If UNREAD, only unread articles are selected.
10438 If SUBJECT, only articles with SUBJECT are selected.
10439 If BACKWARD, the previous article is selected instead of the next."
10440   (interactive "P")
10441   (gnus-set-global-variables)
10442   (cond
10443    ;; Is there such an article?
10444    ((and (gnus-summary-search-forward unread subject backward)
10445          (or (gnus-summary-display-article (gnus-summary-article-number))
10446              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10447     (gnus-summary-position-point))
10448    ;; If not, we try the first unread, if that is wanted.
10449    ((and subject
10450          gnus-auto-select-same
10451          (gnus-summary-first-unread-article))
10452     (gnus-summary-position-point)
10453     (gnus-message 6 "Wrapped"))
10454    ;; Try to get next/previous article not displayed in this group.
10455    ((and gnus-auto-extend-newsgroup
10456          (not unread) (not subject))
10457     (gnus-summary-goto-article
10458      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10459      nil t))
10460    ;; Go to next/previous group.
10461    (t
10462     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10463         (gnus-summary-jump-to-group gnus-newsgroup-name))
10464     (let ((cmd last-command-char)
10465           (group
10466            (if (eq gnus-keep-same-level 'best)
10467                (gnus-summary-best-group gnus-newsgroup-name)
10468              (gnus-summary-search-group backward gnus-keep-same-level))))
10469       ;; For some reason, the group window gets selected.  We change
10470       ;; it back.
10471       (select-window (get-buffer-window (current-buffer)))
10472       ;; Select next unread newsgroup automagically.
10473       (cond
10474        ((or (not gnus-auto-select-next)
10475             (not cmd))
10476         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10477        ((or (eq gnus-auto-select-next 'quietly)
10478             (and (eq gnus-auto-select-next 'slightly-quietly)
10479                  push)
10480             (and (eq gnus-auto-select-next 'almost-quietly)
10481                  (gnus-summary-last-article-p)))
10482         ;; Select quietly.
10483         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10484             (gnus-summary-exit)
10485           (gnus-message 7 "No more%s articles (%s)..."
10486                         (if unread " unread" "")
10487                         (if group (concat "selecting " group)
10488                           "exiting"))
10489           (gnus-summary-next-group nil group backward)))
10490        (t
10491         (gnus-summary-walk-group-buffer
10492          gnus-newsgroup-name cmd unread backward)))))))
10493
10494 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10495   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10496                       (?\C-p (gnus-group-prev-unread-group 1))))
10497         keve key group ended)
10498     (save-excursion
10499       (set-buffer gnus-group-buffer)
10500       (gnus-summary-jump-to-group from-group)
10501       (setq group
10502             (if (eq gnus-keep-same-level 'best)
10503                 (gnus-summary-best-group gnus-newsgroup-name)
10504               (gnus-summary-search-group backward gnus-keep-same-level))))
10505     (while (not ended)
10506       (gnus-message
10507        5 "No more%s articles%s" (if unread " unread" "")
10508        (if (and group
10509                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10510            (format " (Type %s for %s [%s])"
10511                    (single-key-description cmd) group
10512                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10513          (format " (Type %s to exit %s)"
10514                  (single-key-description cmd)
10515                  gnus-newsgroup-name)))
10516       ;; Confirm auto selection.
10517       (setq key (car (setq keve (gnus-read-event-char))))
10518       (setq ended t)
10519       (cond
10520        ((assq key keystrokes)
10521         (let ((obuf (current-buffer)))
10522           (switch-to-buffer gnus-group-buffer)
10523           (and group
10524                (gnus-group-jump-to-group group))
10525           (eval (cadr (assq key keystrokes)))
10526           (setq group (gnus-group-group-name))
10527           (switch-to-buffer obuf))
10528         (setq ended nil))
10529        ((equal key cmd)
10530         (if (or (not group)
10531                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10532             (gnus-summary-exit)
10533           (gnus-summary-next-group nil group backward)))
10534        (t
10535         (push (cdr keve) unread-command-events))))))
10536
10537 (defun gnus-read-event-char ()
10538   "Get the next event."
10539   (let ((event (read-event)))
10540     (cons (and (numberp event) event) event)))
10541
10542 (defun gnus-summary-next-unread-article ()
10543   "Select unread article after current one."
10544   (interactive)
10545   (gnus-summary-next-article t (and gnus-auto-select-same
10546                                     (gnus-summary-article-subject))))
10547
10548 (defun gnus-summary-prev-article (&optional unread subject)
10549   "Select the article after the current one.
10550 If UNREAD is non-nil, only unread articles are selected."
10551   (interactive "P")
10552   (gnus-summary-next-article unread subject t))
10553
10554 (defun gnus-summary-prev-unread-article ()
10555   "Select unred article before current one."
10556   (interactive)
10557   (gnus-summary-prev-article t (and gnus-auto-select-same
10558                                     (gnus-summary-article-subject))))
10559
10560 (defun gnus-summary-next-page (&optional lines circular)
10561   "Show next page of the selected article.
10562 If at the end of the current article, select the next article.
10563 LINES says how many lines should be scrolled up.
10564
10565 If CIRCULAR is non-nil, go to the start of the article instead of
10566 selecting the next article when reaching the end of the current
10567 article."
10568   (interactive "P")
10569   (setq gnus-summary-buffer (current-buffer))
10570   (gnus-set-global-variables)
10571   (let ((article (gnus-summary-article-number))
10572         (endp nil))
10573     (gnus-configure-windows 'article)
10574     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10575         (if (and (eq gnus-summary-goto-unread 'never)
10576                  (not (gnus-summary-last-article-p article)))
10577             (gnus-summary-next-article)
10578           (gnus-summary-next-unread-article))
10579       (if (or (null gnus-current-article)
10580               (null gnus-article-current)
10581               (/= article (cdr gnus-article-current))
10582               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10583           ;; Selected subject is different from current article's.
10584           (gnus-summary-display-article article)
10585         (gnus-eval-in-buffer-window gnus-article-buffer
10586           (setq endp (gnus-article-next-page lines)))
10587         (if endp
10588             (cond (circular
10589                    (gnus-summary-beginning-of-article))
10590                   (lines
10591                    (gnus-message 3 "End of message"))
10592                   ((null lines)
10593                    (if (and (eq gnus-summary-goto-unread 'never)
10594                             (not (gnus-summary-last-article-p article)))
10595                        (gnus-summary-next-article)
10596                      (gnus-summary-next-unread-article)))))))
10597     (gnus-summary-recenter)
10598     (gnus-summary-position-point)))
10599
10600 (defun gnus-summary-prev-page (&optional lines)
10601   "Show previous page of selected article.
10602 Argument LINES specifies lines to be scrolled down."
10603   (interactive "P")
10604   (gnus-set-global-variables)
10605   (let ((article (gnus-summary-article-number)))
10606     (gnus-configure-windows 'article)
10607     (if (or (null gnus-current-article)
10608             (null gnus-article-current)
10609             (/= article (cdr gnus-article-current))
10610             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10611         ;; Selected subject is different from current article's.
10612         (gnus-summary-display-article article)
10613       (gnus-summary-recenter)
10614       (gnus-eval-in-buffer-window gnus-article-buffer
10615         (gnus-article-prev-page lines))))
10616   (gnus-summary-position-point))
10617
10618 (defun gnus-summary-scroll-up (lines)
10619   "Scroll up (or down) one line current article.
10620 Argument LINES specifies lines to be scrolled up (or down if negative)."
10621   (interactive "p")
10622   (gnus-set-global-variables)
10623   (gnus-configure-windows 'article)
10624   (gnus-summary-show-thread)
10625   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10626     (gnus-eval-in-buffer-window gnus-article-buffer
10627       (cond ((> lines 0)
10628              (if (gnus-article-next-page lines)
10629                  (gnus-message 3 "End of message")))
10630             ((< lines 0)
10631              (gnus-article-prev-page (- lines))))))
10632   (gnus-summary-recenter)
10633   (gnus-summary-position-point))
10634
10635 (defun gnus-summary-next-same-subject ()
10636   "Select next article which has the same subject as current one."
10637   (interactive)
10638   (gnus-set-global-variables)
10639   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10640
10641 (defun gnus-summary-prev-same-subject ()
10642   "Select previous article which has the same subject as current one."
10643   (interactive)
10644   (gnus-set-global-variables)
10645   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10646
10647 (defun gnus-summary-next-unread-same-subject ()
10648   "Select next unread article which has the same subject as current one."
10649   (interactive)
10650   (gnus-set-global-variables)
10651   (gnus-summary-next-article t (gnus-summary-article-subject)))
10652
10653 (defun gnus-summary-prev-unread-same-subject ()
10654   "Select previous unread article which has the same subject as current one."
10655   (interactive)
10656   (gnus-set-global-variables)
10657   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10658
10659 (defun gnus-summary-first-unread-article ()
10660   "Select the first unread article.
10661 Return nil if there are no unread articles."
10662   (interactive)
10663   (gnus-set-global-variables)
10664   (prog1
10665       (if (gnus-summary-first-subject t)
10666           (progn
10667             (gnus-summary-show-thread)
10668             (gnus-summary-first-subject t)
10669             (gnus-summary-display-article (gnus-summary-article-number))))
10670     (gnus-summary-position-point)))
10671
10672 (defun gnus-summary-best-unread-article ()
10673   "Select the unread article with the highest score."
10674   (interactive)
10675   (gnus-set-global-variables)
10676   (let ((best -1000000)
10677         (data gnus-newsgroup-data)
10678         article score)
10679     (while data
10680       (and (gnus-data-unread-p (car data))
10681            (> (setq score
10682                     (gnus-summary-article-score (gnus-data-number (car data))))
10683               best)
10684            (setq best score
10685                  article (gnus-data-number (car data))))
10686       (setq data (cdr data)))
10687     (prog1
10688         (if article
10689             (gnus-summary-goto-article article)
10690           (error "No unread articles"))
10691       (gnus-summary-position-point))))
10692
10693 (defun gnus-summary-last-subject ()
10694   "Go to the last displayed subject line in the group."
10695   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10696     (when article
10697       (gnus-summary-goto-subject article))))
10698
10699 (defun gnus-summary-goto-article (article &optional all-headers force)
10700   "Fetch ARTICLE and display it if it exists.
10701 If ALL-HEADERS is non-nil, no header lines are hidden."
10702   (interactive
10703    (list
10704     (string-to-int
10705      (completing-read
10706       "Article number: "
10707       (mapcar (lambda (number) (list (int-to-string number)))
10708               gnus-newsgroup-limit)))
10709     current-prefix-arg
10710     t))
10711   (prog1
10712       (if (gnus-summary-goto-subject article force)
10713           (gnus-summary-display-article article all-headers)
10714         (gnus-message 4 "Couldn't go to article %s" article) nil)
10715     (gnus-summary-position-point)))
10716
10717 (defun gnus-summary-goto-last-article ()
10718   "Go to the previously read article."
10719   (interactive)
10720   (prog1
10721       (and gnus-last-article
10722            (gnus-summary-goto-article gnus-last-article))
10723     (gnus-summary-position-point)))
10724
10725 (defun gnus-summary-pop-article (number)
10726   "Pop one article off the history and go to the previous.
10727 NUMBER articles will be popped off."
10728   (interactive "p")
10729   (let (to)
10730     (setq gnus-newsgroup-history
10731           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10732     (if to
10733         (gnus-summary-goto-article (car to))
10734       (error "Article history empty")))
10735   (gnus-summary-position-point))
10736
10737 ;; Summary commands and functions for limiting the summary buffer.
10738
10739 (defun gnus-summary-limit-to-articles (n)
10740   "Limit the summary buffer to the next N articles.
10741 If not given a prefix, use the process marked articles instead."
10742   (interactive "P")
10743   (gnus-set-global-variables)
10744   (prog1
10745       (let ((articles (gnus-summary-work-articles n)))
10746         (setq gnus-newsgroup-processable nil)
10747         (gnus-summary-limit articles))
10748     (gnus-summary-position-point)))
10749
10750 (defun gnus-summary-pop-limit (&optional total)
10751   "Restore the previous limit.
10752 If given a prefix, remove all limits."
10753   (interactive "P")
10754   (gnus-set-global-variables)
10755   (when total 
10756     (setq gnus-newsgroup-limits
10757           (list (mapcar (lambda (h) (mail-header-number h))
10758                         gnus-newsgroup-headers))))
10759   (unless gnus-newsgroup-limits
10760     (error "No limit to pop"))
10761   (prog1
10762       (gnus-summary-limit nil 'pop)
10763     (gnus-summary-position-point)))
10764
10765 (defun gnus-summary-limit-to-subject (subject &optional header)
10766   "Limit the summary buffer to articles that have subjects that match a regexp."
10767   (interactive "sRegexp: ")
10768   (unless header
10769     (setq header "subject"))
10770   (when (not (equal "" subject))
10771     (prog1
10772         (let ((articles (gnus-summary-find-matching
10773                          (or header "subject") subject 'all)))
10774           (or articles (error "Found no matches for \"%s\"" subject))
10775           (gnus-summary-limit articles))
10776       (gnus-summary-position-point))))
10777
10778 (defun gnus-summary-limit-to-author (from)
10779   "Limit the summary buffer to articles that have authors that match a regexp."
10780   (interactive "sRegexp: ")
10781   (gnus-summary-limit-to-subject from "from"))
10782
10783 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10784 (make-obsolete
10785  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10786
10787 (defun gnus-summary-limit-to-unread (&optional all)
10788   "Limit the summary buffer to articles that are not marked as read.
10789 If ALL is non-nil, limit strictly to unread articles."
10790   (interactive "P")
10791   (if all
10792       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10793     (gnus-summary-limit-to-marks
10794      ;; Concat all the marks that say that an article is read and have
10795      ;; those removed.
10796      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10797            gnus-killed-mark gnus-kill-file-mark
10798            gnus-low-score-mark gnus-expirable-mark
10799            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10800      'reverse)))
10801
10802 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10803 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10804
10805 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10806   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10807 If REVERSE, limit the summary buffer to articles that are not marked
10808 with MARKS.  MARKS can either be a string of marks or a list of marks.
10809 Returns how many articles were removed."
10810   (interactive "sMarks: ")
10811   (gnus-set-global-variables)
10812   (prog1
10813       (let ((data gnus-newsgroup-data)
10814             (marks (if (listp marks) marks
10815                      (append marks nil))) ; Transform to list.
10816             articles)
10817         (while data
10818           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10819                  (memq (gnus-data-mark (car data)) marks))
10820                (setq articles (cons (gnus-data-number (car data)) articles)))
10821           (setq data (cdr data)))
10822         (gnus-summary-limit articles))
10823     (gnus-summary-position-point)))
10824
10825 (defun gnus-summary-limit-to-score (&optional score)
10826   "Limit to articles with score at or above SCORE."
10827   (interactive "P")
10828   (gnus-set-global-variables)
10829   (setq score (if score
10830                   (prefix-numeric-value score)
10831                 (or gnus-summary-default-score 0)))
10832   (let ((data gnus-newsgroup-data)
10833         articles)
10834     (while data
10835       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10836                 score)
10837         (push (gnus-data-number (car data)) articles))
10838       (setq data (cdr data)))
10839     (prog1
10840         (gnus-summary-limit articles)
10841       (gnus-summary-position-point))))
10842
10843 (defun gnus-summary-limit-include-dormant ()
10844   "Display all the hidden articles that are marked as dormant."
10845   (interactive)
10846   (gnus-set-global-variables)
10847   (or gnus-newsgroup-dormant
10848       (error "There are no dormant articles in this group"))
10849   (prog1
10850       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10851     (gnus-summary-position-point)))
10852
10853 (defun gnus-summary-limit-exclude-dormant ()
10854   "Hide all dormant articles."
10855   (interactive)
10856   (gnus-set-global-variables)
10857   (prog1
10858       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10859     (gnus-summary-position-point)))
10860
10861 (defun gnus-summary-limit-exclude-childless-dormant ()
10862   "Hide all dormant articles that have no children."
10863   (interactive)
10864   (gnus-set-global-variables)
10865   (let ((data (gnus-data-list t))
10866         articles d children)
10867     ;; Find all articles that are either not dormant or have
10868     ;; children.
10869     (while (setq d (pop data))
10870       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10871                 (and (setq children 
10872                            (gnus-article-children (gnus-data-number d)))
10873                      (let (found)
10874                        (while children
10875                          (when (memq (car children) articles)
10876                            (setq children nil
10877                                  found t))
10878                          (pop children))
10879                        found)))
10880         (push (gnus-data-number d) articles)))
10881     ;; Do the limiting.
10882     (prog1
10883         (gnus-summary-limit articles)
10884       (gnus-summary-position-point))))
10885
10886 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10887   "Mark all unread excluded articles as read.
10888 If ALL, mark even excluded ticked and dormants as read."
10889   (interactive "P")
10890   (let ((articles (gnus-sorted-complement
10891                    (sort
10892                     (mapcar (lambda (h) (mail-header-number h))
10893                             gnus-newsgroup-headers)
10894                     '<)
10895                    (sort gnus-newsgroup-limit '<)))
10896         article)
10897     (setq gnus-newsgroup-unreads nil)
10898     (if all
10899         (setq gnus-newsgroup-dormant nil
10900               gnus-newsgroup-marked nil
10901               gnus-newsgroup-reads
10902               (nconc
10903                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10904                gnus-newsgroup-reads))
10905       (while (setq article (pop articles))
10906         (unless (or (memq article gnus-newsgroup-dormant)
10907                     (memq article gnus-newsgroup-marked))
10908           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10909
10910 (defun gnus-summary-limit (articles &optional pop)
10911   (if pop
10912       ;; We pop the previous limit off the stack and use that.
10913       (setq articles (car gnus-newsgroup-limits)
10914             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10915     ;; We use the new limit, so we push the old limit on the stack.
10916     (setq gnus-newsgroup-limits
10917           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10918   ;; Set the limit.
10919   (setq gnus-newsgroup-limit articles)
10920   (let ((total (length gnus-newsgroup-data))
10921         (data (gnus-data-find-list (gnus-summary-article-number)))
10922         found)
10923     ;; This will do all the work of generating the new summary buffer
10924     ;; according to the new limit.
10925     (gnus-summary-prepare)
10926     ;; Hide any threads, possibly.
10927     (and gnus-show-threads
10928          gnus-thread-hide-subtree
10929          (gnus-summary-hide-all-threads))
10930     ;; Try to return to the article you were at, or one in the
10931     ;; neighborhood.
10932     (if data
10933         ;; We try to find some article after the current one.
10934         (while data
10935           (and (gnus-summary-goto-subject
10936                 (gnus-data-number (car data)) nil t)
10937                (setq data nil
10938                      found t))
10939           (setq data (cdr data))))
10940     (or found
10941         ;; If there is no data, that means that we were after the last
10942         ;; article.  The same goes when we can't find any articles
10943         ;; after the current one.
10944         (progn
10945           (goto-char (point-max))
10946           (gnus-summary-find-prev)))
10947     ;; We return how many articles were removed from the summary
10948     ;; buffer as a result of the new limit.
10949     (- total (length gnus-newsgroup-data))))
10950
10951 (defsubst gnus-invisible-cut-children (threads)
10952   (let ((num 0))
10953     (while threads
10954       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
10955         (incf num))
10956       (pop threads))
10957     (< num 2)))
10958
10959 (defsubst gnus-cut-thread (thread)
10960   "Go forwards in the thread until we find an article that we want to display."
10961   (when (or (eq gnus-fetch-old-headers 'some)
10962             (eq gnus-build-sparse-threads 'some)
10963             (eq gnus-build-sparse-threads 'more))
10964     ;; Deal with old-fetched headers and sparse threads.
10965     (while (and
10966             thread
10967             (or
10968              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10969              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
10970             (or (<= (length (cdr thread)) 1)
10971                 (gnus-invisible-cut-children (cdr thread))))
10972       (setq thread (cadr thread))))
10973   thread)
10974
10975 (defun gnus-cut-threads (threads)
10976   "Cut off all uninteresting articles from the beginning of threads."
10977   (when (or (eq gnus-fetch-old-headers 'some)
10978             (eq gnus-build-sparse-threads 'some)
10979             (eq gnus-build-sparse-threads 'more))
10980     (let ((th threads))
10981       (while th
10982         (setcar th (gnus-cut-thread (car th)))
10983         (setq th (cdr th)))))
10984   ;; Remove nixed out threads.
10985   (delq nil threads))
10986
10987 (defun gnus-summary-initial-limit (&optional show-if-empty)
10988   "Figure out what the initial limit is supposed to be on group entry.
10989 This entails weeding out unwanted dormants, low-scored articles,
10990 fetch-old-headers verbiage, and so on."
10991   ;; Most groups have nothing to remove.
10992   (if (or gnus-inhibit-limiting
10993           (and (null gnus-newsgroup-dormant)
10994                (not (eq gnus-fetch-old-headers 'some))
10995                (null gnus-summary-expunge-below)
10996                (not (eq gnus-build-sparse-threads 'some))
10997                (not (eq gnus-build-sparse-threads 'more))
10998                (null gnus-thread-expunge-below)
10999                (not gnus-use-nocem)))
11000       () ; Do nothing.
11001     (push gnus-newsgroup-limit gnus-newsgroup-limits)
11002     (setq gnus-newsgroup-limit nil)
11003     (mapatoms
11004      (lambda (node)
11005        (unless (car (symbol-value node))
11006          ;; These threads have no parents -- they are roots.
11007          (let ((nodes (cdr (symbol-value node)))
11008                thread)
11009            (while nodes
11010              (if (and gnus-thread-expunge-below
11011                       (< (gnus-thread-total-score (car nodes))
11012                          gnus-thread-expunge-below))
11013                  (gnus-expunge-thread (pop nodes))
11014                (setq thread (pop nodes))
11015                (gnus-summary-limit-children thread))))))
11016      gnus-newsgroup-dependencies)
11017     ;; If this limitation resulted in an empty group, we might
11018     ;; pop the previous limit and use it instead.
11019     (when (and (not gnus-newsgroup-limit)
11020                show-if-empty)
11021       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
11022     gnus-newsgroup-limit))
11023
11024 (defun gnus-summary-limit-children (thread)
11025   "Return 1 if this subthread is visible and 0 if it is not."
11026   ;; First we get the number of visible children to this thread.  This
11027   ;; is done by recursing down the thread using this function, so this
11028   ;; will really go down to a leaf article first, before slowly
11029   ;; working its way up towards the root.
11030   (when thread
11031     (let ((children
11032            (if (cdr thread)
11033                (apply '+ (mapcar 'gnus-summary-limit-children
11034                                  (cdr thread)))
11035              0))
11036           (number (mail-header-number (car thread)))
11037           score)
11038       (if (or
11039            ;; If this article is dormant and has absolutely no visible
11040            ;; children, then this article isn't visible.
11041            (and (memq number gnus-newsgroup-dormant)
11042                 (= children 0))
11043            ;; If this is "fetch-old-headered" and there is only one
11044            ;; visible child (or less), then we don't want this article.
11045            (and (eq gnus-fetch-old-headers 'some)
11046                 (memq number gnus-newsgroup-ancient)
11047                 (zerop children))
11048            ;; If this is a sparsely inserted article with no children,
11049            ;; we don't want it.
11050            (and (eq gnus-build-sparse-threads 'some)
11051                 (memq number gnus-newsgroup-sparse)
11052                 (zerop children))
11053            ;; If we use expunging, and this article is really
11054            ;; low-scored, then we don't want this article.
11055            (when (and gnus-summary-expunge-below
11056                       (< (setq score
11057                                (or (cdr (assq number gnus-newsgroup-scored))
11058                                    gnus-summary-default-score))
11059                          gnus-summary-expunge-below))
11060              ;; We increase the expunge-tally here, but that has
11061              ;; nothing to do with the limits, really.
11062              (incf gnus-newsgroup-expunged-tally)
11063              ;; We also mark as read here, if that's wanted.
11064              (when (and gnus-summary-mark-below
11065                         (< score gnus-summary-mark-below))
11066                (setq gnus-newsgroup-unreads
11067                      (delq number gnus-newsgroup-unreads))
11068                (if gnus-newsgroup-auto-expire
11069                    (push number gnus-newsgroup-expirable)
11070                  (push (cons number gnus-low-score-mark)
11071                        gnus-newsgroup-reads)))
11072              t)
11073            (and gnus-use-nocem
11074                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
11075           ;; Nope, invisible article.
11076           0
11077         ;; Ok, this article is to be visible, so we add it to the limit
11078         ;; and return 1.
11079         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
11080         1))))
11081
11082 (defun gnus-expunge-thread (thread)
11083   "Mark all articles in THREAD as read."
11084   (let* ((number (mail-header-number (car thread))))
11085     (incf gnus-newsgroup-expunged-tally)
11086     ;; We also mark as read here, if that's wanted.
11087     (setq gnus-newsgroup-unreads
11088           (delq number gnus-newsgroup-unreads))
11089     (if gnus-newsgroup-auto-expire
11090         (push number gnus-newsgroup-expirable)
11091       (push (cons number gnus-low-score-mark)
11092             gnus-newsgroup-reads)))
11093   ;; Go recursively through all subthreads.
11094   (mapcar 'gnus-expunge-thread (cdr thread)))
11095
11096 ;; Summary article oriented commands
11097
11098 (defun gnus-summary-refer-parent-article (n)
11099   "Refer parent article N times.
11100 The difference between N and the number of articles fetched is returned."
11101   (interactive "p")
11102   (gnus-set-global-variables)
11103   (while
11104       (and
11105        (> n 0)
11106        (let* ((header (gnus-summary-article-header))
11107               (ref
11108                ;; If we try to find the parent of the currently
11109                ;; displayed article, then we take a look at the actual
11110                ;; References header, since this is slightly more
11111                ;; reliable than the References field we got from the
11112                ;; server.
11113                (if (and (eq (mail-header-number header)
11114                             (cdr gnus-article-current))
11115                         (equal gnus-newsgroup-name
11116                                (car gnus-article-current)))
11117                    (save-excursion
11118                      (set-buffer gnus-original-article-buffer)
11119                      (nnheader-narrow-to-headers)
11120                      (prog1
11121                          (message-fetch-field "references")
11122                        (widen)))
11123                  ;; It's not the current article, so we take a bet on
11124                  ;; the value we got from the server.
11125                  (mail-header-references header))))
11126          (if (setq ref (or ref (mail-header-references header)))
11127              (or (gnus-summary-refer-article (gnus-parent-id ref))
11128                  (gnus-message 1 "Couldn't find parent"))
11129            (gnus-message 1 "No references in article %d"
11130                          (gnus-summary-article-number))
11131            nil)))
11132     (setq n (1- n)))
11133   (gnus-summary-position-point)
11134   n)
11135
11136 (defun gnus-summary-refer-references ()
11137   "Fetch all articles mentioned in the References header.
11138 Return how many articles were fetched."
11139   (interactive)
11140   (gnus-set-global-variables)
11141   (let ((ref (mail-header-references (gnus-summary-article-header)))
11142         (current (gnus-summary-article-number))
11143         (n 0))
11144     ;; For each Message-ID in the References header...
11145     (while (string-match "<[^>]*>" ref)
11146       (incf n)
11147       ;; ... fetch that article.
11148       (gnus-summary-refer-article
11149        (prog1 (match-string 0 ref)
11150          (setq ref (substring ref (match-end 0))))))
11151     (gnus-summary-goto-subject current)
11152     (gnus-summary-position-point)
11153     n))
11154
11155 (defun gnus-summary-refer-article (message-id)
11156   "Fetch an article specified by MESSAGE-ID."
11157   (interactive "sMessage-ID: ")
11158   (when (and (stringp message-id)
11159              (not (zerop (length message-id))))
11160     ;; Construct the correct Message-ID if necessary.
11161     ;; Suggested by tale@pawl.rpi.edu.
11162     (unless (string-match "^<" message-id)
11163       (setq message-id (concat "<" message-id)))
11164     (unless (string-match ">$" message-id)
11165       (setq message-id (concat message-id ">")))
11166     (let* ((header (gnus-id-to-header message-id))
11167            (sparse (and header
11168                         (memq (mail-header-number header)
11169                               gnus-newsgroup-sparse))))
11170       (if header
11171           (prog1
11172               ;; The article is present in the buffer, to we just go to it.
11173               (gnus-summary-goto-article 
11174                (mail-header-number header) nil header)
11175             (when sparse
11176               (gnus-summary-update-article (mail-header-number header))))
11177         ;; We fetch the article
11178         (let ((gnus-override-method 
11179                (and (gnus-news-group-p gnus-newsgroup-name)
11180                     gnus-refer-article-method))
11181               number)
11182           ;; Start the special refer-article method, if necessary.
11183           (when (and gnus-refer-article-method
11184                      (gnus-news-group-p gnus-newsgroup-name))
11185             (gnus-check-server gnus-refer-article-method))
11186           ;; Fetch the header, and display the article.
11187           (if (setq number (gnus-summary-insert-subject message-id))
11188               (gnus-summary-select-article nil nil nil number)
11189             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11190
11191 (defun gnus-summary-enter-digest-group (&optional force)
11192   "Enter a digest group based on the current article."
11193   (interactive "P")
11194   (gnus-set-global-variables)
11195   (gnus-summary-select-article)
11196   (let ((name (format "%s-%d"
11197                       (gnus-group-prefixed-name
11198                        gnus-newsgroup-name (list 'nndoc ""))
11199                       gnus-current-article))
11200         (ogroup gnus-newsgroup-name)
11201         (case-fold-search t)
11202         (buf (current-buffer))
11203         dig)
11204     (save-excursion
11205       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11206       (insert-buffer-substring gnus-original-article-buffer)
11207       (narrow-to-region
11208        (goto-char (point-min))
11209        (or (search-forward "\n\n" nil t) (point)))
11210       (goto-char (point-min))
11211       (delete-matching-lines "^\\(Path\\):\\|^From ")
11212       (widen))
11213     (unwind-protect
11214         (if (gnus-group-read-ephemeral-group
11215              name `(nndoc ,name (nndoc-address
11216                                  ,(get-buffer dig))
11217                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11218             ;; Make all postings to this group go to the parent group.
11219             (nconc (gnus-info-params (gnus-get-info name))
11220                    (list (cons 'to-group ogroup)))
11221           ;; Couldn't select this doc group.
11222           (switch-to-buffer buf)
11223           (gnus-set-global-variables)
11224           (gnus-configure-windows 'summary)
11225           (gnus-message 3 "Article couldn't be entered?"))
11226       (kill-buffer dig))))
11227
11228 (defun gnus-summary-isearch-article (&optional regexp-p)
11229   "Do incremental search forward on the current article.
11230 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11231   (interactive "P")
11232   (gnus-set-global-variables)
11233   (gnus-summary-select-article)
11234   (gnus-configure-windows 'article)
11235   (gnus-eval-in-buffer-window gnus-article-buffer
11236     (goto-char (point-min))
11237     (isearch-forward regexp-p)))
11238
11239 (defun gnus-summary-search-article-forward (regexp &optional backward)
11240   "Search for an article containing REGEXP forward.
11241 If BACKWARD, search backward instead."
11242   (interactive
11243    (list (read-string
11244           (format "Search article %s (regexp%s): "
11245                   (if current-prefix-arg "backward" "forward")
11246                   (if gnus-last-search-regexp
11247                       (concat ", default " gnus-last-search-regexp)
11248                     "")))
11249          current-prefix-arg))
11250   (gnus-set-global-variables)
11251   (if (string-equal regexp "")
11252       (setq regexp (or gnus-last-search-regexp ""))
11253     (setq gnus-last-search-regexp regexp))
11254   (unless (gnus-summary-search-article regexp backward)
11255     (error "Search failed: \"%s\"" regexp)))
11256
11257 (defun gnus-summary-search-article-backward (regexp)
11258   "Search for an article containing REGEXP backward."
11259   (interactive
11260    (list (read-string
11261           (format "Search article backward (regexp%s): "
11262                   (if gnus-last-search-regexp
11263                       (concat ", default " gnus-last-search-regexp)
11264                     "")))))
11265   (gnus-summary-search-article-forward regexp 'backward))
11266
11267 (defun gnus-summary-search-article (regexp &optional backward)
11268   "Search for an article containing REGEXP.
11269 Optional argument BACKWARD means do search for backward.
11270 `gnus-select-article-hook' is not called during the search."
11271   (let ((gnus-select-article-hook nil)  ;Disable hook.
11272         (gnus-article-display-hook nil)
11273         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11274         (re-search
11275          (if backward
11276              're-search-backward 're-search-forward))
11277         (sum (current-buffer))
11278         (found nil))
11279     (gnus-save-hidden-threads
11280       (gnus-summary-select-article)
11281       (set-buffer gnus-article-buffer)
11282       (when backward
11283         (forward-line -1))
11284       (while (not found)
11285         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11286         (if (if backward
11287                 (re-search-backward regexp nil t)
11288               (re-search-forward regexp nil t))
11289             ;; We found the regexp.
11290             (progn
11291               (setq found 'found)
11292               (beginning-of-line)
11293               (set-window-start
11294                (get-buffer-window (current-buffer))
11295                (point))
11296               (forward-line 1)
11297               (set-buffer sum))
11298           ;; We didn't find it, so we go to the next article.
11299           (set-buffer sum)
11300           (if (not (if backward (gnus-summary-find-prev)
11301                      (gnus-summary-find-next)))
11302               ;; No more articles.
11303               (setq found t)
11304             ;; Select the next article and adjust point.
11305             (gnus-summary-select-article)
11306             (set-buffer gnus-article-buffer)
11307             (widen)
11308             (goto-char (if backward (point-max) (point-min))))))
11309       (gnus-message 7 ""))
11310     ;; Return whether we found the regexp.
11311     (when (eq found 'found)
11312       (gnus-summary-show-thread)
11313       (gnus-summary-goto-subject gnus-current-article)
11314       (gnus-summary-position-point)
11315       t)))
11316
11317 (defun gnus-summary-find-matching (header regexp &optional backward unread
11318                                           not-case-fold)
11319   "Return a list of all articles that match REGEXP on HEADER.
11320 The search stars on the current article and goes forwards unless
11321 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11322 If UNREAD is non-nil, only unread articles will
11323 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11324 in the comparisons."
11325   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11326                 (gnus-data-find-list
11327                  (gnus-summary-article-number) (gnus-data-list backward))))
11328         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11329         (case-fold-search (not not-case-fold))
11330         articles d)
11331     (or (fboundp (intern (concat "mail-header-" header)))
11332         (error "%s is not a valid header" header))
11333     (while data
11334       (setq d (car data))
11335       (and (or (not unread)             ; We want all articles...
11336                (gnus-data-unread-p d))  ; Or just unreads.
11337            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11338            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11339            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11340       (setq data (cdr data)))
11341     (nreverse articles)))
11342
11343 (defun gnus-summary-execute-command (header regexp command &optional backward)
11344   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11345 If HEADER is an empty string (or nil), the match is done on the entire
11346 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11347   (interactive
11348    (list (let ((completion-ignore-case t))
11349            (completing-read
11350             "Header name: "
11351             (mapcar (lambda (string) (list string))
11352                     '("Number" "Subject" "From" "Lines" "Date"
11353                       "Message-ID" "Xref" "References" "Body"))
11354             nil 'require-match))
11355          (read-string "Regexp: ")
11356          (read-key-sequence "Command: ")
11357          current-prefix-arg))
11358   (when (equal header "Body")
11359     (setq header ""))
11360   (gnus-set-global-variables)
11361   ;; Hidden thread subtrees must be searched as well.
11362   (gnus-summary-show-all-threads)
11363   ;; We don't want to change current point nor window configuration.
11364   (save-excursion
11365     (save-window-excursion
11366       (gnus-message 6 "Executing %s..." (key-description command))
11367       ;; We'd like to execute COMMAND interactively so as to give arguments.
11368       (gnus-execute header regexp
11369                     `(lambda () (call-interactively ',(key-binding command)))
11370                     backward)
11371       (gnus-message 6 "Executing %s...done" (key-description command)))))
11372
11373 (defun gnus-summary-beginning-of-article ()
11374   "Scroll the article back to the beginning."
11375   (interactive)
11376   (gnus-set-global-variables)
11377   (gnus-summary-select-article)
11378   (gnus-configure-windows 'article)
11379   (gnus-eval-in-buffer-window gnus-article-buffer
11380     (widen)
11381     (goto-char (point-min))
11382     (and gnus-break-pages (gnus-narrow-to-page))))
11383
11384 (defun gnus-summary-end-of-article ()
11385   "Scroll to the end of the article."
11386   (interactive)
11387   (gnus-set-global-variables)
11388   (gnus-summary-select-article)
11389   (gnus-configure-windows 'article)
11390   (gnus-eval-in-buffer-window gnus-article-buffer
11391     (widen)
11392     (goto-char (point-max))
11393     (recenter -3)
11394     (and gnus-break-pages (gnus-narrow-to-page))))
11395
11396 (defun gnus-summary-show-article (&optional arg)
11397   "Force re-fetching of the current article.
11398 If ARG (the prefix) is non-nil, show the raw article without any
11399 article massaging functions being run."
11400   (interactive "P")
11401   (gnus-set-global-variables)
11402   (if (not arg)
11403       ;; Select the article the normal way.
11404       (gnus-summary-select-article nil 'force)
11405     ;; Bind the article treatment functions to nil.
11406     (let ((gnus-have-all-headers t)
11407           gnus-article-display-hook
11408           gnus-article-prepare-hook
11409           gnus-break-pages
11410           gnus-visual)
11411       (gnus-summary-select-article nil 'force)))
11412   (gnus-summary-goto-subject gnus-current-article)
11413 ;  (gnus-configure-windows 'article)
11414   (gnus-summary-position-point))
11415
11416 (defun gnus-summary-verbose-headers (&optional arg)
11417   "Toggle permanent full header display.
11418 If ARG is a positive number, turn header display on.
11419 If ARG is a negative number, turn header display off."
11420   (interactive "P")
11421   (gnus-set-global-variables)
11422   (gnus-summary-toggle-header arg)
11423   (setq gnus-show-all-headers
11424         (cond ((or (not (numberp arg))
11425                    (zerop arg))
11426                (not gnus-show-all-headers))
11427               ((natnump arg)
11428                t))))
11429
11430 (defun gnus-summary-toggle-header (&optional arg)
11431   "Show the headers if they are hidden, or hide them if they are shown.
11432 If ARG is a positive number, show the entire header.
11433 If ARG is a negative number, hide the unwanted header lines."
11434   (interactive "P")
11435   (gnus-set-global-variables)
11436   (save-excursion
11437     (set-buffer gnus-article-buffer)
11438     (let* ((buffer-read-only nil)
11439            (inhibit-point-motion-hooks t)
11440            (hidden (text-property-any
11441                     (goto-char (point-min)) (search-forward "\n\n")
11442                     'invisible t))
11443            e)
11444       (goto-char (point-min))
11445       (when (search-forward "\n\n" nil t)
11446         (delete-region (point-min) (1- (point))))
11447       (goto-char (point-min))
11448       (save-excursion
11449         (set-buffer gnus-original-article-buffer)
11450         (goto-char (point-min))
11451         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11452       (insert-buffer-substring gnus-original-article-buffer 1 e)
11453       (let ((gnus-inhibit-hiding t))
11454         (run-hooks 'gnus-article-display-hook))
11455       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11456           (gnus-article-hide-headers)))))
11457
11458 (defun gnus-summary-show-all-headers ()
11459   "Make all header lines visible."
11460   (interactive)
11461   (gnus-set-global-variables)
11462   (gnus-article-show-all-headers))
11463
11464 (defun gnus-summary-toggle-mime (&optional arg)
11465   "Toggle MIME processing.
11466 If ARG is a positive number, turn MIME processing on."
11467   (interactive "P")
11468   (gnus-set-global-variables)
11469   (setq gnus-show-mime
11470         (if (null arg) (not gnus-show-mime)
11471           (> (prefix-numeric-value arg) 0)))
11472   (gnus-summary-select-article t 'force))
11473
11474 (defun gnus-summary-caesar-message (&optional arg)
11475   "Caesar rotate the current article by 13.
11476 The numerical prefix specifies how manu places to rotate each letter
11477 forward."
11478   (interactive "P")
11479   (gnus-set-global-variables)
11480   (gnus-summary-select-article)
11481   (let ((mail-header-separator ""))
11482     (gnus-eval-in-buffer-window gnus-article-buffer
11483       (save-restriction
11484         (widen)
11485         (let ((start (window-start))
11486               buffer-read-only)
11487           (message-caesar-buffer-body arg)
11488           (set-window-start (get-buffer-window (current-buffer)) start))))))
11489
11490 (defun gnus-summary-stop-page-breaking ()
11491   "Stop page breaking in the current article."
11492   (interactive)
11493   (gnus-set-global-variables)
11494   (gnus-summary-select-article)
11495   (gnus-eval-in-buffer-window gnus-article-buffer
11496     (widen)))
11497
11498 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11499   "Move the current article to a different newsgroup.
11500 If N is a positive number, move the N next articles.
11501 If N is a negative number, move the N previous articles.
11502 If N is nil and any articles have been marked with the process mark,
11503 move those articles instead.
11504 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11505 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11506 re-spool using this method.
11507
11508 For this function to work, both the current newsgroup and the
11509 newsgroup that you want to move to have to support the `request-move'
11510 and `request-accept' functions."
11511   (interactive "P")
11512   (unless action (setq action 'move))
11513   (gnus-set-global-variables)
11514   ;; Check whether the source group supports the required functions.
11515   (cond ((and (eq action 'move)
11516               (not (gnus-check-backend-function
11517                     'request-move-article gnus-newsgroup-name)))
11518          (error "The current group does not support article moving"))
11519         ((and (eq action 'crosspost)
11520               (not (gnus-check-backend-function
11521                     'request-replace-article gnus-newsgroup-name)))
11522          (error "The current group does not support article editing")))
11523   (let ((articles (gnus-summary-work-articles n))
11524         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11525         (names '((move "Move" "Moving")
11526                  (copy "Copy" "Copying")
11527                  (crosspost "Crosspost" "Crossposting")))
11528         (copy-buf (save-excursion
11529                     (nnheader-set-temp-buffer " *copy article*")))
11530         art-group to-method new-xref article to-groups)
11531     (unless (assq action names)
11532       (error "Unknown action %s" action))
11533     ;; Read the newsgroup name.
11534     (when (and (not to-newsgroup)
11535                (not select-method))
11536       (setq to-newsgroup
11537             (gnus-read-move-group-name
11538              (cadr (assq action names))
11539              (symbol-value (intern (format "gnus-current-%s-group" action)))
11540              articles prefix))
11541       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11542     (setq to-method (or select-method 
11543                         (gnus-group-name-to-method to-newsgroup)))
11544     ;; Check the method we are to move this article to...
11545     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11546         (error "%s does not support article copying" (car to-method)))
11547     (or (gnus-check-server to-method)
11548         (error "Can't open server %s" (car to-method)))
11549     (gnus-message 6 "%s to %s: %s..."
11550                   (caddr (assq action names))
11551                   (or (car select-method) to-newsgroup) articles)
11552     (while articles
11553       (setq article (pop articles))
11554       (setq
11555        art-group
11556        (cond
11557         ;; Move the article.
11558         ((eq action 'move)
11559          (gnus-request-move-article
11560           article                       ; Article to move
11561           gnus-newsgroup-name           ; From newsgrouo
11562           (nth 1 (gnus-find-method-for-group
11563                   gnus-newsgroup-name)) ; Server
11564           (list 'gnus-request-accept-article
11565                 to-newsgroup (list 'quote select-method)
11566                 (not articles))         ; Accept form
11567           (not articles)))              ; Only save nov last time
11568         ;; Copy the article.
11569         ((eq action 'copy)
11570          (save-excursion
11571            (set-buffer copy-buf)
11572            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11573            (gnus-request-accept-article
11574             to-newsgroup select-method (not articles))))
11575         ;; Crosspost the article.
11576         ((eq action 'crosspost)
11577          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11578            (setq new-xref (concat gnus-newsgroup-name ":" article))
11579            (if (and xref (not (string= xref "")))
11580                (progn
11581                  (when (string-match "^Xref: " xref)
11582                    (setq xref (substring xref (match-end 0))))
11583                  (setq new-xref (concat xref " " new-xref)))
11584              (setq new-xref (concat (system-name) " " new-xref)))
11585            (save-excursion
11586              (set-buffer copy-buf)
11587              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11588              (nnheader-replace-header "xref" new-xref)
11589              (gnus-request-accept-article
11590               to-newsgroup select-method (not articles)))))))
11591       (if (not art-group)
11592           (gnus-message 1 "Couldn't %s article %s"
11593                         (cadr (assq action names)) article)
11594         (let* ((entry
11595                 (or
11596                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11597                  (gnus-gethash
11598                   (gnus-group-prefixed-name
11599                    (car art-group)
11600                    (or select-method 
11601                        (gnus-find-method-for-group to-newsgroup)))
11602                   gnus-newsrc-hashtb)))
11603                (info (nth 2 entry))
11604                (to-group (gnus-info-group info)))
11605           ;; Update the group that has been moved to.
11606           (when (and info
11607                      (memq action '(move copy)))
11608             (unless (member to-group to-groups)
11609               (push to-group to-groups))
11610
11611             (unless (memq article gnus-newsgroup-unreads)
11612               (gnus-info-set-read
11613                info (gnus-add-to-range (gnus-info-read info)
11614                                        (list (cdr art-group)))))
11615
11616             ;; Copy any marks over to the new group.
11617             (let ((marks gnus-article-mark-lists)
11618                   (to-article (cdr art-group)))
11619
11620               ;; See whether the article is to be put in the cache.
11621               (when gnus-use-cache
11622                 (gnus-cache-possibly-enter-article
11623                  to-group to-article
11624                  (let ((header (copy-sequence
11625                                 (gnus-summary-article-header article))))
11626                    (mail-header-set-number header to-article)
11627                    header)
11628                  (memq article gnus-newsgroup-marked)
11629                  (memq article gnus-newsgroup-dormant)
11630                  (memq article gnus-newsgroup-unreads)))
11631
11632               (while marks
11633                 (when (memq article (symbol-value
11634                                      (intern (format "gnus-newsgroup-%s"
11635                                                      (caar marks)))))
11636                   ;; If the other group is the same as this group,
11637                   ;; then we have to add the mark to the list.
11638                   (when (equal to-group gnus-newsgroup-name)
11639                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11640                          (cons to-article
11641                                (symbol-value
11642                                 (intern (format "gnus-newsgroup-%s"
11643                                                 (caar marks)))))))
11644                   ;; Copy mark to other group.
11645                   (gnus-add-marked-articles
11646                    to-group (cdar marks) (list to-article) info))
11647                 (setq marks (cdr marks)))))
11648
11649           ;; Update the Xref header in this article to point to
11650           ;; the new crossposted article we have just created.
11651           (when (eq action 'crosspost)
11652             (save-excursion
11653               (set-buffer copy-buf)
11654               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11655               (nnheader-replace-header
11656                "xref" (concat new-xref " " (gnus-group-prefixed-name
11657                                             (car art-group) to-method)
11658                               ":" (cdr art-group)))
11659               (gnus-request-replace-article
11660                article gnus-newsgroup-name (current-buffer)))))
11661
11662         (gnus-summary-goto-subject article)
11663         (when (eq action 'move)
11664           (gnus-summary-mark-article article gnus-canceled-mark)))
11665       (gnus-summary-remove-process-mark article))
11666     ;; Re-activate all groups that have been moved to.
11667     (while to-groups
11668       (gnus-activate-group (pop to-groups)))
11669     
11670     (gnus-kill-buffer copy-buf)
11671     (gnus-summary-position-point)
11672     (gnus-set-mode-line 'summary)))
11673
11674 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11675   "Move the current article to a different newsgroup.
11676 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11677 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11678 re-spool using this method."
11679   (interactive "P")
11680   (gnus-summary-move-article n nil select-method 'copy))
11681
11682 (defun gnus-summary-crosspost-article (&optional n)
11683   "Crosspost the current article to some other group."
11684   (interactive "P")
11685   (gnus-summary-move-article n nil nil 'crosspost))
11686
11687 (defvar gnus-summary-respool-default-method nil
11688   "Default method for respooling an article.  
11689 If nil, use to the current newsgroup method.")
11690
11691 (defun gnus-summary-respool-article (&optional n method)
11692   "Respool the current article.
11693 The article will be squeezed through the mail spooling process again,
11694 which means that it will be put in some mail newsgroup or other
11695 depending on `nnmail-split-methods'.
11696 If N is a positive number, respool the N next articles.
11697 If N is a negative number, respool the N previous articles.
11698 If N is nil and any articles have been marked with the process mark,
11699 respool those articles instead.
11700
11701 Respooling can be done both from mail groups and \"real\" newsgroups.
11702 In the former case, the articles in question will be moved from the
11703 current group into whatever groups they are destined to.  In the
11704 latter case, they will be copied into the relevant groups."
11705   (interactive 
11706    (list current-prefix-arg
11707          (let* ((methods (gnus-methods-using 'respool))
11708                 (methname
11709                  (symbol-name (or gnus-summary-respool-default-method
11710                                   (car (gnus-find-method-for-group
11711                                         gnus-newsgroup-name)))))
11712                 (method
11713                  (gnus-completing-read 
11714                   methname "What backend do you want to use when respooling?"
11715                   methods nil t nil 'gnus-method-history))
11716                 ms)
11717            (cond
11718             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11719              (list (intern method) ""))
11720             ((= 1 (length ms))
11721              (car ms))
11722             (t
11723              (cdr (completing-read 
11724                    "Server name: "
11725                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11726   (gnus-set-global-variables)
11727   (unless method
11728     (error "No method given for respooling"))
11729   (if (assoc (symbol-name
11730               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11731              (gnus-methods-using 'respool))
11732       (gnus-summary-move-article n nil method)
11733     (gnus-summary-copy-article n nil method)))
11734
11735 (defun gnus-summary-import-article (file)
11736   "Import a random file into a mail newsgroup."
11737   (interactive "fImport file: ")
11738   (gnus-set-global-variables)
11739   (let ((group gnus-newsgroup-name)
11740         (now (current-time))
11741         atts lines)
11742     (or (gnus-check-backend-function 'request-accept-article group)
11743         (error "%s does not support article importing" group))
11744     (or (file-readable-p file)
11745         (not (file-regular-p file))
11746         (error "Can't read %s" file))
11747     (save-excursion
11748       (set-buffer (get-buffer-create " *import file*"))
11749       (buffer-disable-undo (current-buffer))
11750       (erase-buffer)
11751       (insert-file-contents file)
11752       (goto-char (point-min))
11753       (unless (nnheader-article-p)
11754         ;; This doesn't look like an article, so we fudge some headers.
11755         (setq atts (file-attributes file)
11756               lines (count-lines (point-min) (point-max)))
11757         (insert "From: " (read-string "From: ") "\n"
11758                 "Subject: " (read-string "Subject: ") "\n"
11759                 "Date: " (timezone-make-date-arpa-standard
11760                           (current-time-string (nth 5 atts))
11761                           (current-time-zone now)
11762                           (current-time-zone now)) "\n"
11763                 "Message-ID: " (message-make-message-id) "\n"
11764                 "Lines: " (int-to-string lines) "\n"
11765                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11766       (gnus-request-accept-article group nil t)
11767       (kill-buffer (current-buffer)))))
11768
11769 (defun gnus-summary-expire-articles (&optional now)
11770   "Expire all articles that are marked as expirable in the current group."
11771   (interactive)
11772   (gnus-set-global-variables)
11773   (when (gnus-check-backend-function
11774          'request-expire-articles gnus-newsgroup-name)
11775     ;; This backend supports expiry.
11776     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11777            (expirable (if total
11778                           (gnus-list-of-read-articles gnus-newsgroup-name)
11779                         (setq gnus-newsgroup-expirable
11780                               (sort gnus-newsgroup-expirable '<))))
11781            (expiry-wait (if now 'immediate
11782                           (gnus-group-get-parameter
11783                            gnus-newsgroup-name 'expiry-wait)))
11784            es)
11785       (when expirable
11786         ;; There are expirable articles in this group, so we run them
11787         ;; through the expiry process.
11788         (gnus-message 6 "Expiring articles...")
11789         ;; The list of articles that weren't expired is returned.
11790         (if expiry-wait
11791             (let ((nnmail-expiry-wait-function nil)
11792                   (nnmail-expiry-wait expiry-wait))
11793               (setq es (gnus-request-expire-articles
11794                         expirable gnus-newsgroup-name)))
11795           (setq es (gnus-request-expire-articles
11796                     expirable gnus-newsgroup-name)))
11797         (or total (setq gnus-newsgroup-expirable es))
11798         ;; We go through the old list of expirable, and mark all
11799         ;; really expired articles as nonexistent.
11800         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11801           (let ((gnus-use-cache nil))
11802             (while expirable
11803               (unless (memq (car expirable) es)
11804                 (when (gnus-data-find (car expirable))
11805                   (gnus-summary-mark-article
11806                    (car expirable) gnus-canceled-mark)))
11807               (setq expirable (cdr expirable)))))
11808         (gnus-message 6 "Expiring articles...done")))))
11809
11810 (defun gnus-summary-expire-articles-now ()
11811   "Expunge all expirable articles in the current group.
11812 This means that *all* articles that are marked as expirable will be
11813 deleted forever, right now."
11814   (interactive)
11815   (gnus-set-global-variables)
11816   (or gnus-expert-user
11817       (gnus-y-or-n-p
11818        "Are you really, really, really sure you want to delete all these messages? ")
11819       (error "Phew!"))
11820   (gnus-summary-expire-articles t))
11821
11822 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11823 (defun gnus-summary-delete-article (&optional n)
11824   "Delete the N next (mail) articles.
11825 This command actually deletes articles.  This is not a marking
11826 command.  The article will disappear forever from your life, never to
11827 return.
11828 If N is negative, delete backwards.
11829 If N is nil and articles have been marked with the process mark,
11830 delete these instead."
11831   (interactive "P")
11832   (gnus-set-global-variables)
11833   (or (gnus-check-backend-function 'request-expire-articles
11834                                    gnus-newsgroup-name)
11835       (error "The current newsgroup does not support article deletion."))
11836   ;; Compute the list of articles to delete.
11837   (let ((articles (gnus-summary-work-articles n))
11838         not-deleted)
11839     (if (and gnus-novice-user
11840              (not (gnus-y-or-n-p
11841                    (format "Do you really want to delete %s forever? "
11842                            (if (> (length articles) 1) 
11843                                (format "these %s articles" (length articles))
11844                              "this article")))))
11845         ()
11846       ;; Delete the articles.
11847       (setq not-deleted (gnus-request-expire-articles
11848                          articles gnus-newsgroup-name 'force))
11849       (while articles
11850         (gnus-summary-remove-process-mark (car articles))
11851         ;; The backend might not have been able to delete the article
11852         ;; after all.
11853         (or (memq (car articles) not-deleted)
11854             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11855         (setq articles (cdr articles))))
11856     (gnus-summary-position-point)
11857     (gnus-set-mode-line 'summary)
11858     not-deleted))
11859
11860 (defun gnus-summary-edit-article (&optional force)
11861   "Enter into a buffer and edit the current article.
11862 This will have permanent effect only in mail groups.
11863 If FORCE is non-nil, allow editing of articles even in read-only
11864 groups."
11865   (interactive "P")
11866   (save-excursion
11867     (set-buffer gnus-summary-buffer)
11868     (gnus-set-global-variables)
11869     (when (and (not force)
11870                (gnus-group-read-only-p))
11871       (error "The current newsgroup does not support article editing."))
11872     (gnus-summary-select-article t nil t)
11873     (gnus-configure-windows 'article)
11874     (select-window (get-buffer-window gnus-article-buffer))
11875     (gnus-message 6 "C-c C-c to end edits")
11876     (setq buffer-read-only nil)
11877     (text-mode)
11878     (use-local-map (copy-keymap (current-local-map)))
11879     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11880     (buffer-enable-undo)
11881     (widen)
11882     (goto-char (point-min))
11883     (search-forward "\n\n" nil t)))
11884
11885 (defun gnus-summary-edit-article-done ()
11886   "Make edits to the current article permanent."
11887   (interactive)
11888   (if (gnus-group-read-only-p)
11889       (progn
11890         (let ((beep (not (eq major-mode 'text-mode))))
11891           (gnus-summary-edit-article-postpone)
11892           (when beep
11893             (gnus-error
11894              3 "The current newsgroup does not support article editing."))))
11895     (let ((buf (format "%s" (buffer-string))))
11896       (erase-buffer)
11897       (insert buf)
11898       (if (not (gnus-request-replace-article
11899                 (cdr gnus-article-current) (car gnus-article-current)
11900                 (current-buffer)))
11901           (error "Couldn't replace article.")
11902         (gnus-article-mode)
11903         (use-local-map gnus-article-mode-map)
11904         (setq buffer-read-only t)
11905         (buffer-disable-undo (current-buffer))
11906         (gnus-configure-windows 'summary)
11907         (gnus-summary-update-article (cdr gnus-article-current))
11908         (when gnus-use-cache
11909           (gnus-cache-update-article    
11910            (car gnus-article-current) (cdr gnus-article-current)))
11911         (when gnus-keep-backlog
11912           (gnus-backlog-remove-article 
11913            (car gnus-article-current) (cdr gnus-article-current))))
11914       (save-excursion
11915         (when (get-buffer gnus-original-article-buffer)
11916           (set-buffer gnus-original-article-buffer)
11917           (setq gnus-original-article nil)))
11918       (setq gnus-article-current nil
11919             gnus-current-article nil)
11920       (run-hooks 'gnus-article-display-hook)
11921       (and (gnus-visual-p 'summary-highlight 'highlight)
11922            (run-hooks 'gnus-visual-mark-article-hook)))))
11923
11924 (defun gnus-summary-edit-article-postpone ()
11925   "Postpone changes to the current article."
11926   (interactive)
11927   (gnus-article-mode)
11928   (use-local-map gnus-article-mode-map)
11929   (setq buffer-read-only t)
11930   (buffer-disable-undo (current-buffer))
11931   (gnus-configure-windows 'summary)
11932   (and (gnus-visual-p 'summary-highlight 'highlight)
11933        (run-hooks 'gnus-visual-mark-article-hook)))
11934
11935 (defun gnus-summary-respool-query ()
11936   "Query where the respool algorithm would put this article."
11937   (interactive)
11938   (gnus-set-global-variables)
11939   (gnus-summary-select-article)
11940   (save-excursion
11941     (set-buffer gnus-article-buffer)
11942     (save-restriction
11943       (goto-char (point-min))
11944       (search-forward "\n\n")
11945       (narrow-to-region (point-min) (point))
11946       (pp-eval-expression
11947        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11948
11949 ;; Summary marking commands.
11950
11951 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11952   "Mark articles which has the same subject as read, and then select the next.
11953 If UNMARK is positive, remove any kind of mark.
11954 If UNMARK is negative, tick articles."
11955   (interactive "P")
11956   (gnus-set-global-variables)
11957   (if unmark
11958       (setq unmark (prefix-numeric-value unmark)))
11959   (let ((count
11960          (gnus-summary-mark-same-subject
11961           (gnus-summary-article-subject) unmark)))
11962     ;; Select next unread article.  If auto-select-same mode, should
11963     ;; select the first unread article.
11964     (gnus-summary-next-article t (and gnus-auto-select-same
11965                                       (gnus-summary-article-subject)))
11966     (gnus-message 7 "%d article%s marked as %s"
11967                   count (if (= count 1) " is" "s are")
11968                   (if unmark "unread" "read"))))
11969
11970 (defun gnus-summary-kill-same-subject (&optional unmark)
11971   "Mark articles which has the same subject as read.
11972 If UNMARK is positive, remove any kind of mark.
11973 If UNMARK is negative, tick articles."
11974   (interactive "P")
11975   (gnus-set-global-variables)
11976   (if unmark
11977       (setq unmark (prefix-numeric-value unmark)))
11978   (let ((count
11979          (gnus-summary-mark-same-subject
11980           (gnus-summary-article-subject) unmark)))
11981     ;; If marked as read, go to next unread subject.
11982     (if (null unmark)
11983         ;; Go to next unread subject.
11984         (gnus-summary-next-subject 1 t))
11985     (gnus-message 7 "%d articles are marked as %s"
11986                   count (if unmark "unread" "read"))))
11987
11988 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11989   "Mark articles with same SUBJECT as read, and return marked number.
11990 If optional argument UNMARK is positive, remove any kinds of marks.
11991 If optional argument UNMARK is negative, mark articles as unread instead."
11992   (let ((count 1))
11993     (save-excursion
11994       (cond
11995        ((null unmark)                   ; Mark as read.
11996         (while (and
11997                 (progn
11998                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11999                   (gnus-summary-show-thread) t)
12000                 (gnus-summary-find-subject subject))
12001           (setq count (1+ count))))
12002        ((> unmark 0)                    ; Tick.
12003         (while (and
12004                 (progn
12005                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
12006                   (gnus-summary-show-thread) t)
12007                 (gnus-summary-find-subject subject))
12008           (setq count (1+ count))))
12009        (t                               ; Mark as unread.
12010         (while (and
12011                 (progn
12012                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
12013                   (gnus-summary-show-thread) t)
12014                 (gnus-summary-find-subject subject))
12015           (setq count (1+ count)))))
12016       (gnus-set-mode-line 'summary)
12017       ;; Return the number of marked articles.
12018       count)))
12019
12020 (defun gnus-summary-mark-as-processable (n &optional unmark)
12021   "Set the process mark on the next N articles.
12022 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
12023 the process mark instead.  The difference between N and the actual
12024 number of articles marked is returned."
12025   (interactive "p")
12026   (gnus-set-global-variables)
12027   (let ((backward (< n 0))
12028         (n (abs n)))
12029     (while (and
12030             (> n 0)
12031             (if unmark
12032                 (gnus-summary-remove-process-mark
12033                  (gnus-summary-article-number))
12034               (gnus-summary-set-process-mark (gnus-summary-article-number)))
12035             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
12036       (setq n (1- n)))
12037     (if (/= 0 n) (gnus-message 7 "No more articles"))
12038     (gnus-summary-recenter)
12039     (gnus-summary-position-point)
12040     n))
12041
12042 (defun gnus-summary-unmark-as-processable (n)
12043   "Remove the process mark from the next N articles.
12044 If N is negative, mark backward instead.  The difference between N and
12045 the actual number of articles marked is returned."
12046   (interactive "p")
12047   (gnus-set-global-variables)
12048   (gnus-summary-mark-as-processable n t))
12049
12050 (defun gnus-summary-unmark-all-processable ()
12051   "Remove the process mark from all articles."
12052   (interactive)
12053   (gnus-set-global-variables)
12054   (save-excursion
12055     (while gnus-newsgroup-processable
12056       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
12057   (gnus-summary-position-point))
12058
12059 (defun gnus-summary-mark-as-expirable (n)
12060   "Mark N articles forward as expirable.
12061 If N is negative, mark backward instead.  The difference between N and
12062 the actual number of articles marked is returned."
12063   (interactive "p")
12064   (gnus-set-global-variables)
12065   (gnus-summary-mark-forward n gnus-expirable-mark))
12066
12067 (defun gnus-summary-mark-article-as-replied (article)
12068   "Mark ARTICLE replied and update the summary line."
12069   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
12070   (let ((buffer-read-only nil))
12071     (when (gnus-summary-goto-subject article)
12072       (gnus-summary-update-secondary-mark article))))
12073
12074 (defun gnus-summary-set-bookmark (article)
12075   "Set a bookmark in current article."
12076   (interactive (list (gnus-summary-article-number)))
12077   (gnus-set-global-variables)
12078   (if (or (not (get-buffer gnus-article-buffer))
12079           (not gnus-current-article)
12080           (not gnus-article-current)
12081           (not (equal gnus-newsgroup-name (car gnus-article-current))))
12082       (error "No current article selected"))
12083   ;; Remove old bookmark, if one exists.
12084   (let ((old (assq article gnus-newsgroup-bookmarks)))
12085     (if old (setq gnus-newsgroup-bookmarks
12086                   (delq old gnus-newsgroup-bookmarks))))
12087   ;; Set the new bookmark, which is on the form
12088   ;; (article-number . line-number-in-body).
12089   (setq gnus-newsgroup-bookmarks
12090         (cons
12091          (cons article
12092                (save-excursion
12093                  (set-buffer gnus-article-buffer)
12094                  (count-lines
12095                   (min (point)
12096                        (save-excursion
12097                          (goto-char (point-min))
12098                          (search-forward "\n\n" nil t)
12099                          (point)))
12100                   (point))))
12101          gnus-newsgroup-bookmarks))
12102   (gnus-message 6 "A bookmark has been added to the current article."))
12103
12104 (defun gnus-summary-remove-bookmark (article)
12105   "Remove the bookmark from the current article."
12106   (interactive (list (gnus-summary-article-number)))
12107   (gnus-set-global-variables)
12108   ;; Remove old bookmark, if one exists.
12109   (let ((old (assq article gnus-newsgroup-bookmarks)))
12110     (if old
12111         (progn
12112           (setq gnus-newsgroup-bookmarks
12113                 (delq old gnus-newsgroup-bookmarks))
12114           (gnus-message 6 "Removed bookmark."))
12115       (gnus-message 6 "No bookmark in current article."))))
12116
12117 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12118 (defun gnus-summary-mark-as-dormant (n)
12119   "Mark N articles forward as dormant.
12120 If N is negative, mark backward instead.  The difference between N and
12121 the actual number of articles marked is returned."
12122   (interactive "p")
12123   (gnus-set-global-variables)
12124   (gnus-summary-mark-forward n gnus-dormant-mark))
12125
12126 (defun gnus-summary-set-process-mark (article)
12127   "Set the process mark on ARTICLE and update the summary line."
12128   (setq gnus-newsgroup-processable
12129         (cons article
12130               (delq article gnus-newsgroup-processable)))
12131   (when (gnus-summary-goto-subject article)
12132     (gnus-summary-show-thread)
12133     (gnus-summary-update-secondary-mark article)))
12134
12135 (defun gnus-summary-remove-process-mark (article)
12136   "Remove the process mark from ARTICLE and update the summary line."
12137   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
12138   (when (gnus-summary-goto-subject article)
12139     (gnus-summary-show-thread)
12140     (gnus-summary-update-secondary-mark article)))
12141
12142 (defun gnus-summary-set-saved-mark (article)
12143   "Set the process mark on ARTICLE and update the summary line."
12144   (push article gnus-newsgroup-saved)
12145   (when (gnus-summary-goto-subject article)
12146     (gnus-summary-update-secondary-mark article)))
12147
12148 (defun gnus-summary-mark-forward (n &optional mark no-expire)
12149   "Mark N articles as read forwards.
12150 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
12151 The difference between N and the actual number of articles marked is
12152 returned."
12153   (interactive "p")
12154   (gnus-set-global-variables)
12155   (let ((backward (< n 0))
12156         (gnus-summary-goto-unread
12157          (and gnus-summary-goto-unread
12158               (not (eq gnus-summary-goto-unread 'never))
12159               (not (memq mark (list gnus-unread-mark
12160                                     gnus-ticked-mark gnus-dormant-mark)))))
12161         (n (abs n))
12162         (mark (or mark gnus-del-mark)))
12163     (while (and (> n 0)
12164                 (gnus-summary-mark-article nil mark no-expire)
12165                 (zerop (gnus-summary-next-subject
12166                         (if backward -1 1)
12167                         (and gnus-summary-goto-unread
12168                              (not (eq gnus-summary-goto-unread 'never)))
12169                         t)))
12170       (setq n (1- n)))
12171     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12172     (gnus-summary-recenter)
12173     (gnus-summary-position-point)
12174     (gnus-set-mode-line 'summary)
12175     n))
12176
12177 (defun gnus-summary-mark-article-as-read (mark)
12178   "Mark the current article quickly as read with MARK."
12179   (let ((article (gnus-summary-article-number)))
12180     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12181     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12182     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12183     (setq gnus-newsgroup-reads
12184           (cons (cons article mark) gnus-newsgroup-reads))
12185     ;; Possibly remove from cache, if that is used.
12186     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12187     ;; Allow the backend to change the mark.
12188     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12189     ;; Check for auto-expiry.
12190     (when (and gnus-newsgroup-auto-expire
12191                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12192                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12193                    (= mark gnus-ancient-mark)
12194                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12195       (setq mark gnus-expirable-mark)
12196       (push article gnus-newsgroup-expirable))
12197     ;; Set the mark in the buffer.
12198     (gnus-summary-update-mark mark 'unread)
12199     t))
12200
12201 (defun gnus-summary-mark-article-as-unread (mark)
12202   "Mark the current article quickly as unread with MARK."
12203   (let ((article (gnus-summary-article-number)))
12204     (if (< article 0)
12205         (gnus-error 1 "Unmarkable article")
12206       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12207       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12208       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12209       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12210       (cond ((= mark gnus-ticked-mark)
12211              (push article gnus-newsgroup-marked))
12212             ((= mark gnus-dormant-mark)
12213              (push article gnus-newsgroup-dormant))
12214             (t
12215              (push article gnus-newsgroup-unreads)))
12216       (setq gnus-newsgroup-reads
12217             (delq (assq article gnus-newsgroup-reads)
12218                   gnus-newsgroup-reads))
12219
12220       ;; See whether the article is to be put in the cache.
12221       (and gnus-use-cache
12222            (vectorp (gnus-summary-article-header article))
12223            (save-excursion
12224              (gnus-cache-possibly-enter-article
12225               gnus-newsgroup-name article
12226               (gnus-summary-article-header article)
12227               (= mark gnus-ticked-mark)
12228               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12229
12230       ;; Fix the mark.
12231       (gnus-summary-update-mark mark 'unread))
12232     t))
12233
12234 (defun gnus-summary-mark-article (&optional article mark no-expire)
12235   "Mark ARTICLE with MARK.  MARK can be any character.
12236 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12237 `??' (dormant) and `?E' (expirable).
12238 If MARK is nil, then the default character `?D' is used.
12239 If ARTICLE is nil, then the article on the current line will be
12240 marked."
12241   ;; The mark might be a string.
12242   (and (stringp mark)
12243        (setq mark (aref mark 0)))
12244   ;; If no mark is given, then we check auto-expiring.
12245   (and (not no-expire)
12246        gnus-newsgroup-auto-expire
12247        (or (not mark)
12248            (and (numberp mark)
12249                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12250                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12251                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12252        (setq mark gnus-expirable-mark))
12253   (let* ((mark (or mark gnus-del-mark))
12254          (article (or article (gnus-summary-article-number))))
12255     (or article (error "No article on current line"))
12256     (if (or (= mark gnus-unread-mark)
12257             (= mark gnus-ticked-mark)
12258             (= mark gnus-dormant-mark))
12259         (gnus-mark-article-as-unread article mark)
12260       (gnus-mark-article-as-read article mark))
12261
12262     ;; See whether the article is to be put in the cache.
12263     (and gnus-use-cache
12264          (not (= mark gnus-canceled-mark))
12265          (vectorp (gnus-summary-article-header article))
12266          (save-excursion
12267            (gnus-cache-possibly-enter-article
12268             gnus-newsgroup-name article
12269             (gnus-summary-article-header article)
12270             (= mark gnus-ticked-mark)
12271             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12272
12273     (if (gnus-summary-goto-subject article nil t)
12274         (let ((buffer-read-only nil))
12275           (gnus-summary-show-thread)
12276           ;; Fix the mark.
12277           (gnus-summary-update-mark mark 'unread)
12278           t))))
12279
12280 (defun gnus-summary-update-secondary-mark (article)
12281   "Update the secondary (read, process, cache) mark."
12282   (gnus-summary-update-mark
12283    (cond ((memq article gnus-newsgroup-processable)
12284           gnus-process-mark)
12285          ((memq article gnus-newsgroup-cached)
12286           gnus-cached-mark)
12287          ((memq article gnus-newsgroup-replied)
12288           gnus-replied-mark)
12289          ((memq article gnus-newsgroup-saved)
12290           gnus-saved-mark)
12291          (t gnus-unread-mark))
12292    'replied)
12293   (when (gnus-visual-p 'summary-highlight 'highlight)
12294     (run-hooks 'gnus-summary-update-hook))
12295   t)
12296
12297 (defun gnus-summary-update-mark (mark type)
12298   (beginning-of-line)
12299   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12300         (buffer-read-only nil))
12301     (when (and forward
12302                (<= (+ forward (point)) (point-max)))
12303       ;; Go to the right position on the line.
12304       (goto-char (+ forward (point)))
12305       ;; Replace the old mark with the new mark.
12306       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12307       ;; Optionally update the marks by some user rule.
12308       (when (eq type 'unread)
12309         (gnus-data-set-mark
12310          (gnus-data-find (gnus-summary-article-number)) mark)
12311         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12312
12313 (defun gnus-mark-article-as-read (article &optional mark)
12314   "Enter ARTICLE in the pertinent lists and remove it from others."
12315   ;; Make the article expirable.
12316   (let ((mark (or mark gnus-del-mark)))
12317     (if (= mark gnus-expirable-mark)
12318         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12319       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12320     ;; Remove from unread and marked lists.
12321     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12322     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12323     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12324     (push (cons article mark) gnus-newsgroup-reads)
12325     ;; Possibly remove from cache, if that is used.
12326     (when gnus-use-cache
12327       (gnus-cache-enter-remove-article article))))
12328
12329 (defun gnus-mark-article-as-unread (article &optional mark)
12330   "Enter ARTICLE in the pertinent lists and remove it from others."
12331   (let ((mark (or mark gnus-ticked-mark)))
12332     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12333     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12334     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12335     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12336     (cond ((= mark gnus-ticked-mark)
12337            (push article gnus-newsgroup-marked))
12338           ((= mark gnus-dormant-mark)
12339            (push article gnus-newsgroup-dormant))
12340           (t
12341            (push article gnus-newsgroup-unreads)))
12342     (setq gnus-newsgroup-reads
12343           (delq (assq article gnus-newsgroup-reads)
12344                 gnus-newsgroup-reads))))
12345
12346 (defalias 'gnus-summary-mark-as-unread-forward
12347   'gnus-summary-tick-article-forward)
12348 (make-obsolete 'gnus-summary-mark-as-unread-forward
12349                'gnus-summary-tick-article-forward)
12350 (defun gnus-summary-tick-article-forward (n)
12351   "Tick N articles forwards.
12352 If N is negative, tick backwards instead.
12353 The difference between N and the number of articles ticked is returned."
12354   (interactive "p")
12355   (gnus-summary-mark-forward n gnus-ticked-mark))
12356
12357 (defalias 'gnus-summary-mark-as-unread-backward
12358   'gnus-summary-tick-article-backward)
12359 (make-obsolete 'gnus-summary-mark-as-unread-backward
12360                'gnus-summary-tick-article-backward)
12361 (defun gnus-summary-tick-article-backward (n)
12362   "Tick N articles backwards.
12363 The difference between N and the number of articles ticked is returned."
12364   (interactive "p")
12365   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12366
12367 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12368 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12369 (defun gnus-summary-tick-article (&optional article clear-mark)
12370   "Mark current article as unread.
12371 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12372 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12373   (interactive)
12374   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12375                                        gnus-ticked-mark)))
12376
12377 (defun gnus-summary-mark-as-read-forward (n)
12378   "Mark N articles as read forwards.
12379 If N is negative, mark backwards instead.
12380 The difference between N and the actual number of articles marked is
12381 returned."
12382   (interactive "p")
12383   (gnus-summary-mark-forward n gnus-del-mark t))
12384
12385 (defun gnus-summary-mark-as-read-backward (n)
12386   "Mark the N articles as read backwards.
12387 The difference between N and the actual number of articles marked is
12388 returned."
12389   (interactive "p")
12390   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12391
12392 (defun gnus-summary-mark-as-read (&optional article mark)
12393   "Mark current article as read.
12394 ARTICLE specifies the article to be marked as read.
12395 MARK specifies a string to be inserted at the beginning of the line."
12396   (gnus-summary-mark-article article mark))
12397
12398 (defun gnus-summary-clear-mark-forward (n)
12399   "Clear marks from N articles forward.
12400 If N is negative, clear backward instead.
12401 The difference between N and the number of marks cleared is returned."
12402   (interactive "p")
12403   (gnus-summary-mark-forward n gnus-unread-mark))
12404
12405 (defun gnus-summary-clear-mark-backward (n)
12406   "Clear marks from N articles backward.
12407 The difference between N and the number of marks cleared is returned."
12408   (interactive "p")
12409   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12410
12411 (defun gnus-summary-mark-unread-as-read ()
12412   "Intended to be used by `gnus-summary-mark-article-hook'."
12413   (when (memq gnus-current-article gnus-newsgroup-unreads)
12414     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12415
12416 (defun gnus-summary-mark-read-and-unread-as-read ()
12417   "Intended to be used by `gnus-summary-mark-article-hook'."
12418   (let ((mark (gnus-summary-article-mark)))
12419     (when (or (gnus-unread-mark-p mark)
12420               (gnus-read-mark-p mark))
12421       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12422
12423 (defun gnus-summary-mark-region-as-read (point mark all)
12424   "Mark all unread articles between point and mark as read.
12425 If given a prefix, mark all articles between point and mark as read,
12426 even ticked and dormant ones."
12427   (interactive "r\nP")
12428   (save-excursion
12429     (let (article)
12430       (goto-char point)
12431       (beginning-of-line)
12432       (while (and
12433               (< (point) mark)
12434               (progn
12435                 (when (or all
12436                           (memq (setq article (gnus-summary-article-number))
12437                                 gnus-newsgroup-unreads))
12438                   (gnus-summary-mark-article article gnus-del-mark))
12439                 t)
12440               (gnus-summary-find-next))))))
12441
12442 (defun gnus-summary-mark-below (score mark)
12443   "Mark articles with score less than SCORE with MARK."
12444   (interactive "P\ncMark: ")
12445   (gnus-set-global-variables)
12446   (setq score (if score
12447                   (prefix-numeric-value score)
12448                 (or gnus-summary-default-score 0)))
12449   (save-excursion
12450     (set-buffer gnus-summary-buffer)
12451     (goto-char (point-min))
12452     (while 
12453         (progn
12454           (and (< (gnus-summary-article-score) score)
12455                (gnus-summary-mark-article nil mark))
12456           (gnus-summary-find-next)))))
12457
12458 (defun gnus-summary-kill-below (&optional score)
12459   "Mark articles with score below SCORE as read."
12460   (interactive "P")
12461   (gnus-set-global-variables)
12462   (gnus-summary-mark-below score gnus-killed-mark))
12463
12464 (defun gnus-summary-clear-above (&optional score)
12465   "Clear all marks from articles with score above SCORE."
12466   (interactive "P")
12467   (gnus-set-global-variables)
12468   (gnus-summary-mark-above score gnus-unread-mark))
12469
12470 (defun gnus-summary-tick-above (&optional score)
12471   "Tick all articles with score above SCORE."
12472   (interactive "P")
12473   (gnus-set-global-variables)
12474   (gnus-summary-mark-above score gnus-ticked-mark))
12475
12476 (defun gnus-summary-mark-above (score mark)
12477   "Mark articles with score over SCORE with MARK."
12478   (interactive "P\ncMark: ")
12479   (gnus-set-global-variables)
12480   (setq score (if score
12481                   (prefix-numeric-value score)
12482                 (or gnus-summary-default-score 0)))
12483   (save-excursion
12484     (set-buffer gnus-summary-buffer)
12485     (goto-char (point-min))
12486     (while (and (progn
12487                   (if (> (gnus-summary-article-score) score)
12488                       (gnus-summary-mark-article nil mark))
12489                   t)
12490                 (gnus-summary-find-next)))))
12491
12492 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12493 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12494 (defun gnus-summary-limit-include-expunged ()
12495   "Display all the hidden articles that were expunged for low scores."
12496   (interactive)
12497   (gnus-set-global-variables)
12498   (let ((buffer-read-only nil))
12499     (let ((scored gnus-newsgroup-scored)
12500           headers h)
12501       (while scored
12502         (or (gnus-summary-goto-subject (caar scored))
12503             (and (setq h (gnus-summary-article-header (caar scored)))
12504                  (< (cdar scored) gnus-summary-expunge-below)
12505                  (setq headers (cons h headers))))
12506         (setq scored (cdr scored)))
12507       (or headers (error "No expunged articles hidden."))
12508       (goto-char (point-min))
12509       (gnus-summary-prepare-unthreaded (nreverse headers)))
12510     (goto-char (point-min))
12511     (gnus-summary-position-point)))
12512
12513 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12514   "Mark all articles not marked as unread in this newsgroup as read.
12515 If prefix argument ALL is non-nil, all articles are marked as read.
12516 If QUIETLY is non-nil, no questions will be asked.
12517 If TO-HERE is non-nil, it should be a point in the buffer.  All
12518 articles before this point will be marked as read.
12519 The number of articles marked as read is returned."
12520   (interactive "P")
12521   (gnus-set-global-variables)
12522   (prog1
12523       (if (or quietly
12524               (not gnus-interactive-catchup) ;Without confirmation?
12525               gnus-expert-user
12526               (gnus-y-or-n-p
12527                (if all
12528                    "Mark absolutely all articles as read? "
12529                  "Mark all unread articles as read? ")))
12530           (if (and not-mark
12531                    (not gnus-newsgroup-adaptive)
12532                    (not gnus-newsgroup-auto-expire))
12533               (progn
12534                 (when all
12535                   (setq gnus-newsgroup-marked nil
12536                         gnus-newsgroup-dormant nil))
12537                 (setq gnus-newsgroup-unreads nil))
12538             ;; We actually mark all articles as canceled, which we
12539             ;; have to do when using auto-expiry or adaptive scoring.
12540             (gnus-summary-show-all-threads)
12541             (if (gnus-summary-first-subject (not all))
12542                 (while (and
12543                         (if to-here (< (point) to-here) t)
12544                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12545                         (gnus-summary-find-next (not all)))))
12546             (unless to-here
12547               (setq gnus-newsgroup-unreads nil))
12548             (gnus-set-mode-line 'summary)))
12549     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12550       (if (and (not to-here) (eq 'nnvirtual (car method)))
12551           (nnvirtual-catchup-group
12552            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12553     (gnus-summary-position-point)))
12554
12555 (defun gnus-summary-catchup-to-here (&optional all)
12556   "Mark all unticked articles before the current one as read.
12557 If ALL is non-nil, also mark ticked and dormant articles as read."
12558   (interactive "P")
12559   (gnus-set-global-variables)
12560   (save-excursion
12561     (gnus-save-hidden-threads
12562       (let ((beg (point)))
12563         ;; We check that there are unread articles.
12564         (when (or all (gnus-summary-find-prev))
12565           (gnus-summary-catchup all t beg)))))
12566   (gnus-summary-position-point))
12567
12568 (defun gnus-summary-catchup-all (&optional quietly)
12569   "Mark all articles in this newsgroup as read."
12570   (interactive "P")
12571   (gnus-set-global-variables)
12572   (gnus-summary-catchup t quietly))
12573
12574 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12575   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12576 If prefix argument ALL is non-nil, all articles are marked as read."
12577   (interactive "P")
12578   (gnus-set-global-variables)
12579   (gnus-summary-catchup all quietly nil 'fast)
12580   ;; Select next newsgroup or exit.
12581   (if (eq gnus-auto-select-next 'quietly)
12582       (gnus-summary-next-group nil)
12583     (gnus-summary-exit)))
12584
12585 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12586   "Mark all articles in this newsgroup as read, and then exit."
12587   (interactive "P")
12588   (gnus-set-global-variables)
12589   (gnus-summary-catchup-and-exit t quietly))
12590
12591 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12592 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12593   "Mark all articles in this group as read and select the next group.
12594 If given a prefix, mark all articles, unread as well as ticked, as
12595 read."
12596   (interactive "P")
12597   (gnus-set-global-variables)
12598   (save-excursion
12599     (gnus-summary-catchup all))
12600   (gnus-summary-next-article t nil nil t))
12601
12602 ;; Thread-based commands.
12603
12604 (defun gnus-summary-articles-in-thread (&optional article)
12605   "Return a list of all articles in the current thread.
12606 If ARTICLE is non-nil, return all articles in the thread that starts
12607 with that article."
12608   (let* ((article (or article (gnus-summary-article-number)))
12609          (data (gnus-data-find-list article))
12610          (top-level (gnus-data-level (car data)))
12611          (top-subject
12612           (cond ((null gnus-thread-operation-ignore-subject)
12613                  (gnus-simplify-subject-re
12614                   (mail-header-subject (gnus-data-header (car data)))))
12615                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12616                  (gnus-simplify-subject-fuzzy
12617                   (mail-header-subject (gnus-data-header (car data)))))
12618                 (t nil)))
12619          (end-point (save-excursion
12620                       (if (gnus-summary-go-to-next-thread) 
12621                           (point) (point-max))))
12622          articles)
12623     (while (and data
12624                 (< (gnus-data-pos (car data)) end-point))
12625       (when (or (not top-subject)
12626                 (string= top-subject
12627                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12628                              (gnus-simplify-subject-fuzzy
12629                               (mail-header-subject
12630                                (gnus-data-header (car data))))
12631                            (gnus-simplify-subject-re
12632                             (mail-header-subject
12633                              (gnus-data-header (car data)))))))
12634         (push (gnus-data-number (car data)) articles))
12635       (unless (and (setq data (cdr data))
12636                    (> (gnus-data-level (car data)) top-level))
12637         (setq data nil)))
12638     ;; Return the list of articles.
12639     (nreverse articles)))
12640
12641 (defun gnus-summary-rethread-current ()
12642   "Rethread the thread the current article is part of."
12643   (interactive)
12644   (gnus-set-global-variables)
12645   (let* ((gnus-show-threads t)
12646          (article (gnus-summary-article-number))
12647          (id (mail-header-id (gnus-summary-article-header)))
12648          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12649     (unless id
12650       (error "No article on the current line"))
12651     (gnus-rebuild-thread id)
12652     (gnus-summary-goto-subject article)))
12653
12654 (defun gnus-summary-reparent-thread ()
12655   "Make current article child of the marked (or previous) article.
12656
12657 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12658 is non-nil or the Subject: of both articles are the same."
12659   (interactive)
12660   (or (not (gnus-group-read-only-p))
12661       (error "The current newsgroup does not support article editing."))
12662   (or (<= (length gnus-newsgroup-processable) 1)
12663       (error "No more than one article may be marked."))
12664   (save-window-excursion
12665     (let ((gnus-article-buffer " *reparent*")
12666           (current-article (gnus-summary-article-number))
12667           ; first grab the marked article, otherwise one line up.
12668           (parent-article (if (not (null gnus-newsgroup-processable))
12669                               (car gnus-newsgroup-processable)
12670                             (save-excursion
12671                               (if (eq (forward-line -1) 0)
12672                                   (gnus-summary-article-number)
12673                                 (error "Beginning of summary buffer."))))))
12674       (or (not (eq current-article parent-article))
12675           (error "An article may not be self-referential."))
12676       (let ((message-id (mail-header-id 
12677                          (gnus-summary-article-header parent-article))))
12678         (or (and message-id (not (equal message-id "")))
12679             (error "No message-id in desired parent."))
12680         (gnus-summary-select-article t t nil current-article)
12681         (set-buffer gnus-article-buffer)
12682         (setq buffer-read-only nil)
12683         (let ((buf (format "%s" (buffer-string))))
12684           (erase-buffer)
12685           (insert buf))
12686         (goto-char (point-min))
12687         (if (search-forward-regexp "^References: " nil t)
12688             (insert message-id " " )
12689           (insert "References: " message-id "\n"))
12690         (or (gnus-request-replace-article current-article
12691                                           (car gnus-article-current)
12692                                           gnus-article-buffer)
12693             (error "Couldn't replace article."))
12694         (set-buffer gnus-summary-buffer)
12695         (gnus-summary-unmark-all-processable)
12696         (gnus-summary-rethread-current)
12697         (gnus-message 3 "Article %d is now the child of article %d."
12698                       current-article parent-article)))))
12699
12700 (defun gnus-summary-toggle-threads (&optional arg)
12701   "Toggle showing conversation threads.
12702 If ARG is positive number, turn showing conversation threads on."
12703   (interactive "P")
12704   (gnus-set-global-variables)
12705   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12706     (setq gnus-show-threads
12707           (if (null arg) (not gnus-show-threads)
12708             (> (prefix-numeric-value arg) 0)))
12709     (gnus-summary-prepare)
12710     (gnus-summary-goto-subject current)
12711     (gnus-summary-position-point)))
12712
12713 (defun gnus-summary-show-all-threads ()
12714   "Show all threads."
12715   (interactive)
12716   (gnus-set-global-variables)
12717   (save-excursion
12718     (let ((buffer-read-only nil))
12719       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12720   (gnus-summary-position-point))
12721
12722 (defun gnus-summary-show-thread ()
12723   "Show thread subtrees.
12724 Returns nil if no thread was there to be shown."
12725   (interactive)
12726   (gnus-set-global-variables)
12727   (let ((buffer-read-only nil)
12728         (orig (point))
12729         ;; first goto end then to beg, to have point at beg after let
12730         (end (progn (end-of-line) (point)))
12731         (beg (progn (beginning-of-line) (point))))
12732     (prog1
12733         ;; Any hidden lines here?
12734         (search-forward "\r" end t)
12735       (subst-char-in-region beg end ?\^M ?\n t)
12736       (goto-char orig)
12737       (gnus-summary-position-point))))
12738
12739 (defun gnus-summary-hide-all-threads ()
12740   "Hide all thread subtrees."
12741   (interactive)
12742   (gnus-set-global-variables)
12743   (save-excursion
12744     (goto-char (point-min))
12745     (gnus-summary-hide-thread)
12746     (while (zerop (gnus-summary-next-thread 1 t))
12747       (gnus-summary-hide-thread)))
12748   (gnus-summary-position-point))
12749
12750 (defun gnus-summary-hide-thread ()
12751   "Hide thread subtrees.
12752 Returns nil if no threads were there to be hidden."
12753   (interactive)
12754   (gnus-set-global-variables)
12755   (let ((buffer-read-only nil)
12756         (start (point))
12757         (article (gnus-summary-article-number)))
12758     (goto-char start)
12759     ;; Go forward until either the buffer ends or the subthread
12760     ;; ends.
12761     (when (and (not (eobp))
12762                (or (zerop (gnus-summary-next-thread 1 t))
12763                    (goto-char (point-max))))
12764       (prog1
12765           (if (and (> (point) start)
12766                    (search-backward "\n" start t))
12767               (progn
12768                 (subst-char-in-region start (point) ?\n ?\^M)
12769                 (gnus-summary-goto-subject article))
12770             (goto-char start)
12771             nil)
12772         ;;(gnus-summary-position-point)
12773         ))))
12774
12775 (defun gnus-summary-go-to-next-thread (&optional previous)
12776   "Go to the same level (or less) next thread.
12777 If PREVIOUS is non-nil, go to previous thread instead.
12778 Return the article number moved to, or nil if moving was impossible."
12779   (let ((level (gnus-summary-thread-level))
12780         (way (if previous -1 1))
12781         (beg (point)))
12782     (forward-line way)
12783     (while (and (not (eobp))
12784                 (< level (gnus-summary-thread-level)))
12785       (forward-line way))
12786     (if (eobp)
12787         (progn
12788           (goto-char beg)
12789           nil)
12790       (setq beg (point))
12791       (prog1
12792           (gnus-summary-article-number)
12793         (goto-char beg)))))
12794
12795 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12796   "Go to the same level (or less) next thread.
12797 If PREVIOUS is non-nil, go to previous thread instead.
12798 Return the article number moved to, or nil if moving was impossible."
12799   (if (and (eq gnus-summary-make-false-root 'dummy)
12800            (gnus-summary-article-intangible-p))
12801       (let ((beg (point)))
12802         (while (and (zerop (forward-line 1))
12803                     (not (gnus-summary-article-intangible-p))
12804                     (not (zerop (save-excursion 
12805                                   (gnus-summary-thread-level))))))
12806         (if (eobp)
12807             (progn
12808               (goto-char beg)
12809               nil)
12810           (point)))
12811     (let* ((level (gnus-summary-thread-level))
12812            (article (gnus-summary-article-number))
12813            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12814            oart)
12815       (while data
12816         (if (<= (gnus-data-level (car data)) level)
12817             (setq oart (gnus-data-number (car data))
12818                   data nil)
12819           (setq data (cdr data))))
12820       (and oart
12821            (gnus-summary-goto-subject oart)))))
12822
12823 (defun gnus-summary-next-thread (n &optional silent)
12824   "Go to the same level next N'th thread.
12825 If N is negative, search backward instead.
12826 Returns the difference between N and the number of skips actually
12827 done.
12828
12829 If SILENT, don't output messages."
12830   (interactive "p")
12831   (gnus-set-global-variables)
12832   (let ((backward (< n 0))
12833         (n (abs n))
12834         old dum int)
12835     (while (and (> n 0)
12836                 (gnus-summary-go-to-next-thread backward))
12837       (decf n))
12838     (unless silent 
12839       (gnus-summary-position-point))
12840     (when (and (not silent) (/= 0 n))
12841       (gnus-message 7 "No more threads"))
12842     n))
12843
12844 (defun gnus-summary-prev-thread (n)
12845   "Go to the same level previous N'th thread.
12846 Returns the difference between N and the number of skips actually
12847 done."
12848   (interactive "p")
12849   (gnus-set-global-variables)
12850   (gnus-summary-next-thread (- n)))
12851
12852 (defun gnus-summary-go-down-thread ()
12853   "Go down one level in the current thread."
12854   (let ((children (gnus-summary-article-children)))
12855     (and children
12856          (gnus-summary-goto-subject (car children)))))
12857
12858 (defun gnus-summary-go-up-thread ()
12859   "Go up one level in the current thread."
12860   (let ((parent (gnus-summary-article-parent)))
12861     (and parent
12862          (gnus-summary-goto-subject parent))))
12863
12864 (defun gnus-summary-down-thread (n)
12865   "Go down thread N steps.
12866 If N is negative, go up instead.
12867 Returns the difference between N and how many steps down that were
12868 taken."
12869   (interactive "p")
12870   (gnus-set-global-variables)
12871   (let ((up (< n 0))
12872         (n (abs n)))
12873     (while (and (> n 0)
12874                 (if up (gnus-summary-go-up-thread)
12875                   (gnus-summary-go-down-thread)))
12876       (setq n (1- n)))
12877     (gnus-summary-position-point)
12878     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12879     n))
12880
12881 (defun gnus-summary-up-thread (n)
12882   "Go up thread N steps.
12883 If N is negative, go up instead.
12884 Returns the difference between N and how many steps down that were
12885 taken."
12886   (interactive "p")
12887   (gnus-set-global-variables)
12888   (gnus-summary-down-thread (- n)))
12889
12890 (defun gnus-summary-top-thread ()
12891   "Go to the top of the thread."
12892   (interactive)
12893   (gnus-set-global-variables)
12894   (while (gnus-summary-go-up-thread))
12895   (gnus-summary-article-number))
12896
12897 (defun gnus-summary-kill-thread (&optional unmark)
12898   "Mark articles under current thread as read.
12899 If the prefix argument is positive, remove any kinds of marks.
12900 If the prefix argument is negative, tick articles instead."
12901   (interactive "P")
12902   (gnus-set-global-variables)
12903   (when unmark
12904     (setq unmark (prefix-numeric-value unmark)))
12905   (let ((articles (gnus-summary-articles-in-thread)))
12906     (save-excursion
12907       ;; Expand the thread.
12908       (gnus-summary-show-thread)
12909       ;; Mark all the articles.
12910       (while articles
12911         (gnus-summary-goto-subject (car articles))
12912         (cond ((null unmark)
12913                (gnus-summary-mark-article-as-read gnus-killed-mark))
12914               ((> unmark 0)
12915                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12916               (t
12917                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12918         (setq articles (cdr articles))))
12919     ;; Hide killed subtrees.
12920     (and (null unmark)
12921          gnus-thread-hide-killed
12922          (gnus-summary-hide-thread))
12923     ;; If marked as read, go to next unread subject.
12924     (if (null unmark)
12925         ;; Go to next unread subject.
12926         (gnus-summary-next-subject 1 t)))
12927   (gnus-set-mode-line 'summary))
12928
12929 ;; Summary sorting commands
12930
12931 (defun gnus-summary-sort-by-number (&optional reverse)
12932   "Sort summary buffer by article number.
12933 Argument REVERSE means reverse order."
12934   (interactive "P")
12935   (gnus-summary-sort 'number reverse))
12936
12937 (defun gnus-summary-sort-by-author (&optional reverse)
12938   "Sort summary buffer by author name alphabetically.
12939 If case-fold-search is non-nil, case of letters is ignored.
12940 Argument REVERSE means reverse order."
12941   (interactive "P")
12942   (gnus-summary-sort 'author reverse))
12943
12944 (defun gnus-summary-sort-by-subject (&optional reverse)
12945   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12946 If case-fold-search is non-nil, case of letters is ignored.
12947 Argument REVERSE means reverse order."
12948   (interactive "P")
12949   (gnus-summary-sort 'subject reverse))
12950
12951 (defun gnus-summary-sort-by-date (&optional reverse)
12952   "Sort summary buffer by date.
12953 Argument REVERSE means reverse order."
12954   (interactive "P")
12955   (gnus-summary-sort 'date reverse))
12956
12957 (defun gnus-summary-sort-by-score (&optional reverse)
12958   "Sort summary buffer by score.
12959 Argument REVERSE means reverse order."
12960   (interactive "P")
12961   (gnus-summary-sort 'score reverse))
12962
12963 (defun gnus-summary-sort (predicate reverse)
12964   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12965   (gnus-set-global-variables)
12966   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12967          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12968          (gnus-thread-sort-functions
12969           (list
12970            (if (not reverse)
12971                thread
12972              `(lambda (t1 t2)
12973                 (,thread t2 t1)))))
12974          (gnus-article-sort-functions
12975           (list
12976            (if (not reverse)
12977                article
12978              `(lambda (t1 t2)
12979                 (,article t2 t1)))))
12980          (buffer-read-only)
12981          (gnus-summary-prepare-hook nil))
12982     ;; We do the sorting by regenerating the threads.
12983     (gnus-summary-prepare)
12984     ;; Hide subthreads if needed.
12985     (when (and gnus-show-threads gnus-thread-hide-subtree)
12986       (gnus-summary-hide-all-threads)))
12987   ;; If in async mode, we send some info to the backend.
12988   (when gnus-newsgroup-async
12989     (gnus-request-asynchronous
12990      gnus-newsgroup-name gnus-newsgroup-data)))
12991
12992 (defun gnus-sortable-date (date)
12993   "Make sortable string by string-lessp from DATE.
12994 Timezone package is used."
12995   (condition-case ()
12996       (progn
12997         (setq date (inline (timezone-fix-time 
12998                             date nil 
12999                             (aref (inline (timezone-parse-date date)) 4))))
13000         (inline
13001           (timezone-make-sortable-date
13002            (aref date 0) (aref date 1) (aref date 2)
13003            (inline
13004              (timezone-make-time-string
13005               (aref date 3) (aref date 4) (aref date 5))))))
13006     (error "")))
13007   
13008 ;; Summary saving commands.
13009
13010 (defun gnus-summary-save-article (&optional n not-saved)
13011   "Save the current article using the default saver function.
13012 If N is a positive number, save the N next articles.
13013 If N is a negative number, save the N previous articles.
13014 If N is nil and any articles have been marked with the process mark,
13015 save those articles instead.
13016 The variable `gnus-default-article-saver' specifies the saver function."
13017   (interactive "P")
13018   (gnus-set-global-variables)
13019   (let ((articles (gnus-summary-work-articles n))
13020         (save-buffer (save-excursion 
13021                        (nnheader-set-temp-buffer " *Gnus Save*")))
13022         file header article)
13023     (while articles
13024       (setq header (gnus-summary-article-header
13025                     (setq article (pop articles))))
13026       (if (not (vectorp header))
13027           ;; This is a pseudo-article.
13028           (if (assq 'name header)
13029               (gnus-copy-file (cdr (assq 'name header)))
13030             (gnus-message 1 "Article %d is unsaveable" article))
13031         ;; This is a real article.
13032         (save-window-excursion
13033           (gnus-summary-select-article t nil nil article))
13034         (save-excursion
13035           (set-buffer save-buffer)
13036           (erase-buffer)
13037           (insert-buffer-substring gnus-original-article-buffer))
13038         (unless gnus-save-all-headers
13039           ;; Remove headers accoring to `gnus-saved-headers'.
13040           (let ((gnus-visible-headers
13041                  (or gnus-saved-headers gnus-visible-headers))
13042                 (gnus-article-buffer save-buffer))
13043             (gnus-article-hide-headers 1 t)))
13044         (save-window-excursion
13045           (if (not gnus-default-article-saver)
13046               (error "No default saver is defined.")
13047             ;; !!! Magic!  The saving functions all save
13048             ;; `gnus-original-article-buffer' (or so they think),
13049             ;; but we bind that variable to our save-buffer.
13050             (set-buffer gnus-article-buffer)
13051             (let ((gnus-original-article-buffer save-buffer))
13052               (set-buffer gnus-summary-buffer)
13053               (setq file (funcall
13054                           gnus-default-article-saver
13055                           (cond
13056                            ((not gnus-prompt-before-saving)
13057                             'default)
13058                            ((eq gnus-prompt-before-saving 'always)
13059                             nil)
13060                            (t file)))))))
13061         (gnus-summary-remove-process-mark article)
13062         (unless not-saved
13063           (gnus-summary-set-saved-mark article))))
13064     (gnus-kill-buffer save-buffer)
13065     (gnus-summary-position-point)
13066     n))
13067
13068 (defun gnus-summary-pipe-output (&optional arg)
13069   "Pipe the current article to a subprocess.
13070 If N is a positive number, pipe the N next articles.
13071 If N is a negative number, pipe the N previous articles.
13072 If N is nil and any articles have been marked with the process mark,
13073 pipe those articles instead."
13074   (interactive "P")
13075   (gnus-set-global-variables)
13076   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
13077     (gnus-summary-save-article arg t))
13078   (gnus-configure-windows 'pipe))
13079
13080 (defun gnus-summary-save-article-mail (&optional arg)
13081   "Append the current article to an mail file.
13082 If N is a positive number, save the N next articles.
13083 If N is a negative number, save the N previous articles.
13084 If N is nil and any articles have been marked with the process mark,
13085 save those articles instead."
13086   (interactive "P")
13087   (gnus-set-global-variables)
13088   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
13089     (gnus-summary-save-article arg)))
13090
13091 (defun gnus-summary-save-article-rmail (&optional arg)
13092   "Append the current article to an rmail file.
13093 If N is a positive number, save the N next articles.
13094 If N is a negative number, save the N previous articles.
13095 If N is nil and any articles have been marked with the process mark,
13096 save those articles instead."
13097   (interactive "P")
13098   (gnus-set-global-variables)
13099   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
13100     (gnus-summary-save-article arg)))
13101
13102 (defun gnus-summary-save-article-file (&optional arg)
13103   "Append the current article to a file.
13104 If N is a positive number, save the N next articles.
13105 If N is a negative number, save the N previous articles.
13106 If N is nil and any articles have been marked with the process mark,
13107 save those articles instead."
13108   (interactive "P")
13109   (gnus-set-global-variables)
13110   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
13111     (gnus-summary-save-article arg)))
13112
13113 (defun gnus-summary-save-article-body-file (&optional arg)
13114   "Append the current article body to a file.
13115 If N is a positive number, save the N next articles.
13116 If N is a negative number, save the N previous articles.
13117 If N is nil and any articles have been marked with the process mark,
13118 save those articles instead."
13119   (interactive "P")
13120   (gnus-set-global-variables)
13121   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
13122     (gnus-summary-save-article arg)))
13123
13124 (defun gnus-get-split-value (methods)
13125   "Return a value based on the split METHODS."
13126   (let (split-name method result match)
13127     (when methods
13128       (save-excursion
13129         (set-buffer gnus-original-article-buffer)
13130         (save-restriction
13131           (nnheader-narrow-to-headers)
13132           (while methods
13133             (goto-char (point-min))
13134             (setq method (pop methods))
13135             (setq match (car method))
13136             (when (cond
13137                    ((stringp match)
13138                     ;; Regular expression.
13139                     (condition-case ()
13140                         (re-search-forward match nil t)
13141                       (error nil)))
13142                    ((gnus-functionp match)
13143                     ;; Function.
13144                     (save-restriction
13145                       (widen)
13146                       (setq result (funcall match gnus-newsgroup-name))))
13147                    ((consp match)
13148                     ;; Form.
13149                     (save-restriction
13150                       (widen)
13151                       (setq result (eval match)))))
13152               (setq split-name (append (cdr method) split-name))
13153               (cond ((stringp result)
13154                      (push result split-name))
13155                     ((consp result)
13156                      (setq split-name (append result split-name)))))))))
13157     split-name))
13158
13159 (defun gnus-read-move-group-name (prompt default articles prefix)
13160   "Read a group name."
13161   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13162          (minibuffer-confirm-incomplete nil) ; XEmacs
13163          group-map
13164          (dum (mapatoms
13165                (lambda (g) 
13166                  (and (boundp g)
13167                       (symbol-name g)
13168                       (memq 'respool
13169                             (assoc (symbol-name
13170                                     (car (gnus-find-method-for-group
13171                                           (symbol-name g))))
13172                                    gnus-valid-select-methods))
13173                       (push (list (symbol-name g)) group-map)))
13174                gnus-active-hashtb))
13175          (prom
13176           (format "%s %s to:"
13177                   prompt
13178                   (if (> (length articles) 1)
13179                       (format "these %d articles" (length articles))
13180                     "this article")))
13181          (to-newsgroup
13182           (cond
13183            ((null split-name)
13184             (gnus-completing-read default prom
13185                                   group-map nil nil prefix
13186                                   'gnus-group-history))
13187            ((= 1 (length split-name))
13188             (gnus-completing-read (car split-name) prom group-map
13189                                   nil nil nil
13190                                   'gnus-group-history))
13191            (t
13192             (gnus-completing-read nil prom 
13193                                   (mapcar (lambda (el) (list el))
13194                                           (nreverse split-name))
13195                                   nil nil nil
13196                                   'gnus-group-history)))))
13197     (when to-newsgroup
13198       (if (or (string= to-newsgroup "")
13199               (string= to-newsgroup prefix))
13200           (setq to-newsgroup (or default "")))
13201       (or (gnus-active to-newsgroup)
13202           (gnus-activate-group to-newsgroup)
13203           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
13204                                      to-newsgroup))
13205               (or (and (gnus-request-create-group 
13206                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
13207                        (gnus-activate-group to-newsgroup nil nil
13208                                             (gnus-group-name-to-method
13209                                              to-newsgroup)))
13210                   (error "Couldn't create group %s" to-newsgroup)))
13211           (error "No such group: %s" to-newsgroup)))
13212     to-newsgroup))
13213
13214 (defun gnus-read-save-file-name (prompt default-name)
13215   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13216          (file
13217           ;; Let the split methods have their say.
13218           (cond
13219            ;; No split name was found.
13220            ((null split-name)
13221             (read-file-name
13222              (concat prompt " (default "
13223                      (file-name-nondirectory default-name) ") ")
13224              (file-name-directory default-name)
13225              default-name))
13226            ;; A single split name was found
13227            ((= 1 (length split-name))
13228             (let* ((name (car split-name))
13229                    (dir (cond ((file-directory-p name)
13230                                (file-name-as-directory name))
13231                               ((file-exists-p name) name)
13232                               (t gnus-article-save-directory))))
13233               (read-file-name
13234                (concat prompt " (default " name ") ")
13235                dir name)))
13236            ;; A list of splits was found.
13237            (t
13238             (setq split-name (nreverse split-name))
13239             (let (result)
13240               (let ((file-name-history (nconc split-name file-name-history)))
13241                 (setq result
13242                       (read-file-name
13243                        (concat prompt " (`M-p' for defaults) ")
13244                        gnus-article-save-directory
13245                        (car split-name))))
13246               (car (push result file-name-history)))))))
13247     ;; If we have read a directory, we append the default file name.
13248     (when (file-directory-p file)
13249       (setq file (concat (file-name-as-directory file)
13250                          (file-name-nondirectory default-name))))
13251     ;; Possibly translate some charaters.
13252     (nnheader-translate-file-chars file)))
13253
13254 (defun gnus-article-archive-name (group)
13255   "Return the first instance of an \"Archive-name\" in the current buffer."
13256   (let ((case-fold-search t))
13257     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13258       (match-string 1))))
13259
13260 (defun gnus-summary-save-in-rmail (&optional filename)
13261   "Append this article to Rmail file.
13262 Optional argument FILENAME specifies file name.
13263 Directory to save to is default to `gnus-article-save-directory'."
13264   (interactive)
13265   (gnus-set-global-variables)
13266   (let ((default-name
13267           (funcall gnus-rmail-save-name gnus-newsgroup-name
13268                    gnus-current-headers gnus-newsgroup-last-rmail)))
13269     (setq filename
13270           (cond ((eq filename 'default)
13271                  default-name)
13272                 (filename filename)
13273                 (t (gnus-read-save-file-name
13274                     "Save in rmail file:" default-name))))
13275     (gnus-make-directory (file-name-directory filename))
13276     (gnus-eval-in-buffer-window gnus-original-article-buffer
13277       (save-excursion
13278         (save-restriction
13279           (widen)
13280           (gnus-output-to-rmail filename))))
13281     ;; Remember the directory name to save articles
13282     (setq gnus-newsgroup-last-rmail filename)))
13283
13284 (defun gnus-summary-save-in-mail (&optional filename)
13285   "Append this article to Unix mail file.
13286 Optional argument FILENAME specifies file name.
13287 Directory to save to is default to `gnus-article-save-directory'."
13288   (interactive)
13289   (gnus-set-global-variables)
13290   (let ((default-name
13291           (funcall gnus-mail-save-name gnus-newsgroup-name
13292                    gnus-current-headers gnus-newsgroup-last-mail)))
13293     (setq filename
13294           (cond ((eq filename 'default)
13295                  default-name)
13296                 (filename filename)
13297                 (t (gnus-read-save-file-name
13298                     "Save in Unix mail file:" default-name))))
13299     (setq filename
13300           (expand-file-name filename
13301                             (and default-name
13302                                  (file-name-directory default-name))))
13303     (gnus-make-directory (file-name-directory filename))
13304     (gnus-eval-in-buffer-window gnus-original-article-buffer
13305       (save-excursion
13306         (save-restriction
13307           (widen)
13308           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13309               (gnus-output-to-rmail filename)
13310             (let ((mail-use-rfc822 t))
13311               (rmail-output filename 1 t t))))))
13312     ;; Remember the directory name to save articles.
13313     (setq gnus-newsgroup-last-mail filename)))
13314
13315 (defun gnus-summary-save-in-file (&optional filename)
13316   "Append this article to file.
13317 Optional argument FILENAME specifies file name.
13318 Directory to save to is default to `gnus-article-save-directory'."
13319   (interactive)
13320   (gnus-set-global-variables)
13321   (let ((default-name
13322           (funcall gnus-file-save-name gnus-newsgroup-name
13323                    gnus-current-headers gnus-newsgroup-last-file)))
13324     (setq filename
13325           (cond ((eq filename 'default)
13326                  default-name)
13327                 (filename filename)
13328                 (t (gnus-read-save-file-name
13329                     "Save in file:" default-name))))
13330     (gnus-make-directory (file-name-directory filename))
13331     (gnus-eval-in-buffer-window gnus-original-article-buffer
13332       (save-excursion
13333         (save-restriction
13334           (widen)
13335           (gnus-output-to-file filename))))
13336     ;; Remember the directory name to save articles.
13337     (setq gnus-newsgroup-last-file filename)))
13338
13339 (defun gnus-summary-save-body-in-file (&optional filename)
13340   "Append this article body to a file.
13341 Optional argument FILENAME specifies file name.
13342 The directory to save in defaults to `gnus-article-save-directory'."
13343   (interactive)
13344   (gnus-set-global-variables)
13345   (let ((default-name
13346           (funcall gnus-file-save-name gnus-newsgroup-name
13347                    gnus-current-headers gnus-newsgroup-last-file)))
13348     (setq filename
13349           (cond ((eq filename 'default)
13350                  default-name)
13351                 (filename filename)
13352                 (t (gnus-read-save-file-name
13353                     "Save body in file:" default-name))))
13354     (gnus-make-directory (file-name-directory filename))
13355     (gnus-eval-in-buffer-window gnus-original-article-buffer
13356       (save-excursion
13357         (save-restriction
13358           (widen)
13359           (goto-char (point-min))
13360           (and (search-forward "\n\n" nil t)
13361                (narrow-to-region (point) (point-max)))
13362           (gnus-output-to-file filename))))
13363     ;; Remember the directory name to save articles.
13364     (setq gnus-newsgroup-last-file filename)))
13365
13366 (defun gnus-summary-save-in-pipe (&optional command)
13367   "Pipe this article to subprocess."
13368   (interactive)
13369   (gnus-set-global-variables)
13370   (setq command
13371         (cond ((eq command 'default)
13372                gnus-last-shell-command)
13373               (command command)
13374               (t (read-string "Shell command on article: "
13375                               gnus-last-shell-command))))
13376   (if (string-equal command "")
13377       (setq command gnus-last-shell-command))
13378   (gnus-eval-in-buffer-window gnus-article-buffer
13379     (save-restriction
13380       (widen)
13381       (shell-command-on-region (point-min) (point-max) command nil)))
13382   (setq gnus-last-shell-command command))
13383
13384 ;; Summary extract commands
13385
13386 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13387   (let ((buffer-read-only nil)
13388         (article (gnus-summary-article-number))
13389         after-article b e)
13390     (or (gnus-summary-goto-subject article)
13391         (error (format "No such article: %d" article)))
13392     (gnus-summary-position-point)
13393     ;; If all commands are to be bunched up on one line, we collect
13394     ;; them here.
13395     (if gnus-view-pseudos-separately
13396         ()
13397       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13398             files action)
13399         (while ps
13400           (setq action (cdr (assq 'action (car ps))))
13401           (setq files (list (cdr (assq 'name (car ps)))))
13402           (while (and ps (cdr ps)
13403                       (string= (or action "1")
13404                                (or (cdr (assq 'action (cadr ps))) "2")))
13405             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13406             (setcdr ps (cddr ps)))
13407           (if (not files)
13408               ()
13409             (if (not (string-match "%s" action))
13410                 (setq files (cons " " files)))
13411             (setq files (cons " " files))
13412             (and (assq 'execute (car ps))
13413                  (setcdr (assq 'execute (car ps))
13414                          (funcall (if (string-match "%s" action)
13415                                       'format 'concat)
13416                                   action
13417                                   (mapconcat (lambda (f) f) files " ")))))
13418           (setq ps (cdr ps)))))
13419     (if (and gnus-view-pseudos (not not-view))
13420         (while pslist
13421           (and (assq 'execute (car pslist))
13422                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13423                                      (eq gnus-view-pseudos 'not-confirm)))
13424           (setq pslist (cdr pslist)))
13425       (save-excursion
13426         (while pslist
13427           (setq after-article (or (cdr (assq 'article (car pslist)))
13428                                   (gnus-summary-article-number)))
13429           (gnus-summary-goto-subject after-article)
13430           (forward-line 1)
13431           (setq b (point))
13432           (insert "    " (file-name-nondirectory
13433                                 (cdr (assq 'name (car pslist))))
13434                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13435           (setq e (point))
13436           (forward-line -1)             ; back to `b'
13437           (gnus-add-text-properties
13438            b (1- e) (list 'gnus-number gnus-reffed-article-number
13439                           gnus-mouse-face-prop gnus-mouse-face))
13440           (gnus-data-enter
13441            after-article gnus-reffed-article-number
13442            gnus-unread-mark b (car pslist) 0 (- e b))
13443           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13444           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13445           (setq pslist (cdr pslist)))))))
13446
13447 (defun gnus-pseudos< (p1 p2)
13448   (let ((c1 (cdr (assq 'action p1)))
13449         (c2 (cdr (assq 'action p2))))
13450     (and c1 c2 (string< c1 c2))))
13451
13452 (defun gnus-request-pseudo-article (props)
13453   (cond ((assq 'execute props)
13454          (gnus-execute-command (cdr (assq 'execute props)))))
13455   (let ((gnus-current-article (gnus-summary-article-number)))
13456     (run-hooks 'gnus-mark-article-hook)))
13457
13458 (defun gnus-execute-command (command &optional automatic)
13459   (save-excursion
13460     (gnus-article-setup-buffer)
13461     (set-buffer gnus-article-buffer)
13462     (setq buffer-read-only nil)
13463     (let ((command (if automatic command (read-string "Command: " command)))
13464           ;; Just binding this here doesn't help, because there might
13465           ;; be output from the process after exiting the scope of 
13466           ;; this `let'.
13467           ;; (buffer-read-only nil)
13468           )
13469       (erase-buffer)
13470       (insert "$ " command "\n\n")
13471       (if gnus-view-pseudo-asynchronously
13472           (start-process "gnus-execute" nil shell-file-name
13473                          shell-command-switch command)
13474         (call-process shell-file-name nil t nil
13475                       shell-command-switch command)))))
13476
13477 (defun gnus-copy-file (file &optional to)
13478   "Copy FILE to TO."
13479   (interactive
13480    (list (read-file-name "Copy file: " default-directory)
13481          (read-file-name "Copy file to: " default-directory)))
13482   (gnus-set-global-variables)
13483   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13484   (and (file-directory-p to)
13485        (setq to (concat (file-name-as-directory to)
13486                         (file-name-nondirectory file))))
13487   (copy-file file to))
13488
13489 ;; Summary kill commands.
13490
13491 (defun gnus-summary-edit-global-kill (article)
13492   "Edit the \"global\" kill file."
13493   (interactive (list (gnus-summary-article-number)))
13494   (gnus-set-global-variables)
13495   (gnus-group-edit-global-kill article))
13496
13497 (defun gnus-summary-edit-local-kill ()
13498   "Edit a local kill file applied to the current newsgroup."
13499   (interactive)
13500   (gnus-set-global-variables)
13501   (setq gnus-current-headers (gnus-summary-article-header))
13502   (gnus-set-global-variables)
13503   (gnus-group-edit-local-kill
13504    (gnus-summary-article-number) gnus-newsgroup-name))
13505
13506 \f
13507 ;;;
13508 ;;; Gnus article mode
13509 ;;;
13510
13511 (put 'gnus-article-mode 'mode-class 'special)
13512
13513 (if gnus-article-mode-map
13514     nil
13515   (setq gnus-article-mode-map (make-keymap))
13516   (suppress-keymap gnus-article-mode-map)
13517
13518   (gnus-define-keys gnus-article-mode-map
13519     " " gnus-article-goto-next-page
13520     "\177" gnus-article-goto-prev-page
13521     [delete] gnus-article-goto-prev-page
13522     "\C-c^" gnus-article-refer-article
13523     "h" gnus-article-show-summary
13524     "s" gnus-article-show-summary
13525     "\C-c\C-m" gnus-article-mail
13526     "?" gnus-article-describe-briefly
13527     gnus-mouse-2 gnus-article-push-button
13528     "\r" gnus-article-press-button
13529     "\t" gnus-article-next-button
13530     "\M-\t" gnus-article-prev-button
13531     "<" beginning-of-buffer
13532     ">" end-of-buffer
13533     "\C-c\C-b" gnus-bug)
13534
13535   (substitute-key-definition
13536    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13537
13538 (defun gnus-article-mode ()
13539   "Major mode for displaying an article.
13540
13541 All normal editing commands are switched off.
13542
13543 The following commands are available:
13544
13545 \\<gnus-article-mode-map>
13546 \\[gnus-article-next-page]\t Scroll the article one page forwards
13547 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13548 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13549 \\[gnus-article-show-summary]\t Display the summary buffer
13550 \\[gnus-article-mail]\t Send a reply to the address near point
13551 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13552 \\[gnus-info-find-node]\t Go to the Gnus info node"
13553   (interactive)
13554   (when (and menu-bar-mode
13555              (gnus-visual-p 'article-menu 'menu))
13556     (gnus-article-make-menu-bar))
13557   (kill-all-local-variables)
13558   (gnus-simplify-mode-line)
13559   (setq mode-name "Article")
13560   (setq major-mode 'gnus-article-mode)
13561   (make-local-variable 'minor-mode-alist)
13562   (or (assq 'gnus-show-mime minor-mode-alist)
13563       (setq minor-mode-alist
13564             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13565   (use-local-map gnus-article-mode-map)
13566   (make-local-variable 'page-delimiter)
13567   (setq page-delimiter gnus-page-delimiter)
13568   (buffer-disable-undo (current-buffer))
13569   (setq buffer-read-only t)             ;Disable modification
13570   (run-hooks 'gnus-article-mode-hook))
13571
13572 (defun gnus-article-setup-buffer ()
13573   "Initialize the article buffer."
13574   (let* ((name (if gnus-single-article-buffer "*Article*"
13575                  (concat "*Article " gnus-newsgroup-name "*")))
13576          (original
13577           (progn (string-match "\\*Article" name)
13578                  (concat " *Original Article"
13579                          (substring name (match-end 0))))))
13580     (setq gnus-article-buffer name)
13581     (setq gnus-original-article-buffer original)
13582     ;; This might be a variable local to the summary buffer.
13583     (unless gnus-single-article-buffer
13584       (save-excursion
13585         (set-buffer gnus-summary-buffer)
13586         (setq gnus-article-buffer name)
13587         (setq gnus-original-article-buffer original)
13588         (gnus-set-global-variables))
13589       (make-local-variable 'gnus-summary-buffer))
13590     ;; Init original article buffer.
13591     (save-excursion
13592       (set-buffer (get-buffer-create gnus-original-article-buffer))
13593       (buffer-disable-undo (current-buffer))
13594       (setq major-mode 'gnus-original-article-mode)
13595       (make-local-variable 'gnus-original-article))
13596     (if (get-buffer name)
13597         (save-excursion
13598           (set-buffer name)
13599           (buffer-disable-undo (current-buffer))
13600           (setq buffer-read-only t)
13601           (gnus-add-current-to-buffer-list)
13602           (or (eq major-mode 'gnus-article-mode)
13603               (gnus-article-mode))
13604           (current-buffer))
13605       (save-excursion
13606         (set-buffer (get-buffer-create name))
13607         (gnus-add-current-to-buffer-list)
13608         (gnus-article-mode)
13609         (current-buffer)))))
13610
13611 ;; Set article window start at LINE, where LINE is the number of lines
13612 ;; from the head of the article.
13613 (defun gnus-article-set-window-start (&optional line)
13614   (set-window-start
13615    (get-buffer-window gnus-article-buffer t)
13616    (save-excursion
13617      (set-buffer gnus-article-buffer)
13618      (goto-char (point-min))
13619      (if (not line)
13620          (point-min)
13621        (gnus-message 6 "Moved to bookmark")
13622        (search-forward "\n\n" nil t)
13623        (forward-line line)
13624        (point)))))
13625
13626 (defun gnus-kill-all-overlays ()
13627   "Delete all overlays in the current buffer."
13628   (when (fboundp 'overlay-lists)
13629     (let* ((overlayss (overlay-lists))
13630            (buffer-read-only nil)
13631            (overlays (nconc (car overlayss) (cdr overlayss))))
13632       (while overlays
13633         (delete-overlay (pop overlays))))))
13634
13635 (defun gnus-request-article-this-buffer (article group)
13636   "Get an article and insert it into this buffer."
13637   (let (do-update-line)
13638     (prog1
13639         (save-excursion
13640           (erase-buffer)
13641           (gnus-kill-all-overlays)
13642           (setq group (or group gnus-newsgroup-name))
13643
13644           ;; Open server if it has closed.
13645           (gnus-check-server (gnus-find-method-for-group group))
13646
13647           ;; Using `gnus-request-article' directly will insert the article into
13648           ;; `nntp-server-buffer' - so we'll save some time by not having to
13649           ;; copy it from the server buffer into the article buffer.
13650
13651           ;; We only request an article by message-id when we do not have the
13652           ;; headers for it, so we'll have to get those.
13653           (when (stringp article)
13654             (let ((gnus-override-method gnus-refer-article-method))
13655               (gnus-read-header article)))
13656
13657           ;; If the article number is negative, that means that this article
13658           ;; doesn't belong in this newsgroup (possibly), so we find its
13659           ;; message-id and request it by id instead of number.
13660           (when (and (numberp article)
13661                      gnus-summary-buffer
13662                      (get-buffer gnus-summary-buffer)
13663                      (buffer-name (get-buffer gnus-summary-buffer)))
13664             (save-excursion
13665               (set-buffer gnus-summary-buffer)
13666               (let ((header (gnus-summary-article-header article)))
13667                 (if (< article 0)
13668                     (cond 
13669                      ((memq article gnus-newsgroup-sparse)
13670                       ;; This is a sparse gap article.
13671                       (setq do-update-line article)
13672                       (setq article (mail-header-id header))
13673                       (let ((gnus-override-method gnus-refer-article-method))
13674                         (gnus-read-header article))
13675                       (setq gnus-newsgroup-sparse
13676                             (delq article gnus-newsgroup-sparse)))
13677                      ((vectorp header)
13678                       ;; It's a real article.
13679                       (setq article (mail-header-id header)))
13680                      (t
13681                       ;; It is an extracted pseudo-article.
13682                       (setq article 'pseudo)
13683                       (gnus-request-pseudo-article header))))
13684                 
13685                 (let ((method (gnus-find-method-for-group 
13686                                gnus-newsgroup-name)))
13687                   (if (not (eq (car method) 'nneething))
13688                       ()
13689                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13690                                        (mail-header-subject header))))
13691                       (if (file-directory-p dir)
13692                           (progn
13693                             (setq article 'nneething)
13694                             (gnus-group-enter-directory dir)))))))))
13695
13696           (cond
13697            ;; Refuse to select canceled articles.
13698            ((and (numberp article)
13699                  gnus-summary-buffer
13700                  (get-buffer gnus-summary-buffer)
13701                  (buffer-name (get-buffer gnus-summary-buffer))
13702                  (eq (cdr (save-excursion
13703                             (set-buffer gnus-summary-buffer)
13704                             (assq article gnus-newsgroup-reads)))
13705                      gnus-canceled-mark))
13706             nil)
13707            ;; We first check `gnus-original-article-buffer'.
13708            ((and (get-buffer gnus-original-article-buffer)
13709                  (numberp article)
13710                  (save-excursion
13711                    (set-buffer gnus-original-article-buffer)
13712                    (and (equal (car gnus-original-article) group)
13713                         (eq (cdr gnus-original-article) article))))
13714             (insert-buffer-substring gnus-original-article-buffer)
13715             'article)
13716            ;; Check the backlog.
13717            ((and gnus-keep-backlog
13718                  (gnus-backlog-request-article group article (current-buffer)))
13719             'article)
13720            ;; Check the cache.
13721            ((and gnus-use-cache
13722                  (numberp article)
13723                  (gnus-cache-request-article article group))
13724             'article)
13725            ;; Get the article and put into the article buffer.
13726            ((or (stringp article) (numberp article))
13727             (let ((gnus-override-method
13728                    (and (stringp article) gnus-refer-article-method))
13729                   (buffer-read-only nil))
13730               (erase-buffer)
13731               (gnus-kill-all-overlays)
13732               (if (gnus-request-article article group (current-buffer))
13733                   (progn
13734                     (and gnus-keep-backlog
13735                          (numberp article)
13736                          (gnus-backlog-enter-article
13737                           group article (current-buffer)))
13738                     'article))))
13739            ;; It was a pseudo.
13740            (t article)))
13741
13742       ;; Take the article from the original article buffer
13743       ;; and place it in the buffer it's supposed to be in.
13744       (when (and (get-buffer gnus-article-buffer)
13745                  ;;(numberp article)
13746                  (equal (buffer-name (current-buffer))
13747                         (buffer-name (get-buffer gnus-article-buffer))))
13748         (save-excursion
13749           (if (get-buffer gnus-original-article-buffer)
13750               (set-buffer (get-buffer gnus-original-article-buffer))
13751             (set-buffer (get-buffer-create gnus-original-article-buffer))
13752             (buffer-disable-undo (current-buffer))
13753             (setq major-mode 'gnus-original-article-mode)
13754             (setq buffer-read-only t)
13755             (gnus-add-current-to-buffer-list))
13756           (let (buffer-read-only)
13757             (erase-buffer)
13758             (insert-buffer-substring gnus-article-buffer))
13759           (setq gnus-original-article (cons group article))))
13760     
13761       ;; Update sparse articles.
13762       (when (and do-update-line
13763                  (or (numberp article)
13764                      (stringp article)))
13765         (let ((buf (current-buffer)))
13766           (set-buffer gnus-summary-buffer)
13767           (gnus-summary-update-article do-update-line)
13768           (gnus-summary-goto-subject do-update-line nil t)
13769           (set-window-point (get-buffer-window (current-buffer) t)
13770                             (point))
13771           (set-buffer buf))))))
13772
13773 (defun gnus-read-header (id &optional header)
13774   "Read the headers of article ID and enter them into the Gnus system."
13775   (let ((group gnus-newsgroup-name)
13776         (gnus-override-method 
13777          (and (gnus-news-group-p gnus-newsgroup-name)
13778               gnus-refer-article-method))       
13779         where)
13780     ;; First we check to see whether the header in question is already
13781     ;; fetched.
13782     (if (stringp id)
13783         ;; This is a Message-ID.
13784         (setq header (or header (gnus-id-to-header id)))
13785       ;; This is an article number.
13786       (setq header (or header (gnus-summary-article-header id))))
13787     (if (and header
13788              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13789         ;; We have found the header.
13790         header
13791       ;; We have to really fetch the header to this article.
13792       (when (setq where (gnus-request-head id group))
13793         (save-excursion
13794           (set-buffer nntp-server-buffer)
13795           (goto-char (point-max))
13796           (insert ".\n")
13797           (goto-char (point-min))
13798           (insert "211 ")
13799           (princ (cond
13800                   ((numberp id) id)
13801                   ((cdr where) (cdr where))
13802                   (header (mail-header-number header))
13803                   (t gnus-reffed-article-number))
13804                  (current-buffer))
13805           (insert " Article retrieved.\n"))
13806         ;(when (and header
13807         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13808         ;  (setcar (gnus-id-to-thread id) nil))
13809         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13810             ()                          ; Malformed head.
13811           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13812             (if (and (stringp id)
13813                      (not (string= (gnus-group-real-name group)
13814                                    (car where))))
13815                 ;; If we fetched by Message-ID and the article came
13816                 ;; from a different group, we fudge some bogus article
13817                 ;; numbers for this article.
13818                 (mail-header-set-number header gnus-reffed-article-number))
13819             (decf gnus-reffed-article-number)
13820             (gnus-remove-header (mail-header-number header))
13821             (push header gnus-newsgroup-headers)
13822             (setq gnus-current-headers header)
13823             (push (mail-header-number header) gnus-newsgroup-limit))
13824           header)))))
13825
13826 (defun gnus-remove-header (number)
13827   "Remove header NUMBER from `gnus-newsgroup-headers'."
13828   (if (and gnus-newsgroup-headers
13829            (= number (mail-header-number (car gnus-newsgroup-headers))))
13830       (pop gnus-newsgroup-headers)
13831     (let ((headers gnus-newsgroup-headers))
13832       (while (and (cdr headers)
13833                   (not (= number (mail-header-number (cadr headers)))))
13834         (pop headers))
13835       (when (cdr headers)
13836         (setcdr headers (cddr headers))))))
13837
13838 (defun gnus-article-prepare (article &optional all-headers header)
13839   "Prepare ARTICLE in article mode buffer.
13840 ARTICLE should either be an article number or a Message-ID.
13841 If ARTICLE is an id, HEADER should be the article headers.
13842 If ALL-HEADERS is non-nil, no headers are hidden."
13843   (save-excursion
13844     ;; Make sure we start in a summary buffer.
13845     (unless (eq major-mode 'gnus-summary-mode)
13846       (set-buffer gnus-summary-buffer))
13847     (setq gnus-summary-buffer (current-buffer))
13848     ;; Make sure the connection to the server is alive.
13849     (unless (gnus-server-opened
13850              (gnus-find-method-for-group gnus-newsgroup-name))
13851       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13852       (gnus-request-group gnus-newsgroup-name t))
13853     (let* ((article (if header (mail-header-number header) article))
13854            (summary-buffer (current-buffer))
13855            (internal-hook gnus-article-internal-prepare-hook)
13856            (group gnus-newsgroup-name)
13857            result)
13858       (save-excursion
13859         (gnus-article-setup-buffer)
13860         (set-buffer gnus-article-buffer)
13861         ;; Deactivate active regions.
13862         (when (and (boundp 'transient-mark-mode)
13863                    transient-mark-mode)
13864           (setq mark-active nil))
13865         (if (not (setq result (let ((buffer-read-only nil))
13866                                 (gnus-request-article-this-buffer
13867                                  article group))))
13868             ;; There is no such article.
13869             (save-excursion
13870               (when (and (numberp article)
13871                          (not (memq article gnus-newsgroup-sparse)))
13872                 (setq gnus-article-current
13873                       (cons gnus-newsgroup-name article))
13874                 (set-buffer gnus-summary-buffer)
13875                 (setq gnus-current-article article)
13876                 (gnus-summary-mark-article article gnus-canceled-mark))
13877               (unless (memq article gnus-newsgroup-sparse)
13878                 (gnus-error
13879                  1 "No such article (may have expired or been canceled)")))
13880           (if (or (eq result 'pseudo) (eq result 'nneething))
13881               (progn
13882                 (save-excursion
13883                   (set-buffer summary-buffer)
13884                   (setq gnus-last-article gnus-current-article
13885                         gnus-newsgroup-history (cons gnus-current-article
13886                                                      gnus-newsgroup-history)
13887                         gnus-current-article 0
13888                         gnus-current-headers nil
13889                         gnus-article-current nil)
13890                   (if (eq result 'nneething)
13891                       (gnus-configure-windows 'summary)
13892                     (gnus-configure-windows 'article))
13893                   (gnus-set-global-variables))
13894                 (gnus-set-mode-line 'article))
13895             ;; The result from the `request' was an actual article -
13896             ;; or at least some text that is now displayed in the
13897             ;; article buffer.
13898             (if (and (numberp article)
13899                      (not (eq article gnus-current-article)))
13900                 ;; Seems like a new article has been selected.
13901                 ;; `gnus-current-article' must be an article number.
13902                 (save-excursion
13903                   (set-buffer summary-buffer)
13904                   (setq gnus-last-article gnus-current-article
13905                         gnus-newsgroup-history (cons gnus-current-article
13906                                                      gnus-newsgroup-history)
13907                         gnus-current-article article
13908                         gnus-current-headers
13909                         (gnus-summary-article-header gnus-current-article)
13910                         gnus-article-current
13911                         (cons gnus-newsgroup-name gnus-current-article))
13912                   (unless (vectorp gnus-current-headers)
13913                     (setq gnus-current-headers nil))
13914                   (gnus-summary-show-thread)
13915                   (run-hooks 'gnus-mark-article-hook)
13916                   (gnus-set-mode-line 'summary)
13917                   (and (gnus-visual-p 'article-highlight 'highlight)
13918                        (run-hooks 'gnus-visual-mark-article-hook))
13919                   ;; Set the global newsgroup variables here.
13920                   ;; Suggested by Jim Sisolak
13921                   ;; <sisolak@trans4.neep.wisc.edu>.
13922                   (gnus-set-global-variables)
13923                   (setq gnus-have-all-headers
13924                         (or all-headers gnus-show-all-headers))
13925                   (and gnus-use-cache
13926                        (vectorp (gnus-summary-article-header article))
13927                        (gnus-cache-possibly-enter-article
13928                         group article
13929                         (gnus-summary-article-header article)
13930                         (memq article gnus-newsgroup-marked)
13931                         (memq article gnus-newsgroup-dormant)
13932                         (memq article gnus-newsgroup-unreads)))))
13933             (when (or (numberp article)
13934                       (stringp article))
13935               ;; Hooks for getting information from the article.
13936               ;; This hook must be called before being narrowed.
13937               (let (buffer-read-only)
13938                 (run-hooks 'internal-hook)
13939                 (run-hooks 'gnus-article-prepare-hook)
13940                 ;; Decode MIME message.
13941                 (if gnus-show-mime
13942                     (if (or (not gnus-strict-mime)
13943                             (gnus-fetch-field "Mime-Version"))
13944                         (funcall gnus-show-mime-method)
13945                       (funcall gnus-decode-encoded-word-method)))
13946                 ;; Perform the article display hooks.
13947                 (run-hooks 'gnus-article-display-hook))
13948               ;; Do page break.
13949               (goto-char (point-min))
13950               (and gnus-break-pages (gnus-narrow-to-page)))
13951             (gnus-set-mode-line 'article)
13952             (gnus-configure-windows 'article)
13953             (goto-char (point-min))
13954             t))))))
13955
13956 (defun gnus-article-show-all-headers ()
13957   "Show all article headers in article mode buffer."
13958   (save-excursion
13959     (gnus-article-setup-buffer)
13960     (set-buffer gnus-article-buffer)
13961     (let ((buffer-read-only nil))
13962       (gnus-unhide-text (point-min) (point-max)))))
13963
13964 (defun gnus-article-hide-headers-if-wanted ()
13965   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13966 Provided for backwards compatibility."
13967   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13968       gnus-inhibit-hiding
13969       (gnus-article-hide-headers)))
13970
13971 (defsubst gnus-article-header-rank ()
13972   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13973   (let ((list gnus-sorted-header-list)
13974         (i 0))
13975     (while list
13976       (when (looking-at (car list))
13977         (setq list nil))
13978       (setq list (cdr list))
13979       (incf i))
13980     i))
13981
13982 (defun gnus-article-hide-headers (&optional arg delete)
13983   "Toggle whether to hide unwanted headers and possibly sort them as well.
13984 If given a negative prefix, always show; if given a positive prefix,
13985 always hide."
13986   (interactive (gnus-hidden-arg))
13987   (if (gnus-article-check-hidden-text 'headers arg)
13988       ;; Show boring headers as well.
13989       (gnus-article-show-hidden-text 'boring-headers)
13990     ;; This function might be inhibited.
13991     (unless gnus-inhibit-hiding
13992       (save-excursion
13993         (set-buffer gnus-article-buffer)
13994         (save-restriction
13995           (let ((buffer-read-only nil)
13996                 (props (nconc (list 'gnus-type 'headers)
13997                               gnus-hidden-properties))
13998                 (max (1+ (length gnus-sorted-header-list)))
13999                 (ignored (when (not (stringp gnus-visible-headers))
14000                            (cond ((stringp gnus-ignored-headers)
14001                                   gnus-ignored-headers)
14002                                  ((listp gnus-ignored-headers)
14003                                   (mapconcat 'identity gnus-ignored-headers
14004                                              "\\|")))))
14005                 (visible
14006                  (cond ((stringp gnus-visible-headers)
14007                         gnus-visible-headers)
14008                        ((and gnus-visible-headers
14009                              (listp gnus-visible-headers))
14010                         (mapconcat 'identity gnus-visible-headers "\\|"))))
14011                 (inhibit-point-motion-hooks t)
14012                 want-list beg)
14013             ;; First we narrow to just the headers.
14014             (widen)
14015             (goto-char (point-min))
14016             ;; Hide any "From " lines at the beginning of (mail) articles.
14017             (while (looking-at "From ")
14018               (forward-line 1))
14019             (unless (bobp)
14020               (if delete
14021                   (delete-region (point-min) (point))
14022                 (gnus-hide-text (point-min) (point) props)))
14023             ;; Then treat the rest of the header lines.
14024             (narrow-to-region
14025              (point)
14026              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
14027             ;; Then we use the two regular expressions
14028             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
14029             ;; select which header lines is to remain visible in the
14030             ;; article buffer.
14031             (goto-char (point-min))
14032             (while (re-search-forward "^[^ \t]*:" nil t)
14033               (beginning-of-line)
14034               ;; We add the headers we want to keep to a list and delete
14035               ;; them from the buffer.
14036               (gnus-put-text-property 
14037                (point) (1+ (point)) 'message-rank
14038                (if (or (and visible (looking-at visible))
14039                        (and ignored
14040                             (not (looking-at ignored))))
14041                    (gnus-article-header-rank) 
14042                  (+ 2 max)))
14043               (forward-line 1))
14044             (message-sort-headers-1)
14045             (when (setq beg (text-property-any 
14046                              (point-min) (point-max) 'message-rank (+ 2 max)))
14047               ;; We make the unwanted headers invisible.
14048               (if delete
14049                   (delete-region beg (point-max))
14050                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
14051                 (gnus-hide-text-type beg (point-max) 'headers))
14052               ;; Work around XEmacs lossage.
14053               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
14054
14055 (defun gnus-article-hide-boring-headers (&optional arg)
14056   "Toggle hiding of headers that aren't very interesting.
14057 If given a negative prefix, always show; if given a positive prefix,
14058 always hide."
14059   (interactive (gnus-hidden-arg))
14060   (unless (gnus-article-check-hidden-text 'boring-headers arg)
14061     (save-excursion
14062       (set-buffer gnus-article-buffer)
14063       (save-restriction
14064         (let ((buffer-read-only nil)
14065               (list gnus-boring-article-headers)
14066               (inhibit-point-motion-hooks t)
14067               elem)
14068           (nnheader-narrow-to-headers)
14069           (while list
14070             (setq elem (pop list))
14071             (goto-char (point-min))
14072             (cond
14073              ;; Hide empty headers.
14074              ((eq elem 'empty)
14075               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
14076                 (forward-line -1)
14077                 (gnus-hide-text-type
14078                  (progn (beginning-of-line) (point))
14079                  (progn 
14080                    (end-of-line)
14081                    (if (re-search-forward "^[^ \t]" nil t)
14082                        (match-beginning 0)
14083                      (point-max)))
14084                  'boring-headers)))
14085              ;; Hide boring Newsgroups header.
14086              ((eq elem 'newsgroups)
14087               (when (equal (message-fetch-field "newsgroups")
14088                            (gnus-group-real-name gnus-newsgroup-name))
14089                 (gnus-article-hide-header "newsgroups")))
14090              ((eq elem 'followup-to)
14091               (when (equal (message-fetch-field "followup-to")
14092                            (message-fetch-field "newsgroups"))
14093                 (gnus-article-hide-header "followup-to")))
14094              ((eq elem 'reply-to)
14095               (let ((from (message-fetch-field "from"))
14096                     (reply-to (message-fetch-field "reply-to")))
14097                 (when (and
14098                        from reply-to
14099                        (equal 
14100                         (nth 1 (funcall gnus-extract-address-components from))
14101                         (nth 1 (funcall gnus-extract-address-components
14102                                         reply-to))))
14103                   (gnus-article-hide-header "reply-to"))))
14104              ((eq elem 'date)
14105               (let ((date (message-fetch-field "date")))
14106                 (when (and date
14107                            (< (gnus-days-between date (current-time-string))
14108                               4))
14109                   (gnus-article-hide-header "date")))))))))))
14110
14111 (defun gnus-article-hide-header (header)
14112   (save-excursion
14113     (goto-char (point-min))
14114     (when (re-search-forward (concat "^" header ":") nil t)
14115       (gnus-hide-text-type
14116        (progn (beginning-of-line) (point))
14117        (progn 
14118          (end-of-line)
14119          (if (re-search-forward "^[^ \t]" nil t)
14120              (match-beginning 0)
14121            (point-max)))
14122        'boring-headers))))
14123
14124 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
14125 (defun gnus-article-treat-overstrike ()
14126   "Translate overstrikes into bold text."
14127   (interactive)
14128   (save-excursion
14129     (set-buffer gnus-article-buffer)
14130     (let ((buffer-read-only nil))
14131       (while (search-forward "\b" nil t)
14132         (let ((next (following-char))
14133               (previous (char-after (- (point) 2))))
14134           (cond 
14135            ((eq next previous)
14136             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14137             (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
14138            ((eq next ?_)
14139             (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
14140             (gnus-put-text-property
14141              (- (point) 2) (1- (point)) 'face 'underline))
14142            ((eq previous ?_)
14143             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14144             (gnus-put-text-property
14145              (point) (1+ (point))       'face 'underline))))))))
14146
14147 (defun gnus-article-word-wrap ()
14148   "Format too long lines."
14149   (interactive)
14150   (save-excursion
14151     (set-buffer gnus-article-buffer)
14152     (let ((buffer-read-only nil))
14153       (widen)
14154       (goto-char (point-min))
14155       (search-forward "\n\n" nil t)
14156       (end-of-line 1)
14157       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
14158             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
14159             (adaptive-fill-mode t))
14160         (while (not (eobp))
14161           (and (>= (current-column) (min fill-column (window-width)))
14162                (/= (preceding-char) ?:)
14163                (fill-paragraph nil))
14164           (end-of-line 2))))))
14165
14166 (defun gnus-article-remove-cr ()
14167   "Remove carriage returns from an article."
14168   (interactive)
14169   (save-excursion
14170     (set-buffer gnus-article-buffer)
14171     (let ((buffer-read-only nil))
14172       (goto-char (point-min))
14173       (while (search-forward "\r" nil t)
14174         (replace-match "" t t)))))
14175
14176 (defun gnus-article-remove-trailing-blank-lines ()
14177   "Remove all trailing blank lines from the article."
14178   (interactive)
14179   (save-excursion
14180     (set-buffer gnus-article-buffer)
14181     (let ((buffer-read-only nil))
14182       (goto-char (point-max))
14183       (delete-region
14184        (point)
14185        (progn
14186          (while (looking-at "^[ \t]*$")
14187            (forward-line -1))
14188          (forward-line 1)
14189          (point))))))
14190
14191 (defun gnus-article-display-x-face (&optional force)
14192   "Look for an X-Face header and display it if present."
14193   (interactive (list 'force))
14194   (save-excursion
14195     (set-buffer gnus-article-buffer)
14196     ;; Delete the old process, if any.
14197     (when (process-status "gnus-x-face")
14198       (delete-process "gnus-x-face"))
14199     (let ((inhibit-point-motion-hooks t)
14200           (case-fold-search nil)
14201           from)
14202       (save-restriction
14203         (nnheader-narrow-to-headers)
14204         (setq from (message-fetch-field "from"))
14205         (goto-char (point-min))
14206         (when (and gnus-article-x-face-command
14207                    (or force
14208                        ;; Check whether this face is censored.
14209                        (not gnus-article-x-face-too-ugly)
14210                        (and gnus-article-x-face-too-ugly from
14211                             (not (string-match gnus-article-x-face-too-ugly
14212                                                from))))
14213                    ;; Has to be present.
14214                    (re-search-forward "^X-Face: " nil t))
14215           ;; We now have the area of the buffer where the X-Face is stored.
14216           (let ((beg (point))
14217                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14218             ;; We display the face.
14219             (if (symbolp gnus-article-x-face-command)
14220                 ;; The command is a lisp function, so we call it.
14221                 (if (gnus-functionp gnus-article-x-face-command)
14222                     (funcall gnus-article-x-face-command beg end)
14223                   (error "%s is not a function" gnus-article-x-face-command))
14224               ;; The command is a string, so we interpret the command
14225               ;; as a, well, command, and fork it off.
14226               (let ((process-connection-type nil))
14227                 (process-kill-without-query
14228                  (start-process
14229                   "gnus-x-face" nil shell-file-name shell-command-switch
14230                   gnus-article-x-face-command))
14231                 (process-send-region "gnus-x-face" beg end)
14232                 (process-send-eof "gnus-x-face")))))))))
14233
14234 (defalias 'gnus-headers-decode-quoted-printable 'gnus-decode-rfc1522)
14235 (defun gnus-decode-rfc1522 ()
14236   "Hack to remove QP encoding from headers."
14237   (let ((case-fold-search t)
14238         (inhibit-point-motion-hooks t)
14239         (buffer-read-only nil)
14240         string)
14241     (save-restriction
14242       (narrow-to-region
14243        (goto-char (point-min))
14244        (or (search-forward "\n\n" nil t) (point-max)))
14245
14246       (while (re-search-forward 
14247               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14248         (setq string (match-string 1))
14249         (narrow-to-region (match-beginning 0) (match-end 0))
14250         (delete-region (point-min) (point-max))
14251         (insert string)
14252         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14253         (subst-char-in-region (point-min) (point-max) ?_ ? )
14254         (widen)
14255         (goto-char (point-min))))))
14256
14257 (defun gnus-article-de-quoted-unreadable (&optional force)
14258   "Do a naive translation of a quoted-printable-encoded article.
14259 This is in no way, shape or form meant as a replacement for real MIME
14260 processing, but is simply a stop-gap measure until MIME support is
14261 written.
14262 If FORCE, decode the article whether it is marked as quoted-printable
14263 or not."
14264   (interactive (list 'force))
14265   (save-excursion
14266     (set-buffer gnus-article-buffer)
14267     (let ((case-fold-search t)
14268           (buffer-read-only nil)
14269           (type (gnus-fetch-field "content-transfer-encoding")))
14270       (gnus-decode-rfc1522)
14271       (when (or force
14272                 (and type (string-match "quoted-printable" (downcase type))))
14273         (goto-char (point-min))
14274         (search-forward "\n\n" nil 'move)
14275         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14276
14277 (defun gnus-mime-decode-quoted-printable (from to)
14278   "Decode Quoted-Printable in the region between FROM and TO."
14279   (interactive "r")
14280   (goto-char from)
14281   (while (search-forward "=" to t)
14282     (cond ((eq (following-char) ?\n)
14283            (delete-char -1)
14284            (delete-char 1))
14285           ((looking-at "[0-9A-F][0-9A-F]")
14286            (subst-char-in-region
14287             (1- (point)) (point) ?=
14288             (hexl-hex-string-to-integer
14289              (buffer-substring (point) (+ 2 (point)))))
14290            (delete-char 2))
14291           ((looking-at "=")
14292            (delete-char 1))
14293           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14294
14295 (defun gnus-article-hide-pgp (&optional arg)
14296   "Toggle hiding of any PGP headers and signatures in the current article.
14297 If given a negative prefix, always show; if given a positive prefix,
14298 always hide."
14299   (interactive (gnus-hidden-arg))
14300   (unless (gnus-article-check-hidden-text 'pgp arg)
14301     (save-excursion
14302       (set-buffer gnus-article-buffer)
14303       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14304             buffer-read-only beg end)
14305         (widen)
14306         (goto-char (point-min))
14307         ;; Hide the "header".
14308         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14309              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14310         (setq beg (point))
14311         ;; Hide the actual signature.
14312         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14313              (setq end (1+ (match-beginning 0)))
14314              (gnus-hide-text
14315               end
14316               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14317                   (match-end 0)
14318                 ;; Perhaps we shouldn't hide to the end of the buffer
14319                 ;; if there is no end to the signature?
14320                 (point-max))
14321               props))
14322         ;; Hide "- " PGP quotation markers.
14323         (when (and beg end)
14324           (narrow-to-region beg end)
14325           (goto-char (point-min))
14326           (while (re-search-forward "^- " nil t)
14327             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14328           (widen))))))
14329
14330 (defun gnus-article-hide-signature (&optional arg)
14331   "Hide the signature in the current article.
14332 If given a negative prefix, always show; if given a positive prefix,
14333 always hide."
14334   (interactive (gnus-hidden-arg))
14335   (unless (gnus-article-check-hidden-text 'signature arg)
14336     (save-excursion
14337       (set-buffer gnus-article-buffer)
14338       (save-restriction
14339         (let ((buffer-read-only nil))
14340           (when (gnus-narrow-to-signature)
14341             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14342
14343 (defun gnus-article-strip-leading-blank-lines ()
14344   "Remove all blank lines from the beginning of the article."
14345   (interactive)
14346   (save-excursion
14347     (set-buffer gnus-article-buffer)
14348     (let (buffer-read-only)
14349       (goto-char (point-min))
14350       (when (search-forward "\n\n" nil t)
14351         (while (looking-at "[ \t]$")
14352           (gnus-delete-line))))))
14353
14354 (defvar mime::preview/content-list)
14355 (defvar mime::preview-content-info/point-min)
14356 (defun gnus-narrow-to-signature ()
14357   "Narrow to the signature."
14358   (widen)
14359   (if (and (boundp 'mime::preview/content-list)
14360            mime::preview/content-list)
14361       (let ((pcinfo (car (last mime::preview/content-list))))
14362         (condition-case ()
14363             (narrow-to-region
14364              (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14365              (point-max))
14366           (error nil))))
14367   (goto-char (point-max))
14368   (when (re-search-backward gnus-signature-separator nil t)
14369     (forward-line 1)
14370     (when (or (null gnus-signature-limit)
14371               (and (numberp gnus-signature-limit)
14372                    (< (- (point-max) (point)) gnus-signature-limit))
14373               (and (gnus-functionp gnus-signature-limit)
14374                    (funcall gnus-signature-limit))
14375               (and (stringp gnus-signature-limit)
14376                    (not (re-search-forward gnus-signature-limit nil t))))
14377       (narrow-to-region (point) (point-max))
14378       t)))
14379
14380 (defun gnus-hidden-arg ()
14381   "Return the current prefix arg as a number, or 0 if no prefix."
14382   (list (if current-prefix-arg
14383             (prefix-numeric-value current-prefix-arg)
14384           0)))
14385
14386 (defun gnus-article-check-hidden-text (type arg)
14387   "Return nil if hiding is necessary.
14388 Arg can be nil or a number.  Nil and positive means hide, negative
14389 means show, 0 means toggle."
14390   (save-excursion
14391     (set-buffer gnus-article-buffer)
14392     (let ((hide (gnus-article-hidden-text-p type)))
14393       (cond
14394        ((or (null arg)
14395             (> arg 0))
14396         nil)
14397        ((< arg 0)
14398         (gnus-article-show-hidden-text type))
14399        (t
14400         (if (eq hide 'hidden)
14401             (gnus-article-show-hidden-text type)
14402           nil))))))
14403
14404 (defun gnus-article-hidden-text-p (type)
14405   "Say whether the current buffer contains hidden text of type TYPE."
14406   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14407     (when pos
14408       (if (get-text-property pos 'invisible)
14409           'hidden
14410         'shown))))
14411
14412 (defun gnus-article-hide (&optional arg force)
14413   "Hide all the gruft in the current article.
14414 This means that PGP stuff, signatures, cited text and (some)
14415 headers will be hidden.
14416 If given a prefix, show the hidden text instead."
14417   (interactive (list current-prefix-arg 'force))
14418   (gnus-article-hide-headers arg)
14419   (gnus-article-hide-pgp arg)
14420   (gnus-article-hide-citation-maybe arg force)
14421   (gnus-article-hide-signature arg))
14422
14423 (defun gnus-article-show-hidden-text (type &optional hide)
14424   "Show all hidden text of type TYPE.
14425 If HIDE, hide the text instead."
14426   (save-excursion
14427     (set-buffer gnus-article-buffer)
14428     (let ((buffer-read-only nil)
14429           (inhibit-point-motion-hooks t)
14430           (beg (point-min)))
14431       (while (gnus-goto-char (text-property-any
14432                               beg (point-max) 'gnus-type type))
14433         (setq beg (point))
14434         (forward-char)
14435         (if hide
14436             (gnus-hide-text beg (point) gnus-hidden-properties)
14437           (gnus-unhide-text beg (point)))
14438         (setq beg (point)))
14439       t)))
14440
14441 (defvar gnus-article-time-units
14442   `((year . ,(* 365.25 24 60 60))
14443     (week . ,(* 7 24 60 60))
14444     (day . ,(* 24 60 60))
14445     (hour . ,(* 60 60))
14446     (minute . 60)
14447     (second . 1))
14448   "Mapping from time units to seconds.")
14449
14450 (defun gnus-article-date-ut (&optional type highlight)
14451   "Convert DATE date to universal time in the current article.
14452 If TYPE is `local', convert to local time; if it is `lapsed', output
14453 how much time has lapsed since DATE."
14454   (interactive (list 'ut t))
14455   (let* ((header (or gnus-current-headers
14456                      (gnus-summary-article-header) ""))
14457          (date (and (vectorp header) (mail-header-date header)))
14458          (date-regexp "^Date: \\|^X-Sent: ")
14459          (now (current-time))
14460          (inhibit-point-motion-hooks t)
14461          bface eface)
14462     (when (and date (not (string= date "")))
14463       (save-excursion
14464         (set-buffer gnus-article-buffer)
14465         (save-restriction
14466           (nnheader-narrow-to-headers)
14467           (let ((buffer-read-only nil))
14468             ;; Delete any old Date headers.
14469             (if (re-search-forward date-regexp nil t)
14470                 (progn
14471                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14472                         eface (get-text-property (1- (gnus-point-at-eol))
14473                                                  'face))
14474                   (message-remove-header date-regexp t)
14475                   (beginning-of-line))
14476               (goto-char (point-max)))
14477             (insert (gnus-make-date-line date type))
14478             ;; Do highlighting.
14479             (forward-line -1)
14480             (when (and (gnus-visual-p 'article-highlight 'highlight)
14481                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14482               (gnus-put-text-property (match-beginning 1) (match-end 1)
14483                                  'face bface)
14484               (gnus-put-text-property (match-beginning 2) (match-end 2)
14485                                  'face eface))))))))
14486
14487 (defun gnus-make-date-line (date type)
14488   "Return a DATE line of TYPE."
14489   (cond
14490    ;; Convert to the local timezone.  We have to slap a
14491    ;; `condition-case' round the calls to the timezone
14492    ;; functions since they aren't particularly resistant to
14493    ;; buggy dates.
14494    ((eq type 'local)
14495     (concat "Date: " (condition-case ()
14496                          (timezone-make-date-arpa-standard date)
14497                        (error date))
14498             "\n"))
14499    ;; Convert to Universal Time.
14500    ((eq type 'ut)
14501     (concat "Date: "
14502             (condition-case ()
14503                 (timezone-make-date-arpa-standard date nil "UT")
14504               (error date))
14505             "\n"))
14506    ;; Get the original date from the article.
14507    ((eq type 'original)
14508     (concat "Date: " date "\n"))
14509    ;; Do an X-Sent lapsed format.
14510    ((eq type 'lapsed)
14511     ;; If the date is seriously mangled, the timezone
14512     ;; functions are liable to bug out, so we condition-case
14513     ;; the entire thing.
14514     (let* ((now (current-time))
14515            (real-time
14516             (condition-case ()
14517                 (gnus-time-minus
14518                  (gnus-encode-date
14519                   (timezone-make-date-arpa-standard
14520                    (current-time-string now)
14521                    (current-time-zone now) "UT"))
14522                  (gnus-encode-date
14523                   (timezone-make-date-arpa-standard
14524                    date nil "UT")))
14525               (error '(0 0))))
14526            (real-sec (+ (* (float (car real-time)) 65536)
14527                         (cadr real-time)))
14528            (sec (abs real-sec))
14529            num prev)
14530       (cond
14531        ((equal real-time '(0 0))
14532         "X-Sent: Unknown\n")
14533        ((zerop sec)
14534         "X-Sent: Now\n")
14535        (t
14536         (concat
14537          "X-Sent: "
14538          ;; This is a bit convoluted, but basically we go
14539          ;; through the time units for years, weeks, etc,
14540          ;; and divide things to see whether that results
14541          ;; in positive answers.
14542          (mapconcat
14543           (lambda (unit)
14544             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14545                 ;; The (remaining) seconds are too few to
14546                 ;; be divided into this time unit.
14547                 ""
14548               ;; It's big enough, so we output it.
14549               (setq sec (- sec (* num (cdr unit))))
14550               (prog1
14551                   (concat (if prev ", " "") (int-to-string
14552                                              (floor num))
14553                           " " (symbol-name (car unit)) 
14554                           (if (> num 1) "s" ""))
14555                 (setq prev t))))
14556           gnus-article-time-units "")
14557          ;; If dates are odd, then it might appear like the
14558          ;; article was sent in the future.
14559          (if (> real-sec 0)
14560              " ago\n"
14561            " in the future\n"))))))
14562    (t
14563     (error "Unknown conversion type: %s" type))))
14564
14565 (defun gnus-article-date-local (&optional highlight)
14566   "Convert the current article date to the local timezone."
14567   (interactive (list t))
14568   (gnus-article-date-ut 'local highlight))
14569
14570 (defun gnus-article-date-original (&optional highlight)
14571   "Convert the current article date to what it was originally.
14572 This is only useful if you have used some other date conversion
14573 function and want to see what the date was before converting."
14574   (interactive (list t))
14575   (gnus-article-date-ut 'original highlight))
14576
14577 (defun gnus-article-date-lapsed (&optional highlight)
14578   "Convert the current article date to time lapsed since it was sent."
14579   (interactive (list t))
14580   (gnus-article-date-ut 'lapsed highlight))
14581
14582 (defun gnus-article-maybe-highlight ()
14583   "Do some article highlighting if `gnus-visual' is non-nil."
14584   (if (gnus-visual-p 'article-highlight 'highlight)
14585       (gnus-article-highlight-some)))
14586
14587 ;;; Article savers.
14588
14589 (defun gnus-output-to-rmail (file-name)
14590   "Append the current article to an Rmail file named FILE-NAME."
14591   (require 'rmail)
14592   ;; Most of these codes are borrowed from rmailout.el.
14593   (setq file-name (expand-file-name file-name))
14594   (setq rmail-default-rmail-file file-name)
14595   (let ((artbuf (current-buffer))
14596         (tmpbuf (get-buffer-create " *Gnus-output*")))
14597     (save-excursion
14598       (or (get-file-buffer file-name)
14599           (file-exists-p file-name)
14600           (if (gnus-yes-or-no-p
14601                (concat "\"" file-name "\" does not exist, create it? "))
14602               (let ((file-buffer (create-file-buffer file-name)))
14603                 (save-excursion
14604                   (set-buffer file-buffer)
14605                   (rmail-insert-rmail-file-header)
14606                   (let ((require-final-newline nil))
14607                     (write-region (point-min) (point-max) file-name t 1)))
14608                 (kill-buffer file-buffer))
14609             (error "Output file does not exist")))
14610       (set-buffer tmpbuf)
14611       (buffer-disable-undo (current-buffer))
14612       (erase-buffer)
14613       (insert-buffer-substring artbuf)
14614       (gnus-convert-article-to-rmail)
14615       ;; Decide whether to append to a file or to an Emacs buffer.
14616       (let ((outbuf (get-file-buffer file-name)))
14617         (if (not outbuf)
14618             (append-to-file (point-min) (point-max) file-name)
14619           ;; File has been visited, in buffer OUTBUF.
14620           (set-buffer outbuf)
14621           (let ((buffer-read-only nil)
14622                 (msg (and (boundp 'rmail-current-message)
14623                           (symbol-value 'rmail-current-message))))
14624             ;; If MSG is non-nil, buffer is in RMAIL mode.
14625             (if msg
14626                 (progn (widen)
14627                        (narrow-to-region (point-max) (point-max))))
14628             (insert-buffer-substring tmpbuf)
14629             (if msg
14630                 (progn
14631                   (goto-char (point-min))
14632                   (widen)
14633                   (search-backward "\^_")
14634                   (narrow-to-region (point) (point-max))
14635                   (goto-char (1+ (point-min)))
14636                   (rmail-count-new-messages t)
14637                   (rmail-show-message msg)))))))
14638     (kill-buffer tmpbuf)))
14639
14640 (defun gnus-output-to-file (file-name)
14641   "Append the current article to a file named FILE-NAME."
14642   (let ((artbuf (current-buffer)))
14643     (nnheader-temp-write nil
14644       (insert-buffer-substring artbuf)
14645       ;; Append newline at end of the buffer as separator, and then
14646       ;; save it to file.
14647       (goto-char (point-max))
14648       (insert "\n")
14649       (append-to-file (point-min) (point-max) file-name))))
14650
14651 (defun gnus-convert-article-to-rmail ()
14652   "Convert article in current buffer to Rmail message format."
14653   (let ((buffer-read-only nil))
14654     ;; Convert article directly into Babyl format.
14655     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14656     (goto-char (point-min))
14657     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14658     (while (search-forward "\n\^_" nil t) ;single char
14659       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14660     (goto-char (point-max))
14661     (insert "\^_")))
14662
14663 (defun gnus-narrow-to-page (&optional arg)
14664   "Narrow the article buffer to a page.
14665 If given a numerical ARG, move forward ARG pages."
14666   (interactive "P")
14667   (setq arg (if arg (prefix-numeric-value arg) 0))
14668   (save-excursion
14669     (set-buffer gnus-article-buffer)
14670     (goto-char (point-min))
14671     (widen)
14672     (when (gnus-visual-p 'page-marker)
14673       (let ((buffer-read-only nil))
14674         (gnus-remove-text-with-property 'gnus-prev)
14675         (gnus-remove-text-with-property 'gnus-next)))
14676     (when
14677         (cond ((< arg 0)
14678                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14679               ((> arg 0)
14680                (re-search-forward page-delimiter nil 'move arg)))
14681       (goto-char (match-end 0)))
14682     (narrow-to-region
14683      (point)
14684      (if (re-search-forward page-delimiter nil 'move)
14685          (match-beginning 0)
14686        (point)))
14687     (when (and (gnus-visual-p 'page-marker)
14688                (not (= (point-min) 1)))
14689       (save-excursion
14690         (goto-char (point-min))
14691         (gnus-insert-prev-page-button)))
14692     (when (and (gnus-visual-p 'page-marker)
14693                (not (= (1- (point-max)) (buffer-size))))
14694       (save-excursion
14695         (goto-char (point-max))
14696         (gnus-insert-next-page-button)))))
14697
14698 ;; Article mode commands
14699
14700 (defun gnus-article-goto-next-page ()
14701   "Show the next page of the article."
14702   (interactive)
14703   (when (gnus-article-next-page)
14704     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14705
14706 (defun gnus-article-goto-prev-page ()
14707   "Show the next page of the article."
14708   (interactive)
14709   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14710     (gnus-article-prev-page nil)))
14711
14712 (defun gnus-article-next-page (&optional lines)
14713   "Show the next page of the current article.
14714 If end of article, return non-nil.  Otherwise return nil.
14715 Argument LINES specifies lines to be scrolled up."
14716   (interactive "p")
14717   (move-to-window-line -1)
14718   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14719   (if (save-excursion
14720         (end-of-line)
14721         (and (pos-visible-in-window-p)  ;Not continuation line.
14722              (eobp)))
14723       ;; Nothing in this page.
14724       (if (or (not gnus-break-pages)
14725               (save-excursion
14726                 (save-restriction
14727                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14728           t                             ;Nothing more.
14729         (gnus-narrow-to-page 1)         ;Go to next page.
14730         nil)
14731     ;; More in this page.
14732     (condition-case ()
14733         (scroll-up lines)
14734       (end-of-buffer
14735        ;; Long lines may cause an end-of-buffer error.
14736        (goto-char (point-max))))
14737     (move-to-window-line 0)
14738     nil))
14739
14740 (defun gnus-article-prev-page (&optional lines)
14741   "Show previous page of current article.
14742 Argument LINES specifies lines to be scrolled down."
14743   (interactive "p")
14744   (move-to-window-line 0)
14745   (if (and gnus-break-pages
14746            (bobp)
14747            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14748       (progn
14749         (gnus-narrow-to-page -1)        ;Go to previous page.
14750         (goto-char (point-max))
14751         (recenter -1))
14752     (prog1
14753         (condition-case ()
14754             (scroll-down lines)
14755           (error nil))
14756       (move-to-window-line 0))))
14757
14758 (defun gnus-article-refer-article ()
14759   "Read article specified by message-id around point."
14760   (interactive)
14761   (let ((point (point)))
14762     (search-forward ">" nil t)          ;Move point to end of "<....>".
14763     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14764         (let ((message-id (match-string 1)))
14765           (goto-char point)
14766           (set-buffer gnus-summary-buffer)
14767           (gnus-summary-refer-article message-id))
14768       (goto-char (point))
14769       (error "No references around point"))))
14770
14771 (defun gnus-article-show-summary ()
14772   "Reconfigure windows to show summary buffer."
14773   (interactive)
14774   (gnus-configure-windows 'article)
14775   (gnus-summary-goto-subject gnus-current-article))
14776
14777 (defun gnus-article-describe-briefly ()
14778   "Describe article mode commands briefly."
14779   (interactive)
14780   (gnus-message 6
14781                 (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")))
14782
14783 (defun gnus-article-summary-command ()
14784   "Execute the last keystroke in the summary buffer."
14785   (interactive)
14786   (let ((obuf (current-buffer))
14787         (owin (current-window-configuration))
14788         func)
14789     (switch-to-buffer gnus-summary-buffer 'norecord)
14790     (setq func (lookup-key (current-local-map) (this-command-keys)))
14791     (call-interactively func)
14792     (set-buffer obuf)
14793     (set-window-configuration owin)
14794     (set-window-point (get-buffer-window (current-buffer)) (point))))
14795
14796 (defun gnus-article-summary-command-nosave ()
14797   "Execute the last keystroke in the summary buffer."
14798   (interactive)
14799   (let (func)
14800     (pop-to-buffer gnus-summary-buffer 'norecord)
14801     (setq func (lookup-key (current-local-map) (this-command-keys)))
14802     (call-interactively func)))
14803
14804 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14805   "Read a summary buffer key sequence and execute it from the article buffer."
14806   (interactive "P")
14807   (let ((nosaves
14808          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14809            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14810            "=" "^" "\M-^" "|"))
14811         keys)
14812     (save-excursion
14813       (set-buffer gnus-summary-buffer)
14814       (push (or key last-command-event) unread-command-events)
14815       (setq keys (read-key-sequence nil)))
14816     (message "")
14817
14818     (if (member keys nosaves)
14819         (let (func)
14820           (pop-to-buffer gnus-summary-buffer 'norecord)
14821           (if (setq func (lookup-key (current-local-map) keys))
14822               (call-interactively func)
14823             (ding)))
14824       (let ((obuf (current-buffer))
14825             (owin (current-window-configuration))
14826             (opoint (point))
14827             func in-buffer)
14828         (if not-restore-window
14829             (pop-to-buffer gnus-summary-buffer 'norecord)
14830           (switch-to-buffer gnus-summary-buffer 'norecord))
14831         (setq in-buffer (current-buffer))
14832         (if (setq func (lookup-key (current-local-map) keys))
14833             (call-interactively func)
14834           (ding))
14835         (when (eq in-buffer (current-buffer))
14836           (set-buffer obuf)
14837           (unless not-restore-window
14838             (set-window-configuration owin))
14839           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14840
14841 \f
14842 ;;;
14843 ;;; Kill file handling.
14844 ;;;
14845
14846 ;;;###autoload
14847 (defalias 'gnus-batch-kill 'gnus-batch-score)
14848 ;;;###autoload
14849 (defun gnus-batch-score ()
14850   "Run batched scoring.
14851 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14852 Newsgroups is a list of strings in Bnews format.  If you want to score
14853 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14854 score the alt hierarchy, you'd say \"!alt.all\"."
14855   (interactive)
14856   (let* ((yes-and-no
14857           (gnus-newsrc-parse-options
14858            (apply (function concat)
14859                   (mapcar (lambda (g) (concat g " "))
14860                           command-line-args-left))))
14861          (gnus-expert-user t)
14862          (nnmail-spool-file nil)
14863          (gnus-use-dribble-file nil)
14864          (yes (car yes-and-no))
14865          (no (cdr yes-and-no))
14866          group newsrc entry
14867          ;; Disable verbose message.
14868          gnus-novice-user gnus-large-newsgroup)
14869     ;; Eat all arguments.
14870     (setq command-line-args-left nil)
14871     ;; Start Gnus.
14872     (gnus)
14873     ;; Apply kills to specified newsgroups in command line arguments.
14874     (setq newsrc (cdr gnus-newsrc-alist))
14875     (while newsrc
14876       (setq group (caar newsrc))
14877       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14878       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14879                (and (car entry)
14880                     (or (eq (car entry) t)
14881                         (not (zerop (car entry)))))
14882                (if yes (string-match yes group) t)
14883                (or (null no) (not (string-match no group))))
14884           (progn
14885             (gnus-summary-read-group group nil t nil t)
14886             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14887                  (gnus-summary-exit))))
14888       (setq newsrc (cdr newsrc)))
14889     ;; Exit Emacs.
14890     (switch-to-buffer gnus-group-buffer)
14891     (gnus-group-save-newsrc)))
14892
14893 (defun gnus-apply-kill-file ()
14894   "Apply a kill file to the current newsgroup.
14895 Returns the number of articles marked as read."
14896   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14897           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14898       (gnus-apply-kill-file-internal)
14899     0))
14900
14901 (defun gnus-kill-save-kill-buffer ()
14902   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14903     (when (get-file-buffer file)
14904       (save-excursion
14905         (set-buffer (get-file-buffer file))
14906         (and (buffer-modified-p) (save-buffer))
14907         (kill-buffer (current-buffer))))))
14908
14909 (defvar gnus-kill-file-name "KILL"
14910   "Suffix of the kill files.")
14911
14912 (defun gnus-newsgroup-kill-file (newsgroup)
14913   "Return the name of a kill file name for NEWSGROUP.
14914 If NEWSGROUP is nil, return the global kill file name instead."
14915   (cond 
14916    ;; The global KILL file is placed at top of the directory.
14917    ((or (null newsgroup)
14918         (string-equal newsgroup ""))
14919     (expand-file-name gnus-kill-file-name
14920                       gnus-kill-files-directory))
14921    ;; Append ".KILL" to newsgroup name.
14922    ((gnus-use-long-file-name 'not-kill)
14923     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14924                               "." gnus-kill-file-name)
14925                       gnus-kill-files-directory))
14926    ;; Place "KILL" under the hierarchical directory.
14927    (t
14928     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14929                               "/" gnus-kill-file-name)
14930                       gnus-kill-files-directory))))
14931
14932 \f
14933 ;;;
14934 ;;; Dribble file
14935 ;;;
14936
14937 (defvar gnus-dribble-ignore nil)
14938 (defvar gnus-dribble-eval-file nil)
14939
14940 (defun gnus-dribble-file-name ()
14941   "Return the dribble file for the current .newsrc."
14942   (concat
14943    (if gnus-dribble-directory
14944        (concat (file-name-as-directory gnus-dribble-directory)
14945                (file-name-nondirectory gnus-current-startup-file))
14946      gnus-current-startup-file)
14947    "-dribble"))
14948
14949 (defun gnus-dribble-enter (string)
14950   "Enter STRING into the dribble buffer."
14951   (if (and (not gnus-dribble-ignore)
14952            gnus-dribble-buffer
14953            (buffer-name gnus-dribble-buffer))
14954       (let ((obuf (current-buffer)))
14955         (set-buffer gnus-dribble-buffer)
14956         (insert string "\n")
14957         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14958         (bury-buffer gnus-dribble-buffer)
14959         (set-buffer obuf))))
14960
14961 (defun gnus-dribble-read-file ()
14962   "Read the dribble file from disk."
14963   (let ((dribble-file (gnus-dribble-file-name)))
14964     (save-excursion
14965       (set-buffer (setq gnus-dribble-buffer
14966                         (get-buffer-create
14967                          (file-name-nondirectory dribble-file))))
14968       (gnus-add-current-to-buffer-list)
14969       (erase-buffer)
14970       (setq buffer-file-name dribble-file)
14971       (auto-save-mode t)
14972       (buffer-disable-undo (current-buffer))
14973       (bury-buffer (current-buffer))
14974       (set-buffer-modified-p nil)
14975       (let ((auto (make-auto-save-file-name))
14976             (gnus-dribble-ignore t)
14977             modes)
14978         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14979           ;; Load whichever file is newest -- the auto save file
14980           ;; or the "real" file.
14981           (if (file-newer-than-file-p auto dribble-file)
14982               (insert-file-contents auto)
14983             (insert-file-contents dribble-file))
14984           (unless (zerop (buffer-size))
14985             (set-buffer-modified-p t))
14986           ;; Set the file modes to reflect the .newsrc file modes.
14987           (save-buffer)
14988           (when (and (file-exists-p gnus-current-startup-file)
14989                      (setq modes (file-modes gnus-current-startup-file)))
14990             (set-file-modes dribble-file modes))
14991           ;; Possibly eval the file later.
14992           (when (gnus-y-or-n-p
14993                  "Auto-save file exists.  Do you want to read it? ")
14994             (setq gnus-dribble-eval-file t)))))))
14995
14996 (defun gnus-dribble-eval-file ()
14997   (when gnus-dribble-eval-file
14998     (setq gnus-dribble-eval-file nil)
14999     (save-excursion
15000       (let ((gnus-dribble-ignore t))
15001         (set-buffer gnus-dribble-buffer)
15002         (eval-buffer (current-buffer))))))
15003
15004 (defun gnus-dribble-delete-file ()
15005   (when (file-exists-p (gnus-dribble-file-name))
15006     (delete-file (gnus-dribble-file-name)))
15007   (when gnus-dribble-buffer
15008     (save-excursion
15009       (set-buffer gnus-dribble-buffer)
15010       (let ((auto (make-auto-save-file-name)))
15011         (if (file-exists-p auto)
15012             (delete-file auto))
15013         (erase-buffer)
15014         (set-buffer-modified-p nil)))))
15015
15016 (defun gnus-dribble-save ()
15017   (when (and gnus-dribble-buffer
15018              (buffer-name gnus-dribble-buffer))
15019     (save-excursion
15020       (set-buffer gnus-dribble-buffer)
15021       (save-buffer))))
15022
15023 (defun gnus-dribble-clear ()
15024   (when (gnus-buffer-exists-p gnus-dribble-buffer)
15025     (save-excursion
15026       (set-buffer gnus-dribble-buffer)
15027       (erase-buffer)
15028       (set-buffer-modified-p nil)
15029       (setq buffer-saved-size (buffer-size)))))
15030
15031 \f
15032 ;;;
15033 ;;; Server Communication
15034 ;;;
15035
15036 (defun gnus-start-news-server (&optional confirm)
15037   "Open a method for getting news.
15038 If CONFIRM is non-nil, the user will be asked for an NNTP server."
15039   (let (how)
15040     (if gnus-current-select-method
15041         ;; Stream is already opened.
15042         nil
15043       ;; Open NNTP server.
15044       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
15045       (if confirm
15046           (progn
15047             ;; Read server name with completion.
15048             (setq gnus-nntp-server
15049                   (completing-read "NNTP server: "
15050                                    (mapcar (lambda (server) (list server))
15051                                            (cons (list gnus-nntp-server)
15052                                                  gnus-secondary-servers))
15053                                    nil nil gnus-nntp-server))))
15054
15055       (if (and gnus-nntp-server
15056                (stringp gnus-nntp-server)
15057                (not (string= gnus-nntp-server "")))
15058           (setq gnus-select-method
15059                 (cond ((or (string= gnus-nntp-server "")
15060                            (string= gnus-nntp-server "::"))
15061                        (list 'nnspool (system-name)))
15062                       ((string-match "^:" gnus-nntp-server)
15063                        (list 'nnmh gnus-nntp-server
15064                              (list 'nnmh-directory
15065                                    (file-name-as-directory
15066                                     (expand-file-name
15067                                      (concat "~/" (substring
15068                                                    gnus-nntp-server 1)))))
15069                              (list 'nnmh-get-new-mail nil)))
15070                       (t
15071                        (list 'nntp gnus-nntp-server)))))
15072
15073       (setq how (car gnus-select-method))
15074       (cond ((eq how 'nnspool)
15075              (require 'nnspool)
15076              (gnus-message 5 "Looking up local news spool..."))
15077             ((eq how 'nnmh)
15078              (require 'nnmh)
15079              (gnus-message 5 "Looking up mh spool..."))
15080             (t
15081              (require 'nntp)))
15082       (setq gnus-current-select-method gnus-select-method)
15083       (run-hooks 'gnus-open-server-hook)
15084       (or
15085        ;; gnus-open-server-hook might have opened it
15086        (gnus-server-opened gnus-select-method)
15087        (gnus-open-server gnus-select-method)
15088        (gnus-y-or-n-p
15089         (format
15090          "%s (%s) open error: '%s'.     Continue? "
15091          (car gnus-select-method) (cadr gnus-select-method)
15092          (gnus-status-message gnus-select-method)))
15093        (gnus-error 1 "Couldn't open server on %s"
15094                    (nth 1 gnus-select-method))))))
15095
15096 (defun gnus-check-group (group)
15097   "Try to make sure that the server where GROUP exists is alive."
15098   (let ((method (gnus-find-method-for-group group)))
15099     (or (gnus-server-opened method)
15100         (gnus-open-server method))))
15101
15102 (defun gnus-check-server (&optional method silent)
15103   "Check whether the connection to METHOD is down.
15104 If METHOD is nil, use `gnus-select-method'.
15105 If it is down, start it up (again)."
15106   (let ((method (or method gnus-select-method)))
15107     ;; Transform virtual server names into select methods.
15108     (when (stringp method)
15109       (setq method (gnus-server-to-method method)))
15110     (if (gnus-server-opened method)
15111         ;; The stream is already opened.
15112         t
15113       ;; Open the server.
15114       (unless silent
15115         (gnus-message 5 "Opening %s server%s..." (car method)
15116                       (if (equal (nth 1 method) "") ""
15117                         (format " on %s" (nth 1 method)))))
15118       (run-hooks 'gnus-open-server-hook)
15119       (prog1
15120           (gnus-open-server method)
15121         (unless silent
15122           (message ""))))))
15123
15124 (defun gnus-get-function (method function &optional noerror)
15125   "Return a function symbol based on METHOD and FUNCTION."
15126   ;; Translate server names into methods.
15127   (unless method
15128     (error "Attempted use of a nil select method"))
15129   (when (stringp method)
15130     (setq method (gnus-server-to-method method)))
15131   (let ((func (intern (format "%s-%s" (car method) function))))
15132     ;; If the functions isn't bound, we require the backend in
15133     ;; question.
15134     (unless (fboundp func)
15135       (require (car method))
15136       (when (and (not (fboundp func))
15137                  (not noerror))
15138         ;; This backend doesn't implement this function.
15139         (error "No such function: %s" func)))
15140     func))
15141
15142 \f
15143 ;;;
15144 ;;; Interface functions to the backends.
15145 ;;;
15146
15147 (defun gnus-open-server (method)
15148   "Open a connection to METHOD."
15149   (when (stringp method)
15150     (setq method (gnus-server-to-method method)))
15151   (let ((elem (assoc method gnus-opened-servers)))
15152     ;; If this method was previously denied, we just return nil.
15153     (if (eq (nth 1 elem) 'denied)
15154         (progn
15155           (gnus-message 1 "Denied server")
15156           nil)
15157       ;; Open the server.
15158       (let ((result
15159              (funcall (gnus-get-function method 'open-server)
15160                       (nth 1 method) (nthcdr 2 method))))
15161         ;; If this hasn't been opened before, we add it to the list.
15162         (unless elem
15163           (setq elem (list method nil)
15164                 gnus-opened-servers (cons elem gnus-opened-servers)))
15165         ;; Set the status of this server.
15166         (setcar (cdr elem) (if result 'ok 'denied))
15167         ;; Return the result from the "open" call.
15168         result))))
15169
15170 (defun gnus-close-server (method)
15171   "Close the connection to METHOD."
15172   (when (stringp method)
15173     (setq method (gnus-server-to-method method)))
15174   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
15175
15176 (defun gnus-request-list (method)
15177   "Request the active file from METHOD."
15178   (when (stringp method)
15179     (setq method (gnus-server-to-method method)))
15180   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
15181
15182 (defun gnus-request-list-newsgroups (method)
15183   "Request the newsgroups file from METHOD."
15184   (when (stringp method)
15185     (setq method (gnus-server-to-method method)))
15186   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
15187
15188 (defun gnus-request-newgroups (date method)
15189   "Request all new groups since DATE from METHOD."
15190   (when (stringp method)
15191     (setq method (gnus-server-to-method method)))
15192   (funcall (gnus-get-function method 'request-newgroups)
15193            date (nth 1 method)))
15194
15195 (defun gnus-server-opened (method)
15196   "Check whether a connection to METHOD has been opened."
15197   (when (stringp method)
15198     (setq method (gnus-server-to-method method)))
15199   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
15200
15201 (defun gnus-status-message (method)
15202   "Return the status message from METHOD.
15203 If METHOD is a string, it is interpreted as a group name.   The method
15204 this group uses will be queried."
15205   (let ((method (if (stringp method) (gnus-find-method-for-group method)
15206                   method)))
15207     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
15208
15209 (defun gnus-request-group (group &optional dont-check method)
15210   "Request GROUP.  If DONT-CHECK, no information is required."
15211   (let ((method (or method (gnus-find-method-for-group group))))
15212     (when (stringp method)
15213       (setq method (gnus-server-to-method method)))
15214     (funcall (gnus-get-function method 'request-group)
15215              (gnus-group-real-name group) (nth 1 method) dont-check)))
15216
15217 (defun gnus-request-asynchronous (group &optional articles)
15218   "Request that GROUP behave asynchronously.
15219 ARTICLES is the `data' of the group."
15220   (let ((method (gnus-find-method-for-group group)))
15221     (funcall (gnus-get-function method 'request-asynchronous)
15222              (gnus-group-real-name group) (nth 1 method) articles)))
15223
15224 (defun gnus-list-active-group (group)
15225   "Request active information on GROUP."
15226   (let ((method (gnus-find-method-for-group group))
15227         (func 'list-active-group))
15228     (when (gnus-check-backend-function func group)
15229       (funcall (gnus-get-function method func)
15230                (gnus-group-real-name group) (nth 1 method)))))
15231
15232 (defun gnus-request-group-description (group)
15233   "Request a description of GROUP."
15234   (let ((method (gnus-find-method-for-group group))
15235         (func 'request-group-description))
15236     (when (gnus-check-backend-function func group)
15237       (funcall (gnus-get-function method func)
15238                (gnus-group-real-name group) (nth 1 method)))))
15239
15240 (defun gnus-close-group (group)
15241   "Request the GROUP be closed."
15242   (let ((method (gnus-find-method-for-group group)))
15243     (funcall (gnus-get-function method 'close-group)
15244              (gnus-group-real-name group) (nth 1 method))))
15245
15246 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15247   "Request headers for ARTICLES in GROUP.
15248 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15249   (let ((method (gnus-find-method-for-group group)))
15250     (if (and gnus-use-cache (numberp (car articles)))
15251         (gnus-cache-retrieve-headers articles group fetch-old)
15252       (funcall (gnus-get-function method 'retrieve-headers)
15253                articles (gnus-group-real-name group) (nth 1 method)
15254                fetch-old))))
15255
15256 (defun gnus-retrieve-groups (groups method)
15257   "Request active information on GROUPS from METHOD."
15258   (when (stringp method)
15259     (setq method (gnus-server-to-method method)))
15260   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15261
15262 (defun gnus-request-type (group &optional article)
15263   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15264   (let ((method (gnus-find-method-for-group group)))
15265     (if (not (gnus-check-backend-function 'request-type (car method)))
15266         'unknown
15267       (funcall (gnus-get-function method 'request-type)
15268                (gnus-group-real-name group) article))))
15269
15270 (defun gnus-request-update-mark (group article mark)
15271   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15272   (let ((method (gnus-find-method-for-group group)))
15273     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15274         mark
15275       (funcall (gnus-get-function method 'request-update-mark)
15276                (gnus-group-real-name group) article mark))))
15277
15278 (defun gnus-request-article (article group &optional buffer)
15279   "Request the ARTICLE in GROUP.
15280 ARTICLE can either be an article number or an article Message-ID.
15281 If BUFFER, insert the article in that group."
15282   (let ((method (gnus-find-method-for-group group)))
15283     (funcall (gnus-get-function method 'request-article)
15284              article (gnus-group-real-name group) (nth 1 method) buffer)))
15285
15286 (defun gnus-request-head (article group)
15287   "Request the head of ARTICLE in GROUP."
15288   (let* ((method (gnus-find-method-for-group group))
15289          (head (gnus-get-function method 'request-head t)))
15290     (if (fboundp head)
15291         (funcall head article (gnus-group-real-name group) (nth 1 method))
15292       (let ((res (gnus-request-article article group)))
15293         (when res
15294           (save-excursion
15295             (set-buffer nntp-server-buffer)
15296             (goto-char (point-min))
15297             (when (search-forward "\n\n" nil t)
15298               (delete-region (1- (point)) (point-max)))
15299             (nnheader-fold-continuation-lines)))
15300         res))))
15301
15302 (defun gnus-request-body (article group)
15303   "Request the body of ARTICLE in GROUP."
15304   (let ((method (gnus-find-method-for-group group)))
15305     (funcall (gnus-get-function method 'request-body)
15306              article (gnus-group-real-name group) (nth 1 method))))
15307
15308 (defun gnus-request-post (method)
15309   "Post the current buffer using METHOD."
15310   (when (stringp method)
15311     (setq method (gnus-server-to-method method)))
15312   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15313
15314 (defun gnus-request-scan (group method)
15315   "Request a SCAN being performed in GROUP from METHOD.
15316 If GROUP is nil, all groups on METHOD are scanned."
15317   (let ((method (if group (gnus-find-method-for-group group) method)))
15318     (funcall (gnus-get-function method 'request-scan)
15319              (and group (gnus-group-real-name group)) (nth 1 method))))
15320
15321 (defsubst gnus-request-update-info (info method)
15322   "Request that METHOD update INFO."
15323   (when (stringp method)
15324     (setq method (gnus-server-to-method method)))
15325   (when (gnus-check-backend-function 'request-update-info (car method))
15326     (funcall (gnus-get-function method 'request-update-info)
15327              (gnus-group-real-name (gnus-info-group info))
15328              info (nth 1 method))))
15329
15330 (defun gnus-request-expire-articles (articles group &optional force)
15331   (let ((method (gnus-find-method-for-group group)))
15332     (funcall (gnus-get-function method 'request-expire-articles)
15333              articles (gnus-group-real-name group) (nth 1 method)
15334              force)))
15335
15336 (defun gnus-request-move-article
15337   (article group server accept-function &optional last)
15338   (let ((method (gnus-find-method-for-group group)))
15339     (funcall (gnus-get-function method 'request-move-article)
15340              article (gnus-group-real-name group)
15341              (nth 1 method) accept-function last)))
15342
15343 (defun gnus-request-accept-article (group method &optional last)
15344   ;; Make sure there's a newline at the end of the article.
15345   (when (stringp method)
15346     (setq method (gnus-server-to-method method)))
15347   (when (and (not method)
15348              (stringp group))
15349     (setq method (gnus-group-name-to-method group)))
15350   (goto-char (point-max))
15351   (unless (bolp)
15352     (insert "\n"))
15353   (let ((func (car (or method (gnus-find-method-for-group group)))))
15354     (funcall (intern (format "%s-request-accept-article" func))
15355              (if (stringp group) (gnus-group-real-name group) group)
15356              (cadr method)
15357              last)))
15358
15359 (defun gnus-request-replace-article (article group buffer)
15360   (let ((func (car (gnus-find-method-for-group group))))
15361     (funcall (intern (format "%s-request-replace-article" func))
15362              article (gnus-group-real-name group) buffer)))
15363
15364 (defun gnus-request-associate-buffer (group)
15365   (let ((method (gnus-find-method-for-group group)))
15366     (funcall (gnus-get-function method 'request-associate-buffer)
15367              (gnus-group-real-name group))))
15368
15369 (defun gnus-request-restore-buffer (article group)
15370   "Request a new buffer restored to the state of ARTICLE."
15371   (let ((method (gnus-find-method-for-group group)))
15372     (funcall (gnus-get-function method 'request-restore-buffer)
15373              article (gnus-group-real-name group) (nth 1 method))))
15374
15375 (defun gnus-request-create-group (group &optional method)
15376   (when (stringp method)
15377     (setq method (gnus-server-to-method method)))
15378   (let ((method (or method (gnus-find-method-for-group group))))
15379     (funcall (gnus-get-function method 'request-create-group)
15380              (gnus-group-real-name group) (nth 1 method))))
15381
15382 (defun gnus-request-delete-group (group &optional force)
15383   (let ((method (gnus-find-method-for-group group)))
15384     (funcall (gnus-get-function method 'request-delete-group)
15385              (gnus-group-real-name group) force (nth 1 method))))
15386
15387 (defun gnus-request-rename-group (group new-name)
15388   (let ((method (gnus-find-method-for-group group)))
15389     (funcall (gnus-get-function method 'request-rename-group)
15390              (gnus-group-real-name group)
15391              (gnus-group-real-name new-name) (nth 1 method))))
15392
15393 (defun gnus-member-of-valid (symbol group)
15394   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15395   (memq symbol (assoc
15396                 (symbol-name (car (gnus-find-method-for-group group)))
15397                 gnus-valid-select-methods)))
15398
15399 (defun gnus-method-option-p (method option)
15400   "Return non-nil if select METHOD has OPTION as a parameter."
15401   (when (stringp method)
15402     (setq method (gnus-server-to-method method)))
15403   (memq option (assoc (format "%s" (car method))
15404                       gnus-valid-select-methods)))
15405
15406 (defun gnus-server-extend-method (group method)
15407   ;; This function "extends" a virtual server.  If the server is
15408   ;; "hello", and the select method is ("hello" (my-var "something"))
15409   ;; in the group "alt.alt", this will result in a new virtual server
15410   ;; called "hello+alt.alt".
15411   (let ((entry
15412          (gnus-copy-sequence
15413           (if (equal (car method) "native") gnus-select-method
15414             (cdr (assoc (car method) gnus-server-alist))))))
15415     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15416     (nconc entry (cdr method))))
15417
15418 (defun gnus-server-status (method)
15419   "Return the status of METHOD."
15420   (nth 1 (assoc method gnus-opened-servers)))
15421
15422 (defun gnus-group-name-to-method (group)
15423   "Return a select method suitable for GROUP."
15424   (if (string-match ":" group)
15425       (let ((server (substring group 0 (match-beginning 0))))
15426         (if (string-match "\\+" server)
15427             (list (intern (substring server 0 (match-beginning 0)))
15428                   (substring server (match-end 0)))
15429           (list (intern server) "")))
15430     gnus-select-method))
15431
15432 (defun gnus-find-method-for-group (group &optional info)
15433   "Find the select method that GROUP uses."
15434   (or gnus-override-method
15435       (and (not group)
15436            gnus-select-method)
15437       (let ((info (or info (gnus-get-info group)))
15438             method)
15439         (if (or (not info)
15440                 (not (setq method (gnus-info-method info)))
15441                 (equal method "native"))
15442             gnus-select-method
15443           (setq method
15444                 (cond ((stringp method)
15445                        (gnus-server-to-method method))
15446                       ((stringp (car method))
15447                        (gnus-server-extend-method group method))
15448                       (t
15449                        method)))
15450           (cond ((equal (cadr method) "")
15451                  method)
15452                 ((null (cadr method))
15453                  (list (car method) ""))
15454                 (t
15455                  (gnus-server-add-address method)))))))
15456
15457 (defun gnus-check-backend-function (func group)
15458   "Check whether GROUP supports function FUNC."
15459   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15460                   group)))
15461     (fboundp (intern (format "%s-%s" method func)))))
15462
15463 (defun gnus-methods-using (feature)
15464   "Find all methods that have FEATURE."
15465   (let ((valids gnus-valid-select-methods)
15466         outs)
15467     (while valids
15468       (if (memq feature (car valids))
15469           (setq outs (cons (car valids) outs)))
15470       (setq valids (cdr valids)))
15471     outs))
15472
15473 \f
15474 ;;;
15475 ;;; Active & Newsrc File Handling
15476 ;;;
15477
15478 (defun gnus-setup-news (&optional rawfile level dont-connect)
15479   "Setup news information.
15480 If RAWFILE is non-nil, the .newsrc file will also be read.
15481 If LEVEL is non-nil, the news will be set up at level LEVEL."
15482   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15483
15484     (when init 
15485       ;; Clear some variables to re-initialize news information.
15486       (setq gnus-newsrc-alist nil
15487             gnus-active-hashtb nil)
15488       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15489       (gnus-read-newsrc-file rawfile))
15490
15491     (when (and (not (assoc "archive" gnus-server-alist))
15492                (gnus-archive-server-wanted-p))
15493       (push (cons "archive" gnus-message-archive-method)
15494             gnus-server-alist))
15495
15496     ;; If we don't read the complete active file, we fill in the
15497     ;; hashtb here.
15498     (if (or (null gnus-read-active-file)
15499             (eq gnus-read-active-file 'some))
15500         (gnus-update-active-hashtb-from-killed))
15501
15502     ;; Read the active file and create `gnus-active-hashtb'.
15503     ;; If `gnus-read-active-file' is nil, then we just create an empty
15504     ;; hash table.  The partial filling out of the hash table will be
15505     ;; done in `gnus-get-unread-articles'.
15506     (and gnus-read-active-file
15507          (not level)
15508          (gnus-read-active-file))
15509
15510     (or gnus-active-hashtb
15511         (setq gnus-active-hashtb (make-vector 4095 0)))
15512
15513     ;; Initialize the cache.
15514     (when gnus-use-cache
15515       (gnus-cache-open))
15516
15517     ;; Possibly eval the dribble file.
15518     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15519
15520     ;; Slave Gnusii should then clear the dribble buffer.
15521     (when (and init gnus-slave)
15522       (gnus-dribble-clear))
15523
15524     (gnus-update-format-specifications)
15525
15526     ;; See whether we need to read the description file.
15527     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15528              (not gnus-description-hashtb)
15529              (not dont-connect)
15530              gnus-read-active-file)
15531         (gnus-read-all-descriptions-files))
15532
15533     ;; Find new newsgroups and treat them.
15534     (if (and init gnus-check-new-newsgroups (not level)
15535              (gnus-check-server gnus-select-method))
15536         (gnus-find-new-newsgroups))
15537
15538     ;; We might read in new NoCeM messages here.
15539     (when (and gnus-use-nocem 
15540                (not level)
15541                (not dont-connect))
15542       (gnus-nocem-scan-groups))
15543
15544     ;; Find the number of unread articles in each non-dead group.
15545     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15546       (gnus-get-unread-articles level))
15547
15548     (if (and init gnus-check-bogus-newsgroups
15549              gnus-read-active-file (not level)
15550              (gnus-server-opened gnus-select-method))
15551         (gnus-check-bogus-newsgroups))))
15552
15553 (defun gnus-find-new-newsgroups (&optional arg)
15554   "Search for new newsgroups and add them.
15555 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15556 The `-n' option line from .newsrc is respected.
15557 If ARG (the prefix), use the `ask-server' method to query
15558 the server for new groups."
15559   (interactive "P")
15560   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15561                        (null gnus-read-active-file)
15562                        (eq gnus-read-active-file 'some))
15563                    'ask-server gnus-check-new-newsgroups)))
15564     (unless (gnus-check-first-time-used)
15565       (if (or (consp check)
15566               (eq check 'ask-server))
15567           ;; Ask the server for new groups.
15568           (gnus-ask-server-for-new-groups)
15569         ;; Go through the active hashtb and look for new groups.
15570         (let ((groups 0)
15571               group new-newsgroups)
15572           (gnus-message 5 "Looking for new newsgroups...")
15573           (unless gnus-have-read-active-file
15574             (gnus-read-active-file))
15575           (setq gnus-newsrc-last-checked-date (current-time-string))
15576           (unless gnus-killed-hashtb
15577             (gnus-make-hashtable-from-killed))
15578           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15579           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15580           (mapatoms
15581            (lambda (sym)
15582              (if (or (null (setq group (symbol-name sym)))
15583                      (not (boundp sym))
15584                      (null (symbol-value sym))
15585                      (gnus-gethash group gnus-killed-hashtb)
15586                      (gnus-gethash group gnus-newsrc-hashtb))
15587                  ()
15588                (let ((do-sub (gnus-matches-options-n group)))
15589                  (cond
15590                   ((eq do-sub 'subscribe)
15591                    (setq groups (1+ groups))
15592                    (gnus-sethash group group gnus-killed-hashtb)
15593                    (funcall gnus-subscribe-options-newsgroup-method group))
15594                   ((eq do-sub 'ignore)
15595                    nil)
15596                   (t
15597                    (setq groups (1+ groups))
15598                    (gnus-sethash group group gnus-killed-hashtb)
15599                    (if gnus-subscribe-hierarchical-interactive
15600                        (setq new-newsgroups (cons group new-newsgroups))
15601                      (funcall gnus-subscribe-newsgroup-method group)))))))
15602            gnus-active-hashtb)
15603           (when new-newsgroups
15604             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15605           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15606           (if (> groups 0)
15607               (gnus-message 6 "%d new newsgroup%s arrived."
15608                             groups (if (> groups 1) "s have" " has"))
15609             (gnus-message 6 "No new newsgroups.")))))))
15610
15611 (defun gnus-matches-options-n (group)
15612   ;; Returns `subscribe' if the group is to be unconditionally
15613   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15614   ;; no match for the group.
15615
15616   ;; First we check the two user variables.
15617   (cond
15618    ((and gnus-options-subscribe
15619          (string-match gnus-options-subscribe group))
15620     'subscribe)
15621    ((and gnus-auto-subscribed-groups
15622          (string-match gnus-auto-subscribed-groups group))
15623     'subscribe)
15624    ((and gnus-options-not-subscribe
15625          (string-match gnus-options-not-subscribe group))
15626     'ignore)
15627    ;; Then we go through the list that was retrieved from the .newsrc
15628    ;; file.  This list has elements on the form
15629    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15630    ;; is in the reverse order of the options line) is returned.
15631    (t
15632     (let ((regs gnus-newsrc-options-n))
15633       (while (and regs
15634                   (not (string-match (caar regs) group)))
15635         (setq regs (cdr regs)))
15636       (and regs (cdar regs))))))
15637
15638 (defun gnus-ask-server-for-new-groups ()
15639   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15640          (methods (cons gnus-select-method
15641                         (nconc
15642                          (when (gnus-archive-server-wanted-p)
15643                            (list "archive"))
15644                          (append
15645                           (and (consp gnus-check-new-newsgroups)
15646                                gnus-check-new-newsgroups)
15647                           gnus-secondary-select-methods))))
15648          (groups 0)
15649          (new-date (current-time-string))
15650          group new-newsgroups got-new method hashtb
15651          gnus-override-subscribe-method)
15652     ;; Go through both primary and secondary select methods and
15653     ;; request new newsgroups.
15654     (while (setq method (gnus-server-get-method nil (pop methods)))
15655       (setq new-newsgroups nil)
15656       (setq gnus-override-subscribe-method method)
15657       (when (and (gnus-check-server method)
15658                  (gnus-request-newgroups date method))
15659         (save-excursion
15660           (setq got-new t)
15661           (setq hashtb (gnus-make-hashtable 100))
15662           (set-buffer nntp-server-buffer)
15663           ;; Enter all the new groups into a hashtable.
15664           (gnus-active-to-gnus-format method hashtb 'ignore))
15665         ;; Now all new groups from `method' are in `hashtb'.
15666         (mapatoms
15667          (lambda (group-sym)
15668            (if (or (null (setq group (symbol-name group-sym)))
15669                    (not (boundp group-sym))
15670                    (null (symbol-value group-sym))
15671                    (gnus-gethash group gnus-newsrc-hashtb)
15672                    (member group gnus-zombie-list)
15673                    (member group gnus-killed-list))
15674                ;; The group is already known.
15675                ()
15676              ;; Make this group active.
15677              (when (symbol-value group-sym)
15678                (gnus-set-active group (symbol-value group-sym)))
15679              ;; Check whether we want it or not.
15680              (let ((do-sub (gnus-matches-options-n group)))
15681                (cond
15682                 ((eq do-sub 'subscribe)
15683                  (incf groups)
15684                  (gnus-sethash group group gnus-killed-hashtb)
15685                  (funcall gnus-subscribe-options-newsgroup-method group))
15686                 ((eq do-sub 'ignore)
15687                  nil)
15688                 (t
15689                  (incf groups)
15690                  (gnus-sethash group group gnus-killed-hashtb)
15691                  (if gnus-subscribe-hierarchical-interactive
15692                      (push group new-newsgroups)
15693                    (funcall gnus-subscribe-newsgroup-method group)))))))
15694          hashtb))
15695       (when new-newsgroups
15696         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15697     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15698     (when (> groups 0)
15699       (gnus-message 6 "%d new newsgroup%s arrived."
15700                     groups (if (> groups 1) "s have" " has")))
15701     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15702     got-new))
15703
15704 (defun gnus-check-first-time-used ()
15705   (if (or (> (length gnus-newsrc-alist) 1)
15706           (file-exists-p gnus-startup-file)
15707           (file-exists-p (concat gnus-startup-file ".el"))
15708           (file-exists-p (concat gnus-startup-file ".eld")))
15709       nil
15710     (gnus-message 6 "First time user; subscribing you to default groups")
15711     (unless (gnus-read-active-file-p)
15712       (gnus-read-active-file))
15713     (setq gnus-newsrc-last-checked-date (current-time-string))
15714     (let ((groups gnus-default-subscribed-newsgroups)
15715           group)
15716       (if (eq groups t)
15717           nil
15718         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15719         (mapatoms
15720          (lambda (sym)
15721            (if (null (setq group (symbol-name sym)))
15722                ()
15723              (let ((do-sub (gnus-matches-options-n group)))
15724                (cond
15725                 ((eq do-sub 'subscribe)
15726                  (gnus-sethash group group gnus-killed-hashtb)
15727                  (funcall gnus-subscribe-options-newsgroup-method group))
15728                 ((eq do-sub 'ignore)
15729                  nil)
15730                 (t
15731                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15732          gnus-active-hashtb)
15733         (while groups
15734           (if (gnus-active (car groups))
15735               (gnus-group-change-level
15736                (car groups) gnus-level-default-subscribed gnus-level-killed))
15737           (setq groups (cdr groups)))
15738         (gnus-group-make-help-group)
15739         (and gnus-novice-user
15740              (gnus-message 7 "`A k' to list killed groups"))))))
15741
15742 (defun gnus-subscribe-group (group previous &optional method)
15743   (gnus-group-change-level
15744    (if method
15745        (list t group gnus-level-default-subscribed nil nil method)
15746      group)
15747    gnus-level-default-subscribed gnus-level-killed previous t))
15748
15749 ;; `gnus-group-change-level' is the fundamental function for changing
15750 ;; subscription levels of newsgroups.  This might mean just changing
15751 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15752 ;; again, which subscribes/unsubscribes a group, which is equally
15753 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15754 ;; from 8-9 to 1-7 means that you remove the group from the list of
15755 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15756 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15757 ;; which is trivial.
15758 ;; ENTRY can either be a string (newsgroup name) or a list (if
15759 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15760 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15761 ;; entries.
15762 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15763 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15764 ;; after.
15765 (defun gnus-group-change-level (entry level &optional oldlevel
15766                                       previous fromkilled)
15767   (let (group info active num)
15768     ;; Glean what info we can from the arguments
15769     (if (consp entry)
15770         (if fromkilled (setq group (nth 1 entry))
15771           (setq group (car (nth 2 entry))))
15772       (setq group entry))
15773     (if (and (stringp entry)
15774              oldlevel
15775              (< oldlevel gnus-level-zombie))
15776         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15777     (if (and (not oldlevel)
15778              (consp entry))
15779         (setq oldlevel (gnus-info-level (nth 2 entry)))
15780       (setq oldlevel (or oldlevel 9)))
15781     (if (stringp previous)
15782         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15783
15784     (if (and (>= oldlevel gnus-level-zombie)
15785              (gnus-gethash group gnus-newsrc-hashtb))
15786         ;; We are trying to subscribe a group that is already
15787         ;; subscribed.
15788         ()                              ; Do nothing.
15789
15790       (or (gnus-ephemeral-group-p group)
15791           (gnus-dribble-enter
15792            (format "(gnus-group-change-level %S %S %S %S %S)"
15793                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15794
15795       ;; Then we remove the newgroup from any old structures, if needed.
15796       ;; If the group was killed, we remove it from the killed or zombie
15797       ;; list.  If not, and it is in fact going to be killed, we remove
15798       ;; it from the newsrc hash table and assoc.
15799       (cond
15800        ((>= oldlevel gnus-level-zombie)
15801         (if (= oldlevel gnus-level-zombie)
15802             (setq gnus-zombie-list (delete group gnus-zombie-list))
15803           (setq gnus-killed-list (delete group gnus-killed-list))))
15804        (t
15805         (if (and (>= level gnus-level-zombie)
15806                  entry)
15807             (progn
15808               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15809               (if (nth 3 entry)
15810                   (setcdr (gnus-gethash (car (nth 3 entry))
15811                                         gnus-newsrc-hashtb)
15812                           (cdr entry)))
15813               (setcdr (cdr entry) (cdddr entry))))))
15814
15815       ;; Finally we enter (if needed) the list where it is supposed to
15816       ;; go, and change the subscription level.  If it is to be killed,
15817       ;; we enter it into the killed or zombie list.
15818       (cond 
15819        ((>= level gnus-level-zombie)
15820         ;; Remove from the hash table.
15821         (gnus-sethash group nil gnus-newsrc-hashtb)
15822         ;; We do not enter foreign groups into the list of dead
15823         ;; groups.
15824         (unless (gnus-group-foreign-p group)
15825           (if (= level gnus-level-zombie)
15826               (setq gnus-zombie-list (cons group gnus-zombie-list))
15827             (setq gnus-killed-list (cons group gnus-killed-list)))))
15828        (t
15829         ;; If the list is to be entered into the newsrc assoc, and
15830         ;; it was killed, we have to create an entry in the newsrc
15831         ;; hashtb format and fix the pointers in the newsrc assoc.
15832         (if (< oldlevel gnus-level-zombie)
15833             ;; It was alive, and it is going to stay alive, so we
15834             ;; just change the level and don't change any pointers or
15835             ;; hash table entries.
15836             (setcar (cdaddr entry) level)
15837           (if (listp entry)
15838               (setq info (cdr entry)
15839                     num (car entry))
15840             (setq active (gnus-active group))
15841             (setq num
15842                   (if active (- (1+ (cdr active)) (car active)) t))
15843             ;; Check whether the group is foreign.  If so, the
15844             ;; foreign select method has to be entered into the
15845             ;; info.
15846             (let ((method (or gnus-override-subscribe-method
15847                               (gnus-group-method group))))
15848               (if (eq method gnus-select-method)
15849                   (setq info (list group level nil))
15850                 (setq info (list group level nil nil method)))))
15851           (unless previous
15852             (setq previous
15853                   (let ((p gnus-newsrc-alist))
15854                     (while (cddr p)
15855                       (setq p (cdr p)))
15856                     p)))
15857           (setq entry (cons info (cddr previous)))
15858           (if (cdr previous)
15859               (progn
15860                 (setcdr (cdr previous) entry)
15861                 (gnus-sethash group (cons num (cdr previous))
15862                               gnus-newsrc-hashtb))
15863             (setcdr previous entry)
15864             (gnus-sethash group (cons num previous)
15865                           gnus-newsrc-hashtb))
15866           (when (cdr entry)
15867             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15868       (when gnus-group-change-level-function
15869         (funcall gnus-group-change-level-function group level oldlevel)))))
15870
15871 (defun gnus-kill-newsgroup (newsgroup)
15872   "Obsolete function.  Kills a newsgroup."
15873   (gnus-group-change-level
15874    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15875
15876 (defun gnus-check-bogus-newsgroups (&optional confirm)
15877   "Remove bogus newsgroups.
15878 If CONFIRM is non-nil, the user has to confirm the deletion of every
15879 newsgroup."
15880   (let ((newsrc (cdr gnus-newsrc-alist))
15881         bogus group entry info)
15882     (gnus-message 5 "Checking bogus newsgroups...")
15883     (unless (gnus-read-active-file-p)
15884       (gnus-read-active-file))
15885     (when (gnus-read-active-file-p)
15886       ;; Find all bogus newsgroup that are subscribed.
15887       (while newsrc
15888         (setq info (pop newsrc)
15889               group (gnus-info-group info))
15890         (unless (or (gnus-active group) ; Active
15891                     (gnus-info-method info) ; Foreign
15892                     (and confirm
15893                          (not (gnus-y-or-n-p
15894                                (format "Remove bogus newsgroup: %s " group)))))
15895           ;; Found a bogus newsgroup.
15896           (push group bogus)))
15897       ;; Remove all bogus subscribed groups by first killing them, and
15898       ;; then removing them from the list of killed groups.
15899       (while bogus
15900         (when (setq entry (gnus-gethash (setq group (pop bogus))
15901                                         gnus-newsrc-hashtb))
15902           (gnus-group-change-level entry gnus-level-killed)
15903           (setq gnus-killed-list (delete group gnus-killed-list))))
15904       ;; Then we remove all bogus groups from the list of killed and
15905       ;; zombie groups.  They are are removed without confirmation.
15906       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15907             killed)
15908         (while dead-lists
15909           (setq killed (symbol-value (car dead-lists)))
15910           (while killed
15911             (unless (gnus-active (setq group (pop killed)))
15912               ;; The group is bogus.
15913               ;; !!!Slow as hell.
15914               (set (car dead-lists)
15915                    (delete group (symbol-value (car dead-lists))))))
15916           (setq dead-lists (cdr dead-lists))))
15917       (gnus-message 5 "Checking bogus newsgroups...done"))))
15918
15919 (defun gnus-check-duplicate-killed-groups ()
15920   "Remove duplicates from the list of killed groups."
15921   (interactive)
15922   (let ((killed gnus-killed-list))
15923     (while killed
15924       (gnus-message 9 "%d" (length killed))
15925       (setcdr killed (delete (car killed) (cdr killed)))
15926       (setq killed (cdr killed)))))
15927
15928 ;; We want to inline a function from gnus-cache, so we cheat here:
15929 (eval-when-compile
15930   (provide 'gnus)
15931   (require 'gnus-cache))
15932
15933 (defun gnus-get-unread-articles-in-group (info active &optional update)
15934   (when active
15935     ;; Allow the backend to update the info in the group.
15936     (when (and update 
15937                (gnus-request-update-info
15938                 info (gnus-find-method-for-group (gnus-info-group info))))
15939       (gnus-activate-group (gnus-info-group info) nil t))
15940     (let* ((range (gnus-info-read info))
15941            (num 0))
15942       ;; If a cache is present, we may have to alter the active info.
15943       (when (and gnus-use-cache info)
15944         (inline (gnus-cache-possibly-alter-active 
15945                  (gnus-info-group info) active)))
15946       ;; Modify the list of read articles according to what articles
15947       ;; are available; then tally the unread articles and add the
15948       ;; number to the group hash table entry.
15949       (cond
15950        ((zerop (cdr active))
15951         (setq num 0))
15952        ((not range)
15953         (setq num (- (1+ (cdr active)) (car active))))
15954        ((not (listp (cdr range)))
15955         ;; Fix a single (num . num) range according to the
15956         ;; active hash table.
15957         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15958         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15959         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15960         ;; Compute number of unread articles.
15961         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15962        (t
15963         ;; The read list is a list of ranges.  Fix them according to
15964         ;; the active hash table.
15965         ;; First peel off any elements that are below the lower
15966         ;; active limit.
15967         (while (and (cdr range)
15968                     (>= (car active)
15969                         (or (and (atom (cadr range)) (cadr range))
15970                             (caadr range))))
15971           (if (numberp (car range))
15972               (setcar range
15973                       (cons (car range)
15974                             (or (and (numberp (cadr range))
15975                                      (cadr range))
15976                                 (cdadr range))))
15977             (setcdr (car range)
15978                     (or (and (numberp (nth 1 range)) (nth 1 range))
15979                         (cdadr range))))
15980           (setcdr range (cddr range)))
15981         ;; Adjust the first element to be the same as the lower limit.
15982         (if (and (not (atom (car range)))
15983                  (< (cdar range) (car active)))
15984             (setcdr (car range) (1- (car active))))
15985         ;; Then we want to peel off any elements that are higher
15986         ;; than the upper active limit.
15987         (let ((srange range))
15988           ;; Go past all legal elements.
15989           (while (and (cdr srange)
15990                       (<= (or (and (atom (cadr srange))
15991                                    (cadr srange))
15992                               (caadr srange)) (cdr active)))
15993             (setq srange (cdr srange)))
15994           (if (cdr srange)
15995               ;; Nuke all remaining illegal elements.
15996               (setcdr srange nil))
15997
15998           ;; Adjust the final element.
15999           (if (and (not (atom (car srange)))
16000                    (> (cdar srange) (cdr active)))
16001               (setcdr (car srange) (cdr active))))
16002         ;; Compute the number of unread articles.
16003         (while range
16004           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
16005                                       (cdar range)))
16006                               (or (and (atom (car range)) (car range))
16007                                   (caar range)))))
16008           (setq range (cdr range)))
16009         (setq num (max 0 (- (cdr active) num)))))
16010       ;; Set the number of unread articles.
16011       (when info
16012         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
16013       num)))
16014
16015 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
16016 ;; and compute how many unread articles there are in each group.
16017 (defun gnus-get-unread-articles (&optional level)
16018   (let* ((newsrc (cdr gnus-newsrc-alist))
16019          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
16020          (foreign-level
16021           (min
16022            (cond ((and gnus-activate-foreign-newsgroups
16023                        (not (numberp gnus-activate-foreign-newsgroups)))
16024                   (1+ gnus-level-subscribed))
16025                  ((numberp gnus-activate-foreign-newsgroups)
16026                   gnus-activate-foreign-newsgroups)
16027                  (t 0))
16028            level))
16029          info group active method)
16030     (gnus-message 5 "Checking new news...")
16031
16032     (while newsrc
16033       (setq active (gnus-active (setq group (gnus-info-group
16034                                              (setq info (pop newsrc))))))
16035
16036       ;; Check newsgroups.  If the user doesn't want to check them, or
16037       ;; they can't be checked (for instance, if the news server can't
16038       ;; be reached) we just set the number of unread articles in this
16039       ;; newsgroup to t.  This means that Gnus thinks that there are
16040       ;; unread articles, but it has no idea how many.
16041       (if (and (setq method (gnus-info-method info))
16042                (not (gnus-server-equal
16043                      gnus-select-method
16044                      (setq method (gnus-server-get-method nil method))))
16045                (not (gnus-secondary-method-p method)))
16046           ;; These groups are foreign.  Check the level.
16047           (when (<= (gnus-info-level info) foreign-level)
16048             (setq active (gnus-activate-group group 'scan))
16049             (unless (inline (gnus-virtual-group-p group))
16050               (inline (gnus-close-group group)))
16051             (when (fboundp (intern (concat (symbol-name (car method))
16052                                            "-request-update-info")))
16053               (inline (gnus-request-update-info info method))))
16054         ;; These groups are native or secondary.
16055         (when (and (<= (gnus-info-level info) level)
16056                    (not gnus-read-active-file))
16057           (setq active (gnus-activate-group group 'scan))
16058           (inline (gnus-close-group group))))
16059
16060       ;; Get the number of unread articles in the group.
16061       (if active
16062           (inline (gnus-get-unread-articles-in-group info active))
16063         ;; The group couldn't be reached, so we nix out the number of
16064         ;; unread articles and stuff.
16065         (gnus-set-active group nil)
16066         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
16067
16068     (gnus-message 5 "Checking new news...done")))
16069
16070 ;; Create a hash table out of the newsrc alist.  The `car's of the
16071 ;; alist elements are used as keys.
16072 (defun gnus-make-hashtable-from-newsrc-alist ()
16073   (let ((alist gnus-newsrc-alist)
16074         (ohashtb gnus-newsrc-hashtb)
16075         prev)
16076     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
16077     (setq alist
16078           (setq prev (setq gnus-newsrc-alist
16079                            (if (equal (caar gnus-newsrc-alist)
16080                                       "dummy.group")
16081                                gnus-newsrc-alist
16082                              (cons (list "dummy.group" 0 nil) alist)))))
16083     (while alist
16084       (gnus-sethash
16085        (caar alist)
16086        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
16087              prev)
16088        gnus-newsrc-hashtb)
16089       (setq prev alist
16090             alist (cdr alist)))))
16091
16092 (defun gnus-make-hashtable-from-killed ()
16093   "Create a hash table from the killed and zombie lists."
16094   (let ((lists '(gnus-killed-list gnus-zombie-list))
16095         list)
16096     (setq gnus-killed-hashtb
16097           (gnus-make-hashtable
16098            (+ (length gnus-killed-list) (length gnus-zombie-list))))
16099     (while (setq list (pop lists))
16100       (setq list (symbol-value list))
16101       (while list
16102         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
16103
16104 (defun gnus-activate-group (group &optional scan dont-check method)
16105   ;; Check whether a group has been activated or not.
16106   ;; If SCAN, request a scan of that group as well.
16107   (let ((method (or method (gnus-find-method-for-group group)))
16108         active)
16109     (and (gnus-check-server method)
16110          ;; We escape all bugs and quit here to make it possible to
16111          ;; continue if a group is so out-there that it reports bugs
16112          ;; and stuff.
16113          (progn
16114            (and scan
16115                 (gnus-check-backend-function 'request-scan (car method))
16116                 (gnus-request-scan group method))
16117            t)
16118          (condition-case ()
16119              (gnus-request-group group dont-check method)
16120         ;   (error nil)
16121            (quit nil))
16122          (save-excursion
16123            (set-buffer nntp-server-buffer)
16124            (goto-char (point-min))
16125            ;; Parse the result we got from `gnus-request-group'.
16126            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
16127                 (progn
16128                   (goto-char (match-beginning 1))
16129                   (gnus-set-active
16130                    group (setq active (cons (read (current-buffer))
16131                                             (read (current-buffer)))))
16132                   ;; Return the new active info.
16133                   active))))))
16134
16135 (defun gnus-update-read-articles (group unread)
16136   "Update the list of read and ticked articles in GROUP using the
16137 UNREAD and TICKED lists.
16138 Note: UNSELECTED has to be sorted over `<'.
16139 Returns whether the updating was successful."
16140   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
16141          (entry (gnus-gethash group gnus-newsrc-hashtb))
16142          (info (nth 2 entry))
16143          (prev 1)
16144          (unread (sort (copy-sequence unread) '<))
16145          read)
16146     (if (or (not info) (not active))
16147         ;; There is no info on this group if it was, in fact,
16148         ;; killed.  Gnus stores no information on killed groups, so
16149         ;; there's nothing to be done.
16150         ;; One could store the information somewhere temporarily,
16151         ;; perhaps...  Hmmm...
16152         ()
16153       ;; Remove any negative articles numbers.
16154       (while (and unread (< (car unread) 0))
16155         (setq unread (cdr unread)))
16156       ;; Remove any expired article numbers
16157       (while (and unread (< (car unread) (car active)))
16158         (setq unread (cdr unread)))
16159       ;; Compute the ranges of read articles by looking at the list of
16160       ;; unread articles.
16161       (while unread
16162         (if (/= (car unread) prev)
16163             (setq read (cons (if (= prev (1- (car unread))) prev
16164                                (cons prev (1- (car unread)))) read)))
16165         (setq prev (1+ (car unread)))
16166         (setq unread (cdr unread)))
16167       (when (<= prev (cdr active))
16168         (setq read (cons (cons prev (cdr active)) read)))
16169       ;; Enter this list into the group info.
16170       (gnus-info-set-read
16171        info (if (> (length read) 1) (nreverse read) read))
16172       ;; Set the number of unread articles in gnus-newsrc-hashtb.
16173       (gnus-get-unread-articles-in-group info (gnus-active group))
16174       t)))
16175
16176 (defun gnus-make-articles-unread (group articles)
16177   "Mark ARTICLES in GROUP as unread."
16178   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
16179                           (gnus-gethash (gnus-group-real-name group)
16180                                         gnus-newsrc-hashtb))))
16181          (ranges (gnus-info-read info))
16182          news article)
16183     (while articles
16184       (when (gnus-member-of-range
16185              (setq article (pop articles)) ranges)
16186         (setq news (cons article news))))
16187     (when news
16188       (gnus-info-set-read
16189        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
16190       (gnus-group-update-group group t))))
16191
16192 ;; Enter all dead groups into the hashtb.
16193 (defun gnus-update-active-hashtb-from-killed ()
16194   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
16195         (lists (list gnus-killed-list gnus-zombie-list))
16196         killed)
16197     (while lists
16198       (setq killed (car lists))
16199       (while killed
16200         (gnus-sethash (car killed) nil hashtb)
16201         (setq killed (cdr killed)))
16202       (setq lists (cdr lists)))))
16203
16204 (defun gnus-get-killed-groups ()
16205   "Go through the active hashtb and all all unknown groups as killed."
16206   ;; First make sure active file has been read.
16207   (unless (gnus-read-active-file-p)
16208     (let ((gnus-read-active-file t))
16209       (gnus-read-active-file)))
16210   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
16211   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
16212   (mapatoms
16213    (lambda (sym)
16214      (let ((groups 0)
16215            (group (symbol-name sym)))
16216        (if (or (null group)
16217                (gnus-gethash group gnus-killed-hashtb)
16218                (gnus-gethash group gnus-newsrc-hashtb))
16219            ()
16220          (let ((do-sub (gnus-matches-options-n group)))
16221            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
16222                ()
16223              (setq groups (1+ groups))
16224              (setq gnus-killed-list
16225                    (cons group gnus-killed-list))
16226              (gnus-sethash group group gnus-killed-hashtb))))))
16227    gnus-active-hashtb))
16228
16229 ;; Get the active file(s) from the backend(s).
16230 (defun gnus-read-active-file ()
16231   (gnus-group-set-mode-line)
16232   (let ((methods 
16233          (append
16234           (if (gnus-check-server gnus-select-method)
16235               ;; The native server is available.
16236               (cons gnus-select-method gnus-secondary-select-methods)
16237             ;; The native server is down, so we just do the
16238             ;; secondary ones.
16239             gnus-secondary-select-methods)
16240           ;; Also read from the archive server.
16241           (when (gnus-archive-server-wanted-p)
16242             (list "archive"))))
16243         list-type)
16244     (setq gnus-have-read-active-file nil)
16245     (save-excursion
16246       (set-buffer nntp-server-buffer)
16247       (while methods
16248         (let* ((method (if (stringp (car methods))
16249                            (gnus-server-get-method nil (car methods))
16250                          (car methods)))
16251                (where (nth 1 method))
16252                (mesg (format "Reading active file%s via %s..."
16253                              (if (and where (not (zerop (length where))))
16254                                  (concat " from " where) "")
16255                              (car method))))
16256           (gnus-message 5 mesg)
16257           (when (gnus-check-server method)
16258             ;; Request that the backend scan its incoming messages.
16259             (and (gnus-check-backend-function 'request-scan (car method))
16260                  (gnus-request-scan nil method))
16261             (cond
16262              ((and (eq gnus-read-active-file 'some)
16263                    (gnus-check-backend-function 'retrieve-groups (car method)))
16264               (let ((newsrc (cdr gnus-newsrc-alist))
16265                     (gmethod (gnus-server-get-method nil method))
16266                     groups info)
16267                 (while (setq info (pop newsrc))
16268                   (when (gnus-server-equal
16269                          (gnus-find-method-for-group 
16270                           (gnus-info-group info) info)
16271                          gmethod)
16272                     (push (gnus-group-real-name (gnus-info-group info)) 
16273                           groups)))
16274                 (when groups
16275                   (gnus-check-server method)
16276                   (setq list-type (gnus-retrieve-groups groups method))
16277                   (cond
16278                    ((not list-type)
16279                     (gnus-error
16280                      1.2 "Cannot read partial active file from %s server."
16281                      (car method)))
16282                    ((eq list-type 'active)
16283                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16284                    (t
16285                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16286              (t
16287               (if (not (gnus-request-list method))
16288                   (unless (equal method gnus-message-archive-method)
16289                     (gnus-error 1 "Cannot read active file from %s server."
16290                                 (car method)))
16291                 (gnus-message 5 mesg)
16292                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16293                 ;; We mark this active file as read.
16294                 (push method gnus-have-read-active-file)
16295                 (gnus-message 5 "%sdone" mesg))))))
16296         (setq methods (cdr methods))))))
16297
16298 ;; Read an active file and place the results in `gnus-active-hashtb'.
16299 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16300   (unless method
16301     (setq method gnus-select-method))
16302   (let ((cur (current-buffer))
16303         (hashtb (or hashtb
16304                     (if (and gnus-active-hashtb
16305                              (not (equal method gnus-select-method)))
16306                         gnus-active-hashtb
16307                       (setq gnus-active-hashtb
16308                             (if (equal method gnus-select-method)
16309                                 (gnus-make-hashtable
16310                                  (count-lines (point-min) (point-max)))
16311                               (gnus-make-hashtable 4096)))))))
16312     ;; Delete unnecessary lines.
16313     (goto-char (point-min))
16314     (while (search-forward "\nto." nil t)
16315       (delete-region (1+ (match-beginning 0))
16316                      (progn (forward-line 1) (point))))
16317     (or (string= gnus-ignored-newsgroups "")
16318         (progn
16319           (goto-char (point-min))
16320           (delete-matching-lines gnus-ignored-newsgroups)))
16321     ;; Make the group names readable as a lisp expression even if they
16322     ;; contain special characters.
16323     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16324     (goto-char (point-max))
16325     (while (re-search-backward "[][';?()#]" nil t)
16326       (insert ?\\))
16327     ;; If these are groups from a foreign select method, we insert the
16328     ;; group prefix in front of the group names.
16329     (and method (not (gnus-server-equal
16330                       (gnus-server-get-method nil method)
16331                       (gnus-server-get-method nil gnus-select-method)))
16332          (let ((prefix (gnus-group-prefixed-name "" method)))
16333            (goto-char (point-min))
16334            (while (and (not (eobp))
16335                        (progn (insert prefix)
16336                               (zerop (forward-line 1)))))))
16337     ;; Store the active file in a hash table.
16338     (goto-char (point-min))
16339     (if (string-match "%[oO]" gnus-group-line-format)
16340         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16341         ;; If we want information on moderated groups, we use this
16342         ;; loop...
16343         (let* ((mod-hashtb (make-vector 7 0))
16344                (m (intern "m" mod-hashtb))
16345                group max min)
16346           (while (not (eobp))
16347             (condition-case nil
16348                 (progn
16349                   (narrow-to-region (point) (gnus-point-at-eol))
16350                   (setq group (let ((obarray hashtb)) (read cur)))
16351                   (if (and (numberp (setq max (read cur)))
16352                            (numberp (setq min (read cur)))
16353                            (progn
16354                              (skip-chars-forward " \t")
16355                              (not
16356                               (or (= (following-char) ?=)
16357                                   (= (following-char) ?x)
16358                                   (= (following-char) ?j)))))
16359                       (set group (cons min max))
16360                     (set group nil))
16361                   ;; Enter moderated groups into a list.
16362                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16363                       (setq gnus-moderated-list
16364                             (cons (symbol-name group) gnus-moderated-list))))
16365               (error
16366                (and group
16367                     (symbolp group)
16368                     (set group nil))))
16369             (widen)
16370             (forward-line 1)))
16371       ;; And if we do not care about moderation, we use this loop,
16372       ;; which is faster.
16373       (let (group max min)
16374         (while (not (eobp))
16375           (condition-case ()
16376               (progn
16377                 (narrow-to-region (point) (gnus-point-at-eol))
16378                 ;; group gets set to a symbol interned in the hash table
16379                 ;; (what a hack!!) - jwz
16380                 (setq group (let ((obarray hashtb)) (read cur)))
16381                 (if (and (numberp (setq max (read cur)))
16382                          (numberp (setq min (read cur)))
16383                          (progn
16384                            (skip-chars-forward " \t")
16385                            (not
16386                             (or (= (following-char) ?=)
16387                                 (= (following-char) ?x)
16388                                 (= (following-char) ?j)))))
16389                     (set group (cons min max))
16390                   (set group nil)))
16391             (error
16392              (progn
16393                (and group
16394                     (symbolp group)
16395                     (set group nil))
16396                (or ignore-errors
16397                    (gnus-message 3 "Warning - illegal active: %s"
16398                                  (buffer-substring
16399                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16400           (widen)
16401           (forward-line 1))))))
16402
16403 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16404   ;; Parse a "groups" active file.
16405   (let ((cur (current-buffer))
16406         (hashtb (or hashtb
16407                     (if (and method gnus-active-hashtb)
16408                         gnus-active-hashtb
16409                       (setq gnus-active-hashtb
16410                             (gnus-make-hashtable
16411                              (count-lines (point-min) (point-max)))))))
16412         (prefix (and method
16413                      (not (gnus-server-equal
16414                            (gnus-server-get-method nil method)
16415                            (gnus-server-get-method nil gnus-select-method)))
16416                      (gnus-group-prefixed-name "" method))))
16417
16418     (goto-char (point-min))
16419     ;; We split this into to separate loops, one with the prefix
16420     ;; and one without to speed the reading up somewhat.
16421     (if prefix
16422         (let (min max opoint group)
16423           (while (not (eobp))
16424             (condition-case ()
16425                 (progn
16426                   (read cur) (read cur)
16427                   (setq min (read cur)
16428                         max (read cur)
16429                         opoint (point))
16430                   (skip-chars-forward " \t")
16431                   (insert prefix)
16432                   (goto-char opoint)
16433                   (set (let ((obarray hashtb)) (read cur))
16434                        (cons min max)))
16435               (error (and group (symbolp group) (set group nil))))
16436             (forward-line 1)))
16437       (let (min max group)
16438         (while (not (eobp))
16439           (condition-case ()
16440               (if (= (following-char) ?2)
16441                   (progn
16442                     (read cur) (read cur)
16443                     (setq min (read cur)
16444                           max (read cur))
16445                     (set (setq group (let ((obarray hashtb)) (read cur)))
16446                          (cons min max))))
16447             (error (and group (symbolp group) (set group nil))))
16448           (forward-line 1))))))
16449
16450 (defun gnus-read-newsrc-file (&optional force)
16451   "Read startup file.
16452 If FORCE is non-nil, the .newsrc file is read."
16453   ;; Reset variables that might be defined in the .newsrc.eld file.
16454   (let ((variables gnus-variable-list))
16455     (while variables
16456       (set (car variables) nil)
16457       (setq variables (cdr variables))))
16458   (let* ((newsrc-file gnus-current-startup-file)
16459          (quick-file (concat newsrc-file ".el")))
16460     (save-excursion
16461       ;; We always load the .newsrc.eld file.  If always contains
16462       ;; much information that can not be gotten from the .newsrc
16463       ;; file (ticked articles, killed groups, foreign methods, etc.)
16464       (gnus-read-newsrc-el-file quick-file)
16465
16466       (if (and (file-exists-p gnus-current-startup-file)
16467                (or force
16468                    (and (file-newer-than-file-p newsrc-file quick-file)
16469                         (file-newer-than-file-p newsrc-file
16470                                                 (concat quick-file "d")))
16471                    (not gnus-newsrc-alist)))
16472           ;; We read the .newsrc file.  Note that if there if a
16473           ;; .newsrc.eld file exists, it has already been read, and
16474           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16475           ;; the .newsrc file, Gnus will only use the information it
16476           ;; can find there for changing the data already read -
16477           ;; ie. reading the .newsrc file will not trash the data
16478           ;; already read (except for read articles).
16479           (save-excursion
16480             (gnus-message 5 "Reading %s..." newsrc-file)
16481             (set-buffer (find-file-noselect newsrc-file))
16482             (buffer-disable-undo (current-buffer))
16483             (gnus-newsrc-to-gnus-format)
16484             (kill-buffer (current-buffer))
16485             (gnus-message 5 "Reading %s...done" newsrc-file)))
16486
16487       ;; Read any slave files.
16488       (unless gnus-slave
16489         (gnus-master-read-slave-newsrc))
16490       
16491       ;; Convert old to new.
16492       (gnus-convert-old-newsrc))))
16493
16494 (defun gnus-continuum-version (version)
16495   "Return VERSION as a floating point number."
16496   (when (or (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16497             (string-match "^\\(.?\\)gnus-\\([0-9.]+\\)$" version))
16498     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16499            (number (match-string 2 version))
16500            major minor least)
16501       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16502       (setq major (string-to-number (match-string 1 number)))
16503       (setq minor (string-to-number (match-string 2 number)))
16504       (setq least (if (match-beginning 3)
16505                       (string-to-number (match-string 3 number))
16506                     0))
16507       (string-to-number
16508        (if (zerop major)
16509            (format "%s00%02d%02d"
16510                    (cond 
16511                     ((member alpha '("(ding)" "d")) "4.99")
16512                     ((member alpha '("September" "s")) "5.01")
16513                     ((member alpha '("Red" "r")) "5.03"))
16514                    minor least)
16515          (format "%d.%02d%02d" major minor least))))))
16516
16517 (defun gnus-convert-old-newsrc ()
16518   "Convert old newsrc into the new format, if needed."
16519   (let ((fcv (and gnus-newsrc-file-version
16520                   (gnus-continuum-version gnus-newsrc-file-version))))
16521     (cond
16522      ;; No .newsrc.eld file was loaded.
16523      ((null fcv) nil)
16524      ;; Gnus 5 .newsrc.eld was loaded.
16525      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16526       (gnus-convert-old-ticks)))))
16527
16528 (defun gnus-convert-old-ticks ()
16529   (let ((newsrc (cdr gnus-newsrc-alist))
16530         marks info dormant ticked)
16531     (while (setq info (pop newsrc))
16532       (when (setq marks (gnus-info-marks info))
16533         (setq dormant (cdr (assq 'dormant marks))
16534               ticked (cdr (assq 'tick marks)))
16535         (when (or dormant ticked)
16536           (gnus-info-set-read
16537            info
16538            (gnus-add-to-range
16539             (gnus-info-read info)
16540             (nconc (gnus-uncompress-range dormant)
16541                    (gnus-uncompress-range ticked)))))))))
16542
16543 (defun gnus-read-newsrc-el-file (file)
16544   (let ((ding-file (concat file "d")))
16545     ;; We always, always read the .eld file.
16546     (gnus-message 5 "Reading %s..." ding-file)
16547     (let (gnus-newsrc-assoc)
16548       (condition-case nil
16549           (load ding-file t t t)
16550         (error
16551          (gnus-error 1 "Error in %s" ding-file)))
16552       (when gnus-newsrc-assoc
16553         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16554     (gnus-make-hashtable-from-newsrc-alist)
16555     (when (file-newer-than-file-p file ding-file)
16556       ;; Old format quick file
16557       (gnus-message 5 "Reading %s..." file)
16558       ;; The .el file is newer than the .eld file, so we read that one
16559       ;; as well.
16560       (gnus-read-old-newsrc-el-file file))))
16561
16562 ;; Parse the old-style quick startup file
16563 (defun gnus-read-old-newsrc-el-file (file)
16564   (let (newsrc killed marked group m info)
16565     (prog1
16566         (let ((gnus-killed-assoc nil)
16567               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16568           (prog1
16569               (condition-case nil
16570                   (load file t t t)
16571                 (error nil))
16572             (setq newsrc gnus-newsrc-assoc
16573                   killed gnus-killed-assoc
16574                   marked gnus-marked-assoc)))
16575       (setq gnus-newsrc-alist nil)
16576       (while (setq group (pop newsrc))
16577         (if (setq info (gnus-get-info (car group)))
16578             (progn
16579               (gnus-info-set-read info (cddr group))
16580               (gnus-info-set-level
16581                info (if (nth 1 group) gnus-level-default-subscribed
16582                       gnus-level-default-unsubscribed))
16583               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16584           (push (setq info
16585                       (list (car group)
16586                             (if (nth 1 group) gnus-level-default-subscribed
16587                               gnus-level-default-unsubscribed)
16588                             (cddr group)))
16589                 gnus-newsrc-alist))
16590         ;; Copy marks into info.
16591         (when (setq m (assoc (car group) marked))
16592           (unless (nthcdr 3 info)
16593             (nconc info (list nil)))
16594           (gnus-info-set-marks
16595            info (list (cons 'tick (gnus-compress-sequence 
16596                                    (sort (cdr m) '<) t))))))
16597       (setq newsrc killed)
16598       (while newsrc
16599         (setcar newsrc (caar newsrc))
16600         (setq newsrc (cdr newsrc)))
16601       (setq gnus-killed-list killed))
16602     ;; The .el file version of this variable does not begin with
16603     ;; "options", while the .eld version does, so we just add it if it
16604     ;; isn't there.
16605     (and
16606      gnus-newsrc-options
16607      (progn
16608        (and (not (string-match "^ *options" gnus-newsrc-options))
16609             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16610        (and (not (string-match "\n$" gnus-newsrc-options))
16611             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16612        ;; Finally, if we read some options lines, we parse them.
16613        (or (string= gnus-newsrc-options "")
16614            (gnus-newsrc-parse-options gnus-newsrc-options))))
16615
16616     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16617     (gnus-make-hashtable-from-newsrc-alist)))
16618
16619 (defun gnus-make-newsrc-file (file)
16620   "Make server dependent file name by catenating FILE and server host name."
16621   (let* ((file (expand-file-name file nil))
16622          (real-file (concat file "-" (nth 1 gnus-select-method))))
16623     (if (or (file-exists-p real-file)
16624             (file-exists-p (concat real-file ".el"))
16625             (file-exists-p (concat real-file ".eld")))
16626         real-file file)))
16627
16628 (defun gnus-newsrc-to-gnus-format ()
16629   (setq gnus-newsrc-options "")
16630   (setq gnus-newsrc-options-n nil)
16631
16632   (or gnus-active-hashtb
16633       (setq gnus-active-hashtb (make-vector 4095 0)))
16634   (let ((buf (current-buffer))
16635         (already-read (> (length gnus-newsrc-alist) 1))
16636         group subscribed options-symbol newsrc Options-symbol
16637         symbol reads num1)
16638     (goto-char (point-min))
16639     ;; We intern the symbol `options' in the active hashtb so that we
16640     ;; can `eq' against it later.
16641     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16642     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16643
16644     (while (not (eobp))
16645       ;; We first read the first word on the line by narrowing and
16646       ;; then reading into `gnus-active-hashtb'.  Most groups will
16647       ;; already exist in that hashtb, so this will save some string
16648       ;; space.
16649       (narrow-to-region
16650        (point)
16651        (progn (skip-chars-forward "^ \t!:\n") (point)))
16652       (goto-char (point-min))
16653       (setq symbol
16654             (and (/= (point-min) (point-max))
16655                  (let ((obarray gnus-active-hashtb)) (read buf))))
16656       (widen)
16657       ;; Now, the symbol we have read is either `options' or a group
16658       ;; name.  If it is an options line, we just add it to a string.
16659       (cond
16660        ((or (eq symbol options-symbol)
16661             (eq symbol Options-symbol))
16662         (setq gnus-newsrc-options
16663               ;; This concating is quite inefficient, but since our
16664               ;; thorough studies show that approx 99.37% of all
16665               ;; .newsrc files only contain a single options line, we
16666               ;; don't give a damn, frankly, my dear.
16667               (concat gnus-newsrc-options
16668                       (buffer-substring
16669                        (gnus-point-at-bol)
16670                        ;; Options may continue on the next line.
16671                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16672                                 (progn (beginning-of-line) (point)))
16673                            (point)))))
16674         (forward-line -1))
16675        (symbol
16676         ;; Group names can be just numbers.  
16677         (when (numberp symbol) 
16678           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16679         (or (boundp symbol) (set symbol nil))
16680         ;; It was a group name.
16681         (setq subscribed (= (following-char) ?:)
16682               group (symbol-name symbol)
16683               reads nil)
16684         (if (eolp)
16685             ;; If the line ends here, this is clearly a buggy line, so
16686             ;; we put point a the beginning of line and let the cond
16687             ;; below do the error handling.
16688             (beginning-of-line)
16689           ;; We skip to the beginning of the ranges.
16690           (skip-chars-forward "!: \t"))
16691         ;; We are now at the beginning of the list of read articles.
16692         ;; We read them range by range.
16693         (while
16694             (cond
16695              ((looking-at "[0-9]+")
16696               ;; We narrow and read a number instead of buffer-substring/
16697               ;; string-to-int because it's faster.  narrow/widen is
16698               ;; faster than save-restriction/narrow, and save-restriction
16699               ;; produces a garbage object.
16700               (setq num1 (progn
16701                            (narrow-to-region (match-beginning 0) (match-end 0))
16702                            (read buf)))
16703               (widen)
16704               ;; If the next character is a dash, then this is a range.
16705               (if (= (following-char) ?-)
16706                   (progn
16707                     ;; We read the upper bound of the range.
16708                     (forward-char 1)
16709                     (if (not (looking-at "[0-9]+"))
16710                         ;; This is a buggy line, by we pretend that
16711                         ;; it's kinda OK.  Perhaps the user should be
16712                         ;; dinged?
16713                         (setq reads (cons num1 reads))
16714                       (setq reads
16715                             (cons
16716                              (cons num1
16717                                    (progn
16718                                      (narrow-to-region (match-beginning 0)
16719                                                        (match-end 0))
16720                                      (read buf)))
16721                              reads))
16722                       (widen)))
16723                 ;; It was just a simple number, so we add it to the
16724                 ;; list of ranges.
16725                 (setq reads (cons num1 reads)))
16726               ;; If the next char in ?\n, then we have reached the end
16727               ;; of the line and return nil.
16728               (/= (following-char) ?\n))
16729              ((= (following-char) ?\n)
16730               ;; End of line, so we end.
16731               nil)
16732              (t
16733               ;; Not numbers and not eol, so this might be a buggy
16734               ;; line...
16735               (or (eobp)
16736                   ;; If it was eob instead of ?\n, we allow it.
16737                   (progn
16738                     ;; The line was buggy.
16739                     (setq group nil)
16740                     (gnus-error 3.1 "Mangled line: %s"
16741                                 (buffer-substring (gnus-point-at-bol)
16742                                                   (gnus-point-at-eol)))))
16743               nil))
16744           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16745           ;; we allow them, because it's a common mistake to put a
16746           ;; space after the comma.
16747           (skip-chars-forward ", "))
16748
16749         ;; We have already read .newsrc.eld, so we gently update the
16750         ;; data in the hash table with the information we have just
16751         ;; read.
16752         (when group
16753           (let ((info (gnus-get-info group))
16754                 level)
16755             (if info
16756                 ;; There is an entry for this file in the alist.
16757                 (progn
16758                   (gnus-info-set-read info (nreverse reads))
16759                   ;; We update the level very gently.  In fact, we
16760                   ;; only change it if there's been a status change
16761                   ;; from subscribed to unsubscribed, or vice versa.
16762                   (setq level (gnus-info-level info))
16763                   (cond ((and (<= level gnus-level-subscribed)
16764                               (not subscribed))
16765                          (setq level (if reads
16766                                          gnus-level-default-unsubscribed
16767                                        (1+ gnus-level-default-unsubscribed))))
16768                         ((and (> level gnus-level-subscribed) subscribed)
16769                          (setq level gnus-level-default-subscribed)))
16770                   (gnus-info-set-level info level))
16771               ;; This is a new group.
16772               (setq info (list group
16773                                (if subscribed
16774                                    gnus-level-default-subscribed
16775                                  (if reads
16776                                      (1+ gnus-level-subscribed)
16777                                    gnus-level-default-unsubscribed))
16778                                (nreverse reads))))
16779             (setq newsrc (cons info newsrc))))))
16780       (forward-line 1))
16781
16782     (setq newsrc (nreverse newsrc))
16783
16784     (if (not already-read)
16785         ()
16786       ;; We now have two newsrc lists - `newsrc', which is what we
16787       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16788       ;; what we've read from .newsrc.eld.  We have to merge these
16789       ;; lists.  We do this by "attaching" any (foreign) groups in the
16790       ;; gnus-newsrc-alist to the (native) group that precedes them.
16791       (let ((rc (cdr gnus-newsrc-alist))
16792             (prev gnus-newsrc-alist)
16793             entry mentry)
16794         (while rc
16795           (or (null (nth 4 (car rc)))   ; It's a native group.
16796               (assoc (caar rc) newsrc) ; It's already in the alist.
16797               (if (setq entry (assoc (caar prev) newsrc))
16798                   (setcdr (setq mentry (memq entry newsrc))
16799                           (cons (car rc) (cdr mentry)))
16800                 (setq newsrc (cons (car rc) newsrc))))
16801           (setq prev rc
16802                 rc (cdr rc)))))
16803
16804     (setq gnus-newsrc-alist newsrc)
16805     ;; We make the newsrc hashtb.
16806     (gnus-make-hashtable-from-newsrc-alist)
16807
16808     ;; Finally, if we read some options lines, we parse them.
16809     (or (string= gnus-newsrc-options "")
16810         (gnus-newsrc-parse-options gnus-newsrc-options))))
16811
16812 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16813 ;; The return value will be a list on the form
16814 ;; ((regexp1 . ignore)
16815 ;;  (regexp2 . subscribe)...)
16816 ;; When handling new newsgroups, groups that match a `ignore' regexp
16817 ;; will be ignored, and groups that match a `subscribe' regexp will be
16818 ;; subscribed.  A line like
16819 ;; options -n !all rec.all
16820 ;; will lead to a list that looks like
16821 ;; (("^rec\\..+" . subscribe)
16822 ;;  ("^.+" . ignore))
16823 ;; So all "rec.*" groups will be subscribed, while all the other
16824 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16825 ;; different from "options -n rec.all !all".
16826 (defun gnus-newsrc-parse-options (options)
16827   (let (out eol)
16828     (save-excursion
16829       (gnus-set-work-buffer)
16830       (insert (regexp-quote options))
16831       ;; First we treat all continuation lines.
16832       (goto-char (point-min))
16833       (while (re-search-forward "\n[ \t]+" nil t)
16834         (replace-match " " t t))
16835       ;; Then we transform all "all"s into ".+"s.
16836       (goto-char (point-min))
16837       (while (re-search-forward "\\ball\\b" nil t)
16838         (replace-match ".+" t t))
16839       (goto-char (point-min))
16840       ;; We remove all other options than the "-n" ones.
16841       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16842         (replace-match " ")
16843         (forward-char -1))
16844       (goto-char (point-min))
16845
16846       ;; We are only interested in "options -n" lines - we
16847       ;; ignore the other option lines.
16848       (while (re-search-forward "[ \t]-n" nil t)
16849         (setq eol
16850               (or (save-excursion
16851                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16852                          (- (point) 2)))
16853                   (gnus-point-at-eol)))
16854         ;; Search for all "words"...
16855         (while (re-search-forward "[^ \t,\n]+" eol t)
16856           (if (= (char-after (match-beginning 0)) ?!)
16857               ;; If the word begins with a bang (!), this is a "not"
16858               ;; spec.  We put this spec (minus the bang) and the
16859               ;; symbol `ignore' into the list.
16860               (setq out (cons (cons (concat
16861                                      "^" (buffer-substring
16862                                           (1+ (match-beginning 0))
16863                                           (match-end 0)))
16864                                     'ignore) out))
16865             ;; There was no bang, so this is a "yes" spec.
16866             (setq out (cons (cons (concat "^" (match-string 0))
16867                                   'subscribe) out)))))
16868
16869       (setq gnus-newsrc-options-n out))))
16870
16871 (defun gnus-save-newsrc-file (&optional force)
16872   "Save .newsrc file."
16873   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16874   ;; from the variable gnus-newsrc-alist.
16875   (when (and (or gnus-newsrc-alist gnus-killed-list)
16876              gnus-current-startup-file)
16877     (save-excursion
16878       (if (and (or gnus-use-dribble-file gnus-slave)
16879                (not force)
16880                (or (not gnus-dribble-buffer)
16881                    (not (buffer-name gnus-dribble-buffer))
16882                    (zerop (save-excursion
16883                             (set-buffer gnus-dribble-buffer)
16884                             (buffer-size)))))
16885           (gnus-message 4 "(No changes need to be saved)")
16886         (run-hooks 'gnus-save-newsrc-hook)
16887         (if gnus-slave
16888             (gnus-slave-save-newsrc)
16889           ;; Save .newsrc.
16890           (when gnus-save-newsrc-file
16891             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16892             (gnus-gnus-to-newsrc-format)
16893             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16894           ;; Save .newsrc.eld.
16895           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16896           (make-local-variable 'version-control)
16897           (setq version-control 'never)
16898           (setq buffer-file-name
16899                 (concat gnus-current-startup-file ".eld"))
16900           (setq default-directory (file-name-directory buffer-file-name))
16901           (gnus-add-current-to-buffer-list)
16902           (buffer-disable-undo (current-buffer))
16903           (erase-buffer)
16904           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16905           (gnus-gnus-to-quick-newsrc-format)
16906           (run-hooks 'gnus-save-quick-newsrc-hook)
16907           (save-buffer)
16908           (kill-buffer (current-buffer))
16909           (gnus-message
16910            5 "Saving %s.eld...done" gnus-current-startup-file))
16911         (gnus-dribble-delete-file)
16912         (gnus-group-set-mode-line)))))
16913
16914 (defun gnus-gnus-to-quick-newsrc-format ()
16915   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16916   (insert ";; Gnus startup file.\n")
16917   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16918   (insert ";; to read .newsrc.\n")
16919   (insert "(setq gnus-newsrc-file-version "
16920           (prin1-to-string gnus-version) ")\n")
16921   (let ((variables
16922          (if gnus-save-killed-list gnus-variable-list
16923            ;; Remove the `gnus-killed-list' from the list of variables
16924            ;; to be saved, if required.
16925            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16926         ;; Peel off the "dummy" group.
16927         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16928         variable)
16929     ;; Insert the variables into the file.
16930     (while variables
16931       (when (and (boundp (setq variable (pop variables)))
16932                  (symbol-value variable))
16933         (insert "(setq " (symbol-name variable) " '")
16934         (prin1 (symbol-value variable) (current-buffer))
16935         (insert ")\n")))))
16936
16937 (defun gnus-gnus-to-newsrc-format ()
16938   ;; Generate and save the .newsrc file.
16939   (save-excursion
16940     (set-buffer (create-file-buffer gnus-current-startup-file))
16941     (let ((newsrc (cdr gnus-newsrc-alist))
16942           (standard-output (current-buffer))
16943           info ranges range method)
16944       (setq buffer-file-name gnus-current-startup-file)
16945       (setq default-directory (file-name-directory buffer-file-name))
16946       (buffer-disable-undo (current-buffer))
16947       (erase-buffer)
16948       ;; Write options.
16949       (if gnus-newsrc-options (insert gnus-newsrc-options))
16950       ;; Write subscribed and unsubscribed.
16951       (while (setq info (pop newsrc))
16952         ;; Don't write foreign groups to .newsrc.
16953         (when (or (null (setq method (gnus-info-method info)))
16954                   (equal method "native")
16955                   (gnus-server-equal method gnus-select-method))
16956           (insert (gnus-info-group info)
16957                   (if (> (gnus-info-level info) gnus-level-subscribed)
16958                       "!" ":"))
16959           (when (setq ranges (gnus-info-read info))
16960             (insert " ")
16961             (if (not (listp (cdr ranges)))
16962                 (if (= (car ranges) (cdr ranges))
16963                     (princ (car ranges))
16964                   (princ (car ranges))
16965                   (insert "-")
16966                   (princ (cdr ranges)))
16967               (while (setq range (pop ranges))
16968                 (if (or (atom range) (= (car range) (cdr range)))
16969                     (princ (or (and (atom range) range) (car range)))
16970                   (princ (car range))
16971                   (insert "-")
16972                   (princ (cdr range)))
16973                 (if ranges (insert ",")))))
16974           (insert "\n")))
16975       (make-local-variable 'version-control)
16976       (setq version-control 'never)
16977       ;; It has been reported that sometime the modtime on the .newsrc
16978       ;; file seems to be off.  We really do want to overwrite it, so
16979       ;; we clear the modtime here before saving.  It's a bit odd,
16980       ;; though...
16981       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16982       ;; delete the silly thing entirely first.  but this fails to provide
16983       ;; such niceties as .newsrc~ creation.
16984       (if gnus-modtime-botch
16985           (delete-file gnus-startup-file)
16986         (clear-visited-file-modtime))
16987       (run-hooks 'gnus-save-standard-newsrc-hook)
16988       (save-buffer)
16989       (kill-buffer (current-buffer)))))
16990
16991 \f
16992 ;;;
16993 ;;; Slave functions.
16994 ;;;
16995
16996 (defun gnus-slave-save-newsrc ()
16997   (save-excursion
16998     (set-buffer gnus-dribble-buffer)
16999     (let ((slave-name
17000            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
17001       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
17002
17003 (defun gnus-master-read-slave-newsrc ()
17004   (let ((slave-files
17005          (directory-files
17006           (file-name-directory gnus-current-startup-file)
17007           t (concat
17008              "^" (regexp-quote
17009                   (concat
17010                    (file-name-nondirectory gnus-current-startup-file)
17011                    "-slave-")))
17012           t))
17013         file)
17014     (if (not slave-files)
17015         ()                              ; There are no slave files to read.
17016       (gnus-message 7 "Reading slave newsrcs...")
17017       (save-excursion
17018         (set-buffer (get-buffer-create " *gnus slave*"))
17019         (buffer-disable-undo (current-buffer))
17020         (setq slave-files
17021               (sort (mapcar (lambda (file)
17022                               (list (nth 5 (file-attributes file)) file))
17023                             slave-files)
17024                     (lambda (f1 f2)
17025                       (or (< (caar f1) (caar f2))
17026                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
17027         (while slave-files
17028           (erase-buffer)
17029           (setq file (nth 1 (car slave-files)))
17030           (insert-file-contents file)
17031           (if (condition-case ()
17032                   (progn
17033                     (eval-buffer (current-buffer))
17034                     t)
17035                 (error
17036                  (gnus-error 3.2 "Possible error in %s" file)
17037                  nil))
17038               (or gnus-slave ; Slaves shouldn't delete these files.
17039                   (condition-case ()
17040                       (delete-file file)
17041                     (error nil))))
17042           (setq slave-files (cdr slave-files))))
17043       (gnus-message 7 "Reading slave newsrcs...done"))))
17044
17045 \f
17046 ;;;
17047 ;;; Group description.
17048 ;;;
17049
17050 (defun gnus-read-all-descriptions-files ()
17051   (let ((methods (cons gnus-select-method 
17052                        (nconc
17053                         (when (gnus-archive-server-wanted-p)
17054                           (list "archive"))
17055                         gnus-secondary-select-methods))))
17056     (while methods
17057       (gnus-read-descriptions-file (car methods))
17058       (setq methods (cdr methods)))
17059     t))
17060
17061 (defun gnus-read-descriptions-file (&optional method)
17062   (let ((method (or method gnus-select-method))
17063         group)
17064     (when (stringp method)
17065       (setq method (gnus-server-to-method method)))
17066     ;; We create the hashtable whether we manage to read the desc file
17067     ;; to avoid trying to re-read after a failed read.
17068     (or gnus-description-hashtb
17069         (setq gnus-description-hashtb
17070               (gnus-make-hashtable (length gnus-active-hashtb))))
17071     ;; Mark this method's desc file as read.
17072     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
17073                   gnus-description-hashtb)
17074
17075     (gnus-message 5 "Reading descriptions file via %s..." (car method))
17076     (cond
17077      ((not (gnus-check-server method))
17078       (gnus-message 1 "Couldn't open server")
17079       nil)
17080      ((not (gnus-request-list-newsgroups method))
17081       (gnus-message 1 "Couldn't read newsgroups descriptions")
17082       nil)
17083      (t
17084       (save-excursion
17085         (save-restriction
17086           (set-buffer nntp-server-buffer)
17087           (goto-char (point-min))
17088           (when (or (search-forward "\n.\n" nil t)
17089                     (goto-char (point-max)))
17090             (beginning-of-line)
17091             (narrow-to-region (point-min) (point)))
17092           ;; If these are groups from a foreign select method, we insert the
17093           ;; group prefix in front of the group names.
17094           (and method (not (gnus-server-equal
17095                             (gnus-server-get-method nil method)
17096                             (gnus-server-get-method nil gnus-select-method)))
17097                (let ((prefix (gnus-group-prefixed-name "" method)))
17098                  (goto-char (point-min))
17099                  (while (and (not (eobp))
17100                              (progn (insert prefix)
17101                                     (zerop (forward-line 1)))))))
17102           (goto-char (point-min))
17103           (while (not (eobp))
17104             ;; If we get an error, we set group to 0, which is not a
17105             ;; symbol...
17106             (setq group
17107                   (condition-case ()
17108                       (let ((obarray gnus-description-hashtb))
17109                         ;; Group is set to a symbol interned in this
17110                         ;; hash table.
17111                         (read nntp-server-buffer))
17112                     (error 0)))
17113             (skip-chars-forward " \t")
17114             ;; ...  which leads to this line being effectively ignored.
17115             (and (symbolp group)
17116                  (set group (buffer-substring
17117                              (point) (progn (end-of-line) (point)))))
17118             (forward-line 1))))
17119       (gnus-message 5 "Reading descriptions file...done")
17120       t))))
17121
17122 (defun gnus-group-get-description (group)
17123   "Get the description of a group by sending XGTITLE to the server."
17124   (when (gnus-request-group-description group)
17125     (save-excursion
17126       (set-buffer nntp-server-buffer)
17127       (goto-char (point-min))
17128       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
17129         (match-string 1)))))
17130
17131 \f
17132 ;;;
17133 ;;; Buffering of read articles.
17134 ;;;
17135
17136 (defvar gnus-backlog-buffer " *Gnus Backlog*")
17137 (defvar gnus-backlog-articles nil)
17138 (defvar gnus-backlog-hashtb nil)
17139
17140 (defun gnus-backlog-buffer ()
17141   "Return the backlog buffer."
17142   (or (get-buffer gnus-backlog-buffer)
17143       (save-excursion
17144         (set-buffer (get-buffer-create gnus-backlog-buffer))
17145         (buffer-disable-undo (current-buffer))
17146         (setq buffer-read-only t)
17147         (gnus-add-current-to-buffer-list)
17148         (get-buffer gnus-backlog-buffer))))
17149
17150 (defun gnus-backlog-setup ()
17151   "Initialize backlog variables."
17152   (unless gnus-backlog-hashtb
17153     (setq gnus-backlog-hashtb (make-vector 1023 0))))
17154
17155 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
17156
17157 (defun gnus-backlog-shutdown ()
17158   "Clear all backlog variables and buffers."
17159   (when (get-buffer gnus-backlog-buffer)
17160     (kill-buffer gnus-backlog-buffer))
17161   (setq gnus-backlog-hashtb nil
17162         gnus-backlog-articles nil))
17163
17164 (defun gnus-backlog-enter-article (group number buffer)
17165   (gnus-backlog-setup)
17166   (let ((ident (intern (concat group ":" (int-to-string number))
17167                        gnus-backlog-hashtb))
17168         b)
17169     (if (memq ident gnus-backlog-articles)
17170         () ; It's already kept.
17171       ;; Remove the oldest article, if necessary.
17172       (and (numberp gnus-keep-backlog)
17173            (>= (length gnus-backlog-articles) gnus-keep-backlog)
17174            (gnus-backlog-remove-oldest-article))
17175       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
17176       ;; Insert the new article.
17177       (save-excursion
17178         (set-buffer (gnus-backlog-buffer))
17179         (let (buffer-read-only)
17180           (goto-char (point-max))
17181           (or (bolp) (insert "\n"))
17182           (setq b (point))
17183           (insert-buffer-substring buffer)
17184           ;; Tag the beginning of the article with the ident.
17185           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
17186
17187 (defun gnus-backlog-remove-oldest-article ()
17188   (save-excursion
17189     (set-buffer (gnus-backlog-buffer))
17190     (goto-char (point-min))
17191     (if (zerop (buffer-size))
17192         () ; The buffer is empty.
17193       (let ((ident (get-text-property (point) 'gnus-backlog))
17194             buffer-read-only)
17195         ;; Remove the ident from the list of articles.
17196         (when ident
17197           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17198         ;; Delete the article itself.
17199         (delete-region
17200          (point) (next-single-property-change
17201                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
17202
17203 (defun gnus-backlog-remove-article (group number)
17204   "Remove article NUMBER in GROUP from the backlog."
17205   (when (numberp number)
17206     (gnus-backlog-setup)
17207     (let ((ident (intern (concat group ":" (int-to-string number))
17208                          gnus-backlog-hashtb))
17209           beg end)
17210       (when (memq ident gnus-backlog-articles)
17211         ;; It was in the backlog.
17212         (save-excursion
17213           (set-buffer (gnus-backlog-buffer))
17214           (let (buffer-read-only)
17215             (when (setq beg (text-property-any
17216                              (point-min) (point-max) 'gnus-backlog
17217                              ident))
17218               ;; Find the end (i. e., the beginning of the next article).
17219               (setq end
17220                     (next-single-property-change
17221                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
17222               (delete-region beg end)
17223               ;; Return success.
17224               t)))))))
17225
17226 (defun gnus-backlog-request-article (group number buffer)
17227   (when (numberp number)
17228     (gnus-backlog-setup)
17229     (let ((ident (intern (concat group ":" (int-to-string number))
17230                          gnus-backlog-hashtb))
17231           beg end)
17232       (when (memq ident gnus-backlog-articles)
17233         ;; It was in the backlog.
17234         (save-excursion
17235           (set-buffer (gnus-backlog-buffer))
17236           (if (not (setq beg (text-property-any
17237                               (point-min) (point-max) 'gnus-backlog
17238                               ident)))
17239               ;; It wasn't in the backlog after all.
17240               (ignore
17241                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17242             ;; Find the end (i. e., the beginning of the next article).
17243             (setq end
17244                   (next-single-property-change
17245                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
17246         (let ((buffer-read-only nil))
17247           (erase-buffer)
17248           (insert-buffer-substring gnus-backlog-buffer beg end)
17249           t)))))
17250
17251 ;; Allow redefinition of Gnus functions.
17252
17253 (gnus-ems-redefine)
17254
17255 (provide 'gnus)
17256
17257 ;;; gnus.el ends here