*** 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 'message)
35 (require 'nnmail)
36 (require 'backquote)
37 (require 'nnoo)
38
39 (eval-when-compile (require 'cl))
40
41 (defvar gnus-directory (or (getenv "SAVEDIR") "~/News/")
42   "*Directory variable from which all other Gnus file variables are derived.")
43
44 ;; Site dependent variables.  These variables should be defined in
45 ;; paths.el.
46
47 (defvar gnus-default-nntp-server nil
48   "Specify a default NNTP server.
49 This variable should be defined in paths.el, and should never be set
50 by the user.
51 If you want to change servers, you should use `gnus-select-method'.
52 See the documentation to that variable.")
53
54 (defvar gnus-backup-default-subscribed-newsgroups
55   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
56   "Default default new newsgroups the first time Gnus is run.
57 Should be set in paths.el, and shouldn't be touched by the user.")
58
59 (defvar gnus-local-domain nil
60   "Local domain name without a host name.
61 The DOMAINNAME environment variable is used instead if it is defined.
62 If the `system-name' function returns the full Internet name, there is
63 no need to set this variable.")
64
65 (defvar gnus-local-organization nil
66   "String with a description of what organization (if any) the user belongs to.
67 The ORGANIZATION environment variable is used instead if it is defined.
68 If this variable contains a function, this function will be called
69 with the current newsgroup name as the argument.  The function should
70 return a string.
71
72 In any case, if the string (either in the variable, in the environment
73 variable, or returned by the function) is a file name, the contents of
74 this file will be used as the organization.")
75
76 ;; Customization variables
77
78 ;; Don't touch this variable.
79 (defvar gnus-nntp-service "nntp"
80   "*NNTP service name (\"nntp\" or 119).
81 This is an obsolete variable, which is scarcely used.  If you use an
82 nntp server for your newsgroup and want to change the port number
83 used to 899, you would say something along these lines:
84
85  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
86
87 (defvar gnus-nntpserver-file "/etc/nntpserver"
88   "*A file with only the name of the nntp server in it.")
89
90 ;; This function is used to check both the environment variable
91 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
92 ;; an nntp server name default.
93 (defun gnus-getenv-nntpserver ()
94   (or (getenv "NNTPSERVER")
95       (and (file-readable-p gnus-nntpserver-file)
96            (save-excursion
97              (set-buffer (get-buffer-create " *gnus nntp*"))
98              (buffer-disable-undo (current-buffer))
99              (insert-file-contents gnus-nntpserver-file)
100              (let ((name (buffer-string)))
101                (prog1
102                    (if (string-match "^[ \t\n]*$" name)
103                        nil
104                      name)
105                  (kill-buffer (current-buffer))))))))
106
107 (defvar gnus-select-method
108   (nconc
109    (list 'nntp (or (condition-case ()
110                        (gnus-getenv-nntpserver)
111                      (error nil))
112                    (if (and gnus-default-nntp-server
113                             (not (string= gnus-default-nntp-server "")))
114                        gnus-default-nntp-server)
115                    (system-name)))
116    (if (or (null gnus-nntp-service)
117            (equal gnus-nntp-service "nntp"))
118        nil
119      (list gnus-nntp-service)))
120   "*Default method for selecting a newsgroup.
121 This variable should be a list, where the first element is how the
122 news is to be fetched, the second is the address.
123
124 For instance, if you want to get your news via NNTP from
125 \"flab.flab.edu\", you could say:
126
127 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
128
129 If you want to use your local spool, say:
130
131 (setq gnus-select-method (list 'nnspool (system-name)))
132
133 If you use this variable, you must set `gnus-nntp-server' to nil.
134
135 There is a lot more to know about select methods and virtual servers -
136 see the manual for details.")
137
138 (defvar gnus-message-archive-method 
139   `(nnfolder
140     "archive"
141     (nnfolder-directory ,(nnheader-concat message-directory "archive"))
142     (nnfolder-active-file 
143      ,(nnheader-concat message-directory "archive/active"))
144     (nnfolder-get-new-mail nil)
145     (nnfolder-inhibit-expiry t))
146   "*Method used for archiving messages you've sent.
147 This should be a mail method.
148
149 It's probably not a very effective to change this variable once you've
150 run Gnus once.  After doing that, you must edit this server from the
151 server buffer.")
152
153 (defvar gnus-message-archive-group nil
154   "*Name of the group in which to save the messages you've written.
155 This can either be a string, a list of strings; or an alist
156 of regexps/functions/forms to be evaluated to return a string (or a list
157 of strings).  The functions are called with the name of the current
158 group (or nil) as a parameter.
159
160 Normally the group names returned by this variable should be
161 unprefixed -- which implictly means \"store on the archive server\".
162 However, you may wish to store the message on some other server.  In
163 that case, just return a fully prefixed name of the group --
164 \"nnml+private:mail.misc\", for instance.")
165
166 (defvar gnus-refer-article-method nil
167   "*Preferred method for fetching an article by Message-ID.
168 If you are reading news from the local spool (with nnspool), fetching
169 articles by Message-ID is painfully slow.  By setting this method to an
170 nntp method, you might get acceptable results.
171
172 The value of this variable must be a valid select method as discussed
173 in the documentation of `gnus-select-method'.")
174
175 (defvar gnus-secondary-select-methods nil
176   "*A list of secondary methods that will be used for reading news.
177 This is a list where each element is a complete select method (see
178 `gnus-select-method').
179
180 If, for instance, you want to read your mail with the nnml backend,
181 you could set this variable:
182
183 (setq gnus-secondary-select-methods '((nnml \"\")))")
184
185 (defvar gnus-secondary-servers nil
186   "*List of NNTP servers that the user can choose between interactively.
187 To make Gnus query you for a server, you have to give `gnus' a
188 non-numeric prefix - `C-u M-x gnus', in short.")
189
190 (defvar gnus-nntp-server nil
191   "*The name of the host running the NNTP server.
192 This variable is semi-obsolete.  Use the `gnus-select-method'
193 variable instead.")
194
195 (defvar gnus-startup-file "~/.newsrc"
196   "*Your `.newsrc' file.
197 `.newsrc-SERVER' will be used instead if that exists.")
198
199 (defvar gnus-init-file "~/.gnus"
200   "*Your Gnus elisp startup file.
201 If a file with the .el or .elc suffixes exist, it will be read
202 instead.")
203
204 (defvar gnus-group-faq-directory
205   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
206     "/ftp@sunsite.auc.dk:/pub/usenet/"
207     "/ftp@sunsite.doc.ic.ac.uk:/pub/usenet/news-faqs/"
208     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
209     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
210     "/ftp@rtfm.mit.edu:/pub/usenet/"
211     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
212     "/ftp@ftp.sunet.se:/pub/usenet/"
213     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
214     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
215     "/ftp@ftp.hk.super.net:/mirror/faqs/")
216   "*Directory where the group FAQs are stored.
217 This will most commonly be on a remote machine, and the file will be
218 fetched by ange-ftp.
219
220 This variable can also be a list of directories.  In that case, the
221 first element in the list will be used by default, and the others will
222 be used as backup sites.
223
224 Note that Gnus uses an aol machine as the default directory.  If this
225 feels fundamentally unclean, just think of it as a way to finally get
226 something of value back from them.
227
228 If the default site is too slow, try one of these:
229
230    North America: mirrors.aol.com                /pub/rtfm/usenet
231                   ftp.seas.gwu.edu               /pub/rtfm
232                   rtfm.mit.edu                   /pub/usenet
233    Europe:        ftp.uni-paderborn.de           /pub/FAQ
234                   src.doc.ic.ac.uk               /usenet/news-FAQS
235                   ftp.sunet.se                   /pub/usenet
236                   sunsite.auc.dk                 /pub/usenet
237    Asia:          nctuccca.edu.tw                /USENET/FAQ
238                   hwarang.postech.ac.kr          /pub/usenet
239                   ftp.hk.super.net               /mirror/faqs")
240
241 (defvar gnus-group-archive-directory
242   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
243   "*The address of the (ding) archives.")
244
245 (defvar gnus-group-recent-archive-directory
246   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
247   "*The address of the most recent (ding) articles.")
248
249 (defvar gnus-default-subscribed-newsgroups nil
250   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
251 It should be a list of strings.
252 If it is `t', Gnus will not do anything special the first time it is
253 started; it'll just use the normal newsgroups subscription methods.")
254
255 (defvar gnus-use-cross-reference t
256   "*Non-nil means that cross referenced articles will be marked as read.
257 If nil, ignore cross references.  If t, mark articles as read in
258 subscribed newsgroups.  If neither t nor nil, mark as read in all
259 newsgroups.")
260
261 (defvar gnus-single-article-buffer t
262   "*If non-nil, display all articles in the same buffer.
263 If nil, each group will get its own article buffer.")
264
265 (defvar gnus-use-dribble-file t
266   "*Non-nil means that Gnus will use a dribble file to store user updates.
267 If Emacs should crash without saving the .newsrc files, complete
268 information can be restored from the dribble file.")
269
270 (defvar gnus-dribble-directory nil
271   "*The directory where dribble files will be saved.
272 If this variable is nil, the directory where the .newsrc files are
273 saved will be used.")
274
275 (defvar gnus-asynchronous nil
276   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
277
278 (defvar gnus-kill-summary-on-exit t
279   "*If non-nil, kill the summary buffer when you exit from it.
280 If nil, the summary will become a \"*Dead Summary*\" buffer, and
281 it will be killed sometime later.")
282
283 (defvar gnus-large-newsgroup 200
284   "*The number of articles which indicates a large newsgroup.
285 If the number of articles in a newsgroup is greater than this value,
286 confirmation is required for selecting the newsgroup.")
287
288 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
289 (defvar gnus-no-groups-message "No news is horrible news"
290   "*Message displayed by Gnus when no groups are available.")
291
292 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
293   "*Non-nil means that the default name of a file to save articles in is the group name.
294 If it's nil, the directory form of the group name is used instead.
295
296 If this variable is a list, and the list contains the element
297 `not-score', long file names will not be used for score files; if it
298 contains the element `not-save', long file names will not be used for
299 saving; and if it contains the element `not-kill', long file names
300 will not be used for kill files.
301
302 Note that the default for this variable varies according to what system
303 type you're using.  On `usg-unix-v' and `xenix' this variable defaults
304 to nil while on all other systems it defaults to t.")
305
306 (defvar gnus-article-save-directory gnus-directory
307   "*Name of the directory articles will be saved in (default \"~/News\").")
308
309 (defvar gnus-kill-files-directory gnus-directory
310   "*Name of the directory where kill files will be stored (default \"~/News\").")
311
312 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
313   "*A function to save articles in your favorite format.
314 The function must be interactively callable (in other words, it must
315 be an Emacs command).
316
317 Gnus provides the following functions:
318
319 * gnus-summary-save-in-rmail (Rmail format)
320 * gnus-summary-save-in-mail (Unix mail format)
321 * gnus-summary-save-in-folder (MH folder)
322 * gnus-summary-save-in-file (article format).
323 * gnus-summary-save-in-vm (use VM's folder format).")
324
325 (defvar gnus-prompt-before-saving 'always
326   "*This variable says how much prompting is to be done when saving articles.
327 If it is nil, no prompting will be done, and the articles will be
328 saved to the default files.  If this variable is `always', each and
329 every article that is saved will be preceded by a prompt, even when
330 saving large batches of articles.  If this variable is neither nil not
331 `always', there the user will be prompted once for a file name for
332 each invocation of the saving commands.")
333
334 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
335   "*A function generating a file name to save articles in Rmail format.
336 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
337
338 (defvar gnus-mail-save-name (function gnus-plain-save-name)
339   "*A function generating a file name to save articles in Unix mail format.
340 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
341
342 (defvar gnus-folder-save-name (function gnus-folder-save-name)
343   "*A function generating a file name to save articles in MH folder.
344 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
345
346 (defvar gnus-file-save-name (function gnus-numeric-save-name)
347   "*A function generating a file name to save articles in article format.
348 The function is called with NEWSGROUP, HEADERS, and optional
349 LAST-FILE.")
350
351 (defvar gnus-split-methods
352   '((gnus-article-archive-name))
353   "*Variable used to suggest where articles are to be saved.
354 For instance, if you would like to save articles related to Gnus in
355 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
356 you could set this variable to something like:
357
358  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
359    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
360
361 This variable is an alist where the where the key is the match and the
362 value is a list of possible files to save in if the match is non-nil.
363
364 If the match is a string, it is used as a regexp match on the
365 article.  If the match is a symbol, that symbol will be funcalled
366 from the buffer of the article to be saved with the newsgroup as the
367 parameter.  If it is a list, it will be evaled in the same buffer.
368
369 If this form or function returns a string, this string will be used as
370 a possible file name; and if it returns a non-nil list, that list will
371 be used as possible file names.")
372
373 (defvar gnus-move-split-methods nil
374   "*Variable used to suggest where articles are to be moved to.
375 It uses the same syntax as the `gnus-split-methods' variable.")
376
377 (defvar gnus-save-score nil
378   "*If non-nil, save group scoring info.")
379
380 (defvar gnus-use-adaptive-scoring nil
381   "*If non-nil, use some adaptive scoring scheme.")
382
383 (defvar gnus-use-cache 'passive
384   "*If nil, Gnus will ignore the article cache.
385 If `passive', it will allow entering (and reading) articles
386 explicitly entered into the cache.  If anything else, use the
387 cache to the full extent of the law.")
388
389 (defvar gnus-use-trees nil
390   "*If non-nil, display a thread tree buffer.")
391
392 (defvar gnus-use-grouplens nil
393   "*If non-nil, use GroupLens ratings.")
394
395 (defvar gnus-keep-backlog nil
396   "*If non-nil, Gnus will keep read articles for later re-retrieval.
397 If it is a number N, then Gnus will only keep the last N articles
398 read.  If it is neither nil nor a number, Gnus will keep all read
399 articles.  This is not a good idea.")
400
401 (defvar gnus-use-nocem nil
402   "*If non-nil, Gnus will read NoCeM cancel messages.")
403
404 (defvar gnus-use-demon nil
405   "If non-nil, Gnus might use some demons.")
406
407 (defvar gnus-use-scoring t
408   "*If non-nil, enable scoring.")
409
410 (defvar gnus-use-picons nil
411   "*If non-nil, display picons.")
412
413 (defvar gnus-fetch-old-headers nil
414   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
415 If an unread article in the group refers to an older, already read (or
416 just marked as read) article, the old article will not normally be
417 displayed in the Summary buffer.  If this variable is non-nil, Gnus
418 will attempt to grab the headers to the old articles, and thereby
419 build complete threads.  If it has the value `some', only enough
420 headers to connect otherwise loose threads will be displayed.
421 This variable can also be a number.  In that case, no more than that
422 number of old headers will be fetched.
423
424 The server has to support NOV for any of this to work.")
425
426 ;see gnus-cus.el
427 ;(defvar gnus-visual t
428 ;  "*If non-nil, will do various highlighting.
429 ;If nil, no mouse highlights (or any other highlights) will be
430 ;performed.  This might speed up Gnus some when generating large group
431 ;and summary buffers.")
432
433 (defvar gnus-novice-user t
434   "*Non-nil means that you are a usenet novice.
435 If non-nil, verbose messages may be displayed and confirmations may be
436 required.")
437
438 (defvar gnus-expert-user nil
439   "*Non-nil means that you will never be asked for confirmation about anything.
440 And that means *anything*.")
441
442 (defvar gnus-verbose 7
443   "*Integer that says how verbose Gnus should be.
444 The higher the number, the more messages Gnus will flash to say what
445 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
446 display most important messages; and at ten, Gnus will keep on
447 jabbering all the time.")
448
449 (defvar gnus-keep-same-level nil
450   "*Non-nil means that the next newsgroup after the current will be on the same level.
451 When you type, for instance, `n' after reading the last article in the
452 current newsgroup, you will go to the next newsgroup.  If this variable
453 is nil, the next newsgroup will be the next from the group
454 buffer.
455 If this variable is non-nil, Gnus will either put you in the
456 next newsgroup with the same level, or, if no such newsgroup is
457 available, the next newsgroup with the lowest possible level higher
458 than the current level.
459 If this variable is `best', Gnus will make the next newsgroup the one
460 with the best level.")
461
462 (defvar gnus-summary-make-false-root 'adopt
463   "*nil means that Gnus won't gather loose threads.
464 If the root of a thread has expired or been read in a previous
465 session, the information necessary to build a complete thread has been
466 lost.  Instead of having many small sub-threads from this original thread
467 scattered all over the summary buffer, Gnus can gather them.
468
469 If non-nil, Gnus will try to gather all loose sub-threads from an
470 original thread into one large thread.
471
472 If this variable is non-nil, it should be one of `none', `adopt',
473 `dummy' or `empty'.
474
475 If this variable is `none', Gnus will not make a false root, but just
476 present the sub-threads after another.
477 If this variable is `dummy', Gnus will create a dummy root that will
478 have all the sub-threads as children.
479 If this variable is `adopt', Gnus will make one of the \"children\"
480 the parent and mark all the step-children as such.
481 If this variable is `empty', the \"children\" are printed with empty
482 subject fields.  (Or rather, they will be printed with a string
483 given by the `gnus-summary-same-subject' variable.)")
484
485 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
486   "*A regexp to match subjects to be excluded from loose thread gathering.
487 As loose thread gathering is done on subjects only, that means that
488 there can be many false gatherings performed.  By rooting out certain
489 common subjects, gathering might become saner.")
490
491 (defvar gnus-summary-gather-subject-limit nil
492   "*Maximum length of subject comparisons when gathering loose threads.
493 Use nil to compare full subjects.  Setting this variable to a low
494 number will help gather threads that have been corrupted by
495 newsreaders chopping off subject lines, but it might also mean that
496 unrelated articles that have subject that happen to begin with the
497 same few characters will be incorrectly gathered.
498
499 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
500 comparing subjects.")
501
502 (defvar gnus-simplify-ignored-prefixes nil
503   "*Regexp, matches for which are removed from subject lines when simplifying.")
504
505 (defvar gnus-build-sparse-threads nil
506   "*If non-nil, fill in the gaps in threads.
507 If `some', only fill in the gaps that are needed to tie loose threads
508 together.  If `more', fill in all leaf nodes that Gnus can find.  If
509 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
510
511 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
512   "Function used for gathering loose threads.
513 There are two pre-defined functions: `gnus-gather-threads-by-subject',
514 which only takes Subjects into consideration; and
515 `gnus-gather-threads-by-references', which compared the References
516 headers of the articles to find matches.")
517
518 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
519 (defvar gnus-summary-same-subject ""
520   "*String indicating that the current article has the same subject as the previous.
521 This variable will only be used if the value of
522 `gnus-summary-make-false-root' is `empty'.")
523
524 (defvar gnus-summary-goto-unread t
525   "*If non-nil, marking commands will go to the next unread article.
526 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
527 whether it is read or not.")
528
529 (defvar gnus-group-goto-unread t
530   "*If non-nil, movement commands will go to the next unread and subscribed group.")
531
532 (defvar gnus-goto-next-group-when-activating t
533   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
534
535 (defvar gnus-check-new-newsgroups t
536   "*Non-nil means that Gnus will add new newsgroups at startup.
537 If this variable is `ask-server', Gnus will ask the server for new
538 groups since the last time it checked.  This means that the killed list
539 is no longer necessary, so you could set `gnus-save-killed-list' to
540 nil.
541
542 A variant is to have this variable be a list of select methods.  Gnus
543 will then use the `ask-server' method on all these select methods to
544 query for new groups from all those servers.
545
546 Eg.
547   (setq gnus-check-new-newsgroups
548         '((nntp \"some.server\") (nntp \"other.server\")))
549
550 If this variable is nil, then you have to tell Gnus explicitly to
551 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
552
553 (defvar gnus-check-bogus-newsgroups nil
554   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
555 If this variable is nil, then you have to tell Gnus explicitly to
556 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
557
558 (defvar gnus-read-active-file t
559   "*Non-nil means that Gnus will read the entire active file at startup.
560 If this variable is nil, Gnus will only know about the groups in your
561 `.newsrc' file.
562
563 If this variable is `some', Gnus will try to only read the relevant
564 parts of the active file from the server.  Not all servers support
565 this, and it might be quite slow with other servers, but this should
566 generally be faster than both the t and nil value.
567
568 If you set this variable to nil or `some', you probably still want to
569 be told about new newsgroups that arrive.  To do that, set
570 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
571 properly with all servers.")
572
573 (defvar gnus-level-subscribed 5
574   "*Groups with levels less than or equal to this variable are subscribed.")
575
576 (defvar gnus-level-unsubscribed 7
577   "*Groups with levels less than or equal to this variable are unsubscribed.
578 Groups with levels less than `gnus-level-subscribed', which should be
579 less than this variable, are subscribed.")
580
581 (defvar gnus-level-zombie 8
582   "*Groups with this level are zombie groups.")
583
584 (defvar gnus-level-killed 9
585   "*Groups with this level are killed.")
586
587 (defvar gnus-level-default-subscribed 3
588   "*New subscribed groups will be subscribed at this level.")
589
590 (defvar gnus-level-default-unsubscribed 6
591   "*New unsubscribed groups will be unsubscribed at this level.")
592
593 (defvar gnus-activate-level (1+ gnus-level-subscribed)
594   "*Groups higher than this level won't be activated on startup.
595 Setting this variable to something log might save lots of time when
596 you have many groups that you aren't interested in.")
597
598 (defvar gnus-activate-foreign-newsgroups 4
599   "*If nil, Gnus will not check foreign newsgroups at startup.
600 If it is non-nil, it should be a number between one and nine.  Foreign
601 newsgroups that have a level lower or equal to this number will be
602 activated on startup.  For instance, if you want to active all
603 subscribed newsgroups, but not the rest, you'd set this variable to
604 `gnus-level-subscribed'.
605
606 If you subscribe to lots of newsgroups from different servers, startup
607 might take a while.  By setting this variable to nil, you'll save time,
608 but you won't be told how many unread articles there are in the
609 groups.")
610
611 (defvar gnus-save-newsrc-file t
612   "*Non-nil means that Gnus will save the `.newsrc' file.
613 Gnus always saves its own startup file, which is called
614 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
615 be readily understood by other newsreaders.  If you don't plan on
616 using other newsreaders, set this variable to nil to save some time on
617 exit.")
618
619 (defvar gnus-save-killed-list t
620   "*If non-nil, save the list of killed groups to the startup file.
621 If you set this variable to nil, you'll save both time (when starting
622 and quitting) and space (both memory and disk), but it will also mean
623 that Gnus has no record of which groups are new and which are old, so
624 the automatic new newsgroups subscription methods become meaningless.
625
626 You should always set `gnus-check-new-newsgroups' to `ask-server' or
627 nil if you set this variable to nil.")
628
629 (defvar gnus-interactive-catchup t
630   "*If non-nil, require your confirmation when catching up a group.")
631
632 (defvar gnus-interactive-exit t
633   "*If non-nil, require your confirmation when exiting Gnus.")
634
635 (defvar gnus-kill-killed t
636   "*If non-nil, Gnus will apply kill files to already killed articles.
637 If it is nil, Gnus will never apply kill files to articles that have
638 already been through the scoring process, which might very well save lots
639 of time.")
640
641 (defvar gnus-extract-address-components 'gnus-extract-address-components
642   "*Function for extracting address components from a From header.
643 Two pre-defined function exist: `gnus-extract-address-components',
644 which is the default, quite fast, and too simplistic solution, and
645 `mail-extract-address-components', which works much better, but is
646 slower.")
647
648 (defvar gnus-summary-default-score 0
649   "*Default article score level.
650 If this variable is nil, scoring will be disabled.")
651
652 (defvar gnus-summary-zcore-fuzz 0
653   "*Fuzziness factor for the zcore in the summary buffer.
654 Articles with scores closer than this to `gnus-summary-default-score'
655 will not be marked.")
656
657 (defvar gnus-simplify-subject-fuzzy-regexp nil
658   "*Strings to be removed when doing fuzzy matches.
659 This can either be a regular expression or list of regular expressions
660 that will be removed from subject strings if fuzzy subject
661 simplification is selected.")
662
663 (defvar gnus-permanently-visible-groups nil
664   "*Regexp to match groups that should always be listed in the group buffer.
665 This means that they will still be listed when there are no unread
666 articles in the groups.")
667
668 (defvar gnus-list-groups-with-ticked-articles t
669   "*If non-nil, list groups that have only ticked articles.
670 If nil, only list groups that have unread articles.")
671
672 (defvar gnus-group-default-list-level gnus-level-subscribed
673   "*Default listing level.
674 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
675
676 (defvar gnus-group-use-permanent-levels nil
677   "*If non-nil, once you set a level, Gnus will use this level.")
678
679 (defvar gnus-group-list-inactive-groups t
680   "*If non-nil, inactive groups will be listed.")
681
682 (defvar gnus-show-mime nil
683   "*If non-nil, do mime processing of articles.
684 The articles will simply be fed to the function given by
685 `gnus-show-mime-method'.")
686
687 (defvar gnus-strict-mime t
688   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
689
690 (defvar gnus-show-mime-method 'metamail-buffer
691   "*Function to process a MIME message.
692 The function is called from the article buffer.")
693
694 (defvar gnus-decode-encoded-word-method (lambda ())
695   "*Function to decode a MIME encoded-words.
696 The function is called from the article buffer.")
697
698 (defvar gnus-show-threads t
699   "*If non-nil, display threads in summary mode.")
700
701 (defvar gnus-thread-hide-subtree nil
702   "*If non-nil, hide all threads initially.
703 If threads are hidden, you have to run the command
704 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
705 to expose hidden threads.")
706
707 (defvar gnus-thread-hide-killed t
708   "*If non-nil, hide killed threads automatically.")
709
710 (defvar gnus-thread-ignore-subject nil
711   "*If non-nil, ignore subjects and do all threading based on the Reference header.
712 If nil, which is the default, articles that have different subjects
713 from their parents will start separate threads.")
714
715 (defvar gnus-thread-operation-ignore-subject t
716   "*If non-nil, subjects will be ignored when doing thread commands.
717 This affects commands like `gnus-summary-kill-thread' and
718 `gnus-summary-lower-thread'.
719
720 If this variable is nil, articles in the same thread with different
721 subjects will not be included in the operation in question.  If this
722 variable is `fuzzy', only articles that have subjects that are fuzzily
723 equal will be included.")
724
725 (defvar gnus-thread-indent-level 4
726   "*Number that says how much each sub-thread should be indented.")
727
728 (defvar gnus-ignored-newsgroups
729   (purecopy (mapconcat 'identity
730                        '("^to\\."       ; not "real" groups
731                          "^[0-9. \t]+ " ; all digits in name
732                          "[][\"#'()]"   ; bogus characters
733                          )
734                        "\\|"))
735   "*A regexp to match uninteresting newsgroups in the active file.
736 Any lines in the active file matching this regular expression are
737 removed from the newsgroup list before anything else is done to it,
738 thus making them effectively non-existent.")
739
740 (defvar gnus-ignored-headers
741   "^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:"
742   "*All headers that match this regexp will be hidden.
743 This variable can also be a list of regexps of headers to be ignored.
744 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
745
746 (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-"
747   "*All headers that do not match this regexp will be hidden.
748 This variable can also be a list of regexp of headers to remain visible.
749 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
750
751 (defvar gnus-sorted-header-list
752   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
753     "^Cc:" "^Date:" "^Organization:")
754   "*This variable is a list of regular expressions.
755 If it is non-nil, headers that match the regular expressions will
756 be placed first in the article buffer in the sequence specified by
757 this list.")
758
759 (defvar gnus-boring-article-headers
760   '(empty followup-to reply-to)
761   "*Headers that are only to be displayed if they have interesting data.
762 Possible values in this list are `empty', `newsgroups', `followup-to',
763 `reply-to', and `date'.")
764
765 (defvar gnus-show-all-headers nil
766   "*If non-nil, don't hide any headers.")
767
768 (defvar gnus-save-all-headers t
769   "*If non-nil, don't remove any headers before saving.")
770
771 (defvar gnus-saved-headers gnus-visible-headers
772   "*Headers to keep if `gnus-save-all-headers' is nil.
773 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
774 If that variable is nil, however, all headers that match this regexp
775 will be kept while the rest will be deleted before saving.")
776
777 (defvar gnus-inhibit-startup-message nil
778   "*If non-nil, the startup message will not be displayed.")
779
780 (defvar gnus-signature-separator "^-- *$"
781   "Regexp matching signature separator.")
782
783 (defvar gnus-signature-limit nil
784   "Provide a limit to what is considered a signature.
785 If it is a number, no signature may not be longer (in characters) than
786 that number.  If it is a function, the function will be called without
787 any parameters, and if it returns nil, there is no signature in the
788 buffer.  If it is a string, it will be used as a regexp.  If it
789 matches, the text in question is not a signature.")
790
791 (defvar gnus-auto-extend-newsgroup t
792   "*If non-nil, extend newsgroup forward and backward when requested.")
793
794 (defvar gnus-auto-select-first t
795   "*If nil, don't select the first unread article when entering a group.
796 If this variable is `best', select the highest-scored unread article
797 in the group.  If neither nil nor `best', select the first unread
798 article.
799
800 If you want to prevent automatic selection of the first unread article
801 in some newsgroups, set the variable to nil in
802 `gnus-select-group-hook'.")
803
804 (defvar gnus-auto-select-next t
805   "*If non-nil, offer to go to the next group from the end of the previous.
806 If the value is t and the next newsgroup is empty, Gnus will exit
807 summary mode and go back to group mode.  If the value is neither nil
808 nor t, Gnus will select the following unread newsgroup.  In
809 particular, if the value is the symbol `quietly', the next unread
810 newsgroup will be selected without any confirmation, and if it is
811 `almost-quietly', the next group will be selected without any
812 confirmation if you are located on the last article in the group.
813 Finally, if this variable is `slightly-quietly', the `Z n' command
814 will go to the next group without confirmation.")
815
816 (defvar gnus-auto-select-same nil
817   "*If non-nil, select the next article with the same subject.")
818
819 (defvar gnus-summary-check-current nil
820   "*If non-nil, consider the current article when moving.
821 The \"unread\" movement commands will stay on the same line if the
822 current article is unread.")
823
824 (defvar gnus-auto-center-summary t
825   "*If non-nil, always center the current summary buffer.
826 In particular, if `vertical' do only vertical recentering.  If non-nil
827 and non-`vertical', do both horizontal and vertical recentering.")
828
829 (defvar gnus-break-pages t
830   "*If non-nil, do page breaking on articles.
831 The page delimiter is specified by the `gnus-page-delimiter'
832 variable.")
833
834 (defvar gnus-page-delimiter "^\^L"
835   "*Regexp describing what to use as article page delimiters.
836 The default value is \"^\^L\", which is a form linefeed at the
837 beginning of a line.")
838
839 (defvar gnus-use-full-window t
840   "*If non-nil, use the entire Emacs screen.")
841
842 (defvar gnus-window-configuration nil
843   "Obsolete variable.  See `gnus-buffer-configuration'.")
844
845 (defvar gnus-window-min-width 2
846   "*Minimum width of Gnus buffers.")
847
848 (defvar gnus-window-min-height 1
849   "*Minimum height of Gnus buffers.")
850
851 (defvar gnus-buffer-configuration
852   '((group
853      (vertical 1.0
854                (group 1.0 point)
855                (if gnus-carpal '(group-carpal 4))))
856     (summary
857      (vertical 1.0
858                (summary 1.0 point)
859                (if gnus-carpal '(summary-carpal 4))))
860     (article
861      (cond 
862       (gnus-use-picons
863        '(frame 1.0
864                (vertical 1.0
865                          (summary 0.25 point)
866                          (if gnus-carpal '(summary-carpal 4))
867                          (article 1.0))
868                (vertical ((height . 5) (width . 15)
869                           (user-position . t)
870                           (left . -1) (top . 1))
871                          (picons 1.0))))
872       (gnus-use-trees
873        '(vertical 1.0
874                   (summary 0.25 point)
875                   (tree 0.25)
876                   (article 1.0)))
877       (t
878        '(vertical 1.0
879                  (summary 0.25 point)
880                  (if gnus-carpal '(summary-carpal 4))
881                  (if gnus-use-trees '(tree 0.25))
882                  (article 1.0)))))
883     (server
884      (vertical 1.0
885                (server 1.0 point)
886                (if gnus-carpal '(server-carpal 2))))
887     (browse
888      (vertical 1.0
889                (browse 1.0 point)
890                (if gnus-carpal '(browse-carpal 2))))
891     (message
892      (vertical 1.0
893                (message 1.0 point)))
894     (pick
895      (vertical 1.0
896                (article 1.0 point)))
897     (info
898      (vertical 1.0
899                (info 1.0 point)))
900     (summary-faq
901      (vertical 1.0
902                (summary 0.25)
903                (faq 1.0 point)))
904     (edit-group
905      (vertical 1.0
906                (group 0.5)
907                (edit-group 1.0 point)))
908     (edit-server
909      (vertical 1.0
910                (server 0.5)
911                (edit-server 1.0 point)))
912     (edit-score
913      (vertical 1.0
914                (summary 0.25)
915                (edit-score 1.0 point)))
916     (post
917      (vertical 1.0
918                (post 1.0 point)))
919     (reply
920      (vertical 1.0
921                (article-copy 0.5)
922                (message 1.0 point)))
923     (forward
924      (vertical 1.0
925                (message 1.0 point)))
926     (reply-yank
927      (vertical 1.0
928                (message 1.0 point)))
929     (mail-bounce
930      (vertical 1.0
931                (article 0.5)
932                (message 1.0 point)))
933     (draft
934      (vertical 1.0
935                (draft 1.0 point)))
936     (pipe
937      (vertical 1.0
938                (summary 0.25 point)
939                (if gnus-carpal '(summary-carpal 4))
940                ("*Shell Command Output*" 1.0)))
941     (bug
942      (vertical 1.0
943                ("*Gnus Help Bug*" 0.5)
944                ("*Gnus Bug*" 1.0 point)))
945     (compose-bounce
946      (vertical 1.0
947                (article 0.5)
948                (message 1.0 point))))
949   "Window configuration for all possible Gnus buffers.
950 This variable is a list of lists.  Each of these lists has a NAME and
951 a RULE.  The NAMEs are commonsense names like `group', which names a
952 rule used when displaying the group buffer; `summary', which names a
953 rule for what happens when you enter a group and do not display an
954 article buffer; and so on.  See the value of this variable for a
955 complete list of NAMEs.
956
957 Each RULE is a list of vectors.  The first element in this vector is
958 the name of the buffer to be displayed; the second element is the
959 percentage of the screen this buffer is to occupy (a number in the
960 0.0-0.99 range); the optional third element is `point', which should
961 be present to denote which buffer point is to go to after making this
962 buffer configuration.")
963
964 (defvar gnus-window-to-buffer
965   '((group . gnus-group-buffer)
966     (summary . gnus-summary-buffer)
967     (article . gnus-article-buffer)
968     (server . gnus-server-buffer)
969     (browse . "*Gnus Browse Server*")
970     (edit-group . gnus-group-edit-buffer)
971     (edit-server . gnus-server-edit-buffer)
972     (group-carpal . gnus-carpal-group-buffer)
973     (summary-carpal . gnus-carpal-summary-buffer)
974     (server-carpal . gnus-carpal-server-buffer)
975     (browse-carpal . gnus-carpal-browse-buffer)
976     (edit-score . gnus-score-edit-buffer)
977     (message . gnus-message-buffer)
978     (mail . gnus-message-buffer)
979     (post-news . gnus-message-buffer)
980     (faq . gnus-faq-buffer)
981     (picons . "*Picons*")
982     (tree . gnus-tree-buffer)
983     (info . gnus-info-buffer)
984     (article-copy . gnus-article-copy)
985     (draft . gnus-draft-buffer))
986   "Mapping from short symbols to buffer names or buffer variables.")
987
988 (defvar gnus-carpal nil
989   "*If non-nil, display clickable icons.")
990
991 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
992   "*Function called with a group name when new group is detected.
993 A few pre-made functions are supplied: `gnus-subscribe-randomly'
994 inserts new groups at the beginning of the list of groups;
995 `gnus-subscribe-alphabetically' inserts new groups in strict
996 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
997 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
998 for your decision; `gnus-subscribe-killed' kills all new groups;
999 `gnus-subscribe-zombies' will make all new groups into zombies.")
1000
1001 ;; Suggested by a bug report by Hallvard B Furuseth.
1002 ;; <h.b.furuseth@usit.uio.no>.
1003 (defvar gnus-subscribe-options-newsgroup-method
1004   (function gnus-subscribe-alphabetically)
1005   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
1006 If, for instance, you want to subscribe to all newsgroups in the
1007 \"no\" and \"alt\" hierarchies, you'd put the following in your
1008 .newsrc file:
1009
1010 options -n no.all alt.all
1011
1012 Gnus will the subscribe all new newsgroups in these hierarchies with
1013 the subscription method in this variable.")
1014
1015 (defvar gnus-subscribe-hierarchical-interactive nil
1016   "*If non-nil, Gnus will offer to subscribe hierarchically.
1017 When a new hierarchy appears, Gnus will ask the user:
1018
1019 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1020
1021 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1022 subscribe to all newsgroups in the hierarchy and `s' will skip this
1023 hierarchy in its entirety.")
1024
1025 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1026   "*Function used for sorting the group buffer.
1027 This function will be called with group info entries as the arguments
1028 for the groups to be sorted.  Pre-made functions include
1029 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1030 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1031 `gnus-group-sort-by-rank'.
1032
1033 This variable can also be a list of sorting functions.  In that case,
1034 the most significant sort function should be the last function in the
1035 list.")
1036
1037 ;; Mark variables suggested by Thomas Michanek
1038 ;; <Thomas.Michanek@telelogic.se>.
1039 (defvar gnus-unread-mark ? 
1040   "*Mark used for unread articles.")
1041 (defvar gnus-ticked-mark ?!
1042   "*Mark used for ticked articles.")
1043 (defvar gnus-dormant-mark ??
1044   "*Mark used for dormant articles.")
1045 (defvar gnus-del-mark ?r
1046   "*Mark used for del'd articles.")
1047 (defvar gnus-read-mark ?R
1048   "*Mark used for read articles.")
1049 (defvar gnus-expirable-mark ?E
1050   "*Mark used for expirable articles.")
1051 (defvar gnus-killed-mark ?K
1052   "*Mark used for killed articles.")
1053 (defvar gnus-souped-mark ?F
1054   "*Mark used for killed articles.")
1055 (defvar gnus-kill-file-mark ?X
1056   "*Mark used for articles killed by kill files.")
1057 (defvar gnus-low-score-mark ?Y
1058   "*Mark used for articles with a low score.")
1059 (defvar gnus-catchup-mark ?C
1060   "*Mark used for articles that are caught up.")
1061 (defvar gnus-replied-mark ?A
1062   "*Mark used for articles that have been replied to.")
1063 (defvar gnus-cached-mark ?*
1064   "*Mark used for articles that are in the cache.")
1065 (defvar gnus-saved-mark ?S
1066   "*Mark used for articles that have been saved to.")
1067 (defvar gnus-process-mark ?#
1068   "*Process mark.")
1069 (defvar gnus-ancient-mark ?O
1070   "*Mark used for ancient articles.")
1071 (defvar gnus-sparse-mark ?Q
1072   "*Mark used for sparsely reffed articles.")
1073 (defvar gnus-canceled-mark ?G
1074   "*Mark used for canceled articles.")
1075 (defvar gnus-score-over-mark ?+
1076   "*Score mark used for articles with high scores.")
1077 (defvar gnus-score-below-mark ?-
1078   "*Score mark used for articles with low scores.")
1079 (defvar gnus-empty-thread-mark ? 
1080   "*There is no thread under the article.")
1081 (defvar gnus-not-empty-thread-mark ?=
1082   "*There is a thread under the article.")
1083
1084 (defvar gnus-view-pseudo-asynchronously nil
1085   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1086
1087 (defvar gnus-view-pseudos nil
1088   "*If `automatic', pseudo-articles will be viewed automatically.
1089 If `not-confirm', pseudos will be viewed automatically, and the user
1090 will not be asked to confirm the command.")
1091
1092 (defvar gnus-view-pseudos-separately t
1093   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1094 If nil, all files that use the same viewing command will be given as a
1095 list of parameters to that command.")
1096
1097 (defvar gnus-insert-pseudo-articles t
1098   "*If non-nil, insert pseudo-articles when decoding articles.")
1099
1100 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1101   "*Format of group lines.
1102 It works along the same lines as a normal formatting string,
1103 with some simple extensions.
1104
1105 %M    Only marked articles (character, \"*\" or \" \")
1106 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1107 %L    Level of subscribedness (integer)
1108 %N    Number of unread articles (integer)
1109 %I    Number of dormant articles (integer)
1110 %i    Number of ticked and dormant (integer)
1111 %T    Number of ticked articles (integer)
1112 %R    Number of read articles (integer)
1113 %t    Total number of articles (integer)
1114 %y    Number of unread, unticked articles (integer)
1115 %G    Group name (string)
1116 %g    Qualified group name (string)
1117 %D    Group description (string)
1118 %s    Select method (string)
1119 %o    Moderated group (char, \"m\")
1120 %p    Process mark (char)
1121 %O    Moderated group (string, \"(m)\" or \"\")
1122 %P    Topic indentation (string)
1123 %l    Whether there are GroupLens predictions for this group (string)
1124 %n    Select from where (string)
1125 %z    A string that look like `<%s:%n>' if a foreign select method is used
1126 %u    User defined specifier.  The next character in the format string should
1127       be a letter.  Gnus will call the function gnus-user-format-function-X,
1128       where X is the letter following %u.  The function will be passed the
1129       current header as argument.  The function should return a string, which
1130       will be inserted into the buffer just like information from any other
1131       group specifier.
1132
1133 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1134 the mouse point move inside the area.  There can only be one such area.
1135
1136 Note that this format specification is not always respected.  For
1137 reasons of efficiency, when listing killed groups, this specification
1138 is ignored altogether.  If the spec is changed considerably, your
1139 output may end up looking strange when listing both alive and killed
1140 groups.
1141
1142 If you use %o or %O, reading the active file will be slower and quite
1143 a bit of extra memory will be used. %D will also worsen performance.
1144 Also note that if you change the format specification to include any
1145 of these specs, you must probably re-start Gnus to see them go into
1146 effect.")
1147
1148 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1149   "*The format specification of the lines in the summary buffer.
1150
1151 It works along the same lines as a normal formatting string,
1152 with some simple extensions.
1153
1154 %N   Article number, left padded with spaces (string)
1155 %S   Subject (string)
1156 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1157 %n   Name of the poster (string)
1158 %a   Extracted name of the poster (string)
1159 %A   Extracted address of the poster (string)
1160 %F   Contents of the From: header (string)
1161 %x   Contents of the Xref: header (string)
1162 %D   Date of the article (string)
1163 %d   Date of the article (string) in DD-MMM format
1164 %M   Message-id of the article (string)
1165 %r   References of the article (string)
1166 %c   Number of characters in the article (integer)
1167 %L   Number of lines in the article (integer)
1168 %I   Indentation based on thread level (a string of spaces)
1169 %T   A string with two possible values: 80 spaces if the article
1170      is on thread level two or larger and 0 spaces on level one
1171 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1172 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1173 %[   Opening bracket (character, \"[\" or \"<\")
1174 %]   Closing bracket (character, \"]\" or \">\")
1175 %>   Spaces of length thread-level (string)
1176 %<   Spaces of length (- 20 thread-level) (string)
1177 %i   Article score (number)
1178 %z   Article zcore (character)
1179 %t   Number of articles under the current thread (number).
1180 %e   Whether the thread is empty or not (character).
1181 %l   GroupLens score (string).
1182 %u   User defined specifier.  The next character in the format string should
1183      be a letter.  Gnus will call the function gnus-user-format-function-X,
1184      where X is the letter following %u.  The function will be passed the
1185      current header as argument.  The function should return a string, which
1186      will be inserted into the summary just like information from any other
1187      summary specifier.
1188
1189 Text between %( and %) will be highlighted with `gnus-mouse-face'
1190 when the mouse point is placed inside the area.  There can only be one
1191 such area.
1192
1193 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1194 with care.  For reasons of efficiency, Gnus will compute what column
1195 these characters will end up in, and \"hard-code\" that.  This means that
1196 it is illegal to have these specs after a variable-length spec.  Well,
1197 you might not be arrested, but your summary buffer will look strange,
1198 which is bad enough.
1199
1200 The smart choice is to have these specs as for to the left as
1201 possible.
1202
1203 This restriction may disappear in later versions of Gnus.")
1204
1205 (defvar gnus-summary-dummy-line-format
1206   "*  %(:                          :%) %S\n"
1207   "*The format specification for the dummy roots in the summary buffer.
1208 It works along the same lines as a normal formatting string,
1209 with some simple extensions.
1210
1211 %S  The subject")
1212
1213 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1214   "*The format specification for the summary mode line.
1215 It works along the same lines as a normal formatting string,
1216 with some simple extensions:
1217
1218 %G  Group name
1219 %p  Unprefixed group name
1220 %A  Current article number
1221 %V  Gnus version
1222 %U  Number of unread articles in the group
1223 %e  Number of unselected articles in the group
1224 %Z  A string with unread/unselected article counts
1225 %g  Shortish group name
1226 %S  Subject of the current article
1227 %u  User-defined spec
1228 %s  Current score file name
1229 %d  Number of dormant articles
1230 %r  Number of articles that have been marked as read in this session
1231 %E  Number of articles expunged by the score files")
1232
1233 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1234   "*The format specification for the article mode line.
1235 See `gnus-summary-mode-line-format' for a closer description.")
1236
1237 (defvar gnus-group-mode-line-format "Gnus: %%b {%M%:%S}"
1238   "*The format specification for the group mode line.
1239 It works along the same lines as a normal formatting string,
1240 with some simple extensions:
1241
1242 %S   The native news server.
1243 %M   The native select method.
1244 %:   \":\" if %S isn't \"\".")
1245
1246 (defvar gnus-valid-select-methods
1247   '(("nntp" post address prompt-address)
1248     ("nnspool" post address)
1249     ("nnvirtual" post-mail virtual prompt-address)
1250     ("nnmbox" mail respool address)
1251     ("nnml" mail respool address)
1252     ("nnmh" mail respool address)
1253     ("nndir" post-mail prompt-address address)
1254     ("nneething" none address prompt-address)
1255     ("nndoc" none address prompt-address)
1256     ("nnbabyl" mail address respool)
1257     ("nnkiboze" post virtual)
1258     ("nnsoup" post-mail address)
1259     ("nndraft" post-mail)
1260     ("nnfolder" mail respool address))
1261   "An alist of valid select methods.
1262 The first element of each list lists should be a string with the name
1263 of the select method.  The other elements may be be the category of
1264 this method (ie. `post', `mail', `none' or whatever) or other
1265 properties that this method has (like being respoolable).
1266 If you implement a new select method, all you should have to change is
1267 this variable.  I think.")
1268
1269 (defvar gnus-updated-mode-lines '(group article summary tree)
1270   "*List of buffers that should update their mode lines.
1271 The list may contain the symbols `group', `article' and `summary'.  If
1272 the corresponding symbol is present, Gnus will keep that mode line
1273 updated with information that may be pertinent.
1274 If this variable is nil, screen refresh may be quicker.")
1275
1276 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1277 (defvar gnus-mode-non-string-length nil
1278   "*Max length of mode-line non-string contents.
1279 If this is nil, Gnus will take space as is needed, leaving the rest
1280 of the modeline intact.")
1281
1282 ;see gnus-cus.el
1283 ;(defvar gnus-mouse-face 'highlight
1284 ;  "*Face used for mouse highlighting in Gnus.
1285 ;No mouse highlights will be done if `gnus-visual' is nil.")
1286
1287 (defvar gnus-summary-mark-below 0
1288   "*Mark all articles with a score below this variable as read.
1289 This variable is local to each summary buffer and usually set by the
1290 score file.")
1291
1292 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1293   "*List of functions used for sorting articles in the summary buffer.
1294 This variable is only used when not using a threaded display.")
1295
1296 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1297   "*List of functions used for sorting threads in the summary buffer.
1298 By default, threads are sorted by article number.
1299
1300 Each function takes two threads and return non-nil if the first thread
1301 should be sorted before the other.  If you use more than one function,
1302 the primary sort function should be the last.  You should probably
1303 always include `gnus-thread-sort-by-number' in the list of sorting
1304 functions -- preferably first.
1305
1306 Ready-mady functions include `gnus-thread-sort-by-number',
1307 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1308 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1309 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1310
1311 (defvar gnus-thread-score-function '+
1312   "*Function used for calculating the total score of a thread.
1313
1314 The function is called with the scores of the article and each
1315 subthread and should then return the score of the thread.
1316
1317 Some functions you can use are `+', `max', or `min'.")
1318
1319 (defvar gnus-summary-expunge-below nil
1320   "All articles that have a score less than this variable will be expunged.")
1321
1322 (defvar gnus-thread-expunge-below nil
1323   "All threads that have a total score less than this variable will be expunged.
1324 See `gnus-thread-score-function' for en explanation of what a
1325 \"thread score\" is.")
1326
1327 (defvar gnus-auto-subscribed-groups
1328   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1329   "*All new groups that match this regexp will be subscribed automatically.
1330 Note that this variable only deals with new groups.  It has no effect
1331 whatsoever on old groups.
1332
1333 New groups that match this regexp will not be handled by
1334 `gnus-subscribe-newsgroup-method'.  Instead, they will
1335 be subscribed using `gnus-subscribe-options-newsgroup-method'.")
1336
1337 (defvar gnus-options-subscribe nil
1338   "*All new groups matching this regexp will be subscribed unconditionally.
1339 Note that this variable deals only with new newsgroups.  This variable
1340 does not affect old newsgroups.
1341
1342 New groups that match this regexp will not be handled by
1343 `gnus-subscribe-newsgroup-method'.  Instead, they will
1344 be subscribed using `gnus-subscribe-options-newsgroup-method'.")
1345
1346 (defvar gnus-options-not-subscribe nil
1347   "*All new groups matching this regexp will be ignored.
1348 Note that this variable deals only with new newsgroups.  This variable
1349 does not affect old (already subscribed) newsgroups.")
1350
1351 (defvar gnus-auto-expirable-newsgroups nil
1352   "*Groups in which to automatically mark read articles as expirable.
1353 If non-nil, this should be a regexp that should match all groups in
1354 which to perform auto-expiry.  This only makes sense for mail groups.")
1355
1356 (defvar gnus-total-expirable-newsgroups nil
1357   "*Groups in which to perform expiry of all read articles.
1358 Use with extreme caution.  All groups that match this regexp will be
1359 expiring - which means that all read articles will be deleted after
1360 (say) one week.  (This only goes for mail groups and the like, of
1361 course.)")
1362
1363 (defvar gnus-group-uncollapsed-levels 1
1364   "Number of group name elements to leave alone when making a short group name.")
1365
1366 (defvar gnus-hidden-properties '(invisible t intangible t)
1367   "Property list to use for hiding text.")
1368
1369 (defvar gnus-modtime-botch nil
1370   "*Non-nil means .newsrc should be deleted prior to save.  
1371 Its use is due to the bogus appearance that .newsrc was modified on
1372 disc.")
1373
1374 ;; Hooks.
1375
1376 (defvar gnus-group-mode-hook nil
1377   "*A hook for Gnus group mode.")
1378
1379 (defvar gnus-summary-mode-hook nil
1380   "*A hook for Gnus summary mode.
1381 This hook is run before any variables are set in the summary buffer.")
1382
1383 (defvar gnus-article-mode-hook nil
1384   "*A hook for Gnus article mode.")
1385
1386 (defvar gnus-summary-prepare-exit-hook nil
1387   "*A hook called when preparing to exit from the summary buffer.
1388 It calls `gnus-summary-expire-articles' by default.")
1389 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1390
1391 (defvar gnus-summary-exit-hook nil
1392   "*A hook called on exit from the summary buffer.")
1393
1394 (defvar gnus-group-catchup-group-hook nil
1395   "*A hook run when catching up a group from the group buffer.")
1396
1397 (defvar gnus-group-update-group-hook nil
1398   "*A hook called when updating group lines.")
1399
1400 (defvar gnus-open-server-hook nil
1401   "*A hook called just before opening connection to the news server.")
1402
1403 (defvar gnus-load-hook nil
1404   "*A hook run while Gnus is loaded.")
1405
1406 (defvar gnus-startup-hook nil
1407   "*A hook called at startup.
1408 This hook is called after Gnus is connected to the NNTP server.")
1409
1410 (defvar gnus-get-new-news-hook nil
1411   "*A hook run just before Gnus checks for new news.")
1412
1413 (defvar gnus-after-getting-new-news-hook nil
1414   "*A hook run after Gnus checks for new news.")
1415
1416 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1417   "*A function that is called to generate the group buffer.
1418 The function is called with three arguments: The first is a number;
1419 all group with a level less or equal to that number should be listed,
1420 if the second is non-nil, empty groups should also be displayed.  If
1421 the third is non-nil, it is a number.  No groups with a level lower
1422 than this number should be displayed.
1423
1424 The only current function implemented is `gnus-group-prepare-flat'.")
1425
1426 (defvar gnus-group-prepare-hook nil
1427   "*A hook called after the group buffer has been generated.
1428 If you want to modify the group buffer, you can use this hook.")
1429
1430 (defvar gnus-summary-prepare-hook nil
1431   "*A hook called after the summary buffer has been generated.
1432 If you want to modify the summary buffer, you can use this hook.")
1433
1434 (defvar gnus-summary-generate-hook nil
1435   "*A hook run just before generating the summary buffer.
1436 This hook is commonly used to customize threading variables and the
1437 like.")
1438
1439 (defvar gnus-article-prepare-hook nil
1440   "*A hook called after an article has been prepared in the article buffer.
1441 If you want to run a special decoding program like nkf, use this hook.")
1442
1443 ;(defvar gnus-article-display-hook nil
1444 ;  "*A hook called after the article is displayed in the article buffer.
1445 ;The hook is designed to change the contents of the article
1446 ;buffer.  Typical functions that this hook may contain are
1447 ;`gnus-article-hide-headers' (hide selected headers),
1448 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1449 ;`gnus-article-hide-signature' (hide signature) and
1450 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1451 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1452 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1453 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1454
1455 (defvar gnus-article-x-face-too-ugly nil
1456   "Regexp matching posters whose face shouldn't be shown automatically.")
1457
1458 (defvar gnus-select-group-hook nil
1459   "*A hook called when a newsgroup is selected.
1460
1461 If you'd like to simplify subjects like the
1462 `gnus-summary-next-same-subject' command does, you can use the
1463 following hook:
1464
1465  (setq gnus-select-group-hook
1466       (list
1467         (lambda ()
1468           (mapcar (lambda (header)
1469                      (mail-header-set-subject
1470                       header
1471                       (gnus-simplify-subject
1472                        (mail-header-subject header) 're-only)))
1473                   gnus-newsgroup-headers))))")
1474
1475 (defvar gnus-select-article-hook nil
1476   "*A hook called when an article is selected.")
1477
1478 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1479   "*A hook called to apply kill files to a group.
1480 This hook is intended to apply a kill file to the selected newsgroup.
1481 The function `gnus-apply-kill-file' is called by default.
1482
1483 Since a general kill file is too heavy to use only for a few
1484 newsgroups, I recommend you to use a lighter hook function.  For
1485 example, if you'd like to apply a kill file to articles which contains
1486 a string `rmgroup' in subject in newsgroup `control', you can use the
1487 following hook:
1488
1489  (setq gnus-apply-kill-hook
1490       (list
1491         (lambda ()
1492           (cond ((string-match \"control\" gnus-newsgroup-name)
1493                  (gnus-kill \"Subject\" \"rmgroup\")
1494                  (gnus-expunge \"X\"))))))")
1495
1496 (defvar gnus-visual-mark-article-hook
1497   (list 'gnus-highlight-selected-summary)
1498   "*Hook run after selecting an article in the summary buffer.
1499 It is meant to be used for highlighting the article in some way.  It
1500 is not run if `gnus-visual' is nil.")
1501
1502 (defvar gnus-parse-headers-hook nil
1503   "*A hook called before parsing the headers.")
1504 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1505
1506 (defvar gnus-exit-group-hook nil
1507   "*A hook called when exiting (not quitting) summary mode.")
1508
1509 (defvar gnus-suspend-gnus-hook nil
1510   "*A hook called when suspending (not exiting) Gnus.")
1511
1512 (defvar gnus-exit-gnus-hook nil
1513   "*A hook called when exiting Gnus.")
1514
1515 (defvar gnus-after-exiting-gnus-hook nil
1516   "*A hook called after exiting Gnus.")
1517
1518 (defvar gnus-save-newsrc-hook nil
1519   "*A hook called before saving any of the newsrc files.")
1520
1521 (defvar gnus-save-quick-newsrc-hook nil
1522   "*A hook called just before saving the quick newsrc file.
1523 Can be used to turn version control on or off.")
1524
1525 (defvar gnus-save-standard-newsrc-hook nil
1526   "*A hook called just before saving the standard newsrc file.
1527 Can be used to turn version control on or off.")
1528
1529 (defvar gnus-summary-update-hook
1530   (list 'gnus-summary-highlight-line)
1531   "*A hook called when a summary line is changed.
1532 The hook will not be called if `gnus-visual' is nil.
1533
1534 The default function `gnus-summary-highlight-line' will
1535 highlight the line according to the `gnus-summary-highlight'
1536 variable.")
1537
1538 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1539   "*A hook called when a group line is changed.
1540 The hook will not be called if `gnus-visual' is nil.
1541
1542 The default function `gnus-group-highlight-line' will
1543 highlight the line according to the `gnus-group-highlight'
1544 variable.")
1545
1546 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1547   "*A hook called when an article is selected for the first time.
1548 The hook is intended to mark an article as read (or unread)
1549 automatically when it is selected.")
1550
1551 (defvar gnus-group-change-level-function nil
1552   "Function run when a group level is changed.
1553 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1554
1555 ;; Remove any hilit infestation.
1556 (add-hook 'gnus-startup-hook
1557           (lambda ()
1558             (remove-hook 'gnus-summary-prepare-hook
1559                          'hilit-rehighlight-buffer-quietly)
1560             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1561             (setq gnus-mark-article-hook
1562                   '(gnus-summary-mark-read-and-unread-as-read))
1563             (remove-hook 'gnus-article-prepare-hook
1564                          'hilit-rehighlight-buffer-quietly)))
1565
1566 \f
1567 ;; Internal variables
1568
1569 (defvar gnus-tree-buffer "*Tree*"
1570   "Buffer where Gnus thread trees are displayed.")
1571
1572 ;; Dummy variable.
1573 (defvar gnus-use-generic-from nil)
1574
1575 (defvar gnus-thread-indent-array nil)
1576 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1577
1578 (defvar gnus-newsrc-file-version nil)
1579
1580 (defvar gnus-method-history nil)
1581 ;; Variable holding the user answers to all method prompts.
1582
1583 (defvar gnus-group-history nil)
1584 ;; Variable holding the user answers to all group prompts.
1585
1586 (defvar gnus-server-alist nil
1587   "List of available servers.")
1588
1589 (defvar gnus-group-indentation-function nil)
1590
1591 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1592
1593 (defvar gnus-goto-missing-group-function nil)
1594
1595 (defvar gnus-override-subscribe-method nil)
1596
1597 (defvar gnus-group-goto-next-group-function nil
1598   "Function to override finding the next group after listing groups.")
1599
1600 (defconst gnus-article-mark-lists
1601   '((marked . tick) (replied . reply)
1602     (expirable . expire) (killed . killed)
1603     (bookmarks . bookmark) (dormant . dormant)
1604     (scored . score) (saved . save)
1605     (cached . cache)
1606     ))
1607
1608 ;; Avoid highlighting in kill files.
1609 (defvar gnus-summary-inhibit-highlight nil)
1610 (defvar gnus-newsgroup-selected-overlay nil)
1611
1612 (defvar gnus-inhibit-hiding nil)
1613 (defvar gnus-group-indentation "")
1614 (defvar gnus-inhibit-limiting nil)
1615 (defvar gnus-created-frames nil)
1616
1617 (defvar gnus-article-mode-map nil)
1618 (defvar gnus-dribble-buffer nil)
1619 (defvar gnus-headers-retrieved-by nil)
1620 (defvar gnus-article-reply nil)
1621 (defvar gnus-override-method nil)
1622 (defvar gnus-article-check-size nil)
1623
1624 (defvar gnus-current-score-file nil)
1625 (defvar gnus-newsgroup-adaptive-score-file nil)
1626 (defvar gnus-scores-exclude-files nil)
1627
1628 (defvar gnus-opened-servers nil)
1629
1630 (defvar gnus-current-move-group nil)
1631 (defvar gnus-current-copy-group nil)
1632 (defvar gnus-current-crosspost-group nil)
1633
1634 (defvar gnus-newsgroup-dependencies nil)
1635 (defvar gnus-newsgroup-async nil)
1636 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1637
1638 (defvar gnus-newsgroup-adaptive nil)
1639
1640 (defvar gnus-summary-display-table nil)
1641 (defvar gnus-summary-display-article-function nil)
1642
1643 (defvar gnus-summary-highlight-line-function nil
1644   "Function called after highlighting a summary line.")
1645
1646 (defvar gnus-group-line-format-alist
1647   `((?M gnus-tmp-marked-mark ?c)
1648     (?S gnus-tmp-subscribed ?c)
1649     (?L gnus-tmp-level ?d)
1650     (?N (cond ((eq number t) "*" )
1651               ((numberp number) 
1652                (int-to-string
1653                 (+ number
1654                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1655                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1656               (t number)) ?s)
1657     (?R gnus-tmp-number-of-read ?s)
1658     (?t gnus-tmp-number-total ?d)
1659     (?y gnus-tmp-number-of-unread ?s)
1660     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1661     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1662     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1663            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1664     (?g gnus-tmp-group ?s)
1665     (?G gnus-tmp-qualified-group ?s)
1666     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1667     (?D gnus-tmp-newsgroup-description ?s)
1668     (?o gnus-tmp-moderated ?c)
1669     (?O gnus-tmp-moderated-string ?s)
1670     (?p gnus-tmp-process-marked ?c)
1671     (?s gnus-tmp-news-server ?s)
1672     (?n gnus-tmp-news-method ?s)
1673     (?P gnus-group-indentation ?s)
1674     (?l gnus-tmp-grouplens ?s)
1675     (?z gnus-tmp-news-method-string ?s)
1676     (?u gnus-tmp-user-defined ?s)))
1677
1678 (defvar gnus-summary-line-format-alist
1679   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1680     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1681     (?s gnus-tmp-subject-or-nil ?s)
1682     (?n gnus-tmp-name ?s)
1683     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1684         ?s)
1685     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1686             gnus-tmp-from) ?s)
1687     (?F gnus-tmp-from ?s)
1688     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1689     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1690     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1691     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1692     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1693     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1694     (?L gnus-tmp-lines ?d)
1695     (?I gnus-tmp-indentation ?s)
1696     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1697     (?R gnus-tmp-replied ?c)
1698     (?\[ gnus-tmp-opening-bracket ?c)
1699     (?\] gnus-tmp-closing-bracket ?c)
1700     (?\> (make-string gnus-tmp-level ? ) ?s)
1701     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1702     (?i gnus-tmp-score ?d)
1703     (?z gnus-tmp-score-char ?c)
1704     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1705     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1706     (?U gnus-tmp-unread ?c)
1707     (?t (gnus-summary-number-of-articles-in-thread
1708          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1709         ?d)
1710     (?e (gnus-summary-number-of-articles-in-thread
1711          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1712         ?c)
1713     (?u gnus-tmp-user-defined ?s))
1714   "An alist of format specifications that can appear in summary lines,
1715 and what variables they correspond with, along with the type of the
1716 variable (string, integer, character, etc).")
1717
1718 (defvar gnus-summary-dummy-line-format-alist
1719   `((?S gnus-tmp-subject ?s)
1720     (?N gnus-tmp-number ?d)
1721     (?u gnus-tmp-user-defined ?s)))
1722
1723 (defvar gnus-summary-mode-line-format-alist
1724   `((?G gnus-tmp-group-name ?s)
1725     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1726     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1727     (?A gnus-tmp-article-number ?d)
1728     (?Z gnus-tmp-unread-and-unselected ?s)
1729     (?V gnus-version ?s)
1730     (?U gnus-tmp-unread-and-unticked ?d)
1731     (?S gnus-tmp-subject ?s)
1732     (?e gnus-tmp-unselected ?d)
1733     (?u gnus-tmp-user-defined ?s)
1734     (?d (length gnus-newsgroup-dormant) ?d)
1735     (?t (length gnus-newsgroup-marked) ?d)
1736     (?r (length gnus-newsgroup-reads) ?d)
1737     (?E gnus-newsgroup-expunged-tally ?d)
1738     (?s (gnus-current-score-file-nondirectory) ?s)))
1739
1740 (defvar gnus-article-mode-line-format-alist
1741   gnus-summary-mode-line-format-alist)
1742
1743 (defvar gnus-group-mode-line-format-alist
1744   `((?S gnus-tmp-news-server ?s)
1745     (?M gnus-tmp-news-method ?s)
1746     (?u gnus-tmp-user-defined ?s)
1747     (?: gnus-tmp-colon ?s)))
1748
1749 (defvar gnus-have-read-active-file nil)
1750
1751 (defconst gnus-maintainer
1752   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1753   "The mail address of the Gnus maintainers.")
1754
1755 (defconst gnus-version-number "5.2.27"
1756   "Version number for this version of Gnus.")
1757
1758 (defconst gnus-version (format "Gnus v%s" gnus-version-number)
1759   "Version string for this version of Gnus.")
1760
1761 (defvar gnus-info-nodes
1762   '((gnus-group-mode "(gnus)The Group Buffer")
1763     (gnus-summary-mode "(gnus)The Summary Buffer")
1764     (gnus-article-mode "(gnus)The Article Buffer")
1765     (gnus-server-mode "(gnus)The Server Buffer")
1766     (gnus-browse-mode "(gnus)Browse Foreign Server")
1767     (gnus-tree-mode "(gnus)Tree Display")
1768     )
1769   "Alist of major modes and related Info nodes.")
1770
1771 (defvar gnus-group-buffer "*Group*")
1772 (defvar gnus-summary-buffer "*Summary*")
1773 (defvar gnus-article-buffer "*Article*")
1774 (defvar gnus-server-buffer "*Server*")
1775
1776 (defvar gnus-work-buffer " *gnus work*")
1777
1778 (defvar gnus-original-article-buffer " *Original Article*")
1779 (defvar gnus-original-article nil)
1780
1781 (defvar gnus-buffer-list nil
1782   "Gnus buffers that should be killed on exit.")
1783
1784 (defvar gnus-slave nil
1785   "Whether this Gnus is a slave or not.")
1786
1787 (defvar gnus-variable-list
1788   '(gnus-newsrc-options gnus-newsrc-options-n
1789     gnus-newsrc-last-checked-date
1790     gnus-newsrc-alist gnus-server-alist
1791     gnus-killed-list gnus-zombie-list
1792     gnus-topic-topology gnus-topic-alist
1793     gnus-format-specs)
1794   "Gnus variables saved in the quick startup file.")
1795
1796 (defvar gnus-newsrc-options nil
1797   "Options line in the .newsrc file.")
1798
1799 (defvar gnus-newsrc-options-n nil
1800   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1801
1802 (defvar gnus-newsrc-last-checked-date nil
1803   "Date Gnus last asked server for new newsgroups.")
1804
1805 (defvar gnus-topic-topology nil
1806   "The complete topic hierarchy.")
1807
1808 (defvar gnus-topic-alist nil
1809   "The complete topic-group alist.")
1810
1811 (defvar gnus-newsrc-alist nil
1812   "Assoc list of read articles.
1813 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1814
1815 (defvar gnus-newsrc-hashtb nil
1816   "Hashtable of gnus-newsrc-alist.")
1817
1818 (defvar gnus-killed-list nil
1819   "List of killed newsgroups.")
1820
1821 (defvar gnus-killed-hashtb nil
1822   "Hash table equivalent of gnus-killed-list.")
1823
1824 (defvar gnus-zombie-list nil
1825   "List of almost dead newsgroups.")
1826
1827 (defvar gnus-description-hashtb nil
1828   "Descriptions of newsgroups.")
1829
1830 (defvar gnus-list-of-killed-groups nil
1831   "List of newsgroups that have recently been killed by the user.")
1832
1833 (defvar gnus-active-hashtb nil
1834   "Hashtable of active articles.")
1835
1836 (defvar gnus-moderated-list nil
1837   "List of moderated newsgroups.")
1838
1839 (defvar gnus-group-marked nil)
1840
1841 (defvar gnus-current-startup-file nil
1842   "Startup file for the current host.")
1843
1844 (defvar gnus-last-search-regexp nil
1845   "Default regexp for article search command.")
1846
1847 (defvar gnus-last-shell-command nil
1848   "Default shell command on article.")
1849
1850 (defvar gnus-current-select-method nil
1851   "The current method for selecting a newsgroup.")
1852
1853 (defvar gnus-group-list-mode nil)
1854
1855 (defvar gnus-article-internal-prepare-hook nil)
1856
1857 (defvar gnus-newsgroup-name nil)
1858 (defvar gnus-newsgroup-begin nil)
1859 (defvar gnus-newsgroup-end nil)
1860 (defvar gnus-newsgroup-last-rmail nil)
1861 (defvar gnus-newsgroup-last-mail nil)
1862 (defvar gnus-newsgroup-last-folder nil)
1863 (defvar gnus-newsgroup-last-file nil)
1864 (defvar gnus-newsgroup-auto-expire nil)
1865 (defvar gnus-newsgroup-active nil)
1866
1867 (defvar gnus-newsgroup-data nil)
1868 (defvar gnus-newsgroup-data-reverse nil)
1869 (defvar gnus-newsgroup-limit nil)
1870 (defvar gnus-newsgroup-limits nil)
1871
1872 (defvar gnus-newsgroup-unreads nil
1873   "List of unread articles in the current newsgroup.")
1874
1875 (defvar gnus-newsgroup-unselected nil
1876   "List of unselected unread articles in the current newsgroup.")
1877
1878 (defvar gnus-newsgroup-reads nil
1879   "Alist of read articles and article marks in the current newsgroup.")
1880
1881 (defvar gnus-newsgroup-expunged-tally nil)
1882
1883 (defvar gnus-newsgroup-marked nil
1884   "List of ticked articles in the current newsgroup (a subset of unread art).")
1885
1886 (defvar gnus-newsgroup-killed nil
1887   "List of ranges of articles that have been through the scoring process.")
1888
1889 (defvar gnus-newsgroup-cached nil
1890   "List of articles that come from the article cache.")
1891
1892 (defvar gnus-newsgroup-saved nil
1893   "List of articles that have been saved.")
1894
1895 (defvar gnus-newsgroup-kill-headers nil)
1896
1897 (defvar gnus-newsgroup-replied nil
1898   "List of articles that have been replied to in the current newsgroup.")
1899
1900 (defvar gnus-newsgroup-expirable nil
1901   "List of articles in the current newsgroup that can be expired.")
1902
1903 (defvar gnus-newsgroup-processable nil
1904   "List of articles in the current newsgroup that can be processed.")
1905
1906 (defvar gnus-newsgroup-bookmarks nil
1907   "List of articles in the current newsgroup that have bookmarks.")
1908
1909 (defvar gnus-newsgroup-dormant nil
1910   "List of dormant articles in the current newsgroup.")
1911
1912 (defvar gnus-newsgroup-scored nil
1913   "List of scored articles in the current newsgroup.")
1914
1915 (defvar gnus-newsgroup-headers nil
1916   "List of article headers in the current newsgroup.")
1917
1918 (defvar gnus-newsgroup-threads nil)
1919
1920 (defvar gnus-newsgroup-prepared nil
1921   "Whether the current group has been prepared properly.")
1922
1923 (defvar gnus-newsgroup-ancient nil
1924   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1925
1926 (defvar gnus-newsgroup-sparse nil)
1927
1928 (defvar gnus-current-article nil)
1929 (defvar gnus-article-current nil)
1930 (defvar gnus-current-headers nil)
1931 (defvar gnus-have-all-headers nil)
1932 (defvar gnus-last-article nil)
1933 (defvar gnus-newsgroup-history nil)
1934 (defvar gnus-current-kill-article nil)
1935
1936 ;; Save window configuration.
1937 (defvar gnus-prev-winconf nil)
1938
1939 (defvar gnus-summary-mark-positions nil)
1940 (defvar gnus-group-mark-positions nil)
1941
1942 (defvar gnus-reffed-article-number nil)
1943
1944 ;;; Let the byte-compiler know that we know about this variable.
1945 (defvar rmail-default-rmail-file)
1946
1947 (defvar gnus-cache-removable-articles nil)
1948
1949 (defvar gnus-dead-summary nil)
1950
1951 (defconst gnus-summary-local-variables
1952   '(gnus-newsgroup-name
1953     gnus-newsgroup-begin gnus-newsgroup-end
1954     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1955     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1956     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1957     gnus-newsgroup-unselected gnus-newsgroup-marked
1958     gnus-newsgroup-reads gnus-newsgroup-saved
1959     gnus-newsgroup-replied gnus-newsgroup-expirable
1960     gnus-newsgroup-processable gnus-newsgroup-killed
1961     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1962     gnus-newsgroup-headers gnus-newsgroup-threads
1963     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1964     gnus-current-article gnus-current-headers gnus-have-all-headers
1965     gnus-last-article gnus-article-internal-prepare-hook
1966     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1967     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1968     gnus-newsgroup-async gnus-thread-expunge-below
1969     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1970     (gnus-summary-mark-below . global)
1971     gnus-newsgroup-active gnus-scores-exclude-files
1972     gnus-newsgroup-history gnus-newsgroup-ancient
1973     gnus-newsgroup-sparse
1974     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1975     gnus-newsgroup-adaptive-score-file
1976     (gnus-newsgroup-expunged-tally . 0)
1977     gnus-cache-removable-articles gnus-newsgroup-cached
1978     gnus-newsgroup-data gnus-newsgroup-data-reverse
1979     gnus-newsgroup-limit gnus-newsgroup-limits)
1980   "Variables that are buffer-local to the summary buffers.")
1981
1982 (defconst gnus-bug-message
1983   "Sending a bug report to the Gnus Towers.
1984 ========================================
1985
1986 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1987 be sent to the Gnus Bug Exterminators.
1988
1989 At the bottom of the buffer you'll see lots of variable settings.
1990 Please do not delete those.  They will tell the Bug People what your
1991 environment is, so that it will be easier to locate the bugs.
1992
1993 If you have found a bug that makes Emacs go \"beep\", set
1994 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1995 and include the backtrace in your bug report.
1996
1997 Please describe the bug in annoying, painstaking detail.
1998
1999 Thank you for your help in stamping out bugs.
2000 ")
2001
2002 ;;; End of variables.
2003
2004 ;; Define some autoload functions Gnus might use.
2005 (eval-and-compile
2006
2007   ;; This little mapcar goes through the list below and marks the
2008   ;; symbols in question as autoloaded functions.
2009   (mapcar
2010    (lambda (package)
2011      (let ((interactive (nth 1 (memq ':interactive package))))
2012        (mapcar
2013         (lambda (function)
2014           (let (keymap)
2015             (when (consp function)
2016               (setq keymap (car (memq 'keymap function)))
2017               (setq function (car function)))
2018             (autoload function (car package) nil interactive keymap)))
2019         (if (eq (nth 1 package) ':interactive)
2020             (cdddr package)
2021           (cdr package)))))
2022    '(("metamail" metamail-buffer)
2023      ("info" Info-goto-node)
2024      ("hexl" hexl-hex-string-to-integer)
2025      ("pp" pp pp-to-string pp-eval-expression)
2026      ("mail-extr" mail-extract-address-components)
2027      ("nnmail" nnmail-split-fancy nnmail-article-group)
2028      ("nnvirtual" nnvirtual-catchup-group)
2029      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
2030       timezone-make-sortable-date timezone-make-time-string)
2031      ("rmailout" rmail-output)
2032      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2033       rmail-show-message)
2034      ("gnus-soup" :interactive t
2035       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2036       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2037      ("nnsoup" nnsoup-pack-replies)
2038      ("gnus-scomo" :interactive t gnus-score-mode)
2039      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2040       gnus-Folder-save-name gnus-folder-save-name)
2041      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2042      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2043       gnus-server-make-menu-bar gnus-article-make-menu-bar
2044       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2045       gnus-summary-highlight-line gnus-carpal-setup-buffer
2046       gnus-group-highlight-line
2047       gnus-article-add-button gnus-insert-next-page-button
2048       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2049      ("gnus-vis" :interactive t
2050       gnus-article-push-button gnus-article-press-button
2051       gnus-article-highlight gnus-article-highlight-some
2052       gnus-article-highlight-headers gnus-article-highlight-signature
2053       gnus-article-add-buttons gnus-article-add-buttons-to-head
2054       gnus-article-next-button gnus-article-prev-button)
2055      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2056       gnus-demon-add-disconnection gnus-demon-add-handler
2057       gnus-demon-remove-handler)
2058      ("gnus-demon" :interactive t
2059       gnus-demon-init gnus-demon-cancel)
2060      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2061       gnus-tree-open gnus-tree-close)
2062      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2063       gnus-nocem-unwanted-article-p)
2064      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2065      ("gnus-srvr" gnus-browse-foreign-server)
2066      ("gnus-cite" :interactive t
2067       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2068       gnus-article-hide-citation gnus-article-fill-cited-article
2069       gnus-article-hide-citation-in-followups)
2070      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2071       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2072       gnus-execute gnus-expunge)
2073      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2074       gnus-cache-possibly-remove-articles gnus-cache-request-article
2075       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2076       gnus-cache-enter-remove-article gnus-cached-article-p
2077       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2078      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2079       gnus-cache-remove-article)
2080      ("gnus-score" :interactive t
2081       gnus-summary-increase-score gnus-summary-lower-score
2082       gnus-score-flush-cache gnus-score-close
2083       gnus-score-raise-same-subject-and-select
2084       gnus-score-raise-same-subject gnus-score-default
2085       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2086       gnus-score-lower-same-subject gnus-score-lower-thread
2087       gnus-possibly-score-headers gnus-summary-raise-score 
2088       gnus-summary-set-score gnus-summary-current-score)
2089      ("gnus-score"
2090       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2091       gnus-current-score-file-nondirectory gnus-score-adaptive
2092       gnus-score-find-trace gnus-score-file-name)
2093      ("gnus-edit" :interactive t gnus-score-customize)
2094      ("gnus-topic" :interactive t gnus-topic-mode)
2095      ("gnus-topic" gnus-topic-remove-group)
2096      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2097      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2098      ("gnus-uu" :interactive t
2099       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2100       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2101       gnus-uu-mark-by-regexp gnus-uu-mark-all
2102       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2103       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2104       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2105       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2106       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2107       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2108       gnus-uu-decode-binhex-view)
2109      ("gnus-msg" (gnus-summary-send-map keymap)
2110       gnus-mail-yank-original gnus-mail-send-and-exit
2111       gnus-article-mail gnus-new-mail gnus-mail-reply)
2112      ("gnus-msg" :interactive t
2113       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2114       gnus-summary-followup gnus-summary-followup-with-original
2115       gnus-summary-cancel-article gnus-summary-supersede-article
2116       gnus-post-news gnus-inews-news 
2117       gnus-summary-reply gnus-summary-reply-with-original
2118       gnus-summary-mail-forward gnus-summary-mail-other-window
2119       gnus-bug)
2120      ("gnus-picon" :interactive t gnus-article-display-picons
2121       gnus-group-display-picons gnus-picons-article-display-x-face
2122       gnus-picons-display-x-face)
2123      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2124       gnus-grouplens-mode)
2125      ("smiley" :interactive t gnus-smiley-display)
2126      ("gnus-vm" gnus-vm-mail-setup)
2127      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2128       gnus-summary-save-article-vm))))
2129
2130 \f
2131
2132 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2133 ;; If you want the cursor to go somewhere else, set these two
2134 ;; functions in some startup hook to whatever you want.
2135 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2136 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2137
2138 ;;; Various macros and substs.
2139
2140 (defun gnus-header-from (header)
2141   (mail-header-from header))
2142
2143 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2144   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2145   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2146         (w (make-symbol "w"))
2147         (buf (make-symbol "buf")))
2148     `(let* ((,tempvar (selected-window))
2149             (,buf ,buffer)
2150             (,w (get-buffer-window ,buf 'visible)))
2151        (unwind-protect
2152            (progn
2153              (if ,w
2154                  (select-window ,w)
2155                (pop-to-buffer ,buf))
2156              ,@forms)
2157          (select-window ,tempvar)))))
2158
2159 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
2160 (put 'gnus-eval-in-buffer-window 'lisp-indent-hook 1)
2161 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
2162
2163 (defmacro gnus-gethash (string hashtable)
2164   "Get hash value of STRING in HASHTABLE."
2165   `(symbol-value (intern-soft ,string ,hashtable)))
2166
2167 (defmacro gnus-sethash (string value hashtable)
2168   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2169   `(set (intern ,string ,hashtable) ,value))
2170
2171 (defmacro gnus-intern-safe (string hashtable)
2172   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2173   `(let ((symbol (intern ,string ,hashtable)))
2174      (or (boundp symbol)
2175          (set symbol nil))
2176      symbol))
2177
2178 (defmacro gnus-group-unread (group)
2179   "Get the currently computed number of unread articles in GROUP."
2180   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2181
2182 (defmacro gnus-group-entry (group)
2183   "Get the newsrc entry for GROUP."
2184   `(gnus-gethash ,group gnus-newsrc-hashtb))
2185
2186 (defmacro gnus-active (group)
2187   "Get active info on GROUP."
2188   `(gnus-gethash ,group gnus-active-hashtb))
2189
2190 (defmacro gnus-set-active (group active)
2191   "Set GROUP's active info."
2192   `(gnus-sethash ,group ,active gnus-active-hashtb))
2193
2194 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2195 ;;   function `substring' might cut on a middle of multi-octet
2196 ;;   character.
2197 (defun gnus-truncate-string (str width)
2198   (substring str 0 width))
2199
2200 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2201 ;; to limit the length of a string.  This function is necessary since
2202 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2203 (defsubst gnus-limit-string (str width)
2204   (if (> (length str) width)
2205       (substring str 0 width)
2206     str))
2207
2208 (defsubst gnus-simplify-subject-re (subject)
2209   "Remove \"Re:\" from subject lines."
2210   (if (string-match "^[Rr][Ee]: *" subject)
2211       (substring subject (match-end 0))
2212     subject))
2213
2214 (defsubst gnus-functionp (form)
2215   "Return non-nil if FORM is funcallable."
2216   (or (and (symbolp form) (fboundp form))
2217       (and (listp form) (eq (car form) 'lambda))))
2218
2219 (defsubst gnus-goto-char (point)
2220   (and point (goto-char point)))
2221
2222 (defmacro gnus-buffer-exists-p (buffer)
2223   `(let ((buffer ,buffer))
2224      (and buffer
2225           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2226                    buffer))))
2227
2228 (defmacro gnus-kill-buffer (buffer)
2229   `(let ((buf ,buffer))
2230      (if (gnus-buffer-exists-p buf)
2231          (kill-buffer buf))))
2232
2233 (defsubst gnus-point-at-bol ()
2234   "Return point at the beginning of the line."
2235   (let ((p (point)))
2236     (beginning-of-line)
2237     (prog1
2238         (point)
2239       (goto-char p))))
2240
2241 (defsubst gnus-point-at-eol ()
2242   "Return point at the end of the line."
2243   (let ((p (point)))
2244     (end-of-line)
2245     (prog1
2246         (point)
2247       (goto-char p))))
2248
2249 (defun gnus-alive-p ()
2250   "Say whether Gnus is running or not."
2251   (and gnus-group-buffer
2252        (get-buffer gnus-group-buffer)))
2253
2254 (defun gnus-delete-first (elt list)
2255   "Delete by side effect the first occurrence of ELT as a member of LIST."
2256   (if (equal (car list) elt)
2257       (cdr list)
2258     (let ((total list))
2259       (while (and (cdr list)
2260                   (not (equal (cadr list) elt)))
2261         (setq list (cdr list)))
2262       (when (cdr list)
2263         (setcdr list (cddr list)))
2264       total)))
2265
2266 ;; Delete the current line (and the next N lines.);
2267 (defmacro gnus-delete-line (&optional n)
2268   `(delete-region (progn (beginning-of-line) (point))
2269                   (progn (forward-line ,(or n 1)) (point))))
2270
2271 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2272 (defvar gnus-init-inhibit nil)
2273 (defun gnus-read-init-file (&optional inhibit-next)
2274   (if gnus-init-inhibit
2275       (setq gnus-init-inhibit nil)
2276     (setq gnus-init-inhibit inhibit-next)
2277     (and gnus-init-file
2278          (or (and (file-exists-p gnus-init-file)
2279                   ;; Don't try to load a directory.
2280                   (not (file-directory-p gnus-init-file)))
2281              (file-exists-p (concat gnus-init-file ".el"))
2282              (file-exists-p (concat gnus-init-file ".elc")))
2283          (condition-case var
2284              (load gnus-init-file nil t)
2285            (error
2286             (error "Error in %s: %s" gnus-init-file var))))))
2287
2288 ;; Info access macros.
2289
2290 (defmacro gnus-info-group (info)
2291   `(nth 0 ,info))
2292 (defmacro gnus-info-rank (info)
2293   `(nth 1 ,info))
2294 (defmacro gnus-info-read (info)
2295   `(nth 2 ,info))
2296 (defmacro gnus-info-marks (info)
2297   `(nth 3 ,info))
2298 (defmacro gnus-info-method (info)
2299   `(nth 4 ,info))
2300 (defmacro gnus-info-params (info)
2301   `(nth 5 ,info))
2302
2303 (defmacro gnus-info-level (info)
2304   `(let ((rank (gnus-info-rank ,info)))
2305      (if (consp rank)
2306          (car rank)
2307        rank)))
2308 (defmacro gnus-info-score (info)
2309   `(let ((rank (gnus-info-rank ,info)))
2310      (or (and (consp rank) (cdr rank)) 0)))
2311
2312 (defmacro gnus-info-set-group (info group)
2313   `(setcar ,info ,group))
2314 (defmacro gnus-info-set-rank (info rank)
2315   `(setcar (nthcdr 1 ,info) ,rank))
2316 (defmacro gnus-info-set-read (info read)
2317   `(setcar (nthcdr 2 ,info) ,read))
2318 (defmacro gnus-info-set-marks (info marks)
2319   `(setcar (nthcdr 3 ,info) ,marks))
2320 (defmacro gnus-info-set-method (info method)
2321   `(setcar (nthcdr 4 ,info) ,method))
2322 (defmacro gnus-info-set-params (info params)
2323   `(setcar (nthcdr 5 ,info) ,params))
2324
2325 (defmacro gnus-info-set-level (info level)
2326   `(let ((rank (cdr ,info)))
2327      (if (consp (car rank))
2328          (setcar (car rank) ,level)
2329        (setcar rank ,level))))
2330 (defmacro gnus-info-set-score (info score)
2331   `(let ((rank (cdr ,info)))
2332      (if (consp (car rank))
2333          (setcdr (car rank) ,score)
2334        (setcar rank (cons (car rank) ,score)))))
2335
2336 (defmacro gnus-get-info (group)
2337   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2338
2339 (defun gnus-byte-code (func)
2340   "Return a form that can be `eval'ed based on FUNC."
2341   (let ((fval (symbol-function func)))
2342     (if (byte-code-function-p fval)
2343         (let ((flist (append fval nil)))
2344           (setcar flist 'byte-code)
2345           flist)
2346       (cons 'progn (cddr fval)))))
2347
2348 ;; Find out whether the gnus-visual TYPE is wanted.
2349 (defun gnus-visual-p (&optional type class)
2350   (and gnus-visual                      ; Has to be non-nil, at least.
2351        (if (not type)                   ; We don't care about type.
2352            gnus-visual
2353          (if (listp gnus-visual)        ; It's a list, so we check it.
2354              (or (memq type gnus-visual)
2355                  (memq class gnus-visual))
2356            t))))
2357
2358 ;;; Load the compatability functions.
2359
2360 (require 'gnus-cus)
2361 (require 'gnus-ems)
2362
2363 \f
2364 ;;;
2365 ;;; Shutdown
2366 ;;;
2367
2368 (defvar gnus-shutdown-alist nil)
2369
2370 (defun gnus-add-shutdown (function &rest symbols)
2371   "Run FUNCTION whenever one of SYMBOLS is shut down."
2372   (push (cons function symbols) gnus-shutdown-alist))
2373
2374 (defun gnus-shutdown (symbol)
2375   "Shut down everything that waits for SYMBOL."
2376   (let ((alist gnus-shutdown-alist)
2377         entry)
2378     (while (setq entry (pop alist))
2379       (when (memq symbol (cdr entry))
2380         (funcall (car entry))))))
2381
2382 \f
2383
2384 ;; Format specs.  The chunks below are the machine-generated forms
2385 ;; that are to be evaled as the result of the default format strings.
2386 ;; We write them in here to get them byte-compiled.  That way the
2387 ;; default actions will be quite fast, while still retaining the full
2388 ;; flexibility of the user-defined format specs.
2389
2390 ;; First we have lots of dummy defvars to let the compiler know these
2391 ;; are really dynamic variables.
2392
2393 (defvar gnus-tmp-unread)
2394 (defvar gnus-tmp-replied)
2395 (defvar gnus-tmp-score-char)
2396 (defvar gnus-tmp-indentation)
2397 (defvar gnus-tmp-opening-bracket)
2398 (defvar gnus-tmp-lines)
2399 (defvar gnus-tmp-name)
2400 (defvar gnus-tmp-closing-bracket)
2401 (defvar gnus-tmp-subject-or-nil)
2402 (defvar gnus-tmp-subject)
2403 (defvar gnus-tmp-marked)
2404 (defvar gnus-tmp-marked-mark)
2405 (defvar gnus-tmp-subscribed)
2406 (defvar gnus-tmp-process-marked)
2407 (defvar gnus-tmp-number-of-unread)
2408 (defvar gnus-tmp-group-name)
2409 (defvar gnus-tmp-group)
2410 (defvar gnus-tmp-article-number)
2411 (defvar gnus-tmp-unread-and-unselected)
2412 (defvar gnus-tmp-news-method)
2413 (defvar gnus-tmp-news-server)
2414 (defvar gnus-tmp-article-number)
2415 (defvar gnus-mouse-face)
2416 (defvar gnus-mouse-face-prop)
2417
2418 (defun gnus-summary-line-format-spec ()
2419   (insert gnus-tmp-unread gnus-tmp-replied
2420           gnus-tmp-score-char gnus-tmp-indentation)
2421   (gnus-put-text-property
2422    (point)
2423    (progn
2424      (insert
2425       gnus-tmp-opening-bracket
2426       (format "%4d: %-20s"
2427               gnus-tmp-lines
2428               (if (> (length gnus-tmp-name) 20)
2429                   (substring gnus-tmp-name 0 20)
2430                 gnus-tmp-name))
2431       gnus-tmp-closing-bracket)
2432      (point))
2433    gnus-mouse-face-prop gnus-mouse-face)
2434   (insert " " gnus-tmp-subject-or-nil "\n"))
2435
2436 (defvar gnus-summary-line-format-spec
2437   (gnus-byte-code 'gnus-summary-line-format-spec))
2438
2439 (defun gnus-summary-dummy-line-format-spec ()
2440   (insert "*  ")
2441   (gnus-put-text-property
2442    (point)
2443    (progn
2444      (insert ":                          :")
2445      (point))
2446    gnus-mouse-face-prop gnus-mouse-face)
2447   (insert " " gnus-tmp-subject "\n"))
2448
2449 (defvar gnus-summary-dummy-line-format-spec
2450   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2451
2452 (defun gnus-group-line-format-spec ()
2453   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2454           gnus-tmp-process-marked
2455           gnus-group-indentation
2456           (format "%5s: " gnus-tmp-number-of-unread))
2457   (gnus-put-text-property
2458    (point)
2459    (progn
2460      (insert gnus-tmp-group "\n")
2461      (1- (point)))
2462    gnus-mouse-face-prop gnus-mouse-face))
2463 (defvar gnus-group-line-format-spec
2464   (gnus-byte-code 'gnus-group-line-format-spec))
2465
2466 (defvar gnus-format-specs
2467   `((version . ,emacs-version)
2468     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2469     (summary-dummy ,gnus-summary-dummy-line-format
2470                    ,gnus-summary-dummy-line-format-spec)
2471     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2472
2473 (defvar gnus-article-mode-line-format-spec nil)
2474 (defvar gnus-summary-mode-line-format-spec nil)
2475 (defvar gnus-group-mode-line-format-spec nil)
2476
2477 ;;; Phew.  All that gruft is over, fortunately.
2478
2479 \f
2480 ;;;
2481 ;;; Gnus Utility Functions
2482 ;;;
2483
2484 (defun gnus-extract-address-components (from)
2485   (let (name address)
2486     ;; First find the address - the thing with the @ in it.  This may
2487     ;; not be accurate in mail addresses, but does the trick most of
2488     ;; the time in news messages.
2489     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2490         (setq address (substring from (match-beginning 0) (match-end 0))))
2491     ;; Then we check whether the "name <address>" format is used.
2492     (and address
2493          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2494          ;; Linear white space is not required.
2495          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2496          (and (setq name (substring from 0 (match-beginning 0)))
2497               ;; Strip any quotes from the name.
2498               (string-match "\".*\"" name)
2499               (setq name (substring name 1 (1- (match-end 0))))))
2500     ;; If not, then "address (name)" is used.
2501     (or name
2502         (and (string-match "(.+)" from)
2503              (setq name (substring from (1+ (match-beginning 0))
2504                                    (1- (match-end 0)))))
2505         (and (string-match "()" from)
2506              (setq name address))
2507         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2508         ;; XOVER might not support folded From headers.
2509         (and (string-match "(.*" from)
2510              (setq name (substring from (1+ (match-beginning 0))
2511                                    (match-end 0)))))
2512     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2513     (list (or name from) (or address from))))
2514
2515 (defun gnus-fetch-field (field)
2516   "Return the value of the header FIELD of current article."
2517   (save-excursion
2518     (save-restriction
2519       (let ((case-fold-search t)
2520             (inhibit-point-motion-hooks t))
2521         (nnheader-narrow-to-headers)
2522         (message-fetch-field field)))))
2523
2524 (defun gnus-goto-colon ()
2525   (beginning-of-line)
2526   (search-forward ":" (gnus-point-at-eol) t))
2527
2528 ;;;###autoload
2529 (defun gnus-update-format (var)
2530   "Update the format specification near point."
2531   (interactive
2532    (list
2533     (save-excursion
2534       (eval-defun nil)
2535       ;; Find the end of the current word.
2536       (re-search-forward "[ \t\n]" nil t)
2537       ;; Search backward.
2538       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2539         (match-string 1)))))
2540   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2541                               (match-string 1 var))))
2542          (entry (assq type gnus-format-specs))
2543          value spec)
2544     (when entry
2545       (setq gnus-format-specs (delq entry gnus-format-specs)))
2546     (set
2547      (intern (format "%s-spec" var))
2548      (gnus-parse-format (setq value (symbol-value (intern var)))
2549                         (symbol-value (intern (format "%s-alist" var)))
2550                         (not (string-match "mode" var))))
2551     (setq spec (symbol-value (intern (format "%s-spec" var))))
2552     (push (list type value spec) gnus-format-specs)
2553
2554     (pop-to-buffer "*Gnus Format*")
2555     (erase-buffer)
2556     (lisp-interaction-mode)
2557     (insert (pp-to-string spec))))
2558
2559 (defun gnus-update-format-specifications (&optional force)
2560   "Update all (necessary) format specifications."
2561   ;; Make the indentation array.
2562   (gnus-make-thread-indent-array)
2563
2564   ;; See whether all the stored info needs to be flushed.
2565   (when (or force
2566             (not (equal emacs-version
2567                         (cdr (assq 'version gnus-format-specs)))))
2568     (setq gnus-format-specs nil))
2569
2570   ;; Go through all the formats and see whether they need updating.
2571   (let ((types '(summary summary-dummy group
2572                          summary-mode group-mode article-mode))
2573         new-format entry type val)
2574     (while (setq type (pop types))
2575       ;; Jump to the proper buffer to find out the value of
2576       ;; the variable, if possible.  (It may be buffer-local.)
2577       (save-excursion
2578         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2579               val)
2580           (when (and (boundp buffer)
2581                      (setq val (symbol-value buffer))
2582                      (get-buffer val)
2583                      (buffer-name (get-buffer val)))
2584             (set-buffer (get-buffer val)))
2585           (setq new-format (symbol-value
2586                             (intern (format "gnus-%s-line-format" type))))))
2587       (setq entry (cdr (assq type gnus-format-specs)))
2588       (if (and entry
2589                (equal (car entry) new-format))
2590           ;; Use the old format.
2591           (set (intern (format "gnus-%s-line-format-spec" type))
2592                (cadr entry))
2593         ;; This is a new format.
2594         (setq val
2595               (if (not (stringp new-format))
2596                   ;; This is a function call or something.
2597                   new-format
2598                 ;; This is a "real" format.
2599                 (gnus-parse-format
2600                  new-format
2601                  (symbol-value
2602                   (intern (format "gnus-%s-line-format-alist"
2603                                   (if (eq type 'article-mode)
2604                                       'summary-mode type))))
2605                  (not (string-match "mode$" (symbol-name type))))))
2606         ;; Enter the new format spec into the list.
2607         (if entry
2608             (progn
2609               (setcar (cdr entry) val)
2610               (setcar entry new-format))
2611           (push (list type new-format val) gnus-format-specs))
2612         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2613
2614   (unless (assq 'version gnus-format-specs)
2615     (push (cons 'version emacs-version) gnus-format-specs))
2616
2617   (gnus-update-group-mark-positions)
2618   (gnus-update-summary-mark-positions))
2619
2620 (defun gnus-update-summary-mark-positions ()
2621   "Compute where the summary marks are to go."
2622   (save-excursion
2623     (when (and gnus-summary-buffer
2624                (get-buffer gnus-summary-buffer)
2625                (buffer-name (get-buffer gnus-summary-buffer)))
2626       (set-buffer gnus-summary-buffer))
2627     (let ((gnus-replied-mark 129)
2628           (gnus-score-below-mark 130)
2629           (gnus-score-over-mark 130)
2630           (thread nil)
2631           (gnus-visual nil)
2632           (spec gnus-summary-line-format-spec)
2633           pos)
2634       (save-excursion
2635         (gnus-set-work-buffer)
2636         (let ((gnus-summary-line-format-spec spec))
2637           (gnus-summary-insert-line
2638            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2639           (goto-char (point-min))
2640           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2641                                              (- (point) 2)))))
2642           (goto-char (point-min))
2643           (push (cons 'replied (and (search-forward "\201" nil t) 
2644                                     (- (point) 2)))
2645                 pos)
2646           (goto-char (point-min))
2647           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2648                 pos)))
2649       (setq gnus-summary-mark-positions pos))))
2650
2651 (defun gnus-update-group-mark-positions ()
2652   (save-excursion
2653     (let ((gnus-process-mark 128)
2654           (gnus-group-marked '("dummy.group"))
2655           (gnus-active-hashtb (make-vector 10 0)))
2656       (gnus-set-active "dummy.group" '(0 . 0))
2657       (gnus-set-work-buffer)
2658       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2659       (goto-char (point-min))
2660       (setq gnus-group-mark-positions
2661             (list (cons 'process (and (search-forward "\200" nil t)
2662                                       (- (point) 2))))))))
2663
2664 (defvar gnus-mouse-face-0 'highlight)
2665 (defvar gnus-mouse-face-1 'highlight)
2666 (defvar gnus-mouse-face-2 'highlight)
2667 (defvar gnus-mouse-face-3 'highlight)
2668 (defvar gnus-mouse-face-4 'highlight)
2669
2670 (defun gnus-mouse-face-function (form type)
2671   `(gnus-put-text-property
2672     (point) (progn ,@form (point))
2673     gnus-mouse-face-prop
2674     ,(if (equal type 0)
2675          'gnus-mouse-face
2676        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2677
2678 (defvar gnus-face-0 'bold)
2679 (defvar gnus-face-1 'italic)
2680 (defvar gnus-face-2 'bold-italic)
2681 (defvar gnus-face-3 'bold)
2682 (defvar gnus-face-4 'bold)
2683
2684 (defun gnus-face-face-function (form type)
2685   `(gnus-put-text-property
2686     (point) (progn ,@form (point))
2687     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2688
2689 (defun gnus-max-width-function (el max-width)
2690   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2691   (if (symbolp el)
2692       `(if (> (length ,el) ,max-width)
2693            (substring ,el 0 ,max-width)
2694          ,el)
2695     `(let ((val (eval ,el)))
2696        (if (numberp val)
2697            (setq val (int-to-string val)))
2698        (if (> (length val) ,max-width)
2699            (substring val 0 ,max-width)
2700          val))))
2701
2702 (defun gnus-parse-format (format spec-alist &optional insert)
2703   ;; This function parses the FORMAT string with the help of the
2704   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2705   ;; string.  If the FORMAT string contains the specifiers %( and %)
2706   ;; the text between them will have the mouse-face text property.
2707   (if (string-match
2708        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2709        format)
2710       (gnus-parse-complex-format format spec-alist)
2711     ;; This is a simple format.
2712     (gnus-parse-simple-format format spec-alist insert)))
2713
2714 (defun gnus-parse-complex-format (format spec-alist)
2715   (save-excursion
2716     (gnus-set-work-buffer)
2717     (insert format)
2718     (goto-char (point-min))
2719     (while (re-search-forward "\"" nil t)
2720       (replace-match "\\\"" nil t))
2721     (goto-char (point-min))
2722     (insert "(\"")
2723     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2724       (let ((number (if (match-beginning 1)
2725                         (match-string 1) "0"))
2726             (delim (aref (match-string 2) 0)))
2727         (if (or (= delim ?\() (= delim ?\{))
2728             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2729                                    " " number " \""))
2730           (replace-match "\")\""))))
2731     (goto-char (point-max))
2732     (insert "\")")
2733     (goto-char (point-min))
2734     (let ((form (read (current-buffer))))
2735       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2736
2737 (defun gnus-complex-form-to-spec (form spec-alist)
2738   (delq nil
2739         (mapcar
2740          (lambda (sform)
2741            (if (stringp sform)
2742                (gnus-parse-simple-format sform spec-alist t)
2743              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2744                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2745                       (nth 1 sform))))
2746          form)))
2747
2748 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2749   ;; This function parses the FORMAT string with the help of the
2750   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2751   ;; string.
2752   (let ((max-width 0)
2753         spec flist fstring newspec elem beg result dontinsert)
2754     (save-excursion
2755       (gnus-set-work-buffer)
2756       (insert format)
2757       (goto-char (point-min))
2758       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2759                                 nil t)
2760         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2761               (setq newspec "%"
2762                     beg (1+ (match-beginning 0)))
2763           ;; First check if there are any specs that look anything like
2764           ;; "%12,12A", ie. with a "max width specification".  These have
2765           ;; to be treated specially.
2766           (if (setq beg (match-beginning 1))
2767               (setq max-width
2768                     (string-to-int
2769                      (buffer-substring
2770                       (1+ (match-beginning 1)) (match-end 1))))
2771             (setq max-width 0)
2772             (setq beg (match-beginning 2)))
2773           ;; Find the specification from `spec-alist'.
2774           (unless (setq elem (cdr (assq spec spec-alist)))
2775             (setq elem '("*" ?s)))
2776           ;; Treat user defined format specifiers specially.
2777           (when (eq (car elem) 'gnus-tmp-user-defined)
2778             (setq elem
2779                   (list
2780                    (list (intern (concat "gnus-user-format-function-"
2781                                          (match-string 3)))
2782                          'gnus-tmp-header) ?s))
2783             (delete-region (match-beginning 3) (match-end 3)))
2784           (if (not (zerop max-width))
2785               (let ((el (car elem)))
2786                 (cond ((= (cadr elem) ?c)
2787                        (setq el (list 'char-to-string el)))
2788                       ((= (cadr elem) ?d)
2789                        (setq el (list 'int-to-string el))))
2790                 (setq flist (cons (gnus-max-width-function el max-width)
2791                                   flist))
2792                 (setq newspec ?s))
2793             (progn
2794               (setq flist (cons (car elem) flist))
2795               (setq newspec (cadr elem)))))
2796         ;; Remove the old specification (and possibly a ",12" string).
2797         (delete-region beg (match-end 2))
2798         ;; Insert the new specification.
2799         (goto-char beg)
2800         (insert newspec))
2801       (setq fstring (buffer-substring 1 (point-max))))
2802     ;; Do some postprocessing to increase efficiency.
2803     (setq
2804      result
2805      (cond
2806       ;; Emptyness.
2807       ((string= fstring "")
2808        nil)
2809       ;; Not a format string.
2810       ((not (string-match "%" fstring))
2811        (list fstring))
2812       ;; A format string with just a single string spec.
2813       ((string= fstring "%s")
2814        (list (car flist)))
2815       ;; A single character.
2816       ((string= fstring "%c")
2817        (list (car flist)))
2818       ;; A single number.
2819       ((string= fstring "%d")
2820        (setq dontinsert)
2821        (if insert
2822            (list `(princ ,(car flist)))
2823          (list `(int-to-string ,(car flist)))))
2824       ;; Just lots of chars and strings.
2825       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2826        (nreverse flist))
2827       ;; A single string spec at the beginning of the spec.
2828       ((string-match "\\`%[sc][^%]+\\'" fstring)
2829        (list (car flist) (substring fstring 2)))
2830       ;; A single string spec in the middle of the spec.
2831       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2832        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2833       ;; A single string spec in the end of the spec.
2834       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2835        (list (match-string 1 fstring) (car flist)))
2836       ;; A more complex spec.
2837       (t
2838        (list (cons 'format (cons fstring (nreverse flist)))))))
2839
2840     (if insert
2841         (when result
2842           (if dontinsert
2843               result
2844             (cons 'insert result)))
2845       (cond ((stringp result)
2846              result)
2847             ((consp result)
2848              (cons 'concat result))
2849             (t "")))))
2850
2851 (defun gnus-eval-format (format &optional alist props)
2852   "Eval the format variable FORMAT, using ALIST.
2853 If PROPS, insert the result."
2854   (let ((form (gnus-parse-format format alist props)))
2855     (if props
2856         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2857       (eval form))))
2858
2859 (defun gnus-remove-text-with-property (prop)
2860   "Delete all text in the current buffer with text property PROP."
2861   (save-excursion
2862     (goto-char (point-min))
2863     (while (not (eobp))
2864       (while (get-text-property (point) prop)
2865         (delete-char 1))
2866       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2867
2868 (defun gnus-set-work-buffer ()
2869   (if (get-buffer gnus-work-buffer)
2870       (progn
2871         (set-buffer gnus-work-buffer)
2872         (erase-buffer))
2873     (set-buffer (get-buffer-create gnus-work-buffer))
2874     (kill-all-local-variables)
2875     (buffer-disable-undo (current-buffer))
2876     (gnus-add-current-to-buffer-list)))
2877
2878 ;; Article file names when saving.
2879
2880 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2881   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2882 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2883 Otherwise, it is like ~/News/news/group/num."
2884   (let ((default
2885           (expand-file-name
2886            (concat (if (gnus-use-long-file-name 'not-save)
2887                        (gnus-capitalize-newsgroup newsgroup)
2888                      (gnus-newsgroup-directory-form newsgroup))
2889                    "/" (int-to-string (mail-header-number headers)))
2890            gnus-article-save-directory)))
2891     (if (and last-file
2892              (string-equal (file-name-directory default)
2893                            (file-name-directory last-file))
2894              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2895         default
2896       (or last-file default))))
2897
2898 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2899   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2900 If variable `gnus-use-long-file-name' is non-nil, it is
2901 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2902   (let ((default
2903           (expand-file-name
2904            (concat (if (gnus-use-long-file-name 'not-save)
2905                        newsgroup
2906                      (gnus-newsgroup-directory-form newsgroup))
2907                    "/" (int-to-string (mail-header-number headers)))
2908            gnus-article-save-directory)))
2909     (if (and last-file
2910              (string-equal (file-name-directory default)
2911                            (file-name-directory last-file))
2912              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2913         default
2914       (or last-file default))))
2915
2916 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2917   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2918 If variable `gnus-use-long-file-name' is non-nil, it is
2919 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2920   (or last-file
2921       (expand-file-name
2922        (if (gnus-use-long-file-name 'not-save)
2923            (gnus-capitalize-newsgroup newsgroup)
2924          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2925        gnus-article-save-directory)))
2926
2927 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2928   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2929 If variable `gnus-use-long-file-name' is non-nil, it is
2930 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2931   (or last-file
2932       (expand-file-name
2933        (if (gnus-use-long-file-name 'not-save)
2934            newsgroup
2935          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2936        gnus-article-save-directory)))
2937
2938 ;; For subscribing new newsgroup
2939
2940 (defun gnus-subscribe-hierarchical-interactive (groups)
2941   (let ((groups (sort groups 'string<))
2942         prefixes prefix start ans group starts)
2943     (while groups
2944       (setq prefixes (list "^"))
2945       (while (and groups prefixes)
2946         (while (not (string-match (car prefixes) (car groups)))
2947           (setq prefixes (cdr prefixes)))
2948         (setq prefix (car prefixes))
2949         (setq start (1- (length prefix)))
2950         (if (and (string-match "[^\\.]\\." (car groups) start)
2951                  (cdr groups)
2952                  (setq prefix
2953                        (concat "^" (substring (car groups) 0 (match-end 0))))
2954                  (string-match prefix (cadr groups)))
2955             (progn
2956               (setq prefixes (cons prefix prefixes))
2957               (message "Descend hierarchy %s? ([y]nsq): "
2958                        (substring prefix 1 (1- (length prefix))))
2959               (while (not (memq (setq ans (read-char)) '(?y ?\n ?n ?s ?q)))
2960                 (ding)
2961                 (message "Descend hierarchy %s? ([y]nsq): "
2962                          (substring prefix 1 (1- (length prefix)))))
2963               (cond ((= ans ?n)
2964                      (while (and groups
2965                                  (string-match prefix
2966                                                (setq group (car groups))))
2967                        (setq gnus-killed-list
2968                              (cons group gnus-killed-list))
2969                        (gnus-sethash group group gnus-killed-hashtb)
2970                        (setq groups (cdr groups)))
2971                      (setq starts (cdr starts)))
2972                     ((= ans ?s)
2973                      (while (and groups
2974                                  (string-match prefix
2975                                                (setq group (car groups))))
2976                        (gnus-sethash group group gnus-killed-hashtb)
2977                        (gnus-subscribe-alphabetically (car groups))
2978                        (setq groups (cdr groups)))
2979                      (setq starts (cdr starts)))
2980                     ((= ans ?q)
2981                      (while groups
2982                        (setq group (car groups))
2983                        (setq gnus-killed-list (cons group gnus-killed-list))
2984                        (gnus-sethash group group gnus-killed-hashtb)
2985                        (setq groups (cdr groups))))
2986                     (t nil)))
2987           (message "Subscribe %s? ([n]yq)" (car groups))
2988           (while (not (memq (setq ans (read-char)) '(?y ?\n ?q ?n)))
2989             (ding)
2990             (message "Subscribe %s? ([n]yq)" (car groups)))
2991           (setq group (car groups))
2992           (cond ((= ans ?y)
2993                  (gnus-subscribe-alphabetically (car groups))
2994                  (gnus-sethash group group gnus-killed-hashtb))
2995                 ((= ans ?q)
2996                  (while groups
2997                    (setq group (car groups))
2998                    (setq gnus-killed-list (cons group gnus-killed-list))
2999                    (gnus-sethash group group gnus-killed-hashtb)
3000                    (setq groups (cdr groups))))
3001                 (t
3002                  (setq gnus-killed-list (cons group gnus-killed-list))
3003                  (gnus-sethash group group gnus-killed-hashtb)))
3004           (setq groups (cdr groups)))))))
3005
3006 (defun gnus-subscribe-randomly (newsgroup)
3007   "Subscribe new NEWSGROUP by making it the first newsgroup."
3008   (gnus-subscribe-newsgroup newsgroup))
3009
3010 (defun gnus-subscribe-alphabetically (newgroup)
3011   "Subscribe new NEWSGROUP and insert it in alphabetical order."
3012   (let ((groups (cdr gnus-newsrc-alist))
3013         before)
3014     (while (and (not before) groups)
3015       (if (string< newgroup (caar groups))
3016           (setq before (caar groups))
3017         (setq groups (cdr groups))))
3018     (gnus-subscribe-newsgroup newgroup before)))
3019
3020 (defun gnus-subscribe-hierarchically (newgroup)
3021   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
3022   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
3023   (save-excursion
3024     (set-buffer (find-file-noselect gnus-current-startup-file))
3025     (let ((groupkey newgroup)
3026           before)
3027       (while (and (not before) groupkey)
3028         (goto-char (point-min))
3029         (let ((groupkey-re
3030                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
3031           (while (and (re-search-forward groupkey-re nil t)
3032                       (progn
3033                         (setq before (match-string 1))
3034                         (string< before newgroup)))))
3035         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
3036         (setq groupkey
3037               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
3038                   (substring groupkey (match-beginning 1) (match-end 1)))))
3039       (gnus-subscribe-newsgroup newgroup before))))
3040
3041 (defun gnus-subscribe-interactively (group)
3042   "Subscribe the new GROUP interactively.
3043 It is inserted in hierarchical newsgroup order if subscribed.  If not,
3044 it is killed."
3045   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
3046       (gnus-subscribe-hierarchically group)
3047     (push group gnus-killed-list)))
3048
3049 (defun gnus-subscribe-zombies (group)
3050   "Make the new GROUP into a zombie group."
3051   (push group gnus-zombie-list))
3052
3053 (defun gnus-subscribe-killed (group)
3054   "Make the new GROUP a killed group."
3055   (push group gnus-killed-list))
3056
3057 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
3058   "Subscribe new NEWSGROUP.
3059 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
3060 the first newsgroup."
3061   ;; We subscribe the group by changing its level to `subscribed'.
3062   (gnus-group-change-level
3063    newsgroup gnus-level-default-subscribed
3064    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3065   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3066
3067 ;; For directories
3068
3069 (defun gnus-newsgroup-directory-form (newsgroup)
3070   "Make hierarchical directory name from NEWSGROUP name."
3071   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3072         (len (length newsgroup))
3073         idx)
3074     ;; If this is a foreign group, we don't want to translate the
3075     ;; entire name.
3076     (if (setq idx (string-match ":" newsgroup))
3077         (aset newsgroup idx ?/)
3078       (setq idx 0))
3079     ;; Replace all occurrences of `.' with `/'.
3080     (while (< idx len)
3081       (if (= (aref newsgroup idx) ?.)
3082           (aset newsgroup idx ?/))
3083       (setq idx (1+ idx)))
3084     newsgroup))
3085
3086 (defun gnus-newsgroup-savable-name (group)
3087   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3088   ;; with dots.
3089   (nnheader-replace-chars-in-string group ?/ ?.))
3090
3091 (defun gnus-make-directory (dir)
3092   "Make DIRECTORY recursively."
3093   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3094   ;; of the many mysteries of the universe.
3095   (let* ((dir (expand-file-name dir default-directory))
3096          dirs err)
3097     (if (string-match "/$" dir)
3098         (setq dir (substring dir 0 (match-beginning 0))))
3099     ;; First go down the path until we find a directory that exists.
3100     (while (not (file-exists-p dir))
3101       (setq dirs (cons dir dirs))
3102       (string-match "/[^/]+$" dir)
3103       (setq dir (substring dir 0 (match-beginning 0))))
3104     ;; Then create all the subdirs.
3105     (while (and dirs (not err))
3106       (condition-case ()
3107           (make-directory (car dirs))
3108         (error (setq err t)))
3109       (setq dirs (cdr dirs)))
3110     ;; We return whether we were successful or not.
3111     (not dirs)))
3112
3113 (defun gnus-capitalize-newsgroup (newsgroup)
3114   "Capitalize NEWSGROUP name."
3115   (and (not (zerop (length newsgroup)))
3116        (concat (char-to-string (upcase (aref newsgroup 0)))
3117                (substring newsgroup 1))))
3118
3119 ;; Various... things.
3120
3121 (defun gnus-simplify-subject (subject &optional re-only)
3122   "Remove `Re:' and words in parentheses.
3123 If RE-ONLY is non-nil, strip leading `Re:'s only."
3124   (let ((case-fold-search t))           ;Ignore case.
3125     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3126     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3127       (setq subject (substring subject (match-end 0))))
3128     ;; Remove uninteresting prefixes.
3129     (if (and (not re-only)
3130              gnus-simplify-ignored-prefixes
3131              (string-match gnus-simplify-ignored-prefixes subject))
3132         (setq subject (substring subject (match-end 0))))
3133     ;; Remove words in parentheses from end.
3134     (unless re-only
3135       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3136         (setq subject (substring subject 0 (match-beginning 0)))))
3137     ;; Return subject string.
3138     subject))
3139
3140 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3141 ;; all whitespace.
3142 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3143 (defun gnus-simplify-buffer-fuzzy ()
3144   (let ((case-fold-search t))
3145     (goto-char (point-min))
3146     (while (search-forward "\t" nil t)
3147       (replace-match " " t t))
3148     (goto-char (point-min))
3149     (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3150     (goto-char (match-beginning 0))
3151     (while (or
3152             (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3153             (looking-at "^[[].*: .*[]]$"))
3154       (goto-char (point-min))
3155       (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3156                                 nil t)
3157         (replace-match "" t t))
3158       (goto-char (point-min))
3159       (while (re-search-forward "^[[].*: .*[]]$" nil t)
3160         (goto-char (match-end 0))
3161         (delete-char -1)
3162         (delete-region
3163          (progn (goto-char (match-beginning 0)))
3164          (re-search-forward ":"))))
3165     (goto-char (point-min))
3166     (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3167       (replace-match "" t t))
3168     (goto-char (point-min))
3169     (while (re-search-forward "  +" nil t)
3170       (replace-match " " t t))
3171     (goto-char (point-min))
3172     (while (re-search-forward " $" nil t)
3173       (replace-match "" t t))
3174     (goto-char (point-min))
3175     (while (re-search-forward "^ +" nil t)
3176       (replace-match "" t t))
3177     (goto-char (point-min))
3178     (when gnus-simplify-subject-fuzzy-regexp
3179       (if (listp gnus-simplify-subject-fuzzy-regexp)
3180           (let ((list gnus-simplify-subject-fuzzy-regexp))
3181             (while list
3182               (goto-char (point-min))
3183               (while (re-search-forward (car list) nil t)
3184                 (replace-match "" t t))
3185               (setq list (cdr list))))
3186         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3187           (replace-match "" t t))))))
3188
3189 (defun gnus-simplify-subject-fuzzy (subject)
3190   "Siplify a subject string fuzzily."
3191   (save-excursion
3192     (gnus-set-work-buffer)
3193     (let ((case-fold-search t))
3194       (insert subject)
3195       (inline (gnus-simplify-buffer-fuzzy))
3196       (buffer-string))))
3197
3198 ;; Add the current buffer to the list of buffers to be killed on exit.
3199 (defun gnus-add-current-to-buffer-list ()
3200   (or (memq (current-buffer) gnus-buffer-list)
3201       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3202
3203 (defun gnus-string> (s1 s2)
3204   (not (or (string< s1 s2)
3205            (string= s1 s2))))
3206
3207 (defun gnus-read-active-file-p ()
3208   "Say whether the active file has been read from `gnus-select-method'."
3209   (memq gnus-select-method gnus-have-read-active-file))
3210
3211 ;;; General various misc type functions.
3212
3213 (defun gnus-clear-system ()
3214   "Clear all variables and buffers."
3215   ;; Clear Gnus variables.
3216   (let ((variables gnus-variable-list))
3217     (while variables
3218       (set (car variables) nil)
3219       (setq variables (cdr variables))))
3220   ;; Clear other internal variables.
3221   (setq gnus-list-of-killed-groups nil
3222         gnus-have-read-active-file nil
3223         gnus-newsrc-alist nil
3224         gnus-newsrc-hashtb nil
3225         gnus-killed-list nil
3226         gnus-zombie-list nil
3227         gnus-killed-hashtb nil
3228         gnus-active-hashtb nil
3229         gnus-moderated-list nil
3230         gnus-description-hashtb nil
3231         gnus-current-headers nil
3232         gnus-thread-indent-array nil
3233         gnus-newsgroup-headers nil
3234         gnus-newsgroup-name nil
3235         gnus-server-alist nil
3236         gnus-group-list-mode nil
3237         gnus-opened-servers nil
3238         gnus-group-mark-positions nil
3239         gnus-newsgroup-data nil
3240         gnus-newsgroup-unreads nil
3241         nnoo-state-alist nil
3242         gnus-current-select-method nil)
3243   (gnus-shutdown 'gnus)
3244   ;; Kill the startup file.
3245   (and gnus-current-startup-file
3246        (get-file-buffer gnus-current-startup-file)
3247        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3248   ;; Clear the dribble buffer.
3249   (gnus-dribble-clear)
3250   ;; Kill global KILL file buffer.
3251   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3252     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3253   (gnus-kill-buffer nntp-server-buffer)
3254   ;; Kill Gnus buffers.
3255   (while gnus-buffer-list
3256     (gnus-kill-buffer (pop gnus-buffer-list)))
3257   ;; Remove Gnus frames.
3258   (gnus-kill-gnus-frames))
3259
3260 (defun gnus-kill-gnus-frames ()
3261   "Kill all frames Gnus has created."
3262   (while gnus-created-frames
3263     (when (frame-live-p (car gnus-created-frames))
3264       ;; We slap a condition-case around this `delete-frame' to ensure 
3265       ;; against errors if we try do delete the single frame that's left.
3266       (condition-case ()
3267           (delete-frame (car gnus-created-frames))
3268         (error nil)))
3269     (pop gnus-created-frames)))
3270
3271 (defun gnus-windows-old-to-new (setting)
3272   ;; First we take care of the really, really old Gnus 3 actions.
3273   (when (symbolp setting)
3274     (setq setting
3275           ;; Take care of ooold GNUS 3.x values.
3276           (cond ((eq setting 'SelectArticle) 'article)
3277                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3278                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3279                 (t setting))))
3280   (if (or (listp setting)
3281           (not (and gnus-window-configuration
3282                     (memq setting '(group summary article)))))
3283       setting
3284     (let* ((setting (if (eq setting 'group)
3285                         (if (assq 'newsgroup gnus-window-configuration)
3286                             'newsgroup
3287                           'newsgroups) setting))
3288            (elem (cadr (assq setting gnus-window-configuration)))
3289            (total (apply '+ elem))
3290            (types '(group summary article))
3291            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3292            (i 0)
3293            perc
3294            out)
3295       (while (< i 3)
3296         (or (not (numberp (nth i elem)))
3297             (zerop (nth i elem))
3298             (progn
3299               (setq perc (if (= i 2)
3300                              1.0
3301                            (/ (float (nth 0 elem)) total)))
3302               (setq out (cons (if (eq pbuf (nth i types))
3303                                   (list (nth i types) perc 'point)
3304                                 (list (nth i types) perc))
3305                               out))))
3306         (setq i (1+ i)))
3307       `(vertical 1.0 ,@(nreverse out)))))
3308
3309 ;;;###autoload
3310 (defun gnus-add-configuration (conf)
3311   "Add the window configuration CONF to `gnus-buffer-configuration'."
3312   (setq gnus-buffer-configuration
3313         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3314                          gnus-buffer-configuration))))
3315
3316 (defvar gnus-frame-list nil)
3317
3318 (defun gnus-configure-frame (split &optional window)
3319   "Split WINDOW according to SPLIT."
3320   (unless window
3321     (setq window (get-buffer-window (current-buffer))))
3322   (select-window window)
3323   ;; This might be an old-stylee buffer config.
3324   (when (vectorp split)
3325     (setq split (append split nil)))
3326   (when (or (consp (car split))
3327             (vectorp (car split)))
3328     (push 1.0 split)
3329     (push 'vertical split))
3330   ;; The SPLIT might be something that is to be evaled to
3331   ;; return a new SPLIT.
3332   (while (and (not (assq (car split) gnus-window-to-buffer))
3333               (gnus-functionp (car split)))
3334     (setq split (eval split)))
3335   (let* ((type (car split))
3336          (subs (cddr split))
3337          (len (if (eq type 'horizontal) (window-width) (window-height)))
3338          (total 0)
3339          (window-min-width (or gnus-window-min-width window-min-width))
3340          (window-min-height (or gnus-window-min-height window-min-height))
3341          s result new-win rest comp-subs size sub)
3342     (cond
3343      ;; Nothing to do here.
3344      ((null split))
3345      ;; Don't switch buffers.
3346      ((null type)
3347       (and (memq 'point split) window))
3348      ;; This is a buffer to be selected.
3349      ((not (memq type '(frame horizontal vertical)))
3350       (let ((buffer (cond ((stringp type) type)
3351                           (t (cdr (assq type gnus-window-to-buffer)))))
3352             buf)
3353         (unless buffer
3354           (error "Illegal buffer type: %s" type))
3355         (unless (setq buf (get-buffer (if (symbolp buffer)
3356                                           (symbol-value buffer) buffer)))
3357           (setq buf (get-buffer-create (if (symbolp buffer)
3358                                            (symbol-value buffer) buffer))))
3359         (switch-to-buffer buf)
3360         ;; We return the window if it has the `point' spec.
3361         (and (memq 'point split) window)))
3362      ;; This is a frame split.
3363      ((eq type 'frame)
3364       (unless gnus-frame-list
3365         (setq gnus-frame-list (list (window-frame
3366                                      (get-buffer-window (current-buffer))))))
3367       (let ((i 0)
3368             params frame fresult)
3369         (while (< i (length subs))
3370           ;; Frame parameter is gotten from the sub-split.
3371           (setq params (cadr (elt subs i)))
3372           ;; It should be a list.
3373           (unless (listp params)
3374             (setq params nil))
3375           ;; Create a new frame?
3376           (unless (setq frame (elt gnus-frame-list i))
3377             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3378             (push frame gnus-created-frames))
3379           ;; Is the old frame still alive?
3380           (unless (frame-live-p frame)
3381             (setcar (nthcdr i gnus-frame-list)
3382                     (setq frame (make-frame params))))
3383           ;; Select the frame in question and do more splits there.
3384           (select-frame frame)
3385           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3386           (incf i))
3387         ;; Select the frame that has the selected buffer.
3388         (when fresult
3389           (select-frame (window-frame fresult)))))
3390      ;; This is a normal split.
3391      (t
3392       (when (> (length subs) 0)
3393         ;; First we have to compute the sizes of all new windows.
3394         (while subs
3395           (setq sub (append (pop subs) nil))
3396           (while (and (not (assq (car sub) gnus-window-to-buffer))
3397                       (gnus-functionp (car sub)))
3398             (setq sub (eval sub)))
3399           (when sub
3400             (push sub comp-subs)
3401             (setq size (cadar comp-subs))
3402             (cond ((equal size 1.0)
3403                    (setq rest (car comp-subs))
3404                    (setq s 0))
3405                   ((floatp size)
3406                    (setq s (floor (* size len))))
3407                   ((integerp size)
3408                    (setq s size))
3409                   (t
3410                    (error "Illegal size: %s" size)))
3411             ;; Try to make sure that we are inside the safe limits.
3412             (cond ((zerop s))
3413                   ((eq type 'horizontal)
3414                    (setq s (max s window-min-width)))
3415                   ((eq type 'vertical)
3416                    (setq s (max s window-min-height))))
3417             (setcar (cdar comp-subs) s)
3418             (incf total s)))
3419         ;; Take care of the "1.0" spec.
3420         (if rest
3421             (setcar (cdr rest) (- len total))
3422           (error "No 1.0 specs in %s" split))
3423         ;; The we do the actual splitting in a nice recursive
3424         ;; fashion.
3425         (setq comp-subs (nreverse comp-subs))
3426         (while comp-subs
3427           (if (null (cdr comp-subs))
3428               (setq new-win window)
3429             (setq new-win
3430                   (split-window window (cadar comp-subs)
3431                                 (eq type 'horizontal))))
3432           (setq result (or (gnus-configure-frame
3433                             (car comp-subs) window) result))
3434           (select-window new-win)
3435           (setq window new-win)
3436           (setq comp-subs (cdr comp-subs))))
3437       ;; Return the proper window, if any.
3438       (when result
3439         (select-window result))))))
3440
3441 (defvar gnus-frame-split-p nil)
3442
3443 (defun gnus-configure-windows (setting &optional force)
3444   (setq setting (gnus-windows-old-to-new setting))
3445   (let ((split (if (symbolp setting)
3446                    (cadr (assq setting gnus-buffer-configuration))
3447                  setting))
3448         all-visible)
3449
3450     (setq gnus-frame-split-p nil)
3451
3452     (unless split
3453       (error "No such setting: %s" setting))
3454
3455     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3456              (not force))
3457         ;; All the windows mentioned are already visible, so we just
3458         ;; put point in the assigned buffer, and do not touch the
3459         ;; winconf.
3460         (select-window all-visible)
3461
3462       ;; Either remove all windows or just remove all Gnus windows.
3463       (let ((frame (selected-frame)))
3464         (unwind-protect
3465             (if gnus-use-full-window
3466                 ;; We want to remove all other windows.
3467                 (if (not gnus-frame-split-p)
3468                     ;; This is not a `frame' split, so we ignore the
3469                     ;; other frames.  
3470                     (delete-other-windows)
3471                   ;; This is a `frame' split, so we delete all windows
3472                   ;; on all frames.
3473                   (mapcar 
3474                    (lambda (frame)
3475                      (unless (eq (cdr (assq 'minibuffer
3476                                             (frame-parameters frame)))
3477                                  'only)
3478                        (select-frame frame)
3479                        (delete-other-windows)))
3480                    (frame-list)))
3481               ;; Just remove some windows.
3482               (gnus-remove-some-windows)
3483               (switch-to-buffer nntp-server-buffer))
3484           (select-frame frame)))
3485
3486       (switch-to-buffer nntp-server-buffer)
3487       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3488
3489 (defun gnus-all-windows-visible-p (split)
3490   "Say whether all buffers in SPLIT are currently visible.
3491 In particular, the value returned will be the window that
3492 should have point."
3493   (let ((stack (list split))
3494         (all-visible t)
3495         type buffer win buf)
3496     (while (and (setq split (pop stack))
3497                 all-visible)
3498       ;; Be backwards compatible.
3499       (when (vectorp split)
3500         (setq split (append split nil)))
3501       (when (or (consp (car split))
3502                 (vectorp (car split)))
3503         (push 1.0 split)
3504         (push 'vertical split))
3505       ;; The SPLIT might be something that is to be evaled to
3506       ;; return a new SPLIT.
3507       (while (and (not (assq (car split) gnus-window-to-buffer))
3508                   (gnus-functionp (car split)))
3509         (setq split (eval split)))
3510
3511       (setq type (elt split 0))
3512       (cond
3513        ;; Nothing here.
3514        ((null split) t)
3515        ;; A buffer.
3516        ((not (memq type '(horizontal vertical frame)))
3517         (setq buffer (cond ((stringp type) type)
3518                            (t (cdr (assq type gnus-window-to-buffer)))))
3519         (unless buffer
3520           (error "Illegal buffer type: %s" type))
3521         (when (setq buf (get-buffer (if (symbolp buffer)
3522                                         (symbol-value buffer)
3523                                       buffer)))
3524           (setq win (get-buffer-window buf t)))
3525         (if win
3526             (when (memq 'point split)
3527                 (setq all-visible win))
3528           (setq all-visible nil)))
3529        (t
3530         (when (eq type 'frame)
3531           (setq gnus-frame-split-p t))
3532         (setq stack (append (cddr split) stack)))))
3533     (unless (eq all-visible t)
3534       all-visible)))
3535
3536 (defun gnus-window-top-edge (&optional window)
3537   (nth 1 (window-edges window)))
3538
3539 (defun gnus-remove-some-windows ()
3540   (let ((buffers gnus-window-to-buffer)
3541         buf bufs lowest-buf lowest)
3542     (save-excursion
3543       ;; Remove windows on all known Gnus buffers.
3544       (while buffers
3545         (setq buf (cdar buffers))
3546         (if (symbolp buf)
3547             (setq buf (and (boundp buf) (symbol-value buf))))
3548         (and buf
3549              (get-buffer-window buf)
3550              (progn
3551                (setq bufs (cons buf bufs))
3552                (pop-to-buffer buf)
3553                (if (or (not lowest)
3554                        (< (gnus-window-top-edge) lowest))
3555                    (progn
3556                      (setq lowest (gnus-window-top-edge))
3557                      (setq lowest-buf buf)))))
3558         (setq buffers (cdr buffers)))
3559       ;; Remove windows on *all* summary buffers.
3560       (walk-windows
3561        (lambda (win)
3562          (let ((buf (window-buffer win)))
3563            (if (string-match    "^\\*Summary" (buffer-name buf))
3564                (progn
3565                  (setq bufs (cons buf bufs))
3566                  (pop-to-buffer buf)
3567                  (if (or (not lowest)
3568                          (< (gnus-window-top-edge) lowest))
3569                      (progn
3570                        (setq lowest-buf buf)
3571                        (setq lowest (gnus-window-top-edge)))))))))
3572       (and lowest-buf
3573            (progn
3574              (pop-to-buffer lowest-buf)
3575              (switch-to-buffer nntp-server-buffer)))
3576       (while bufs
3577         (and (not (eq (car bufs) lowest-buf))
3578              (delete-windows-on (car bufs)))
3579         (setq bufs (cdr bufs))))))
3580
3581 (defun gnus-version (&optional arg)
3582   "Version number of this version of Gnus.
3583 If ARG, insert string at point."
3584   (interactive "P")
3585   (let ((methods gnus-valid-select-methods)
3586         (mess gnus-version)
3587         meth)
3588     ;; Go through all the legal select methods and add their version
3589     ;; numbers to the total version string.  Only the backends that are
3590     ;; currently in use will have their message numbers taken into
3591     ;; consideration.
3592     (while methods
3593       (setq meth (intern (concat (caar methods) "-version")))
3594       (and (boundp meth)
3595            (stringp (symbol-value meth))
3596            (setq mess (concat mess "; " (symbol-value meth))))
3597       (setq methods (cdr methods)))
3598     (if arg
3599         (insert (message mess))
3600       (message mess))))
3601
3602 (defun gnus-info-find-node ()
3603   "Find Info documentation of Gnus."
3604   (interactive)
3605   ;; Enlarge info window if needed.
3606   (let ((mode major-mode)
3607         gnus-info-buffer)
3608     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3609     (setq gnus-info-buffer (current-buffer))
3610     (gnus-configure-windows 'info)))
3611
3612 (defun gnus-days-between (date1 date2)
3613   ;; Return the number of days between date1 and date2.
3614   (- (gnus-day-number date1) (gnus-day-number date2)))
3615
3616 (defun gnus-day-number (date)
3617   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3618                      (timezone-parse-date date))))
3619     (timezone-absolute-from-gregorian
3620      (nth 1 dat) (nth 2 dat) (car dat))))
3621
3622 (defun gnus-encode-date (date)
3623   "Convert DATE to internal time."
3624   (let* ((parse (timezone-parse-date date))
3625          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3626          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3627     (encode-time (caddr time) (cadr time) (car time)
3628                  (caddr date) (cadr date) (car date) (nth 4 date))))
3629
3630 (defun gnus-time-minus (t1 t2)
3631   "Subtract two internal times."
3632   (let ((borrow (< (cadr t1) (cadr t2))))
3633     (list (- (car t1) (car t2) (if borrow 1 0))
3634           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3635
3636 (defun gnus-file-newer-than (file date)
3637   (let ((fdate (nth 5 (file-attributes file))))
3638     (or (> (car fdate) (car date))
3639         (and (= (car fdate) (car date))
3640              (> (nth 1 fdate) (nth 1 date))))))
3641
3642 (defmacro gnus-local-set-keys (&rest plist)
3643   "Set the keys in PLIST in the current keymap."
3644   `(gnus-define-keys-1 (current-local-map) ',plist))
3645
3646 (defmacro gnus-define-keys (keymap &rest plist)
3647   "Define all keys in PLIST in KEYMAP."
3648   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3649
3650 (put 'gnus-define-keys 'lisp-indent-function 1)
3651 (put 'gnus-define-keys 'lisp-indent-hook 1)
3652 (put 'gnus-define-keymap 'lisp-indent-function 1)
3653 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3654
3655 (defmacro gnus-define-keymap (keymap &rest plist)
3656   "Define all keys in PLIST in KEYMAP."
3657   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3658
3659 (defun gnus-define-keys-1 (keymap plist)
3660   (when (null keymap)
3661     (error "Can't set keys in a null keymap"))
3662   (cond ((symbolp keymap)
3663          (setq keymap (symbol-value keymap)))
3664         ((keymapp keymap))
3665         ((listp keymap)
3666          (set (car keymap) nil)
3667          (define-prefix-command (car keymap))
3668          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3669          (setq keymap (symbol-value (car keymap)))))
3670   (let (key)
3671     (while plist
3672       (when (symbolp (setq key (pop plist)))
3673         (setq key (symbol-value key)))
3674       (define-key keymap key (pop plist)))))
3675
3676 (defun gnus-group-read-only-p (&optional group)
3677   "Check whether GROUP supports editing or not.
3678 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3679 that that variable is buffer-local to the summary buffers."
3680   (let ((group (or group gnus-newsgroup-name)))
3681     (not (gnus-check-backend-function 'request-replace-article group))))
3682
3683 (defun gnus-group-total-expirable-p (group)
3684   "Check whether GROUP is total-expirable or not."
3685   (let ((params (gnus-info-params (gnus-get-info group))))
3686     (or (memq 'total-expire params)
3687         (cdr (assq 'total-expire params)) ; (total-expire . t)
3688         (and gnus-total-expirable-newsgroups ; Check var.
3689              (string-match gnus-total-expirable-newsgroups group)))))
3690
3691 (defun gnus-group-auto-expirable-p (group)
3692   "Check whether GROUP is total-expirable or not."
3693   (let ((params (gnus-info-params (gnus-get-info group))))
3694     (or (memq 'auto-expire params)
3695         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3696         (and gnus-auto-expirable-newsgroups ; Check var.
3697              (string-match gnus-auto-expirable-newsgroups group)))))
3698
3699 (defun gnus-virtual-group-p (group)
3700   "Say whether GROUP is virtual or not."
3701   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3702                         gnus-valid-select-methods)))
3703
3704 (defun gnus-news-group-p (group &optional article)
3705   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3706   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3707       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3708            (eq (gnus-request-type group article) 'news))))
3709
3710 (defsubst gnus-simplify-subject-fully (subject)
3711   "Simplify a subject string according to the user's wishes."
3712   (cond
3713    ((null gnus-summary-gather-subject-limit)
3714     (gnus-simplify-subject-re subject))
3715    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3716     (gnus-simplify-subject-fuzzy subject))
3717    ((numberp gnus-summary-gather-subject-limit)
3718     (gnus-limit-string (gnus-simplify-subject-re subject)
3719                        gnus-summary-gather-subject-limit))
3720    (t
3721     subject)))
3722
3723 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3724   "Check whether two subjects are equal.  If optional argument
3725 simple-first is t, first argument is already simplified."
3726   (cond
3727    ((null simple-first)
3728     (equal (gnus-simplify-subject-fully s1)
3729            (gnus-simplify-subject-fully s2)))
3730    (t
3731     (equal s1
3732            (gnus-simplify-subject-fully s2)))))
3733
3734 ;; Returns a list of writable groups.
3735 (defun gnus-writable-groups ()
3736   (let ((alist gnus-newsrc-alist)
3737         groups group)
3738     (while (setq group (car (pop alist)))
3739       (unless (gnus-group-read-only-p group)
3740         (push group groups)))
3741     (nreverse groups)))
3742
3743 (defun gnus-completing-read (default prompt &rest args)
3744   ;; Like `completing-read', except that DEFAULT is the default argument.
3745   (let* ((prompt (if default 
3746                      (concat prompt " (default " default ") ")
3747                    (concat prompt " ")))
3748          (answer (apply 'completing-read prompt args)))
3749     (if (or (null answer) (zerop (length answer)))
3750         default
3751       answer)))
3752
3753 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3754 ;; the echo area.
3755 (defun gnus-y-or-n-p (prompt)
3756   (prog1
3757       (y-or-n-p prompt)
3758     (message "")))
3759
3760 (defun gnus-yes-or-no-p (prompt)
3761   (prog1
3762       (yes-or-no-p prompt)
3763     (message "")))
3764
3765 ;; Check whether to use long file names.
3766 (defun gnus-use-long-file-name (symbol)
3767   ;; The variable has to be set...
3768   (and gnus-use-long-file-name
3769        ;; If it isn't a list, then we return t.
3770        (or (not (listp gnus-use-long-file-name))
3771            ;; If it is a list, and the list contains `symbol', we
3772            ;; return nil.
3773            (not (memq symbol gnus-use-long-file-name)))))
3774
3775 ;; I suspect there's a better way, but I haven't taken the time to do
3776 ;; it yet. -erik selberg@cs.washington.edu
3777 (defun gnus-dd-mmm (messy-date)
3778   "Return a string like DD-MMM from a big messy string"
3779   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3780                    (error nil))))
3781     (if (not datevec)
3782         "??-???"
3783       (format "%2s-%s"
3784               (condition-case ()
3785                   ;; Make sure leading zeroes are stripped.
3786                   (number-to-string (string-to-number (aref datevec 2)))
3787                 (error "??"))
3788               (capitalize
3789                (or (car
3790                     (nth (1- (string-to-number (aref datevec 1)))
3791                          timezone-months-assoc))
3792                    "???"))))))
3793
3794 (defun gnus-mode-string-quote (string)
3795   "Quote all \"%\" in STRING."
3796   (save-excursion
3797     (gnus-set-work-buffer)
3798     (insert string)
3799     (goto-char (point-min))
3800     (while (search-forward "%" nil t)
3801       (insert "%"))
3802     (buffer-string)))
3803
3804 ;; Make a hash table (default and minimum size is 255).
3805 ;; Optional argument HASHSIZE specifies the table size.
3806 (defun gnus-make-hashtable (&optional hashsize)
3807   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3808
3809 ;; Make a number that is suitable for hashing; bigger than MIN and one
3810 ;; less than 2^x.
3811 (defun gnus-create-hash-size (min)
3812   (let ((i 1))
3813     (while (< i min)
3814       (setq i (* 2 i)))
3815     (1- i)))
3816
3817 ;; Show message if message has a lower level than `gnus-verbose'.
3818 ;; Guideline for numbers:
3819 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3820 ;; for things that take a long time, 7 - not very important messages
3821 ;; on stuff, 9 - messages inside loops.
3822 (defun gnus-message (level &rest args)
3823   (if (<= level gnus-verbose)
3824       (apply 'message args)
3825     ;; We have to do this format thingy here even if the result isn't
3826     ;; shown - the return value has to be the same as the return value
3827     ;; from `message'.
3828     (apply 'format args)))
3829
3830 (defun gnus-error (level &rest args)
3831   "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
3832   (when (<= (floor level) gnus-verbose)
3833     (apply 'message args)
3834     (ding)
3835     (let (duration)
3836       (when (and (floatp level)
3837                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3838         (sit-for duration))))
3839   nil)
3840
3841 ;; Generate a unique new group name.
3842 (defun gnus-generate-new-group-name (leaf)
3843   (let ((name leaf)
3844         (num 0))
3845     (while (gnus-gethash name gnus-newsrc-hashtb)
3846       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3847     name))
3848
3849 (defsubst gnus-hide-text (b e props)
3850   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3851   (gnus-add-text-properties b e props)
3852   (when (memq 'intangible props)
3853     (gnus-put-text-property (max (1- b) (point-min))
3854                        b 'intangible (cddr (memq 'intangible props)))))
3855
3856 (defsubst gnus-unhide-text (b e)
3857   "Remove hidden text properties from region between B and E."
3858   (remove-text-properties b e gnus-hidden-properties)
3859   (when (memq 'intangible gnus-hidden-properties)
3860     (gnus-put-text-property (max (1- b) (point-min))
3861                             b 'intangible nil)))
3862
3863 (defun gnus-hide-text-type (b e type)
3864   "Hide text of TYPE between B and E."
3865   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3866
3867 (defun gnus-parent-headers (headers &optional generation)
3868   "Return the headers of the GENERATIONeth parent of HEADERS."
3869   (unless generation 
3870     (setq generation 1))
3871   (let (references parent)
3872     (while (and headers (not (zerop generation)))
3873       (setq references (mail-header-references headers))
3874       (when (and references
3875                  (setq parent (gnus-parent-id references))
3876                  (setq headers (car (gnus-id-to-thread parent))))
3877         (decf generation)))
3878     headers))
3879
3880 (defun gnus-parent-id (references)
3881   "Return the last Message-ID in REFERENCES."
3882   (when (and references
3883              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3884     (substring references (match-beginning 1) (match-end 1))))
3885
3886 (defun gnus-split-references (references)
3887   "Return a list of Message-IDs in REFERENCES."
3888   (let ((beg 0)
3889         ids)
3890     (while (string-match "<[^>]+>" references beg)
3891       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3892             ids))
3893     (nreverse ids)))
3894
3895 (defun gnus-buffer-live-p (buffer)
3896   "Say whether BUFFER is alive or not."
3897   (and buffer
3898        (get-buffer buffer)
3899        (buffer-name (get-buffer buffer))))
3900
3901 (defun gnus-ephemeral-group-p (group)
3902   "Say whether GROUP is ephemeral or not."
3903   (gnus-group-get-parameter group 'quit-config))
3904
3905 (defun gnus-group-quit-config (group)
3906   "Return the quit-config of GROUP."
3907   (gnus-group-get-parameter group 'quit-config))
3908
3909 (defun gnus-simplify-mode-line ()
3910   "Make mode lines a bit simpler."
3911   (setq mode-line-modified "-- ")
3912   (when (listp mode-line-format)
3913     (make-local-variable 'mode-line-format)
3914     (setq mode-line-format (copy-sequence mode-line-format))
3915     (when (equal (nth 3 mode-line-format) "   ")
3916       (setcar (nthcdr 3 mode-line-format) " "))))
3917
3918 ;;; List and range functions
3919
3920 (defun gnus-last-element (list)
3921   "Return last element of LIST."
3922   (while (cdr list)
3923     (setq list (cdr list)))
3924   (car list))
3925
3926 (defun gnus-copy-sequence (list)
3927   "Do a complete, total copy of a list."
3928   (if (and (consp list) (not (consp (cdr list))))
3929       (cons (car list) (cdr list))
3930     (mapcar (lambda (elem) (if (consp elem)
3931                                (if (consp (cdr elem))
3932                                    (gnus-copy-sequence elem)
3933                                  (cons (car elem) (cdr elem)))
3934                              elem))
3935             list)))
3936
3937 (defun gnus-set-difference (list1 list2)
3938   "Return a list of elements of LIST1 that do not appear in LIST2."
3939   (let ((list1 (copy-sequence list1)))
3940     (while list2
3941       (setq list1 (delq (car list2) list1))
3942       (setq list2 (cdr list2)))
3943     list1))
3944
3945 (defun gnus-sorted-complement (list1 list2)
3946   "Return a list of elements of LIST1 that do not appear in LIST2.
3947 Both lists have to be sorted over <."
3948   (let (out)
3949     (if (or (null list1) (null list2))
3950         (or list1 list2)
3951       (while (and list1 list2)
3952         (cond ((= (car list1) (car list2))
3953                (setq list1 (cdr list1)
3954                      list2 (cdr list2)))
3955               ((< (car list1) (car list2))
3956                (setq out (cons (car list1) out))
3957                (setq list1 (cdr list1)))
3958               (t
3959                (setq out (cons (car list2) out))
3960                (setq list2 (cdr list2)))))
3961       (nconc (nreverse out) (or list1 list2)))))
3962
3963 (defun gnus-intersection (list1 list2)
3964   (let ((result nil))
3965     (while list2
3966       (if (memq (car list2) list1)
3967           (setq result (cons (car list2) result)))
3968       (setq list2 (cdr list2)))
3969     result))
3970
3971 (defun gnus-sorted-intersection (list1 list2)
3972   ;; LIST1 and LIST2 have to be sorted over <.
3973   (let (out)
3974     (while (and list1 list2)
3975       (cond ((= (car list1) (car list2))
3976              (setq out (cons (car list1) out)
3977                    list1 (cdr list1)
3978                    list2 (cdr list2)))
3979             ((< (car list1) (car list2))
3980              (setq list1 (cdr list1)))
3981             (t
3982              (setq list2 (cdr list2)))))
3983     (nreverse out)))
3984
3985 (defun gnus-set-sorted-intersection (list1 list2)
3986   ;; LIST1 and LIST2 have to be sorted over <.
3987   ;; This function modifies LIST1.
3988   (let* ((top (cons nil list1))
3989          (prev top))
3990     (while (and list1 list2)
3991       (cond ((= (car list1) (car list2))
3992              (setq prev list1
3993                    list1 (cdr list1)
3994                    list2 (cdr list2)))
3995             ((< (car list1) (car list2))
3996              (setcdr prev (cdr list1))
3997              (setq list1 (cdr list1)))
3998             (t
3999              (setq list2 (cdr list2)))))
4000     (setcdr prev nil)
4001     (cdr top)))
4002
4003 (defun gnus-compress-sequence (numbers &optional always-list)
4004   "Convert list of numbers to a list of ranges or a single range.
4005 If ALWAYS-LIST is non-nil, this function will always release a list of
4006 ranges."
4007   (let* ((first (car numbers))
4008          (last (car numbers))
4009          result)
4010     (if (null numbers)
4011         nil
4012       (if (not (listp (cdr numbers)))
4013           numbers
4014         (while numbers
4015           (cond ((= last (car numbers)) nil) ;Omit duplicated number
4016                 ((= (1+ last) (car numbers)) ;Still in sequence
4017                  (setq last (car numbers)))
4018                 (t                      ;End of one sequence
4019                  (setq result
4020                        (cons (if (= first last) first
4021                                (cons first last)) result))
4022                  (setq first (car numbers))
4023                  (setq last  (car numbers))))
4024           (setq numbers (cdr numbers)))
4025         (if (and (not always-list) (null result))
4026             (if (= first last) (list first) (cons first last))
4027           (nreverse (cons (if (= first last) first (cons first last))
4028                           result)))))))
4029
4030 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
4031 (defun gnus-uncompress-range (ranges)
4032   "Expand a list of ranges into a list of numbers.
4033 RANGES is either a single range on the form `(num . num)' or a list of
4034 these ranges."
4035   (let (first last result)
4036     (cond
4037      ((null ranges)
4038       nil)
4039      ((not (listp (cdr ranges)))
4040       (setq first (car ranges))
4041       (setq last (cdr ranges))
4042       (while (<= first last)
4043         (setq result (cons first result))
4044         (setq first (1+ first)))
4045       (nreverse result))
4046      (t
4047       (while ranges
4048         (if (atom (car ranges))
4049             (if (numberp (car ranges))
4050                 (setq result (cons (car ranges) result)))
4051           (setq first (caar ranges))
4052           (setq last  (cdar ranges))
4053           (while (<= first last)
4054             (setq result (cons first result))
4055             (setq first (1+ first))))
4056         (setq ranges (cdr ranges)))
4057       (nreverse result)))))
4058
4059 (defun gnus-add-to-range (ranges list)
4060   "Return a list of ranges that has all articles from both RANGES and LIST.
4061 Note: LIST has to be sorted over `<'."
4062   (if (not ranges)
4063       (gnus-compress-sequence list t)
4064     (setq list (copy-sequence list))
4065     (or (listp (cdr ranges))
4066         (setq ranges (list ranges)))
4067     (let ((out ranges)
4068           ilist lowest highest temp)
4069       (while (and ranges list)
4070         (setq ilist list)
4071         (setq lowest (or (and (atom (car ranges)) (car ranges))
4072                          (caar ranges)))
4073         (while (and list (cdr list) (< (cadr list) lowest))
4074           (setq list (cdr list)))
4075         (if (< (car ilist) lowest)
4076             (progn
4077               (setq temp list)
4078               (setq list (cdr list))
4079               (setcdr temp nil)
4080               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4081         (setq highest (or (and (atom (car ranges)) (car ranges))
4082                           (cdar ranges)))
4083         (while (and list (<= (car list) highest))
4084           (setq list (cdr list)))
4085         (setq ranges (cdr ranges)))
4086       (if list
4087           (setq out (nconc (gnus-compress-sequence list t) out)))
4088       (setq out (sort out (lambda (r1 r2)
4089                             (< (or (and (atom r1) r1) (car r1))
4090                                (or (and (atom r2) r2) (car r2))))))
4091       (setq ranges out)
4092       (while ranges
4093         (if (atom (car ranges))
4094             (if (cdr ranges)
4095                 (if (atom (cadr ranges))
4096                     (if (= (1+ (car ranges)) (cadr ranges))
4097                         (progn
4098                           (setcar ranges (cons (car ranges)
4099                                                (cadr ranges)))
4100                           (setcdr ranges (cddr ranges))))
4101                   (if (= (1+ (car ranges)) (caadr ranges))
4102                       (progn
4103                         (setcar (cadr ranges) (car ranges))
4104                         (setcar ranges (cadr ranges))
4105                         (setcdr ranges (cddr ranges))))))
4106           (if (cdr ranges)
4107               (if (atom (cadr ranges))
4108                   (if (= (1+ (cdar ranges)) (cadr ranges))
4109                       (progn
4110                         (setcdr (car ranges) (cadr ranges))
4111                         (setcdr ranges (cddr ranges))))
4112                 (if (= (1+ (cdar ranges)) (caadr ranges))
4113                     (progn
4114                       (setcdr (car ranges) (cdadr ranges))
4115                       (setcdr ranges (cddr ranges)))))))
4116         (setq ranges (cdr ranges)))
4117       out)))
4118
4119 (defun gnus-remove-from-range (ranges list)
4120   "Return a list of ranges that has all articles from LIST removed from RANGES.
4121 Note: LIST has to be sorted over `<'."
4122   ;; !!! This function shouldn't look like this, but I've got a headache.
4123   (gnus-compress-sequence
4124    (gnus-sorted-complement
4125     (gnus-uncompress-range ranges) list)))
4126
4127 (defun gnus-member-of-range (number ranges)
4128   (if (not (listp (cdr ranges)))
4129       (and (>= number (car ranges))
4130            (<= number (cdr ranges)))
4131     (let ((not-stop t))
4132       (while (and ranges
4133                   (if (numberp (car ranges))
4134                       (>= number (car ranges))
4135                     (>= number (caar ranges)))
4136                   not-stop)
4137         (if (if (numberp (car ranges))
4138                 (= number (car ranges))
4139               (and (>= number (caar ranges))
4140                    (<= number (cdar ranges))))
4141             (setq not-stop nil))
4142         (setq ranges (cdr ranges)))
4143       (not not-stop))))
4144
4145 (defun gnus-range-length (range)
4146   "Return the length RANGE would have if uncompressed."
4147   (length (gnus-uncompress-range range)))
4148
4149 (defun gnus-sublist-p (list sublist)
4150   "Test whether all elements in SUBLIST are members of LIST."
4151   (let ((sublistp t))
4152     (while sublist
4153       (unless (memq (pop sublist) list)
4154         (setq sublistp nil
4155               sublist nil)))
4156     sublistp))
4157
4158 \f
4159 ;;;
4160 ;;; Gnus group mode
4161 ;;;
4162
4163 (defvar gnus-group-mode-map nil)
4164 (put 'gnus-group-mode 'mode-class 'special)
4165
4166 (unless gnus-group-mode-map
4167   (setq gnus-group-mode-map (make-keymap))
4168   (suppress-keymap gnus-group-mode-map)
4169
4170   (gnus-define-keys gnus-group-mode-map
4171     " " gnus-group-read-group
4172     "=" gnus-group-select-group
4173     "\r" gnus-group-select-group
4174     "\M-\r" gnus-group-quick-select-group
4175     "j" gnus-group-jump-to-group
4176     "n" gnus-group-next-unread-group
4177     "p" gnus-group-prev-unread-group
4178     "\177" gnus-group-prev-unread-group
4179     [delete] gnus-group-prev-unread-group
4180     "N" gnus-group-next-group
4181     "P" gnus-group-prev-group
4182     "\M-n" gnus-group-next-unread-group-same-level
4183     "\M-p" gnus-group-prev-unread-group-same-level
4184     "," gnus-group-best-unread-group
4185     "." gnus-group-first-unread-group
4186     "u" gnus-group-unsubscribe-current-group
4187     "U" gnus-group-unsubscribe-group
4188     "c" gnus-group-catchup-current
4189     "C" gnus-group-catchup-current-all
4190     "l" gnus-group-list-groups
4191     "L" gnus-group-list-all-groups
4192     "m" gnus-group-mail
4193     "g" gnus-group-get-new-news
4194     "\M-g" gnus-group-get-new-news-this-group
4195     "R" gnus-group-restart
4196     "r" gnus-group-read-init-file
4197     "B" gnus-group-browse-foreign-server
4198     "b" gnus-group-check-bogus-groups
4199     "F" gnus-find-new-newsgroups
4200     "\C-c\C-d" gnus-group-describe-group
4201     "\M-d" gnus-group-describe-all-groups
4202     "\C-c\C-a" gnus-group-apropos
4203     "\C-c\M-\C-a" gnus-group-description-apropos
4204     "a" gnus-group-post-news
4205     "\ek" gnus-group-edit-local-kill
4206     "\eK" gnus-group-edit-global-kill
4207     "\C-k" gnus-group-kill-group
4208     "\C-y" gnus-group-yank-group
4209     "\C-w" gnus-group-kill-region
4210     "\C-x\C-t" gnus-group-transpose-groups
4211     "\C-c\C-l" gnus-group-list-killed
4212     "\C-c\C-x" gnus-group-expire-articles
4213     "\C-c\M-\C-x" gnus-group-expire-all-groups
4214     "V" gnus-version
4215     "s" gnus-group-save-newsrc
4216     "z" gnus-group-suspend
4217 ;    "Z" gnus-group-clear-dribble
4218     "q" gnus-group-exit
4219     "Q" gnus-group-quit
4220     "?" gnus-group-describe-briefly
4221     "\C-c\C-i" gnus-info-find-node
4222     "\M-e" gnus-group-edit-group-method
4223     "^" gnus-group-enter-server-mode
4224     gnus-mouse-2 gnus-mouse-pick-group
4225     "<" beginning-of-buffer
4226     ">" end-of-buffer
4227     "\C-c\C-b" gnus-bug
4228     "\C-c\C-s" gnus-group-sort-groups
4229     "t" gnus-topic-mode
4230     "\C-c\M-g" gnus-activate-all-groups
4231     "\M-&" gnus-group-universal-argument
4232     "#" gnus-group-mark-group
4233     "\M-#" gnus-group-unmark-group)
4234
4235   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4236     "m" gnus-group-mark-group
4237     "u" gnus-group-unmark-group
4238     "w" gnus-group-mark-region
4239     "m" gnus-group-mark-buffer
4240     "r" gnus-group-mark-regexp
4241     "U" gnus-group-unmark-all-groups)
4242
4243   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4244     "d" gnus-group-make-directory-group
4245     "h" gnus-group-make-help-group
4246     "a" gnus-group-make-archive-group
4247     "k" gnus-group-make-kiboze-group
4248     "m" gnus-group-make-group
4249     "E" gnus-group-edit-group
4250     "e" gnus-group-edit-group-method
4251     "p" gnus-group-edit-group-parameters
4252     "v" gnus-group-add-to-virtual
4253     "V" gnus-group-make-empty-virtual
4254     "D" gnus-group-enter-directory
4255     "f" gnus-group-make-doc-group
4256     "r" gnus-group-rename-group
4257     "\177" gnus-group-delete-group
4258     [delete] gnus-group-delete-group)
4259
4260    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4261      "b" gnus-group-brew-soup
4262      "w" gnus-soup-save-areas
4263      "s" gnus-soup-send-replies
4264      "p" gnus-soup-pack-packet
4265      "r" nnsoup-pack-replies)
4266
4267    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4268      "s" gnus-group-sort-groups
4269      "a" gnus-group-sort-groups-by-alphabet
4270      "u" gnus-group-sort-groups-by-unread
4271      "l" gnus-group-sort-groups-by-level
4272      "v" gnus-group-sort-groups-by-score
4273      "r" gnus-group-sort-groups-by-rank
4274      "m" gnus-group-sort-groups-by-method)
4275
4276    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4277      "k" gnus-group-list-killed
4278      "z" gnus-group-list-zombies
4279      "s" gnus-group-list-groups
4280      "u" gnus-group-list-all-groups
4281      "A" gnus-group-list-active
4282      "a" gnus-group-apropos
4283      "d" gnus-group-description-apropos
4284      "m" gnus-group-list-matching
4285      "M" gnus-group-list-all-matching
4286      "l" gnus-group-list-level)
4287
4288    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4289      "f" gnus-score-flush-cache)
4290
4291    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4292      "f" gnus-group-fetch-faq)
4293
4294    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4295      "l" gnus-group-set-current-level
4296      "t" gnus-group-unsubscribe-current-group
4297      "s" gnus-group-unsubscribe-group
4298      "k" gnus-group-kill-group
4299      "y" gnus-group-yank-group
4300      "w" gnus-group-kill-region
4301      "\C-k" gnus-group-kill-level
4302      "z" gnus-group-kill-all-zombies))
4303
4304 (defun gnus-group-mode ()
4305   "Major mode for reading news.
4306
4307 All normal editing commands are switched off.
4308 \\<gnus-group-mode-map>
4309 The group buffer lists (some of) the groups available.  For instance,
4310 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4311 lists all zombie groups.
4312
4313 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4314 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4315
4316 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4317
4318 The following commands are available:
4319
4320 \\{gnus-group-mode-map}"
4321   (interactive)
4322   (when (and menu-bar-mode
4323              (gnus-visual-p 'group-menu 'menu))
4324     (gnus-group-make-menu-bar))
4325   (kill-all-local-variables)
4326   (gnus-simplify-mode-line)
4327   (setq major-mode 'gnus-group-mode)
4328   (setq mode-name "Group")
4329   (gnus-group-set-mode-line)
4330   (setq mode-line-process nil)
4331   (use-local-map gnus-group-mode-map)
4332   (buffer-disable-undo (current-buffer))
4333   (setq truncate-lines t)
4334   (setq buffer-read-only t)
4335   (gnus-make-local-hook 'post-command-hook)
4336   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
4337   (run-hooks 'gnus-group-mode-hook))
4338
4339 (defun gnus-clear-inboxes-moved ()
4340   (setq nnmail-moved-inboxes nil))
4341
4342 (defun gnus-mouse-pick-group (e)
4343   "Enter the group under the mouse pointer."
4344   (interactive "e")
4345   (mouse-set-point e)
4346   (gnus-group-read-group nil))
4347
4348 ;; Look at LEVEL and find out what the level is really supposed to be.
4349 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4350 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4351 (defun gnus-group-default-level (&optional level number-or-nil)
4352   (cond
4353    (gnus-group-use-permanent-levels
4354     (or (setq gnus-group-use-permanent-levels
4355               (or level (if (numberp gnus-group-use-permanent-levels)
4356                             gnus-group-use-permanent-levels
4357                           (or gnus-group-default-list-level
4358                               gnus-level-subscribed))))
4359         gnus-group-default-list-level gnus-level-subscribed))
4360    (number-or-nil
4361     level)
4362    (t
4363     (or level gnus-group-default-list-level gnus-level-subscribed))))
4364
4365 ;;;###autoload
4366 (defun gnus-slave-no-server (&optional arg)
4367   "Read network news as a slave, without connecting to local server"
4368   (interactive "P")
4369   (gnus-no-server arg t))
4370
4371 ;;;###autoload
4372 (defun gnus-no-server (&optional arg slave)
4373   "Read network news.
4374 If ARG is a positive number, Gnus will use that as the
4375 startup level.  If ARG is nil, Gnus will be started at level 2.
4376 If ARG is non-nil and not a positive number, Gnus will
4377 prompt the user for the name of an NNTP server to use.
4378 As opposed to `gnus', this command will not connect to the local server."
4379   (interactive "P")
4380   (let ((val (or arg (1- gnus-level-default-subscribed))))
4381     (gnus val t slave)
4382     (make-local-variable 'gnus-group-use-permanent-levels)
4383     (setq gnus-group-use-permanent-levels val)))
4384
4385 ;;;###autoload
4386 (defun gnus-slave (&optional arg)
4387   "Read news as a slave."
4388   (interactive "P")
4389   (gnus arg nil 'slave))
4390
4391 ;;;###autoload
4392 (defun gnus-other-frame (&optional arg)
4393   "Pop up a frame to read news."
4394   (interactive "P")
4395   (if (get-buffer gnus-group-buffer)
4396       (let ((pop-up-frames t))
4397         (gnus arg))
4398     (select-frame (make-frame))
4399     (gnus arg)))
4400
4401 ;;;###autoload
4402 (defun gnus (&optional arg dont-connect slave)
4403   "Read network news.
4404 If ARG is non-nil and a positive number, Gnus will use that as the
4405 startup level.  If ARG is non-nil and not a positive number, Gnus will
4406 prompt the user for the name of an NNTP server to use."
4407   (interactive "P")
4408
4409   (if (get-buffer gnus-group-buffer)
4410       (progn
4411         (switch-to-buffer gnus-group-buffer)
4412         (gnus-group-get-new-news))
4413
4414     (gnus-clear-system)
4415     (nnheader-init-server-buffer)
4416     (gnus-read-init-file)
4417     (setq gnus-slave slave)
4418
4419     (gnus-group-setup-buffer)
4420     (let ((buffer-read-only nil))
4421       (erase-buffer)
4422       (if (not gnus-inhibit-startup-message)
4423           (progn
4424             (gnus-group-startup-message)
4425             (sit-for 0))))
4426
4427     (let ((level (and (numberp arg) (> arg 0) arg))
4428           did-connect)
4429       (unwind-protect
4430           (progn
4431             (or dont-connect
4432                 (setq did-connect
4433                       (gnus-start-news-server (and arg (not level))))))
4434         (if (and (not dont-connect)
4435                  (not did-connect))
4436             (gnus-group-quit)
4437           (run-hooks 'gnus-startup-hook)
4438           ;; NNTP server is successfully open.
4439
4440           ;; Find the current startup file name.
4441           (setq gnus-current-startup-file
4442                 (gnus-make-newsrc-file gnus-startup-file))
4443
4444           ;; Read the dribble file.
4445           (when (or gnus-slave gnus-use-dribble-file)
4446             (gnus-dribble-read-file))
4447
4448           ;; Allow using GroupLens predictions.
4449           (when gnus-use-grouplens
4450             (bbb-login)
4451             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4452
4453           (gnus-summary-make-display-table)
4454           ;; Do the actual startup.
4455           (gnus-setup-news nil level dont-connect)
4456           ;; Generate the group buffer.
4457           (gnus-group-list-groups level)
4458           (gnus-group-first-unread-group)
4459           (gnus-configure-windows 'group)
4460           (gnus-group-set-mode-line))))))
4461
4462 (defun gnus-unload ()
4463   "Unload all Gnus features."
4464   (interactive)
4465   (or (boundp 'load-history)
4466       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4467   (let ((history load-history)
4468         feature)
4469     (while history
4470       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4471            (setq feature (cdr (assq 'provide (car history))))
4472            (unload-feature feature 'force))
4473       (setq history (cdr history)))))
4474
4475 (defun gnus-compile ()
4476   "Byte-compile the user-defined format specs."
4477   (interactive)
4478   (let ((entries gnus-format-specs)
4479         entry gnus-tmp-func)
4480     (save-excursion
4481       (gnus-message 7 "Compiling format specs...")
4482
4483       (while entries
4484         (setq entry (pop entries))
4485         (if (eq (car entry) 'version)
4486             (setq gnus-format-specs (delq entry gnus-format-specs))
4487           (when (and (listp (caddr entry))
4488                      (not (eq 'byte-code (caaddr entry))))
4489             (fset 'gnus-tmp-func
4490                   `(lambda () ,(caddr entry)))
4491             (byte-compile 'gnus-tmp-func)
4492             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4493
4494       (push (cons 'version emacs-version) gnus-format-specs)
4495
4496       (gnus-message 7 "Compiling user specs...done"))))
4497
4498 (defun gnus-indent-rigidly (start end arg)
4499   "Indent rigidly using only spaces and no tabs."
4500   (save-excursion
4501     (save-restriction
4502       (narrow-to-region start end)
4503       (indent-rigidly start end arg)
4504       (goto-char (point-min))
4505       (while (search-forward "\t" nil t)
4506         (replace-match "        " t t)))))
4507
4508 (defun gnus-group-startup-message (&optional x y)
4509   "Insert startup message in current buffer."
4510   ;; Insert the message.
4511   (erase-buffer)
4512   (insert
4513    (format "              %s
4514           _    ___ _             _
4515           _ ___ __ ___  __    _ ___
4516           __   _     ___    __  ___
4517               _           ___     _
4518              _  _ __             _
4519              ___   __            _
4520                    __           _
4521                     _      _   _
4522                    _      _    _
4523                       _  _    _
4524                   __  ___
4525                  _   _ _     _
4526                 _   _
4527               _    _
4528              _    _
4529             _
4530           __
4531
4532 "
4533            ""))
4534   ;; And then hack it.
4535   (gnus-indent-rigidly (point-min) (point-max)
4536                        (/ (max (- (window-width) (or x 46)) 0) 2))
4537   (goto-char (point-min))
4538   (forward-line 1)
4539   (let* ((pheight (count-lines (point-min) (point-max)))
4540          (wheight (window-height))
4541          (rest (- wheight pheight)))
4542     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4543   ;; Fontify some.
4544   (goto-char (point-min))
4545   (and (search-forward "Praxis" nil t)
4546        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4547   (goto-char (point-min))
4548   (let* ((mode-string (gnus-group-set-mode-line)))
4549     (setq mode-line-buffer-identification
4550           (list (concat gnus-version (substring (car mode-string) 4))))
4551     (set-buffer-modified-p t)))
4552
4553 (defun gnus-group-setup-buffer ()
4554   (or (get-buffer gnus-group-buffer)
4555       (progn
4556         (switch-to-buffer gnus-group-buffer)
4557         (gnus-add-current-to-buffer-list)
4558         (gnus-group-mode)
4559         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4560
4561 (defun gnus-group-list-groups (&optional level unread lowest)
4562   "List newsgroups with level LEVEL or lower that have unread articles.
4563 Default is all subscribed groups.
4564 If argument UNREAD is non-nil, groups with no unread articles are also
4565 listed."
4566   (interactive (list (if current-prefix-arg
4567                          (prefix-numeric-value current-prefix-arg)
4568                        (or
4569                         (gnus-group-default-level nil t)
4570                         gnus-group-default-list-level
4571                         gnus-level-subscribed))))
4572   (or level
4573       (setq level (car gnus-group-list-mode)
4574             unread (cdr gnus-group-list-mode)))
4575   (setq level (gnus-group-default-level level))
4576   (gnus-group-setup-buffer)             ;May call from out of group buffer
4577   (gnus-update-format-specifications)
4578   (let ((case-fold-search nil)
4579         (props (text-properties-at (gnus-point-at-bol)))
4580         (group (gnus-group-group-name)))
4581     (set-buffer gnus-group-buffer)
4582     (funcall gnus-group-prepare-function level unread lowest)
4583     (if (zerop (buffer-size))
4584         (gnus-message 5 gnus-no-groups-message)
4585       (goto-char (point-max))
4586       (when (or (not gnus-group-goto-next-group-function)
4587                 (not (funcall gnus-group-goto-next-group-function 
4588                               group props)))
4589         (if (not group)
4590             ;; Go to the first group with unread articles.
4591             (gnus-group-search-forward t)
4592           ;; Find the right group to put point on.  If the current group
4593           ;; has disappeared in the new listing, try to find the next
4594           ;; one.        If no next one can be found, just leave point at the
4595           ;; first newsgroup in the buffer.
4596           (if (not (gnus-goto-char
4597                     (text-property-any
4598                      (point-min) (point-max)
4599                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4600               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4601                 (while (and newsrc
4602                             (not (gnus-goto-char
4603                                   (text-property-any
4604                                    (point-min) (point-max) 'gnus-group
4605                                    (gnus-intern-safe
4606                                     (caar newsrc) gnus-active-hashtb)))))
4607                   (setq newsrc (cdr newsrc)))
4608                 (or newsrc (progn (goto-char (point-max))
4609                                   (forward-line -1)))))))
4610       ;; Adjust cursor point.
4611       (gnus-group-position-point))))
4612
4613 (defun gnus-group-list-level (level &optional all)
4614   "List groups on LEVEL.
4615 If ALL (the prefix), also list groups that have no unread articles."
4616   (interactive "nList groups on level: \nP")
4617   (gnus-group-list-groups level all level))
4618
4619 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4620   "List all newsgroups with unread articles of level LEVEL or lower.
4621 If ALL is non-nil, list groups that have no unread articles.
4622 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4623 If REGEXP, only list groups matching REGEXP."
4624   (set-buffer gnus-group-buffer)
4625   (let ((buffer-read-only nil)
4626         (newsrc (cdr gnus-newsrc-alist))
4627         (lowest (or lowest 1))
4628         info clevel unread group params)
4629     (erase-buffer)
4630     (if (< lowest gnus-level-zombie)
4631         ;; List living groups.
4632         (while newsrc
4633           (setq info (car newsrc)
4634                 group (gnus-info-group info)
4635                 params (gnus-info-params info)
4636                 newsrc (cdr newsrc)
4637                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4638           (and unread                   ; This group might be bogus
4639                (or (not regexp)
4640                    (string-match regexp group))
4641                (<= (setq clevel (gnus-info-level info)) level)
4642                (>= clevel lowest)
4643                (or all                  ; We list all groups?
4644                    (if (eq unread t)    ; Unactivated?
4645                        gnus-group-list-inactive-groups ; We list unactivated 
4646                      (> unread 0))      ; We list groups with unread articles
4647                    (and gnus-list-groups-with-ticked-articles
4648                         (cdr (assq 'tick (gnus-info-marks info))))
4649                                         ; And groups with tickeds
4650                    ;; Check for permanent visibility.
4651                    (and gnus-permanently-visible-groups
4652                         (string-match gnus-permanently-visible-groups
4653                                       group))
4654                    (memq 'visible params)
4655                    (cdr (assq 'visible params)))
4656                (gnus-group-insert-group-line
4657                 group (gnus-info-level info)
4658                 (gnus-info-marks info) unread (gnus-info-method info)))))
4659
4660     ;; List dead groups.
4661     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4662          (gnus-group-prepare-flat-list-dead
4663           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4664           gnus-level-zombie ?Z
4665           regexp))
4666     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4667          (gnus-group-prepare-flat-list-dead
4668           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4669           gnus-level-killed ?K regexp))
4670
4671     (gnus-group-set-mode-line)
4672     (setq gnus-group-list-mode (cons level all))
4673     (run-hooks 'gnus-group-prepare-hook)))
4674
4675 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4676   ;; List zombies and killed lists somewhat faster, which was
4677   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4678   ;; this by ignoring the group format specification altogether.
4679   (let (group)
4680     (if regexp
4681         ;; This loop is used when listing groups that match some
4682         ;; regexp.
4683         (while groups
4684           (setq group (pop groups))
4685           (when (string-match regexp group)
4686             (gnus-add-text-properties
4687              (point) (prog1 (1+ (point))
4688                        (insert " " mark "     *: " group "\n"))
4689              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4690                    'gnus-unread t
4691                    'gnus-level level))))
4692       ;; This loop is used when listing all groups.
4693       (while groups
4694         (gnus-add-text-properties
4695          (point) (prog1 (1+ (point))
4696                    (insert " " mark "     *: "
4697                            (setq group (pop groups)) "\n"))
4698          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4699                'gnus-unread t
4700                'gnus-level level))))))
4701
4702 (defmacro gnus-group-real-name (group)
4703   "Find the real name of a foreign newsgroup."
4704   `(let ((gname ,group))
4705      (if (string-match ":[^:]+$" gname)
4706          (substring gname (1+ (match-beginning 0)))
4707        gname)))
4708
4709 (defsubst gnus-server-add-address (method)
4710   (let ((method-name (symbol-name (car method))))
4711     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4712              (not (assq (intern (concat method-name "-address")) method)))
4713         (append method (list (list (intern (concat method-name "-address"))
4714                                    (nth 1 method))))
4715       method)))
4716
4717 (defsubst gnus-server-get-method (group method)
4718   ;; Input either a server name, and extended server name, or a
4719   ;; select method, and return a select method.
4720   (cond ((stringp method)
4721          (gnus-server-to-method method))
4722         ((equal method gnus-select-method)
4723          gnus-select-method)
4724         ((and (stringp (car method)) group)
4725          (gnus-server-extend-method group method))
4726         ((and method (not group)
4727               (equal (cadr method) ""))
4728          method)
4729         (t
4730          (gnus-server-add-address method))))
4731
4732 (defun gnus-server-to-method (server)
4733   "Map virtual server names to select methods."
4734   (or 
4735    ;; Is this a method, perhaps?
4736    (and server (listp server) server)
4737    ;; Perhaps this is the native server?
4738    (and (equal server "native") gnus-select-method)
4739    ;; It should be in the server alist.
4740    (cdr (assoc server gnus-server-alist))
4741    ;; If not, we look through all the opened server
4742    ;; to see whether we can find it there.
4743    (let ((opened gnus-opened-servers))
4744      (while (and opened
4745                  (not (equal server (format "%s:%s" (caaar opened)
4746                                             (cadaar opened)))))
4747        (pop opened))
4748      (caar opened))))
4749
4750 (defmacro gnus-method-equal (ss1 ss2)
4751   "Say whether two servers are equal."
4752   `(let ((s1 ,ss1)
4753          (s2 ,ss2))
4754      (or (equal s1 s2)
4755          (and (= (length s1) (length s2))
4756               (progn
4757                 (while (and s1 (member (car s1) s2))
4758                   (setq s1 (cdr s1)))
4759                 (null s1))))))
4760
4761 (defun gnus-server-equal (m1 m2)
4762   "Say whether two methods are equal."
4763   (let ((m1 (cond ((null m1) gnus-select-method)
4764                   ((stringp m1) (gnus-server-to-method m1))
4765                   (t m1)))
4766         (m2 (cond ((null m2) gnus-select-method)
4767                   ((stringp m2) (gnus-server-to-method m2))
4768                   (t m2))))
4769     (gnus-method-equal m1 m2)))
4770
4771 (defun gnus-servers-using-backend (backend)
4772   "Return a list of known servers using BACKEND."
4773   (let ((opened gnus-opened-servers)
4774         out)
4775     (while opened
4776       (when (eq backend (caaar opened))
4777         (push (caar opened) out))
4778       (pop opened))
4779     out))
4780
4781 (defun gnus-archive-server-wanted-p ()
4782   "Say whether the user wants to use the archive server."
4783   (cond 
4784    ((not gnus-message-archive-method)
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 level) 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))
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 killed 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 '(score bookmark))
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          (or (gnus-summary-first-unread-article)
10452              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10453     (gnus-summary-position-point)
10454     (gnus-message 6 "Wrapped"))
10455    ;; Try to get next/previous article not displayed in this group.
10456    ((and gnus-auto-extend-newsgroup
10457          (not unread) (not subject))
10458     (gnus-summary-goto-article
10459      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10460      nil t))
10461    ;; Go to next/previous group.
10462    (t
10463     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10464         (gnus-summary-jump-to-group gnus-newsgroup-name))
10465     (let ((cmd last-command-char)
10466           (group
10467            (if (eq gnus-keep-same-level 'best)
10468                (gnus-summary-best-group gnus-newsgroup-name)
10469              (gnus-summary-search-group backward gnus-keep-same-level))))
10470       ;; For some reason, the group window gets selected.  We change
10471       ;; it back.
10472       (select-window (get-buffer-window (current-buffer)))
10473       ;; Select next unread newsgroup automagically.
10474       (cond
10475        ((or (not gnus-auto-select-next)
10476             (not cmd))
10477         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10478        ((or (eq gnus-auto-select-next 'quietly)
10479             (and (eq gnus-auto-select-next 'slightly-quietly)
10480                  push)
10481             (and (eq gnus-auto-select-next 'almost-quietly)
10482                  (gnus-summary-last-article-p)))
10483         ;; Select quietly.
10484         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10485             (gnus-summary-exit)
10486           (gnus-message 7 "No more%s articles (%s)..."
10487                         (if unread " unread" "")
10488                         (if group (concat "selecting " group)
10489                           "exiting"))
10490           (gnus-summary-next-group nil group backward)))
10491        (t
10492         (gnus-summary-walk-group-buffer
10493          gnus-newsgroup-name cmd unread backward)))))))
10494
10495 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10496   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10497                       (?\C-p (gnus-group-prev-unread-group 1))))
10498         keve key group ended)
10499     (save-excursion
10500       (set-buffer gnus-group-buffer)
10501       (gnus-summary-jump-to-group from-group)
10502       (setq group
10503             (if (eq gnus-keep-same-level 'best)
10504                 (gnus-summary-best-group gnus-newsgroup-name)
10505               (gnus-summary-search-group backward gnus-keep-same-level))))
10506     (while (not ended)
10507       (gnus-message
10508        5 "No more%s articles%s" (if unread " unread" "")
10509        (if (and group
10510                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10511            (format " (Type %s for %s [%s])"
10512                    (single-key-description cmd) group
10513                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10514          (format " (Type %s to exit %s)"
10515                  (single-key-description cmd)
10516                  gnus-newsgroup-name)))
10517       ;; Confirm auto selection.
10518       (setq key (car (setq keve (gnus-read-event-char))))
10519       (setq ended t)
10520       (cond
10521        ((assq key keystrokes)
10522         (let ((obuf (current-buffer)))
10523           (switch-to-buffer gnus-group-buffer)
10524           (and group
10525                (gnus-group-jump-to-group group))
10526           (eval (cadr (assq key keystrokes)))
10527           (setq group (gnus-group-group-name))
10528           (switch-to-buffer obuf))
10529         (setq ended nil))
10530        ((equal key cmd)
10531         (if (or (not group)
10532                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10533             (gnus-summary-exit)
10534           (gnus-summary-next-group nil group backward)))
10535        (t
10536         (push (cdr keve) unread-command-events))))))
10537
10538 (defun gnus-read-event-char ()
10539   "Get the next event."
10540   (let ((event (read-event)))
10541     (cons (and (numberp event) event) event)))
10542
10543 (defun gnus-summary-next-unread-article ()
10544   "Select unread article after current one."
10545   (interactive)
10546   (gnus-summary-next-article t (and gnus-auto-select-same
10547                                     (gnus-summary-article-subject))))
10548
10549 (defun gnus-summary-prev-article (&optional unread subject)
10550   "Select the article after the current one.
10551 If UNREAD is non-nil, only unread articles are selected."
10552   (interactive "P")
10553   (gnus-summary-next-article unread subject t))
10554
10555 (defun gnus-summary-prev-unread-article ()
10556   "Select unred article before current one."
10557   (interactive)
10558   (gnus-summary-prev-article t (and gnus-auto-select-same
10559                                     (gnus-summary-article-subject))))
10560
10561 (defun gnus-summary-next-page (&optional lines circular)
10562   "Show next page of the selected article.
10563 If at the end of the current article, select the next article.
10564 LINES says how many lines should be scrolled up.
10565
10566 If CIRCULAR is non-nil, go to the start of the article instead of
10567 selecting the next article when reaching the end of the current
10568 article."
10569   (interactive "P")
10570   (setq gnus-summary-buffer (current-buffer))
10571   (gnus-set-global-variables)
10572   (let ((article (gnus-summary-article-number))
10573         (endp nil))
10574     (gnus-configure-windows 'article)
10575     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10576         (if (and (eq gnus-summary-goto-unread 'never)
10577                  (not (gnus-summary-last-article-p article)))
10578             (gnus-summary-next-article)
10579           (gnus-summary-next-unread-article))
10580       (if (or (null gnus-current-article)
10581               (null gnus-article-current)
10582               (/= article (cdr gnus-article-current))
10583               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10584           ;; Selected subject is different from current article's.
10585           (gnus-summary-display-article article)
10586         (gnus-eval-in-buffer-window gnus-article-buffer
10587           (setq endp (gnus-article-next-page lines)))
10588         (if endp
10589             (cond (circular
10590                    (gnus-summary-beginning-of-article))
10591                   (lines
10592                    (gnus-message 3 "End of message"))
10593                   ((null lines)
10594                    (if (and (eq gnus-summary-goto-unread 'never)
10595                             (not (gnus-summary-last-article-p article)))
10596                        (gnus-summary-next-article)
10597                      (gnus-summary-next-unread-article)))))))
10598     (gnus-summary-recenter)
10599     (gnus-summary-position-point)))
10600
10601 (defun gnus-summary-prev-page (&optional lines)
10602   "Show previous page of selected article.
10603 Argument LINES specifies lines to be scrolled down."
10604   (interactive "P")
10605   (gnus-set-global-variables)
10606   (let ((article (gnus-summary-article-number)))
10607     (gnus-configure-windows 'article)
10608     (if (or (null gnus-current-article)
10609             (null gnus-article-current)
10610             (/= article (cdr gnus-article-current))
10611             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10612         ;; Selected subject is different from current article's.
10613         (gnus-summary-display-article article)
10614       (gnus-summary-recenter)
10615       (gnus-eval-in-buffer-window gnus-article-buffer
10616         (gnus-article-prev-page lines))))
10617   (gnus-summary-position-point))
10618
10619 (defun gnus-summary-scroll-up (lines)
10620   "Scroll up (or down) one line current article.
10621 Argument LINES specifies lines to be scrolled up (or down if negative)."
10622   (interactive "p")
10623   (gnus-set-global-variables)
10624   (gnus-configure-windows 'article)
10625   (gnus-summary-show-thread)
10626   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10627     (gnus-eval-in-buffer-window gnus-article-buffer
10628       (cond ((> lines 0)
10629              (if (gnus-article-next-page lines)
10630                  (gnus-message 3 "End of message")))
10631             ((< lines 0)
10632              (gnus-article-prev-page (- lines))))))
10633   (gnus-summary-recenter)
10634   (gnus-summary-position-point))
10635
10636 (defun gnus-summary-next-same-subject ()
10637   "Select next article which has the same subject as current one."
10638   (interactive)
10639   (gnus-set-global-variables)
10640   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10641
10642 (defun gnus-summary-prev-same-subject ()
10643   "Select previous article which has the same subject as current one."
10644   (interactive)
10645   (gnus-set-global-variables)
10646   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10647
10648 (defun gnus-summary-next-unread-same-subject ()
10649   "Select next unread article which has the same subject as current one."
10650   (interactive)
10651   (gnus-set-global-variables)
10652   (gnus-summary-next-article t (gnus-summary-article-subject)))
10653
10654 (defun gnus-summary-prev-unread-same-subject ()
10655   "Select previous unread article which has the same subject as current one."
10656   (interactive)
10657   (gnus-set-global-variables)
10658   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10659
10660 (defun gnus-summary-first-unread-article ()
10661   "Select the first unread article.
10662 Return nil if there are no unread articles."
10663   (interactive)
10664   (gnus-set-global-variables)
10665   (prog1
10666       (if (gnus-summary-first-subject t)
10667           (progn
10668             (gnus-summary-show-thread)
10669             (gnus-summary-first-subject t)
10670             (gnus-summary-display-article (gnus-summary-article-number))))
10671     (gnus-summary-position-point)))
10672
10673 (defun gnus-summary-best-unread-article ()
10674   "Select the unread article with the highest score."
10675   (interactive)
10676   (gnus-set-global-variables)
10677   (let ((best -1000000)
10678         (data gnus-newsgroup-data)
10679         article score)
10680     (while data
10681       (and (gnus-data-unread-p (car data))
10682            (> (setq score
10683                     (gnus-summary-article-score (gnus-data-number (car data))))
10684               best)
10685            (setq best score
10686                  article (gnus-data-number (car data))))
10687       (setq data (cdr data)))
10688     (prog1
10689         (if article
10690             (gnus-summary-goto-article article)
10691           (error "No unread articles"))
10692       (gnus-summary-position-point))))
10693
10694 (defun gnus-summary-last-subject ()
10695   "Go to the last displayed subject line in the group."
10696   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10697     (when article
10698       (gnus-summary-goto-subject article))))
10699
10700 (defun gnus-summary-goto-article (article &optional all-headers force)
10701   "Fetch ARTICLE and display it if it exists.
10702 If ALL-HEADERS is non-nil, no header lines are hidden."
10703   (interactive
10704    (list
10705     (string-to-int
10706      (completing-read
10707       "Article number: "
10708       (mapcar (lambda (number) (list (int-to-string number)))
10709               gnus-newsgroup-limit)))
10710     current-prefix-arg
10711     t))
10712   (prog1
10713       (if (gnus-summary-goto-subject article force)
10714           (gnus-summary-display-article article all-headers)
10715         (gnus-message 4 "Couldn't go to article %s" article) nil)
10716     (gnus-summary-position-point)))
10717
10718 (defun gnus-summary-goto-last-article ()
10719   "Go to the previously read article."
10720   (interactive)
10721   (prog1
10722       (and gnus-last-article
10723            (gnus-summary-goto-article gnus-last-article))
10724     (gnus-summary-position-point)))
10725
10726 (defun gnus-summary-pop-article (number)
10727   "Pop one article off the history and go to the previous.
10728 NUMBER articles will be popped off."
10729   (interactive "p")
10730   (let (to)
10731     (setq gnus-newsgroup-history
10732           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10733     (if to
10734         (gnus-summary-goto-article (car to))
10735       (error "Article history empty")))
10736   (gnus-summary-position-point))
10737
10738 ;; Summary commands and functions for limiting the summary buffer.
10739
10740 (defun gnus-summary-limit-to-articles (n)
10741   "Limit the summary buffer to the next N articles.
10742 If not given a prefix, use the process marked articles instead."
10743   (interactive "P")
10744   (gnus-set-global-variables)
10745   (prog1
10746       (let ((articles (gnus-summary-work-articles n)))
10747         (setq gnus-newsgroup-processable nil)
10748         (gnus-summary-limit articles))
10749     (gnus-summary-position-point)))
10750
10751 (defun gnus-summary-pop-limit (&optional total)
10752   "Restore the previous limit.
10753 If given a prefix, remove all limits."
10754   (interactive "P")
10755   (gnus-set-global-variables)
10756   (when total 
10757     (setq gnus-newsgroup-limits
10758           (list (mapcar (lambda (h) (mail-header-number h))
10759                         gnus-newsgroup-headers))))
10760   (unless gnus-newsgroup-limits
10761     (error "No limit to pop"))
10762   (prog1
10763       (gnus-summary-limit nil 'pop)
10764     (gnus-summary-position-point)))
10765
10766 (defun gnus-summary-limit-to-subject (subject &optional header)
10767   "Limit the summary buffer to articles that have subjects that match a regexp."
10768   (interactive "sRegexp: ")
10769   (unless header
10770     (setq header "subject"))
10771   (when (not (equal "" subject))
10772     (prog1
10773         (let ((articles (gnus-summary-find-matching
10774                          (or header "subject") subject 'all)))
10775           (or articles (error "Found no matches for \"%s\"" subject))
10776           (gnus-summary-limit articles))
10777       (gnus-summary-position-point))))
10778
10779 (defun gnus-summary-limit-to-author (from)
10780   "Limit the summary buffer to articles that have authors that match a regexp."
10781   (interactive "sRegexp: ")
10782   (gnus-summary-limit-to-subject from "from"))
10783
10784 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10785 (make-obsolete
10786  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10787
10788 (defun gnus-summary-limit-to-unread (&optional all)
10789   "Limit the summary buffer to articles that are not marked as read.
10790 If ALL is non-nil, limit strictly to unread articles."
10791   (interactive "P")
10792   (if all
10793       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10794     (gnus-summary-limit-to-marks
10795      ;; Concat all the marks that say that an article is read and have
10796      ;; those removed.
10797      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10798            gnus-killed-mark gnus-kill-file-mark
10799            gnus-low-score-mark gnus-expirable-mark
10800            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10801      'reverse)))
10802
10803 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10804 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10805
10806 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10807   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10808 If REVERSE, limit the summary buffer to articles that are not marked
10809 with MARKS.  MARKS can either be a string of marks or a list of marks.
10810 Returns how many articles were removed."
10811   (interactive "sMarks: ")
10812   (gnus-set-global-variables)
10813   (prog1
10814       (let ((data gnus-newsgroup-data)
10815             (marks (if (listp marks) marks
10816                      (append marks nil))) ; Transform to list.
10817             articles)
10818         (while data
10819           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10820                  (memq (gnus-data-mark (car data)) marks))
10821                (setq articles (cons (gnus-data-number (car data)) articles)))
10822           (setq data (cdr data)))
10823         (gnus-summary-limit articles))
10824     (gnus-summary-position-point)))
10825
10826 (defun gnus-summary-limit-to-score (&optional score)
10827   "Limit to articles with score at or above SCORE."
10828   (interactive "P")
10829   (gnus-set-global-variables)
10830   (setq score (if score
10831                   (prefix-numeric-value score)
10832                 (or gnus-summary-default-score 0)))
10833   (let ((data gnus-newsgroup-data)
10834         articles)
10835     (while data
10836       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10837                 score)
10838         (push (gnus-data-number (car data)) articles))
10839       (setq data (cdr data)))
10840     (prog1
10841         (gnus-summary-limit articles)
10842       (gnus-summary-position-point))))
10843
10844 (defun gnus-summary-limit-include-dormant ()
10845   "Display all the hidden articles that are marked as dormant."
10846   (interactive)
10847   (gnus-set-global-variables)
10848   (or gnus-newsgroup-dormant
10849       (error "There are no dormant articles in this group"))
10850   (prog1
10851       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10852     (gnus-summary-position-point)))
10853
10854 (defun gnus-summary-limit-exclude-dormant ()
10855   "Hide all dormant articles."
10856   (interactive)
10857   (gnus-set-global-variables)
10858   (prog1
10859       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10860     (gnus-summary-position-point)))
10861
10862 (defun gnus-summary-limit-exclude-childless-dormant ()
10863   "Hide all dormant articles that have no children."
10864   (interactive)
10865   (gnus-set-global-variables)
10866   (let ((data (gnus-data-list t))
10867         articles d children)
10868     ;; Find all articles that are either not dormant or have
10869     ;; children.
10870     (while (setq d (pop data))
10871       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10872                 (and (setq children 
10873                            (gnus-article-children (gnus-data-number d)))
10874                      (let (found)
10875                        (while children
10876                          (when (memq (car children) articles)
10877                            (setq children nil
10878                                  found t))
10879                          (pop children))
10880                        found)))
10881         (push (gnus-data-number d) articles)))
10882     ;; Do the limiting.
10883     (prog1
10884         (gnus-summary-limit articles)
10885       (gnus-summary-position-point))))
10886
10887 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10888   "Mark all unread excluded articles as read.
10889 If ALL, mark even excluded ticked and dormants as read."
10890   (interactive "P")
10891   (let ((articles (gnus-sorted-complement
10892                    (sort
10893                     (mapcar (lambda (h) (mail-header-number h))
10894                             gnus-newsgroup-headers)
10895                     '<)
10896                    (sort gnus-newsgroup-limit '<)))
10897         article)
10898     (setq gnus-newsgroup-unreads nil)
10899     (if all
10900         (setq gnus-newsgroup-dormant nil
10901               gnus-newsgroup-marked nil
10902               gnus-newsgroup-reads
10903               (nconc
10904                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10905                gnus-newsgroup-reads))
10906       (while (setq article (pop articles))
10907         (unless (or (memq article gnus-newsgroup-dormant)
10908                     (memq article gnus-newsgroup-marked))
10909           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10910
10911 (defun gnus-summary-limit (articles &optional pop)
10912   (if pop
10913       ;; We pop the previous limit off the stack and use that.
10914       (setq articles (car gnus-newsgroup-limits)
10915             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10916     ;; We use the new limit, so we push the old limit on the stack.
10917     (setq gnus-newsgroup-limits
10918           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10919   ;; Set the limit.
10920   (setq gnus-newsgroup-limit articles)
10921   (let ((total (length gnus-newsgroup-data))
10922         (data (gnus-data-find-list (gnus-summary-article-number)))
10923         found)
10924     ;; This will do all the work of generating the new summary buffer
10925     ;; according to the new limit.
10926     (gnus-summary-prepare)
10927     ;; Hide any threads, possibly.
10928     (and gnus-show-threads
10929          gnus-thread-hide-subtree
10930          (gnus-summary-hide-all-threads))
10931     ;; Try to return to the article you were at, or one in the
10932     ;; neighborhood.
10933     (if data
10934         ;; We try to find some article after the current one.
10935         (while data
10936           (and (gnus-summary-goto-subject
10937                 (gnus-data-number (car data)) nil t)
10938                (setq data nil
10939                      found t))
10940           (setq data (cdr data))))
10941     (or found
10942         ;; If there is no data, that means that we were after the last
10943         ;; article.  The same goes when we can't find any articles
10944         ;; after the current one.
10945         (progn
10946           (goto-char (point-max))
10947           (gnus-summary-find-prev)))
10948     ;; We return how many articles were removed from the summary
10949     ;; buffer as a result of the new limit.
10950     (- total (length gnus-newsgroup-data))))
10951
10952 (defsubst gnus-invisible-cut-children (threads)
10953   (let ((num 0))
10954     (while threads
10955       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
10956         (incf num))
10957       (pop threads))
10958     (< num 2)))
10959
10960 (defsubst gnus-cut-thread (thread)
10961   "Go forwards in the thread until we find an article that we want to display."
10962   (when (or (eq gnus-fetch-old-headers 'some)
10963             (eq gnus-build-sparse-threads 'some)
10964             (eq gnus-build-sparse-threads 'more))
10965     ;; Deal with old-fetched headers and sparse threads.
10966     (while (and
10967             thread
10968             (or
10969              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10970              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
10971             (or (<= (length (cdr thread)) 1)
10972                 (gnus-invisible-cut-children (cdr thread))))
10973       (setq thread (cadr thread))))
10974   thread)
10975
10976 (defun gnus-cut-threads (threads)
10977   "Cut off all uninteresting articles from the beginning of threads."
10978   (when (or (eq gnus-fetch-old-headers 'some)
10979             (eq gnus-build-sparse-threads 'some)
10980             (eq gnus-build-sparse-threads 'more))
10981     (let ((th threads))
10982       (while th
10983         (setcar th (gnus-cut-thread (car th)))
10984         (setq th (cdr th)))))
10985   ;; Remove nixed out threads.
10986   (delq nil threads))
10987
10988 (defun gnus-summary-initial-limit (&optional show-if-empty)
10989   "Figure out what the initial limit is supposed to be on group entry.
10990 This entails weeding out unwanted dormants, low-scored articles,
10991 fetch-old-headers verbiage, and so on."
10992   ;; Most groups have nothing to remove.
10993   (if (or gnus-inhibit-limiting
10994           (and (null gnus-newsgroup-dormant)
10995                (not (eq gnus-fetch-old-headers 'some))
10996                (null gnus-summary-expunge-below)
10997                (not (eq gnus-build-sparse-threads 'some))
10998                (not (eq gnus-build-sparse-threads 'more))
10999                (null gnus-thread-expunge-below)
11000                (not gnus-use-nocem)))
11001       () ; Do nothing.
11002     (push gnus-newsgroup-limit gnus-newsgroup-limits)
11003     (setq gnus-newsgroup-limit nil)
11004     (mapatoms
11005      (lambda (node)
11006        (unless (car (symbol-value node))
11007          ;; These threads have no parents -- they are roots.
11008          (let ((nodes (cdr (symbol-value node)))
11009                thread)
11010            (while nodes
11011              (if (and gnus-thread-expunge-below
11012                       (< (gnus-thread-total-score (car nodes))
11013                          gnus-thread-expunge-below))
11014                  (gnus-expunge-thread (pop nodes))
11015                (setq thread (pop nodes))
11016                (gnus-summary-limit-children thread))))))
11017      gnus-newsgroup-dependencies)
11018     ;; If this limitation resulted in an empty group, we might
11019     ;; pop the previous limit and use it instead.
11020     (when (and (not gnus-newsgroup-limit)
11021                show-if-empty)
11022       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
11023     gnus-newsgroup-limit))
11024
11025 (defun gnus-summary-limit-children (thread)
11026   "Return 1 if this subthread is visible and 0 if it is not."
11027   ;; First we get the number of visible children to this thread.  This
11028   ;; is done by recursing down the thread using this function, so this
11029   ;; will really go down to a leaf article first, before slowly
11030   ;; working its way up towards the root.
11031   (when thread
11032     (let ((children
11033            (if (cdr thread)
11034                (apply '+ (mapcar 'gnus-summary-limit-children
11035                                  (cdr thread)))
11036              0))
11037           (number (mail-header-number (car thread)))
11038           score)
11039       (if (or
11040            ;; If this article is dormant and has absolutely no visible
11041            ;; children, then this article isn't visible.
11042            (and (memq number gnus-newsgroup-dormant)
11043                 (= children 0))
11044            ;; If this is "fetch-old-headered" and there is only one
11045            ;; visible child (or less), then we don't want this article.
11046            (and (eq gnus-fetch-old-headers 'some)
11047                 (memq number gnus-newsgroup-ancient)
11048                 (zerop children))
11049            ;; If this is a sparsely inserted article with no children,
11050            ;; we don't want it.
11051            (and (eq gnus-build-sparse-threads 'some)
11052                 (memq number gnus-newsgroup-sparse)
11053                 (zerop children))
11054            ;; If we use expunging, and this article is really
11055            ;; low-scored, then we don't want this article.
11056            (when (and gnus-summary-expunge-below
11057                       (< (setq score
11058                                (or (cdr (assq number gnus-newsgroup-scored))
11059                                    gnus-summary-default-score))
11060                          gnus-summary-expunge-below))
11061              ;; We increase the expunge-tally here, but that has
11062              ;; nothing to do with the limits, really.
11063              (incf gnus-newsgroup-expunged-tally)
11064              ;; We also mark as read here, if that's wanted.
11065              (when (and gnus-summary-mark-below
11066                         (< score gnus-summary-mark-below))
11067                (setq gnus-newsgroup-unreads
11068                      (delq number gnus-newsgroup-unreads))
11069                (if gnus-newsgroup-auto-expire
11070                    (push number gnus-newsgroup-expirable)
11071                  (push (cons number gnus-low-score-mark)
11072                        gnus-newsgroup-reads)))
11073              t)
11074            (and gnus-use-nocem
11075                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
11076           ;; Nope, invisible article.
11077           0
11078         ;; Ok, this article is to be visible, so we add it to the limit
11079         ;; and return 1.
11080         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
11081         1))))
11082
11083 (defun gnus-expunge-thread (thread)
11084   "Mark all articles in THREAD as read."
11085   (let* ((number (mail-header-number (car thread))))
11086     (incf gnus-newsgroup-expunged-tally)
11087     ;; We also mark as read here, if that's wanted.
11088     (setq gnus-newsgroup-unreads
11089           (delq number gnus-newsgroup-unreads))
11090     (if gnus-newsgroup-auto-expire
11091         (push number gnus-newsgroup-expirable)
11092       (push (cons number gnus-low-score-mark)
11093             gnus-newsgroup-reads)))
11094   ;; Go recursively through all subthreads.
11095   (mapcar 'gnus-expunge-thread (cdr thread)))
11096
11097 ;; Summary article oriented commands
11098
11099 (defun gnus-summary-refer-parent-article (n)
11100   "Refer parent article N times.
11101 The difference between N and the number of articles fetched is returned."
11102   (interactive "p")
11103   (gnus-set-global-variables)
11104   (while
11105       (and
11106        (> n 0)
11107        (let* ((header (gnus-summary-article-header))
11108               (ref
11109                ;; If we try to find the parent of the currently
11110                ;; displayed article, then we take a look at the actual
11111                ;; References header, since this is slightly more
11112                ;; reliable than the References field we got from the
11113                ;; server.
11114                (if (and (eq (mail-header-number header)
11115                             (cdr gnus-article-current))
11116                         (equal gnus-newsgroup-name
11117                                (car gnus-article-current)))
11118                    (save-excursion
11119                      (set-buffer gnus-original-article-buffer)
11120                      (nnheader-narrow-to-headers)
11121                      (prog1
11122                          (message-fetch-field "references")
11123                        (widen)))
11124                  ;; It's not the current article, so we take a bet on
11125                  ;; the value we got from the server.
11126                  (mail-header-references header))))
11127          (if (setq ref (or ref (mail-header-references header)))
11128              (or (gnus-summary-refer-article (gnus-parent-id ref))
11129                  (gnus-message 1 "Couldn't find parent"))
11130            (gnus-message 1 "No references in article %d"
11131                          (gnus-summary-article-number))
11132            nil)))
11133     (setq n (1- n)))
11134   (gnus-summary-position-point)
11135   n)
11136
11137 (defun gnus-summary-refer-references ()
11138   "Fetch all articles mentioned in the References header.
11139 Return how many articles were fetched."
11140   (interactive)
11141   (gnus-set-global-variables)
11142   (let ((ref (mail-header-references (gnus-summary-article-header)))
11143         (current (gnus-summary-article-number))
11144         (n 0))
11145     ;; For each Message-ID in the References header...
11146     (while (string-match "<[^>]*>" ref)
11147       (incf n)
11148       ;; ... fetch that article.
11149       (gnus-summary-refer-article
11150        (prog1 (match-string 0 ref)
11151          (setq ref (substring ref (match-end 0))))))
11152     (gnus-summary-goto-subject current)
11153     (gnus-summary-position-point)
11154     n))
11155
11156 (defun gnus-summary-refer-article (message-id)
11157   "Fetch an article specified by MESSAGE-ID."
11158   (interactive "sMessage-ID: ")
11159   (when (and (stringp message-id)
11160              (not (zerop (length message-id))))
11161     ;; Construct the correct Message-ID if necessary.
11162     ;; Suggested by tale@pawl.rpi.edu.
11163     (unless (string-match "^<" message-id)
11164       (setq message-id (concat "<" message-id)))
11165     (unless (string-match ">$" message-id)
11166       (setq message-id (concat message-id ">")))
11167     (let* ((header (gnus-id-to-header message-id))
11168            (sparse (and header
11169                         (memq (mail-header-number header)
11170                               gnus-newsgroup-sparse))))
11171       (if header
11172           (prog1
11173               ;; The article is present in the buffer, to we just go to it.
11174               (gnus-summary-goto-article 
11175                (mail-header-number header) nil header)
11176             (when sparse
11177               (gnus-summary-update-article (mail-header-number header))))
11178         ;; We fetch the article
11179         (let ((gnus-override-method 
11180                (and (gnus-news-group-p gnus-newsgroup-name)
11181                     gnus-refer-article-method))
11182               number)
11183           ;; Start the special refer-article method, if necessary.
11184           (when (and gnus-refer-article-method
11185                      (gnus-news-group-p gnus-newsgroup-name))
11186             (gnus-check-server gnus-refer-article-method))
11187           ;; Fetch the header, and display the article.
11188           (if (setq number (gnus-summary-insert-subject message-id))
11189               (gnus-summary-select-article nil nil nil number)
11190             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11191
11192 (defun gnus-summary-enter-digest-group (&optional force)
11193   "Enter a digest group based on the current article."
11194   (interactive "P")
11195   (gnus-set-global-variables)
11196   (gnus-summary-select-article)
11197   (let ((name (format "%s-%d"
11198                       (gnus-group-prefixed-name
11199                        gnus-newsgroup-name (list 'nndoc ""))
11200                       gnus-current-article))
11201         (ogroup gnus-newsgroup-name)
11202         (case-fold-search t)
11203         (buf (current-buffer))
11204         dig)
11205     (save-excursion
11206       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11207       (insert-buffer-substring gnus-original-article-buffer)
11208       (narrow-to-region
11209        (goto-char (point-min))
11210        (or (search-forward "\n\n" nil t) (point)))
11211       (goto-char (point-min))
11212       (delete-matching-lines "^\\(Path\\):\\|^From ")
11213       (widen))
11214     (unwind-protect
11215         (if (gnus-group-read-ephemeral-group
11216              name `(nndoc ,name (nndoc-address
11217                                  ,(get-buffer dig))
11218                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11219             ;; Make all postings to this group go to the parent group.
11220             (nconc (gnus-info-params (gnus-get-info name))
11221                    (list (cons 'to-group ogroup)))
11222           ;; Couldn't select this doc group.
11223           (switch-to-buffer buf)
11224           (gnus-set-global-variables)
11225           (gnus-configure-windows 'summary)
11226           (gnus-message 3 "Article couldn't be entered?"))
11227       (kill-buffer dig))))
11228
11229 (defun gnus-summary-isearch-article (&optional regexp-p)
11230   "Do incremental search forward on the current article.
11231 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11232   (interactive "P")
11233   (gnus-set-global-variables)
11234   (gnus-summary-select-article)
11235   (gnus-configure-windows 'article)
11236   (gnus-eval-in-buffer-window gnus-article-buffer
11237     (goto-char (point-min))
11238     (isearch-forward regexp-p)))
11239
11240 (defun gnus-summary-search-article-forward (regexp &optional backward)
11241   "Search for an article containing REGEXP forward.
11242 If BACKWARD, search backward instead."
11243   (interactive
11244    (list (read-string
11245           (format "Search article %s (regexp%s): "
11246                   (if current-prefix-arg "backward" "forward")
11247                   (if gnus-last-search-regexp
11248                       (concat ", default " gnus-last-search-regexp)
11249                     "")))
11250          current-prefix-arg))
11251   (gnus-set-global-variables)
11252   (if (string-equal regexp "")
11253       (setq regexp (or gnus-last-search-regexp ""))
11254     (setq gnus-last-search-regexp regexp))
11255   (unless (gnus-summary-search-article regexp backward)
11256     (error "Search failed: \"%s\"" regexp)))
11257
11258 (defun gnus-summary-search-article-backward (regexp)
11259   "Search for an article containing REGEXP backward."
11260   (interactive
11261    (list (read-string
11262           (format "Search article backward (regexp%s): "
11263                   (if gnus-last-search-regexp
11264                       (concat ", default " gnus-last-search-regexp)
11265                     "")))))
11266   (gnus-summary-search-article-forward regexp 'backward))
11267
11268 (defun gnus-summary-search-article (regexp &optional backward)
11269   "Search for an article containing REGEXP.
11270 Optional argument BACKWARD means do search for backward.
11271 `gnus-select-article-hook' is not called during the search."
11272   (let ((gnus-select-article-hook nil)  ;Disable hook.
11273         (gnus-article-display-hook nil)
11274         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11275         (re-search
11276          (if backward
11277              're-search-backward 're-search-forward))
11278         (sum (current-buffer))
11279         (found nil))
11280     (gnus-save-hidden-threads
11281       (gnus-summary-select-article)
11282       (set-buffer gnus-article-buffer)
11283       (when backward
11284         (forward-line -1))
11285       (while (not found)
11286         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11287         (if (if backward
11288                 (re-search-backward regexp nil t)
11289               (re-search-forward regexp nil t))
11290             ;; We found the regexp.
11291             (progn
11292               (setq found 'found)
11293               (beginning-of-line)
11294               (set-window-start
11295                (get-buffer-window (current-buffer))
11296                (point))
11297               (forward-line 1)
11298               (set-buffer sum))
11299           ;; We didn't find it, so we go to the next article.
11300           (set-buffer sum)
11301           (if (not (if backward (gnus-summary-find-prev)
11302                      (gnus-summary-find-next)))
11303               ;; No more articles.
11304               (setq found t)
11305             ;; Select the next article and adjust point.
11306             (gnus-summary-select-article)
11307             (set-buffer gnus-article-buffer)
11308             (widen)
11309             (goto-char (if backward (point-max) (point-min))))))
11310       (gnus-message 7 ""))
11311     ;; Return whether we found the regexp.
11312     (when (eq found 'found)
11313       (gnus-summary-show-thread)
11314       (gnus-summary-goto-subject gnus-current-article)
11315       (gnus-summary-position-point)
11316       t)))
11317
11318 (defun gnus-summary-find-matching (header regexp &optional backward unread
11319                                           not-case-fold)
11320   "Return a list of all articles that match REGEXP on HEADER.
11321 The search stars on the current article and goes forwards unless
11322 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11323 If UNREAD is non-nil, only unread articles will
11324 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11325 in the comparisons."
11326   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11327                 (gnus-data-find-list
11328                  (gnus-summary-article-number) (gnus-data-list backward))))
11329         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11330         (case-fold-search (not not-case-fold))
11331         articles d)
11332     (or (fboundp (intern (concat "mail-header-" header)))
11333         (error "%s is not a valid header" header))
11334     (while data
11335       (setq d (car data))
11336       (and (or (not unread)             ; We want all articles...
11337                (gnus-data-unread-p d))  ; Or just unreads.
11338            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11339            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11340            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11341       (setq data (cdr data)))
11342     (nreverse articles)))
11343
11344 (defun gnus-summary-execute-command (header regexp command &optional backward)
11345   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11346 If HEADER is an empty string (or nil), the match is done on the entire
11347 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11348   (interactive
11349    (list (let ((completion-ignore-case t))
11350            (completing-read
11351             "Header name: "
11352             (mapcar (lambda (string) (list string))
11353                     '("Number" "Subject" "From" "Lines" "Date"
11354                       "Message-ID" "Xref" "References" "Body"))
11355             nil 'require-match))
11356          (read-string "Regexp: ")
11357          (read-key-sequence "Command: ")
11358          current-prefix-arg))
11359   (when (equal header "Body")
11360     (setq header ""))
11361   (gnus-set-global-variables)
11362   ;; Hidden thread subtrees must be searched as well.
11363   (gnus-summary-show-all-threads)
11364   ;; We don't want to change current point nor window configuration.
11365   (save-excursion
11366     (save-window-excursion
11367       (gnus-message 6 "Executing %s..." (key-description command))
11368       ;; We'd like to execute COMMAND interactively so as to give arguments.
11369       (gnus-execute header regexp
11370                     `(lambda () (call-interactively ',(key-binding command)))
11371                     backward)
11372       (gnus-message 6 "Executing %s...done" (key-description command)))))
11373
11374 (defun gnus-summary-beginning-of-article ()
11375   "Scroll the article back to the beginning."
11376   (interactive)
11377   (gnus-set-global-variables)
11378   (gnus-summary-select-article)
11379   (gnus-configure-windows 'article)
11380   (gnus-eval-in-buffer-window gnus-article-buffer
11381     (widen)
11382     (goto-char (point-min))
11383     (and gnus-break-pages (gnus-narrow-to-page))))
11384
11385 (defun gnus-summary-end-of-article ()
11386   "Scroll to the end of the article."
11387   (interactive)
11388   (gnus-set-global-variables)
11389   (gnus-summary-select-article)
11390   (gnus-configure-windows 'article)
11391   (gnus-eval-in-buffer-window gnus-article-buffer
11392     (widen)
11393     (goto-char (point-max))
11394     (recenter -3)
11395     (and gnus-break-pages (gnus-narrow-to-page))))
11396
11397 (defun gnus-summary-show-article (&optional arg)
11398   "Force re-fetching of the current article.
11399 If ARG (the prefix) is non-nil, show the raw article without any
11400 article massaging functions being run."
11401   (interactive "P")
11402   (gnus-set-global-variables)
11403   (if (not arg)
11404       ;; Select the article the normal way.
11405       (gnus-summary-select-article nil 'force)
11406     ;; Bind the article treatment functions to nil.
11407     (let ((gnus-have-all-headers t)
11408           gnus-article-display-hook
11409           gnus-article-prepare-hook
11410           gnus-break-pages
11411           gnus-visual)
11412       (gnus-summary-select-article nil 'force)))
11413   (gnus-summary-goto-subject gnus-current-article)
11414 ;  (gnus-configure-windows 'article)
11415   (gnus-summary-position-point))
11416
11417 (defun gnus-summary-verbose-headers (&optional arg)
11418   "Toggle permanent full header display.
11419 If ARG is a positive number, turn header display on.
11420 If ARG is a negative number, turn header display off."
11421   (interactive "P")
11422   (gnus-set-global-variables)
11423   (gnus-summary-toggle-header arg)
11424   (setq gnus-show-all-headers
11425         (cond ((or (not (numberp arg))
11426                    (zerop arg))
11427                (not gnus-show-all-headers))
11428               ((natnump arg)
11429                t))))
11430
11431 (defun gnus-summary-toggle-header (&optional arg)
11432   "Show the headers if they are hidden, or hide them if they are shown.
11433 If ARG is a positive number, show the entire header.
11434 If ARG is a negative number, hide the unwanted header lines."
11435   (interactive "P")
11436   (gnus-set-global-variables)
11437   (save-excursion
11438     (set-buffer gnus-article-buffer)
11439     (let* ((buffer-read-only nil)
11440            (inhibit-point-motion-hooks t)
11441            (hidden (text-property-any
11442                     (goto-char (point-min)) (search-forward "\n\n")
11443                     'invisible t))
11444            e)
11445       (goto-char (point-min))
11446       (when (search-forward "\n\n" nil t)
11447         (delete-region (point-min) (1- (point))))
11448       (goto-char (point-min))
11449       (save-excursion
11450         (set-buffer gnus-original-article-buffer)
11451         (goto-char (point-min))
11452         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11453       (insert-buffer-substring gnus-original-article-buffer 1 e)
11454       (let ((gnus-inhibit-hiding t))
11455         (run-hooks 'gnus-article-display-hook))
11456       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11457           (gnus-article-hide-headers)))))
11458
11459 (defun gnus-summary-show-all-headers ()
11460   "Make all header lines visible."
11461   (interactive)
11462   (gnus-set-global-variables)
11463   (gnus-article-show-all-headers))
11464
11465 (defun gnus-summary-toggle-mime (&optional arg)
11466   "Toggle MIME processing.
11467 If ARG is a positive number, turn MIME processing on."
11468   (interactive "P")
11469   (gnus-set-global-variables)
11470   (setq gnus-show-mime
11471         (if (null arg) (not gnus-show-mime)
11472           (> (prefix-numeric-value arg) 0)))
11473   (gnus-summary-select-article t 'force))
11474
11475 (defun gnus-summary-caesar-message (&optional arg)
11476   "Caesar rotate the current article by 13.
11477 The numerical prefix specifies how manu places to rotate each letter
11478 forward."
11479   (interactive "P")
11480   (gnus-set-global-variables)
11481   (gnus-summary-select-article)
11482   (let ((mail-header-separator ""))
11483     (gnus-eval-in-buffer-window gnus-article-buffer
11484       (save-restriction
11485         (widen)
11486         (let ((start (window-start))
11487               buffer-read-only)
11488           (message-caesar-buffer-body arg)
11489           (set-window-start (get-buffer-window (current-buffer)) start))))))
11490
11491 (defun gnus-summary-stop-page-breaking ()
11492   "Stop page breaking in the current article."
11493   (interactive)
11494   (gnus-set-global-variables)
11495   (gnus-summary-select-article)
11496   (gnus-eval-in-buffer-window gnus-article-buffer
11497     (widen)))
11498
11499 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11500   "Move the current article to a different newsgroup.
11501 If N is a positive number, move the N next articles.
11502 If N is a negative number, move the N previous articles.
11503 If N is nil and any articles have been marked with the process mark,
11504 move those articles instead.
11505 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11506 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11507 re-spool using this method.
11508
11509 For this function to work, both the current newsgroup and the
11510 newsgroup that you want to move to have to support the `request-move'
11511 and `request-accept' functions."
11512   (interactive "P")
11513   (unless action (setq action 'move))
11514   (gnus-set-global-variables)
11515   ;; Check whether the source group supports the required functions.
11516   (cond ((and (eq action 'move)
11517               (not (gnus-check-backend-function
11518                     'request-move-article gnus-newsgroup-name)))
11519          (error "The current group does not support article moving"))
11520         ((and (eq action 'crosspost)
11521               (not (gnus-check-backend-function
11522                     'request-replace-article gnus-newsgroup-name)))
11523          (error "The current group does not support article editing")))
11524   (let ((articles (gnus-summary-work-articles n))
11525         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11526         (names '((move "Move" "Moving")
11527                  (copy "Copy" "Copying")
11528                  (crosspost "Crosspost" "Crossposting")))
11529         (copy-buf (save-excursion
11530                     (nnheader-set-temp-buffer " *copy article*")))
11531         art-group to-method new-xref article to-groups)
11532     (unless (assq action names)
11533       (error "Unknown action %s" action))
11534     ;; Read the newsgroup name.
11535     (when (and (not to-newsgroup)
11536                (not select-method))
11537       (setq to-newsgroup
11538             (gnus-read-move-group-name
11539              (cadr (assq action names))
11540              (symbol-value (intern (format "gnus-current-%s-group" action)))
11541              articles prefix))
11542       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11543     (setq to-method (or select-method 
11544                         (gnus-group-name-to-method to-newsgroup)))
11545     ;; Check the method we are to move this article to...
11546     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11547         (error "%s does not support article copying" (car to-method)))
11548     (or (gnus-check-server to-method)
11549         (error "Can't open server %s" (car to-method)))
11550     (gnus-message 6 "%s to %s: %s..."
11551                   (caddr (assq action names))
11552                   (or (car select-method) to-newsgroup) articles)
11553     (while articles
11554       (setq article (pop articles))
11555       (setq
11556        art-group
11557        (cond
11558         ;; Move the article.
11559         ((eq action 'move)
11560          (gnus-request-move-article
11561           article                       ; Article to move
11562           gnus-newsgroup-name           ; From newsgrouo
11563           (nth 1 (gnus-find-method-for-group
11564                   gnus-newsgroup-name)) ; Server
11565           (list 'gnus-request-accept-article
11566                 to-newsgroup (list 'quote select-method)
11567                 (not articles))         ; Accept form
11568           (not articles)))              ; Only save nov last time
11569         ;; Copy the article.
11570         ((eq action 'copy)
11571          (save-excursion
11572            (set-buffer copy-buf)
11573            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11574            (gnus-request-accept-article
11575             to-newsgroup select-method (not articles))))
11576         ;; Crosspost the article.
11577         ((eq action 'crosspost)
11578          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11579            (setq new-xref (concat gnus-newsgroup-name ":" article))
11580            (if (and xref (not (string= xref "")))
11581                (progn
11582                  (when (string-match "^Xref: " xref)
11583                    (setq xref (substring xref (match-end 0))))
11584                  (setq new-xref (concat xref " " new-xref)))
11585              (setq new-xref (concat (system-name) " " new-xref)))
11586            (save-excursion
11587              (set-buffer copy-buf)
11588              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11589              (nnheader-replace-header "xref" new-xref)
11590              (gnus-request-accept-article
11591               to-newsgroup select-method (not articles)))))))
11592       (if (not art-group)
11593           (gnus-message 1 "Couldn't %s article %s"
11594                         (cadr (assq action names)) article)
11595         (let* ((entry
11596                 (or
11597                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11598                  (gnus-gethash
11599                   (gnus-group-prefixed-name
11600                    (car art-group)
11601                    (or select-method 
11602                        (gnus-find-method-for-group to-newsgroup)))
11603                   gnus-newsrc-hashtb)))
11604                (info (nth 2 entry))
11605                (to-group (gnus-info-group info)))
11606           ;; Update the group that has been moved to.
11607           (when (and info
11608                      (memq action '(move copy)))
11609             (unless (member to-group to-groups)
11610               (push to-group to-groups))
11611
11612             (unless (memq article gnus-newsgroup-unreads)
11613               (gnus-info-set-read
11614                info (gnus-add-to-range (gnus-info-read info)
11615                                        (list (cdr art-group)))))
11616
11617             ;; Copy any marks over to the new group.
11618             (let ((marks gnus-article-mark-lists)
11619                   (to-article (cdr art-group)))
11620
11621               ;; See whether the article is to be put in the cache.
11622               (when gnus-use-cache
11623                 (gnus-cache-possibly-enter-article
11624                  to-group to-article
11625                  (let ((header (copy-sequence
11626                                 (gnus-summary-article-header article))))
11627                    (mail-header-set-number header to-article)
11628                    header)
11629                  (memq article gnus-newsgroup-marked)
11630                  (memq article gnus-newsgroup-dormant)
11631                  (memq article gnus-newsgroup-unreads)))
11632
11633               (while marks
11634                 (when (memq article (symbol-value
11635                                      (intern (format "gnus-newsgroup-%s"
11636                                                      (caar marks)))))
11637                   ;; If the other group is the same as this group,
11638                   ;; then we have to add the mark to the list.
11639                   (when (equal to-group gnus-newsgroup-name)
11640                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11641                          (cons to-article
11642                                (symbol-value
11643                                 (intern (format "gnus-newsgroup-%s"
11644                                                 (caar marks)))))))
11645                   ;; Copy mark to other group.
11646                   (gnus-add-marked-articles
11647                    to-group (cdar marks) (list to-article) info))
11648                 (setq marks (cdr marks)))))
11649
11650           ;; Update the Xref header in this article to point to
11651           ;; the new crossposted article we have just created.
11652           (when (eq action 'crosspost)
11653             (save-excursion
11654               (set-buffer copy-buf)
11655               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11656               (nnheader-replace-header
11657                "xref" (concat new-xref " " (gnus-group-prefixed-name
11658                                             (car art-group) to-method)
11659                               ":" (cdr art-group)))
11660               (gnus-request-replace-article
11661                article gnus-newsgroup-name (current-buffer)))))
11662
11663         (gnus-summary-goto-subject article)
11664         (when (eq action 'move)
11665           (gnus-summary-mark-article article gnus-canceled-mark)))
11666       (gnus-summary-remove-process-mark article))
11667     ;; Re-activate all groups that have been moved to.
11668     (while to-groups
11669       (gnus-activate-group (pop to-groups)))
11670     
11671     (gnus-kill-buffer copy-buf)
11672     (gnus-summary-position-point)
11673     (gnus-set-mode-line 'summary)))
11674
11675 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11676   "Move the current article to a different newsgroup.
11677 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11678 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11679 re-spool using this method."
11680   (interactive "P")
11681   (gnus-summary-move-article n nil select-method 'copy))
11682
11683 (defun gnus-summary-crosspost-article (&optional n)
11684   "Crosspost the current article to some other group."
11685   (interactive "P")
11686   (gnus-summary-move-article n nil nil 'crosspost))
11687
11688 (defvar gnus-summary-respool-default-method nil
11689   "Default method for respooling an article.  
11690 If nil, use to the current newsgroup method.")
11691
11692 (defun gnus-summary-respool-article (&optional n method)
11693   "Respool the current article.
11694 The article will be squeezed through the mail spooling process again,
11695 which means that it will be put in some mail newsgroup or other
11696 depending on `nnmail-split-methods'.
11697 If N is a positive number, respool the N next articles.
11698 If N is a negative number, respool the N previous articles.
11699 If N is nil and any articles have been marked with the process mark,
11700 respool those articles instead.
11701
11702 Respooling can be done both from mail groups and \"real\" newsgroups.
11703 In the former case, the articles in question will be moved from the
11704 current group into whatever groups they are destined to.  In the
11705 latter case, they will be copied into the relevant groups."
11706   (interactive 
11707    (list current-prefix-arg
11708          (let* ((methods (gnus-methods-using 'respool))
11709                 (methname
11710                  (symbol-name (or gnus-summary-respool-default-method
11711                                   (car (gnus-find-method-for-group
11712                                         gnus-newsgroup-name)))))
11713                 (method
11714                  (gnus-completing-read 
11715                   methname "What backend do you want to use when respooling?"
11716                   methods nil t nil 'gnus-method-history))
11717                 ms)
11718            (cond
11719             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11720              (list (intern method) ""))
11721             ((= 1 (length ms))
11722              (car ms))
11723             (t
11724              (cdr (completing-read 
11725                    "Server name: "
11726                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11727   (gnus-set-global-variables)
11728   (unless method
11729     (error "No method given for respooling"))
11730   (if (assoc (symbol-name
11731               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11732              (gnus-methods-using 'respool))
11733       (gnus-summary-move-article n nil method)
11734     (gnus-summary-copy-article n nil method)))
11735
11736 (defun gnus-summary-import-article (file)
11737   "Import a random file into a mail newsgroup."
11738   (interactive "fImport file: ")
11739   (gnus-set-global-variables)
11740   (let ((group gnus-newsgroup-name)
11741         (now (current-time))
11742         atts lines)
11743     (or (gnus-check-backend-function 'request-accept-article group)
11744         (error "%s does not support article importing" group))
11745     (or (file-readable-p file)
11746         (not (file-regular-p file))
11747         (error "Can't read %s" file))
11748     (save-excursion
11749       (set-buffer (get-buffer-create " *import file*"))
11750       (buffer-disable-undo (current-buffer))
11751       (erase-buffer)
11752       (insert-file-contents file)
11753       (goto-char (point-min))
11754       (unless (nnheader-article-p)
11755         ;; This doesn't look like an article, so we fudge some headers.
11756         (setq atts (file-attributes file)
11757               lines (count-lines (point-min) (point-max)))
11758         (insert "From: " (read-string "From: ") "\n"
11759                 "Subject: " (read-string "Subject: ") "\n"
11760                 "Date: " (timezone-make-date-arpa-standard
11761                           (current-time-string (nth 5 atts))
11762                           (current-time-zone now)
11763                           (current-time-zone now)) "\n"
11764                 "Message-ID: " (message-make-message-id) "\n"
11765                 "Lines: " (int-to-string lines) "\n"
11766                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11767       (gnus-request-accept-article group nil t)
11768       (kill-buffer (current-buffer)))))
11769
11770 (defun gnus-summary-expire-articles (&optional now)
11771   "Expire all articles that are marked as expirable in the current group."
11772   (interactive)
11773   (gnus-set-global-variables)
11774   (when (gnus-check-backend-function
11775          'request-expire-articles gnus-newsgroup-name)
11776     ;; This backend supports expiry.
11777     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11778            (expirable (if total
11779                           (gnus-list-of-read-articles gnus-newsgroup-name)
11780                         (setq gnus-newsgroup-expirable
11781                               (sort gnus-newsgroup-expirable '<))))
11782            (expiry-wait (if now 'immediate
11783                           (gnus-group-get-parameter
11784                            gnus-newsgroup-name 'expiry-wait)))
11785            es)
11786       (when expirable
11787         ;; There are expirable articles in this group, so we run them
11788         ;; through the expiry process.
11789         (gnus-message 6 "Expiring articles...")
11790         ;; The list of articles that weren't expired is returned.
11791         (if expiry-wait
11792             (let ((nnmail-expiry-wait-function nil)
11793                   (nnmail-expiry-wait expiry-wait))
11794               (setq es (gnus-request-expire-articles
11795                         expirable gnus-newsgroup-name)))
11796           (setq es (gnus-request-expire-articles
11797                     expirable gnus-newsgroup-name)))
11798         (or total (setq gnus-newsgroup-expirable es))
11799         ;; We go through the old list of expirable, and mark all
11800         ;; really expired articles as nonexistent.
11801         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11802           (let ((gnus-use-cache nil))
11803             (while expirable
11804               (unless (memq (car expirable) es)
11805                 (when (gnus-data-find (car expirable))
11806                   (gnus-summary-mark-article
11807                    (car expirable) gnus-canceled-mark)))
11808               (setq expirable (cdr expirable)))))
11809         (gnus-message 6 "Expiring articles...done")))))
11810
11811 (defun gnus-summary-expire-articles-now ()
11812   "Expunge all expirable articles in the current group.
11813 This means that *all* articles that are marked as expirable will be
11814 deleted forever, right now."
11815   (interactive)
11816   (gnus-set-global-variables)
11817   (or gnus-expert-user
11818       (gnus-y-or-n-p
11819        "Are you really, really, really sure you want to delete all these messages? ")
11820       (error "Phew!"))
11821   (gnus-summary-expire-articles t))
11822
11823 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11824 (defun gnus-summary-delete-article (&optional n)
11825   "Delete the N next (mail) articles.
11826 This command actually deletes articles.  This is not a marking
11827 command.  The article will disappear forever from your life, never to
11828 return.
11829 If N is negative, delete backwards.
11830 If N is nil and articles have been marked with the process mark,
11831 delete these instead."
11832   (interactive "P")
11833   (gnus-set-global-variables)
11834   (or (gnus-check-backend-function 'request-expire-articles
11835                                    gnus-newsgroup-name)
11836       (error "The current newsgroup does not support article deletion."))
11837   ;; Compute the list of articles to delete.
11838   (let ((articles (gnus-summary-work-articles n))
11839         not-deleted)
11840     (if (and gnus-novice-user
11841              (not (gnus-y-or-n-p
11842                    (format "Do you really want to delete %s forever? "
11843                            (if (> (length articles) 1) 
11844                                (format "these %s articles" (length articles))
11845                              "this article")))))
11846         ()
11847       ;; Delete the articles.
11848       (setq not-deleted (gnus-request-expire-articles
11849                          articles gnus-newsgroup-name 'force))
11850       (while articles
11851         (gnus-summary-remove-process-mark (car articles))
11852         ;; The backend might not have been able to delete the article
11853         ;; after all.
11854         (or (memq (car articles) not-deleted)
11855             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11856         (setq articles (cdr articles))))
11857     (gnus-summary-position-point)
11858     (gnus-set-mode-line 'summary)
11859     not-deleted))
11860
11861 (defun gnus-summary-edit-article (&optional force)
11862   "Enter into a buffer and edit the current article.
11863 This will have permanent effect only in mail groups.
11864 If FORCE is non-nil, allow editing of articles even in read-only
11865 groups."
11866   (interactive "P")
11867   (save-excursion
11868     (set-buffer gnus-summary-buffer)
11869     (gnus-set-global-variables)
11870     (when (and (not force)
11871                (gnus-group-read-only-p))
11872       (error "The current newsgroup does not support article editing."))
11873     (gnus-summary-select-article t nil t)
11874     (gnus-configure-windows 'article)
11875     (select-window (get-buffer-window gnus-article-buffer))
11876     (gnus-message 6 "C-c C-c to end edits")
11877     (setq buffer-read-only nil)
11878     (text-mode)
11879     (use-local-map (copy-keymap (current-local-map)))
11880     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11881     (buffer-enable-undo)
11882     (widen)
11883     (goto-char (point-min))
11884     (search-forward "\n\n" nil t)))
11885
11886 (defun gnus-summary-edit-article-done ()
11887   "Make edits to the current article permanent."
11888   (interactive)
11889   (if (gnus-group-read-only-p)
11890       (progn
11891         (let ((beep (not (eq major-mode 'text-mode))))
11892           (gnus-summary-edit-article-postpone)
11893           (when beep
11894             (gnus-error
11895              3 "The current newsgroup does not support article editing."))))
11896     (let ((buf (format "%s" (buffer-string))))
11897       (erase-buffer)
11898       (insert buf)
11899       (if (not (gnus-request-replace-article
11900                 (cdr gnus-article-current) (car gnus-article-current)
11901                 (current-buffer)))
11902           (error "Couldn't replace article.")
11903         (gnus-article-mode)
11904         (use-local-map gnus-article-mode-map)
11905         (setq buffer-read-only t)
11906         (buffer-disable-undo (current-buffer))
11907         (gnus-configure-windows 'summary)
11908         (gnus-summary-update-article (cdr gnus-article-current))
11909         (when gnus-use-cache
11910           (gnus-cache-update-article    
11911            (car gnus-article-current) (cdr gnus-article-current)))
11912         (when gnus-keep-backlog
11913           (gnus-backlog-remove-article 
11914            (car gnus-article-current) (cdr gnus-article-current))))
11915       (save-excursion
11916         (when (get-buffer gnus-original-article-buffer)
11917           (set-buffer gnus-original-article-buffer)
11918           (setq gnus-original-article nil)))
11919       (setq gnus-article-current nil
11920             gnus-current-article nil)
11921       (run-hooks 'gnus-article-display-hook)
11922       (and (gnus-visual-p 'summary-highlight 'highlight)
11923            (run-hooks 'gnus-visual-mark-article-hook)))))
11924
11925 (defun gnus-summary-edit-article-postpone ()
11926   "Postpone changes to the current article."
11927   (interactive)
11928   (gnus-article-mode)
11929   (use-local-map gnus-article-mode-map)
11930   (setq buffer-read-only t)
11931   (buffer-disable-undo (current-buffer))
11932   (gnus-configure-windows 'summary)
11933   (and (gnus-visual-p 'summary-highlight 'highlight)
11934        (run-hooks 'gnus-visual-mark-article-hook)))
11935
11936 (defun gnus-summary-respool-query ()
11937   "Query where the respool algorithm would put this article."
11938   (interactive)
11939   (gnus-set-global-variables)
11940   (gnus-summary-select-article)
11941   (save-excursion
11942     (set-buffer gnus-article-buffer)
11943     (save-restriction
11944       (goto-char (point-min))
11945       (search-forward "\n\n")
11946       (narrow-to-region (point-min) (point))
11947       (pp-eval-expression
11948        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11949
11950 ;; Summary marking commands.
11951
11952 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11953   "Mark articles which has the same subject as read, and then select the next.
11954 If UNMARK is positive, remove any kind of mark.
11955 If UNMARK is negative, tick articles."
11956   (interactive "P")
11957   (gnus-set-global-variables)
11958   (if unmark
11959       (setq unmark (prefix-numeric-value unmark)))
11960   (let ((count
11961          (gnus-summary-mark-same-subject
11962           (gnus-summary-article-subject) unmark)))
11963     ;; Select next unread article.  If auto-select-same mode, should
11964     ;; select the first unread article.
11965     (gnus-summary-next-article t (and gnus-auto-select-same
11966                                       (gnus-summary-article-subject)))
11967     (gnus-message 7 "%d article%s marked as %s"
11968                   count (if (= count 1) " is" "s are")
11969                   (if unmark "unread" "read"))))
11970
11971 (defun gnus-summary-kill-same-subject (&optional unmark)
11972   "Mark articles which has the same subject as read.
11973 If UNMARK is positive, remove any kind of mark.
11974 If UNMARK is negative, tick articles."
11975   (interactive "P")
11976   (gnus-set-global-variables)
11977   (if unmark
11978       (setq unmark (prefix-numeric-value unmark)))
11979   (let ((count
11980          (gnus-summary-mark-same-subject
11981           (gnus-summary-article-subject) unmark)))
11982     ;; If marked as read, go to next unread subject.
11983     (if (null unmark)
11984         ;; Go to next unread subject.
11985         (gnus-summary-next-subject 1 t))
11986     (gnus-message 7 "%d articles are marked as %s"
11987                   count (if unmark "unread" "read"))))
11988
11989 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11990   "Mark articles with same SUBJECT as read, and return marked number.
11991 If optional argument UNMARK is positive, remove any kinds of marks.
11992 If optional argument UNMARK is negative, mark articles as unread instead."
11993   (let ((count 1))
11994     (save-excursion
11995       (cond
11996        ((null unmark)                   ; Mark as read.
11997         (while (and
11998                 (progn
11999                   (gnus-summary-mark-article-as-read gnus-killed-mark)
12000                   (gnus-summary-show-thread) t)
12001                 (gnus-summary-find-subject subject))
12002           (setq count (1+ count))))
12003        ((> unmark 0)                    ; Tick.
12004         (while (and
12005                 (progn
12006                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
12007                   (gnus-summary-show-thread) t)
12008                 (gnus-summary-find-subject subject))
12009           (setq count (1+ count))))
12010        (t                               ; Mark as unread.
12011         (while (and
12012                 (progn
12013                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
12014                   (gnus-summary-show-thread) t)
12015                 (gnus-summary-find-subject subject))
12016           (setq count (1+ count)))))
12017       (gnus-set-mode-line 'summary)
12018       ;; Return the number of marked articles.
12019       count)))
12020
12021 (defun gnus-summary-mark-as-processable (n &optional unmark)
12022   "Set the process mark on the next N articles.
12023 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
12024 the process mark instead.  The difference between N and the actual
12025 number of articles marked is returned."
12026   (interactive "p")
12027   (gnus-set-global-variables)
12028   (let ((backward (< n 0))
12029         (n (abs n)))
12030     (while (and
12031             (> n 0)
12032             (if unmark
12033                 (gnus-summary-remove-process-mark
12034                  (gnus-summary-article-number))
12035               (gnus-summary-set-process-mark (gnus-summary-article-number)))
12036             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
12037       (setq n (1- n)))
12038     (if (/= 0 n) (gnus-message 7 "No more articles"))
12039     (gnus-summary-recenter)
12040     (gnus-summary-position-point)
12041     n))
12042
12043 (defun gnus-summary-unmark-as-processable (n)
12044   "Remove the process mark from the next N articles.
12045 If N is negative, mark backward instead.  The difference between N and
12046 the actual number of articles marked is returned."
12047   (interactive "p")
12048   (gnus-set-global-variables)
12049   (gnus-summary-mark-as-processable n t))
12050
12051 (defun gnus-summary-unmark-all-processable ()
12052   "Remove the process mark from all articles."
12053   (interactive)
12054   (gnus-set-global-variables)
12055   (save-excursion
12056     (while gnus-newsgroup-processable
12057       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
12058   (gnus-summary-position-point))
12059
12060 (defun gnus-summary-mark-as-expirable (n)
12061   "Mark N articles forward as expirable.
12062 If N is negative, mark backward instead.  The difference between N and
12063 the actual number of articles marked is returned."
12064   (interactive "p")
12065   (gnus-set-global-variables)
12066   (gnus-summary-mark-forward n gnus-expirable-mark))
12067
12068 (defun gnus-summary-mark-article-as-replied (article)
12069   "Mark ARTICLE replied and update the summary line."
12070   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
12071   (let ((buffer-read-only nil))
12072     (when (gnus-summary-goto-subject article)
12073       (gnus-summary-update-secondary-mark article))))
12074
12075 (defun gnus-summary-set-bookmark (article)
12076   "Set a bookmark in current article."
12077   (interactive (list (gnus-summary-article-number)))
12078   (gnus-set-global-variables)
12079   (if (or (not (get-buffer gnus-article-buffer))
12080           (not gnus-current-article)
12081           (not gnus-article-current)
12082           (not (equal gnus-newsgroup-name (car gnus-article-current))))
12083       (error "No current article selected"))
12084   ;; Remove old bookmark, if one exists.
12085   (let ((old (assq article gnus-newsgroup-bookmarks)))
12086     (if old (setq gnus-newsgroup-bookmarks
12087                   (delq old gnus-newsgroup-bookmarks))))
12088   ;; Set the new bookmark, which is on the form
12089   ;; (article-number . line-number-in-body).
12090   (setq gnus-newsgroup-bookmarks
12091         (cons
12092          (cons article
12093                (save-excursion
12094                  (set-buffer gnus-article-buffer)
12095                  (count-lines
12096                   (min (point)
12097                        (save-excursion
12098                          (goto-char (point-min))
12099                          (search-forward "\n\n" nil t)
12100                          (point)))
12101                   (point))))
12102          gnus-newsgroup-bookmarks))
12103   (gnus-message 6 "A bookmark has been added to the current article."))
12104
12105 (defun gnus-summary-remove-bookmark (article)
12106   "Remove the bookmark from the current article."
12107   (interactive (list (gnus-summary-article-number)))
12108   (gnus-set-global-variables)
12109   ;; Remove old bookmark, if one exists.
12110   (let ((old (assq article gnus-newsgroup-bookmarks)))
12111     (if old
12112         (progn
12113           (setq gnus-newsgroup-bookmarks
12114                 (delq old gnus-newsgroup-bookmarks))
12115           (gnus-message 6 "Removed bookmark."))
12116       (gnus-message 6 "No bookmark in current article."))))
12117
12118 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12119 (defun gnus-summary-mark-as-dormant (n)
12120   "Mark N articles forward as dormant.
12121 If N is negative, mark backward instead.  The difference between N and
12122 the actual number of articles marked is returned."
12123   (interactive "p")
12124   (gnus-set-global-variables)
12125   (gnus-summary-mark-forward n gnus-dormant-mark))
12126
12127 (defun gnus-summary-set-process-mark (article)
12128   "Set the process mark on ARTICLE and update the summary line."
12129   (setq gnus-newsgroup-processable
12130         (cons article
12131               (delq article gnus-newsgroup-processable)))
12132   (when (gnus-summary-goto-subject article)
12133     (gnus-summary-show-thread)
12134     (gnus-summary-update-secondary-mark article)))
12135
12136 (defun gnus-summary-remove-process-mark (article)
12137   "Remove the process mark from ARTICLE and update the summary line."
12138   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
12139   (when (gnus-summary-goto-subject article)
12140     (gnus-summary-show-thread)
12141     (gnus-summary-update-secondary-mark article)))
12142
12143 (defun gnus-summary-set-saved-mark (article)
12144   "Set the process mark on ARTICLE and update the summary line."
12145   (push article gnus-newsgroup-saved)
12146   (when (gnus-summary-goto-subject article)
12147     (gnus-summary-update-secondary-mark article)))
12148
12149 (defun gnus-summary-mark-forward (n &optional mark no-expire)
12150   "Mark N articles as read forwards.
12151 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
12152 The difference between N and the actual number of articles marked is
12153 returned."
12154   (interactive "p")
12155   (gnus-set-global-variables)
12156   (let ((backward (< n 0))
12157         (gnus-summary-goto-unread
12158          (and gnus-summary-goto-unread
12159               (not (eq gnus-summary-goto-unread 'never))
12160               (not (memq mark (list gnus-unread-mark
12161                                     gnus-ticked-mark gnus-dormant-mark)))))
12162         (n (abs n))
12163         (mark (or mark gnus-del-mark)))
12164     (while (and (> n 0)
12165                 (gnus-summary-mark-article nil mark no-expire)
12166                 (zerop (gnus-summary-next-subject
12167                         (if backward -1 1)
12168                         (and gnus-summary-goto-unread
12169                              (not (eq gnus-summary-goto-unread 'never)))
12170                         t)))
12171       (setq n (1- n)))
12172     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12173     (gnus-summary-recenter)
12174     (gnus-summary-position-point)
12175     (gnus-set-mode-line 'summary)
12176     n))
12177
12178 (defun gnus-summary-mark-article-as-read (mark)
12179   "Mark the current article quickly as read with MARK."
12180   (let ((article (gnus-summary-article-number)))
12181     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12182     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12183     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12184     (setq gnus-newsgroup-reads
12185           (cons (cons article mark) gnus-newsgroup-reads))
12186     ;; Possibly remove from cache, if that is used.
12187     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12188     ;; Allow the backend to change the mark.
12189     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12190     ;; Check for auto-expiry.
12191     (when (and gnus-newsgroup-auto-expire
12192                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12193                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12194                    (= mark gnus-ancient-mark)
12195                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12196       (setq mark gnus-expirable-mark)
12197       (push article gnus-newsgroup-expirable))
12198     ;; Set the mark in the buffer.
12199     (gnus-summary-update-mark mark 'unread)
12200     t))
12201
12202 (defun gnus-summary-mark-article-as-unread (mark)
12203   "Mark the current article quickly as unread with MARK."
12204   (let ((article (gnus-summary-article-number)))
12205     (if (< article 0)
12206         (gnus-error 1 "Unmarkable article")
12207       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12208       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12209       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12210       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12211       (cond ((= mark gnus-ticked-mark)
12212              (push article gnus-newsgroup-marked))
12213             ((= mark gnus-dormant-mark)
12214              (push article gnus-newsgroup-dormant))
12215             (t
12216              (push article gnus-newsgroup-unreads)))
12217       (setq gnus-newsgroup-reads
12218             (delq (assq article gnus-newsgroup-reads)
12219                   gnus-newsgroup-reads))
12220
12221       ;; See whether the article is to be put in the cache.
12222       (and gnus-use-cache
12223            (vectorp (gnus-summary-article-header article))
12224            (save-excursion
12225              (gnus-cache-possibly-enter-article
12226               gnus-newsgroup-name article
12227               (gnus-summary-article-header article)
12228               (= mark gnus-ticked-mark)
12229               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12230
12231       ;; Fix the mark.
12232       (gnus-summary-update-mark mark 'unread))
12233     t))
12234
12235 (defun gnus-summary-mark-article (&optional article mark no-expire)
12236   "Mark ARTICLE with MARK.  MARK can be any character.
12237 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12238 `??' (dormant) and `?E' (expirable).
12239 If MARK is nil, then the default character `?D' is used.
12240 If ARTICLE is nil, then the article on the current line will be
12241 marked."
12242   ;; The mark might be a string.
12243   (and (stringp mark)
12244        (setq mark (aref mark 0)))
12245   ;; If no mark is given, then we check auto-expiring.
12246   (and (not no-expire)
12247        gnus-newsgroup-auto-expire
12248        (or (not mark)
12249            (and (numberp mark)
12250                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12251                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12252                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12253        (setq mark gnus-expirable-mark))
12254   (let* ((mark (or mark gnus-del-mark))
12255          (article (or article (gnus-summary-article-number))))
12256     (or article (error "No article on current line"))
12257     (if (or (= mark gnus-unread-mark)
12258             (= mark gnus-ticked-mark)
12259             (= mark gnus-dormant-mark))
12260         (gnus-mark-article-as-unread article mark)
12261       (gnus-mark-article-as-read article mark))
12262
12263     ;; See whether the article is to be put in the cache.
12264     (and gnus-use-cache
12265          (not (= mark gnus-canceled-mark))
12266          (vectorp (gnus-summary-article-header article))
12267          (save-excursion
12268            (gnus-cache-possibly-enter-article
12269             gnus-newsgroup-name article
12270             (gnus-summary-article-header article)
12271             (= mark gnus-ticked-mark)
12272             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12273
12274     (if (gnus-summary-goto-subject article nil t)
12275         (let ((buffer-read-only nil))
12276           (gnus-summary-show-thread)
12277           ;; Fix the mark.
12278           (gnus-summary-update-mark mark 'unread)
12279           t))))
12280
12281 (defun gnus-summary-update-secondary-mark (article)
12282   "Update the secondary (read, process, cache) mark."
12283   (gnus-summary-update-mark
12284    (cond ((memq article gnus-newsgroup-processable)
12285           gnus-process-mark)
12286          ((memq article gnus-newsgroup-cached)
12287           gnus-cached-mark)
12288          ((memq article gnus-newsgroup-replied)
12289           gnus-replied-mark)
12290          ((memq article gnus-newsgroup-saved)
12291           gnus-saved-mark)
12292          (t gnus-unread-mark))
12293    'replied)
12294   (when (gnus-visual-p 'summary-highlight 'highlight)
12295     (run-hooks 'gnus-summary-update-hook))
12296   t)
12297
12298 (defun gnus-summary-update-mark (mark type)
12299   (beginning-of-line)
12300   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12301         (buffer-read-only nil))
12302     (when (and forward
12303                (<= (+ forward (point)) (point-max)))
12304       ;; Go to the right position on the line.
12305       (goto-char (+ forward (point)))
12306       ;; Replace the old mark with the new mark.
12307       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12308       ;; Optionally update the marks by some user rule.
12309       (when (eq type 'unread)
12310         (gnus-data-set-mark
12311          (gnus-data-find (gnus-summary-article-number)) mark)
12312         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12313
12314 (defun gnus-mark-article-as-read (article &optional mark)
12315   "Enter ARTICLE in the pertinent lists and remove it from others."
12316   ;; Make the article expirable.
12317   (let ((mark (or mark gnus-del-mark)))
12318     (if (= mark gnus-expirable-mark)
12319         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12320       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12321     ;; Remove from unread and marked lists.
12322     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12323     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12324     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12325     (push (cons article mark) gnus-newsgroup-reads)
12326     ;; Possibly remove from cache, if that is used.
12327     (when gnus-use-cache
12328       (gnus-cache-enter-remove-article article))))
12329
12330 (defun gnus-mark-article-as-unread (article &optional mark)
12331   "Enter ARTICLE in the pertinent lists and remove it from others."
12332   (let ((mark (or mark gnus-ticked-mark)))
12333     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12334     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12335     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12336     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12337     (cond ((= mark gnus-ticked-mark)
12338            (push article gnus-newsgroup-marked))
12339           ((= mark gnus-dormant-mark)
12340            (push article gnus-newsgroup-dormant))
12341           (t
12342            (push article gnus-newsgroup-unreads)))
12343     (setq gnus-newsgroup-reads
12344           (delq (assq article gnus-newsgroup-reads)
12345                 gnus-newsgroup-reads))))
12346
12347 (defalias 'gnus-summary-mark-as-unread-forward
12348   'gnus-summary-tick-article-forward)
12349 (make-obsolete 'gnus-summary-mark-as-unread-forward
12350                'gnus-summary-tick-article-forward)
12351 (defun gnus-summary-tick-article-forward (n)
12352   "Tick N articles forwards.
12353 If N is negative, tick backwards instead.
12354 The difference between N and the number of articles ticked is returned."
12355   (interactive "p")
12356   (gnus-summary-mark-forward n gnus-ticked-mark))
12357
12358 (defalias 'gnus-summary-mark-as-unread-backward
12359   'gnus-summary-tick-article-backward)
12360 (make-obsolete 'gnus-summary-mark-as-unread-backward
12361                'gnus-summary-tick-article-backward)
12362 (defun gnus-summary-tick-article-backward (n)
12363   "Tick N articles backwards.
12364 The difference between N and the number of articles ticked is returned."
12365   (interactive "p")
12366   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12367
12368 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12369 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12370 (defun gnus-summary-tick-article (&optional article clear-mark)
12371   "Mark current article as unread.
12372 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12373 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12374   (interactive)
12375   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12376                                        gnus-ticked-mark)))
12377
12378 (defun gnus-summary-mark-as-read-forward (n)
12379   "Mark N articles as read forwards.
12380 If N is negative, mark backwards instead.
12381 The difference between N and the actual number of articles marked is
12382 returned."
12383   (interactive "p")
12384   (gnus-summary-mark-forward n gnus-del-mark t))
12385
12386 (defun gnus-summary-mark-as-read-backward (n)
12387   "Mark the N articles as read backwards.
12388 The difference between N and the actual number of articles marked is
12389 returned."
12390   (interactive "p")
12391   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12392
12393 (defun gnus-summary-mark-as-read (&optional article mark)
12394   "Mark current article as read.
12395 ARTICLE specifies the article to be marked as read.
12396 MARK specifies a string to be inserted at the beginning of the line."
12397   (gnus-summary-mark-article article mark))
12398
12399 (defun gnus-summary-clear-mark-forward (n)
12400   "Clear marks from N articles forward.
12401 If N is negative, clear backward instead.
12402 The difference between N and the number of marks cleared is returned."
12403   (interactive "p")
12404   (gnus-summary-mark-forward n gnus-unread-mark))
12405
12406 (defun gnus-summary-clear-mark-backward (n)
12407   "Clear marks from N articles backward.
12408 The difference between N and the number of marks cleared is returned."
12409   (interactive "p")
12410   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12411
12412 (defun gnus-summary-mark-unread-as-read ()
12413   "Intended to be used by `gnus-summary-mark-article-hook'."
12414   (when (memq gnus-current-article gnus-newsgroup-unreads)
12415     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12416
12417 (defun gnus-summary-mark-read-and-unread-as-read ()
12418   "Intended to be used by `gnus-summary-mark-article-hook'."
12419   (let ((mark (gnus-summary-article-mark)))
12420     (when (or (gnus-unread-mark-p mark)
12421               (gnus-read-mark-p mark))
12422       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12423
12424 (defun gnus-summary-mark-region-as-read (point mark all)
12425   "Mark all unread articles between point and mark as read.
12426 If given a prefix, mark all articles between point and mark as read,
12427 even ticked and dormant ones."
12428   (interactive "r\nP")
12429   (save-excursion
12430     (let (article)
12431       (goto-char point)
12432       (beginning-of-line)
12433       (while (and
12434               (< (point) mark)
12435               (progn
12436                 (when (or all
12437                           (memq (setq article (gnus-summary-article-number))
12438                                 gnus-newsgroup-unreads))
12439                   (gnus-summary-mark-article article gnus-del-mark))
12440                 t)
12441               (gnus-summary-find-next))))))
12442
12443 (defun gnus-summary-mark-below (score mark)
12444   "Mark articles with score less than SCORE with MARK."
12445   (interactive "P\ncMark: ")
12446   (gnus-set-global-variables)
12447   (setq score (if score
12448                   (prefix-numeric-value score)
12449                 (or gnus-summary-default-score 0)))
12450   (save-excursion
12451     (set-buffer gnus-summary-buffer)
12452     (goto-char (point-min))
12453     (while 
12454         (progn
12455           (and (< (gnus-summary-article-score) score)
12456                (gnus-summary-mark-article nil mark))
12457           (gnus-summary-find-next)))))
12458
12459 (defun gnus-summary-kill-below (&optional score)
12460   "Mark articles with score below SCORE as read."
12461   (interactive "P")
12462   (gnus-set-global-variables)
12463   (gnus-summary-mark-below score gnus-killed-mark))
12464
12465 (defun gnus-summary-clear-above (&optional score)
12466   "Clear all marks from articles with score above SCORE."
12467   (interactive "P")
12468   (gnus-set-global-variables)
12469   (gnus-summary-mark-above score gnus-unread-mark))
12470
12471 (defun gnus-summary-tick-above (&optional score)
12472   "Tick all articles with score above SCORE."
12473   (interactive "P")
12474   (gnus-set-global-variables)
12475   (gnus-summary-mark-above score gnus-ticked-mark))
12476
12477 (defun gnus-summary-mark-above (score mark)
12478   "Mark articles with score over SCORE with MARK."
12479   (interactive "P\ncMark: ")
12480   (gnus-set-global-variables)
12481   (setq score (if score
12482                   (prefix-numeric-value score)
12483                 (or gnus-summary-default-score 0)))
12484   (save-excursion
12485     (set-buffer gnus-summary-buffer)
12486     (goto-char (point-min))
12487     (while (and (progn
12488                   (if (> (gnus-summary-article-score) score)
12489                       (gnus-summary-mark-article nil mark))
12490                   t)
12491                 (gnus-summary-find-next)))))
12492
12493 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12494 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12495 (defun gnus-summary-limit-include-expunged ()
12496   "Display all the hidden articles that were expunged for low scores."
12497   (interactive)
12498   (gnus-set-global-variables)
12499   (let ((buffer-read-only nil))
12500     (let ((scored gnus-newsgroup-scored)
12501           headers h)
12502       (while scored
12503         (or (gnus-summary-goto-subject (caar scored))
12504             (and (setq h (gnus-summary-article-header (caar scored)))
12505                  (< (cdar scored) gnus-summary-expunge-below)
12506                  (setq headers (cons h headers))))
12507         (setq scored (cdr scored)))
12508       (or headers (error "No expunged articles hidden."))
12509       (goto-char (point-min))
12510       (gnus-summary-prepare-unthreaded (nreverse headers)))
12511     (goto-char (point-min))
12512     (gnus-summary-position-point)))
12513
12514 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12515   "Mark all articles not marked as unread in this newsgroup as read.
12516 If prefix argument ALL is non-nil, all articles are marked as read.
12517 If QUIETLY is non-nil, no questions will be asked.
12518 If TO-HERE is non-nil, it should be a point in the buffer.  All
12519 articles before this point will be marked as read.
12520 The number of articles marked as read is returned."
12521   (interactive "P")
12522   (gnus-set-global-variables)
12523   (prog1
12524       (if (or quietly
12525               (not gnus-interactive-catchup) ;Without confirmation?
12526               gnus-expert-user
12527               (gnus-y-or-n-p
12528                (if all
12529                    "Mark absolutely all articles as read? "
12530                  "Mark all unread articles as read? ")))
12531           (if (and not-mark
12532                    (not gnus-newsgroup-adaptive)
12533                    (not gnus-newsgroup-auto-expire))
12534               (progn
12535                 (when all
12536                   (setq gnus-newsgroup-marked nil
12537                         gnus-newsgroup-dormant nil))
12538                 (setq gnus-newsgroup-unreads nil))
12539             ;; We actually mark all articles as canceled, which we
12540             ;; have to do when using auto-expiry or adaptive scoring.
12541             (gnus-summary-show-all-threads)
12542             (if (gnus-summary-first-subject (not all))
12543                 (while (and
12544                         (if to-here (< (point) to-here) t)
12545                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12546                         (gnus-summary-find-next (not all)))))
12547             (unless to-here
12548               (setq gnus-newsgroup-unreads nil))
12549             (gnus-set-mode-line 'summary)))
12550     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12551       (if (and (not to-here) (eq 'nnvirtual (car method)))
12552           (nnvirtual-catchup-group
12553            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12554     (gnus-summary-position-point)))
12555
12556 (defun gnus-summary-catchup-to-here (&optional all)
12557   "Mark all unticked articles before the current one as read.
12558 If ALL is non-nil, also mark ticked and dormant articles as read."
12559   (interactive "P")
12560   (gnus-set-global-variables)
12561   (save-excursion
12562     (gnus-save-hidden-threads
12563       (let ((beg (point)))
12564         ;; We check that there are unread articles.
12565         (when (or all (gnus-summary-find-prev))
12566           (gnus-summary-catchup all t beg)))))
12567   (gnus-summary-position-point))
12568
12569 (defun gnus-summary-catchup-all (&optional quietly)
12570   "Mark all articles in this newsgroup as read."
12571   (interactive "P")
12572   (gnus-set-global-variables)
12573   (gnus-summary-catchup t quietly))
12574
12575 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12576   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12577 If prefix argument ALL is non-nil, all articles are marked as read."
12578   (interactive "P")
12579   (gnus-set-global-variables)
12580   (gnus-summary-catchup all quietly nil 'fast)
12581   ;; Select next newsgroup or exit.
12582   (if (eq gnus-auto-select-next 'quietly)
12583       (gnus-summary-next-group nil)
12584     (gnus-summary-exit)))
12585
12586 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12587   "Mark all articles in this newsgroup as read, and then exit."
12588   (interactive "P")
12589   (gnus-set-global-variables)
12590   (gnus-summary-catchup-and-exit t quietly))
12591
12592 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12593 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12594   "Mark all articles in this group as read and select the next group.
12595 If given a prefix, mark all articles, unread as well as ticked, as
12596 read."
12597   (interactive "P")
12598   (gnus-set-global-variables)
12599   (save-excursion
12600     (gnus-summary-catchup all))
12601   (gnus-summary-next-article t nil nil t))
12602
12603 ;; Thread-based commands.
12604
12605 (defun gnus-summary-articles-in-thread (&optional article)
12606   "Return a list of all articles in the current thread.
12607 If ARTICLE is non-nil, return all articles in the thread that starts
12608 with that article."
12609   (let* ((article (or article (gnus-summary-article-number)))
12610          (data (gnus-data-find-list article))
12611          (top-level (gnus-data-level (car data)))
12612          (top-subject
12613           (cond ((null gnus-thread-operation-ignore-subject)
12614                  (gnus-simplify-subject-re
12615                   (mail-header-subject (gnus-data-header (car data)))))
12616                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12617                  (gnus-simplify-subject-fuzzy
12618                   (mail-header-subject (gnus-data-header (car data)))))
12619                 (t nil)))
12620          (end-point (save-excursion
12621                       (if (gnus-summary-go-to-next-thread) 
12622                           (point) (point-max))))
12623          articles)
12624     (while (and data
12625                 (< (gnus-data-pos (car data)) end-point))
12626       (when (or (not top-subject)
12627                 (string= top-subject
12628                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12629                              (gnus-simplify-subject-fuzzy
12630                               (mail-header-subject
12631                                (gnus-data-header (car data))))
12632                            (gnus-simplify-subject-re
12633                             (mail-header-subject
12634                              (gnus-data-header (car data)))))))
12635         (push (gnus-data-number (car data)) articles))
12636       (unless (and (setq data (cdr data))
12637                    (> (gnus-data-level (car data)) top-level))
12638         (setq data nil)))
12639     ;; Return the list of articles.
12640     (nreverse articles)))
12641
12642 (defun gnus-summary-rethread-current ()
12643   "Rethread the thread the current article is part of."
12644   (interactive)
12645   (gnus-set-global-variables)
12646   (let* ((gnus-show-threads t)
12647          (article (gnus-summary-article-number))
12648          (id (mail-header-id (gnus-summary-article-header)))
12649          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12650     (unless id
12651       (error "No article on the current line"))
12652     (gnus-rebuild-thread id)
12653     (gnus-summary-goto-subject article)))
12654
12655 (defun gnus-summary-reparent-thread ()
12656   "Make current article child of the marked (or previous) article.
12657
12658 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12659 is non-nil or the Subject: of both articles are the same."
12660   (interactive)
12661   (or (not (gnus-group-read-only-p))
12662       (error "The current newsgroup does not support article editing."))
12663   (or (<= (length gnus-newsgroup-processable) 1)
12664       (error "No more than one article may be marked."))
12665   (save-window-excursion
12666     (let ((gnus-article-buffer " *reparent*")
12667           (current-article (gnus-summary-article-number))
12668           ; first grab the marked article, otherwise one line up.
12669           (parent-article (if (not (null gnus-newsgroup-processable))
12670                               (car gnus-newsgroup-processable)
12671                             (save-excursion
12672                               (if (eq (forward-line -1) 0)
12673                                   (gnus-summary-article-number)
12674                                 (error "Beginning of summary buffer."))))))
12675       (or (not (eq current-article parent-article))
12676           (error "An article may not be self-referential."))
12677       (let ((message-id (mail-header-id 
12678                          (gnus-summary-article-header parent-article))))
12679         (or (and message-id (not (equal message-id "")))
12680             (error "No message-id in desired parent."))
12681         (gnus-summary-select-article t t nil current-article)
12682         (set-buffer gnus-article-buffer)
12683         (setq buffer-read-only nil)
12684         (let ((buf (format "%s" (buffer-string))))
12685           (erase-buffer)
12686           (insert buf))
12687         (goto-char (point-min))
12688         (if (search-forward-regexp "^References: " nil t)
12689             (insert message-id " " )
12690           (insert "References: " message-id "\n"))
12691         (or (gnus-request-replace-article current-article
12692                                           (car gnus-article-current)
12693                                           gnus-article-buffer)
12694             (error "Couldn't replace article."))
12695         (set-buffer gnus-summary-buffer)
12696         (gnus-summary-unmark-all-processable)
12697         (gnus-summary-rethread-current)
12698         (gnus-message 3 "Article %d is now the child of article %d."
12699                       current-article parent-article)))))
12700
12701 (defun gnus-summary-toggle-threads (&optional arg)
12702   "Toggle showing conversation threads.
12703 If ARG is positive number, turn showing conversation threads on."
12704   (interactive "P")
12705   (gnus-set-global-variables)
12706   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12707     (setq gnus-show-threads
12708           (if (null arg) (not gnus-show-threads)
12709             (> (prefix-numeric-value arg) 0)))
12710     (gnus-summary-prepare)
12711     (gnus-summary-goto-subject current)
12712     (gnus-summary-position-point)))
12713
12714 (defun gnus-summary-show-all-threads ()
12715   "Show all threads."
12716   (interactive)
12717   (gnus-set-global-variables)
12718   (save-excursion
12719     (let ((buffer-read-only nil))
12720       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12721   (gnus-summary-position-point))
12722
12723 (defun gnus-summary-show-thread ()
12724   "Show thread subtrees.
12725 Returns nil if no thread was there to be shown."
12726   (interactive)
12727   (gnus-set-global-variables)
12728   (let ((buffer-read-only nil)
12729         (orig (point))
12730         ;; first goto end then to beg, to have point at beg after let
12731         (end (progn (end-of-line) (point)))
12732         (beg (progn (beginning-of-line) (point))))
12733     (prog1
12734         ;; Any hidden lines here?
12735         (search-forward "\r" end t)
12736       (subst-char-in-region beg end ?\^M ?\n t)
12737       (goto-char orig)
12738       (gnus-summary-position-point))))
12739
12740 (defun gnus-summary-hide-all-threads ()
12741   "Hide all thread subtrees."
12742   (interactive)
12743   (gnus-set-global-variables)
12744   (save-excursion
12745     (goto-char (point-min))
12746     (gnus-summary-hide-thread)
12747     (while (zerop (gnus-summary-next-thread 1 t))
12748       (gnus-summary-hide-thread)))
12749   (gnus-summary-position-point))
12750
12751 (defun gnus-summary-hide-thread ()
12752   "Hide thread subtrees.
12753 Returns nil if no threads were there to be hidden."
12754   (interactive)
12755   (gnus-set-global-variables)
12756   (let ((buffer-read-only nil)
12757         (start (point))
12758         (article (gnus-summary-article-number)))
12759     (goto-char start)
12760     ;; Go forward until either the buffer ends or the subthread
12761     ;; ends.
12762     (when (and (not (eobp))
12763                (or (zerop (gnus-summary-next-thread 1 t))
12764                    (goto-char (point-max))))
12765       (prog1
12766           (if (and (> (point) start)
12767                    (search-backward "\n" start t))
12768               (progn
12769                 (subst-char-in-region start (point) ?\n ?\^M)
12770                 (gnus-summary-goto-subject article))
12771             (goto-char start)
12772             nil)
12773         ;;(gnus-summary-position-point)
12774         ))))
12775
12776 (defun gnus-summary-go-to-next-thread (&optional previous)
12777   "Go to the same level (or less) next thread.
12778 If PREVIOUS is non-nil, go to previous thread instead.
12779 Return the article number moved to, or nil if moving was impossible."
12780   (let ((level (gnus-summary-thread-level))
12781         (way (if previous -1 1))
12782         (beg (point)))
12783     (forward-line way)
12784     (while (and (not (eobp))
12785                 (< level (gnus-summary-thread-level)))
12786       (forward-line way))
12787     (if (eobp)
12788         (progn
12789           (goto-char beg)
12790           nil)
12791       (setq beg (point))
12792       (prog1
12793           (gnus-summary-article-number)
12794         (goto-char beg)))))
12795
12796 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12797   "Go to the same level (or less) next thread.
12798 If PREVIOUS is non-nil, go to previous thread instead.
12799 Return the article number moved to, or nil if moving was impossible."
12800   (if (and (eq gnus-summary-make-false-root 'dummy)
12801            (gnus-summary-article-intangible-p))
12802       (let ((beg (point)))
12803         (while (and (zerop (forward-line 1))
12804                     (not (gnus-summary-article-intangible-p))
12805                     (not (zerop (save-excursion 
12806                                   (gnus-summary-thread-level))))))
12807         (if (eobp)
12808             (progn
12809               (goto-char beg)
12810               nil)
12811           (point)))
12812     (let* ((level (gnus-summary-thread-level))
12813            (article (gnus-summary-article-number))
12814            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12815            oart)
12816       (while data
12817         (if (<= (gnus-data-level (car data)) level)
12818             (setq oart (gnus-data-number (car data))
12819                   data nil)
12820           (setq data (cdr data))))
12821       (and oart
12822            (gnus-summary-goto-subject oart)))))
12823
12824 (defun gnus-summary-next-thread (n &optional silent)
12825   "Go to the same level next N'th thread.
12826 If N is negative, search backward instead.
12827 Returns the difference between N and the number of skips actually
12828 done.
12829
12830 If SILENT, don't output messages."
12831   (interactive "p")
12832   (gnus-set-global-variables)
12833   (let ((backward (< n 0))
12834         (n (abs n))
12835         old dum int)
12836     (while (and (> n 0)
12837                 (gnus-summary-go-to-next-thread backward))
12838       (decf n))
12839     (unless silent 
12840       (gnus-summary-position-point))
12841     (when (and (not silent) (/= 0 n))
12842       (gnus-message 7 "No more threads"))
12843     n))
12844
12845 (defun gnus-summary-prev-thread (n)
12846   "Go to the same level previous N'th thread.
12847 Returns the difference between N and the number of skips actually
12848 done."
12849   (interactive "p")
12850   (gnus-set-global-variables)
12851   (gnus-summary-next-thread (- n)))
12852
12853 (defun gnus-summary-go-down-thread ()
12854   "Go down one level in the current thread."
12855   (let ((children (gnus-summary-article-children)))
12856     (and children
12857          (gnus-summary-goto-subject (car children)))))
12858
12859 (defun gnus-summary-go-up-thread ()
12860   "Go up one level in the current thread."
12861   (let ((parent (gnus-summary-article-parent)))
12862     (and parent
12863          (gnus-summary-goto-subject parent))))
12864
12865 (defun gnus-summary-down-thread (n)
12866   "Go down thread N steps.
12867 If N is negative, go up instead.
12868 Returns the difference between N and how many steps down that were
12869 taken."
12870   (interactive "p")
12871   (gnus-set-global-variables)
12872   (let ((up (< n 0))
12873         (n (abs n)))
12874     (while (and (> n 0)
12875                 (if up (gnus-summary-go-up-thread)
12876                   (gnus-summary-go-down-thread)))
12877       (setq n (1- n)))
12878     (gnus-summary-position-point)
12879     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12880     n))
12881
12882 (defun gnus-summary-up-thread (n)
12883   "Go up thread N steps.
12884 If N is negative, go up instead.
12885 Returns the difference between N and how many steps down that were
12886 taken."
12887   (interactive "p")
12888   (gnus-set-global-variables)
12889   (gnus-summary-down-thread (- n)))
12890
12891 (defun gnus-summary-top-thread ()
12892   "Go to the top of the thread."
12893   (interactive)
12894   (gnus-set-global-variables)
12895   (while (gnus-summary-go-up-thread))
12896   (gnus-summary-article-number))
12897
12898 (defun gnus-summary-kill-thread (&optional unmark)
12899   "Mark articles under current thread as read.
12900 If the prefix argument is positive, remove any kinds of marks.
12901 If the prefix argument is negative, tick articles instead."
12902   (interactive "P")
12903   (gnus-set-global-variables)
12904   (when unmark
12905     (setq unmark (prefix-numeric-value unmark)))
12906   (let ((articles (gnus-summary-articles-in-thread)))
12907     (save-excursion
12908       ;; Expand the thread.
12909       (gnus-summary-show-thread)
12910       ;; Mark all the articles.
12911       (while articles
12912         (gnus-summary-goto-subject (car articles))
12913         (cond ((null unmark)
12914                (gnus-summary-mark-article-as-read gnus-killed-mark))
12915               ((> unmark 0)
12916                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12917               (t
12918                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12919         (setq articles (cdr articles))))
12920     ;; Hide killed subtrees.
12921     (and (null unmark)
12922          gnus-thread-hide-killed
12923          (gnus-summary-hide-thread))
12924     ;; If marked as read, go to next unread subject.
12925     (if (null unmark)
12926         ;; Go to next unread subject.
12927         (gnus-summary-next-subject 1 t)))
12928   (gnus-set-mode-line 'summary))
12929
12930 ;; Summary sorting commands
12931
12932 (defun gnus-summary-sort-by-number (&optional reverse)
12933   "Sort summary buffer by article number.
12934 Argument REVERSE means reverse order."
12935   (interactive "P")
12936   (gnus-summary-sort 'number reverse))
12937
12938 (defun gnus-summary-sort-by-author (&optional reverse)
12939   "Sort summary buffer by author name alphabetically.
12940 If case-fold-search is non-nil, case of letters is ignored.
12941 Argument REVERSE means reverse order."
12942   (interactive "P")
12943   (gnus-summary-sort 'author reverse))
12944
12945 (defun gnus-summary-sort-by-subject (&optional reverse)
12946   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12947 If case-fold-search is non-nil, case of letters is ignored.
12948 Argument REVERSE means reverse order."
12949   (interactive "P")
12950   (gnus-summary-sort 'subject reverse))
12951
12952 (defun gnus-summary-sort-by-date (&optional reverse)
12953   "Sort summary buffer by date.
12954 Argument REVERSE means reverse order."
12955   (interactive "P")
12956   (gnus-summary-sort 'date reverse))
12957
12958 (defun gnus-summary-sort-by-score (&optional reverse)
12959   "Sort summary buffer by score.
12960 Argument REVERSE means reverse order."
12961   (interactive "P")
12962   (gnus-summary-sort 'score reverse))
12963
12964 (defun gnus-summary-sort (predicate reverse)
12965   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12966   (gnus-set-global-variables)
12967   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12968          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12969          (gnus-thread-sort-functions
12970           (list
12971            (if (not reverse)
12972                thread
12973              `(lambda (t1 t2)
12974                 (,thread t2 t1)))))
12975          (gnus-article-sort-functions
12976           (list
12977            (if (not reverse)
12978                article
12979              `(lambda (t1 t2)
12980                 (,article t2 t1)))))
12981          (buffer-read-only)
12982          (gnus-summary-prepare-hook nil))
12983     ;; We do the sorting by regenerating the threads.
12984     (gnus-summary-prepare)
12985     ;; Hide subthreads if needed.
12986     (when (and gnus-show-threads gnus-thread-hide-subtree)
12987       (gnus-summary-hide-all-threads)))
12988   ;; If in async mode, we send some info to the backend.
12989   (when gnus-newsgroup-async
12990     (gnus-request-asynchronous
12991      gnus-newsgroup-name gnus-newsgroup-data)))
12992
12993 (defun gnus-sortable-date (date)
12994   "Make sortable string by string-lessp from DATE.
12995 Timezone package is used."
12996   (condition-case ()
12997       (progn
12998         (setq date (inline (timezone-fix-time 
12999                             date nil 
13000                             (aref (inline (timezone-parse-date date)) 4))))
13001         (inline
13002           (timezone-make-sortable-date
13003            (aref date 0) (aref date 1) (aref date 2)
13004            (inline
13005              (timezone-make-time-string
13006               (aref date 3) (aref date 4) (aref date 5))))))
13007     (error "")))
13008   
13009 ;; Summary saving commands.
13010
13011 (defun gnus-summary-save-article (&optional n not-saved)
13012   "Save the current article using the default saver function.
13013 If N is a positive number, save the N next articles.
13014 If N is a negative number, save the N previous articles.
13015 If N is nil and any articles have been marked with the process mark,
13016 save those articles instead.
13017 The variable `gnus-default-article-saver' specifies the saver function."
13018   (interactive "P")
13019   (gnus-set-global-variables)
13020   (let ((articles (gnus-summary-work-articles n))
13021         (save-buffer (save-excursion 
13022                        (nnheader-set-temp-buffer " *Gnus Save*")))
13023         file header article)
13024     (while articles
13025       (setq header (gnus-summary-article-header
13026                     (setq article (pop articles))))
13027       (if (not (vectorp header))
13028           ;; This is a pseudo-article.
13029           (if (assq 'name header)
13030               (gnus-copy-file (cdr (assq 'name header)))
13031             (gnus-message 1 "Article %d is unsaveable" article))
13032         ;; This is a real article.
13033         (save-window-excursion
13034           (gnus-summary-select-article t nil nil article))
13035         (save-excursion
13036           (set-buffer save-buffer)
13037           (erase-buffer)
13038           (insert-buffer-substring gnus-original-article-buffer))
13039         (unless gnus-save-all-headers
13040           ;; Remove headers accoring to `gnus-saved-headers'.
13041           (let ((gnus-visible-headers
13042                  (or gnus-saved-headers gnus-visible-headers))
13043                 (gnus-article-buffer save-buffer))
13044             (gnus-article-hide-headers 1 t)))
13045         (save-window-excursion
13046           (if (not gnus-default-article-saver)
13047               (error "No default saver is defined.")
13048             ;; !!! Magic!  The saving functions all save
13049             ;; `gnus-original-article-buffer' (or so they think),
13050             ;; but we bind that variable to our save-buffer.
13051             (set-buffer gnus-article-buffer)
13052             (let ((gnus-original-article-buffer save-buffer))
13053               (set-buffer gnus-summary-buffer)
13054               (setq file (funcall
13055                           gnus-default-article-saver
13056                           (cond
13057                            ((not gnus-prompt-before-saving)
13058                             'default)
13059                            ((eq gnus-prompt-before-saving 'always)
13060                             nil)
13061                            (t file)))))))
13062         (gnus-summary-remove-process-mark article)
13063         (unless not-saved
13064           (gnus-summary-set-saved-mark article))))
13065     (gnus-kill-buffer save-buffer)
13066     (gnus-summary-position-point)
13067     n))
13068
13069 (defun gnus-summary-pipe-output (&optional arg)
13070   "Pipe the current article to a subprocess.
13071 If N is a positive number, pipe the N next articles.
13072 If N is a negative number, pipe the N previous articles.
13073 If N is nil and any articles have been marked with the process mark,
13074 pipe those articles instead."
13075   (interactive "P")
13076   (gnus-set-global-variables)
13077   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
13078     (gnus-summary-save-article arg t))
13079   (gnus-configure-windows 'pipe))
13080
13081 (defun gnus-summary-save-article-mail (&optional arg)
13082   "Append the current article to an mail file.
13083 If N is a positive number, save the N next articles.
13084 If N is a negative number, save the N previous articles.
13085 If N is nil and any articles have been marked with the process mark,
13086 save those articles instead."
13087   (interactive "P")
13088   (gnus-set-global-variables)
13089   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
13090     (gnus-summary-save-article arg)))
13091
13092 (defun gnus-summary-save-article-rmail (&optional arg)
13093   "Append the current article to an rmail file.
13094 If N is a positive number, save the N next articles.
13095 If N is a negative number, save the N previous articles.
13096 If N is nil and any articles have been marked with the process mark,
13097 save those articles instead."
13098   (interactive "P")
13099   (gnus-set-global-variables)
13100   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
13101     (gnus-summary-save-article arg)))
13102
13103 (defun gnus-summary-save-article-file (&optional arg)
13104   "Append the current article to a file.
13105 If N is a positive number, save the N next articles.
13106 If N is a negative number, save the N previous articles.
13107 If N is nil and any articles have been marked with the process mark,
13108 save those articles instead."
13109   (interactive "P")
13110   (gnus-set-global-variables)
13111   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
13112     (gnus-summary-save-article arg)))
13113
13114 (defun gnus-summary-save-article-body-file (&optional arg)
13115   "Append the current article body to a file.
13116 If N is a positive number, save the N next articles.
13117 If N is a negative number, save the N previous articles.
13118 If N is nil and any articles have been marked with the process mark,
13119 save those articles instead."
13120   (interactive "P")
13121   (gnus-set-global-variables)
13122   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
13123     (gnus-summary-save-article arg)))
13124
13125 (defun gnus-get-split-value (methods)
13126   "Return a value based on the split METHODS."
13127   (let (split-name method result match)
13128     (when methods
13129       (save-excursion
13130         (set-buffer gnus-original-article-buffer)
13131         (save-restriction
13132           (nnheader-narrow-to-headers)
13133           (while methods
13134             (goto-char (point-min))
13135             (setq method (pop methods))
13136             (setq match (car method))
13137             (when (cond
13138                    ((stringp match)
13139                     ;; Regular expression.
13140                     (condition-case ()
13141                         (re-search-forward match nil t)
13142                       (error nil)))
13143                    ((gnus-functionp match)
13144                     ;; Function.
13145                     (save-restriction
13146                       (widen)
13147                       (setq result (funcall match gnus-newsgroup-name))))
13148                    ((consp match)
13149                     ;; Form.
13150                     (save-restriction
13151                       (widen)
13152                       (setq result (eval match)))))
13153               (setq split-name (append (cdr method) split-name))
13154               (cond ((stringp result)
13155                      (push result split-name))
13156                     ((consp result)
13157                      (setq split-name (append result split-name)))))))))
13158     split-name))
13159
13160 (defun gnus-read-move-group-name (prompt default articles prefix)
13161   "Read a group name."
13162   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13163          (minibuffer-confirm-incomplete nil) ; XEmacs
13164          group-map
13165          (dum (mapatoms
13166                (lambda (g) 
13167                  (and (boundp g)
13168                       (symbol-name g)
13169                       (memq 'respool
13170                             (assoc (symbol-name
13171                                     (car (gnus-find-method-for-group
13172                                           (symbol-name g))))
13173                                    gnus-valid-select-methods))
13174                       (push (list (symbol-name g)) group-map)))
13175                gnus-active-hashtb))
13176          (prom
13177           (format "%s %s to:"
13178                   prompt
13179                   (if (> (length articles) 1)
13180                       (format "these %d articles" (length articles))
13181                     "this article")))
13182          (to-newsgroup
13183           (cond
13184            ((null split-name)
13185             (gnus-completing-read default prom
13186                                   group-map nil nil prefix
13187                                   'gnus-group-history))
13188            ((= 1 (length split-name))
13189             (gnus-completing-read (car split-name) prom group-map
13190                                   nil nil nil
13191                                   'gnus-group-history))
13192            (t
13193             (gnus-completing-read nil prom 
13194                                   (mapcar (lambda (el) (list el))
13195                                           (nreverse split-name))
13196                                   nil nil nil
13197                                   'gnus-group-history)))))
13198     (when to-newsgroup
13199       (if (or (string= to-newsgroup "")
13200               (string= to-newsgroup prefix))
13201           (setq to-newsgroup (or default "")))
13202       (or (gnus-active to-newsgroup)
13203           (gnus-activate-group to-newsgroup)
13204           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
13205                                      to-newsgroup))
13206               (or (and (gnus-request-create-group 
13207                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
13208                        (gnus-activate-group to-newsgroup nil nil
13209                                             (gnus-group-name-to-method
13210                                              to-newsgroup)))
13211                   (error "Couldn't create group %s" to-newsgroup)))
13212           (error "No such group: %s" to-newsgroup)))
13213     to-newsgroup))
13214
13215 (defun gnus-read-save-file-name (prompt default-name)
13216   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13217          (file
13218           ;; Let the split methods have their say.
13219           (cond
13220            ;; No split name was found.
13221            ((null split-name)
13222             (read-file-name
13223              (concat prompt " (default "
13224                      (file-name-nondirectory default-name) ") ")
13225              (file-name-directory default-name)
13226              default-name))
13227            ;; A single split name was found
13228            ((= 1 (length split-name))
13229             (let* ((name (car split-name))
13230                    (dir (cond ((file-directory-p name)
13231                                (file-name-as-directory name))
13232                               ((file-exists-p name) name)
13233                               (t gnus-article-save-directory))))
13234               (read-file-name
13235                (concat prompt " (default " name ") ")
13236                dir name)))
13237            ;; A list of splits was found.
13238            (t
13239             (setq split-name (nreverse split-name))
13240             (let (result)
13241               (let ((file-name-history (nconc split-name file-name-history)))
13242                 (setq result
13243                       (read-file-name
13244                        (concat prompt " (`M-p' for defaults) ")
13245                        gnus-article-save-directory
13246                        (car split-name))))
13247               (car (push result file-name-history)))))))
13248     ;; If we have read a directory, we append the default file name.
13249     (when (file-directory-p file)
13250       (setq file (concat (file-name-as-directory file)
13251                          (file-name-nondirectory default-name))))
13252     ;; Possibly translate some charaters.
13253     (nnheader-translate-file-chars file)))
13254
13255 (defun gnus-article-archive-name (group)
13256   "Return the first instance of an \"Archive-name\" in the current buffer."
13257   (let ((case-fold-search t))
13258     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13259       (match-string 1))))
13260
13261 (defun gnus-summary-save-in-rmail (&optional filename)
13262   "Append this article to Rmail file.
13263 Optional argument FILENAME specifies file name.
13264 Directory to save to is default to `gnus-article-save-directory'."
13265   (interactive)
13266   (gnus-set-global-variables)
13267   (let ((default-name
13268           (funcall gnus-rmail-save-name gnus-newsgroup-name
13269                    gnus-current-headers gnus-newsgroup-last-rmail)))
13270     (setq filename
13271           (cond ((eq filename 'default)
13272                  default-name)
13273                 (filename filename)
13274                 (t (gnus-read-save-file-name
13275                     "Save in rmail file:" default-name))))
13276     (gnus-make-directory (file-name-directory filename))
13277     (gnus-eval-in-buffer-window gnus-original-article-buffer
13278       (save-excursion
13279         (save-restriction
13280           (widen)
13281           (gnus-output-to-rmail filename))))
13282     ;; Remember the directory name to save articles
13283     (setq gnus-newsgroup-last-rmail filename)))
13284
13285 (defun gnus-summary-save-in-mail (&optional filename)
13286   "Append this article to Unix mail file.
13287 Optional argument FILENAME specifies file name.
13288 Directory to save to is default to `gnus-article-save-directory'."
13289   (interactive)
13290   (gnus-set-global-variables)
13291   (let ((default-name
13292           (funcall gnus-mail-save-name gnus-newsgroup-name
13293                    gnus-current-headers gnus-newsgroup-last-mail)))
13294     (setq filename
13295           (cond ((eq filename 'default)
13296                  default-name)
13297                 (filename filename)
13298                 (t (gnus-read-save-file-name
13299                     "Save in Unix mail file:" default-name))))
13300     (setq filename
13301           (expand-file-name filename
13302                             (and default-name
13303                                  (file-name-directory default-name))))
13304     (gnus-make-directory (file-name-directory filename))
13305     (gnus-eval-in-buffer-window gnus-original-article-buffer
13306       (save-excursion
13307         (save-restriction
13308           (widen)
13309           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13310               (gnus-output-to-rmail filename)
13311             (let ((mail-use-rfc822 t))
13312               (rmail-output filename 1 t t))))))
13313     ;; Remember the directory name to save articles.
13314     (setq gnus-newsgroup-last-mail filename)))
13315
13316 (defun gnus-summary-save-in-file (&optional filename)
13317   "Append this article to file.
13318 Optional argument FILENAME specifies file name.
13319 Directory to save to is default to `gnus-article-save-directory'."
13320   (interactive)
13321   (gnus-set-global-variables)
13322   (let ((default-name
13323           (funcall gnus-file-save-name gnus-newsgroup-name
13324                    gnus-current-headers gnus-newsgroup-last-file)))
13325     (setq filename
13326           (cond ((eq filename 'default)
13327                  default-name)
13328                 (filename filename)
13329                 (t (gnus-read-save-file-name
13330                     "Save in file:" default-name))))
13331     (gnus-make-directory (file-name-directory filename))
13332     (gnus-eval-in-buffer-window gnus-original-article-buffer
13333       (save-excursion
13334         (save-restriction
13335           (widen)
13336           (gnus-output-to-file filename))))
13337     ;; Remember the directory name to save articles.
13338     (setq gnus-newsgroup-last-file filename)))
13339
13340 (defun gnus-summary-save-body-in-file (&optional filename)
13341   "Append this article body to a file.
13342 Optional argument FILENAME specifies file name.
13343 The directory to save in defaults to `gnus-article-save-directory'."
13344   (interactive)
13345   (gnus-set-global-variables)
13346   (let ((default-name
13347           (funcall gnus-file-save-name gnus-newsgroup-name
13348                    gnus-current-headers gnus-newsgroup-last-file)))
13349     (setq filename
13350           (cond ((eq filename 'default)
13351                  default-name)
13352                 (filename filename)
13353                 (t (gnus-read-save-file-name
13354                     "Save body in file:" default-name))))
13355     (gnus-make-directory (file-name-directory filename))
13356     (gnus-eval-in-buffer-window gnus-original-article-buffer
13357       (save-excursion
13358         (save-restriction
13359           (widen)
13360           (goto-char (point-min))
13361           (and (search-forward "\n\n" nil t)
13362                (narrow-to-region (point) (point-max)))
13363           (gnus-output-to-file filename))))
13364     ;; Remember the directory name to save articles.
13365     (setq gnus-newsgroup-last-file filename)))
13366
13367 (defun gnus-summary-save-in-pipe (&optional command)
13368   "Pipe this article to subprocess."
13369   (interactive)
13370   (gnus-set-global-variables)
13371   (setq command
13372         (cond ((eq command 'default)
13373                gnus-last-shell-command)
13374               (command command)
13375               (t (read-string "Shell command on article: "
13376                               gnus-last-shell-command))))
13377   (if (string-equal command "")
13378       (setq command gnus-last-shell-command))
13379   (gnus-eval-in-buffer-window gnus-article-buffer
13380     (save-restriction
13381       (widen)
13382       (shell-command-on-region (point-min) (point-max) command nil)))
13383   (setq gnus-last-shell-command command))
13384
13385 ;; Summary extract commands
13386
13387 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13388   (let ((buffer-read-only nil)
13389         (article (gnus-summary-article-number))
13390         after-article b e)
13391     (or (gnus-summary-goto-subject article)
13392         (error (format "No such article: %d" article)))
13393     (gnus-summary-position-point)
13394     ;; If all commands are to be bunched up on one line, we collect
13395     ;; them here.
13396     (if gnus-view-pseudos-separately
13397         ()
13398       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13399             files action)
13400         (while ps
13401           (setq action (cdr (assq 'action (car ps))))
13402           (setq files (list (cdr (assq 'name (car ps)))))
13403           (while (and ps (cdr ps)
13404                       (string= (or action "1")
13405                                (or (cdr (assq 'action (cadr ps))) "2")))
13406             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13407             (setcdr ps (cddr ps)))
13408           (if (not files)
13409               ()
13410             (if (not (string-match "%s" action))
13411                 (setq files (cons " " files)))
13412             (setq files (cons " " files))
13413             (and (assq 'execute (car ps))
13414                  (setcdr (assq 'execute (car ps))
13415                          (funcall (if (string-match "%s" action)
13416                                       'format 'concat)
13417                                   action
13418                                   (mapconcat (lambda (f) f) files " ")))))
13419           (setq ps (cdr ps)))))
13420     (if (and gnus-view-pseudos (not not-view))
13421         (while pslist
13422           (and (assq 'execute (car pslist))
13423                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13424                                      (eq gnus-view-pseudos 'not-confirm)))
13425           (setq pslist (cdr pslist)))
13426       (save-excursion
13427         (while pslist
13428           (setq after-article (or (cdr (assq 'article (car pslist)))
13429                                   (gnus-summary-article-number)))
13430           (gnus-summary-goto-subject after-article)
13431           (forward-line 1)
13432           (setq b (point))
13433           (insert "    " (file-name-nondirectory
13434                                 (cdr (assq 'name (car pslist))))
13435                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13436           (setq e (point))
13437           (forward-line -1)             ; back to `b'
13438           (gnus-add-text-properties
13439            b (1- e) (list 'gnus-number gnus-reffed-article-number
13440                           gnus-mouse-face-prop gnus-mouse-face))
13441           (gnus-data-enter
13442            after-article gnus-reffed-article-number
13443            gnus-unread-mark b (car pslist) 0 (- e b))
13444           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13445           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13446           (setq pslist (cdr pslist)))))))
13447
13448 (defun gnus-pseudos< (p1 p2)
13449   (let ((c1 (cdr (assq 'action p1)))
13450         (c2 (cdr (assq 'action p2))))
13451     (and c1 c2 (string< c1 c2))))
13452
13453 (defun gnus-request-pseudo-article (props)
13454   (cond ((assq 'execute props)
13455          (gnus-execute-command (cdr (assq 'execute props)))))
13456   (let ((gnus-current-article (gnus-summary-article-number)))
13457     (run-hooks 'gnus-mark-article-hook)))
13458
13459 (defun gnus-execute-command (command &optional automatic)
13460   (save-excursion
13461     (gnus-article-setup-buffer)
13462     (set-buffer gnus-article-buffer)
13463     (setq buffer-read-only nil)
13464     (let ((command (if automatic command (read-string "Command: " command)))
13465           ;; Just binding this here doesn't help, because there might
13466           ;; be output from the process after exiting the scope of 
13467           ;; this `let'.
13468           ;; (buffer-read-only nil)
13469           )
13470       (erase-buffer)
13471       (insert "$ " command "\n\n")
13472       (if gnus-view-pseudo-asynchronously
13473           (start-process "gnus-execute" nil shell-file-name
13474                          shell-command-switch command)
13475         (call-process shell-file-name nil t nil
13476                       shell-command-switch command)))))
13477
13478 (defun gnus-copy-file (file &optional to)
13479   "Copy FILE to TO."
13480   (interactive
13481    (list (read-file-name "Copy file: " default-directory)
13482          (read-file-name "Copy file to: " default-directory)))
13483   (gnus-set-global-variables)
13484   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13485   (and (file-directory-p to)
13486        (setq to (concat (file-name-as-directory to)
13487                         (file-name-nondirectory file))))
13488   (copy-file file to))
13489
13490 ;; Summary kill commands.
13491
13492 (defun gnus-summary-edit-global-kill (article)
13493   "Edit the \"global\" kill file."
13494   (interactive (list (gnus-summary-article-number)))
13495   (gnus-set-global-variables)
13496   (gnus-group-edit-global-kill article))
13497
13498 (defun gnus-summary-edit-local-kill ()
13499   "Edit a local kill file applied to the current newsgroup."
13500   (interactive)
13501   (gnus-set-global-variables)
13502   (setq gnus-current-headers (gnus-summary-article-header))
13503   (gnus-set-global-variables)
13504   (gnus-group-edit-local-kill
13505    (gnus-summary-article-number) gnus-newsgroup-name))
13506
13507 \f
13508 ;;;
13509 ;;; Gnus article mode
13510 ;;;
13511
13512 (put 'gnus-article-mode 'mode-class 'special)
13513
13514 (if gnus-article-mode-map
13515     nil
13516   (setq gnus-article-mode-map (make-keymap))
13517   (suppress-keymap gnus-article-mode-map)
13518
13519   (gnus-define-keys gnus-article-mode-map
13520     " " gnus-article-goto-next-page
13521     "\177" gnus-article-goto-prev-page
13522     [delete] gnus-article-goto-prev-page
13523     "\C-c^" gnus-article-refer-article
13524     "h" gnus-article-show-summary
13525     "s" gnus-article-show-summary
13526     "\C-c\C-m" gnus-article-mail
13527     "?" gnus-article-describe-briefly
13528     gnus-mouse-2 gnus-article-push-button
13529     "\r" gnus-article-press-button
13530     "\t" gnus-article-next-button
13531     "\M-\t" gnus-article-prev-button
13532     "<" beginning-of-buffer
13533     ">" end-of-buffer
13534     "\C-c\C-b" gnus-bug)
13535
13536   (substitute-key-definition
13537    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13538
13539 (defun gnus-article-mode ()
13540   "Major mode for displaying an article.
13541
13542 All normal editing commands are switched off.
13543
13544 The following commands are available:
13545
13546 \\<gnus-article-mode-map>
13547 \\[gnus-article-next-page]\t Scroll the article one page forwards
13548 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13549 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13550 \\[gnus-article-show-summary]\t Display the summary buffer
13551 \\[gnus-article-mail]\t Send a reply to the address near point
13552 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13553 \\[gnus-info-find-node]\t Go to the Gnus info node"
13554   (interactive)
13555   (when (and menu-bar-mode
13556              (gnus-visual-p 'article-menu 'menu))
13557     (gnus-article-make-menu-bar))
13558   (kill-all-local-variables)
13559   (gnus-simplify-mode-line)
13560   (setq mode-name "Article")
13561   (setq major-mode 'gnus-article-mode)
13562   (make-local-variable 'minor-mode-alist)
13563   (or (assq 'gnus-show-mime minor-mode-alist)
13564       (setq minor-mode-alist
13565             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13566   (use-local-map gnus-article-mode-map)
13567   (make-local-variable 'page-delimiter)
13568   (setq page-delimiter gnus-page-delimiter)
13569   (buffer-disable-undo (current-buffer))
13570   (setq buffer-read-only t)             ;Disable modification
13571   (run-hooks 'gnus-article-mode-hook))
13572
13573 (defun gnus-article-setup-buffer ()
13574   "Initialize the article buffer."
13575   (let* ((name (if gnus-single-article-buffer "*Article*"
13576                  (concat "*Article " gnus-newsgroup-name "*")))
13577          (original
13578           (progn (string-match "\\*Article" name)
13579                  (concat " *Original Article"
13580                          (substring name (match-end 0))))))
13581     (setq gnus-article-buffer name)
13582     (setq gnus-original-article-buffer original)
13583     ;; This might be a variable local to the summary buffer.
13584     (unless gnus-single-article-buffer
13585       (save-excursion
13586         (set-buffer gnus-summary-buffer)
13587         (setq gnus-article-buffer name)
13588         (setq gnus-original-article-buffer original)
13589         (gnus-set-global-variables))
13590       (make-local-variable 'gnus-summary-buffer))
13591     ;; Init original article buffer.
13592     (save-excursion
13593       (set-buffer (get-buffer-create gnus-original-article-buffer))
13594       (buffer-disable-undo (current-buffer))
13595       (setq major-mode 'gnus-original-article-mode)
13596       (make-local-variable 'gnus-original-article))
13597     (if (get-buffer name)
13598         (save-excursion
13599           (set-buffer name)
13600           (buffer-disable-undo (current-buffer))
13601           (setq buffer-read-only t)
13602           (gnus-add-current-to-buffer-list)
13603           (or (eq major-mode 'gnus-article-mode)
13604               (gnus-article-mode))
13605           (current-buffer))
13606       (save-excursion
13607         (set-buffer (get-buffer-create name))
13608         (gnus-add-current-to-buffer-list)
13609         (gnus-article-mode)
13610         (current-buffer)))))
13611
13612 ;; Set article window start at LINE, where LINE is the number of lines
13613 ;; from the head of the article.
13614 (defun gnus-article-set-window-start (&optional line)
13615   (set-window-start
13616    (get-buffer-window gnus-article-buffer t)
13617    (save-excursion
13618      (set-buffer gnus-article-buffer)
13619      (goto-char (point-min))
13620      (if (not line)
13621          (point-min)
13622        (gnus-message 6 "Moved to bookmark")
13623        (search-forward "\n\n" nil t)
13624        (forward-line line)
13625        (point)))))
13626
13627 (defun gnus-kill-all-overlays ()
13628   "Delete all overlays in the current buffer."
13629   (when (fboundp 'overlay-lists)
13630     (let* ((overlayss (overlay-lists))
13631            (buffer-read-only nil)
13632            (overlays (nconc (car overlayss) (cdr overlayss))))
13633       (while overlays
13634         (delete-overlay (pop overlays))))))
13635
13636 (defun gnus-request-article-this-buffer (article group)
13637   "Get an article and insert it into this buffer."
13638   (let (do-update-line)
13639     (prog1
13640         (save-excursion
13641           (erase-buffer)
13642           (gnus-kill-all-overlays)
13643           (setq group (or group gnus-newsgroup-name))
13644
13645           ;; Open server if it has closed.
13646           (gnus-check-server (gnus-find-method-for-group group))
13647
13648           ;; Using `gnus-request-article' directly will insert the article into
13649           ;; `nntp-server-buffer' - so we'll save some time by not having to
13650           ;; copy it from the server buffer into the article buffer.
13651
13652           ;; We only request an article by message-id when we do not have the
13653           ;; headers for it, so we'll have to get those.
13654           (when (stringp article)
13655             (let ((gnus-override-method gnus-refer-article-method))
13656               (gnus-read-header article)))
13657
13658           ;; If the article number is negative, that means that this article
13659           ;; doesn't belong in this newsgroup (possibly), so we find its
13660           ;; message-id and request it by id instead of number.
13661           (when (and (numberp article)
13662                      gnus-summary-buffer
13663                      (get-buffer gnus-summary-buffer)
13664                      (buffer-name (get-buffer gnus-summary-buffer)))
13665             (save-excursion
13666               (set-buffer gnus-summary-buffer)
13667               (let ((header (gnus-summary-article-header article)))
13668                 (if (< article 0)
13669                     (cond 
13670                      ((memq article gnus-newsgroup-sparse)
13671                       ;; This is a sparse gap article.
13672                       (setq do-update-line article)
13673                       (setq article (mail-header-id header))
13674                       (let ((gnus-override-method gnus-refer-article-method))
13675                         (gnus-read-header article))
13676                       (setq gnus-newsgroup-sparse
13677                             (delq article gnus-newsgroup-sparse)))
13678                      ((vectorp header)
13679                       ;; It's a real article.
13680                       (setq article (mail-header-id header)))
13681                      (t
13682                       ;; It is an extracted pseudo-article.
13683                       (setq article 'pseudo)
13684                       (gnus-request-pseudo-article header))))
13685                 
13686                 (let ((method (gnus-find-method-for-group 
13687                                gnus-newsgroup-name)))
13688                   (if (not (eq (car method) 'nneething))
13689                       ()
13690                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13691                                        (mail-header-subject header))))
13692                       (if (file-directory-p dir)
13693                           (progn
13694                             (setq article 'nneething)
13695                             (gnus-group-enter-directory dir)))))))))
13696
13697           (cond
13698            ;; Refuse to select canceled articles.
13699            ((and (numberp article)
13700                  gnus-summary-buffer
13701                  (get-buffer gnus-summary-buffer)
13702                  (buffer-name (get-buffer gnus-summary-buffer))
13703                  (eq (cdr (save-excursion
13704                             (set-buffer gnus-summary-buffer)
13705                             (assq article gnus-newsgroup-reads)))
13706                      gnus-canceled-mark))
13707             nil)
13708            ;; We first check `gnus-original-article-buffer'.
13709            ((and (get-buffer gnus-original-article-buffer)
13710                  (numberp article)
13711                  (save-excursion
13712                    (set-buffer gnus-original-article-buffer)
13713                    (and (equal (car gnus-original-article) group)
13714                         (eq (cdr gnus-original-article) article))))
13715             (insert-buffer-substring gnus-original-article-buffer)
13716             'article)
13717            ;; Check the backlog.
13718            ((and gnus-keep-backlog
13719                  (gnus-backlog-request-article group article (current-buffer)))
13720             'article)
13721            ;; Check the cache.
13722            ((and gnus-use-cache
13723                  (numberp article)
13724                  (gnus-cache-request-article article group))
13725             'article)
13726            ;; Get the article and put into the article buffer.
13727            ((or (stringp article) (numberp article))
13728             (let ((gnus-override-method
13729                    (and (stringp article) gnus-refer-article-method))
13730                   (buffer-read-only nil))
13731               (erase-buffer)
13732               (gnus-kill-all-overlays)
13733               (if (gnus-request-article article group (current-buffer))
13734                   (progn
13735                     (and gnus-keep-backlog
13736                          (numberp article)
13737                          (gnus-backlog-enter-article
13738                           group article (current-buffer)))
13739                     'article))))
13740            ;; It was a pseudo.
13741            (t article)))
13742
13743       ;; Take the article from the original article buffer
13744       ;; and place it in the buffer it's supposed to be in.
13745       (when (and (get-buffer gnus-article-buffer)
13746                  ;;(numberp article)
13747                  (equal (buffer-name (current-buffer))
13748                         (buffer-name (get-buffer gnus-article-buffer))))
13749         (save-excursion
13750           (if (get-buffer gnus-original-article-buffer)
13751               (set-buffer (get-buffer gnus-original-article-buffer))
13752             (set-buffer (get-buffer-create gnus-original-article-buffer))
13753             (buffer-disable-undo (current-buffer))
13754             (setq major-mode 'gnus-original-article-mode)
13755             (setq buffer-read-only t)
13756             (gnus-add-current-to-buffer-list))
13757           (let (buffer-read-only)
13758             (erase-buffer)
13759             (insert-buffer-substring gnus-article-buffer))
13760           (setq gnus-original-article (cons group article))))
13761     
13762       ;; Update sparse articles.
13763       (when (and do-update-line
13764                  (or (numberp article)
13765                      (stringp article)))
13766         (let ((buf (current-buffer)))
13767           (set-buffer gnus-summary-buffer)
13768           (gnus-summary-update-article do-update-line)
13769           (gnus-summary-goto-subject do-update-line nil t)
13770           (set-window-point (get-buffer-window (current-buffer) t)
13771                             (point))
13772           (set-buffer buf))))))
13773
13774 (defun gnus-read-header (id &optional header)
13775   "Read the headers of article ID and enter them into the Gnus system."
13776   (let ((group gnus-newsgroup-name)
13777         (gnus-override-method 
13778          (and (gnus-news-group-p gnus-newsgroup-name)
13779               gnus-refer-article-method))       
13780         where)
13781     ;; First we check to see whether the header in question is already
13782     ;; fetched.
13783     (if (stringp id)
13784         ;; This is a Message-ID.
13785         (setq header (or header (gnus-id-to-header id)))
13786       ;; This is an article number.
13787       (setq header (or header (gnus-summary-article-header id))))
13788     (if (and header
13789              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13790         ;; We have found the header.
13791         header
13792       ;; We have to really fetch the header to this article.
13793       (when (setq where (gnus-request-head id group))
13794         (save-excursion
13795           (set-buffer nntp-server-buffer)
13796           (goto-char (point-max))
13797           (insert ".\n")
13798           (goto-char (point-min))
13799           (insert "211 ")
13800           (princ (cond
13801                   ((numberp id) id)
13802                   ((cdr where) (cdr where))
13803                   (header (mail-header-number header))
13804                   (t gnus-reffed-article-number))
13805                  (current-buffer))
13806           (insert " Article retrieved.\n"))
13807         ;(when (and header
13808         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13809         ;  (setcar (gnus-id-to-thread id) nil))
13810         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13811             ()                          ; Malformed head.
13812           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13813             (if (and (stringp id)
13814                      (not (string= (gnus-group-real-name group)
13815                                    (car where))))
13816                 ;; If we fetched by Message-ID and the article came
13817                 ;; from a different group, we fudge some bogus article
13818                 ;; numbers for this article.
13819                 (mail-header-set-number header gnus-reffed-article-number))
13820             (decf gnus-reffed-article-number)
13821             (gnus-remove-header (mail-header-number header))
13822             (push header gnus-newsgroup-headers)
13823             (setq gnus-current-headers header)
13824             (push (mail-header-number header) gnus-newsgroup-limit))
13825           header)))))
13826
13827 (defun gnus-remove-header (number)
13828   "Remove header NUMBER from `gnus-newsgroup-headers'."
13829   (if (and gnus-newsgroup-headers
13830            (= number (mail-header-number (car gnus-newsgroup-headers))))
13831       (pop gnus-newsgroup-headers)
13832     (let ((headers gnus-newsgroup-headers))
13833       (while (and (cdr headers)
13834                   (not (= number (mail-header-number (cadr headers)))))
13835         (pop headers))
13836       (when (cdr headers)
13837         (setcdr headers (cddr headers))))))
13838
13839 (defun gnus-article-prepare (article &optional all-headers header)
13840   "Prepare ARTICLE in article mode buffer.
13841 ARTICLE should either be an article number or a Message-ID.
13842 If ARTICLE is an id, HEADER should be the article headers.
13843 If ALL-HEADERS is non-nil, no headers are hidden."
13844   (save-excursion
13845     ;; Make sure we start in a summary buffer.
13846     (unless (eq major-mode 'gnus-summary-mode)
13847       (set-buffer gnus-summary-buffer))
13848     (setq gnus-summary-buffer (current-buffer))
13849     ;; Make sure the connection to the server is alive.
13850     (unless (gnus-server-opened
13851              (gnus-find-method-for-group gnus-newsgroup-name))
13852       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13853       (gnus-request-group gnus-newsgroup-name t))
13854     (let* ((article (if header (mail-header-number header) article))
13855            (summary-buffer (current-buffer))
13856            (internal-hook gnus-article-internal-prepare-hook)
13857            (group gnus-newsgroup-name)
13858            result)
13859       (save-excursion
13860         (gnus-article-setup-buffer)
13861         (set-buffer gnus-article-buffer)
13862         ;; Deactivate active regions.
13863         (when (and (boundp 'transient-mark-mode)
13864                    transient-mark-mode)
13865           (setq mark-active nil))
13866         (if (not (setq result (let ((buffer-read-only nil))
13867                                 (gnus-request-article-this-buffer
13868                                  article group))))
13869             ;; There is no such article.
13870             (save-excursion
13871               (when (and (numberp article)
13872                          (not (memq article gnus-newsgroup-sparse)))
13873                 (setq gnus-article-current
13874                       (cons gnus-newsgroup-name article))
13875                 (set-buffer gnus-summary-buffer)
13876                 (setq gnus-current-article article)
13877                 (gnus-summary-mark-article article gnus-canceled-mark))
13878               (unless (memq article gnus-newsgroup-sparse)
13879                 (gnus-error
13880                  1 "No such article (may have expired or been canceled)")))
13881           (if (or (eq result 'pseudo) (eq result 'nneething))
13882               (progn
13883                 (save-excursion
13884                   (set-buffer summary-buffer)
13885                   (setq gnus-last-article gnus-current-article
13886                         gnus-newsgroup-history (cons gnus-current-article
13887                                                      gnus-newsgroup-history)
13888                         gnus-current-article 0
13889                         gnus-current-headers nil
13890                         gnus-article-current nil)
13891                   (if (eq result 'nneething)
13892                       (gnus-configure-windows 'summary)
13893                     (gnus-configure-windows 'article))
13894                   (gnus-set-global-variables))
13895                 (gnus-set-mode-line 'article))
13896             ;; The result from the `request' was an actual article -
13897             ;; or at least some text that is now displayed in the
13898             ;; article buffer.
13899             (if (and (numberp article)
13900                      (not (eq article gnus-current-article)))
13901                 ;; Seems like a new article has been selected.
13902                 ;; `gnus-current-article' must be an article number.
13903                 (save-excursion
13904                   (set-buffer summary-buffer)
13905                   (setq gnus-last-article gnus-current-article
13906                         gnus-newsgroup-history (cons gnus-current-article
13907                                                      gnus-newsgroup-history)
13908                         gnus-current-article article
13909                         gnus-current-headers
13910                         (gnus-summary-article-header gnus-current-article)
13911                         gnus-article-current
13912                         (cons gnus-newsgroup-name gnus-current-article))
13913                   (unless (vectorp gnus-current-headers)
13914                     (setq gnus-current-headers nil))
13915                   (gnus-summary-show-thread)
13916                   (run-hooks 'gnus-mark-article-hook)
13917                   (gnus-set-mode-line 'summary)
13918                   (and (gnus-visual-p 'article-highlight 'highlight)
13919                        (run-hooks 'gnus-visual-mark-article-hook))
13920                   ;; Set the global newsgroup variables here.
13921                   ;; Suggested by Jim Sisolak
13922                   ;; <sisolak@trans4.neep.wisc.edu>.
13923                   (gnus-set-global-variables)
13924                   (setq gnus-have-all-headers
13925                         (or all-headers gnus-show-all-headers))
13926                   (and gnus-use-cache
13927                        (vectorp (gnus-summary-article-header article))
13928                        (gnus-cache-possibly-enter-article
13929                         group article
13930                         (gnus-summary-article-header article)
13931                         (memq article gnus-newsgroup-marked)
13932                         (memq article gnus-newsgroup-dormant)
13933                         (memq article gnus-newsgroup-unreads)))))
13934             (when (or (numberp article)
13935                       (stringp article))
13936               ;; Hooks for getting information from the article.
13937               ;; This hook must be called before being narrowed.
13938               (let (buffer-read-only)
13939                 (run-hooks 'internal-hook)
13940                 (run-hooks 'gnus-article-prepare-hook)
13941                 ;; Decode MIME message.
13942                 (if gnus-show-mime
13943                     (if (or (not gnus-strict-mime)
13944                             (gnus-fetch-field "Mime-Version"))
13945                         (funcall gnus-show-mime-method)
13946                       (funcall gnus-decode-encoded-word-method)))
13947                 ;; Perform the article display hooks.
13948                 (run-hooks 'gnus-article-display-hook))
13949               ;; Do page break.
13950               (goto-char (point-min))
13951               (and gnus-break-pages (gnus-narrow-to-page)))
13952             (gnus-set-mode-line 'article)
13953             (gnus-configure-windows 'article)
13954             (goto-char (point-min))
13955             t))))))
13956
13957 (defun gnus-article-show-all-headers ()
13958   "Show all article headers in article mode buffer."
13959   (save-excursion
13960     (gnus-article-setup-buffer)
13961     (set-buffer gnus-article-buffer)
13962     (let ((buffer-read-only nil))
13963       (gnus-unhide-text (point-min) (point-max)))))
13964
13965 (defun gnus-article-hide-headers-if-wanted ()
13966   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13967 Provided for backwards compatibility."
13968   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13969       gnus-inhibit-hiding
13970       (gnus-article-hide-headers)))
13971
13972 (defsubst gnus-article-header-rank ()
13973   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13974   (let ((list gnus-sorted-header-list)
13975         (i 0))
13976     (while list
13977       (when (looking-at (car list))
13978         (setq list nil))
13979       (setq list (cdr list))
13980       (incf i))
13981     i))
13982
13983 (defun gnus-article-hide-headers (&optional arg delete)
13984   "Toggle whether to hide unwanted headers and possibly sort them as well.
13985 If given a negative prefix, always show; if given a positive prefix,
13986 always hide."
13987   (interactive (gnus-hidden-arg))
13988   (if (gnus-article-check-hidden-text 'headers arg)
13989       ;; Show boring headers as well.
13990       (gnus-article-show-hidden-text 'boring-headers)
13991     ;; This function might be inhibited.
13992     (unless gnus-inhibit-hiding
13993       (save-excursion
13994         (set-buffer gnus-article-buffer)
13995         (save-restriction
13996           (let ((buffer-read-only nil)
13997                 (props (nconc (list 'gnus-type 'headers)
13998                               gnus-hidden-properties))
13999                 (max (1+ (length gnus-sorted-header-list)))
14000                 (ignored (when (not (stringp gnus-visible-headers))
14001                            (cond ((stringp gnus-ignored-headers)
14002                                   gnus-ignored-headers)
14003                                  ((listp gnus-ignored-headers)
14004                                   (mapconcat 'identity gnus-ignored-headers
14005                                              "\\|")))))
14006                 (visible
14007                  (cond ((stringp gnus-visible-headers)
14008                         gnus-visible-headers)
14009                        ((and gnus-visible-headers
14010                              (listp gnus-visible-headers))
14011                         (mapconcat 'identity gnus-visible-headers "\\|"))))
14012                 (inhibit-point-motion-hooks t)
14013                 want-list beg)
14014             ;; First we narrow to just the headers.
14015             (widen)
14016             (goto-char (point-min))
14017             ;; Hide any "From " lines at the beginning of (mail) articles.
14018             (while (looking-at "From ")
14019               (forward-line 1))
14020             (unless (bobp)
14021               (if delete
14022                   (delete-region (point-min) (point))
14023                 (gnus-hide-text (point-min) (point) props)))
14024             ;; Then treat the rest of the header lines.
14025             (narrow-to-region
14026              (point)
14027              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
14028             ;; Then we use the two regular expressions
14029             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
14030             ;; select which header lines is to remain visible in the
14031             ;; article buffer.
14032             (goto-char (point-min))
14033             (while (re-search-forward "^[^ \t]*:" nil t)
14034               (beginning-of-line)
14035               ;; We add the headers we want to keep to a list and delete
14036               ;; them from the buffer.
14037               (gnus-put-text-property 
14038                (point) (1+ (point)) 'message-rank
14039                (if (or (and visible (looking-at visible))
14040                        (and ignored
14041                             (not (looking-at ignored))))
14042                    (gnus-article-header-rank) 
14043                  (+ 2 max)))
14044               (forward-line 1))
14045             (message-sort-headers-1)
14046             (when (setq beg (text-property-any 
14047                              (point-min) (point-max) 'message-rank (+ 2 max)))
14048               ;; We make the unwanted headers invisible.
14049               (if delete
14050                   (delete-region beg (point-max))
14051                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
14052                 (gnus-hide-text-type beg (point-max) 'headers))
14053               ;; Work around XEmacs lossage.
14054               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
14055
14056 (defun gnus-article-hide-boring-headers (&optional arg)
14057   "Toggle hiding of headers that aren't very interesting.
14058 If given a negative prefix, always show; if given a positive prefix,
14059 always hide."
14060   (interactive (gnus-hidden-arg))
14061   (unless (gnus-article-check-hidden-text 'boring-headers arg)
14062     (save-excursion
14063       (set-buffer gnus-article-buffer)
14064       (save-restriction
14065         (let ((buffer-read-only nil)
14066               (list gnus-boring-article-headers)
14067               (inhibit-point-motion-hooks t)
14068               elem)
14069           (nnheader-narrow-to-headers)
14070           (while list
14071             (setq elem (pop list))
14072             (goto-char (point-min))
14073             (cond
14074              ;; Hide empty headers.
14075              ((eq elem 'empty)
14076               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
14077                 (forward-line -1)
14078                 (gnus-hide-text-type
14079                  (progn (beginning-of-line) (point))
14080                  (progn 
14081                    (end-of-line)
14082                    (if (re-search-forward "^[^ \t]" nil t)
14083                        (match-beginning 0)
14084                      (point-max)))
14085                  'boring-headers)))
14086              ;; Hide boring Newsgroups header.
14087              ((eq elem 'newsgroups)
14088               (when (equal (message-fetch-field "newsgroups")
14089                            (gnus-group-real-name gnus-newsgroup-name))
14090                 (gnus-article-hide-header "newsgroups")))
14091              ((eq elem 'followup-to)
14092               (when (equal (message-fetch-field "followup-to")
14093                            (message-fetch-field "newsgroups"))
14094                 (gnus-article-hide-header "followup-to")))
14095              ((eq elem 'reply-to)
14096               (let ((from (message-fetch-field "from"))
14097                     (reply-to (message-fetch-field "reply-to")))
14098                 (when (and
14099                        from reply-to
14100                        (equal 
14101                         (nth 1 (funcall gnus-extract-address-components from))
14102                         (nth 1 (funcall gnus-extract-address-components
14103                                         reply-to))))
14104                   (gnus-article-hide-header "reply-to"))))
14105              ((eq elem 'date)
14106               (let ((date (message-fetch-field "date")))
14107                 (when (and date
14108                            (< (gnus-days-between date (current-time-string))
14109                               4))
14110                   (gnus-article-hide-header "date")))))))))))
14111
14112 (defun gnus-article-hide-header (header)
14113   (save-excursion
14114     (goto-char (point-min))
14115     (when (re-search-forward (concat "^" header ":") nil t)
14116       (gnus-hide-text-type
14117        (progn (beginning-of-line) (point))
14118        (progn 
14119          (end-of-line)
14120          (if (re-search-forward "^[^ \t]" nil t)
14121              (match-beginning 0)
14122            (point-max)))
14123        'boring-headers))))
14124
14125 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
14126 (defun gnus-article-treat-overstrike ()
14127   "Translate overstrikes into bold text."
14128   (interactive)
14129   (save-excursion
14130     (set-buffer gnus-article-buffer)
14131     (let ((buffer-read-only nil))
14132       (while (search-forward "\b" nil t)
14133         (let ((next (following-char))
14134               (previous (char-after (- (point) 2))))
14135           (cond 
14136            ((eq next previous)
14137             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14138             (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
14139            ((eq next ?_)
14140             (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
14141             (gnus-put-text-property
14142              (- (point) 2) (1- (point)) 'face 'underline))
14143            ((eq previous ?_)
14144             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14145             (gnus-put-text-property
14146              (point) (1+ (point))       'face 'underline))))))))
14147
14148 (defun gnus-article-word-wrap ()
14149   "Format too long lines."
14150   (interactive)
14151   (save-excursion
14152     (set-buffer gnus-article-buffer)
14153     (let ((buffer-read-only nil))
14154       (widen)
14155       (goto-char (point-min))
14156       (search-forward "\n\n" nil t)
14157       (end-of-line 1)
14158       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
14159             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
14160             (adaptive-fill-mode t))
14161         (while (not (eobp))
14162           (and (>= (current-column) (min fill-column (window-width)))
14163                (/= (preceding-char) ?:)
14164                (fill-paragraph nil))
14165           (end-of-line 2))))))
14166
14167 (defun gnus-article-remove-cr ()
14168   "Remove carriage returns from an article."
14169   (interactive)
14170   (save-excursion
14171     (set-buffer gnus-article-buffer)
14172     (let ((buffer-read-only nil))
14173       (goto-char (point-min))
14174       (while (search-forward "\r" nil t)
14175         (replace-match "" t t)))))
14176
14177 (defun gnus-article-remove-trailing-blank-lines ()
14178   "Remove all trailing blank lines from the article."
14179   (interactive)
14180   (save-excursion
14181     (set-buffer gnus-article-buffer)
14182     (let ((buffer-read-only nil))
14183       (goto-char (point-max))
14184       (delete-region
14185        (point)
14186        (progn
14187          (while (looking-at "^[ \t]*$")
14188            (forward-line -1))
14189          (forward-line 1)
14190          (point))))))
14191
14192 (defun gnus-article-display-x-face (&optional force)
14193   "Look for an X-Face header and display it if present."
14194   (interactive (list 'force))
14195   (save-excursion
14196     (set-buffer gnus-article-buffer)
14197     ;; Delete the old process, if any.
14198     (when (process-status "gnus-x-face")
14199       (delete-process "gnus-x-face"))
14200     (let ((inhibit-point-motion-hooks t)
14201           (case-fold-search nil)
14202           from)
14203       (save-restriction
14204         (nnheader-narrow-to-headers)
14205         (setq from (message-fetch-field "from"))
14206         (goto-char (point-min))
14207         (when (and gnus-article-x-face-command
14208                    (or force
14209                        ;; Check whether this face is censored.
14210                        (not gnus-article-x-face-too-ugly)
14211                        (and gnus-article-x-face-too-ugly from
14212                             (not (string-match gnus-article-x-face-too-ugly
14213                                                from))))
14214                    ;; Has to be present.
14215                    (re-search-forward "^X-Face: " nil t))
14216           ;; We now have the area of the buffer where the X-Face is stored.
14217           (let ((beg (point))
14218                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14219             ;; We display the face.
14220             (if (symbolp gnus-article-x-face-command)
14221                 ;; The command is a lisp function, so we call it.
14222                 (if (gnus-functionp gnus-article-x-face-command)
14223                     (funcall gnus-article-x-face-command beg end)
14224                   (error "%s is not a function" gnus-article-x-face-command))
14225               ;; The command is a string, so we interpret the command
14226               ;; as a, well, command, and fork it off.
14227               (let ((process-connection-type nil))
14228                 (process-kill-without-query
14229                  (start-process
14230                   "gnus-x-face" nil shell-file-name shell-command-switch
14231                   gnus-article-x-face-command))
14232                 (process-send-region "gnus-x-face" beg end)
14233                 (process-send-eof "gnus-x-face")))))))))
14234
14235 (defalias 'gnus-headers-decode-quoted-printable 'gnus-decode-rfc1522)
14236 (defun gnus-decode-rfc1522 ()
14237   "Hack to remove QP encoding from headers."
14238   (let ((case-fold-search t)
14239         (inhibit-point-motion-hooks t)
14240         (buffer-read-only nil)
14241         string)
14242     (save-restriction
14243       (narrow-to-region
14244        (goto-char (point-min))
14245        (or (search-forward "\n\n" nil t) (point-max)))
14246
14247       (while (re-search-forward 
14248               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14249         (setq string (match-string 1))
14250         (narrow-to-region (match-beginning 0) (match-end 0))
14251         (delete-region (point-min) (point-max))
14252         (insert string)
14253         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14254         (subst-char-in-region (point-min) (point-max) ?_ ? )
14255         (widen)
14256         (goto-char (point-min))))))
14257
14258 (defun gnus-article-de-quoted-unreadable (&optional force)
14259   "Do a naive translation of a quoted-printable-encoded article.
14260 This is in no way, shape or form meant as a replacement for real MIME
14261 processing, but is simply a stop-gap measure until MIME support is
14262 written.
14263 If FORCE, decode the article whether it is marked as quoted-printable
14264 or not."
14265   (interactive (list 'force))
14266   (save-excursion
14267     (set-buffer gnus-article-buffer)
14268     (let ((case-fold-search t)
14269           (buffer-read-only nil)
14270           (type (gnus-fetch-field "content-transfer-encoding")))
14271       (gnus-decode-rfc1522)
14272       (when (or force
14273                 (and type (string-match "quoted-printable" (downcase type))))
14274         (goto-char (point-min))
14275         (search-forward "\n\n" nil 'move)
14276         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14277
14278 (defun gnus-mime-decode-quoted-printable (from to)
14279   "Decode Quoted-Printable in the region between FROM and TO."
14280   (interactive "r")
14281   (goto-char from)
14282   (while (search-forward "=" to t)
14283     (cond ((eq (following-char) ?\n)
14284            (delete-char -1)
14285            (delete-char 1))
14286           ((looking-at "[0-9A-F][0-9A-F]")
14287            (subst-char-in-region
14288             (1- (point)) (point) ?=
14289             (hexl-hex-string-to-integer
14290              (buffer-substring (point) (+ 2 (point)))))
14291            (delete-char 2))
14292           ((looking-at "=")
14293            (delete-char 1))
14294           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14295
14296 (defun gnus-article-hide-pgp (&optional arg)
14297   "Toggle hiding of any PGP headers and signatures in the current article.
14298 If given a negative prefix, always show; if given a positive prefix,
14299 always hide."
14300   (interactive (gnus-hidden-arg))
14301   (unless (gnus-article-check-hidden-text 'pgp arg)
14302     (save-excursion
14303       (set-buffer gnus-article-buffer)
14304       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14305             buffer-read-only beg end)
14306         (widen)
14307         (goto-char (point-min))
14308         ;; Hide the "header".
14309         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14310              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14311         (setq beg (point))
14312         ;; Hide the actual signature.
14313         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14314              (setq end (1+ (match-beginning 0)))
14315              (gnus-hide-text
14316               end
14317               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14318                   (match-end 0)
14319                 ;; Perhaps we shouldn't hide to the end of the buffer
14320                 ;; if there is no end to the signature?
14321                 (point-max))
14322               props))
14323         ;; Hide "- " PGP quotation markers.
14324         (when (and beg end)
14325           (narrow-to-region beg end)
14326           (goto-char (point-min))
14327           (while (re-search-forward "^- " nil t)
14328             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14329           (widen))))))
14330
14331 (defun gnus-article-hide-signature (&optional arg)
14332   "Hide the signature in the current article.
14333 If given a negative prefix, always show; if given a positive prefix,
14334 always hide."
14335   (interactive (gnus-hidden-arg))
14336   (unless (gnus-article-check-hidden-text 'signature arg)
14337     (save-excursion
14338       (set-buffer gnus-article-buffer)
14339       (save-restriction
14340         (let ((buffer-read-only nil))
14341           (when (gnus-narrow-to-signature)
14342             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14343
14344 (defun gnus-article-strip-leading-blank-lines ()
14345   "Remove all blank lines from the beginning of the article."
14346   (interactive)
14347   (save-excursion
14348     (set-buffer gnus-article-buffer)
14349     (let (buffer-read-only)
14350       (goto-char (point-min))
14351       (when (search-forward "\n\n" nil t)
14352         (while (looking-at "[ \t]$")
14353           (gnus-delete-line))))))
14354
14355 (defvar mime::preview/content-list)
14356 (defvar mime::preview-content-info/point-min)
14357 (defun gnus-narrow-to-signature ()
14358   "Narrow to the signature."
14359   (widen)
14360   (if (and (boundp 'mime::preview/content-list)
14361            mime::preview/content-list)
14362       (let ((pcinfo (car (last mime::preview/content-list))))
14363         (condition-case ()
14364             (narrow-to-region
14365              (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14366              (point-max))
14367           (error nil))))
14368   (goto-char (point-max))
14369   (when (re-search-backward gnus-signature-separator nil t)
14370     (forward-line 1)
14371     (when (or (null gnus-signature-limit)
14372               (and (numberp gnus-signature-limit)
14373                    (< (- (point-max) (point)) gnus-signature-limit))
14374               (and (gnus-functionp gnus-signature-limit)
14375                    (funcall gnus-signature-limit))
14376               (and (stringp gnus-signature-limit)
14377                    (not (re-search-forward gnus-signature-limit nil t))))
14378       (narrow-to-region (point) (point-max))
14379       t)))
14380
14381 (defun gnus-hidden-arg ()
14382   "Return the current prefix arg as a number, or 0 if no prefix."
14383   (list (if current-prefix-arg
14384             (prefix-numeric-value current-prefix-arg)
14385           0)))
14386
14387 (defun gnus-article-check-hidden-text (type arg)
14388   "Return nil if hiding is necessary.
14389 Arg can be nil or a number.  Nil and positive means hide, negative
14390 means show, 0 means toggle."
14391   (save-excursion
14392     (set-buffer gnus-article-buffer)
14393     (let ((hide (gnus-article-hidden-text-p type)))
14394       (cond
14395        ((or (null arg)
14396             (> arg 0))
14397         nil)
14398        ((< arg 0)
14399         (gnus-article-show-hidden-text type))
14400        (t
14401         (if (eq hide 'hidden)
14402             (gnus-article-show-hidden-text type)
14403           nil))))))
14404
14405 (defun gnus-article-hidden-text-p (type)
14406   "Say whether the current buffer contains hidden text of type TYPE."
14407   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14408     (when pos
14409       (if (get-text-property pos 'invisible)
14410           'hidden
14411         'shown))))
14412
14413 (defun gnus-article-hide (&optional arg force)
14414   "Hide all the gruft in the current article.
14415 This means that PGP stuff, signatures, cited text and (some)
14416 headers will be hidden.
14417 If given a prefix, show the hidden text instead."
14418   (interactive (list current-prefix-arg 'force))
14419   (gnus-article-hide-headers arg)
14420   (gnus-article-hide-pgp arg)
14421   (gnus-article-hide-citation-maybe arg force)
14422   (gnus-article-hide-signature arg))
14423
14424 (defun gnus-article-show-hidden-text (type &optional hide)
14425   "Show all hidden text of type TYPE.
14426 If HIDE, hide the text instead."
14427   (save-excursion
14428     (set-buffer gnus-article-buffer)
14429     (let ((buffer-read-only nil)
14430           (inhibit-point-motion-hooks t)
14431           (beg (point-min)))
14432       (while (gnus-goto-char (text-property-any
14433                               beg (point-max) 'gnus-type type))
14434         (setq beg (point))
14435         (forward-char)
14436         (if hide
14437             (gnus-hide-text beg (point) gnus-hidden-properties)
14438           (gnus-unhide-text beg (point)))
14439         (setq beg (point)))
14440       t)))
14441
14442 (defvar gnus-article-time-units
14443   `((year . ,(* 365.25 24 60 60))
14444     (week . ,(* 7 24 60 60))
14445     (day . ,(* 24 60 60))
14446     (hour . ,(* 60 60))
14447     (minute . 60)
14448     (second . 1))
14449   "Mapping from time units to seconds.")
14450
14451 (defun gnus-article-date-ut (&optional type highlight)
14452   "Convert DATE date to universal time in the current article.
14453 If TYPE is `local', convert to local time; if it is `lapsed', output
14454 how much time has lapsed since DATE."
14455   (interactive (list 'ut t))
14456   (let* ((header (or gnus-current-headers
14457                      (gnus-summary-article-header) ""))
14458          (date (and (vectorp header) (mail-header-date header)))
14459          (date-regexp "^Date: \\|^X-Sent: ")
14460          (now (current-time))
14461          (inhibit-point-motion-hooks t)
14462          bface eface)
14463     (when (and date (not (string= date "")))
14464       (save-excursion
14465         (set-buffer gnus-article-buffer)
14466         (save-restriction
14467           (nnheader-narrow-to-headers)
14468           (let ((buffer-read-only nil))
14469             ;; Delete any old Date headers.
14470             (if (re-search-forward date-regexp nil t)
14471                 (progn
14472                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14473                         eface (get-text-property (1- (gnus-point-at-eol))
14474                                                  'face))
14475                   (message-remove-header date-regexp t)
14476                   (beginning-of-line))
14477               (goto-char (point-max)))
14478             (insert (gnus-make-date-line date type))
14479             ;; Do highlighting.
14480             (forward-line -1)
14481             (when (and (gnus-visual-p 'article-highlight 'highlight)
14482                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14483               (gnus-put-text-property (match-beginning 1) (match-end 1)
14484                                  'face bface)
14485               (gnus-put-text-property (match-beginning 2) (match-end 2)
14486                                  'face eface))))))))
14487
14488 (defun gnus-make-date-line (date type)
14489   "Return a DATE line of TYPE."
14490   (cond
14491    ;; Convert to the local timezone.  We have to slap a
14492    ;; `condition-case' round the calls to the timezone
14493    ;; functions since they aren't particularly resistant to
14494    ;; buggy dates.
14495    ((eq type 'local)
14496     (concat "Date: " (condition-case ()
14497                          (timezone-make-date-arpa-standard date)
14498                        (error date))
14499             "\n"))
14500    ;; Convert to Universal Time.
14501    ((eq type 'ut)
14502     (concat "Date: "
14503             (condition-case ()
14504                 (timezone-make-date-arpa-standard date nil "UT")
14505               (error date))
14506             "\n"))
14507    ;; Get the original date from the article.
14508    ((eq type 'original)
14509     (concat "Date: " date "\n"))
14510    ;; Do an X-Sent lapsed format.
14511    ((eq type 'lapsed)
14512     ;; If the date is seriously mangled, the timezone
14513     ;; functions are liable to bug out, so we condition-case
14514     ;; the entire thing.
14515     (let* ((now (current-time))
14516            (real-time
14517             (condition-case ()
14518                 (gnus-time-minus
14519                  (gnus-encode-date
14520                   (timezone-make-date-arpa-standard
14521                    (current-time-string now)
14522                    (current-time-zone now) "UT"))
14523                  (gnus-encode-date
14524                   (timezone-make-date-arpa-standard
14525                    date nil "UT")))
14526               (error '(0 0))))
14527            (real-sec (+ (* (float (car real-time)) 65536)
14528                         (cadr real-time)))
14529            (sec (abs real-sec))
14530            num prev)
14531       (cond
14532        ((equal real-time '(0 0))
14533         "X-Sent: Unknown\n")
14534        ((zerop sec)
14535         "X-Sent: Now\n")
14536        (t
14537         (concat
14538          "X-Sent: "
14539          ;; This is a bit convoluted, but basically we go
14540          ;; through the time units for years, weeks, etc,
14541          ;; and divide things to see whether that results
14542          ;; in positive answers.
14543          (mapconcat
14544           (lambda (unit)
14545             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14546                 ;; The (remaining) seconds are too few to
14547                 ;; be divided into this time unit.
14548                 ""
14549               ;; It's big enough, so we output it.
14550               (setq sec (- sec (* num (cdr unit))))
14551               (prog1
14552                   (concat (if prev ", " "") (int-to-string
14553                                              (floor num))
14554                           " " (symbol-name (car unit)) 
14555                           (if (> num 1) "s" ""))
14556                 (setq prev t))))
14557           gnus-article-time-units "")
14558          ;; If dates are odd, then it might appear like the
14559          ;; article was sent in the future.
14560          (if (> real-sec 0)
14561              " ago\n"
14562            " in the future\n"))))))
14563    (t
14564     (error "Unknown conversion type: %s" type))))
14565
14566 (defun gnus-article-date-local (&optional highlight)
14567   "Convert the current article date to the local timezone."
14568   (interactive (list t))
14569   (gnus-article-date-ut 'local highlight))
14570
14571 (defun gnus-article-date-original (&optional highlight)
14572   "Convert the current article date to what it was originally.
14573 This is only useful if you have used some other date conversion
14574 function and want to see what the date was before converting."
14575   (interactive (list t))
14576   (gnus-article-date-ut 'original highlight))
14577
14578 (defun gnus-article-date-lapsed (&optional highlight)
14579   "Convert the current article date to time lapsed since it was sent."
14580   (interactive (list t))
14581   (gnus-article-date-ut 'lapsed highlight))
14582
14583 (defun gnus-article-maybe-highlight ()
14584   "Do some article highlighting if `gnus-visual' is non-nil."
14585   (if (gnus-visual-p 'article-highlight 'highlight)
14586       (gnus-article-highlight-some)))
14587
14588 ;;; Article savers.
14589
14590 (defun gnus-output-to-rmail (file-name)
14591   "Append the current article to an Rmail file named FILE-NAME."
14592   (require 'rmail)
14593   ;; Most of these codes are borrowed from rmailout.el.
14594   (setq file-name (expand-file-name file-name))
14595   (setq rmail-default-rmail-file file-name)
14596   (let ((artbuf (current-buffer))
14597         (tmpbuf (get-buffer-create " *Gnus-output*")))
14598     (save-excursion
14599       (or (get-file-buffer file-name)
14600           (file-exists-p file-name)
14601           (if (gnus-yes-or-no-p
14602                (concat "\"" file-name "\" does not exist, create it? "))
14603               (let ((file-buffer (create-file-buffer file-name)))
14604                 (save-excursion
14605                   (set-buffer file-buffer)
14606                   (rmail-insert-rmail-file-header)
14607                   (let ((require-final-newline nil))
14608                     (write-region (point-min) (point-max) file-name t 1)))
14609                 (kill-buffer file-buffer))
14610             (error "Output file does not exist")))
14611       (set-buffer tmpbuf)
14612       (buffer-disable-undo (current-buffer))
14613       (erase-buffer)
14614       (insert-buffer-substring artbuf)
14615       (gnus-convert-article-to-rmail)
14616       ;; Decide whether to append to a file or to an Emacs buffer.
14617       (let ((outbuf (get-file-buffer file-name)))
14618         (if (not outbuf)
14619             (append-to-file (point-min) (point-max) file-name)
14620           ;; File has been visited, in buffer OUTBUF.
14621           (set-buffer outbuf)
14622           (let ((buffer-read-only nil)
14623                 (msg (and (boundp 'rmail-current-message)
14624                           (symbol-value 'rmail-current-message))))
14625             ;; If MSG is non-nil, buffer is in RMAIL mode.
14626             (if msg
14627                 (progn (widen)
14628                        (narrow-to-region (point-max) (point-max))))
14629             (insert-buffer-substring tmpbuf)
14630             (if msg
14631                 (progn
14632                   (goto-char (point-min))
14633                   (widen)
14634                   (search-backward "\^_")
14635                   (narrow-to-region (point) (point-max))
14636                   (goto-char (1+ (point-min)))
14637                   (rmail-count-new-messages t)
14638                   (rmail-show-message msg)))))))
14639     (kill-buffer tmpbuf)))
14640
14641 (defun gnus-output-to-file (file-name)
14642   "Append the current article to a file named FILE-NAME."
14643   (let ((artbuf (current-buffer)))
14644     (nnheader-temp-write nil
14645       (insert-buffer-substring artbuf)
14646       ;; Append newline at end of the buffer as separator, and then
14647       ;; save it to file.
14648       (goto-char (point-max))
14649       (insert "\n")
14650       (append-to-file (point-min) (point-max) file-name))))
14651
14652 (defun gnus-convert-article-to-rmail ()
14653   "Convert article in current buffer to Rmail message format."
14654   (let ((buffer-read-only nil))
14655     ;; Convert article directly into Babyl format.
14656     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14657     (goto-char (point-min))
14658     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14659     (while (search-forward "\n\^_" nil t) ;single char
14660       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14661     (goto-char (point-max))
14662     (insert "\^_")))
14663
14664 (defun gnus-narrow-to-page (&optional arg)
14665   "Narrow the article buffer to a page.
14666 If given a numerical ARG, move forward ARG pages."
14667   (interactive "P")
14668   (setq arg (if arg (prefix-numeric-value arg) 0))
14669   (save-excursion
14670     (set-buffer gnus-article-buffer)
14671     (goto-char (point-min))
14672     (widen)
14673     (when (gnus-visual-p 'page-marker)
14674       (let ((buffer-read-only nil))
14675         (gnus-remove-text-with-property 'gnus-prev)
14676         (gnus-remove-text-with-property 'gnus-next)))
14677     (when
14678         (cond ((< arg 0)
14679                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14680               ((> arg 0)
14681                (re-search-forward page-delimiter nil 'move arg)))
14682       (goto-char (match-end 0)))
14683     (narrow-to-region
14684      (point)
14685      (if (re-search-forward page-delimiter nil 'move)
14686          (match-beginning 0)
14687        (point)))
14688     (when (and (gnus-visual-p 'page-marker)
14689                (not (= (point-min) 1)))
14690       (save-excursion
14691         (goto-char (point-min))
14692         (gnus-insert-prev-page-button)))
14693     (when (and (gnus-visual-p 'page-marker)
14694                (not (= (1- (point-max)) (buffer-size))))
14695       (save-excursion
14696         (goto-char (point-max))
14697         (gnus-insert-next-page-button)))))
14698
14699 ;; Article mode commands
14700
14701 (defun gnus-article-goto-next-page ()
14702   "Show the next page of the article."
14703   (interactive)
14704   (when (gnus-article-next-page)
14705     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14706
14707 (defun gnus-article-goto-prev-page ()
14708   "Show the next page of the article."
14709   (interactive)
14710   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14711     (gnus-article-prev-page nil)))
14712
14713 (defun gnus-article-next-page (&optional lines)
14714   "Show the next page of the current article.
14715 If end of article, return non-nil.  Otherwise return nil.
14716 Argument LINES specifies lines to be scrolled up."
14717   (interactive "p")
14718   (move-to-window-line -1)
14719   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14720   (if (save-excursion
14721         (end-of-line)
14722         (and (pos-visible-in-window-p)  ;Not continuation line.
14723              (eobp)))
14724       ;; Nothing in this page.
14725       (if (or (not gnus-break-pages)
14726               (save-excursion
14727                 (save-restriction
14728                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14729           t                             ;Nothing more.
14730         (gnus-narrow-to-page 1)         ;Go to next page.
14731         nil)
14732     ;; More in this page.
14733     (condition-case ()
14734         (scroll-up lines)
14735       (end-of-buffer
14736        ;; Long lines may cause an end-of-buffer error.
14737        (goto-char (point-max))))
14738     (move-to-window-line 0)
14739     nil))
14740
14741 (defun gnus-article-prev-page (&optional lines)
14742   "Show previous page of current article.
14743 Argument LINES specifies lines to be scrolled down."
14744   (interactive "p")
14745   (move-to-window-line 0)
14746   (if (and gnus-break-pages
14747            (bobp)
14748            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14749       (progn
14750         (gnus-narrow-to-page -1)        ;Go to previous page.
14751         (goto-char (point-max))
14752         (recenter -1))
14753     (prog1
14754         (condition-case ()
14755             (scroll-down lines)
14756           (error nil))
14757       (move-to-window-line 0))))
14758
14759 (defun gnus-article-refer-article ()
14760   "Read article specified by message-id around point."
14761   (interactive)
14762   (let ((point (point)))
14763     (search-forward ">" nil t)          ;Move point to end of "<....>".
14764     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14765         (let ((message-id (match-string 1)))
14766           (goto-char point)
14767           (set-buffer gnus-summary-buffer)
14768           (gnus-summary-refer-article message-id))
14769       (goto-char (point))
14770       (error "No references around point"))))
14771
14772 (defun gnus-article-show-summary ()
14773   "Reconfigure windows to show summary buffer."
14774   (interactive)
14775   (gnus-configure-windows 'article)
14776   (gnus-summary-goto-subject gnus-current-article))
14777
14778 (defun gnus-article-describe-briefly ()
14779   "Describe article mode commands briefly."
14780   (interactive)
14781   (gnus-message 6
14782                 (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")))
14783
14784 (defun gnus-article-summary-command ()
14785   "Execute the last keystroke in the summary buffer."
14786   (interactive)
14787   (let ((obuf (current-buffer))
14788         (owin (current-window-configuration))
14789         func)
14790     (switch-to-buffer gnus-summary-buffer 'norecord)
14791     (setq func (lookup-key (current-local-map) (this-command-keys)))
14792     (call-interactively func)
14793     (set-buffer obuf)
14794     (set-window-configuration owin)
14795     (set-window-point (get-buffer-window (current-buffer)) (point))))
14796
14797 (defun gnus-article-summary-command-nosave ()
14798   "Execute the last keystroke in the summary buffer."
14799   (interactive)
14800   (let (func)
14801     (pop-to-buffer gnus-summary-buffer 'norecord)
14802     (setq func (lookup-key (current-local-map) (this-command-keys)))
14803     (call-interactively func)))
14804
14805 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14806   "Read a summary buffer key sequence and execute it from the article buffer."
14807   (interactive "P")
14808   (let ((nosaves
14809          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14810            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14811            "=" "^" "\M-^" "|"))
14812         keys)
14813     (save-excursion
14814       (set-buffer gnus-summary-buffer)
14815       (push (or key last-command-event) unread-command-events)
14816       (setq keys (read-key-sequence nil)))
14817     (message "")
14818
14819     (if (member keys nosaves)
14820         (let (func)
14821           (pop-to-buffer gnus-summary-buffer 'norecord)
14822           (if (setq func (lookup-key (current-local-map) keys))
14823               (call-interactively func)
14824             (ding)))
14825       (let ((obuf (current-buffer))
14826             (owin (current-window-configuration))
14827             (opoint (point))
14828             func in-buffer)
14829         (if not-restore-window
14830             (pop-to-buffer gnus-summary-buffer 'norecord)
14831           (switch-to-buffer gnus-summary-buffer 'norecord))
14832         (setq in-buffer (current-buffer))
14833         (if (setq func (lookup-key (current-local-map) keys))
14834             (call-interactively func)
14835           (ding))
14836         (when (eq in-buffer (current-buffer))
14837           (set-buffer obuf)
14838           (unless not-restore-window
14839             (set-window-configuration owin))
14840           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14841
14842 \f
14843 ;;;
14844 ;;; Kill file handling.
14845 ;;;
14846
14847 ;;;###autoload
14848 (defalias 'gnus-batch-kill 'gnus-batch-score)
14849 ;;;###autoload
14850 (defun gnus-batch-score ()
14851   "Run batched scoring.
14852 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14853 Newsgroups is a list of strings in Bnews format.  If you want to score
14854 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14855 score the alt hierarchy, you'd say \"!alt.all\"."
14856   (interactive)
14857   (let* ((yes-and-no
14858           (gnus-newsrc-parse-options
14859            (apply (function concat)
14860                   (mapcar (lambda (g) (concat g " "))
14861                           command-line-args-left))))
14862          (gnus-expert-user t)
14863          (nnmail-spool-file nil)
14864          (gnus-use-dribble-file nil)
14865          (yes (car yes-and-no))
14866          (no (cdr yes-and-no))
14867          group newsrc entry
14868          ;; Disable verbose message.
14869          gnus-novice-user gnus-large-newsgroup)
14870     ;; Eat all arguments.
14871     (setq command-line-args-left nil)
14872     ;; Start Gnus.
14873     (gnus)
14874     ;; Apply kills to specified newsgroups in command line arguments.
14875     (setq newsrc (cdr gnus-newsrc-alist))
14876     (while newsrc
14877       (setq group (caar newsrc))
14878       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14879       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14880                (and (car entry)
14881                     (or (eq (car entry) t)
14882                         (not (zerop (car entry)))))
14883                (if yes (string-match yes group) t)
14884                (or (null no) (not (string-match no group))))
14885           (progn
14886             (gnus-summary-read-group group nil t nil t)
14887             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14888                  (gnus-summary-exit))))
14889       (setq newsrc (cdr newsrc)))
14890     ;; Exit Emacs.
14891     (switch-to-buffer gnus-group-buffer)
14892     (gnus-group-save-newsrc)))
14893
14894 (defun gnus-apply-kill-file ()
14895   "Apply a kill file to the current newsgroup.
14896 Returns the number of articles marked as read."
14897   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14898           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14899       (gnus-apply-kill-file-internal)
14900     0))
14901
14902 (defun gnus-kill-save-kill-buffer ()
14903   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14904     (when (get-file-buffer file)
14905       (save-excursion
14906         (set-buffer (get-file-buffer file))
14907         (and (buffer-modified-p) (save-buffer))
14908         (kill-buffer (current-buffer))))))
14909
14910 (defvar gnus-kill-file-name "KILL"
14911   "Suffix of the kill files.")
14912
14913 (defun gnus-newsgroup-kill-file (newsgroup)
14914   "Return the name of a kill file name for NEWSGROUP.
14915 If NEWSGROUP is nil, return the global kill file name instead."
14916   (cond 
14917    ;; The global KILL file is placed at top of the directory.
14918    ((or (null newsgroup)
14919         (string-equal newsgroup ""))
14920     (expand-file-name gnus-kill-file-name
14921                       gnus-kill-files-directory))
14922    ;; Append ".KILL" to newsgroup name.
14923    ((gnus-use-long-file-name 'not-kill)
14924     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14925                               "." gnus-kill-file-name)
14926                       gnus-kill-files-directory))
14927    ;; Place "KILL" under the hierarchical directory.
14928    (t
14929     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14930                               "/" gnus-kill-file-name)
14931                       gnus-kill-files-directory))))
14932
14933 \f
14934 ;;;
14935 ;;; Dribble file
14936 ;;;
14937
14938 (defvar gnus-dribble-ignore nil)
14939 (defvar gnus-dribble-eval-file nil)
14940
14941 (defun gnus-dribble-file-name ()
14942   "Return the dribble file for the current .newsrc."
14943   (concat
14944    (if gnus-dribble-directory
14945        (concat (file-name-as-directory gnus-dribble-directory)
14946                (file-name-nondirectory gnus-current-startup-file))
14947      gnus-current-startup-file)
14948    "-dribble"))
14949
14950 (defun gnus-dribble-enter (string)
14951   "Enter STRING into the dribble buffer."
14952   (if (and (not gnus-dribble-ignore)
14953            gnus-dribble-buffer
14954            (buffer-name gnus-dribble-buffer))
14955       (let ((obuf (current-buffer)))
14956         (set-buffer gnus-dribble-buffer)
14957         (insert string "\n")
14958         (set-window-point (get-buffer-window (current-buffer)) (point-max))
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